SAMD51-specific code

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
AlainB
 
Posts: 26
Joined: Sat Jul 07, 2018 7:28 am

SAMD51-specific code

Post by AlainB »

Hello,

Still learning about the use ItsyBitsy M4... Could you enlighten me some more on this:
  • 1. While working with (Genuino Uno and) Adafruit Feather M0 Express board, we used the ARDUINO_ARCH_SAMD C-preprocessor symbols for SAMD21-specific code. It appears that some of our code (e.g. code accessing timers) is in fact specific to SAMD21 and should be rewritten for SAMD51, but symbol ARDUINO_ARCH_SAMD is defined for both. What would be the symbol to user to detect specifically SAMD21 or SAMD51?
  • 2. For processor-specific code, we use symbol __SAMD21G18A__ with the Feather M0 Express. What would be the corresponding symbol for the ItsyBitsy M4 processor? I cannot find the exact reference of the processor in the Adafruit documentation (just ATSAMD51) and my guesses (__ATSAMD51G19A__? __ATSAMD51J20A__?) have been unsuccessful so far (although I tried brute force Google search as recommanded :-) )... See my "test program" below.
  • 3. I see that the Arduino Servo library currently (v1.1.8) does not support SAMD51 (this issue is well documented on https://github.com/arduino-libraries/Servo/issues/77 and compilation currently fails for ItsyBitsy M4 with a lot of errors). Is there an alternative available ?
Thanks in advance!

Code: Select all

#include "Arduino.h"

#ifdef ARDUINO_SAMD_FEATHER_M0_EXPRESS
#error Feather M0 express
#else
#error not "Feather M0 express"
#endif

#ifdef ARDUINO_ITSYBITSY_M4
#error M4
#else
#error "not M4"
#endif

#ifdef ARDUINO_ARCH_SAMD
#error ARDUINO_ARCH_SAMD
#else
#error "not ARDUINO_ARCH_SAMD"
#endif

#ifdef __SAMD21G18A__
#error "__SAMD21G18A__"
#else
#error "not __SAMD21G18A__"
#endif

#ifdef __ATSAMD51__
#error __ATSAMD51__
#else
#error "not __ATSAMD51__"
#endif

#ifdef __ATSAMD51G19A__
#error __ATSAMD51G19A__
#else
#error "not __ATSAMD51G19A__"
#endif

#ifdef __ATSAMD51J20A__
#error __ATSAMD51J20A__
#else
#error "not __ATSAMD51J20A__"
#endif
#ifdef ARDUINO_SAMD_ZERO
#error "ARDUINO_SAMD_ZERO"
#else
#error "not ARDUINO_SAMD_ZERO"
#endif
void setup() {}
void loop() {}
Result for Feather M0 Express:

Code: Select all

/Users/Alain/Documents/Arduino/cansat-asgard/personal/baudhuin/TestItsyBitsyM4/TestItsyBitsyM4.ino:6:2: error: #error Feather M0 express
 #error Feather M0 express
 #error "not M4"
 #error ARDUINO_ARCH_SAMD
 #error "__SAMD21G18A__"
 #error "not __ATSAMD51__"
 #error "not __ATSAMD51G19A__"
 #error "not __ATSAMD51J20A__"
 #error "ARDUINO_SAMD_ZERO"
Result for ItsyBitsy M4:

Code: Select all

 #error not "Feather M0 express"
 #error M4
 #error ARDUINO_ARCH_SAMD
 #error "not __SAMD21G18A__"
 #error "not __ATSAMD51__"
 #error "not __ATSAMD51G19A__"
 #error "not __ATSAMD51J20A__"
 #error "not ARDUINO_SAMD_ZERO"

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: SAMD51-specific code

Post by adafruit_support_mike »

AlainB wrote:What would be the symbol to user to detect specifically SAMD21 or SAMD51?
We only use the SAMD21 and SAMD51, so we tend to bundle all the code for those platforms together, with __SAMD51__ as the selector for non-SAMD21 code.
AlainB wrote:What would be the corresponding symbol for the ItsyBitsy M4 processor?
It’s also a SAMD51G. Do you have a specific need to treat the ItsyBitsy separately from any other SAMD51 board?
AlainB wrote: I see that the Arduino Servo library currently (v1.1.8) does not support SAMD51 (this issue is well documented on https://github.com/arduino-libraries/Servo/issues/77 and compilation currently fails for ItsyBitsy M4 with a lot of errors). Is there an alternative available ?
That looks like the Arduino Servo library (thank you for the link BTW, it looks like a good analysis of the problem).

We have our own servo libraries for both C++ and CircuitPython:

https://github.com/adafruit/?q=servo

User avatar
AlainB
 
Posts: 26
Joined: Sat Jul 07, 2018 7:28 am

Re: SAMD51-specific code

Post by AlainB »

Hello, thanks for your prompt support.
This is everything I need, indeed. As for your question:
Do you have a specific need to treat the ItsyBitsy separately from any other SAMD51 board?
I hope not, but I don't really know... We currently have SAMD-specific code we wrote for the feather M0 express (e.g Serco programming) , conditional to __SAMD21G18A__ and I was planning to make the M4 version conditional to the specific ItsyBitsy M4 processor. But you have a good point, I should make it conditional to SAMD, and discriminate between SAMD21 and SAMD51 with __SAMD51__, it would be much more portable to other boards as long as I don't need more specific code.

As for SAMD-specific code, the following configuration of a second serial port on pins 10-11 of the Feather M0 Express does not work with the ItsyBitsy M4. Do you by any chance have the equivalent available anywhere for M4 ?

Thanks in advance,
Alain

Code: Select all

class SercomSerial1 : public Uart {
 public:
    SercomSerial1();
    /** Initialize serial port */
    void begin(unsigned long baudRate);
    /** Obtain the TX pin number */
    byte getTX() { return tx;};
    /** Obtain the RX pin number */
    byte getRX() { return rx;};
 private:
    byte rx;
    byte tx;
 };

SercomSerial1::SercomSerial1()
  : Uart(&sercom1,  11, 10 ,  SERCOM_RX_PAD_0 , UART_TX_PAD_2), rx(11), tx(10) {};

void SercomSerial1::begin(unsigned long baudRate) 
{
  Uart::begin(baudRate);
  pinPeripheral(rx, PIO_SERCOM); // Pins 10/11 must be configured to use the Sercom Mux
  pinPeripheral(tx, PIO_SERCOM); // Warning: this cannot be done in the constructor!
}

// Interrupt request handler
void SERCOM1_Handler()
{
  Serial2.IrqHandler();
}

SercomSerial1 Serial2;

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: SAMD51-specific code

Post by adafruit_support_mike »

The SAMD51’s SERCOMs are different from the SAMD21 version.

The main difference is that the pins are bundled in groups of four called ‘IOSET’s. All of the pins for a SAMD51 SERCOM have to belong to the same IOSET, and some signals can only be assigned to a specific pin in the IOSET.

Those facts are helpfully located about 800 pages apart in the SAMD51 datashseet. IOSETs are listed in section 6.2.8 around page 36, and the ’only specific pins can do this’ thing only shows up under the register settings for SERCOM UART control register A on page 873.

The combinations aren’t trivial, so you really do have to read the relevant sections of the datasheet.

User avatar
AlainB
 
Posts: 26
Joined: Sat Jul 07, 2018 7:28 am

Re: SAMD51-specific code

Post by AlainB »

Mmmh, I can't wait to dig into that datasheet. Thanks for the pointers.
Still hoped for a "using-atsamd51-sercom-to-add-more-spi-i2c-serial-ports/creating-a-new-serial", similar to https://learn.adafruit.com/using-atsamd ... new-serial, before going into that journey... Too bad :-)

Where should I look for the default allocation of Sercoms in the ItsyBitsy M4 core? For the Feather M0, I found some references showing that Sercom0 = Serial1, Sercom1=free, Sercom2=Flash SPI1, Sercom3=I2C, Sercom4=SPI, sercom5=hw debug, but I don't see similar information anywhere for the ItsyBitsy M4 ?

Thanks for your help, already,
Alain.

PS: you have very busy sundays... :-)

User avatar
AlainB
 
Posts: 26
Joined: Sat Jul 07, 2018 7:28 am

Re: SAMD51-specific code

Post by AlainB »

I've made some progress in my research: according to variant.h/cpp files, Sercom1=SPI, Sercom2=I2C, Sercom3=Serial1.
Can you confirm whether Sercom 0, 4 an 5 are indeed available? I guess one of them could be used for the HW debug (if any on ItsyBitsy M4?)

On the other hand, https://forum.arduino.cc/t/samd51-multi ... d/659368/7 seems to imply that Sercom 3 should be available, which does not make sense for me...
What would be the final reference about the default Sercom configuration on IB M4 ?

Thanks in advance,
Alain

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: SAMD51-specific code

Post by adafruit_support_mike »

AlainB wrote:Where should I look for the default allocation of Sercoms in the ItsyBitsy M4 core?
In the SAMD core, they’re broken out as SERCOM.h and SERCOM.cpp:

https://github.com/adafruit/ArduinoCore ... o/SERCOM.h
https://github.com/adafruit/ArduinoCore ... SERCOM.cpp

For a specific board, the SERCOMs used for specific interfaces are defined in the variant.h file:

https://github.com/adafruit/ArduinoCore ... #L122-L160

and the hardware Serial interface is created at the bottom of variant.cpp:

https://github.com/adafruit/ArduinoCore ... t.cpp#L114

The SPI and I2C interfaces are created when you pull in the library, using pin info from variant.h and functions from SERCOM.cpp

User avatar
AlainB
 
Posts: 26
Joined: Sat Jul 07, 2018 7:28 am

Re: SAMD51-specific code

Post by AlainB »

Thanks a lot for your help, my Serial2 configuration is now operational!
Alain.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: SAMD51-specific code

Post by adafruit_support_mike »

Glad to hear you got it working. Happy hacking!

Locked
Please be positive and constructive with your questions and comments.

Return to “Itsy Bitsy Boards”