SPI Driver API in RTOS SDK v1.3

esp8266ac
Posts: 7
Joined: Sun Oct 04, 2015 12:39 am

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封装了上述功能吗? 还请帮忙澄清一下,谢谢!

esp8266ac
Posts: 7
Joined: Sun Oct 04, 2015 12:39 am

Re: SPI Driver API in RTOS SDK v1.3

Postby esp8266ac » Wed Mar 16, 2016 9:45 am

经过测试, 在做spi_flash_write 之前需要先Erase Sector;

SPI Flash 读写速度太慢了, 以一个Sector 为例, 从读回数据备份,擦除Sector,写回数据, 4K数据需要耗费5分钟左右;

有办法优化速度?

ESP_Faye
Posts: 1646
Joined: Mon Oct 27, 2014 11:08 am

Re: SPI Driver API in RTOS SDK v1.3

Postby ESP_Faye » Wed Mar 16, 2016 10:57 am

您好,

SPI Flash 读写速度太慢了, 以一个Sector 为例, 从读回数据备份,擦除Sector,写回数据, 4K数据需要耗费5分钟左右

擦写 Flash 不会这么慢的,能否提供您的测试代码,以供查证?

esp8266ac
Posts: 7
Joined: Sun Oct 04, 2015 12:39 am

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