UART (Pin Alternate Functions)

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
mpython
Posts: 4
Joined: Sat Nov 14, 2020 7:32 pm

UART (Pin Alternate Functions)

Post by mpython » Sat Nov 14, 2020 7:48 pm

I am trying to reconfigure UART to new pins on Nucleo F429ZI board.

And followings code doesn't work.
pd0=Pin('D0')
>>> pd1=Pin('D1')
>>> pd0.init(mode=machine.Pin.ALT, alt=machine.Pin.AF7_USART2)
>>> pd1.init(mode=machine.Pin.ALT, alt=machine.Pin.AF7_USART2)
>>> pd1.af()
7
>>> pd0.af()
7
>>> from machine import UART
>>> uart=UART(2,9600)
>>> uart.write ('Ahoj')
4

Where can I found Pin alternate functions for all pins?

Thanks for your response.

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

Re: UART (Pin Alternate Functions)

Post by jimmo » Sun Nov 15, 2020 4:28 am

mpython wrote:
Sat Nov 14, 2020 7:48 pm
I am trying to reconfigure UART to new pins on Nucleo F429ZI board.
This code looks right (although might be worth trying setting the pin AFs after initialising the UART, but I don't think that's it).

One thing to check... these ST Nucleo boards are very confusing because the pins are simultaneously laid out for Arduino, Morpho and ST pin naming.

So when you say "D0", do you mean the "D0" Arduino pin, or PD0 (i.e. port D, pin 0). Looking at pins.csv for this board, it doesn't appear that MicroPython knows about the Arduino names, so I suspect you're getting the latter. However, looking at the datasheet, PD0/PD1 don't have USART2 as alternate functions:
USART2_TX: PA2, PD5
USART2_RX: PA3, PD6

PD6 and PD5 are available on the header, they're shown in the schematic at USART_B_RX/USART_B_TX. PA2 and PA3 are on the morpho connector.

mpython
Posts: 4
Joined: Sat Nov 14, 2020 7:32 pm

Re: UART (Pin Alternate Functions)

Post by mpython » Wed Nov 18, 2020 8:18 pm

OK, I have to really call the stm32 pins.
So, when some pins don't have the alternate function, which is in the: Pin.af_list().
I can't add a new function to the pins?

User avatar
dhylands
Posts: 3592
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: UART (Pin Alternate Functions)

Post by dhylands » Wed Nov 18, 2020 9:03 pm

mpython wrote:
Wed Nov 18, 2020 8:18 pm
OK, I have to really call the stm32 pins.
So, when some pins don't have the alternate function, which is in the: Pin.af_list().
I can't add a new function to the pins?
No. The AF functions are hard-coded in the hardware. You can find the detailed mapping in the datasheet for the particular processor you're using.

For example, with the STM32F405 used in the pyboard: https://www.st.com/resource/en/datasheet/dm00037051.pdf If you look at Tabele 9 (Alternate function mapping) (on page 62) it gives the full alternate functions for each pin.

The Pin.af_list shows a subset of those, as it only shows the af's which are supported. For example you won't see the DCMI pins shown in the af_list for Pin A4 since micropython doesn't currently have any DCMI support.

This file: https://github.com/micropython/micropyt ... 405_af.csv is the actual file that MicroPython uses, and it was extracted from the datasheet. There are occasionally some transcription errors.

This is the list of "supported" peripherals: https://github.com/micropython/micropyt ... ins.py#L11

The make-pins.py parses the _af.csv file aloing with the pins.csv file for a particular board to produce the set of pins and af which can be seen from within micropython.

mpython
Posts: 4
Joined: Sat Nov 14, 2020 7:32 pm

Re: UART (Pin Alternate Functions)

Post by mpython » Sat Dec 05, 2020 10:59 pm

Thank you for your response.
Now I can write to UART, but not read. Where could be the problem?

Code: Select all

>>> from machine import UART
>>> uart=UART(2,9600)
>>> uart.write ('444')
3
>>> response=uart.read(5)
>>> print (response)
None

Post Reply