How to write programs for a custom SAMD21 board using Arduino IDE

Hello everyone,

I am designing a new board using ATSAMD21E17D processor. Originally the processor was ATSAMD21E17A, but since it was out of stock I switched it for "D" variant.

I have a J-link debugger and I was able to program a simple blink example to it using "Microchip Studio for AVR and SAM Devices", to see that the processor works fine. But I want to write my programs using Arduino IDE, because of the vast and diverse software libraries that are available. In order to do so, I feel that I have the following options:

  • Write my codes in Arduino IDE, export the binary file, and use the exported .bin to program the processor using Microchip Studio.
    I was unable to use this approach. When I export the binary (Sketch >> Export compiled Binary) , it will compile the code for selected board (Tools >> Board). None of the board that I know of use ATSAMD21E17D. There is Tau from Rabid Prototypes that use ATSAMD21E17A. I tried to upload the code for the simple blink example using a few boards such as Tau, MKRZERO, Zero, etc with no success. Microchip Studio can upload the code to the processor but nothing happens.

  • Write the program in Arduino IDE, then use Microchip Studio's feature: Create project from Arduino sketch
    I was unable to even import the simple Blink example to Microchip Studio. I saved the original Blink example on my computer, then in Microchip Studio I selected File>>New Project>>Create project from Arduino Sketch, I selected MKRZERO (and a few other boards), and I compiled the code (I did not even change the processor). I got 54 errors (see attached file).

Well, there were a couple of more approaches that I tried without success, but my question is already getting unusually long. So I will just mention them:

  • I was unable to upload the SAM-BA bootloader to my board.
  • I was unable to create a custom board matching my processor in Arduino IDE.

Any help is much appreciated, however buying another programmer is not an option for me.
Thanks and I am terribly sorry for the long post.

errors.pdf (97.8 KB)

Hi @meghdadi

I recently had a discussion about this on the forum.

For the the SMAD21E17D you'll need to burn a modified bootloader to account for the microcontroller's smaller memory compared with the SAMD21G18A used on SAMD21 Arduino boards. For this you'll require your J-Link programmer connected to the microcontroller's SWD port.

Here's a link to a zip file containing a minimal bootloader for the SAMD21E17D: Lower memory SAMD21 variant - #20 by MartinL.

After burning the bootloader you can remove the programmer.

You'll also need to change the Arduino IDE's BOSSSA executable, in order to upload sketches over the microcontroller's native USB port. To do this you'll need to swap Arduino's "bossac.exe", for one that's compatible with the SAMD21E17D. Here's a zip file containing "bossac.exe" and instructions for that: Lower memory SAMD21 variant - #37 by MartinL.

Thank you very much @MartinL, I will test your solution and let you know.

Hi @MartinL, I uploaded the bootloader in your zip file. After the bootloader was loaded to my device, I got the error "USB device not recognized" from windows, and the port submenu in Arduino IDE is grayed out.

Just to be sure, here is my procedure in Microchip Studio: Tools>>Device Programming>>Memories>>Flash (132KB). I load your bootloader and click on Program.

Since there are only 4 pins (Vcc, GND, D+, and D-), and I already ohm-checked them, I seriously doubt that it is a connection error.

Is there a fuse-bit that I need to change in the processor?

Maybe this is caused by my previous attempts to load "bad" bootloaders into my chip and now windows cannot recognize this. I will try to find another computer and connect it to that PC to see if it is recognized.

Thanks

@meghdadi Have you tried double tapping the reset button (two times in quick succession)?

This will normally place the board into bootloader mode. The bootloader's COM port should enumerate and be visible and selectable in the Arduino IDE (Tools->Port->COMx).

@meghdadi I presume that the bootloader upload was successful.

One fuse to check is the BOOTPROT or bootloader protection fuse. This should be (depending on your version of Microchip/Atmel Studio 7) either 0x7 or 0 bytes.

Arduino's bootloader doesn't set this fuse, other bootloaders such as Adafruit's UF2 does.

@MartinL , yes, the bootloader upload was successful.
The BOOTPROT fuse is set to zero bytes. The double reset gives the same "Device not recognized" error.

@MartinL , thanks to you I have advanced a step, on a fresh computer, the board is recognized as a Arduino Zero after uploading your bootloader. After replacing the bossac.exe with your file, I was also able to program the board (I selected Arduino Zero - Native USB Port) and I got the following message:

Atmel SMART device 0x10010094 found
Device : ATSAMD21E17D
Chip ID : 10010094
Version : v2.0 [Arduino:XYZP] Oct 2 2021 13:30:30
Address : 8192
Pages : 1920
Page Size : 64 bytes
Total Size : 120KB
Planes : 1
Lock Regions : 16
Locked : none
Security : false
Boot Flash : true
BOD : true
BOR : true
Arduino : FAST_CHIP_ERASE
Arduino : FAST_MULTI_PAGE_WRITE
Arduino : CAN_CHECKSUM_MEMORY_BUFFER
Erase flash
done in 0.444 seconds

Write 11440 bytes to flash (179 pages)
[==============================] 100% (179/179 pages)
done in 0.072 seconds

Verify 11440 bytes of flash with checksum.
Verify successful
done in 0.011 seconds
CPU reset.

However, the board disappears after that and the port sub-menu in Arduino is grayed out. The board is recognized again after double tapping the reset button, but my code does not seem to run. It is the simple Blink code that should turn A4 (which is pin PA05, i.e. pin number 6 of ATSAMD21E17D chip) on and off. But the pin seems to be floating and does not oscillate. Any suggestions?

@meghdadi Does your custom board have an external 32.768kHz crystal?

I configured the bootloader to operate crystalless using the microcontroller's own internal 32.768kHz oscillator. However, if the board selected in the Arduino IDE uses an external crystal (like the Arduino Zero), but your custom board doesn't have one, the sketch will stall immediately after upload.

@meghdadi To configure the Arduno Zero for crystalless operation, it's necessary to edit line 71 of the SAMD21 core's "boards.txt" file and add the compiler flag -DCRYSTALLESS:

arduino_zero_native.build.extra_flags=-DCRYSTALLESS -D__SAMD21G18A__ {build.usb_flags}

The boards.txt file can be found (on my PC) at:

C:\Users\Computer\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.11\

@meghdadi You might also want to change the memory configuration on lines 60 and 61 of the "boards.txt" file as well for 128kB flash and 16kB RAM:

arduino_zero_native.upload.maximum_size=131072
arduino_zero_native.upload.maximum_data_size=16384

There's also the memory configuration on lines 28 and 29 in the core code's "flash_with_bootloader.ld" linker script file, that can be set to 128kB flash, 16kB RAM:

MEMORY
{
  FLASH (rx) : ORIGIN = 0x00000000+0x2000, LENGTH = 0x00020000-0x2000 /* First 8KB used by bootloader */
  RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00004000
}

The linker script file can be found (again on my PC) here:

C:\Users\Computer\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.11\variants\arduino_zero\linker_scripts\gcc\

@MartinL, Are you a lifesaver or what :wink:
Thanks a lot. The new boards.txt and linker script did the trick. Big big thanks.

Just for anyone else running into same problem, be sure to go through the discussion from beginning.

@meghdadi Good to hear that your board's now working :grinning:.