Fatfs+HAL库的使用

    技术2024-03-11  89

    环境

    FatFs R0.11STM32CubeMX 5.6.1STM32F103C8T6

    个人推荐配置

    选择使用TINY模式可以节省内存,具体其他配置可查看配置详解

    完善相关函数

    生成代码后需要完善的函数有:初始化、读、写、状态、查询

    打开user_diskio.c文件进行补充

    USER_initialize

    函数初始化,加入存储设备的初始化函数

    USER_status

    状态函数,建议直接返回RES_OK

    USER_read

    读函数,加入存储设备读取函数

    USER_write

    写函数,加入存储设备写函数

    USER_ioctl

    查询、命令函数,需要添加以下命令的返回,在diskio.h中53行可进行查看

    #define CTRL_SYNC 0 /* Complete pending write process (needed at _FS_READONLY == 0) */ #define GET_SECTOR_COUNT 1 /* Get media size (needed at _USE_MKFS == 1) */ #define GET_SECTOR_SIZE 2 /* Get sector size (needed at _MAX_SS != _MIN_SS) */ #define GET_BLOCK_SIZE 3 /* Get erase block size (needed at _USE_MKFS == 1) */ #define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at _USE_TRIM == 1) */

    主要就是,块大小、块数量,其他均返回正常即可

    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 dre = RES_OK; switch(cmd) { case CTRL_SYNC: dre = RES_OK; break; case GET_SECTOR_COUNT: *(uint32_t*)buff = w25qxx.SectorCount; dre = RES_OK; break; case GET_SECTOR_SIZE: *(uint32_t*)buff = w25qxx.SectorSize; dre = RES_OK; break; case GET_BLOCK_SIZE: *(uint32_t*)buff = w25qxx.BlockSize; dre = RES_OK; break; case CTRL_TRIM: dre = RES_OK; break; default: break; } return dre; /* USER CODE END IOCTL */

     

    from: https://blog.csdn.net/shaynerain

    Processed: 0.059, SQL: 9