SPI Driver API in RTOS SDK v1.3
SPI Driver API in RTOS SDK v1.3
Postby esp8266ac » Tue Mar 08, 2016 2:41 pm
RTOS SDKv1.3.0 中提供 SPI Driver API:
• uint32 spi_flash_get_id (void)
Get ID info of SPI Flash.
• SpiFlashOpResult spi_flash_read_status (uint32 ∗ status)
Read state register of SPI Flash.
• SpiFlashOpResult spi_flash_write_status (uint32 status_value)
Write state register of SPI Flash.
• SpiFlashOpResult spi_flash_erase_sector (uint16 sec)
Erase the Flash sector.
• SpiFlashOpResult spi_flash_write (uint32 des_addr, uint32 ∗ src_addr, uint32 size)
Write data to Flash.
• SpiFlashOpResult spi_flash_read (uint32 src_addr, uint32 ∗ des_addr, uint32 size)
Read data from Flash.
而在RTOS SDK Programming Guide中提及:
9. 基本示例:从 Flash 读取数据
(1) Flash 读写数据,要求必须 4 字节对齐。例如,从 Flash 读取数据:
(2) 向 Flash 写入数据同理,要求 4 字节对齐,使用接口 spi_flash_write 即可。
(3) 编译应用程序,生成固件烧录到 ESP8266 硬件模组中。
(4) 硬件模组掉电,切换到运行模式,重新上电,运行应用程序。
Flash Write 一般是要先Read/Backup 有用数据, Erase Sector, 合并备份的数据和要写入的数据,把所有数据写到Flash;
ProgrammingGuide 说 直接使用 spi_flash_write, 难道是spi_flash_write封装了上述功能吗? 还请帮忙澄清一下,谢谢!
• uint32 spi_flash_get_id (void)
Get ID info of SPI Flash.
• SpiFlashOpResult spi_flash_read_status (uint32 ∗ status)
Read state register of SPI Flash.
• SpiFlashOpResult spi_flash_write_status (uint32 status_value)
Write state register of SPI Flash.
• SpiFlashOpResult spi_flash_erase_sector (uint16 sec)
Erase the Flash sector.
• SpiFlashOpResult spi_flash_write (uint32 des_addr, uint32 ∗ src_addr, uint32 size)
Write data to Flash.
• SpiFlashOpResult spi_flash_read (uint32 src_addr, uint32 ∗ des_addr, uint32 size)
Read data from Flash.
而在RTOS SDK Programming Guide中提及:
9. 基本示例:从 Flash 读取数据
(1) Flash 读写数据,要求必须 4 字节对齐。例如,从 Flash 读取数据:
(2) 向 Flash 写入数据同理,要求 4 字节对齐,使用接口 spi_flash_write 即可。
(3) 编译应用程序,生成固件烧录到 ESP8266 硬件模组中。
(4) 硬件模组掉电,切换到运行模式,重新上电,运行应用程序。
Flash Write 一般是要先Read/Backup 有用数据, Erase Sector, 合并备份的数据和要写入的数据,把所有数据写到Flash;
ProgrammingGuide 说 直接使用 spi_flash_write, 难道是spi_flash_write封装了上述功能吗? 还请帮忙澄清一下,谢谢!
Re: SPI Driver API in RTOS SDK v1.3
Postby esp8266ac » Wed Mar 16, 2016 11:55 pm
请帮忙 看一下代码有没有问题,谢谢!
Code: Select all
uint8 param_set_entry(uint8 EntryNum, uint16 EntrySize, uint32 *EntryPtr)
{
uint8 retval;
uint8 retry;
uint16 sect;
uint16 size;
uint32 *buff = NULL;
SpiFlashOpResult result;
if((EntryNum < PARAM_ENTRY_MAX) && (EntrySize <= CONFIG_PARAM_ENTRY_SIZE))
{
sect = (CONFIG_PARAM_USRSEC_OFFSET + EntryNum * CONFIG_PARAM_ENTRY_SIZE) / CONFIG_SPI_FLASH_SEC_SIZE;
/* Allocate buff for a sector */
if( (PARAM_ENTRY_MAX * CONFIG_PARAM_ENTRY_SIZE) <= CONFIG_SPI_FLASH_SEC_SIZE )
{
/* There is only 1 sector used, the current Entry locates in the 1st Sector */
size = PARAM_ENTRY_MAX * CONFIG_PARAM_ENTRY_SIZE;
}
else /* There is only 2 sector used*/
{
if((EntryNum * CONFIG_PARAM_ENTRY_SIZE) >= CONFIG_SPI_FLASH_SEC_SIZE)
{
/*the current Entry locates in the 2st Sector*/
size = PARAM_ENTRY_MAX * CONFIG_PARAM_ENTRY_SIZE - CONFIG_SPI_FLASH_SEC_SIZE;
}
else /*the current Entry locates in the 1st Sector*/
{
size = CONFIG_SPI_FLASH_SEC_SIZE;
}
}
buff = (uint32 *)malloc(size);
if(NULL == buff)
return false;
/*Try to backup Entries to be erased */
retry = CONFIG_PARAM_RETRY;
retval = false;
while(retry)
{
result = spi_flash_read((sect * CONFIG_SPI_FLASH_SEC_SIZE), buff, (uint32)size);
switch(result)
{
case SPI_FLASH_RESULT_OK:
{
retval = true;
retry = 1;
break;
}
default:
{
break;;
}
}
retry --;
}
if(false == retval)
goto Failed;
/*Try to erased sector */
retry = CONFIG_PARAM_RETRY;
retval = false;
while(retry)
{
result = spi_flash_erase_sector(sect);
switch(result)
{
case SPI_FLASH_RESULT_OK:
{
retval = true;
retry = 1;
break;
}
default:
{
break;;
}
}
retry --;
}
if(false == retval)
goto Failed;
/* Merge Data into buffer */
memcpy((buff + ((EntryNum * CONFIG_PARAM_ENTRY_SIZE) % CONFIG_SPI_FLASH_SEC_SIZE)), EntryPtr, EntrySize);
/*Try to Write Entries to Erased Sector */
retry = CONFIG_PARAM_RETRY;
retval = false;
while(retry)
{
result = spi_flash_write((sect * CONFIG_SPI_FLASH_SEC_SIZE), buff, (uint32)size);
switch(result)
{
case SPI_FLASH_RESULT_OK:
{
retval = true;
retry = 1;
break;
}
default:
{
break;;
}
}
retry --;
}
Failed:
if(NULL != buff)
free(buff);
return retval;
}
return false;
}
Who is online
Users browsing this forum: No registered users and 189 guests
Login
Newbies Start Here
Are you new to ESP8266?
Unsure what to do?
Dunno where to start?
Start right here!
Latest SDK
Documentation
Complete listing of the official ESP8266 related documentation release by ESPRESSIF!
Must read here!
- All times are UTC+08:00
- Top
- Delete all board cookies
About Us
Espressif Systems is a fabless semiconductor company providing cutting-edge low power WiFi SoCs and wireless solutions for wireless communications and Internet of Things applications. We are the manufacturer of ESP8266EX.