digital inputs on Moteino M0

Started by Jim, December 04, 2020, 04:02:06 PM

Jim

Hi,
is it possible to use pin 12 (PA07) D9 as an analog input on the board without any modification of the definition file?

Felix

#1
Here is the definition:
{ PORTA,  7, PIO_TIMER, (PIN_ATTR_DIGITAL|PIN_ATTR_PWM|PIN_ATTR_TIMER), ADC_Channel7, PWM1_CH1, TCC1_CH1, EXTERNAL_INT_7 }, // TCC1/WO[1]

It's analog capable but not designated as such. So the short answer is no, not without changing the current definition or releasing a new version to add analog to those pins.
This arduino thread talks about the changes that would be required:
https://forum.arduino.cc/index.php?topic=363393.0

I should add that if you analogWrite() to a TIMER pin such as the one above, it will put out a PWM signal even if it's not defined as an analog pin.

Anyway I will consider adding analog to these pins in a future release, to allow such pins to be used as analog INPUTs as well.

Jim

Thanks Felix!
Is there any documentation or how do you know you need to put PWM1_CH1, TCC1_CH1 to PortA 7 ?
I would also like to use the pins on the USB port as digital outputs. I attached a USB cable to the board and can control two leds on PA24 & PA25.
However I'm wondering if I need to remove the USB definition in order not to turn on/off the leds on those lines?

my test sketch
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(28, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(28, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);                       // wait for a second
  digitalWrite(28, LOW);    // turn the LED off by making the voltage LOW
  delay(500);                       // wait for a second
}


my modified variant.cpp
  // 27..29 - USB
  // --------------------
/*
  { PORTA, 28, PIO_COM, PIN_ATTR_NONE, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE }, // USB Host enable
  { PORTA, 24, PIO_COM, PIN_ATTR_NONE, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE }, // USB/DM
  { PORTA, 25, PIO_COM, PIN_ATTR_NONE, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE }, // USB/DP
*/
  { PORTA, 28, PIO_DIGITAL, (PIN_ATTR_DIGITAL), No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE },
  { PORTA, 24, PIO_DIGITAL, (PIN_ATTR_DIGITAL), No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE },
  { PORTA, 25, PIO_DIGITAL, (PIN_ATTR_DIGITAL), No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE },

Felix

I'm not sure, I never tried something like that. To reflash the firmware you'd have to always manually double tap it into bootloader, and your external LEDs (if you only have LEDs maybe thats OK) would flash as USB is communicating. So ... touchy thing.

Maybe something like USBDevice.detach(); would work, but you'd have to check how pinMode and digitalWrite treat pins that are not GPIO defined, if that doesn't work then you might have to simply define them as GPIO as you indicated above.

Anyway you'd have to try, I don't really have great advice on hacking into the USB pins. But I thought the M0 has plenty of GPIO, if not there's the larger pin variants or SAMD51 as well if you're building something more serious.


Jim

Thanks for your reply, ok. Yes I uploaded the sketch directly with SWD and set the flags to use no bootloader.

In future SAMD51 is propably a good idea but I have here some Moteinos and would like to test how far I can go.

One other thing when I write Serial.print("test"); will it output it to USBSerial or to pin 37/38?
I'm not sure if I messed up the whole thing. When using Serial0 that it writes/reads from pin 37/38. I tried to change it so that the "normal" Serial prints to pins 37/38 but get some redefinition error in some USBapi-files :/