Implementing a Bootloader on the SAM D21 MCU

 Objective

This page shows you how to:

  • Use Atmel START to create a simple example project (your application).
  • Specify the starting address of your application in program memory.
  • Configure the SAM-BA Monitor (MCU application) bootloader to execute your application (using its starting address).
  • Use the SAM-BA Host (PC) utility to upload your application to the SAM D21 MCU.

 Materials

Hardware Tools

Tool About Purchase
board-50px.png
SAM D21 Xplained Pro
Evaluation Kit

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
swtool-28px.png
Atmel® Studio
Integrated Development Environment
Atmel® START (ASF4)
Integrated Software Framework
Web Based
SAM-BA® Host
PC utility
About >
SAM-BA® Monitor
MCU application
About >

Optional Lab Manual

If you would like a hardcopy of this tutorial: Click Here >

Introduction:

The following steps show you how to use a bootloader on the SAM D21 microcontroller.

A bootloader gives you the ability to update the firmware in your microcontroller, without the need of a programmer/debugger (e.g., SAM-ICE™ JTAG Emulator). This is particularly useful for field updates. Just connect to the microcontroller’s USB or UART interface and hold a specific I/O pin low during a reset to upload new firmware.

In step 1, you will use Atmel START to create two new projects:

  • LEDflasher0 blinks LED0 once every second
  • LEDflasher1 blinks LED0 five times every second

You will upload these projects in later steps with the SAM-BA bootloader.

In step 2, you will program the SAM D21 MCU with the SAM-BA Monitor (BA stands for boot assistance) application. The SAM-BA Monitor is used to communicate with the SAM-BA host PC utility. On reset, the SAM-BA Monitor will look for a Start condition (bootloader mode) on the UART and/or USB interfaces. When the Start condition is detected, the SAM-BA Monitor enters an infinite loop waiting for commands from the SAM-BA PC utility. The most essential command is the “upload this file” command.

In step 3, you will use the SAM-BA Monitor to upload one of the LEDflasher applications to the MCU. You now have both the SAM-BA Monitor application and LEDflasher application loaded into the MCU.

In step 4, you will use the bootloader to change one LEDflasher application with the other (i.e., update the firmware). You will use the SAM-BA Monitor to establish a connection with the PC application and upload the second LEDflasher application to the MCU.

Procedure:

Step 1: Create two application projects using Atmel START
Step 2: Program the SAM D21 XPRO with the SAM-BA Monitor
Step 3: Use SAM-BA Host (PC) to upload the application to board
Step 4: Use bootloader to replace one application with another

Step 1: Create two application projects using Atmel START.

In this first assignment, you will use Atmel START to create two simple new projects. One project blinks the LED once per second. The other blinks the LED five times per second.

