# Customizing the board def and bootloader for SAMD21G17A

**URL:** <https://forum.arduino.cc/t/customizing-the-board-def-and-bootloader-for-samd21g17a/1059489>\
**Category:** Zero\
**Created:** [November 29, 2022, 7:56pm UTC](https://forum.arduino.cc/t/customizing-the-board-def-and-bootloader-for-samd21g17a/1059489 "2022-11-29T19:56:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidpruitt](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@davidpruitt](https://forum.arduino.cc/u/davidpruitt)\
**Post date:** [November 29, 2022, 7:56pm UTC](https://forum.arduino.cc/t/customizing-the-board-def-and-bootloader-for-samd21g17a/1059489/1 "2022-11-29T19:56:08Z")

</div>

Hey all,

I have a custom board using a SAMD21G17A. I've created a custom board definition and I've also re-built the bootloader to work with the smaller memory specs of the G17A compared to the G18A. Unfortunately, I am having some issues, so I figured I would see if y'all can provide any wisdom.

The main issue is with regard to the USB virtual serial port being available. Here is the behavior I am seeing:

1. I first use Atmel Studio to burn my bootloader to the board. After doing so, the USB virtual serial port does not immediately appear. If I unplug the USB cable, and plug it back into a _different_ USB port, it then appears (as COM3 for the bootloader).
2. At this point, I open the Arduino IDE, and I upload a very simple blink sketch. The upload is _successful_, but the USB virtual serial port disappears. I must unplug the USB cable, and plug it back in. It now appears as COM37 for the uploaded sketch.
3. Now, let's assume I want to upload the sketch again, or even upload a different sketch. I try uploading the sketch, but the upload fails. It attempts to use the 1200 bps method to force a reset and enter the bootloader, but this fails. Meanwhile, COM37 disappears.
4. If I once again disconnect the USB cable and reconnect it, now we are back in the bootloader, and COM3 appears.

So basically, if I want to upload, _and if_ I am in the bootloader, I can successfully upload - but then I need to disconnect/reconnect the USB in order for the sketch to work.

Otherwise, if I want to upload, but if I am _not_ in the bootloader, I must attempt to upload (which then fails), then disconnect/reconnect the USB, and then I will be in the bootloader again.

Now, you're probably wondering what changes I have made to bootloader and such, so I'll describe my changes below.

I am using the Arduino Zero bootloader, but I have made the following changes.

First, to the `board_definitions_arduino_zero.h` file in the bootloader: I have changed the BOOT\_DOUBLE\_TAP\_ADDRESS to be the correct address for the SAMD21G17A. The correct address is calculated as:

Double Tap Status Address: 0x20000000 + 0x4000 - 4 = 0x20003FFC

Code changes:

```cpp
#define BOOT_DOUBLE_TAP_ADDRESS (0x20003FFCul)
#define BOOT_DOUBLE_TAP_DATA (*((volatile uint32_t *) BOOT_DOUBLE_TAP_ADDRESS))

```

Next, I changed the bootloader linker file to account for the fact that the SAMD21G17A only has 16 KB of RAM (0x4000), rather than 32 KB (0x8000) like the SAMD21G18A:

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

```

Those are all the changes for the bootloader.

In my _board definition_, I have changed the linker definition to also account for the memory differences between the G17A and the G18A:

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

```

In the boards.txt file, I have changed the maximum upload size and specified we are using G17A:

```cpp
my_custom_board.upload.maximum_size=122880
my_custom_board.build.extra_flags=-D __SAMD21G17A__ {build.usb_flags}

```

And here is my OpenOCD script:

```cpp
source [find interface/cmsis-dap.cfg]

# chip name
set CHIPNAME at91samd21g17
set ENDIAN little

# choose a port here
set telnet_port 0

source [find target/at91samdXX.cfg]

```

Many of these changes are exactly what has been described as necessary in an older forum thread: [128k vs 256k ATSAMD21 - #2 by MartinL](https://forum.arduino.cc/t/128k-vs-256k-atsamd21/577071/2)

If anyone has any insights as to what could be going on, I would greatly appreciate it! Thank you!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 28, 2023, 7:56pm UTC](https://forum.arduino.cc/t/customizing-the-board-def-and-bootloader-for-samd21g17a/1059489/2 "2023-05-28T19:56:55Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
