int main(void) { uint32_t i; DelayInit(); GPIO_QuickInit(HW_GPIOE, 6, kGPIO_Mode_OPP); UART_QuickInit(UART0_RX_PD06_TX_PD07, 115200); printf("SD test\r\n"); printf("please insert SD card...\r\n"); //SD卡模块快速初始化,设置速度为20000000 SD_QuickInit(20000000); //获取SD卡的容量 printf("SD size:%dMB\r\n", SD_GetSizeInMB()); /* 读取0扇区数据,每一个扇区512字节 */ SD_ReadSingleBlock(0, sd_buffer); /* 打印0扇区数据 */ printf("sectoer 0 data:\r\n"); for(i = 0; i < 512; i++) { printf("0x%02X ", sd_buffer[i]); } while(1) { //小灯闪烁 GPIO_ToggleBit(HW_GPIOE, 6); DelayMs(500); } }
int main(void) { DelayInit(); GPIO_QuickInit(HW_GPIOE, 6, kGPIO_Mode_OPP); UART_QuickInit(UART0_RX_PD06_TX_PD07, 115200); printf("FATFS test\r\n"); printf("please insert SD card...\r\n"); if(SD_QuickInit(20*1000*1000)) { printf("SD card init failed!\r\n"); while(1); } printf("SD size:%dMB\r\n", SD_GetSizeInMB()); FRESULT rc; FATFS fs_sd; FIL fil; FATFS *fs; fs = &fs_sd; UINT bw,br; /* bw = byte writted br = byte readed */ DWORD fre_clust, fre_sect, tot_sect; /* 挂载文件系统 */ rc = f_mount(fs, "0:", 0); ERROR_TRACE(rc); rc = f_getfree("0:", &fre_clust, &fs); ERROR_TRACE(rc); /* 计算磁盘空间及剩余空间 */ tot_sect = (fs->n_fatent - 2) * fs->csize; fre_sect = fre_clust * fs->csize; printf("%d KB total drive space.\r\n%d KB available.\r\n", tot_sect / 2, fre_sect / 2); /* 写入文件 */ printf("open or create file\r\n"); rc = f_open(&fil, "0:/fatfs.txt", FA_WRITE | FA_CREATE_ALWAYS); ERROR_TRACE(rc); printf("write file\r\n"); rc = f_write(&fil, "HelloWorld\r\n", 12, &bw); ERROR_TRACE(rc); printf("%d bytes writen\r\n", bw); rc = f_close(&fil); /* 读取文件 */ rc = f_open(&fil, "0:/fatfs.txt", FA_READ); ERROR_TRACE(rc); printf("file size:%l\r\n", f_size(&fil)); printf("file contents:\r\n"); while(1) { rc = f_read(&fil, buf, sizeof(buf), &br); if(rc || !br ) break; printf("%s", buf); } rc = f_close(&fil); ERROR_TRACE(rc); while(1) { GPIO_ToggleBit(HW_GPIOE, 6); DelayMs(500); } }
int main(void) { DelayInit(); GPIO_QuickInit(HW_GPIOE, 6, kGPIO_Mode_OPP); SRAM_Init(); UART_QuickInit(UART0_RX_PD06_TX_PD07, 115200); GUI_Init(); GUI_DispString("BMP file test\r\n"); GUI_DispString("please insert SD card...\r\n"); SD_QuickInit(10*1000*1000); printf("SD size:%dMB\r\n", SD_GetSizeInMB()); FATFS fs_sd; FATFS *fs = &fs_sd; /* 挂载文件系统 */ f_mount(fs, "0:", 0); while(1) { scan_files("0:"); GPIO_ToggleBit(HW_GPIOE, 6); } }
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 SD_CARD : switch(cmd) { case CTRL_SYNC: res = RES_OK; break; case GET_SECTOR_SIZE: *(WORD*)buff = 512; res = RES_OK; break; case GET_BLOCK_SIZE: *(WORD*)buff = 8; res = RES_OK; break; case GET_SECTOR_COUNT: *(DWORD*)buff = SD_GetSizeInMB()*1024*2; res = RES_OK; break; default: res = RES_PARERR; break; } return res; } return RES_PARERR; }