#include #include #include #include "drv_spi.h" #include "spi_flash_sfud.h" #include #define W25Q64_SPI_DEVICE_NAME "W25Q64" static int rt_hw_spi_flash_init(void) { HAL_PIN_Set(PAD_PA24, SPI1_DIO, PIN_PULLDOWN, 1); // SPI1 (Nor flash) HAL_PIN_Set(PAD_PA25, SPI1_DI, PIN_PULLUP, 1); HAL_PIN_Set(PAD_PA28, SPI1_CLK, PIN_NOPULL, 1); HAL_PIN_Set(PAD_PA29, SPI1_CS, PIN_NOPULL, 1); rt_hw_spi_device_attach("spi1", W25Q64_SPI_DEVICE_NAME); //注意此处w25q64如果读取失败,可能是因为时钟频率设置过高导致的,原本为50MHz,改为20MHz if (RT_NULL == rt_sfud_flash_probe("W25Q64", W25Q64_SPI_DEVICE_NAME)) //注册块设备,这一步可以将外部flash抽象为系统的块设备 { return -RT_ERROR; }; return RT_EOK; } /* 导出到自动初始化 */ INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init); //文件系统挂载 int mnt_init(void) { if (dfs_mount("W25Q64", "/", "elm", 0, 0) == 0) // "W25Q64":挂载的设备名称,"/":挂载路径,这里挂载到跟目录下 { rt_kprintf("W25Q64 mount successful! \n"); } else { dfs_mkfs("elm", "W25Q64"); // 如果是第一次挂载文件系统必须要先格式化 if(dfs_mount("W25Q64", "/", "elm", 0, 0) != 0) { rt_kprintf("W25Q64 mount failed! \n"); } else { rt_kprintf("W25Q64 mount successful! \n"); } } return 0; } INIT_ENV_EXPORT(mnt_init);