Board pins vs MCU pins #18186
Replies: 6 comments 6 replies
-
|
I have now discovered the (undocumented?) |
Beta Was this translation helpful? Give feedback.
-
|
This is documented for the pyboard (and by extension, other STM32 boards) here: class Pin – control I/O pins. It's present in other ports, too, such as on the rp2: With RP2, the board and CPU mappings appear to be the same. I'd have expected the board mapping to contain physical pin numbers (1..40), but that doesn't seem to be the case |
Beta Was this translation helpful? Give feedback.
-
|
Oh, interesting, so it looks like the |
Beta Was this translation helpful? Give feedback.
-
|
I am using an STM32F411CEU6 board. I do not include the help() function in my build. Instead, I use the REPL autocomplete feature (i.e., pressing the TAB key). Here is what I have: >>> import pyb
>>> pyb.Pin.cpu.
A0 A1 A10 A11
A12 A13 A14 A15
A2 A3 A4 A5
A6 A7 A8 A9
B0 B1 B10 B12
B13 B14 B15 B2
B3 B4 B5 B6
B7 B8 B9 C13
C14 C15 H0 H1
>>> pyb.Pin.board.
A0 A1 A2 A3
A4 A5 F_CS F_MISO
F_MOSI F_SCK LED_BLUE OSC32_IN
OSC32_OUT PA0 PA1 PA10
PA11 PA12 PA15 PA2
PA3 PA4 PA5 PA6
PA7 PA8 PA9 PB0
PB1 PB10 PB12 PB13
PB14 PB15 PB2 PB3
PB4 PB5 PB6 PB7
PB8 PB9 PC13
8000
PC14
PC15 PH0 PH1 SW
SWCLK SWDIO USB_DM USB_DP
>>> pyb.Pin.board.PA0 == pyb.Pin.cpu.A0
True
>>> pyb.Pin.board.PB9 == pyb.Pin.cpu.B9
True
>>> pyb.Pin.board.A0 == pyb.Pin.cpu.A0
True
>>> pyb.Pin.board.LED_BLUE == pyb.Pin.cpu.C13
True
>>> led_on = pyb.Pin.cpu.C13.low
>>> led_off = pyb.Pin.board.LED_BLUE.high
>>> led_on() # blue LED on
>>> led_off() # blue LED off
>>> import machine
>>> machine.Pin('C13') == pyb.Pin.cpu.C13
True
>>> LED = 'C13'
>>> machine.Pin(LED) == pyb.Pin.cpu.C13 == pyb.Pin.board.LED_BLUE
TrueWhen working on STM32, I prefer to use |
Beta Was this translation helpful? Give feedback.
-
|
Maybe this is an appropriate place for a somewhat related question regarding advisable pin definitions. There can be very standardized boards across different cpu/mcu ports. In particular, the SEED studio XIAO line has 2x7 pin boards with lots of generic interfacing boards (compatible with all of them). The XIAO mcu boards all seem to have D0 to D10 GPIO pins (plus 5V, GND, 3.3V) accessible on the side (top/bottom) plus a seemingly (almost) equally standardized set of pads on the bottom (MTDO, MTDI, MTCK, MTMS plus a few not relevant to programmable GPIO). CPU/MCU-depending, these have aliases; for example, on microcontrollers with an ADC, "D0" is always also labelled "A0", and likewise for consecutive pins (but how many of them depends on the MCU). Also, XIAO boards have 0 to 4 GPIO-controllable LEDs (plus one for an integrated battery charger if there is one, or 5V power indicator if not). IMHO it would be very helpful to have consistent pin naming, especially since lots of projects will then be trivially portable across all or most XIAO boards, but MicroPython has gone a different way:
The situation is bound to get worse with existing (or likely future!) PRs being even less consistent:
So I wonder:
|
Beta Was this translation helpful? Give feedback.
-
|
You’re seeing pin-naming collisions because the board aliases (A0-A8, D0-D71) map to MCU names (e.g., PA4) in the firmware. The string names for MCU pins lose the leading “P”. So Pin('A4') ends up referencing the board alias instead of MCU pin PA4. The constructor Pin((port, pin)) raises a TypeError on the STM32 port in v1.26.0, so you can’t force access to MCU pins by tuple. That means you either use board names, or rebuild with changed aliases. You can patch pins.csv, remove the board alias, or enable a Pin.cpu namespace to restore direct MCU pin naming. For future enhancement, it’d be helpful if MicroPython warned on name collisions or provided cpu: vs board: prefix to disambiguate. Also, see this link for context on custom STM32 board design: https://www.pcbway.com/blog/PCB_Design_Tutorial/Tutorial__How_to_Design_Your_Own_Custom_STM32_Microcontroller_Board.html |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using an STM32 Nucleo board, and the board has a defined set of pins for analog inputs (A0-A8) and Digital IO (D0-D71). However, the MCU has pins labelled A0-A15, B0-B15, C0-C15, D0-D15, etc. If I want to access the MCU pin C3 I would do so using
machine.Pin('C4'). However, there is a naming collision between the board pins and MCU pins on A0-A8 and D0-D15. Micropython appears to preference the board pins, soPin('A4')is the same as (MCU pin)Pin('F5')and there is no direct way to access the MCU pin A4 (I would need to use the board pinPin('D24'))While these pins are defined in the boards
pins.csv, and the MCU pins all start withP(e.g.PA4), micropython strips the leading P. The docs say a pin can be creating using a tuple of(port, pin)(e.g.Pin((0,4))), this returns a TypeError on the STM32 port (as of micropython 1.26.0). It is not possible to positively specify an MCU pin (e.g.Pin('PA4'))So, this post is partly a cautionary tale, partly a question if this behaviour is appropriate (I would prefer some kind of error or warning if there are pin name collisions, or a mechanism to specify a board pin vs MCU pin).
The workarounds I see at the moment are to either use the board pins, or to change pins.csv to remove the board pin definitions
Beta Was this translation helpful? Give feedback.
All reactions