Microchip® Advanced Software Framework

Quick Start Guide for AT25DFx SerialFlash Driver

This quick start will read, unprotect, erase and write bytes to an AT25DF081A that is connected to the SPI interface on EXT1 header of SAM D20 Xplained Pro or EXT3 of SAM D21 Xplained Pro.

The SERCOM SPI will be configured with the following settings:

For SAM D20 Xplained Pro

  • 500 kHz baud rate
  • SCK, MISO and MOSI on EXT1 header's SPI pins

For SAM D21 Xplained Pro

  • 120 kHz baud rate
  • SCK, MISO and MOSI on EXT3

The AT25DFx driver instance will be configured with the following settings:

For SAM D20 Xplained Pro

  • CS on EXT1 header's SS0 pin

For SAM D21 Xplained Pro

  • CS on EXT3 PA13

Setup

Prerequisites

There are no special setup requirements for this use-case.

Code

Add to the main application source file, outside of any functions:

#define AT25DFX_BUFFER_SIZE (10)
static uint8_t read_buffer[AT25DFX_BUFFER_SIZE];
static uint8_t write_buffer[AT25DFX_BUFFER_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

Create a new function for initializing the AT25DFx:

static void at25dfx_init(void)
{
struct spi_config at25dfx_spi_config;
at25dfx_spi_get_config_defaults(&at25dfx_spi_config);
at25dfx_spi_config.mode_specific.master.baudrate = AT25DFX_CLOCK_SPEED;
at25dfx_spi_config.mux_setting = AT25DFX_SPI_PINMUX_SETTING;
at25dfx_spi_config.pinmux_pad0 = AT25DFX_SPI_PINMUX_PAD0;
at25dfx_spi_config.pinmux_pad1 = AT25DFX_SPI_PINMUX_PAD1;
at25dfx_spi_config.pinmux_pad2 = AT25DFX_SPI_PINMUX_PAD2;
at25dfx_spi_config.pinmux_pad3 = AT25DFX_SPI_PINMUX_PAD3;
spi_init(&at25dfx_spi, AT25DFX_SPI, &at25dfx_spi_config);
at25dfx_chip_config.type = AT25DFX_MEM_TYPE;
}

If not already present, add to the initialization code:

Workflow

  1. Create read and write buffers.
    #define AT25DFX_BUFFER_SIZE (10)
    static uint8_t read_buffer[AT25DFX_BUFFER_SIZE];
    static uint8_t write_buffer[AT25DFX_BUFFER_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  2. Create global instances of SPI and AT25DFx chip.
  3. Create a function to contain the AT25DFx initialization code.
  4. Create local instances of SPI and AT25DFx configurations.
    struct spi_config at25dfx_spi_config;
  5. Initialize the SPI for AT25DFx connected to EXT1 header.
    at25dfx_spi_get_config_defaults(&at25dfx_spi_config);
    at25dfx_spi_config.mode_specific.master.baudrate = AT25DFX_CLOCK_SPEED;
    at25dfx_spi_config.mux_setting = AT25DFX_SPI_PINMUX_SETTING;
    at25dfx_spi_config.pinmux_pad0 = AT25DFX_SPI_PINMUX_PAD0;
    at25dfx_spi_config.pinmux_pad1 = AT25DFX_SPI_PINMUX_PAD1;
    at25dfx_spi_config.pinmux_pad2 = AT25DFX_SPI_PINMUX_PAD2;
    at25dfx_spi_config.pinmux_pad3 = AT25DFX_SPI_PINMUX_PAD3;
    spi_init(&at25dfx_spi, AT25DFX_SPI, &at25dfx_spi_config);
  6. Initialize the AT25DFx instance for AT25DF081A, with Slave Select on the SS_0 pin on EXT1.

Use Case

Code

Copy into main application:

Workflow

  1. Wakeup serialFlash.
  2. Check that the SerialFlash is present.
    // Handle missing or non-responsive device
    }
  3. Read out the first AT25DFX_BUFFER_SIZE bytes, starting at the very first flash address.
  4. Disable protection of the second sector.
    Note
    This device has sectors with a uniform size of 64 kB, so the address 0x10000 marks the start of the second sector. This can differ between devices.
  5. Erase the first 4 kB of the second 64 kB block (64-68 kB range).
  6. Write AT25DFX_BUFFER_SIZE bytes, starting at the beginning of the sector and the newly erased memory locations.
  7. Enable protection of all sectors, to prevent accidental erases of content.
  8. Put SerialFlash device to sleep, to conserve power.