初中生
最后登录1970-1-1
在线时间 小时
注册时间2021-10-2
|
楼主 |
发表于 2021-10-2 16:23:24
|
显示全部楼层
- #include "stm32f10x.h"
- #include "bsp_usart.h"
- #include "bsp_spi.h"
- #include "ff.h"
- char wdata[]="这是一个FATFS文件系统读写测试";
- char rdata[4096];
- FATFS fs;
- FIL fp;
- UINT bw;
- UINT br;
- int main(void)
- {
- FRESULT res_flash;
- usart_config();
- res_flash=f_mkfs("4:",0,0);
- printf("\r\n****** 这是一个SPI FLASH 文件系统实验 ******");
- res_flash=f_mount(&fs,"4:",1);
- printf("\r\nf_mount=%d",res_flash);
- if(res_flash==FR_OK)
- {
- printf("\r\n逻辑驱动装载完成");
- }
- else
- {
- if(res_flash==FR_NO_FILESYSTEM)
- {
- res_flash=f_mkfs("4:",0,0);
- printf("\r\nf_mkfs=%d",res_flash);
- //卸载逻辑驱动
- f_mount(NULL,"4:",1);
- //装载逻辑驱动
- f_mount(&fs,"4:",1);
- }
- }
- res_flash=f_open(&fp,"4:xyz.txt",FA_WRITE|FA_OPEN_ALWAYS);
- printf("\r\nf_open=%d",res_flash);
- if(res_flash==FR_OK)
- {
- printf("\r\n文件打开成功");
- res_flash=f_write(&fp,wdata,sizeof(wdata),&bw);
- if(res_flash==FR_OK)
- {
- printf("\r\nbw=%d",bw);
- }
- }
- f_close(&fp);
- res_flash=f_open(&fp,"4:xyz.txt",FA_READ|FA_OPEN_EXISTING);
- if(res_flash==FR_OK)
- {
- f_lseek(&fp,0);
- res_flash=f_read(&fp,rdata,f_size(&fp),&br);
- if(res_flash==FR_OK)
- {
- printf("\r\n文件中的内容为:%s",rdata);
- printf("\r\nbr=%d",br);
- }
- }
- f_close(&fp);
- printf("\r\n文件关闭");
- }
复制代码 |
-
|