Atmel SAMD21 Bootloader Issues

Hi there, I'm having issues bootloading a SAMD21G15 Chip. The bootloaders given by Arduino are for the SAMD21G18 Variant and therefore expect to see 32Kb of bootloader RAM, whereas my SAMD21G15 Chip only has 4Kb I believe? I can't seem to find a bootloader for the chip I have, any help will very much appreciated.

Hi @Callum_Austin11

You might like to check out this thread on the Arduino forum, it's for the SAMD21E17D, but the steps are the same: https://forum.arduino.cc/t/how-to-write-programs-for-a-custom-samd21-board-using-arduino-ide/914110.

You'll need to:

  1. Compile and build new bootloader for the SAMD21G15A, to account for the reduced memory footprint and upload this using a programmer.
  2. Replace the bossac.exe file (that uploads the code on to your microcontroller) for one that's compatible with your device.
  3. Make few changes to the "boards.txt" file and "flash_with_bootloader.ld" linker script in the Arduino SAMD core code.

If you require, I can compile and build you a bootloader binary that's compatible with your SAMD21G15A. I just need to know whether your custom board uses an external 32.768kHz crystal or not, and also which Status, Tx and Rx LEDs you're using, if any.

1 Like

Hi Martin, thanks for the reply. I believe I've managed to change the boards.txt and flash_with_bootloader.id files in the linker script folder. Stuck with replacing the bossac.exe file. Not sure how I do that.

Got that link, however not sure where the bossac.exe bit is

There is an external crystal on my board thats 32.768KHz, however, i will need SPI which needs to be ran at 10MHz. It shouldn't be an issue using the external crystal should it?

Hi @Callum_Austin11

Here are the instructions for the bossac.exe file: https://forum.arduino.cc/t/lower-memory-samd21-variant/890009/37.

If your board has an external crystal then the bootloader can be set to either use it, or in its absence the SAMD21's own on-chip internal 32.768kHz oscillator. If you've got an external crystal, the bootloader can be set to run with it.

On ARM microcontrollers the SPI port doesn't have any involvement with regard to the bootloader or the programmer used to upload it.

Thanks a lot, managed to get bossac installed, I am now having trouble with the bootloader. I believe I have managed to bootload it through Atmel studio. I have now plugged in a usb to the chip however when I go to upload a program through arduino IDE it disconnects. Only way to get it back up is if I Upload a program through the EDBG Chip I have externally. I may have done the bootloader wrong, any ideas? Or if possible, do you have a bootloader I can download and test on the chip. My system was prototyped using the MKR Zero so if I could use that bootloader with pin descriptions, that would be good. Many thanks

Hi @Callum_Austin11

To account for the SAMD21G15A smaller memory footprint, you need to change the bootloader's linker script "bootloader_samd21x18.ld" file RAM memory definitions on line 30, (I've modified it for the G15A's 4KB RAM):

MEMORY
{
  FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x2000 /* First 8KB used by bootloader */
  RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00001000-0x0004 /* last 4 bytes used by bootloader to keep data between resets, but reserves 1024 bytes for sketches to have same possibility */
}

It's also necessary to change the address of the "Magic Number" used for the double tap reset functionality (that can recover the board whenever a sketch crashes), since this is stored in the last 4-bytes in RAM.

Edit the "board_definitions_arduino_zero.h" file and change the Magic Number's address (again I've modified it for your target):

#define BOOT_DOUBLE_TAP_ADDRESS           (0x20000FFCul)
#define BOOT_DOUBLE_TAP_DATA              (*((volatile uint32_t *) BOOT_DOUBLE_TAP_ADDRESS))

In the same file, comment out if not used, or modify the status, tx and rx leds, the numbers represent the microcontroller's port pin numbers, for example (0) & (28) is PORTA pin 28:

/*
 * LEDs definitions
 */
#define BOARD_LED_PORT                    (0)
#define BOARD_LED_PIN                     (28)

//#define BOARD_LEDRX_PORT                  (1)
//#define BOARD_LEDRX_PIN                   (3)

//#define BOARD_LEDTX_PORT                  (0)
//#define BOARD_LEDTX_PIN                   (27)

In this file it's also possible to change the SERCOM and pins for the optional UART upload:

#define BOOT_USART_MODULE                 SERCOM0
#define BOOT_USART_BUS_CLOCK_INDEX        PM_APBCMASK_SERCOM0
#define BOOT_USART_PER_CLOCK_INDEX        GCLK_CLKCTRL_ID_SERCOM0_CORE_Val
#define BOOT_USART_PAD_SETTINGS           UART_RX_PAD3_TX_PAD2
#define BOOT_USART_PAD3                   PINMUX_PA11C_SERCOM0_PAD3
#define BOOT_USART_PAD2                   PINMUX_PA10C_SERCOM0_PAD2
#define BOOT_USART_PAD1                   PINMUX_UNUSED
#define BOOT_USART_PAD0                   PINMUX_UNUSED

To determine if the optional UART upload is enabled in addition to the native USB port, edit the "MakeFile":

If you only want the native USB upload and not the UART option, change line 65 in the bootloader's "Makefile" from:

SAM_BA_INTERFACES?=SAM_BA_BOTH_INTERFACES

to

SAM_BA_INTERFACES?=SAM_BA_USBCDC_ONLY

Leave the compiler option beneath that set to SAMD21G18A, the bootloader code isn't too fussy which variant's being used, since it uses only the resources common to all of them.

By default the bootloader will be set to operate with an external 32.768kHz crystal.

Thanks for the speedy reply. Done all of what you said. These are the last few lines of the output monitor. Does this still mean its bootloaded ok?

