static uint8_t Test(CLS1_ConstStdIOTypePtr io) { static FIL fp; UINT bw; uint8_t read_buf[16]; uint8_t write_buf[10]; uint8_t i; if (FAT1_isWriteProtected()) { CLS1_SendStr((unsigned char*)"disk is write protected!\r\n", io->stdErr); return ERR_FAILED; } /* write file */ CLS1_SendStr((const unsigned char*)"Creating test.txt...\r\n", io->stdOut); if (FAT1_open(&fp, "./test.txt", FA_CREATE_ALWAYS|FA_WRITE)!=FR_OK) { CLS1_SendStr((const unsigned char*)"*** Failed creating file!\r\n", io->stdErr); return ERR_FAILED; } /* write text */ if (FAT1_write(&fp, "Hello world ", sizeof("Hello world ")-1, &bw)!=FR_OK) { CLS1_SendStr((const unsigned char*)"*** Failed writing string!\r\n", io->stdErr); (void)FAT1_close(&fp); return ERR_FAILED; } write_buf[0] = '\0'; for(i=0;i<4;i++) { UTIL1_strcatNum8u(write_buf, sizeof(write_buf), i); UTIL1_chcat(write_buf, sizeof(write_buf), ' '); } if (FAT1_write(&fp, write_buf, UTIL1_strlen((char*)write_buf), &bw)!=FR_OK) { CLS1_SendStr((const unsigned char*)"*** Failed writing string!\r\n", io->stdErr); (void)FAT1_close(&fp); return ERR_FAILED; } /* closing file */ (void)FAT1_close(&fp); /* read from file */ CLS1_SendStr((const unsigned char*)"Read from file...\r\n", io->stdOut); if (FAT1_open(&fp, "./test.txt", FA_READ)!=FR_OK) { CLS1_SendStr((const unsigned char*)"*** Failed opening file!\r\n", io->stdErr); return ERR_FAILED; } if (FAT1_read(&fp, &read_buf[0], sizeof(read_buf)-2, &bw)!=FR_OK) { CLS1_SendStr((const unsigned char*)"*** Failed reading file!\r\n", io->stdErr); (void)FAT1_close(&fp); return ERR_FAILED; } read_buf[sizeof(read_buf)-2] = '\0'; /* terminate string */ UTIL1_strcat(read_buf, sizeof(read_buf), (unsigned char*)"\r\n"); CLS1_SendStr(read_buf, io->stdOut); CLS1_SendStr((const unsigned char*)"\r\n", io->stdOut); /* close file */ (void)FAT1_close(&fp); return ERR_OK; }
uint8_t PLR_StartNewFile(const char* filename, bool turn) { uint16_t data; if(turn == TRUE){ turning = TRUE; turntime = (config_turnval*1000)/TASKDEL_MS; /*Turn in taskcycles*/ } //Stop actual playback PLR_StopPlayback(); VS_ReadRegister(VS_MODE, &data); data = (data | VS_MODE_SM_LINE1 | VS_MODE_SM_SDINEW); VS_WriteRegister(VS_MODE, data); /*resync*/ VS_WriteRegister(VS_WRAMADDR, 0x1E29); VS_WriteRegister(VS_WRAM, 0); if (FAT1_open(&fp, filename, FA_READ) != FR_OK) { return ERR_FAILED; } /*As in datasheet explained set twice 0 in REG_DECODETIME to set back playback time*/ VS_WriteRegister(VS_DECODE_TIME, 0x00); VS_WriteRegister(VS_DECODE_TIME, 0x00); setPlayback(); while (!VS_Ready()) { //wait until DREQ high } return ERR_OK; }
void LogToFile(DataBuffer* myBuf,char annotation, char* fileName) { int16_t currentBuf = myBuf->currentReadBuffer; //uint8_t write_buf[48]; UINT bw; /* open file */ if (FAT1_open(&fp, fileName, FA_OPEN_ALWAYS|FA_WRITE)!=FR_OK) { Err(/*OPEN_FILE_ERROR*/); } /* move to the end of the file */ if (FAT1_lseek(&fp, fp.fsize) != FR_OK || fp.fptr != fp.fsize) { Err(/*END_OF_FILE_ERROR*/); } /* write data */ write_buf[0] = '\0'; //time_t run_time; for(int cur_buf_index=0;cur_buf_index<BUFFER_SIZE;++cur_buf_index) { /** Time Components */ UTIL1_strcatNum8u(write_buf, sizeof(write_buf), myBuf->dataBuffer[currentBuf][cur_buf_index].time.hour); UTIL1_chcat(write_buf, sizeof(write_buf), ':'); UTIL1_strcatNum8u(write_buf, sizeof(write_buf), myBuf->dataBuffer[currentBuf][cur_buf_index].time.minute); UTIL1_chcat(write_buf, sizeof(write_buf), ':'); UTIL1_strcatNum8u(write_buf, sizeof(write_buf), myBuf->dataBuffer[currentBuf][cur_buf_index].time.second); UTIL1_chcat(write_buf, sizeof(write_buf), ':'); UTIL1_strcatNum8u(write_buf, sizeof(write_buf), myBuf->dataBuffer[currentBuf][cur_buf_index].time.milliBig); UTIL1_strcatNum8u(write_buf, sizeof(write_buf), myBuf->dataBuffer[currentBuf][cur_buf_index].time.milliSmall); UTIL1_chcat(write_buf, sizeof(write_buf), ','); ; /** Accelerometer Components */ UTIL1_strcatNum16s(write_buf, sizeof(write_buf), myBuf->dataBuffer[currentBuf][cur_buf_index].x); UTIL1_chcat(write_buf, sizeof(write_buf), ','); UTIL1_strcatNum16s(write_buf, sizeof(write_buf), myBuf->dataBuffer[currentBuf][cur_buf_index].y); UTIL1_chcat(write_buf, sizeof(write_buf), ','); UTIL1_strcatNum16s(write_buf, sizeof(write_buf), myBuf->dataBuffer[currentBuf][cur_buf_index].z); UTIL1_chcat(write_buf, sizeof(write_buf), ','); /** Annotation mode **/ UTIL1_chcat(write_buf, sizeof(write_buf), annotation); UTIL1_strcat(write_buf, sizeof(write_buf), (unsigned char*)"\n"); /** Write to file **/ if (FAT1_write(&fp, write_buf, UTIL1_strlen((char*)write_buf), &bw)!=FR_OK) { (void)FAT1_close(&fp); Err(/*WRITE_ERROR*/); } memset(write_buf,'\0',48); } if(myBuf->currentReadBuffer == currentBuf) myBuf->currentReadBuffer=-1; /* closing file */ (void)FAT1_close(&fp); }
static void Test(void) { UINT bw; /* number of bytes written */ if (FAT1_isDiskPresent()) { /* if no hardware CardDetect pin is assigned, it will always return TRUE */ LEDR_On(); /* turn red RGB LED on */ FAT1_mount(0, &fs); /* mount file system */ if (!FAT1_isWriteProtected()) { /* if no hardware WritePtotect pin is assigned, it will always return FALSE */ LEDG_On(); /* turn green RGB LED on */ if (FAT1_open(&fp, "./test.txt", FA_CREATE_ALWAYS|FA_WRITE)!=FR_OK) { /* open file, will always create it if not already on disk */ for(;;){} /* error! */ } if (FAT1_write(&fp, "Hello World!", sizeof("Hello World!")-1, &bw)!=FR_OK) { /* write string to file */ for(;;){} /* error! */ } } (void)FAT1_close(&fp); /* close file */ FAT1_mount(0, NULL); /* unmount file system */ } }
static void LogToFile(int16_t x, int16_t y, int16_t z) { uint8_t write_buf[48]; UINT bw; TIMEREC time; /* open file */ if (FAT1_open(&fp, "./log.txt", FA_OPEN_ALWAYS|FA_WRITE)!=FR_OK) { Err(); } /* move to the end of the file */ if (FAT1_lseek(&fp, fp.fsize) != FR_OK || fp.fptr != fp.fsize) { Err(); } /* get time */ if (TmDt1_GetTime(&time)!=ERR_OK) { Err(); } /* write data */ write_buf[0] = '\0'; UTIL1_strcatNum8u(write_buf, sizeof(write_buf), time.Hour); UTIL1_chcat(write_buf, sizeof(write_buf), ':'); UTIL1_strcatNum8u(write_buf, sizeof(write_buf), time.Min); UTIL1_chcat(write_buf, sizeof(write_buf), ':'); UTIL1_strcatNum8u(write_buf, sizeof(write_buf), time.Sec); UTIL1_chcat(write_buf, sizeof(write_buf), '\t'); UTIL1_strcatNum16s(write_buf, sizeof(write_buf), x); UTIL1_chcat(write_buf, sizeof(write_buf), '\t'); UTIL1_strcatNum16s(write_buf, sizeof(write_buf), y); UTIL1_chcat(write_buf, sizeof(write_buf), '\t'); UTIL1_strcatNum16s(write_buf, sizeof(write_buf), z); UTIL1_strcat(write_buf, sizeof(write_buf), (unsigned char*)"\r\n"); if (FAT1_write(&fp, write_buf, UTIL1_strlen((char*)write_buf), &bw)!=FR_OK) { (void)FAT1_close(&fp); Err(); } /* closing file */ (void)FAT1_close(&fp); }
uint8_t VS_PlaySong(const uint8_t *fileName, const CLS1_StdIOType *io) { UINT bytesRead; uint8_t readBuf[32]; uint8_t res = ERR_OK; static FIL fp; if (io!=NULL) { CLS1_SendStr("Playing file '", io->stdOut); CLS1_SendStr(fileName, io->stdOut); CLS1_SendStr("'\r\n", io->stdOut); } if (FAT1_open(&fp, fileName, FA_READ)!=FR_OK) { if (io!=NULL) { CLS1_SendStr("ERR: Failed to open song file\r\n", io->stdErr); } return ERR_FAILED; } for(;;) { /* breaks */ bytesRead = 0; if (FAT1_read(&fp, readBuf, sizeof(readBuf), &bytesRead)!=FR_OK) { if (io!=NULL) { CLS1_SendStr("ERR: Failed to read file\r\n", io->stdErr); } res = ERR_FAILED; break; } if (bytesRead==0) { /* end of file? */ break; } while(!VS_Ready()) { FRTOS1_vTaskDelay(10/portTICK_RATE_MS); } VS_SendData(readBuf, sizeof(readBuf)); } /* closing file */ (void)FAT1_close(&fp); VS_StartSong(); return res; }
/** Data Collection Session**/ void APP_Run(int sel, int collectCount) { //Local variables. int16_t x,y,z; uint8_t res; char* fileName = malloc(sizeof(char)*256); extern int collecting; /* SD card detection: PTE6 with pull-down! */ PORT_PDD_SetPinPullSelect(PORTE_BASE_PTR, 6, PORT_PDD_PULL_DOWN); PORT_PDD_SetPinPullEnable(PORTE_BASE_PTR, 6, PORT_PDD_PULL_ENABLE); int color; char annotation; if(sel == 0) { color = LED_RED; annotation = 'w'; } else if(sel == 1) { color = LED_YELLOW; annotation = 'r'; } else if(sel == 2) { color = LED_GREEN; annotation = 'b'; } else if(sel == 3) { color = LED_CYAN; annotation = 'c'; } else if(sel == 4) { color = LED_BLUE; annotation = 'B'; } else if(sel == 5) { color = LED_MAGENTA; annotation = 't'; } sprintf(fileName, "%d-%c.CSV", collectCount, annotation); /* create and open file */ if (FAT1_open(&fp, fileName, FA_OPEN_ALWAYS|FA_WRITE)!=FR_OK) { Err(/*CREATE_FILE_ERROR*/); } /* closing file */ (void)FAT1_close(&fp); int count = 1; DataBuffer* myBuffer = getDataBuffer(); while(isCollectingData()==1) { if(myBuffer->currentReadBuffer!=-1) { LogToFile(myBuffer,annotation,fileName); } WAIT1_Waitms(2); } me.currentWriteIndex = 0; }
/*! \brief Simple benchmark function: first we are going to write a file, then we will copy it */ static void benchmark(const CLS1_StdIOType *io) { static FIL fp; uint16_t i; UINT bw; uint8_t read_buf[10]; TIMEREC time, startTime; int32_t start_mseconds, mseconds; /* write benchmark */ CLS1_SendStr((const unsigned char*)"Benchmark: open file, write 10k times 10 bytes (100'000 bytes), close file:\r\n", io->stdOut); CLS1_SendStr((const unsigned char*)"Deleting any existing files...\r\n", io->stdOut); (void)FAT1_DeleteFile((const unsigned char*)"./bench.txt", io); (void)FAT1_DeleteFile((const unsigned char*)"./copy.txt", io); CLS1_SendStr((const unsigned char*)"Creating benchmark file...\r\n", io->stdOut); (void)TmDt1_GetTime(&startTime); if (FAT1_open(&fp, "./bench.txt", FA_CREATE_ALWAYS|FA_WRITE)!=FR_OK) { CLS1_SendStr((const unsigned char*)"*** Failed opening benchmark file!\r\n", io->stdErr); return; } for(i=0;i<10000;i++) { if (FAT1_write(&fp, "benchmark ", sizeof("benchmark ")-1, &bw)!=FR_OK) { CLS1_SendStr((const unsigned char*)"*** Failed writing file!\r\n", io->stdErr); (void)FAT1_close(&fp); return; } } (void)FAT1_close(&fp); (void)TmDt1_GetTime(&time); start_mseconds = startTime.Hour*60*60*1000 + startTime.Min*60*1000 + startTime.Sec*1000 + startTime.Sec100*10; mseconds = time.Hour*60*60*1000 + time.Min*60*1000 + time.Sec*1000 + time.Sec100*10 - start_mseconds; CLS1_SendNum32s(mseconds, io->stdOut); CLS1_SendStr((const unsigned char*)" mseconds needed for command.\r\n", io->stdOut); /* read benchmark */ CLS1_SendStr((const unsigned char*)"Reading benchmark file...\r\n", io->stdOut); (void)TmDt1_GetTime(&startTime); if (FAT1_open(&fp, "./bench.txt", FA_READ)!=FR_OK) { CLS1_SendStr((const unsigned char*)"*** Failed opening benchmark file!\r\n", io->stdErr); return; } for(i=0;i<10000;i++) { if (FAT1_read(&fp, &read_buf[0], sizeof(read_buf), &bw)!=FR_OK) { CLS1_SendStr((const unsigned char*)"*** Failed reading file!\r\n", io->stdErr); (void)FAT1_close(&fp); return; } } (void)FAT1_close(&fp); (void)TmDt1_GetTime(&time); start_mseconds = startTime.Hour*60*60*1000 + startTime.Min*60*1000 + startTime.Sec*1000 + startTime.Sec100*10; mseconds = time.Hour*60*60*1000 + time.Min*60*1000 + time.Sec*1000 + time.Sec100*10 - start_mseconds; CLS1_SendNum32s(mseconds, io->stdOut); CLS1_SendStr((const unsigned char*)" mseconds needed for command.\r\n", io->stdOut); /* copy benchmark */ CLS1_SendStr((const unsigned char*)"Benchmark: copy file (100'000 bytes):\r\n", io->stdOut); CLS1_SendStr((const unsigned char*)"Going to copy file...\r\n", io->stdOut); (void)TmDt1_GetTime(&startTime); (void)FAT1_CopyFile((const unsigned char*)"./bench.txt", (const unsigned char*)"./copy.txt", io); (void)TmDt1_GetTime(&time); start_mseconds = startTime.Hour*60*60*1000 + startTime.Min*60*1000 + startTime.Sec*1000 + startTime.Sec100*10; mseconds = time.Hour*60*60*1000 + time.Min*60*1000 + time.Sec*1000 + time.Sec100*10 - start_mseconds; CLS1_SendNum32s(mseconds, io->stdOut); CLS1_SendStr((const unsigned char*)" mseconds needed for command.\r\n", io->stdOut); CLS1_SendStr((const unsigned char*)"done!\r\n", io->stdOut); }
/*! * \brief Copy the source file to a destination file * \param[in] srcFileName Source file name * \param[in] dstFileName Destination file name * \param[in] io IO handler for output * \return Error code, ERR_OK for success. */ byte UFFS_FAT_CopyFile(const byte *srcFileName, const byte *dstFileName, const CLS1_StdIOType *io) { bool sourceUffs = pdTRUE; ///< source is uffs (otherwise FAT) bool destUffs = pdTRUE; ///< destination is uffs (otherwise FAT) int fd1=-1, fd2=-1; // uffs file pointers FAT1_FIL fsrc, fdst; // FAT file objects FAT1_FRESULT fres; // FAT result uint8_t buffer[32]; /* copy buffer */ UINT br, bw, bt=0; /* file read/write counters */ byte res = ERR_OK; if( srcFileName[1] == ':') sourceUffs = pdFALSE; if( dstFileName[1] == ':') { destUffs = pdFALSE; if (FAT1_isWriteProtected() || FAT1_FS_READONLY) { MSGLN("destination FAT disk is write protected!"); return ERR_FAILED; } } /* open source file */ if(sourceUffs) { fd1 = uffs_open((char*)srcFileName, UO_RDONLY); if (fd1 < 0) { MSGLN("open source file failed"); return ERR_FAILED; } } else { fres = FAT1_open(&fsrc, (char*)srcFileName, FA_OPEN_EXISTING | FA_READ); if (fres != FR_OK) { MSGLN("open source file failed. Result=%d", fres); return ERR_FAILED; } } /* create destination file */ if(destUffs) { fd2 = uffs_open((char*)dstFileName, UO_CREATE|UO_WRONLY); if (fd2 < 0) { MSGLN("open destination file failed"); return ERR_FAILED; } } else { fres = FAT1_open(&fdst, (char*)dstFileName, FA_CREATE_ALWAYS | FA_WRITE); if (fres != FR_OK) { MSGLN("open destination file failed. Result=%d", fres); return ERR_FAILED; } } /* now copy source to destination */ for (;;) { if(sourceUffs) { br = uffs_read(fd1, buffer, sizeof(buffer)); } else { fres = FAT1_read(&fsrc, buffer, sizeof(buffer), &br); if (fres != FR_OK) { MSGLN("reading source file failed. Result=%d", fres); res = ERR_FAILED; break; } } bt += br; if (br == 0) { /* EOF */ break; /* get out of loop */ } if(destUffs) { bw = uffs_write(fd2, buffer, br); } else { fres = FAT1_write(&fdst, buffer, br, &bw); if (fres != ERR_OK) { MSGLN("writing destination file failed. Result=%d", fres); res = ERR_FAILED; break; } } if (bw < br) { MSGLN("failed writing destination file, or disk full"); res = ERR_FAILED; break; } } /* for */ /* close all files */ if(sourceUffs) uffs_close(fd1); else { fres = FAT1_close(&fsrc); if (fres != FR_OK) { MSGLN("closing source file failed. Result=%d", fres); res = ERR_FAILED; } } if(destUffs) uffs_close(fd2); else { fres = FAT1_close(&fdst); if (fres != FR_OK) { MSGLN("closing destination file failed. Result=%d", fres); res = ERR_FAILED; } } MSGLN("%u bytes copied.", bt); return res; }