Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Flash Program Example

Purpose

This basic example shows how to use the Flash service available on the Atmel SAM microcontrollers. It details steps required to program the internal flash, and manage secure and lock bits.

Requirements

This package can be used with SAM evaluation kits.

Description

The program performs the following set of commands:

  • Unlock the test page.
  • Program the test page of the embedded flash with walking bit pattern (0x1, 0x2, 0x4, ...).
  • Check if the flash is correctly programmed by reading all the values programmed.
  • Lock the test page and check if it has been locked correctly.
  • Set the security bit.

The SAM MCU features a security bit, based on a specific General Purpose NVM bit 0. When the security bit is enabled, any access to the Flash, SRAM, Core Registers and Internal Peripherals through the ICE interface is forbidden. This example will reproduce this scene.

The SAM MCU ROM code embeds small In-Application Programming (IAP) Procedure. Since this function is executed from ROM, this allows Flash programming (such as sector write) to be done when code is running out of Flash. We will use IAP function by default in flash driver.

Using Atmel Studio or Segger to debug, to be able to correctly watch the changes in the flash through the Memory view or through Segger J-Mem, caching needs to be disabled. This can be done in the Tool tab in the Project Properties in Atmel Studio. We also need to place a breakpoint in the flash_write function then do a Step Out to see the changes, otherwise stopping the program execution anywhere just shows 0xFFs instead of the walking pattern.

Usage

  1. Build the program and download it into the evaluation board.
  2. On the computer, open and configure a terminal application (e.g., HyperTerminal on Microsoft Windows) with these settings:
    • 115200 bauds
    • 8 bits of data
    • No parity
    • 1 stop bit
    • No flow control
  3. Start the application.
  4. In the terminal window, the following text should appear:
    -- Flash Program Example --
    -- xxxxxx-xx
    -- Compiled: xxx xx xxxx xx:xx:xx --
    -I- Unlocking test page
    -I- Writing test page with walking bit pattern
    -I- Checking page contents ......................................... ok
    -I- Locking test page
    -I- Try to program the locked page...
    -I- Please open Segger's JMem program
    -I- Read memory at address 0xxxxxxxxx to check contents
    -I- Press any key to continue...
    -I- Good job!
    -I- Now set the security bit
    -I- Press any key to continue to see what happened...
    -I- Setting security bit
    -I- All tests done