From c63d9a88647052ebfa494d8928e348f3a1d061be Mon Sep 17 00:00:00 2001 From: armink Date: Sun, 28 Jan 2018 18:02:08 +0800 Subject: [PATCH 1/2] [DeviceDriver][SFUD] Update SFUD to latest version. --- components/drivers/spi/sfud/README.md | 65 ++++++++++++------- components/drivers/spi/sfud/inc/sfud.h | 9 +++ components/drivers/spi/sfud/inc/sfud_cfg.h | 5 -- components/drivers/spi/sfud/inc/sfud_def.h | 2 +- .../drivers/spi/sfud/inc/sfud_flash_def.h | 32 ++++----- components/drivers/spi/sfud/src/sfud.c | 11 ++-- components/drivers/spi/sfud/src/sfud_sfdp.c | 14 ++-- 7 files changed, 79 insertions(+), 59 deletions(-) diff --git a/components/drivers/spi/sfud/README.md b/components/drivers/spi/sfud/README.md index 4ad5f0ee44..0f16b03952 100644 --- a/components/drivers/spi/sfud/README.md +++ b/components/drivers/spi/sfud/README.md @@ -29,7 +29,9 @@ |:--:|:----:|:--:|:--:|:--:|:--:| |[W25Q40BV](http://microchip.ua/esp8266/W25Q40BV(EOL).pdf)|Winbond|4Mb|50Mhz|不支持|已停产| |[W25Q80DV](http://www.winbond.com/resource-files/w25q80dv_revg_07212015.pdf)|Winbond|8Mb|104Mhz|支持|| +|[W25Q16BV](https://media.digikey.com/pdf/Data%20Sheets/Winbond%20PDFs/W25Q16BV.pdf)|Winbond|16Mb|104Mhz|不支持| by [slipperstree](https://github.com/slipperstree)| |[W25Q16CV](http://www.winbond.com/resource-files/da00-w25q16cvf1.pdf)|Winbond|16Mb|104Mhz|支持|| +|[W25Q16DV](http://www.winbond.com/resource-files/w25q16dv%20revk%2005232016%20doc.pdf)|Winbond|16Mb|104Mhz|支持| by [slipperstree](https://github.com/slipperstree)| |[W25Q32BV](http://www.winbond.com/resource-files/w25q32bv_revi_100413_wo_automotive.pdf)|Winbond|32Mb|104Mhz|支持|| |[W25Q64CV](http://www.winbond.com/resource-files/w25q64cv_revh_052214[2].pdf)|Winbond|64Mb|80Mhz|支持|| |[W25Q128BV](http://www.winbond.com/resource-files/w25q128bv_revh_100313_wo_automotive.pdf)|Winbond|128Mb|104Mhz|支持|| @@ -44,6 +46,7 @@ |[GD25Q16B](http://www.gigadevice.com/product/detail/5/410.html)|GigaDevice|16Mb|120Mhz|不支持| by [TanekLiang](https://github.com/TanekLiang) | |[GD25Q64B](http://www.gigadevice.com/product/detail/5/364.html)|GigaDevice|64Mb|120Mhz|不支持|| |[S25FL216K](http://www.cypress.com/file/197346/download)|Cypress|16Mb|65Mhz|不支持|| +|[S25FL032P](http://www.cypress.com/file/196861/download)|Cypress|32Mb|104Mhz|不支持| by [yc_911](https://gitee.com/yc_911) | |[S25FL164K](http://www.cypress.com/file/196886/download)|Cypress|64Mb|108Mhz|支持|| |[A25L080](http://www.amictechnology.com/datasheets/A25L080.pdf)|AMIC|8Mb|100Mhz|不支持|| |[A25LQ64](http://www.amictechnology.com/datasheets/A25LQ64.pdf)|AMIC|64Mb|104Mhz|支持|| @@ -57,13 +60,25 @@ #### 2.2.1 初始化 SFUD 库 +将会调用 `sfud_device_init` ,初始化 Flash 设备表中的全部设备。如果只有一个 Flash 也可以只使用 `sfud_device_init` 进行单一初始化。 + > 注意:初始化完的 SPI Flash 默认都 **已取消写保护** 状态,如需开启写保护,请使用 sfud_write_status 函数修改 SPI Flash 状态。 ```C sfud_err sfud_init(void) ``` -#### 2.2.2 获取 Flash 设备对象 +#### 2.2.2 初始化指定的 Flash 设备 + +```C +sfud_err sfud_device_init(sfud_flash *flash) +``` + +|参数 |描述| +|:----- |:----| +|flash |待初始化的 Flash 设备| + +#### 2.2.3 获取 Flash 设备对象 在 SFUD 配置文件中会定义 Flash 设备表,负责存放所有将要使用的 Flash 设备对象,所以 SFUD 支持多个 Flash 设备同时驱动。设备表的配置在 `/sfud/inc/sfud_cfg.h` 中 `SFUD_FLASH_DEVICE_TABLE` 宏定义,详细配置方法参照 [2.3 配置方法 Flash](#23-配置方法))。本方法通过 Flash 设备位于设备表中索引值来返回 Flash 设备对象,超出设备表范围返回 `NULL` 。 @@ -75,21 +90,7 @@ sfud_flash *sfud_get_device(size_t index) |:----- |:----| |index |Flash 设备位于 FLash 设备表中的索引值| -#### 2.2.3 获取 Flash 设备总数 - -返回 Flash 设备表的总长度。 - -```C -size_t sfud_get_device_num(void) -``` - -#### 2.2.4 获取 Flash 设备表 - -```C -const sfud_flash *sfud_get_device_table(void) -``` - -#### 2.2.5 读取 Flash 数据 +#### 2.2.4 读取 Flash 数据 ```C sfud_err sfud_read(const sfud_flash *flash, uint32_t addr, size_t size, uint8_t *data) @@ -102,7 +103,7 @@ sfud_err sfud_read(const sfud_flash *flash, uint32_t addr, size_t size, uint8_t |size |从起始地址开始读取数据的总大小| |data |读取到的数据| -#### 2.2.6 擦除 Flash 数据 +#### 2.2.5 擦除 Flash 数据 > 注意:擦除操作将会按照 Flash 芯片的擦除粒度(详见 Flash 数据手册,一般为 block 大小。初始化完成后,可以通过 `sfud_flash->chip.erase_gran` 查看)对齐,请注意保证起始地址和擦除数据大小按照 Flash 芯片的擦除粒度对齐,否则执行擦除操作后,将会导致其他数据丢失。 @@ -116,7 +117,7 @@ sfud_err sfud_erase(const sfud_flash *flash, uint32_t addr, size_t size) |addr |起始地址| |size |从起始地址开始擦除数据的总大小| -#### 2.2.7 擦除 Flash 全部数据 +#### 2.2.6 擦除 Flash 全部数据 ```C sfud_err sfud_chip_erase(const sfud_flash *flash) @@ -126,7 +127,7 @@ sfud_err sfud_chip_erase(const sfud_flash *flash) |:----- |:----| |flash |Flash 设备对象| -#### 2.2.8 往 Flash 写数据 +#### 2.2.7 往 Flash 写数据 ```C sfud_err sfud_write(const sfud_flash *flash, uint32_t addr, size_t size, const uint8_t *data) @@ -139,7 +140,7 @@ sfud_err sfud_write(const sfud_flash *flash, uint32_t addr, size_t size, const u |size |从起始地址开始写入数据的总大小| |data |待写入的数据| -#### 2.2.9 先擦除再往 Flash 写数据 +#### 2.2.8 先擦除再往 Flash 写数据 > 注意:擦除操作将会按照 Flash 芯片的擦除粒度(详见 Flash 数据手册,一般为 block 大小。初始化完成后,可以通过 `sfud_flash->chip.erase_gran` 查看)对齐,请注意保证起始地址和擦除数据大小按照 Flash 芯片的擦除粒度对齐,否则执行擦除操作后,将会导致其他数据丢失。 @@ -154,7 +155,7 @@ sfud_err sfud_erase_write(const sfud_flash *flash, uint32_t addr, size_t size, c |size |从起始地址开始写入数据的总大小| |data |待写入的数据| -#### 2.2.10 读取 Flash 状态 +#### 2.2.9 读取 Flash 状态 ```C sfud_err sfud_read_status(const sfud_flash *flash, uint8_t *status) @@ -165,7 +166,7 @@ sfud_err sfud_read_status(const sfud_flash *flash, uint8_t *status) |flash |Flash 设备对象| |status |当前状态寄存器值| -#### 2.2.11 写(修改) Flash 状态 +#### 2.2.10 写(修改) Flash 状态 ```C sfud_err sfud_write_status(const sfud_flash *flash, bool is_volatile, uint8_t status) @@ -197,9 +198,25 @@ sfud_err sfud_write_status(const sfud_flash *flash, bool is_volatile, uint8_t st > 注意:关闭后该库只驱动支持 SFDP 规范的 Flash,也会适当的降低部分代码量。另外 2.3.2 及 2.3.3 这两个宏定义至少定义一种,也可以两种方式都选择。 -#### 2.3.4 Flash 设备表 +#### 2.3.4 既不使用 SFDP ,也不使用 Flash 参数信息表 -主要修改 `SFUD_FLASH_DEVICE_TABLE` 这个宏定义,示例如下: +为了进一步降低代码量,`SFUD_USING_SFDP` 与 `SFUD_USING_FLASH_INFO_TABLE` 也可以 **都不定义** 。 + +此时,只要在定义 Flash 设备时,指定好 Flash 参数,之后再调用 `sfud_device_init` 对该设备进行初始化。参考如下代码: + +```C +sfud_flash sfud_norflash0 = { + .name = "norflash0", + .spi.name = "SPI1", + .chip = { "W25Q64FV", SFUD_MF_ID_WINBOND, 0x40, 0x17, 8L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 } }; +...... +sfud_device_init(&sfud_norflash0); +...... +``` + +#### 2.3.5 Flash 设备表 + +如果产品中存在多个 Flash ,可以添加 Flash 设备表。修改 `SFUD_FLASH_DEVICE_TABLE` 这个宏定义,示例如下: ```C enum { diff --git a/components/drivers/spi/sfud/inc/sfud.h b/components/drivers/spi/sfud/inc/sfud.h index 1d7642ef9a..be1ccb208e 100644 --- a/components/drivers/spi/sfud/inc/sfud.h +++ b/components/drivers/spi/sfud/inc/sfud.h @@ -43,6 +43,15 @@ extern "C" { */ sfud_err sfud_init(void); +/** + * SFUD initialize by flash device + * + * @param flash flash device + * + * @return result + */ +sfud_err sfud_device_init(sfud_flash *flash); + /** * get flash device by its index which in the flash information table * diff --git a/components/drivers/spi/sfud/inc/sfud_cfg.h b/components/drivers/spi/sfud/inc/sfud_cfg.h index eb79f84dc3..5dd43fa2e4 100644 --- a/components/drivers/spi/sfud/inc/sfud_cfg.h +++ b/components/drivers/spi/sfud/inc/sfud_cfg.h @@ -52,11 +52,6 @@ #define SFUD_USING_FLASH_INFO_TABLE #endif - -#if !defined(RT_SFUD_USING_SFDP) && !defined(RT_SFUD_USING_FLASH_INFO_TABLE) -#error "Please configure RT_SFUD_USING_SFDP or RT_SFUD_USING_FLASH_INFO_TABLE at least one kind of mode (in rtconfig.h)." -#endif - #define SFUD_FLASH_DEVICE_TABLE {0} #endif /* _SFUD_CFG_H_ */ diff --git a/components/drivers/spi/sfud/inc/sfud_def.h b/components/drivers/spi/sfud/inc/sfud_def.h index 1b5636c1b4..bb460d3f77 100644 --- a/components/drivers/spi/sfud/inc/sfud_def.h +++ b/components/drivers/spi/sfud/inc/sfud_def.h @@ -77,7 +77,7 @@ if (!(EXPR)) \ else {if (__delay_temp) {__delay_temp();} retry --;} /* software version number */ -#define SFUD_SW_VERSION "1.0.2" +#define SFUD_SW_VERSION "1.0.4" /* * all defined supported command */ diff --git a/components/drivers/spi/sfud/inc/sfud_flash_def.h b/components/drivers/spi/sfud/inc/sfud_flash_def.h index 26b5ace3b4..29878229b7 100644 --- a/components/drivers/spi/sfud/inc/sfud_flash_def.h +++ b/components/drivers/spi/sfud/inc/sfud_flash_def.h @@ -107,21 +107,23 @@ typedef struct { * notice me for update it. The configuration information name and index reference the sfud_flash_chip structure. * | name | mf_id | type_id | capacity_id | capacity | write_mode | erase_gran | erase_gran_cmd | */ -#define SFUD_FLASH_CHIP_TABLE \ -{ \ - {"AT45DB161E", SFUD_MF_ID_ATMEL, 0x26, 0x00, 2*1024*1024, SFUD_WM_BYTE|SFUD_WM_DUAL_BUFFER, 512, 0x81}, \ - {"W25Q40BV", SFUD_MF_ID_WINBOND, 0x40, 0x13, 512*1024, SFUD_WM_PAGE_256B, 4096, 0x20}, \ - {"SST25VF016B", SFUD_MF_ID_SST, 0x25, 0x41, 2*1024*1024, SFUD_WM_BYTE|SFUD_WM_AAI, 4096, 0x20}, \ - {"M25P32", SFUD_MF_ID_MICRON, 0x20, 0x16, 4*1024*1024, SFUD_WM_PAGE_256B, 64*1024, 0xD8}, \ - {"M25P80", SFUD_MF_ID_MICRON, 0x20, 0x14, 1*1024*1024, SFUD_WM_PAGE_256B, 64*1024, 0xD8}, \ - {"M25P40", SFUD_MF_ID_MICRON, 0x20, 0x13, 512*1024, SFUD_WM_PAGE_256B, 64*1024, 0xD8}, \ - {"EN25Q32B", SFUD_MF_ID_EON, 0x30, 0x16, 4*1024*1024, SFUD_WM_PAGE_256B, 4096, 0x20}, \ - {"GD25Q64B", SFUD_MF_ID_GIGADEVICE, 0x40, 0x17, 8*1024*1024, SFUD_WM_PAGE_256B, 4096, 0x20}, \ - {"GD25Q16B", SFUD_MF_ID_GIGADEVICE, 0x40, 0x15, 2*1024*1024, SFUD_WM_PAGE_256B, 4096, 0x20}, \ - {"S25FL216K", SFUD_MF_ID_CYPRESS, 0x40, 0x15, 2*1024*1024, SFUD_WM_PAGE_256B, 4096, 0x20}, \ - {"A25L080", SFUD_MF_ID_AMIC, 0x30, 0x14, 1*1024*1024, SFUD_WM_PAGE_256B, 4096, 0x20}, \ - {"F25L004", SFUD_MF_ID_ESMT, 0x20, 0x13, 512*1024, SFUD_WM_BYTE|SFUD_WM_AAI, 4096, 0x20}, \ - {"PCT25VF016B", SFUD_MF_ID_SST, 0x25, 0x41, 2*1024*1024, SFUD_WM_BYTE|SFUD_WM_AAI, 4096, 0x20}, \ +#define SFUD_FLASH_CHIP_TABLE \ +{ \ + {"AT45DB161E", SFUD_MF_ID_ATMEL, 0x26, 0x00, 2L*1024L*1024L, SFUD_WM_BYTE|SFUD_WM_DUAL_BUFFER, 512, 0x81}, \ + {"W25Q40BV", SFUD_MF_ID_WINBOND, 0x40, 0x13, 512L*1024L, SFUD_WM_PAGE_256B, 4096, 0x20}, \ + {"W25Q16BV", SFUD_MF_ID_WINBOND, 0x40, 0x15, 2L*1024L*1024L, SFUD_WM_PAGE_256B, 4096, 0x20}, \ + {"SST25VF016B", SFUD_MF_ID_SST, 0x25, 0x41, 2L*1024L*1024L, SFUD_WM_BYTE|SFUD_WM_AAI, 4096, 0x20}, \ + {"M25P32", SFUD_MF_ID_MICRON, 0x20, 0x16, 4L*1024L*1024L, SFUD_WM_PAGE_256B, 64L*1024L, 0xD8}, \ + {"M25P80", SFUD_MF_ID_MICRON, 0x20, 0x14, 1L*1024L*1024L, SFUD_WM_PAGE_256B, 64L*1024L, 0xD8}, \ + {"M25P40", SFUD_MF_ID_MICRON, 0x20, 0x13, 512L*1024L, SFUD_WM_PAGE_256B, 64L*1024L, 0xD8}, \ + {"EN25Q32B", SFUD_MF_ID_EON, 0x30, 0x16, 4L*1024L*1024L, SFUD_WM_PAGE_256B, 4096, 0x20}, \ + {"GD25Q64B", SFUD_MF_ID_GIGADEVICE, 0x40, 0x17, 8L*1024L*1024L, SFUD_WM_PAGE_256B, 4096, 0x20}, \ + {"GD25Q16B", SFUD_MF_ID_GIGADEVICE, 0x40, 0x15, 2L*1024L*1024L, SFUD_WM_PAGE_256B, 4096, 0x20}, \ + {"S25FL216K", SFUD_MF_ID_CYPRESS, 0x40, 0x15, 2L*1024L*1024L, SFUD_WM_PAGE_256B, 4096, 0x20}, \ + {"S25FL032P", SFUD_MF_ID_CYPRESS, 0x02, 0x15, 4L*1024L*1024L, SFUD_WM_PAGE_256B, 4096, 0x20}, \ + {"A25L080", SFUD_MF_ID_AMIC, 0x30, 0x14, 1L*1024L*1024L, SFUD_WM_PAGE_256B, 4096, 0x20}, \ + {"F25L004", SFUD_MF_ID_ESMT, 0x20, 0x13, 512L*1024L, SFUD_WM_BYTE|SFUD_WM_AAI, 4096, 0x20}, \ + {"PCT25VF016B", SFUD_MF_ID_SST, 0x25, 0x41, 2L*1024L*1024L, SFUD_WM_BYTE|SFUD_WM_AAI, 4096, 0x20}, \ } #endif /* SFUD_USING_FLASH_INFO_TABLE */ diff --git a/components/drivers/spi/sfud/src/sfud.c b/components/drivers/spi/sfud/src/sfud.c index 475cbd5d9e..9b220e3e63 100644 --- a/components/drivers/spi/sfud/src/sfud.c +++ b/components/drivers/spi/sfud/src/sfud.c @@ -36,10 +36,6 @@ #error "Please configure the flash device information table in (in sfud_cfg.h)." #endif -#if !defined(SFUD_USING_SFDP) && !defined(SFUD_USING_FLASH_INFO_TABLE) -#error "Please configure SFUD_USING_SFDP or SFUD_USING_FLASH_INFO_TABLE at least one kind of mode (in sfud_cfg.h)." -#endif - /* user configured flash device information table */ static sfud_flash flash_table[] = SFUD_FLASH_DEVICE_TABLE; /* supported manufacturer information table */ @@ -251,7 +247,7 @@ static sfud_err hardware_init(sfud_flash *flash) { return result; } - /* I found when the flash read mode is supported AAI mode. The flash all blocks is protected, + /* I found when the flash write mode is supported AAI mode. The flash all blocks is protected, * so need change the flash status to unprotected before write and erase operate. */ if (flash->chip.write_mode & SFUD_WM_AAI) { result = sfud_write_status(flash, true, 0x00); @@ -261,7 +257,7 @@ static sfud_err hardware_init(sfud_flash *flash) { } /* if the flash is large than 16MB (256Mb) then enter in 4-Byte addressing mode */ - if (flash->chip.capacity > (1 << 24)) { + if (flash->chip.capacity > (1L << 24)) { result = set_4_byte_address_mode(flash, true); } else { flash->addr_in_4_byte = false; @@ -401,7 +397,7 @@ sfud_err sfud_erase(const sfud_flash *flash, uint32_t addr, size_t size) { sfud_err result = SFUD_SUCCESS; const sfud_spi *spi = &flash->spi; uint8_t cmd_data[5], cmd_size, cur_erase_cmd; - size_t eraser_index, cur_erase_size; + size_t cur_erase_size; SFUD_ASSERT(flash); /* must be call this function after initialize OK */ @@ -425,6 +421,7 @@ sfud_err sfud_erase(const sfud_flash *flash, uint32_t addr, size_t size) { while (size) { /* if this flash is support SFDP parameter, then used SFDP parameter supplies eraser */ #ifdef SFUD_USING_SFDP + size_t eraser_index; if (flash->sfdp.available) { /* get the suitable eraser for erase process from SFDP parameter */ eraser_index = sfud_sfdp_get_suitable_eraser(flash, addr, size); diff --git a/components/drivers/spi/sfud/src/sfud_sfdp.c b/components/drivers/spi/sfud/src/sfud_sfdp.c index 32873ab8cb..0c81e073e6 100644 --- a/components/drivers/spi/sfud/src/sfud_sfdp.c +++ b/components/drivers/spi/sfud/src/sfud_sfdp.c @@ -159,7 +159,7 @@ static bool read_basic_header(const sfud_flash *flash, sfdp_para_header *basic_h basic_header->minor_rev = header[1]; basic_header->major_rev = header[2]; basic_header->len = header[3]; - basic_header->ptp = header[4] | header[5] << 8 | header[6] << 16; + basic_header->ptp = (long)header[4] | (long)header[5] << 8 | (long)header[6] << 16; /* check JEDEC basic flash parameter header */ if (basic_header->major_rev > SUPPORT_MAX_SFDP_MAJOR_REV) { SFUD_INFO("Error: This reversion(V%d.%d) JEDEC basic flash parameter header is not supported.", @@ -171,7 +171,7 @@ static bool read_basic_header(const sfud_flash *flash, sfdp_para_header *basic_h return false; } SFUD_DEBUG("Check JEDEC basic flash parameter header is OK. The table id is %d, reversion is V%d.%d," - " length is %d, parameter table pointer is 0x%06X.", basic_header->id, basic_header->major_rev, + " length is %d, parameter table pointer is 0x%06lX.", basic_header->id, basic_header->major_rev, basic_header->minor_rev, basic_header->len, basic_header->ptp); return true; @@ -196,7 +196,7 @@ static bool read_basic_table(sfud_flash *flash, sfdp_para_header *basic_header) /* read JEDEC basic flash parameter table */ if (read_sfdp_data(flash, table_addr, table, sizeof(table)) != SFUD_SUCCESS) { - SFUD_INFO("Error: Can't read JEDEC basic flash parameter table."); + SFUD_INFO("Warning: Can't read JEDEC basic flash parameter table."); return false; } /* print JEDEC basic flash parameter table info */ @@ -287,7 +287,7 @@ static bool read_basic_table(sfud_flash *flash, sfdp_para_header *basic_header) return false; } /* get flash memory capacity */ - uint32_t table2_temp = (table[7] << 24) | (table[6] << 16) | (table[5] << 8) | table[4]; + uint32_t table2_temp = ((long)table[7] << 24) | ((long)table[6] << 16) | ((long)table[5] << 8) | (long)table[4]; switch ((table[7] & (0x01 << 7)) >> 7) { case 0: sfdp->capacity = 1 + (table2_temp >> 3); @@ -299,14 +299,14 @@ static bool read_basic_table(sfud_flash *flash, sfdp_para_header *basic_header) SFUD_INFO("Error: The flash capacity is grater than 32 Gb/ 4 GB! Not Supported."); return false; } - sfdp->capacity = 1 << (table2_temp - 3); + sfdp->capacity = 1L << (table2_temp - 3); break; } SFUD_DEBUG("Capacity is %ld Bytes.", sfdp->capacity); /* get erase size and erase command */ for (i = 0, j = 0; i < SFUD_SFDP_ERASE_TYPE_MAX_NUM; i++) { if (table[28 + 2 * i] != 0x00) { - sfdp->eraser[j].size = 1 << table[28 + 2 * i]; + sfdp->eraser[j].size = 1L << table[28 + 2 * i]; sfdp->eraser[j].cmd = table[28 + 2 * i + 1]; SFUD_DEBUG("Flash device supports %ldKB block erase. Command is 0x%02X.", sfdp->eraser[j].size / 1024, sfdp->eraser[j].cmd); @@ -344,7 +344,7 @@ static sfud_err read_sfdp_data(const sfud_flash *flash, uint32_t addr, uint8_t * }; SFUD_ASSERT(flash); - SFUD_ASSERT(addr < 1 << 24); + SFUD_ASSERT(addr < 1L << 24); SFUD_ASSERT(read_buf); SFUD_ASSERT(flash->spi.wr); From 7b8f8d961587c66312d8b68f3c931a065b9c1b7b Mon Sep 17 00:00:00 2001 From: armink Date: Sun, 28 Jan 2018 18:29:33 +0800 Subject: [PATCH 2/2] [DeviceDriver][SFUD] Fix flash device lock not detach when an error occurred. #1167 --- components/drivers/spi/spi_flash_sfud.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/components/drivers/spi/spi_flash_sfud.c b/components/drivers/spi/spi_flash_sfud.c index c94effa613..d86179aaf8 100644 --- a/components/drivers/spi/spi_flash_sfud.c +++ b/components/drivers/spi/spi_flash_sfud.c @@ -250,8 +250,13 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const spi_flash_dev_name_bak = (char *) rt_malloc(rt_strlen(spi_flash_dev_name) + 1); spi_dev_name_bak = (char *) rt_malloc(rt_strlen(spi_dev_name) + 1); - if (rtt_dev && sfud_dev && spi_flash_dev_name_bak && spi_dev_name_bak) { + if (rtt_dev) { rt_memset(rtt_dev, 0, sizeof(struct spi_flash_device)); + /* initialize lock */ + rt_mutex_init(&(rtt_dev->lock), spi_flash_dev_name, RT_IPC_FLAG_FIFO); + } + + if (rtt_dev && sfud_dev && spi_flash_dev_name_bak && spi_dev_name_bak) { rt_memset(sfud_dev, 0, sizeof(sfud_flash)); rt_strncpy(spi_flash_dev_name_bak, spi_flash_dev_name, rt_strlen(spi_flash_dev_name)); rt_strncpy(spi_dev_name_bak, spi_dev_name, rt_strlen(spi_dev_name)); @@ -268,8 +273,6 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const } sfud_dev->spi.name = spi_dev_name_bak; rt_spi_configure(rtt_dev->rt_spi_device, &cfg); - /* initialize lock */ - rt_mutex_init(&(rtt_dev->lock), spi_flash_dev_name, RT_IPC_FLAG_FIFO); } /* SFUD flash device initialize */ { @@ -308,6 +311,10 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const } error: + + if (rtt_dev) { + rt_mutex_detach(&(rtt_dev->lock)); + } /* may be one of objects memory was malloc success, so need free all */ rt_free(rtt_dev); rt_free(sfud_dev);