HW_Lib/lib/flash/flash.cpp

1039 lines
31 KiB
C++
Raw Normal View History

#include "flash.h"
#include <string.h>
/* send dummy data for read data */
#define DUMMY_DATA 0xFF
#ifndef FLASH_FLASH_DEVICE_TABLE
#error "Please configure the flash device information table in (in flash_cfg.h)."
#endif
/* user configured flash device information table */
static FLASH_t flash_table[] = FLASH_FLASH_DEVICE_TABLE;
/* supported manufacturer information table */
static const flash_mf mf_table[] = FLASH_MF_TABLE;
#ifdef FLASH_USING_FLASH_INFO_TABLE
/* supported flash chip information table */
static const flash_chip flash_chip_table[] = FLASH_FLASH_CHIP_TABLE;
#endif
#ifdef FLASH_USING_QSPI
/**
* flash read data mode
*/
enum flash_qspi_read_mode {
NORMAL_SPI_READ = 1 << 0, /**< mormal spi read mode */
DUAL_OUTPUT = 1 << 1, /**< qspi fast read dual output */
DUAL_IO = 1 << 2, /**< qspi fast read dual input/output */
QUAD_OUTPUT = 1 << 3, /**< qspi fast read quad output */
QUAD_IO = 1 << 4, /**< qspi fast read quad input/output */
};
/* QSPI flash chip's extended information table */
static const flash_qspi_flash_ext_info qspi_flash_ext_info_table[] = FLASH_FLASH_EXT_INFO_TABLE;
#endif /* FLASH_USING_QSPI */
static flash_err software_init(const FLASH_t *flash);
static flash_err hardware_init(FLASH_t *flash);
static flash_err page256_or_1_byte_write(const FLASH_t *flash, uint32_t addr, size_t size, uint16_t write_gran,
const uint8_t *data);
static flash_err aai_write(const FLASH_t *flash, uint32_t addr, size_t size, const uint8_t *data);
static flash_err wait_busy(const FLASH_t *flash);
static flash_err reset(const FLASH_t *flash);
static flash_err read_jedec_id(FLASH_t *flash);
static flash_err set_write_enabled(const FLASH_t *flash, bool enabled);
static flash_err set_4_byte_address_mode(FLASH_t *flash, bool enabled);
static void make_address_byte_array(const FLASH_t *flash, uint32_t addr, uint8_t *array);
/* ../port/sfup_port.c */
extern void flash_log_debug(const char *file, const long line, const char *format, ...);
extern void flash_log_info(const char *format, ...);
/**
* FLASH initialize by flash device
*
* @param flash flash device
*
* @return result
*/
flash_err flash_device_init(FLASH_t *flash) {
flash_err result = FLASH_SUCCESS;
/* hardware initialize */
result = hardware_init(flash);
if (result == FLASH_SUCCESS) {
result = software_init(flash);
}
if (result == FLASH_SUCCESS) {
flash->init_ok = true;
FLASH_INFO("%s flash device initialized successfully.", flash->name);
} else {
flash->init_ok = false;
FLASH_INFO("Error: %s flash device initialization failed.", flash->name);
}
return result;
}
/**
* FLASH library initialize.
*
* @return result
*/
flash_err flash_init(void) {
flash_err cur_flash_result = FLASH_SUCCESS, all_flash_result = FLASH_SUCCESS;
size_t i;
FLASH_DEBUG("Start initialize Serial Flash Universal Driver(FLASH) V%s.", FLASH_SW_VERSION);
FLASH_DEBUG("You can get the latest version on https://github.com/armink/FLASH .");
/* initialize all flash device in flash device table */
for (i = 0; i < sizeof(flash_table) / sizeof(FLASH_t); i++) {
/* initialize flash device index of flash device information table */
flash_table[i].index = i;
cur_flash_result = flash_device_init(&flash_table[i]);
if (cur_flash_result != FLASH_SUCCESS) {
all_flash_result = cur_flash_result;
}
}
return all_flash_result;
}
/**
* get flash device by its index which in the flash information table
*
* @param index the index which in the flash information table @see flash_table
*
* @return flash device
*/
FLASH_t *flash_get_device(size_t index) {
if (index < flash_get_device_num()) {
return &flash_table[index];
} else {
return NULL;
}
}
/**
* get flash device total number on flash device information table @see flash_table
*
* @return flash device total number
*/
size_t flash_get_device_num(void) {
return sizeof(flash_table) / sizeof(FLASH_t);
}
/**
* get flash device information table @see flash_table
*
* @return flash device table pointer
*/
const FLASH_t *flash_get_device_table(void) {
return flash_table;
}
#ifdef FLASH_USING_QSPI
static void qspi_set_read_cmd_format(FLASH_t *flash, uint8_t ins, uint8_t ins_lines, uint8_t addr_lines,
uint8_t dummy_cycles, uint8_t data_lines) {
/* if medium size greater than 16Mb, use 4-Byte address, instruction should be added one */
if (flash->chip.capacity <= 0x1000000) {
flash->read_cmd_format.instruction = ins;
flash->read_cmd_format.address_size = 24;
} else {
if (ins == FLASH_CMD_READ_DATA) {
flash->read_cmd_format.instruction = ins + 0x10;
} else {
flash->read_cmd_format.instruction = ins + 1;
}
flash->read_cmd_format.address_size = 32;
}
flash->read_cmd_format.instruction_lines = ins_lines;
flash->read_cmd_format.address_lines = addr_lines;
flash->read_cmd_format.alternate_bytes_lines = 0;
flash->read_cmd_format.dummy_cycles = dummy_cycles;
flash->read_cmd_format.data_lines = data_lines;
}
/**
* Enbale the fast read mode in QSPI flash mode. Default read mode is normal SPI mode.
*
* it will find the appropriate fast-read instruction to replace the read instruction(0x03)
* fast-read instruction @see FLASH_FLASH_EXT_INFO_TABLE
*
* @note When Flash is in QSPI mode, the method must be called after flash_device_init().
*
* @param flash flash device
* @param data_line_width the data lines max width which QSPI bus supported, such as 1, 2, 4
*
* @return result
*/
flash_err flash_qspi_fast_read_enable(FLASH_t *flash, uint8_t data_line_width) {
size_t i = 0;
uint8_t read_mode = NORMAL_SPI_READ;
flash_err result = FLASH_SUCCESS;
FLASH_ASSERT(flash);
FLASH_ASSERT(data_line_width == 1 || data_line_width == 2 || data_line_width == 4);
/* get read_mode, If don't found, the default is FLASH_QSPI_NORMAL_SPI_READ */
for (i = 0; i < sizeof(qspi_flash_ext_info_table) / sizeof(flash_qspi_flash_ext_info); i++) {
if ((qspi_flash_ext_info_table[i].mf_id == flash->chip.mf_id)
&& (qspi_flash_ext_info_table[i].type_id == flash->chip.type_id)
&& (qspi_flash_ext_info_table[i].capacity_id == flash->chip.capacity_id)) {
read_mode = qspi_flash_ext_info_table[i].read_mode;
}
}
/* determine qspi supports which read mode and set read_cmd_format struct */
switch (data_line_width) {
case 1:
qspi_set_read_cmd_format(flash, FLASH_CMD_READ_DATA, 1, 1, 0, 1);
break;
case 2:
if (read_mode & DUAL_IO) {
qspi_set_read_cmd_format(flash, FLASH_CMD_DUAL_IO_READ_DATA, 1, 2, 4, 2);
} else if (read_mode & DUAL_OUTPUT) {
qspi_set_read_cmd_format(flash, FLASH_CMD_DUAL_OUTPUT_READ_DATA, 1, 1, 8, 2);
} else {
qspi_set_read_cmd_format(flash, FLASH_CMD_READ_DATA, 1, 1, 0, 1);
}
break;
case 4:
if (read_mode & QUAD_IO) {
qspi_set_read_cmd_format(flash, FLASH_CMD_QUAD_IO_READ_DATA, 1, 4, 6, 4);
} else if (read_mode & QUAD_OUTPUT) {
qspi_set_read_cmd_format(flash, FLASH_CMD_QUAD_OUTPUT_READ_DATA, 1, 1, 8, 4);
} else {
qspi_set_read_cmd_format(flash, FLASH_CMD_READ_DATA, 1, 1, 0, 1);
}
break;
}
return result;
}
#endif /* FLASH_USING_QSPI */
/**
* hardware initialize
*/
static flash_err hardware_init(FLASH_t *flash) {
extern flash_err flash_spi_port_init(FLASH_t *flash);
flash_err result = FLASH_SUCCESS;
size_t i;
FLASH_ASSERT(flash);
result = flash_spi_port_init(flash);
if (result != FLASH_SUCCESS) {
return result;
}
#ifdef FLASH_USING_QSPI
/* set default read instruction */
flash->read_cmd_format.instruction = FLASH_CMD_READ_DATA;
#endif /* FLASH_USING_QSPI */
/* SPI write read function must be initialize */
FLASH_ASSERT(flash->spi.wr);
/* if the user don't configure flash chip information then using SFDP parameter or static flash parameter table */
if (flash->chip.capacity == 0 || flash->chip.write_mode == 0 || flash->chip.erase_gran == 0
|| flash->chip.erase_gran_cmd == 0) {
/* read JEDEC ID include manufacturer ID, memory type ID and flash capacity ID */
result = read_jedec_id(flash);
if (result != FLASH_SUCCESS) {
return result;
}
#ifdef FLASH_USING_SFDP
extern bool flash_read_sfdp(FLASH_t *flash);
/* read SFDP parameters */
if (flash_read_sfdp(flash)) {
flash->chip.name = NULL;
flash->chip.capacity = flash->sfdp.capacity;
/* only 1 byte or 256 bytes write mode for SFDP */
if (flash->sfdp.write_gran == 1) {
flash->chip.write_mode = FLASH_WM_BYTE;
} else {
flash->chip.write_mode = FLASH_WM_PAGE_256B;
}
/* find the the smallest erase sector size for eraser. then will use this size for erase granularity */
flash->chip.erase_gran = flash->sfdp.eraser[0].size;
flash->chip.erase_gran_cmd = flash->sfdp.eraser[0].cmd;
for (i = 1; i < FLASH_SFDP_ERASE_TYPE_MAX_NUM; i++) {
if (flash->sfdp.eraser[i].size != 0 && flash->chip.erase_gran > flash->sfdp.eraser[i].size) {
flash->chip.erase_gran = flash->sfdp.eraser[i].size;
flash->chip.erase_gran_cmd = flash->sfdp.eraser[i].cmd;
}
}
} else {
#endif
#ifdef FLASH_USING_FLASH_INFO_TABLE
/* read SFDP parameters failed then using FLASH library provided static parameter */
for (i = 0; i < sizeof(flash_chip_table) / sizeof(flash_chip); i++) {
if ((flash_chip_table[i].mf_id == flash->chip.mf_id)
&& (flash_chip_table[i].type_id == flash->chip.type_id)
&& (flash_chip_table[i].capacity_id == flash->chip.capacity_id)) {
flash->chip.name = flash_chip_table[i].name;
flash->chip.capacity = flash_chip_table[i].capacity;
flash->chip.write_mode = flash_chip_table[i].write_mode;
flash->chip.erase_gran = flash_chip_table[i].erase_gran;
flash->chip.erase_gran_cmd = flash_chip_table[i].erase_gran_cmd;
break;
}
}
#endif
#ifdef FLASH_USING_SFDP
}
#endif
}
if (flash->chip.capacity == 0 || flash->chip.write_mode == 0 || flash->chip.erase_gran == 0
|| flash->chip.erase_gran_cmd == 0) {
FLASH_INFO("Warning: This flash device is not found or not supported.");
return FLASH_ERR_NOT_FOUND;
} else {
const char *flash_mf_name = NULL;
/* find the manufacturer information */
for (i = 0; i < sizeof(mf_table) / sizeof(flash_mf); i++) {
if (mf_table[i].id == flash->chip.mf_id) {
flash_mf_name = mf_table[i].name;
break;
}
}
/* print manufacturer and flash chip name */
if (flash_mf_name && flash->chip.name) {
FLASH_INFO("Found a %s %s flash chip. Size is %ld bytes.", flash_mf_name, flash->chip.name,
flash->chip.capacity);
} else if (flash_mf_name) {
FLASH_INFO("Found a %s flash chip. Size is %ld bytes.", flash_mf_name, flash->chip.capacity);
} else {
FLASH_INFO("Found a flash chip. Size is %ld bytes.", flash->chip.capacity);
}
}
/* reset flash device */
result = reset(flash);
if (result != FLASH_SUCCESS) {
return result;
}
/* The flash all blocks is protected,so need change the flash status to unprotected before write and erase operate. */
if (flash->chip.write_mode & FLASH_WM_AAI) {
result = flash_write_status(flash, true, 0x00);
} else {
/* MX25L3206E */
if ((0xC2 == flash->chip.mf_id) && (0x20 == flash->chip.type_id) && (0x16 == flash->chip.capacity_id)) {
result = flash_write_status(flash, false, 0x00);
}
}
if (result != FLASH_SUCCESS) {
return result;
}
/* if the flash is large than 16MB (256Mb) then enter in 4-Byte addressing mode */
if (flash->chip.capacity > (1L << 24)) {
result = set_4_byte_address_mode(flash, true);
} else {
flash->addr_in_4_byte = false;
}
return result;
}
/**
* software initialize
*
* @param flash flash device
*
* @return result
*/
static flash_err software_init(const FLASH_t *flash) {
flash_err result = FLASH_SUCCESS;
FLASH_ASSERT(flash);
return result;
}
/**
* read flash data
*
* @param flash flash device
* @param addr start address
* @param size read size
* @param data read data pointer
*
* @return result
*/
flash_err flash_read(const FLASH_t *flash, uint32_t addr, size_t size, uint8_t *data) {
flash_err result = FLASH_SUCCESS;
const flash_spi *spi = &flash->spi;
uint8_t cmd_data[5 + FLASH_READ_DUMMY_BYTE_CNT];
uint8_t cmd_size;
uint8_t i;
FLASH_ASSERT(flash);
FLASH_ASSERT(data);
/* must be call this function after initialize OK */
FLASH_ASSERT(flash->init_ok);
/* check the flash address bound */
if (addr + size > flash->chip.capacity) {
FLASH_INFO("Error: Flash address is out of bound.");
return FLASH_ERR_ADDR_OUT_OF_BOUND;
}
/* lock SPI */
if (spi->lock) {
spi->lock(spi);
}
result = wait_busy(flash);
if (result == FLASH_SUCCESS) {
#ifdef FLASH_USING_QSPI
if (flash->read_cmd_format.instruction != FLASH_CMD_READ_DATA) {
result = spi->qspi_read(spi, addr, (flash_qspi_read_cmd_format *) &flash->read_cmd_format, data, size);
} else
#endif
{
#ifdef FLASH_USING_FAST_READ
cmd_data[0] = FLASH_CMD_FAST_READ_DATA;
#else
cmd_data[0] = FLASH_CMD_READ_DATA;
#endif
make_address_byte_array(flash, addr, &cmd_data[1]);
cmd_size = flash->addr_in_4_byte ? 5 : 4;
for (i = 0; i < FLASH_READ_DUMMY_BYTE_CNT; i++) {
cmd_data[cmd_size] = FLASH_DUMMY_DATA;
cmd_size++;
}
result = spi->wr(spi, cmd_data, cmd_size, data, size);
}
}
/* unlock SPI */
if (spi->unlock) {
spi->unlock(spi);
}
return result;
}
/**
* erase all flash data
*
* @param flash flash device
*
* @return result
*/
flash_err flash_chip_erase(const FLASH_t *flash) {
flash_err result = FLASH_SUCCESS;
const flash_spi *spi = &flash->spi;
uint8_t cmd_data[4];
FLASH_ASSERT(flash);
/* must be call this function after initialize OK */
FLASH_ASSERT(flash->init_ok);
/* lock SPI */
if (spi->lock) {
spi->lock(spi);
}
/* set the flash write enable */
result = set_write_enabled(flash, true);
if (result != FLASH_SUCCESS) {
goto __exit;
}
cmd_data[0] = FLASH_CMD_ERASE_CHIP;
/* dual-buffer write, like AT45DB series flash chip erase operate is different for other flash */
if (flash->chip.write_mode & FLASH_WM_DUAL_BUFFER) {
cmd_data[1] = 0x94;
cmd_data[2] = 0x80;
cmd_data[3] = 0x9A;
result = spi->wr(spi, cmd_data, 4, NULL, 0);
} else {
result = spi->wr(spi, cmd_data, 1, NULL, 0);
}
if (result != FLASH_SUCCESS) {
FLASH_INFO("Error: Flash chip erase SPI communicate error.");
goto __exit;
}
result = wait_busy(flash);
__exit:
/* set the flash write disable */
set_write_enabled(flash, false);
/* unlock SPI */
if (spi->unlock) {
spi->unlock(spi);
}
return result;
}
/**
* erase flash data
*
* @note It will erase align by erase granularity.
*
* @param flash flash device
* @param addr start address
* @param size erase size
*
* @return result
*/
flash_err flash_erase(const FLASH_t *flash, uint32_t addr, size_t size) {
extern size_t flash_sfdp_get_suitable_eraser(const FLASH_t *flash, uint32_t addr, size_t erase_size);
flash_err result = FLASH_SUCCESS;
const flash_spi *spi = &flash->spi;
uint8_t cmd_data[5], cmd_size, cur_erase_cmd;
size_t cur_erase_size;
FLASH_ASSERT(flash);
/* must be call this function after initialize OK */
FLASH_ASSERT(flash->init_ok);
/* check the flash address bound */
if (addr + size > flash->chip.capacity) {
FLASH_INFO("Error: Flash address is out of bound.");
return FLASH_ERR_ADDR_OUT_OF_BOUND;
}
if (addr == 0 && size == flash->chip.capacity) {
return flash_chip_erase(flash);
}
/* lock SPI */
if (spi->lock) {
spi->lock(spi);
}
/* loop erase operate. erase unit is erase granularity */
while (size) {
/* if this flash is support SFDP parameter, then used SFDP parameter supplies eraser */
#ifdef FLASH_USING_SFDP
size_t eraser_index;
if (flash->sfdp.available) {
/* get the suitable eraser for erase process from SFDP parameter */
eraser_index = flash_sfdp_get_suitable_eraser(flash, addr, size);
cur_erase_cmd = flash->sfdp.eraser[eraser_index].cmd;
cur_erase_size = flash->sfdp.eraser[eraser_index].size;
} else {
#else
{
#endif
cur_erase_cmd = flash->chip.erase_gran_cmd;
cur_erase_size = flash->chip.erase_gran;
}
/* set the flash write enable */
result = set_write_enabled(flash, true);
if (result != FLASH_SUCCESS) {
goto __exit;
}
cmd_data[0] = cur_erase_cmd;
make_address_byte_array(flash, addr, &cmd_data[1]);
cmd_size = flash->addr_in_4_byte ? 5 : 4;
result = spi->wr(spi, cmd_data, cmd_size, NULL, 0);
if (result != FLASH_SUCCESS) {
FLASH_INFO("Error: Flash erase SPI communicate error.");
goto __exit;
}
result = wait_busy(flash);
if (result != FLASH_SUCCESS) {
goto __exit;
}
/* make erase align and calculate next erase address */
if (addr % cur_erase_size != 0) {
if (size > cur_erase_size - (addr % cur_erase_size)) {
size -= cur_erase_size - (addr % cur_erase_size);
addr += cur_erase_size - (addr % cur_erase_size);
} else {
goto __exit;
}
} else {
if (size > cur_erase_size) {
size -= cur_erase_size;
addr += cur_erase_size;
} else {
goto __exit;
}
}
}
__exit:
/* set the flash write disable */
set_write_enabled(flash, false);
/* unlock SPI */
if (spi->unlock) {
spi->unlock(spi);
}
return result;
}
/**
* write flash data (no erase operate) for write 1 to 256 bytes per page mode or byte write mode
*
* @param flash flash device
* @param addr start address
* @param size write size
* @param write_gran write granularity bytes, only support 1 or 256
* @param data write data
*
* @return result
*/
static flash_err page256_or_1_byte_write(const FLASH_t *flash, uint32_t addr, size_t size, uint16_t write_gran,
const uint8_t *data) {
flash_err result = FLASH_SUCCESS;
const flash_spi *spi = &flash->spi;
static uint8_t cmd_data[5 + FLASH_WRITE_MAX_PAGE_SIZE];
uint8_t cmd_size;
size_t data_size;
FLASH_ASSERT(flash);
/* only support 1 or 256 */
FLASH_ASSERT(write_gran == 1 || write_gran == 256);
/* must be call this function after initialize OK */
FLASH_ASSERT(flash->init_ok);
/* check the flash address bound */
if (addr + size > flash->chip.capacity) {
FLASH_INFO("Error: Flash address is out of bound.");
return FLASH_ERR_ADDR_OUT_OF_BOUND;
}
/* lock SPI */
if (spi->lock) {
spi->lock(spi);
}
/* loop write operate. write unit is write granularity */
while (size) {
/* set the flash write enable */
result = set_write_enabled(flash, true);
if (result != FLASH_SUCCESS) {
goto __exit;
}
cmd_data[0] = FLASH_CMD_PAGE_PROGRAM;
make_address_byte_array(flash, addr, &cmd_data[1]);
cmd_size = flash->addr_in_4_byte ? 5 : 4;
/* make write align and calculate next write address */
if (addr % write_gran != 0) {
if (size > write_gran - (addr % write_gran)) {
data_size = write_gran - (addr % write_gran);
} else {
data_size = size;
}
} else {
if (size > write_gran) {
data_size = write_gran;
} else {
data_size = size;
}
}
size -= data_size;
addr += data_size;
memcpy(&cmd_data[cmd_size], data, data_size);
result = spi->wr(spi, cmd_data, cmd_size + data_size, NULL, 0);
if (result != FLASH_SUCCESS) {
FLASH_INFO("Error: Flash write SPI communicate error.");
goto __exit;
}
result = wait_busy(flash);
if (result != FLASH_SUCCESS) {
goto __exit;
}
data += data_size;
}
__exit:
/* set the flash write disable */
set_write_enabled(flash, false);
/* unlock SPI */
if (spi->unlock) {
spi->unlock(spi);
}
return result;
}
/**
* write flash data (no erase operate) for auto address increment mode
*
* If the address is odd number, it will place one 0xFF before the start of data for protect the old data.
* If the latest remain size is 1, it will append one 0xFF at the end of data for protect the old data.
*
* @param flash flash device
* @param addr start address
* @param size write size
* @param data write data
*
* @return result
*/
static flash_err aai_write(const FLASH_t *flash, uint32_t addr, size_t size, const uint8_t *data) {
flash_err result = FLASH_SUCCESS;
const flash_spi *spi = &flash->spi;
uint8_t cmd_data[8], cmd_size;
bool first_write = true;
FLASH_ASSERT(flash);
FLASH_ASSERT(flash->init_ok);
/* check the flash address bound */
if (addr + size > flash->chip.capacity) {
FLASH_INFO("Error: Flash address is out of bound.");
return FLASH_ERR_ADDR_OUT_OF_BOUND;
}
/* lock SPI */
if (spi->lock) {
spi->lock(spi);
}
/* The address must be even for AAI write mode. So it must write one byte first when address is odd. */
if (addr % 2 != 0) {
result = page256_or_1_byte_write(flash, addr++, 1, 1, data++);
if (result != FLASH_SUCCESS) {
goto __exit;
}
size--;
}
/* set the flash write enable */
result = set_write_enabled(flash, true);
if (result != FLASH_SUCCESS) {
goto __exit;
}
/* loop write operate. */
cmd_data[0] = FLASH_CMD_AAI_WORD_PROGRAM;
while (size >= 2) {
if (first_write) {
make_address_byte_array(flash, addr, &cmd_data[1]);
cmd_size = flash->addr_in_4_byte ? 5 : 4;
cmd_data[cmd_size] = *data;
cmd_data[cmd_size + 1] = *(data + 1);
first_write = false;
} else {
cmd_size = 1;
cmd_data[1] = *data;
cmd_data[2] = *(data + 1);
}
result = spi->wr(spi, cmd_data, cmd_size + 2, NULL, 0);
if (result != FLASH_SUCCESS) {
FLASH_INFO("Error: Flash write SPI communicate error.");
goto __exit;
}
result = wait_busy(flash);
if (result != FLASH_SUCCESS) {
goto __exit;
}
size -= 2;
addr += 2;
data += 2;
}
/* set the flash write disable for exit AAI mode */
result = set_write_enabled(flash, false);
/* write last one byte data when origin write size is odd */
if (result == FLASH_SUCCESS && size == 1) {
result = page256_or_1_byte_write(flash, addr, 1, 1, data);
}
__exit:
if (result != FLASH_SUCCESS) {
set_write_enabled(flash, false);
}
/* unlock SPI */
if (spi->unlock) {
spi->unlock(spi);
}
return result;
}
/**
* write flash data (no erase operate)
*
* @param flash flash device
* @param addr start address
* @param size write size
* @param data write data
*
* @return result
*/
flash_err flash_write(const FLASH_t *flash, uint32_t addr, size_t size, const uint8_t *data) {
flash_err result = FLASH_SUCCESS;
if (flash->chip.write_mode & FLASH_WM_PAGE_256B) {
result = page256_or_1_byte_write(flash, addr, size, 256, data);
} else if (flash->chip.write_mode & FLASH_WM_AAI) {
result = aai_write(flash, addr, size, data);
} else if (flash->chip.write_mode & FLASH_WM_DUAL_BUFFER) {
//TODO dual-buffer write mode
}
return result;
}
/**
* erase and write flash data
*
* @param flash flash device
* @param addr start address
* @param size write size
* @param data write data
*
* @return result
*/
flash_err flash_erase_write(const FLASH_t *flash, uint32_t addr, size_t size, const uint8_t *data) {
flash_err result = FLASH_SUCCESS;
result = flash_erase(flash, addr, size);
if (result == FLASH_SUCCESS) {
result = flash_write(flash, addr, size, data);
}
return result;
}
static flash_err reset(const FLASH_t *flash) {
flash_err result = FLASH_SUCCESS;
const flash_spi *spi = &flash->spi;
uint8_t cmd_data[2];
FLASH_ASSERT(flash);
cmd_data[0] = FLASH_CMD_ENABLE_RESET;
result = spi->wr(spi, cmd_data, 1, NULL, 0);
if (result == FLASH_SUCCESS) {
result = wait_busy(flash);
} else {
FLASH_INFO("Error: Flash device reset failed.");
return result;
}
cmd_data[1] = FLASH_CMD_RESET;
result = spi->wr(spi, &cmd_data[1], 1, NULL, 0);
if (result == FLASH_SUCCESS) {
result = wait_busy(flash);
}
if (result == FLASH_SUCCESS) {
FLASH_DEBUG("Flash device reset success.");
} else {
FLASH_INFO("Error: Flash device reset failed.");
}
return result;
}
static flash_err read_jedec_id(FLASH_t *flash) {
flash_err result = FLASH_SUCCESS;
const flash_spi *spi = &flash->spi;
uint8_t cmd_data[1], recv_data[3];
FLASH_ASSERT(flash);
cmd_data[0] = FLASH_CMD_JEDEC_ID;
result = spi->wr(spi, cmd_data, sizeof(cmd_data), recv_data, sizeof(recv_data));
if (result == FLASH_SUCCESS) {
flash->chip.mf_id = recv_data[0];
flash->chip.type_id = recv_data[1];
flash->chip.capacity_id = recv_data[2];
FLASH_DEBUG("The flash device manufacturer ID is 0x%02X, memory type ID is 0x%02X, capacity ID is 0x%02X.",
flash->chip.mf_id, flash->chip.type_id, flash->chip.capacity_id);
} else {
FLASH_INFO("Error: Read flash device JEDEC ID error.");
}
return result;
}
/**
* set the flash write enable or write disable
*
* @param flash flash device
* @param enabled true: enable false: disable
*
* @return result
*/
static flash_err set_write_enabled(const FLASH_t *flash, bool enabled) {
flash_err result = FLASH_SUCCESS;
uint8_t cmd, register_status;
FLASH_ASSERT(flash);
if (enabled) {
cmd = FLASH_CMD_WRITE_ENABLE;
} else {
cmd = FLASH_CMD_WRITE_DISABLE;
}
result = flash->spi.wr(&flash->spi, &cmd, 1, NULL, 0);
if (result == FLASH_SUCCESS) {
result = flash_read_status(flash, &register_status);
}
if (result == FLASH_SUCCESS) {
if (enabled && (register_status & FLASH_STATUS_REGISTER_WEL) == 0) {
FLASH_INFO("Error: Can't enable write status.");
return FLASH_ERR_WRITE;
} else if (!enabled && (register_status & FLASH_STATUS_REGISTER_WEL) != 0) {
FLASH_INFO("Error: Can't disable write status.");
return FLASH_ERR_WRITE;
}
}
return result;
}
/**
* enable or disable 4-Byte addressing for flash
*
* @note The 4-Byte addressing just supported for the flash capacity which is large then 16MB (256Mb).
*
* @param flash flash device
* @param enabled true: enable false: disable
*
* @return result
*/
static flash_err set_4_byte_address_mode(FLASH_t *flash, bool enabled) {
flash_err result = FLASH_SUCCESS;
uint8_t cmd;
FLASH_ASSERT(flash);
/* set the flash write enable */
result = set_write_enabled(flash, true);
if (result != FLASH_SUCCESS) {
return result;
}
if (enabled) {
cmd = FLASH_CMD_ENTER_4B_ADDRESS_MODE;
} else {
cmd = FLASH_CMD_EXIT_4B_ADDRESS_MODE;
}
result = flash->spi.wr(&flash->spi, &cmd, 1, NULL, 0);
if (result == FLASH_SUCCESS) {
flash->addr_in_4_byte = enabled ? true : false;
FLASH_DEBUG("%s 4-Byte addressing mode success.", enabled ? "Enter" : "Exit");
} else {
FLASH_INFO("Error: %s 4-Byte addressing mode failed.", enabled ? "Enter" : "Exit");
}
return result;
}
/**
* read flash register status
*
* @param flash flash device
* @param status register status
*
* @return result
*/
flash_err flash_read_status(const FLASH_t *flash, uint8_t *status) {
uint8_t cmd = FLASH_CMD_READ_STATUS_REGISTER;
FLASH_ASSERT(flash);
FLASH_ASSERT(status);
return flash->spi.wr(&flash->spi, &cmd, 1, status, 1);
}
static flash_err wait_busy(const FLASH_t *flash) {
flash_err result = FLASH_SUCCESS;
uint8_t status;
size_t retry_times = flash->retry.times;
FLASH_ASSERT(flash);
while (true) {
result = flash_read_status(flash, &status);
if (result == FLASH_SUCCESS && ((status & FLASH_STATUS_REGISTER_BUSY)) == 0) {
break;
}
/* retry counts */
FLASH_RETRY_PROCESS(flash->retry.delay, retry_times, result);
}
if (result != FLASH_SUCCESS || ((status & FLASH_STATUS_REGISTER_BUSY)) != 0) {
FLASH_INFO("Error: Flash wait busy has an error.");
}
return result;
}
static void make_address_byte_array(const FLASH_t *flash, uint32_t addr, uint8_t *array) {
uint8_t len, i;
FLASH_ASSERT(flash);
FLASH_ASSERT(array);
len = flash->addr_in_4_byte ? 4 : 3;
for (i = 0; i < len; i++) {
array[i] = (addr >> ((len - (i + 1)) * 8)) & 0xFF;
}
}
/**
* write status register
*
* @param flash flash device
* @param is_volatile true: volatile mode, false: non-volatile mode
* @param status register status
*
* @return result
*/
flash_err flash_write_status(const FLASH_t *flash, bool is_volatile, uint8_t status) {
flash_err result = FLASH_SUCCESS;
const flash_spi *spi = &flash->spi;
uint8_t cmd_data[2];
FLASH_ASSERT(flash);
if (is_volatile) {
cmd_data[0] = FLASH_VOLATILE_SR_WRITE_ENABLE;
result = spi->wr(spi, cmd_data, 1, NULL, 0);
} else {
result = set_write_enabled(flash, true);
}
if (result == FLASH_SUCCESS) {
cmd_data[0] = FLASH_CMD_WRITE_STATUS_REGISTER;
cmd_data[1] = status;
result = spi->wr(spi, cmd_data, 2, NULL, 0);
}
if (result != FLASH_SUCCESS) {
FLASH_INFO("Error: Write_status register failed.");
}
return result;
}