UART on alternate pins

The official PYBD running MicroPython, and its accessories.
Target audience: Users with a PYBD
Post Reply
incognico
Posts: 9
Joined: Tue Jun 05, 2018 4:51 am

UART on alternate pins

Post by incognico » Thu Nov 14, 2019 6:48 am

Hi all,

How would I go about using UART 3 on the AF pins D8 & D9...?

--Nick

chrismas9
Posts: 124
Joined: Wed Jun 25, 2014 10:07 am

Re: UART on alternate pins

Post by chrismas9 » Fri Nov 15, 2019 2:48 am

Edit the pin assignment in mpconfigboard.h for your port and rebuild MicroPython

incognico
Posts: 9
Joined: Tue Jun 05, 2018 4:51 am

Re: UART on alternate pins

Post by incognico » Fri Nov 15, 2019 3:01 am

Perfect, thanks!

User avatar
jimmo
Posts: 890
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: UART on alternate pins

Post by jimmo » Mon Nov 18, 2019 2:08 am

I haven't tested this, but I would imagine that you could also manually switch the pins into the UART AF mode?

Code: Select all

u = machine.UART(3)
machine.Pin.board.Y9.init(mode=machine.Pin.IN) # Reset default TX to hi-z
machine.Pin.board.Y10.init(mode=machine.Pin.IN) # Reset default RX to hi-z
machine.Pin.cpu.D8.init(mode=machine.Pin.ALT, alt=AF7_USART3) # TX af
machine.Pin.cpu.D9.init(mode=machine.Pin.ALT, alt=AF7_USART3) # RX af

incognico
Posts: 9
Joined: Tue Jun 05, 2018 4:51 am

Re: UART on alternate pins

Post by incognico » Fri Nov 22, 2019 9:41 am

Nice suggestion Jimmo, and I got a chance to have a play with this approach today.

In short, yes this works fine, you just have to make sure you clear the original pins of their alternate function after the uart is initialized. Your code snippet only created the uart and I was then initializing it to test after the pin reassignments. To future readers (or more likely, future me:) this doesn't work as the init() resets the function of the original pins and while TX will likely work, RX will not work.

Appreciate your suggestion, I got it working after the original posting by editing/building the firmware, but clearly this is easier!

For reference, working code on PYBD F6-

Code: Select all

from pyb import Pin, UART
u = UART(3,19200)
Pin.board.Y9.init(mode=Pin.IN) # Reset default TX to hi-z
Pin.board.Y10.init(mode=Pin.IN) # Reset default RX to hi-z
Pin.cpu.D8.init(mode=Pin.ALT, pull=Pin.PULL_UP, alt=Pin.AF7_USART3) # TX (settings copied from init() of default pins)
Pin.cpu.D9.init(mode=Pin.ALT, pull=Pin.PULL_UP, alt=Pin.AF7_USART3) # RX

Post Reply