请选择 进入手机版 | 继续访问电脑版

你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

用FATFS 操作SD卡的异常

[复制链接]
annQian 提问时间:2019-3-13 16:31 /
     用FATFS (13a版本)操作SD卡(8G),但没法 成功建目录或文件,请问是怎么回事?SD卡的驱动正常。
收藏 评论10 发布时间:2019-3-13 16:31

举报

10个回答
废鱼 回答时间:2019-3-13 16:43:33
看一下返回的结果。可能是因为你使用的驱动盘不一样。新的支持USB、ATA、MMC。具体看一下diskio.c中,disk_status函数。

评分

参与人数 1蝴蝶豆 +2 收起 理由
STMCU + 2

查看全部评分

annQian 回答时间:2019-3-13 17:20:29
安 发表于 2019-3-13 16:43
看一下返回的结果。可能是因为你使用的驱动盘不一样。新的支持USB、ATA、MMC。具体看一下diskio.c中,disk_ ...

执行res = find_volume(&path, &fs, FA_WRITE); 返回结果不对;
fs->fs_type = 0;                                        /* Clear the filesystem object */
        fs->pdrv = LD2PD(vol);                                /* Bind the logical drive and a physical drive */
        stat = disk_initialize(fs->pdrv);        /* Initialize the physical drive */
        if (stat & STA_NOINIT) {                         /* Check if the initialization succeeded */
                return FR_NOT_READY;                        /* Failed to initialize due to no medium or hard error */
从这里返回了。
但是不用文件系统,直接读写SD 卡,操作是正常的。
        }
废鱼 回答时间:2019-3-14 08:45:11
楼主,你按照我的方法去查一下,你用的是盘符是什么?SD卡用的是SPI的驱动吗?
废鱼 回答时间:2019-3-14 08:45:47
进入disk_initialize查看一下,是不是因为盘符不对,导致文件系统找不到路径。
annQian 回答时间:2019-3-14 09:05:45
SD 卡用的是SPI驱动,我第一次搞文件系统,不太知道怎样判断盘符是否正确?
DSTATUS disk_initialize (
        BYTE pdrv                                /* Physical drive nmuber to identify the drive */
)
{
         return (DSTATUS)Sd_disk_initialize();
}
废鱼 回答时间:2019-3-14 09:15:28
就是你要打开的文件的名称。
annQian 回答时间:2019-3-14 09:57:42
初始化部分正常,这个函数有问题
DRESULT disk_ioctl (
        BYTE pdrv,                /* Physical drive nmuber (0..) */
        BYTE cmd,                /* Control code */
        void *buff                /* Buffer to send/receive control data */
)
{
        DRESULT res;
        int result;

        switch (pdrv) {
        case DEV_SD :
                switch(cmd)
                {
                        case CTRL_SYNC:
                                if(Sd_disk_sync())
                                {
                                        return RES_ERROR;
                                }
                                return RES_OK;
                        case GET_SECTOR_COUNT:
                                res = _sd_sectors();
                                if(res >0 )
                                {
                                        *((DWORD *)buff) = res;
                                        return RES_OK;
                                }
                                else
                                {
                                        return RES_ERROR;
                                }
                        case GET_SECTOR_SIZE:
                                *(DWORD *)buff = 512;
                                res = RES_OK;
                        case GET_BLOCK_SIZE:
                                *(DWORD *)buff = 1;
                                res = RES_OK;                               
                }

                return res;
        }

        return RES_PARERR;
}
/**********************************************/
uint32_t Sd_disk_sync(void)
{
        return 0;
}
/********************************************************************/
static uint32_t ext_bits(unsigned char *data, uint32_t msb, uint32_t lsb)
{
    uint32_t bits = 0;
    uint32_t size = 1 + msb - lsb;
    for (uint32_t i = 0; i < size; i++) {
        uint32_t position = lsb + i;
        uint32_t byte = 15 - (position >> 3);
        uint32_t bit = position & 0x7;
        uint32_t value = (data[byte] >> bit) & 1;
        bits |= value << i;
    }
    return bits;
}

uint64_t _sd_sectors(void)
{
    uint32_t c_size, c_size_mult, read_bl_len;
    uint32_t block_len, mult, blocknr, capacity;
    uint32_t hc_c_size;
    uint64_t blocks;
   
    // CMD9, Response R2 (R1 byte + 16-byte block read)
    if (_cmdx(9, 0) != 0) {
        //debug("Didn't get a response from the disk\n");
        return 0;
    }
   
    uint8_t csd[16];
    if (_read(csd, 16) != 0) {
        //debug("Couldn't read csd response from disk\n");
        return 0;
    }
   
    // csd_structure : csd[127:126]
    // c_size        : csd[73:62]
    // c_size_mult   : csd[49:47]
    // read_bl_len   : csd[83:80] - the *maximum* read block length
   
    uint32_t csd_structure = ext_bits(csd, 127, 126);
   
    switch (csd_structure) {
        case 0:
            cdv = 512;
            c_size = ext_bits(csd, 73, 62);
            c_size_mult = ext_bits(csd, 49, 47);
            read_bl_len = ext_bits(csd, 83, 80);
            
            block_len = 1 << read_bl_len;
            mult = 1 << (c_size_mult + 2);
            blocknr = (c_size + 1) * mult;
            capacity = blocknr * block_len;
            blocks = capacity / 512;
//         debug_if(SD_DBG, "\n\rSDCard\n\rc_size: %d \n\rcapacity: %ld \n\rsectors: %lld\n\r", c_size, capacity, blocks);
            break;
        
        case 1:
            cdv = 1;
            hc_c_size = ext_bits(csd, 63, 48);//0x3b37=15159
            blocks = (hc_c_size+1)*1024;//0xece000=15523840
//          debug_if(SD_DBG, "\n\rSDHC Card \n\rhc_c_size: %d\n\rcapacity: %lld \n\rsectors: %lld\n\r", hc_c_size, blocks*512, blocks);
            break;
        
        default:
            //debug("CSD struct unsupported\r\n");
            return 0;
    };
    return blocks;
}
我用的U盘是8G的,f_mkfs 函数报FR_DISK_ERR
废鱼 回答时间:2019-3-14 10:15:03
在使用之前,是否有挂载设备的操作?f_mount();

评分

参与人数 1蝴蝶豆 +1 收起 理由
STMCU + 1

查看全部评分

annQian 回答时间:2019-3-14 10:22:11
   /* Create FAT volume */
    res = f_mkfs("", FM_FAT32, 0, work, sizeof (work));
    if (!res)
        {
                //success
                Sys_Delay(1);
        }
                /* Register work area */
                 res = f_mount(&fs, "", 0);
         if (!res)
                {
                        //success
                        Sys_Delay(1);
                }
                /*
                /* Create a file as new */
                res = f_open(&fil, "hello.txt", FA_CREATE_NEW | FA_WRITE);
                 if (!res)
                {
                        //success
                        Sys_Delay(1);
                }
               
                        /* Write a message */
                        f_write(&fil, "Hello, World!\r\n", 15, &bw);
                        if (bw != 15)
                        /* Close the file */
                                f_close(&fil);

main函数调用
废鱼 回答时间:2019-3-14 11:25:49
可能使用的FATFS库不一样,我使用时res = f_mount(&fs, "1:", 0);我这里是需要增加一个盘符的操作。看一下res = f_mount(&fs, "", 0);后结果是什么。如果前面挂载不成功,后面创建文件也是创建不了的。

所属标签

相似问题

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版