OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 4964|回复: 2

FATFS移植问题,初始化后打开还是原值

[复制链接]

5

主题

14

帖子

0

精华

初级会员

Rank: 2

积分
68
金钱
68
注册时间
2021-6-12
在线时间
19 小时
发表于 2021-7-24 10:51:10 | 显示全部楼层 |阅读模式
5金钱
求助大佬!!探索者F407开发板。f_mkfs初始化后,f_open打开还是初始化之前的值。以下是打印出的值。

这是一个文件系统移植实验
f_mount res =13
f_mkfs res =0
f_open = 13
f_close = 9


下面是主程序
FATFS fs;
FRESULT res;
FIL fil;

/**
  * @brief        擦除写入实验
  * @param        无
  * @retval        写入之前必须先擦除
  */
int main(void)
{       
        LED_Init();
       
        uart_init(115200);
       
        printf("\r\n这是一个文件系统移植实验");
       
        res = f_mount(&fs,"1:",1);
       
        printf("\r\nf_mount res =%d ",res);
       
        if(res == FR_NO_FILESYSTEM)
        {
                //格式化
                res = f_mkfs("1:",0,2);
                printf("\r\nf_mkfs res =%d ",res);
               
                //格式化后需要重新挂载文件系统
                res = f_mount(NULL,"1:",1);
               
                res = f_mount(&fs,"1:",1);
        }
       
        res = f_open(&fil,"1:mes.txt",FA_CREATE_ALWAYS|FA_READ|FA_WRITE);
        printf("\r\nf_open = %d",res);


        res = f_close(&fil);
        printf("\r\nf_close = %d",res);

        while(1)
        {}
}


以下是diskio修改后的程序。
#define SD_CARD 0
#define SPI_FLASH 1

#define FLASH_SECTOR_SIZE 4096


/*-----------------------------------------------------------------------*/
/* Get Drive Status                                                      */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
        BYTE pdrv                /* Physical drive nmuber to identify the drive */
)
{
        DSTATUS stat;
        u32 spi_result;
       
        switch (pdrv) {
               
        //SD卡状态返回分支
        case SD_CARD :

        // translate the reslut code here

                return stat;
       
        //SPI_FLASH状态返回分支
        case SPI_FLASH :

        spi_result = SPI_FLASH_ReadDeviceID();
        if(spi_result == 0x17)
        {
                stat &= !STA_NOINIT; //stat最低位为0,表示FLASH为正常状态
        }
       
        else
        {
                stat |= STA_NOINIT;//stat最低位为1,表示FLASH为不正常状态
        }

                return stat;
        }
       
        return STA_NOINIT;
}



/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
        BYTE pdrv                                /* Physical drive nmuber to identify the drive */
)
{
        DSTATUS stat;

        switch (pdrv) {
        //SD卡初始化分支
        case SD_CARD :

        // translate the reslut code here

                return stat;
       
        //SPI_FLASH初始化分支
        case SPI_FLASH :
                                SPI_FLASH_Init();
                                stat = disk_status(SPI_FLASH);

                return stat;

        }
        return STA_NOINIT;
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_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 */
)
{
        DRESULT res;

        switch (pdrv) {
        case SD_CARD :
                // translate the arguments here


                // translate the reslut code here

                return res;

        case SPI_FLASH :
                //SPI_FLASH读取分支
                //要读取的扇区号转换成地址
               
                SPI_FLASH_BufferRead((u8 *)buff,sector * FLASH_SECTOR_SIZE,count*FLASH_SECTOR_SIZE);

                //默认每次都能正常读取
                return RES_OK;
        }

        return RES_PARERR;
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

#if _USE_WRITE
DRESULT disk_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 */
)
{
        DRESULT res;

        switch (pdrv) {
        case SD_CARD :
                // translate the arguments here

                // translate the reslut code here

                return res;

        case SPI_FLASH :
                //SPI_FLASH写入分支
                while(count--)
                {
                        //写入前先擦除
                        SPI_FLASH_SectorErase(sector * FLASH_SECTOR_SIZE);
                       
                        //要写入的扇区号转换成地址
                        SPI_FLASH_BufferWrite((u8 *)buff,sector * FLASH_SECTOR_SIZE,count*FLASH_SECTOR_SIZE);

                        sector++;
                        buff += FLASH_SECTOR_SIZE;
                }

                        //默认每次都能正常写入
                        return RES_OK;
        }
        return RES_PARERR;
       
}
#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

#if _USE_IOCTL
DRESULT disk_ioctl (
        BYTE pdrv,                /* Physical drive nmuber (0..) */
        BYTE cmd,                /* Control code */
        void *buff                /* Buffer to send/receive control data */
)
{
        DRESULT res;
       
        switch (pdrv)
        {
        case SD_CARD :

                // Process of the command for the ATA drive

                return res;

        case SPI_FLASH :
                //SPI_FLASH分支
                        switch(cmd)
                        {
                                //存储介质有多少个sector,文件系统通过该值获得存储介质的容量
                                case GET_SECTOR_COUNT:
                                        *(DWORD *)buff = 16 * 1024 * 1024 / FLASH_SECTOR_SIZE;
                                        res = RES_OK;
                                        break;
                               
                                //每个扇区的个数
                                case GET_SECTOR_SIZE:
                                        *(WORD *)buff = FLASH_SECTOR_SIZE;
                                        res = RES_OK;
                                        break;
                               
                                //获取擦除的最小个数,以sector为单位;
                                case GET_BLOCK_SIZE:
                                        *(DWORD *)buff = 1;
                                        res = RES_OK;
                                        break;
                               
                                //写入同步在disk_write函数已经完成,这里默认返回
                                case CTRL_SYNC:
                                        res = RES_OK;
                                        break;
                                       
                                default :
                                        res = RES_ERROR;
                                        break;
                        }

                return res;
        }

        return RES_PARERR;
}
#endif

DWORD get_fattime (void)
{
        return 0;
}

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

6

主题

890

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1477
金钱
1477
注册时间
2020-8-19
在线时间
335 小时
发表于 2021-7-24 12:30:42 | 显示全部楼层
回复

使用道具 举报

5

主题

14

帖子

0

精华

初级会员

Rank: 2

积分
68
金钱
68
注册时间
2021-6-12
在线时间
19 小时
 楼主| 发表于 2021-7-25 22:35:54 | 显示全部楼层
顶一下
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-2-27 08:23

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表