完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
这个例程建立在已经有了SPI驱动的条件下
1 cubemx 配置 勾选user-defiend, 4096 扇区支持 如果要支持中文和长文件名勾选这个 设置堆栈大小 2 代码配置 user_disokio.c中添加读写函数 1 初始化 DSTATUS USER_initialize ( BYTE pdrv /* Physical drive nmuber to identify the drive */ ) { /* USER CODE BEGIN INIT */ Stat = STA_NOINIT; if(SPI_FLASH_ReadID() == sFLASH_ID){ Stat &= ~STA_NOINIT; }else{ printf("fatfs flash init begin errorrn"); } return Stat; /* USER CODE END INIT */ } 2 获取状态 DSTATUS USER_status ( BYTE pdrv /* Physical drive number to identify the drive */ ) { /* USER CODE BEGIN STATUS */ Stat = STA_NOINIT; //printf("------- this is fatfs flash get status ok---rn"); if(SPI_FLASH_ReadID() == sFLASH_ID){ Stat = ~STA_NOINIT; //printf("------- this is fatfs flash get status ok---rn"); }else{ Stat = STA_NOINIT; } return Stat; /* USER CODE END STATUS */ } 3 读取 因为sector 和count单位不一样, 要进行转换 DRESULT USER_read ( BYTE pdrv, /* Physical drive nmuber to identify the drive */ BYTE *buff, /* Data buffer to store read data */ DWORD sector, /* Sector address in LBA */ UINT count /* Number of sectors to read */ ) { /* USER CODE BEGIN READ */ SPI_FLASH_BufferRead(buff, sector <<12, count<<12); return RES_OK; /* USER CODE END READ */ } 4 写入 DRESULT USER_write ( BYTE pdrv, /* Physical drive nmuber to identify the drive */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address in LBA */ UINT count /* Number of sectors to write */ ) { /* USER CODE BEGIN WRITE */ /* USER CODE HERE */ uint32_t write_addr; write_addr = sector<<12; SPI_FLASH_SectorErase(write_addr); SPI_FLASH_BufferWrite((uint8_t *)buff,write_addr,count<<12); return RES_OK; /* USER CODE END WRITE */ } 5 控制 #define SPI_FLASH_FATFS_SIZE (8*1024*1024) // 用作fatfs的大小 #define SPI_FLASH_BLOCK_SIZE 4096 #define SPI_FLASH_FATFS_SELECTOR_SIZE (SPI_FLASH_FATFS_SIZE/SPI_FLASH_BLOCK_SIZE) // fatfs占用的扇区 DRESULT USER_ioctl ( BYTE pdrv, /* Physical drive nmuber (0..) */ BYTE cmd, /* Control code */ void *buff /* Buffer to send/receive control data */ ) { /* USER CODE BEGIN IOCTL */ DRESULT res = RES_ERROR; switch (cmd) { /* 扇区数量:2560*4096/1024/1024=10(MB) */ case GET_SECTOR_COUNT: *(DWORD * )buff = SPI_FLASH_FATFS_SELECTOR_SIZE;//768-3M//1024; break; /* 扇区大小 */ case GET_SECTOR_SIZE : *(WORD * )buff = SPI_FLASH_BLOCK_SIZE; break; /* 同时擦除扇区个数 */ case GET_BLOCK_SIZE : *(DWORD * )buff = 1; break; } res = RES_OK; return res; /* USER CODE END IOCTL */ } 3 测试 FATFS spi_flash; void fatfs_spi_flash_init() { FRESULT retSD; // 挂载spi flash -- 如果userfs不能用, 就用自己定义的fatfs retSD = f_mount(&spi_flash, USERPath, 0); // 如果挂载不成功 if(retSD) { // 如果没文件系统 if(retSD == FR_NO_FILESYSTEM) { printf("mkfs spi-flash rn"); retSD = f_mkfs(USERPath,FM_FAT,0,work_buffer,WORK_BUFFER_SIZE); if(retSD == FR_OK) { retSD = f_mount(&USERFatFS, USERPath, 0); if(retSD == FR_OK) { printf("spi-flash init fs ok!rn"); } } } else { printf("spi-flash init fs ok!rn"); } }else { printf(" mount spi-flash okrn"); } // uint32_t total,free; // exf_getfree("1:",&total,&free); // printf("flash size:%d KB %dMB, free size:%d KB %dMBrn",total,total/1024,free,free/1024); } static void fatfs_test(char *filename_t) { fr = f_open(&fd, filename_t, FA_CREATE_ALWAYS | FA_WRITE); if(fr == FR_OK) { printf("rnfatfs open file okrn"); fr = f_write(&fd, write_dat, sizeof(write_dat), (void *)&write_num); fr = f_close(&fd); if(fr == FR_OK) { retSD = f_open(&fd, filename_t, FA_OPEN_EXISTING | FA_READ); if(retSD == FR_OK) { retSD = f_read(&fd, read_buf, sizeof(read_buf), &count); if(retSD != FR_OK) { f_close(&fd); } else { printf("rnfatfs read buf:%srn", read_buf); f_close(&fd); } } } }else{ printf("rnfatfs open file errorrn"); } } main.c调用 注意我这里已经有了sd fatfs, 所以spi-flash是1, 如果没有设置sd, 那么flash就是0 fatfs_spi_flash_init(); fatfs_test("1:test.txt"); |
|
|
|
只有小组成员才能发言,加入小组>>
3278 浏览 9 评论
2956 浏览 16 评论
3457 浏览 1 评论
8995 浏览 16 评论
4050 浏览 18 评论
1103浏览 3评论
570浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
568浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2301浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1857浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-22 17:06 , Processed in 1.118820 second(s), Total 80, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号