Atmel START (http://start.atmel.com) is an online project generation tool that helps you to select and configure software components, drivers, middleware and example projects. It performs all the MCU initialization and configuration tasks, allowing you to focus on your application code.

1

Download and build an example Atmel START project.

start_examples.png
  • In the Board field select 'SAM D21 Xplained Pro'.
  • In the Category field select 'I/O'.
  • Scroll down to the “LED flasher” example and click on it.
  • Click DOWNLOAD SELECTED EXAMPLE.
  • Select 'SAM D21 Xplained Pro' then 'Select board'.
  • Browse to the folder you want to save the project in and click Save.
start_examples1.png
  • Open the Studio 7 IDE.
  • Click on File > Import > Atmel Start Project.
import.png
  • Browse to the folder you saved the project in and select the .atzip file as the Atmel START project.
  • Provide a location and name for the project, then click OK.
import1.png
  • Find the led_flasher_main.c file at the bottom of the Solution Explorer window. Double-click to open it.
solution_explorer.png
  • Find the main function at the bottom of the file. Note this example project toggles LED0 once every 500 ms.
main.png
  • Change the project build properties to locate the application start address at 0x6000.
    • The SAM-BA Monitor needs to know where your application code lives, so it can start it if no bootloader event is found. This application start address is hardcoded in one of the SAM-BA Monitor header files (device_config.h).
    • Right-click on the project name (LEDflasher0 in this case) in the Solution Explorer window, then select ‘Properties’.
    • Select Toolchain > ARM/GNU Linker > Miscellaneous.
    • Add these flags to the Linker Flags window:
      • -Wl,—section-start=.text=0x6000,
linker_start.png
  • Generate the binary file (<project name>.bin) for this project by clicking on the Build Solution icon.
    • This file will be found in the ‘<project name>/Debug’ folder.
build.png

2

Create and build a second project.

Create a duplicate project based on the one you just built.

  • Import the same Atmel Start project.
    • Click on File > Import > Atmel Start Project.
  • Provide a different name and location for the project and click OK.

You should now have two open projects in the Solution Explorer window.

  • Find the new led_flasher_main.c file in the project you just created and open it.
  • Change the parameter for the delay_ms function from 500 to 100.
    • This delay toggles LED0 every 100 ms.
  • Save the file (type Ctrl + s).
main1.png
  • As you did in the previous project, change the project build properties to locate the application start address at 0x6000.
    • Right-click on the project name (LEDflasher1 in this case) in the Solution Explorer window, then select ‘Properties’.
    • Select Toolchain > ARM/GNU Linker > Miscellaneous.
    • Add these flags to the Linker Flags window:
      • -Wl,—section-start=.text=0x6000,
  • Generate the binary file for this second project by clicking on the Build Solution icon.
build.png

You now have two projects; each one with its own binary file. These binary files will be uploaded to the SAM D21 (via bootloader) in steps 3 and 4.

Step 2: Program the SAM D21 XPRO with the SAM-BA Monitor.

1

Download the SAM-BA Host Utility and Monitor Project.
Go to Microchip’s SAM-BA In-system Programmer website and download the SAM-BA Monitor (SAM MCU project) and the SAM-BA Host PC utility.

  • Website >
    • SAM-BA MONITOR ROMLESS v2.18
    • SAM-BA 2.18 for Windows®
  • Install the SAM-BA Host PC utility and extract the SAM-BA Monitor zip file.

2

Open the SAM-BA_MONITOR_ROMLESS project in the Studio 7 IDE:
File > Open > Project/Solution…
• Browse to SAM-BA_MONITOR_ROMLESS.atsln file and click Open.

open_monitor.png

3

Change the Application Start Address in the SAM-BA Monitor.

If the SAM-BA Monitor does not detect a bootloader event, it needs to know the starting address of your application so it can execute it.

The default application start address is 0x2000. You need to change this to 0x6000 to match both the SAM-BA host utility and your application project configuration (you configured this in the previous step).

The SAM-BA Host utility protects the bootloader area from being overwritten accidentally. It does this by specifying the size of the SAM-BA Monitor application. If you want to choose a different Monitor size, you can customize and regenerate applets for the SAM-BA Host. See application note AN2565 for details.

  • Open device_config.h.
    • Found in the Solution Explorer window in the SAM-BA_MONITOR_ROMLESS/device_config folder.
  • Change the 'DEFAULT_APP_START_ADDRESS' to 0x6000.
default_start_addr.png

4

Program the SAM-BA Monitor onto the SAM D21 board.

  • Connect the SAM D21 Xplained Pro board to your computer using the DEBUG USB Connector.
    • Note: Please be patient as the driver installs (it may take a minute or so).
edbg.png

5

Select the debugger/programmer, then compile and program the project.

  • Select the embedded (on-board) debugger found on the SAM D21 XPRO board as the debugger/programmer for the project:
    • Click on Project > Properties.
proj_prop.png
  • Select Tool > EDBG as debugger/programmer and 'SWD' as Interface:
edbg1.png
  • Compile the project by clicking on the Build Solution icon or by typing F7.
build.png
  • Program the application by clicking on the Start Without Debugging icon.
start_without_debug.png

Note: If the firmware on the evaluation board is out of date, a Firmware Upgrade window will appear asking you if you want to upgrade the firmware. Select Upgrade and allow the process to complete.

The SAMBA Monitor is now running on the evaluation board.

Step 3: Use SAM-BA Host (PC) to upload the application to board.

1

Connect to SAM D21 XPRO Using the SAM-BA Host Utility.

  • Make sure your PC is still connected to the ‘DEBUG USB’ connector on the SAM D21 XPRO board.
  • Open the SAM-BA Host utility.
  • Use the drop-down window to select the Windows COM port.
  • Use the drop-down window to select the board you are connecting to.
    • samd21_xplained_pro[not factory programmed]

Use the Windows Device Manager to find the COM port number assigned to the SAM D21 XPRO board (EDGB Virtual COM Port).

connect.png

After clicking Connect, the yellow ‘STATUS’ LED on the SAM D21 XPRO will turn on. The SAM-BA Host and Monitor are now connected.

2

Upload an application to the board.

  • Before uploading your application to the board, erase the flash memory used to store the application.
    • Click the Execute button in the Scripts field (see following image).

The ‘application’ area in flash does not include the flash addresses containing the bootloader.

  • Upload your project’s binary file to the SAM D21 XPRO board.
    • Specify the start address for your application as 0x6000.
      • This is the same location you configured both application projects for in step 1 and is the same location specified in the SAM-BA Monitor.
      • Note: If you want to choose a different start address, you can customize and regenerate applets for the SAM-BA Host. See app note AN2565 for details.
    • Click on the folder icon to browse to the folder containing the binary file for one of the ‘LED flasher’ projects you created in step 1.
      • This file will be found in the ‘<project name>/Debug’ folder.
    • Click the Send File button.
erase_upload.png

3

Reset the board to verify you’ve successfully uploaded your project.

  • Push the RESET button (located beside the DEBUG USB connector) to exit the SAM-BA Monitor mode.
  • LED0 should blink every second or five times a second (depending on which application you uploaded).

Step 4: Use bootloader to replace one application with another.

Both the SAM-BA Monitor and your application are running on the SAM D21 XPRO board. In this step, you will replace one LED flasher application (created in step 1) with another.

How the SAM-BA Monitor Works:

When the SAM D21 is reset, the SAM-BA Monitor checks if the starting application address is erased (i.e., no application has been uploaded). If it is erased, it stays in bootloader mode, allowing you to upload your application.

If the starting application is not erased (i.e., an application exists), the SAM-BA Monitor will check the status of one of its I/O pins to determine if it should stay in bootloader mode or execute the application.

If the I/O pin is low, the SAM-BA Monitor will keep the SAM D21 in bootloader mode until the next reset.

If the I/O pin is high, the SAM-BA Monitor will execute the application as if no bootloader exists.

1

Place the SAM D21 in bootloader mode.

The SAM-BA Monitor can use the SAM D21’s USB and/or UART interface(s) to upload an application’s binary file. You configure the SAM-BA Monitor project for the interface you want to use (default configuration is both interfaces).

The SAM D21’s USB interface is connected to the ‘TARGET USB’ connector.
The SAM D21’s UART interface is connected (virtually) to the ‘DEBUG USB’ connector.

In the previous step, you connected to the SAM D21 XPRO board via the ‘DEBUG USB’ connector, so you had a virtual connection to the UART on the SAM D21. In this step, you will connect to the ‘TARGET USB’ connector to verify the SAM-BA Monitor’s USB bootloader functionality.

  • Move the USB cable from the ‘DEBUG USB’ connector to the ‘TARGET USB’ connector.
    • You will note the application you loaded in the previous step starts to run automatically.
  • Put the SAM D21 in bootloader mode.
    • Press and hold the SW0 push button.
    • Press and release the RESET push button.
    • Release the SW0 push button.
    • Your LED flasher application will stop running (LED0 stops flashing) indicating the SAM-BA monitor is now running. Now that the SAM-BA monitor is running, it can connect to the SAM-BA Host utility.

2

Upload your second project to the SAM D21.

  • Start the SAM-BA Host utility on the PC.
  • Select the ‘\USBserial\COMx’ connection and choose the 'samd21_xplained_pro board'.
  • Click the Connect button.
usb_connection.png
  • As you did in the previous step, click the Execute button in the Scripts field to erase the flash application area.
  • Change the start address from 0x0000 to 0x6000.
  • Browse to the folder containing your second application and select the binary file.
  • Click the Send File button to upload the application.
erase_upload2.png

3

Reset the board to verify you’ve successfully uploaded your project.

  • Push the RESET button (located beside the DEBUG USB connector) to exit the SAM-BA Monitor mode.
  • Verify LED0 blinks as expected (either once or five times per second).

Congratulations! You've implemented a bootloader on the SAM D21 MCU!

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.