diff --git a/bsp/stm32/stm32f407-atk-explorer/board/Kconfig b/bsp/stm32/stm32f407-atk-explorer/board/Kconfig index 70e0c09762..24228acfec 100644 --- a/bsp/stm32/stm32f407-atk-explorer/board/Kconfig +++ b/bsp/stm32/stm32f407-atk-explorer/board/Kconfig @@ -100,7 +100,8 @@ menu "Onboard Peripheral Drivers" select BSP_USING_SPI_FLASH select BSP_USING_FS select PKG_USING_FAL - select FAL_USING_SFUD_PORT + select FAL_USING_AUTO_INIT + select FAL_PART_HAS_TABLE_CFG select PKG_USING_LITTLEFS select RT_USING_SYSTEM_WORKQUEUE default n diff --git a/bsp/stm32/stm32f407-atk-explorer/board/SConscript b/bsp/stm32/stm32f407-atk-explorer/board/SConscript index 64b8ab3f41..e81e43dcbf 100644 --- a/bsp/stm32/stm32f407-atk-explorer/board/SConscript +++ b/bsp/stm32/stm32f407-atk-explorer/board/SConscript @@ -20,6 +20,8 @@ if GetDepend(['BSP_USING_SPI_FLASH']): if GetDepend(['BSP_USING_FS']): src += Glob('ports/drv_filesystem.c') + if GetDepend(['BSP_USING_SPI_FLASH_LITTLEFS']): + src += Glob('ports/fal_spi_flash_sfud_port.c') if GetDepend(['BSP_USING_SRAM']): src += Glob('ports/drv_sram.c') diff --git a/bsp/stm32/stm32f407-atk-explorer/board/ports/drv_filesystem.c b/bsp/stm32/stm32f407-atk-explorer/board/ports/drv_filesystem.c index 529b1e1157..78ee5d0967 100644 --- a/bsp/stm32/stm32f407-atk-explorer/board/ports/drv_filesystem.c +++ b/bsp/stm32/stm32f407-atk-explorer/board/ports/drv_filesystem.c @@ -83,7 +83,9 @@ static int onboard_spiflash_mount(void) { struct rt_device *mtd_dev = RT_NULL; +#ifndef FAL_USING_AUTO_INIT fal_init(); +#endif mtd_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME); if (!mtd_dev) diff --git a/bsp/stm32/stm32f407-atk-explorer/board/ports/fal_cfg.h b/bsp/stm32/stm32f407-atk-explorer/board/ports/fal_cfg.h index 304efe869d..adeb531953 100644 --- a/bsp/stm32/stm32f407-atk-explorer/board/ports/fal_cfg.h +++ b/bsp/stm32/stm32f407-atk-explorer/board/ports/fal_cfg.h @@ -15,7 +15,7 @@ #include #ifdef BSP_USING_SPI_FLASH_LITTLEFS -extern struct fal_flash_dev nor_flash0; +extern struct fal_flash_dev w25q128; #else #define FLASH_SIZE_GRANULARITY_16K (4 * 16 * 1024) #define FLASH_SIZE_GRANULARITY_64K (64 * 1024) @@ -34,7 +34,7 @@ extern const struct fal_flash_dev stm32_onchip_flash_128k; #ifdef BSP_USING_SPI_FLASH_LITTLEFS #define FAL_FLASH_DEV_TABLE \ { \ - &nor_flash0, \ + &w25q128, \ } #else #define FAL_FLASH_DEV_TABLE \ @@ -52,7 +52,7 @@ extern const struct fal_flash_dev stm32_onchip_flash_128k; #ifdef BSP_USING_SPI_FLASH_LITTLEFS #define FAL_PART_TABLE \ { \ - {FAL_PART_MAGIC_WROD, "spiflash0", FAL_USING_NOR_FLASH_DEV_NAME, 0 , 16 * 1024 * 1024, 0}, \ + {FAL_PART_MAGIC_WROD, "spiflash0", "W25Q128", 0 , 16 * 1024 * 1024, 0}, \ } #else #define FAL_PART_TABLE \ diff --git a/bsp/stm32/stm32f407-atk-explorer/board/ports/fal_spi_flash_sfud_port.c b/bsp/stm32/stm32f407-atk-explorer/board/ports/fal_spi_flash_sfud_port.c new file mode 100644 index 0000000000..96da95b4c4 --- /dev/null +++ b/bsp/stm32/stm32f407-atk-explorer/board/ports/fal_spi_flash_sfud_port.c @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-08-07 Meco Man first version + */ + +#include +#include + +#ifdef RT_USING_SFUD +#include +#endif + +static int init(void); +static int read(long offset, uint8_t *buf, size_t size); +static int write(long offset, const uint8_t *buf, size_t size); +static int erase(long offset, size_t size); + +static sfud_flash_t sfud_dev = NULL; +struct fal_flash_dev w25q128 = +{ + .name = "W25Q128", + .addr = 0, + .len = 16 * 1024 * 1024, + .blk_size = 4096, + .ops = {init, read, write, erase}, + .write_gran = 1 +}; + +static int init(void) +{ + sfud_dev = rt_sfud_flash_find_by_dev_name("W25Q128"); + if (RT_NULL == sfud_dev) + { + return -1; + } + + /* update the flash chip information */ + w25q128.blk_size = sfud_dev->chip.erase_gran; + w25q128.len = sfud_dev->chip.capacity; + + return 0; +} + +static int read(long offset, uint8_t *buf, size_t size) +{ + assert(sfud_dev); + assert(sfud_dev->init_ok); + sfud_read(sfud_dev, w25q128.addr + offset, size, buf); + + return size; +} + +static int write(long offset, const uint8_t *buf, size_t size) +{ + assert(sfud_dev); + assert(sfud_dev->init_ok); + if (sfud_write(sfud_dev, w25q128.addr + offset, size, buf) != SFUD_SUCCESS) + { + return -1; + } + + return size; +} + +static int erase(long offset, size_t size) +{ + assert(sfud_dev); + assert(sfud_dev->init_ok); + if (sfud_erase(sfud_dev, w25q128.addr + offset, size) != SFUD_SUCCESS) + { + return -1; + } + + return size; +} diff --git a/bsp/stm32/stm32f407-atk-explorer/board/ports/spi_flash_init.c b/bsp/stm32/stm32f407-atk-explorer/board/ports/spi_flash_init.c index b5e8571cd5..4299f8fd48 100644 --- a/bsp/stm32/stm32f407-atk-explorer/board/ports/spi_flash_init.c +++ b/bsp/stm32/stm32f407-atk-explorer/board/ports/spi_flash_init.c @@ -15,21 +15,15 @@ #if defined(BSP_USING_SPI_FLASH) -#ifdef FAL_USING_NOR_FLASH_DEV_NAME -#define _SPI_FLASH_NAME FAL_USING_NOR_FLASH_DEV_NAME -#else -#define _SPI_FLASH_NAME "W25Q128" -#endif - static int rt_hw_spi_flash_init(void) { __HAL_RCC_GPIOB_CLK_ENABLE(); rt_hw_spi_device_attach("spi1", "spi10", GPIOB, GPIO_PIN_14); - if (RT_NULL == rt_sfud_flash_probe(_SPI_FLASH_NAME, "spi10")) + if (RT_NULL == rt_sfud_flash_probe("W25Q128", "spi10")) { return -RT_ERROR; - }; + } return RT_EOK; }