Samd21 build help - handling pin definition for boards

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
MCHobby
Posts: 51
Joined: Mon Jan 26, 2015 2:05 pm
Contact:

Samd21 build help - handling pin definition for boards

Post by MCHobby » Sat May 01, 2021 11:40 pm

I'm working on the Samd21 version of MicroPython.
I can blink the onboard LED of the Trinket M0 from MicroPython :D

I will need some help to organize the PIN declaration for multiple SAMD21 board (Trinket M0, Itsy Bitsy M0, Feather M0, Gemma M0).

Primilarily work

Presently that LED is assigned to PA10, so I defined a primilary machine_pin_obj (in machine_pin.c) with pins for 0 to 10 corresponding to PA00 to PA10. This definition was done for test/development purpose.

Code: Select all

STATIC const machine_pin_obj_t machine_pin_obj[] = {
    {{&machine_pin_type}, PIN_PA00, 0x00 | 0},
    {{&machine_pin_type}, PIN_PA01, 0x00 | 1},
    {{&machine_pin_type}, PIN_PA02, 0x00 | 2},
    {{&machine_pin_type}, PIN_PA03, 0x00 | 3},
    {{&machine_pin_type}, PIN_PA04, 0x00 | 4},
    {{&machine_pin_type}, PIN_PA05, 0x00 | 5},
    {{&machine_pin_type}, PIN_PA06, 0x00 | 6},
    {{&machine_pin_type}, PIN_PA07, 0x00 | 7},
    {{&machine_pin_type}, PIN_PA08, 0x00 | 8},
    {{&machine_pin_type}, PIN_PA09, 0x00 | 9},
    {{&machine_pin_type}, PIN_PA10, 0x00 | 10},
    {{&machine_pin_type}, PIN_PA11, 0x00 | 11},
    {{NULL}, -1, -1},
    {{NULL}, -1, -1},
    {{&machine_pin_type}, PIN_PA14, 0x00 | 14},
    {{&machine_pin_type}, PIN_PA15, 0x00 | 15},
    {{&machine_pin_type}, PIN_PA16, 0x00 | 16},
    {{&machine_pin_type}, PIN_PA17, 0x00 | 17},
    {{&machine_pin_type}, PIN_PA18, 0x00 | 18},
    {{&machine_pin_type}, PIN_PA19, 0x00 | 19},
    {{NULL}, -1, -1},
    {{NULL}, -1, -1},
    {{&machine_pin_type}, PIN_PA22, 0x00 | 22},
    {{&machine_pin_type}, PIN_PA23, 0x00 | 23},
    {{&machine_pin_type}, PIN_PA24, 0x00 | 24},
    {{&machine_pin_type}, PIN_PA25, 0x00 | 25},
    {{NULL}, -1, -1},
    {{&machine_pin_type}, PIN_PA27, 0x00 | 27},
    {{&machine_pin_type}, PIN_PA28, 0x00 | 28},
    {{NULL}, -1, -1},
    {{&machine_pin_type}, PIN_PA30, 0x00 | 30},
    {{&machine_pin_type}, PIN_PA31, 0x00 | 31},
};
So, to change the LED state, I do this in MicroPython.

Code: Select all

from machine import Pin
p = Pin( 10, Pin.OUT )  # presently match PA10
p.value( 1 ) # light up the LED
Definition for Trinket M0

However, on the Trinket M0, the LED (wired to PA10) is labelled as Pin #13.
Sélection_056.jpg
Sélection_056.jpg (55.36 KiB) Viewed 1222 times
So the machine_pin_obj should be deifined in such a way to have the 13th position associated to PIN_PA10.
This will ONLY covers the Trinket M0 use-case. The feather M0 or ItsyBitsy M0 or Gemma M0 will have a different definition of machine_pin_obj because of different MCU pin assignment on the board.

My current approach
My current approach will be add a #define HW_BOARD_ADAFRUIT_TRINKET_M0 in the boards/ADAFRUIT_TRINKET_M0/mpconfigboard.h file.

Then a #ifdef HW_BOARD_ADAFRUIT_TRINKET_M0 will be used to defined the proper machine_pin_obj at compile time in the machine_pin.h .

Is there a better way?

It should be a better MicroPython way of doing my pin assignment.
If someone could advice, that would be really great!
Cheers,
Dominique

MCHobby
Posts: 51
Joined: Mon Jan 26, 2015 2:05 pm
Contact:

Re: Samd21 build help - handling pin definition for boards

Post by MCHobby » Sun May 02, 2021 12:14 pm

Damned stupid question!

Just move the machine_pin.h with pin definition into the /ports/samd/ADAFRUIT_TRINKET_M0/ .

So each board can have a customized pin definition.

scruss
Posts: 70
Joined: Sat Aug 12, 2017 2:27 pm

Re: Samd21 build help - handling pin definition for boards

Post by scruss » Sun May 02, 2021 3:07 pm

The Pin 13 definition is left over from Arduino days, when pin 13 was always the LED pin

Post Reply