Arduino SAMD Variants Files

Hello,

I've been using the arduino mkr wifi1010 for the past month, in order to learn about it because I am structuring a product for a company I work with around it.

I'm in the process of designed a samd21 board, however, I have a question about how the variants files work.

First of all, in the variants.cpp file for the mkr, I can see that the lines of code set the function of each pin and in the variants.h file, a variable is assigned an external pin number that is printed on the board.

However, I see no lines of code that connect the microcontroller physcial pin/function to the variables in the variants.h which set the mkr board pin.

How does this work?

Could you write a quick explanation and or link a website/tutorial about this?

Thank you

-Jim

The Arduino pin number is the index of the g_APinDescription array in variant.cpp. variant.h is mostly just giving nice names to the pins so that universal code can be written.

So when you do something like this:

digitalWrite(LED_BUILTIN, HIGH);

You have the definition of LED_BUILTIN in variant.h:

#define PIN_LED     (6u)
#define LED_BUILTIN PIN_LED

On the MKR WiFi 1010, the onboard LED happens to be on Arduino pin 6. On other boards, it might be on a different pin, so the LED_BUILTIN macro allows us to write code that will always blink the onboard LED, no matter which board it's run on.

Now 6 is just an arbitrary identifier for one of the IO pins on the ATSAMD21G18 microcontroller. It doesn't necessarily have any relation to the physical pin number or port number. We just say "hey, to make things easy, let's call this pin '6'".

So there needs to be some way to translate from the Arduino pin number to the low level code required to manipulate that pin. This is what the g_APinDescription array in variant.cpp does. If you look at element 6 of the array:

 { PORTA, 20, PIO_DIGITAL, (PIN_ATTR_DIGITAL|PIN_ATTR_PWM|PIN_ATTR_TIMER_ALT), No_ADC_Channel, PWM0_CH6,   TCC0_CH6,     EXTERNAL_INT_4    },

you can see that Arduino pin 6 is port A, bit 20, with a bunch of other cool attributes.

jimtechstuff:
However, I see no lines of code that connect the microcontroller physcial pin/function to the variables in the variants.h which set the mkr board pin.

How does this work?

https://forum.arduino.cc/index.php?topic=677909.msg4562647#msg4562647

Thank you for your amazing reply @pert.

Could you answer one more question?

In the example that you used, in the array, I could see that it mentioned CH6, which I assume somehow refers to the PIN_LED (6u)?

However, what about the analog pin arrays? On line 98 in the variants.cpp, I can see that an analog pin on port A, bit 2 is set, however, the only other thing I see in the array is that it is on ADC channel 10, however, then looking in the variants.h starting on line 81, I see no mention of (10u) similar to the (6u) above...

How does this work? Why do only some line up? Also, what do the (15u) values and so on for the other analog pins mean and come from?

Also, in the other digital arrays for variants.cpp, I see that a lot of the channel names/numbers are mixed up? Where does this turn into the 1,2,3 arduino mkr pins?

I have a rough understanding, but there are several differences that confuse me... I would love to read your feedback!

Thanks

jimtechstuff:
Also, in the other digital arrays for variants.cpp, I see that a lot of the channel names/numbers are mixed up? Where does this turn into the 1,2,3 arduino mkr pins?

you missed it in Per's post. the pin is the index to access the array.

Thank you.

Could you explain that to me please???

I have only a basic knowledge of programming.
Thank you

Hello,

Nvm my last post. I think I figured it out. The problem was that I didn't know where the index of the array was in the file.

I just found it at the top.

Is this correct, if I change the order of the arrays in the variant.cpp file, for example, I place PB23 in front of PB22, would that mean that PB23 is now called 0 and PB22 is then called 1 in the software?

Also, If I make changes to variant.cpp, what do I have to do to save/upload it? Or can I just save the txt file and I will automatically work on my MKR1010?

Thank you so much for all your help! I think that you all are true tech heroes and I am extremely grateful.

jimtechstuff:
Hello,

Nvm my last post. I think I figured it out. The problem was that I didn't know where the index of the array was in the file.

I just found it at the top.

Is this correct, if I change the order of the arrays in the variant.cpp file, for example, I place PB23 in front of PB22, would that mean that PB23 is now called 0 and PB22 is then called 1 in the software?

Also, If I make changes to variant.cpp, what do I have to do to save/upload it? Or can I just save the txt file and I will automatically work on my MKR1010?

Thank you so much for all your help! I think that you all are true tech heroes and I am extremely grateful.

if you swaps this lines, pin labeled 0 will work as pin 1 in code and pin labeled 1 will be pin 0 in code.

why would you change the variant file? it describes the board.