Go Down

Topic: RXLED/TXLED as input pins (Read 692 times) previous topic - next topic

LMagalhaes

Hi there,

I've designed a custom board using the ATSAMD21G18 and needed to use these two pins as inputs. I've seen the variant file and it has this:

Code: [Select]
// 25..26 - RX/TX LEDS (PB03/PA27)
  // --------------------
  { PORTB,  3, PIO_OUTPUT, PIN_ATTR_DIGITAL, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE }, // used as output only
  { PORTA, 27, PIO_OUTPUT, PIN_ATTR_DIGITAL, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE }, // used as output only


So they're declared as output only. If I change it to


Code: [Select]
// 25..26 - RX/TX LEDS (PB03/PA27)
  // --------------------
  { PORTB,  3, PIO_DIGITAL, PIN_ATTR_DIGITAL, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE },
  { PORTA, 27, PIO_DIGITAL, PIN_ATTR_DIGITAL, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE },


Would they be able to be used as input pins ? Has anyone tried it yet? I didn't get my custom hardware yet but I'm already developing the firmware and wanted to solve this issue asap.

Best regards

MartinL

#1
Aug 27, 2019, 07:02 pm Last Edit: Aug 27, 2019, 07:03 pm by MartinL
Hi LMagalhaes,

Yes, it's possible, however by default the TX and RX leds are used by both the Zero's bootloader and USB core code to indicate activity on the USB port.

To disable LED activity in the Zero's bootloader, it's necessary to comment out the BOARD_LEDTX_PORT and BOARD_LEDRX_PORT definitions in the bootloader's "board_definitions_arduino_zero.h" file. This prevents LEDs from being turned on and off in the bootloader's "board_LED_driver.h" file. The bootloader code needs to be edited, compiled and uploaded using a device programmer via the SWD port.

To disable LED activity in the Zero's core files, it's necessary to comment out the PIN_LED_TXL and PIN_LED_RXL definitions in the Zero's "variant.h" file. Again this prevents the LEDs from being turned on and off, but this time in the USB core file named "USBCore.cpp".

With the change you suggest in the "variant.cpp" file, it should be possible to simply set up the pins as inputs and read them using digitalRead() in your sketch.

LMagalhaes

But even if the pins are used by the bootloader, does it matter during runtime ? I only need to check them in the beggining of the program to detect the presence of external hardware. I could also add a wait on the beggining of the program to ensure that the bootloader doesn't leave the pins in an unknown state before reading them. Wouldn't that be enough?

Also, changing the variant file requires the rebuilding of the bootloader ?

MartinL

Whether you need to modify the bootloader code or not, depends on if it actively drives the LEDs. Earlier versions of the Arduino Zero bootloader didn't drive the LEDs at all, newer versions probably do and might require a small modification to the bootloader to disable them.

It's just about avoiding a situation where the attached device and microcontroller, (running either bootloader or core code), are simultaneously trying to drive the LED pins at the same time.

The variant files are used to map the Arduino board IO during runtime and aren't associated with the bootloader.

Go Up