/*---------------------------------------------------------------------------*/ static void die ( /* Stop with dying message */ FRESULT rc /* FatFs return value */ ) { put_rc (rc); for (;;) ; }
/*---------------------------------------------------------------------------*/ int cfs_open(const char *n, int f) { UINT accessmode_app = (FA_OPEN_ALWAYS|FA_WRITE|FA_READ); UINT accessmode = (FA_CREATE_NEW|FA_WRITE|FA_READ); UINT accessmode_ow = (FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_WRITE|FA_READ); UINT file_exist = 0; FRESULT rc; DWORD ptr; PRINTF("DEBUG: OrisenSpectrum: cfs_open - flags = %d\r\n", f); rc = f_open(&fil, n, accessmode_app); PRINTF("DEBUG: -OrisenSpectrum: cfs_open: rc = %d\r\n", rc); if (rc == FR_OK) { PRINTF("file exists\r\n"); file_exist = 1; } f_close(&fil); if(f & CFS_READ) { rc = f_open(&fil, n, FA_READ); PRINTF("DEBUG: --OrisenSpectrum: cfs_open: rc = %d\r\n", rc); } if((f & CFS_WRITE) || (f & CFS_APPEND)) { if((f & CFS_WRITE) || ((f & CFS_APPEND) && (file_exist == 0)) ){ PRINTF("File over write=%d\r\n", fil.fptr); rc = f_open(&fil, n, accessmode_ow); } else if((f & CFS_APPEND) && (file_exist == 1)) { //append file rc = f_open(&fil, n, accessmode_app); PRINTF("File append pointer=%d, size=%d\r\n", fil.fptr, fil.fsize); rc |= f_lseek(&fil,fil.fsize); PRINTF("File pointer=%d\r\n", fil.fptr); rc |=f_sync(&fil); } } PRINTF("DEBUG: ---OrisenSpectrum: cfs_open: rc = %d\r\n", rc); if (rc) { //die(rc); return -1; } put_rc(rc); return rc; }
/*---------------------------------------------------------------------------*/ int cfs_write(int f, const void *buf, unsigned int len) { UINT bw; char readbuf[5]; FRESULT rc; rc=f_write(&fil, buf, len, &bw); if (bw != len) { printf("Error writing data packet, %u bytes written\n", bw); return -1; } //rc |= f_read(&fil, readbuf, 0, &bw); rc |=f_sync(&fil); put_rc(rc); // Jagun - the number of bytes written is supposed to be returned return bw; // return rc; // rather than error code- see the Contiki API }
/*---------------------------------------------------------------------------*/ int cfs_read(int f, void *buf, unsigned int len) { FRESULT rc; // Jagun@UCL f_read takes care of this. No need for return code checking // We will just return the number of bytes read, which could be 0 // for (;;) { rc = f_read(&fil, buf, len, &br); /* Read a chunk of file */ // if (rc || !br) break; /* Error or end of file */ // } //if (rc) die(rc); // Jagun@UCL If we close the file, br might be affected and // files should be closed explicitly by cfs_close() // rc = f_close(&fil); //if (rc) die(rc); put_rc(rc); //Jagun@UCL cfs_read is supposed to return the no. of bytes read // NOT error code; See the Contiki API // return rc; return br; }
int32_t main(void) { FRESULT res; UINT s2, cnt; uint32_t i; /* Unlock protected registers */ SYS_UnlockReg(); /* Init System, IP clock and multi-function I/O */ SYS_Init(); //In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register. /* Lock protected registers */ SYS_LockReg(); /* Init UART0 for printf */ UART0_Init(); /* Init USB host and prepare a root hub*/ USBH_Open(); /* Set USB host to support USB mass storage device*/ USBH_MassInit(); /*Delay for power stable*/ Delay(0x500000); printf("+-----------------------------------------------+\n"); printf("| |\n"); printf("| M451 USB Host Mass Storage sample program |\n"); printf("| |\n"); printf("+-----------------------------------------------+\n"); printf("OTG->PHYCTL = 0x%x\n", OTG->PHYCTL); printf("OTG->STS = 0x%x\n", OTG->STATUS); printf("OTG->CTL = 0x%x\n", OTG->CTL); while(1) { USBH_ProcessHubEvents();//Process hub events that include resume, suspense, plug-in, unplug and so on. Delay(0x500000); if(disk_status(0)&&u8Disk) //Check mass storage device status { put_rc(f_mount(0, NULL));//No disk u8Disk = 0; printf("\n======= No disk found =======\n\n"); } if((disk_status(0)==0)&&(u8Disk==0)) { put_rc(f_mount(0, &FatFs[0]));//disk found u8Disk = 1; printf("\n======= Disk found =======\n"); #ifdef DiskTest putchar('\n'); res = f_open(&file1, (TCHAR*)"Test.txt", FA_OPEN_EXISTING | FA_READ);// Open "Test.txt" file for read operation if(res)//Check the result of FatFs function {//Fail --> Maybe connection fail or Not find "Text.txt" file put_rc(res);//Print fail message res = f_open(&file1, (TCHAR*)"Test.txt", FA_CREATE_ALWAYS | FA_WRITE);// Open "Test.txt" file for write operation if(res) {//Fail put_rc(res);//Print fail message } else {//Pass cnt = 512;//Set data lenght for(i=0;i<cnt;i++) Buff[i] = i;//Prepare data for write res = f_write(&file1, Buff, cnt, &s2);//Write data into file. if(res != FR_OK) {//Fail put_rc(res);//Print fail message } f_close(&file1);//Close "Test.txt" file printf("\nData lenght:%d\n",s2);//Print how many wrote data } } else {//Pass cnt = 512;//Set data lenght res = f_read(&file1, Buff, cnt, &s2);//Read data from file. if(res != FR_OK) {//Fail put_rc(res);//Print fail message } else {//Pass printf("Data size:%d\n",s2);//Print how many read data for(i=0;i<s2;i++) { if((i%8) == 0 ) printf("\n"); printf("0x%2x ",Buff[i]);//Print read data } } f_close(&file1);//Close "Test.txt" file printf("\n\nData lenght:%d\n",s2);//Print how many wrote data printf("Test finish.\n");//Print how many wrote data } #endif } } }
void sd_status(void) { long p2; PF_BYTE res, b; const PF_BYTE ft[] = { 0, 12, 16, 32 }; CDCprintln("\nf_getfree(): "); res = f_getfree("/", (DWORD*) &p2, &Fat); if (!res) { CDCprintln(" FAT type = FAT%u", ft[Fat->fs_type & 3]); CDCprintln(" Bytes/Cluster = %u", Fat->csize * 512UL); CDCprintln(" Number of FATs = %u", Fat->n_fats); CDCprintln(" Root DIR entries = %u", Fat->n_rootdir); CDCprintln(" Sectors/FAT = %u", Fat->fsize); CDCprintln(" Number of clusters = %u", Fat->n_fatent - 2); CDCprintln(" FAT start (lba) = %u", Fat->fatbase); CDCprintln(" DIR start (lba,cluster) = %u", Fat->dirbase); CDCprintln(" Data start (lba) = %u", Fat->database); #ifdef SD_DEBUG } else { put_rc(res); #endif } AccSize = AccFiles = AccDirs = 0; CDCprintln("\nscan_files(): "); res = scan_files("/"); if (!res) { CDCprintln(" %u files, %u bytes.", AccFiles, AccSize); CDCprintln(" %u folders.", AccDirs); CDCprintln(" %u KB total disk space.", (Fat->n_fatent - 2) * (Fat->csize / 2)); CDCprintln(" %u KB available.\r\n", p2 * (Fat->csize / 2)); #ifdef SD_DEBUG } else { CDCprintln("Failed!"); put_rc(res); #endif } if ((res = disk_ioctl(0, MMC_GET_TYPE, &b)) == RES_OK) { CDCprintln("MMC/SDC type: %u", b); #ifdef SD_DEBUG } else { CDCprintf("Could not determine card type. "); put_rc(res); #endif } if ((res = disk_ioctl(0, GET_SECTOR_COUNT, &p2)) == RES_OK) { CDCprintln("Drive size: %u sectors", p2); #ifdef SD_DEBUG } else { CDCprintf("Could not determine drive size. "); put_rc(res); #endif } if ((res = disk_ioctl(0, GET_BLOCK_SIZE, &p2)) == RES_OK) { CDCprintln("Erase block: %u sectors", p2); #ifdef SD_DEBUG } else { CDCprintf("Could not determine Erase block size. "); put_rc(res); #endif } }
void main (void) { FIL f1, f2; /* 文件对象 */ FRESULT res; /* FatFs 函数公共结果代码 */ char cmd[100]; TCHAR *ptr, *ptr2, pool[50]; long p1, p2, p3; BYTE *buf; UINT s1, s2, cnt; WORD w; DWORD dw, ofs = 0, sect = 0, drv = 0; static const BYTE ft[] = {0, 12, 16, 32}; FATFS *fs; /* Pointer to file system object */ DIR dir; /* Directory object */ FIL file[2]; /* File objects */ while(1){ ptr=cmd; printf(">"); scanf("%s",ptr); switch (*ptr++){ case 'q': return 0; case '?': printf("%s\n",HelpStr ); break; case 'd' : /* Disk I/O command */ switch (*ptr++) { /* 第二个指令 */ case 'd' : /* dd [<pd#> <sect>] - Dump a secrtor */ if (!xatoi(&ptr, &p1)) { p1 = drv; p2 = sect; } else { if (!xatoi(&ptr, &p2)) break; } res = disk_read((BYTE)p1, Buff, p2, 1); if (res) { printf("rc=%d\n", (WORD)res); break; } printf("Drive:%u Sector:%lu\n", p1, p2); if (disk_ioctl((BYTE)p1, GET_SECTOR_SIZE, &w) != RES_OK) break; sect = p2 + 1; drv = p1; for (buf = Buff, ofs = 0; ofs < w; buf += 16, ofs += 16) put_dump(buf, ofs, 16); break; case 'i' : /* di <pd#> - 磁盘初始化 */ if (!xatoi(&ptr, &p1)) break; res = disk_initialize((BYTE)p1); printf("rc=%d\n", res); if (disk_ioctl((BYTE)p1, GET_SECTOR_SIZE, &w) == RES_OK) printf("Sector size = %u\n", w); if (disk_ioctl((BYTE)p1, GET_SECTOR_COUNT, &dw) == RES_OK) printf("Number of sectors = %u\n", dw); break; case 's': if (!xatoi(&ptr, &p1)) break; if (disk_ioctl((BYTE)p1, GET_SECTOR_SIZE, &w) == RES_OK) printf("Sector size = %u\n", w); if (disk_ioctl((BYTE)p1, GET_SECTOR_COUNT, &dw) == RES_OK) printf("Number of sectors = %u\n", dw); } case 'f' : /* FatFs test command */ switch (*ptr++) { /* Branch by secondary command character */ case 'i':/*挂载fi <ld#> [<mount>]*/ if (!xatoi(&ptr, &p1) || (UINT)p1 > 9) break; if (!xatoi(&ptr, &p2)) p2 = 0; sprintf(ptr, "%d:", p1); res=f_mount(&FatFs[p1], ptr, (BYTE)p2); printf("%d\n",res ); break; case 's':/*状态fs [<path>]*/ while (*ptr == ' ') ptr++; ptr2 = ptr; #if _FS_READONLY res = f_opendir(&dir, ptr); if (res) { fs = dir.fs; f_closedir(&dir); } #else res = f_getfree(ptr, (DWORD*)&p1, &fs); #endif if (res) { put_rc(res); break; } printf("FAT type = FAT%u\nNumber of FATs = %u\n", ft[fs->fs_type & 3], fs->n_fats); printf("Cluster size = %u sectors, %lu bytes\n", #if _MAX_SS != 512 fs->csize, (DWORD)fs->csize * fs->ssize); #else fs->csize, (DWORD)fs->csize * 512); #endif if (fs->fs_type != FS_FAT32) printf("Root DIR entries = %u\n", fs->n_rootdir); printf("Sectors/FAT = %lu\nNumber of clusters = %lu\nVolume start sector = %lu\nFAT start sector = %lu\nRoot DIR start %s = %lu\nData start sector = %lu\n\n", fs->fsize, fs->n_fatent - 2, fs->volbase, fs->fatbase, fs->fs_type == FS_FAT32 ? _T("cluster") : _T("sector"), fs->dirbase, fs->database); #if _USE_LABEL res = f_getlabel(ptr2, pool, &dw); if (res) { put_rc(res); break; } _tprintf(pool[0] ? _T("Volume name is %s\n") : _T("No volume label\n"), pool); _tprintf(_T("Volume S/N is %04X-%04X\n"), dw >> 16, dw & 0xFFFF); #endif printf("..."); AccSize = AccFiles = AccDirs = 0; res = scan_files(ptr); if (res) { put_rc(res); break; } p2 = (fs->n_fatent - 2) * fs->csize; p3 = p1 * fs->csize; #if _MAX_SS != 512 p2 *= fs->ssize / 512; p3 *= fs->ssize / 512; #endif p2 /= 2; p3 /= 2; printf("\r%u files, %I64u bytes.\n%u folders.\n%lu KiB total disk space.\n", AccFiles, AccSize, AccDirs, p2); #if !FS_READONLY printf("%lu KiB available.\n", p3); #endif break; } } } /* 为逻辑驱动器注册工作区 */ printf("%d\n",f_mount(&fs[0],"",1)); printf("%d\n",f_mkfs("",0,0) ); getch(); printf("%d\n",f_mkdir("test")); printf("%d\n",f_mkdir("test\\test1")); f_open(&f1, "message.txt", FA_CREATE_NEW); printf("%d\n",f_mkdir("test2")); return; }