spi_flash.cpp File Reference
#include <stdint.h>
#include <string.h>
#include "spi_flash.h"
#include "ssp1.h"
#include "disk_defines.h"
#include "bio.h"
#include "fat/ff.h"
Include dependency graph for spi_flash.cpp:

Macros

#define CHIP_SELECT_OP()
 
#define FLASH_MANUFACTURER_ID   (0x1F)
 This should match BYTE #1 of manufacturer and device ID information. More...
 
#define FLASH_SECTOR_SIZE   (512)
 Minimum sector size that works with FATFS (do not change this) More...
 
#define FLASH_PAGENUM_BIT_OFFSET   9
 
#define FLASH_PAGESIZE_256   256
 Page size defines. We support 256-528 byte page size. Do not change these. More...
 
#define FLASH_PAGESIZE_512   512
 
#define FLASH_PAGESIZE_264   264
 
#define FLASH_PAGESIZE_528   528
 

Typedefs

typedef void(* flash_io_func_t) (uint8_t *data, const uint32_t addr, const uint32_t size)
 Function pointer of I/O operation. More...
 

Enumerations

enum  flash_opcode_t {
  opcode_status_reg = 0xD7, opcode_get_sig = 0x9F, opcode_read_continous = 0xE8, opcode_read_cont_lowfreq = 0x03,
  opcode_page_erase = 0x81, opcode_prog_thru_buffer1 = 0x82, opcode_write_buffer1 = 0x84, opcode_buffer1_to_mem_no_builtin_erase = 0x88,
  opcode_read_security_reg = 0x77, opcode_write_security_reg = 0x9B
}
 Flash device opcodes. More...
 
enum  flash_cap_t {
  flash_cap_invalid = 0, flash_cap_8mbit = 0x25, flash_cap_16mbit = 0x26, flash_cap_32mbit = 0x27,
  flash_cap_64mbit = 0x28, flash_cap_first_valid = flash_cap_8mbit, flash_cap_last_valid = flash_cap_64mbit
}
 This should match BYTE #2 of manufacturer and device ID information. More...
 

Functions

DSTATUS flash_initialize ()
 
DRESULT flash_read_sectors (unsigned char *pData, int sectorNum, int sectorCount)
 
DRESULT flash_write_sectors (unsigned char *pData, int sectorNum, int sectorCount)
 
DRESULT flash_ioctl (BYTE ctrl, void *buff)
 
void flash_write_permanent_id (char *id_64bytes)
 
void flash_read_permanent_id (char *id_64bytes)
 
uint32_t flash_get_page_count (void)
 
uint32_t flash_get_page_size (void)
 
bool flash_supports_metadata (void)
 
uint32_t flash_get_page_write_count (uint32_t page_number)
 
void flash_chip_erase (void)
 

Detailed Description

This file supports Adesto Flash (formerly Atmel data flash) It should work with the following part numbers : 8mbit : AT45DB081E 16mbit : AT45DB161E 32mbit : AT45DB321E 64mbit : AT45DB641E

Any other model will also work as long as the page size or the minimum size we can program is between 256 - 528 inclusively.

Macro Definition Documentation

#define CHIP_SELECT_OP ( )
Value:
for(uint8_t ___i = board_io_flash_cs();\
___i; \
___i = (board_io_flash_ds() || board_io_flash_ds() || board_io_flash_ds()))

Macro to select and deselect SPI device during an operation. When we run in high frequency (fast CPU clock), we need to make sure there is at least 50ns difference between chip-selects, so we issue board_io_flash_ds() multiple times on purpose

#define FLASH_MANUFACTURER_ID   (0x1F)

This should match BYTE #1 of manufacturer and device ID information.

#define FLASH_PAGENUM_BIT_OFFSET   9

Bit number for specifying flash page offset when using non-standard page-size

Example Page read/write with 264 bytes or 528 bytes: 3 address bytes : | 23:16 | 15:8 | 7:0 |

First 3 bits are don't care. Next 12-bits specify page number. Last 9 specify byte offset.

#define FLASH_PAGESIZE_256   256

Page size defines. We support 256-528 byte page size. Do not change these.

#define FLASH_PAGESIZE_264   264
#define FLASH_PAGESIZE_512   512
#define FLASH_PAGESIZE_528   528
#define FLASH_SECTOR_SIZE   (512)

Minimum sector size that works with FATFS (do not change this)

Typedef Documentation

typedef void(* flash_io_func_t) (uint8_t *data, const uint32_t addr, const uint32_t size)

Function pointer of I/O operation.

Enumeration Type Documentation

This should match BYTE #2 of manufacturer and device ID information.

Enumerator
flash_cap_invalid 
flash_cap_8mbit 
flash_cap_16mbit 
flash_cap_32mbit 
flash_cap_64mbit 
flash_cap_first_valid 

SPI Flash signature must fall in between the capacity IDs for the initialization to be considered successful

flash_cap_last_valid 

Flash device opcodes.

Enumerator
opcode_status_reg 
opcode_get_sig 
opcode_read_continous 

Works up to 66Mhz but requires 4 dummy bytes.

opcode_read_cont_lowfreq 

Works up to 33Mhz.

opcode_page_erase 

Memory write options: Easiest way to write a page is using opcode_prog_thru_buffer1. Efficient way to write a page (if flash is busy) is to write buffer 1 (while busy) then perform page erase and buffer1 to memory without built-in-erase

opcode_prog_thru_buffer1 
opcode_write_buffer1 
opcode_buffer1_to_mem_no_builtin_erase 
opcode_read_security_reg 
opcode_write_security_reg 

Function Documentation

void flash_chip_erase ( void  )

This will ERASE the entire chip, including the meta-data!! This can take several seconds to perform the chip erase...

uint32_t flash_get_page_count ( void  )

Flash memory metadata functions If the flash memory contains additional space for each page, then each time a page is written, we also write its write-counter value. You can then retrieve the page write count values by the page number.

uint32_t flash_get_page_size ( void  )
uint32_t flash_get_page_write_count ( uint32_t  page_number)
Warning
DO NOT USE THIS FUNCTION WITHOUT THE SPI SEMAPHORE!!!
DSTATUS flash_initialize ( )

Initializes the Flash Memory

DRESULT flash_ioctl ( BYTE  ctrl,
void *  buff 
)

Gets control information from this flash memory drive, such as sector count, sector size

Parameters
ctrlThe type of information to get
buffThe pointer to save the requested information to
void flash_read_permanent_id ( char *  id_64bytes)

Reads the permanent ID programmed into the SPI flash

Warning
DO NOT USE THIS FUNCTION WITHOUT THE SPI SEMAPHORE!!!
DRESULT flash_read_sectors ( unsigned char *  pData,
int  sectorNum,
int  sectorCount 
)

Reads a sector from the Flash Memory

Parameters
pDataThe pointer to the data to save the read
sectorNumThe sector number to read with each sector being 512 bytes
sectorCountThe number of sectors to read
bool flash_supports_metadata ( void  )
void flash_write_permanent_id ( char *  id_64bytes)

Writes a permanent ID on the SPI Flash

Warning
THIS CAN ONLY BE DONE ONCE IN A LIFETIME OF THE SPI FLASH
DO NOT USE THIS FUNCTION WITHOUT THE SPI SEMAPHORE!!!
DRESULT flash_write_sectors ( unsigned char *  pData,
int  sectorNum,
int  sectorCount 
)

Writes a sector to the Flash Memory

Parameters
pDataThe pointer to the data to write
sectorNumThe sector number to write with each sector being 512 bytes
sectorCountThe number of sectors to write