
1、配置app.overlayoctospi1{statusokay;/* 必须为 okay */pinctrl-0octospi1_clk_pf10octospi1_ncs_pg6octospi1_io0_pf8octospi1_io1_pf9octospi1_io2_pf7octospi1_io3_pf6;pinctrl-namesdefault;w25q64:w25q640{compatiblest,stm32-ospi-nor;/* 必须是这个字符串注意是 ospi */statusokay;/* 必须为 okay */reg0;jedec-id[ef4017];size67108864;ospi-max-frequency20000000;spi-bus-width1;data-rate1;};};w25q64{/* 注意这里要改成你实际的 Flash 节点名称比如 mx25r64 或 flash0 */partitions{compatiblefixed-partitions;#address-cells1;#size-cells1;/* 假设分配前 1MB 给 LittleFS (0x0 到 0x100000) */lfs_part:partition0{labellittlefs_storage;reg0x00x100000;};};};pinctrl{octospi1_clk_pf10:octospi1_clk_pf10{pinmuxSTM32_PINMUX(F,10,AF9);bias-pull-down;slew-ratevery-high-speed;};octospi1_ncs_pg6:octospi1_ncs_pg6{pinmuxSTM32_PINMUX(G,6,AF10);bias-pull-up;slew-ratevery-high-speed;};octospi1_io0_pf8:octospi1_io0_pf8{pinmuxSTM32_PINMUX(F,8,AF10);bias-pull-up;slew-ratevery-high-speed;};octospi1_io1_pf9:octospi1_io1_pf9{pinmuxSTM32_PINMUX(F,9,AF10);bias-pull-up;slew-ratevery-high-speed;};octospi1_io2_pf7:octospi1_io2_pf7{pinmuxSTM32_PINMUX(F,7,AF10);bias-pull-up;slew-ratevery-high-speed;};octospi1_io3_pf6:octospi1_io3_pf6{pinmuxSTM32_PINMUX(F,6,AF10);bias-pull-up;slew-ratevery-high-speed;};};fstab{compatiblezephyr,fstab;lfs:lfs{compatiblezephyr,fstab,littlefs;mount-point/lfs;/* 挂载路径 */partitionlfs_part;/* 绑定的分区 */automount;/* 系统启动时自动挂载 */read-size16;/* 读操作的最小单位 */prog-size16;/* 写操作的最小单位 */cache-size64;/* 缓存大小 */lookahead-size32;/* 预读大小 */block-cycles512;/* 磨损均衡的循环次数 */};};2、配置prj.confCONFIG_SPI_NORy CONFIG_FLASHy CONFIG_FLASH_JESD216_APIy # 支持读取 Flash 的 JEDEC 参数 # 开启 Flash 驱动子系统的调试日志#CONFIG_FLASH_LOG_LEVEL_DBGy# 开启 SPI/OSPI 总线的调试日志#CONFIG_SPI_LOG_LEVEL_DBGyCONFIG_FLASH_MAPy CONFIG_NVSy # 开启 LittleFS 文件系统 CONFIG_FILE_SYSTEM_LITTLEFSy CONFIG_FILE_SYSTEM_SHELLy3、代码段//-------------- 只识别了flash的对flash进行读写操作。-------------#includezephyr/fs/fs.h#includeff.h#includezephyr/drivers/flash.h/* 1. 获取 Flash 设备句柄 (对应设备树中的 w25q64 节点) */staticconststructdevice*flash_devDEVICE_DT_GET(DT_NODELABEL(w25q64));voidthread_entry4(void*p1,void*p2,void*p3){// 使用 LOG_INF 替代 printkLOG_INF(Hello from thread 4!);structflash_pages_infoinfo;intret;// 1. 检查 Flash 设备是否就绪if(!device_is_ready(flash_dev)){LOG_ERR(W25Q64 Flash device is not ready!);return;}LOG_INF(W25Q64 Flash initialized successfully.);// 2. 获取 Flash 空间信息retflash_get_page_info_by_idx(flash_dev,0,info);if(ret0){LOG_INF(Flash Page Size: %zu bytes,info.size);}// 3. 准备测试数据uint32_ttest_addr0x0000;uint8_twrite_buf[4]{0xDE,0xAD,0xBE,0xEF};uint8_tread_buf[4]{0};// 4. 擦除扇区LOG_INF(Erasing flash at 0x%x (4KB)...,test_addr);retflash_erase(flash_dev,test_addr,4096);if(ret!0){LOG_ERR(Flash erase failed: %d,ret);return;}LOG_INF(Flash erase successful!);// 5. 写入数据LOG_INF(Writing data to flash...);retflash_write(flash_dev,test_addr,write_buf,sizeof(write_buf));if(ret!0){LOG_ERR(Flash write failed: %d,ret);return;}// 6. 读取并验证LOG_INF(Reading data from flash...);retflash_read(flash_dev,test_addr,read_buf,sizeof(read_buf));if(ret!0){LOG_ERR(Flash read failed: %d,ret);return;}if(memcmp(write_buf,read_buf,sizeof(write_buf))0){LOG_INF(Flash Test PASSED! Data: %02X %02X %02X %02X,read_buf[0],read_buf[1],read_buf[2],read_buf[3]);}else{LOG_ERR(Flash Test FAILED! Data mismatch.);}while(1){k_sleep(K_MSEC(500));}}//-----------------------加上了 LittleFS---------------------------------/* 1. 获取 Flash 设备句柄 (对应设备树中的 w25q64 节点) */staticconststructdevice*flash_devDEVICE_DT_GET(DT_NODELABEL(w25q64));voidthread_entry4(void*p1,void*p2,void*p3){LOG_INF(Hello from thread 4!);intret;structfs_file_tfile;/* 1. 打开/创建文件 */fs_file_t_init(file);retfs_open(file,/lfs/test.txt,FS_O_CREATE|FS_O_RDWR);if(ret0){printk(Failed to open file: %d\n,ret);}/* 2. 写入数据 */constchar*dataHello LittleFS!;retfs_write(file,data,strlen(data));printk(Bytes written: %d\n,ret);/* 3. 关闭文件 */fs_close(file);printk(LittleFS test completed successfully!\n);while(1){k_sleep(K_MSEC(500));}}使用开启CONFIG_SHELLy 通过串口输入记得添加\r\n fs stat/lfs 查询 flash 及其相关指令 fs ls/lfs 展示当前路径下的文件列表信息 fs read/lfs/test.txt 读取文件内容演示结果如下