stm32 NUCLEO boards: pin name conflicts
On pyboard:
>>> import pyb
>>> pin = pyb.Pin('A2')
>>> pin
Pin(Pin.cpu.A2, mode=Pin.ALT, pull=Pin.PULL_UP, af=Pin.AF7_USART2)
On many NUCLEO boards:
>>> import pyb
>>> pin = pyb.Pin('A2')
>>> pin
Pin(Pin.cpu.A4, mode=Pin.ANALOG)
On NUCLEO boards (eg NUCLEO_F091RC or L476RG) 'A2' maps to cpu pin A4.
This is because pins.csv has a board pin entry 'A2' for the Arduino analog pin name. Quite a few of the Arduino pin names are the same as the STM32 port names.
I can think of 4 solutions:
- Remove the Arduino pin names from pins.csv.
- Prepend the Arduino name with something, eg connector name -
CN8_A2 - Require cpu pins to be entered with a P, eg
pyb.Pin('PA2'). - Require cpu pins to be entered with a
cpu.prefix, egpyb.Pin('cpu.A2').
Options 3 & 4 are most likely to break existing code, although this could be avoided by mapping the short names in the csv file, eg A2,PA2 or A2,cpu.A2, for non NUCLEO ports.
Option 3 may not work for all MCUs as some MCUs don't have a P, just A2, etc.
I am currently using option 2, ie make board pin names unique.
Any suggestions on the best fix?
Add support for selecting pin alternate functions from python.
Converts generted pins to use qstrs instead of string pointers.
This patch also adds the following functions:
pyb.Pin.names()
pyb.Pin.af_list()
dir(pyb.Pin.board) and dir(pyb.Pin.cpu) also produce useful results.
pyb.Pin now takes kw args.
pyb.Pin.str now prints more useful information about the pin
configuration.
I found the following functions in my boot.py to be useful:
def pins():
for pin_name in dir(pyb.Pin.board):
pin = pyb.Pin(pin_name)
print('{:10s} {:s}'.format(pin_name, str(pin)))
def af():
for pin_name in dir(pyb.Pin.board):
pin = pyb.Pin(pin_name)
print('{:10s} {:s}'.format(pin_name, str(pin.af_list())))