Programming SPI Serial Flash Memory (AT45DB161D)
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.
Use: void SPI_Flash_Init(void)
Description: Initializes the SPI interface for access to the flash memory.
Use: unit8_t SPI_Flash_Test(void)
Return Code: OK / ERROR
Description: Tests access to the flash memory.
Use: void df_read_open(uint32_t addr)
Description: Sets the current read address (0 to 0x1FFFFF).
Use: void df_write_open(uint32_t addr)
Description: Sets the current write address (0 to 0x1FFFFF).
Use: void df_read(uint8_t *buf, uint16_t size)
Description: Reads from the current Flash address as set in df_read_open.
Use: void df_write(uint8_t *buf, uint16_t size)
Description: Write to the current Flash address as set in df_write_open.
Use: void df_erase(uint32_t sec)
Description: Erases the selected sector.
- 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).
/* 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);
// Initialize and test the flash memory
cFlashIDCorrect = SPI_Flash_test();
if(cFlashIDCorrect == OK)
// Read setup data from flash
// Check if setup data is valid
// 0x55 and 0xAA are sample data
if(cSetup != 0x55 || cSetup != 0xAA)
// No valid setup; use default settings
cSetup = 0x55;
cSetup = 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