Additional Information

Site Information

 Loading... Please wait...

Programming SPI Serial Flash Memory (AT45DB161D)

This page applies to programming the LandTiger LPC12768 Development Board and the Copperhill jBoard-X2.

The Atmel® AT45DB161D is a 16-megabit (2 MByte) 2.5V or 2.7V, serial-interface sequential access Flash memory ideally suited for a wide variety of program code- and data-storage applications.

The total of 2,097152 bytes (0x1FFFFF) are organized in 4096 pages with 512 bytes per page. Even though the chip offers a variety of erasing options, our software library supports Block Erase, with one block containing 4 Kbytes.

Note: In order to over-write existing data, you need to erase the corresponding block first. For further detailed information, please refer to the AT45DB161D data sheet.

Reading and writing data is accomplished in a fairly simple fashion, basically by providing a read/write address (0 to 0x1FFFFF) and data length information. Using the existing basic functions, it is possible to extended the code to support an extensive file access system, but for the sake of better performance (i.e. execution speed) we provide only the bare necessities.

Necessary Files:     

  • Flash.c
  • Flash.h
  • SST25VF016B.c
  • SST25VF016B.h

Function: SPI_FLASH_Init

Use:   void SPI_Flash_Init(void)
Description: Initializes the SPI interface for access to the flash memory.

Function: SPI_FLASH_Test

Use: unit8_t SPI_Flash_Test(void)
Return Code: OK / ERROR
Description: Tests access to the flash memory. 

Function: df_read_open

Use: void df_read_open(uint32_t addr)

Description: Sets the current read address (0 to 0x1FFFFF).

Function:  df_write_open

Use: void df_write_open(uint32_t addr)
Description: Sets the current write address (0 to 0x1FFFFF).

Function: df_read

Use: void df_read(uint8_t *buf, uint16_t size)
Description: Reads from the current Flash address as set in df_read_open.

Function: df_write

Use: void df_write(uint8_t *buf, uint16_t size)
Description: Write to the current Flash address as set in df_write_open.

Function: df_erase

Use: void df_erase(uint32_t sec)
Description: Erases the selected sector.

Notes:

  • The source code library does include function calls such as df_read_close and df_write_close, but they are not filled with code, i.e. they have no impact on the Flash access. It is important that the Flash access is mainly controlled by setting a read/write address within the range of 0 to 0x1FFFFF.
  • It is in the nature of the flash memory chip that, before writing new data, the corresponding sector needs to be erased (See also the programming example).

Programming Example:

#include “SST25VF016B.h”
#include “Flash.h”

/* Flash Memory */

extern void df_erase(uint32_t sec);
extern void df_read_open(uint32_t addr);
extern void df_write_open(uint32_t addr);
extern void df_read(uint8_t *buf, uint16_t size);
extern void df_write(uint8_t *buf, uint16_t size);
extern uint8_t SPI_FLASH_Test(void);
extern void SPI_FlashInit(void);

int main(void)
{

uint8_t cFlashIDCorrect;
uint8_t cSetup[100];

// Initialize and test the flash memory
SPI_FlashInit();
cFlashIDCorrect = SPI_Flash_test();

if(cFlashIDCorrect == OK)
{

// Read setup data from flash
df_read_open(0);
df_read(&cSetup[0], 100);

// Check if setup data is valid
// 0x55 and 0xAA are sample data
if(cSetup[0] != 0x55 || cSetup[1] != 0xAA)
{

// No valid setup; use default settings
cSetup[0] = 0x55;
cSetup[1] = 0xAA;

// Add more data to the array…

// Erase the setup sector
df_erase(0);  // Sector = 0…511 = 512 sectors per 4096 bytes

// Write the data to flash memory
df_write_open(0)
df_write(&cSetup[0], 100);

}

}

return 0;

}