/*********************************************************** * Function: * Description: * Input: * Input: * Output: * Return: * Others: ***********************************************************/ static void rt_thread_entry_sd_test( void* parameter ) { FRESULT res; char tempbuf[64]; rt_sem_take( &sem_dataflash, RT_TICK_PER_SECOND * FLASH_SEM_DELAY ); SPI_Configuration(); res = SD_Init(); if(0 == res) { rt_kprintf("\r\n SD CARD INIT OK!"); memset(tempbuf,0,sizeof(tempbuf)); SD_GetCID(tempbuf); rt_kprintf("\r\n CID="); printf_hex_data(tempbuf, 16); SD_GetCSD(tempbuf); rt_kprintf("\r\n SID="); printf_hex_data(tempbuf, 16); rt_kprintf("\r\n SD 容量=%d",SD_GetCapacity()); } else { rt_kprintf("\r\n SD CARD INIT ERR = %d",res); } if(0 == f_mount(MMC, &fs)) { rt_kprintf("\r\n f_mount SD OK!"); } rt_sem_release(&sem_dataflash); while( 1 ) { rt_thread_delay( RT_TICK_PER_SECOND / 20 ); } }
//FAT初始化,不含SD的初始化,用之前应先调用sd的初始化 //返回值:0,初始化成功 // 其他,初始化失败 unsigned char FAT_Init(void)//Initialize of FAT need initialize SD first { bootsector710 *bs = 0; bpb710 *bpb = 0; partrecord *pr = 0; DWORD hidsec=0; u32 Capacity; Capacity = SD_GetCapacity(); if(Capacity<0xff)return 1; if(SD_ReadSingleBlock(0,fat_buffer))return 2; bs = (bootsector710 *)fat_buffer; pr = (partrecord *)((partsector *)fat_buffer)->psPart;//first partition hidsec = pr->prStartLBA;//the hidden sectors if(hidsec >= Capacity/512)hidsec = 0; else { if(SD_ReadSingleBlock(pr->prStartLBA,fat_buffer))return 3;//read the bpb sector bs = (bootsector710 *)fat_buffer; if(bs->bsJump[0]!=0xE9 && bs->bsJump[0]!=0xEB) { hidsec = 0; if(SD_ReadSingleBlock(0,fat_buffer))return 4;//read the bpb sector bs = (bootsector710 *)fat_buffer; } } if(bs->bsJump[0]!=0xE9 && bs->bsJump[0]!=0xEB)return 5;//对付没有bootsect的sd卡 //dead with the card which has no bootsect bpb = (bpb710 *)bs->bsBPB; if(bpb->bpbFATsecs)//detemine thd FAT type //do not support FAT12 { FAT32_Enable=0; //FAT16 FATsectors = bpb->bpbFATsecs;//FAT表占用的扇区数 FirstDirClust = 2; } else { FAT32_Enable=1; //FAT32 FATsectors = bpb->bpbBigFATsecs;//FAT占用的扇区数 FirstDirClust = bpb->bpbRootClust; } BytesPerSector = bpb->bpbBytesPerSec; //每扇区字节数 SectorsPerClust = (BYTE)bpb->bpbSecPerClust;//每簇扇区数 FirstFATSector = bpb->bpbResSectors+hidsec;//第一个FAT表扇区 RootDirCount = bpb->bpbRootDirEnts; //根目录项数 RootDirSectors = (RootDirCount*32)>>9; //根目录占用的扇区数 FirstDirSector = FirstFATSector+bpb->bpbFATs*FATsectors;//第一个目录扇区 FirstDataSector = FirstDirSector+RootDirSectors;//第一个数据扇区 return 0; }
DRESULT disk_ioctl ( BYTE drv, /* Physical drive nmuber (0..) */ BYTE ctrl, /* Control code */ void *buff /* Buffer to send/receive control data */ ) { DRESULT res; if (drv) { return RES_PARERR; // only supports single disk operation, or return parameter error } // FATFS only deal with the current version of CTRL_SYNC, GET_SECTOR_COUNT, GET_BLOCK_SIZ three commands switch(ctrl) { case CTRL_SYNC: MSD_CS_ENABLE(); if(SD_WaitReady()==0) { res = RES_OK; } else { res = RES_ERROR; } MSD_CS_DISABLE(); break; case GET_BLOCK_SIZE: *(WORD*)buff = 512; res = RES_OK; break; case GET_SECTOR_COUNT: *(DWORD*)buff = SD_GetCapacity(); res = RES_OK; break; default: res = RES_PARERR; break; } return res; }
DRESULT disk_ioctl ( BYTE drv, /* Physical drive nmuber (0..) */ BYTE ctrl, /* Control code */ void *buff /* Buffer to send/receive control data */ ) { DRESULT res; if (drv) { return RES_PARERR; //仅支持单磁盘操作,否则返回参数错误 } //FATFS目前版本仅需处理CTRL_SYNC,GET_SECTOR_COUNT,GET_BLOCK_SIZ三个命令 switch(ctrl) { case CTRL_SYNC: SD_CS_ENABLE(); if(SD_WaitReady()==0) { res = RES_OK; } else { res = RES_ERROR; } SD_CS_DISABLE(); break; case GET_BLOCK_SIZE: *(WORD*)buff = 512; res = RES_OK; break; case GET_SECTOR_COUNT: *(DWORD*)buff = SD_GetCapacity(); res = RES_OK; break; default: res = RES_PARERR; break; } return res; }
DRESULT disk_ioctl (BYTE drv,BYTE ctrl,void* buff)//哪个盘,控制命令(0刷新缓存,3获取块大小,1获取磁盘容量),块大小和磁盘容量存放的指针 { DRESULT res; if (drv) { return RES_PARERR; //仅支持单磁盘操作,否则返回参数错误 } //FATFS目前版本仅需处理CTRL_SYNC,GET_SECTOR_COUNT,GET_BLOCK_SIZ三个命令 switch(ctrl) { case CTRL_SYNC: //刷新磁盘缓存 SD_CS_ENABLE(); if(SD_WaitReady()==0) { res = RES_OK; } else { res = RES_ERROR; } SD_CS_DISABLE(); break; case GET_BLOCK_SIZE://获取块的大小 *(WORD*)buff = 512; res = RES_OK; break; case GET_SECTOR_COUNT: //获取磁盘的容量 *(DWORD*)buff = SD_GetCapacity(); res = RES_OK; break; default: res = RES_PARERR; break; } return res; }