执行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 卡,操作是正常的。
}
评分
查看全部评分
执行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 卡,操作是正常的。
}
DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
return (DSTATUS)Sd_disk_initialize();
}
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
评分
查看全部评分
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函数调用