** Programming Finished **
** Verify Started **
verified 6504 bytes in 0.565486s (11.232 KiB/s)
** Verified OK **
** Resetting Target **
target halted due to breakpoint, current mode: Handler HardFault
xPSR: 0x61000003 pc: 0x00000600 msp: 0x20007fd8
shutdown command invoked

Hi @Callum_Austin11

Looks like the upload was successful, but the bootloader code encountered a problem.

Please find attached a minimal bootloader for the G15A, native USB only, runs crystalless, irrespective of whether your board has a crystal or not, plus all LEDs are disabled:

samd21_sam_ba.zip (4.7 KB)

Thanks for that bootloader. I used atmel studio 7 to upload it using an external EDBG Chip. So now going back to arduino IDE. It shows up on the port, (COM 6 Arduino Zero Native Port). However, when I go to upload a sketch I get this on the output; (Just trying to upload simple Blink Example Sketch)

Forcing reset using 1200bps open/close on port COM6
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
Uploading using selected port: COM6
C:\Users\SMS\AppData\Local\Arduino15\packages\arduino\tools\bossac\1.7.0-arduino3/bossac.exe -i -d --port=COM6 -U true -i -e -w -v C:\Users\SMS\AppData\Local\Temp\arduino_build_180595/Blink.ino.bin -R 
Set binary mode

readWord(addr=0)=0x20000ffc
SAM-BA operation failed
readWord(addr=0xe000ed00)=0x410cc601
readWord(addr=0x41002018)=0x10011524
version()=v2.0 [Arduino:XYZP] Oct 20 2021 15:22:27
chipId=0x10010024
Connected at 921600 baud
readWord(addr=0)=0x20000ffc
readWord(addr=0xe000ed00)=0x410cc601
readWord(addr=0x41002018)=0x10011524
Atmel SMART device 0x10010024 found
write(addr=0x20000800,size=0x34)
writeWord(addr=0x20000830,value=0x10)
writeWord(addr=0x20000820,value=0x20001000)
Device       : ATSAMD21G15B
readWord(addr=0)=0x20000ffc
readWord(addr=0xe000ed00)=0x410cc601
readWord(addr=0x41002018)=0x10011524
Chip ID      : 10010024
version()=v2.0 [Arduino:XYZP] Oct 20 2021 15:22:27
Version      : v2.0 [Arduino:XYZP] Oct 20 2021 15:22:27
Address      : 8192
Pages        : 384
Page Size    : 64 bytes
Total Size   : 24KB
Planes       : 1
Lock Regions : 16
Locked       : readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
readWord(addr=0x41004020)=0xffff
none
readWord(addr=0x41004018)=0
Security     : false
Boot Flash   : true
readWord(addr=0x40000834)=0x7000a
BOD          : true
readWord(addr=0x40000834)=0x7000a
BOR          : true
Arduino      : FAST_CHIP_ERASE
Arduino      : FAST_MULTI_PAGE_WRITE
Arduino      : CAN_CHECKSUM_MEMORY_BUFFER
Erase flash
chipErase(addr=0x2000)
done in 0.109 seconds

Write 11440 bytes to flash (179 pages)
write(addr=0x20000c00,size=0x1000)
An error occurred while uploading the sketch

Hi @Callum_Austin11

Might I ask if you're using a SAMD21G15A or SAMD21G15B?

It's just that the bossac.exe that I provided in the link only supports the former.

Hi Martin, Yes im using the SAMD21G15B

@Callum_Austin11 It looks like the latest version 1.9.1 of Shumatech's orginal Bossa supports the SAMD21G15B: https://github.com/shumatech/BOSSA/releases/tag/1.9.1.

Here's the executable:

Removed

Hopefully this should work. Bossa checks the microcontroller's device ID, so (any given version of it) is quite particular about which devices it supports.

Hi Martin, I downloaded the new bossac.exe like you said.
Still no succession, get this on the output now;

Forcing reset using 1200bps open/close on port COM6
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
PORTS {COM4, COM6, } / {COM4, COM6, } => {}
Uploading using selected port: COM6
C:\Users\SMS\AppData\Local\Arduino15\packages\arduino\tools\bossac\1.7.0-arduino3/bossac.exe -i -d --port=COM6 -U true -i -e -w -v C:\Users\SMS\AppData\Local\Temp\arduino_build_428772/Blink.ino.bin -R 
bossac.exe: extra arguments found
Try 'bossac.exe -h' or 'bossac.exe --help' for more information
An error occurred while uploading the sketch

Hi @Callum_Austin11

Just to comfirm, did the bootloader upload successfully using the programmer?

Do you get a valid COM port in the Arduino IDE, if you double tap the board's reset button?

Hi Martin, Yes I do get a valid COM port, Shows as COM 6 (Arduino Zero (native USB port))

Hi @Callum_Austin11

I tested this version of bossac.exe on my SAMD21G18A board and it generates the same error. There must be an issue with this particular exexcutable.

Ah I see, is there an earlier version that can be used?

What I stated was wrong.

It looks like the bossac.exe that I orginally linked to does support the SAMD21G15B: https://forum.arduino.cc/t/lower-memory-samd21-variant/890009/37.

This also works with my custom SAMD21G18A board.

I think it's worth going back to that version. From your upload output, it looks like it has something to do with the microcontroller's memory.

Thats okay martin, at least we've figured out that issue out. The original bootloader you gave me, how would you upload that to the chip, would you also use Atmel Studio or would you do it through Arduino IDE? I'm not entirely sure where to put the file or access it if done from the Arduino IDE.

I'd do that through Atmel Studio, using the menu Tools->Device Programming, select your programmer, then click on the "Apply" button followed by the "Read" button, to detect your target device.

Go to the memories section and in the top text box enter the path to the bootloader binary. This should then erase, program and verify the bootloader upload.