int main(void) { //******************************************************************* Init(); TMRInit(2); // Initialize Timer interface with Priority=2 BLIInit(); // Initialize Signal interface //******************************************************************* //_T1IE = 0; // Temporarily disable Timer1 interrupt //******************************************************************* LogInit(); // Initialize Data Logger //******************************************************************* //------------------------------------------------------------------- #define MaxRec 60 //------------------------------------------------------------------- struct { DWORD TS; WORD Index; char filler[506]; } Data; //------------------------------------------------------------------- uint i; for (i = 0; i < sizeof(Data.filler); i++) { Data.filler[i] = 0; } //------------------------------------------------------------------- DWORD TimeStart; float TimePoints[MaxRec]; // WORD TimeIndex[MaxRec]; //------------------------------------------------------------------- char pFN[15]; WORD RC; uint ReadCnt; FSFILE File; //******************************************************************* BLISignalON(); //------------------------------ // Create (Open) Log file for Writing and Reading //------------------------------ RC = LogNewFile(&File); while ( RC != LE_GOOD ); //------------------------------ // Retrieve and save for future new log file name //------------------------------ RC = FS_GetName(pFN, 15, &File); while ( RC != LE_GOOD ); //------------------------------ // Write sample data to file //------------------------------ for (i=0; i < MaxRec; i++) { Data.Index = i; Data.TS = TMRGetTS(); RC = FS_WriteSector(&File, &Data); while (CE_GOOD != RC); } //------------------------------ // Check position in the file //------------------------------ i = FS_GetPosition (&File); while (i != 512*MaxRec); //------------------------------ // Close the file - save changes //------------------------------ RC = FS_Flush(&File); while ( RC != CE_GOOD ); //------------------------------ //------------------------------ // Open file for Reading //------------------------------ RC = FS_CreateFile(pFN, FS_READ_ONLY, &File); while ( RC != CE_GOOD ); //------------------------------ // Reed records //------------------------------ for (i=0; i < MaxRec; i++) { RC = FS_Read (&File, &Data, sizeof(Data), &ReadCnt); while (CE_GOOD != RC); //-------------------------- // TimeIndex[i] = Data.Index; //-------------------------- if (0 == i) TimeStart = Data.TS; //-------------------------- TimePoints[i] = (Data.TS - TimeStart) * TMRGetTSRate() * 1000.0; // in msec //-------------------------- TimeStart = Data.TS; } //------------------------------ // Close the file - save changes //------------------------------ RC = FS_Flush(&File); while ( RC != CE_GOOD ); //******************************************************************* BLISignalOFF(); //------------------------------ i = 1 + i; //------------------------------ while(1); return 0; }
int main(void) { //******************************************************************* Init(); TMRInit(2); // Initialize Timer interface with Priority=2 BLIInit(); // Initialize Signal interface //******************************************************************* //_T1IE = 0; // Temporarily disable Timer1 interrupt //******************************************************************* LogInit(); // Initialize Data Logger //******************************************************************* uint i; char RData[30]; char CData[30]; for (i = 0; i < 30; i++) { RData[i] = CData[i] = 0; } char* pFN = "log1.txt"; void* pWData = &"01 02 03 04 05 06 07 08 09"; //------------------------------------------------------------------- CETYPE RC; uint ReadCnt; FSFILE File; //******************************************************************* BLISignalON(); //------------------------------ // Create (Open) file for Writing and Reading //------------------------------ RC = FS_CreateFile(pFN, FS_CREATE_NEW, &File); while ( RC != CE_GOOD ); //------------------------------ // Write sample string to file //------------------------------ RC = FS_Write (&File, pWData, 26 ); while (CE_GOOD != RC); //------------------------------ // Check position in the file //------------------------------ i = FS_GetPosition (&File); while (i != 26); //------------------------------ // Seek to start //------------------------------ FS_SeekStart(&File); //------------------------------ // Reed from start //------------------------------ RC = FS_Read (&File, RData, 26, &ReadCnt); while (CE_GOOD != RC); //------------------------------ // Close the file - save changes //------------------------------ RC = FS_Flush(&File); while ( RC != CE_GOOD ); //------------------------------ //------------------------------ // Open file for Reading //------------------------------ RC = FS_CreateFile(pFN, FS_READ_ONLY, &File); while ( RC != CE_GOOD ); //------------------------------ // Reed from start //------------------------------ RC = FS_Read (&File, CData, 26, &ReadCnt); while (CE_GOOD != RC); //------------------------------ // Close the file - save changes //------------------------------ RC = FS_Flush(&File); while ( RC != CE_GOOD ); //------------------------------ i = 1 + i; //******************************************************************* BLISignalOFF(); //------------------------------ while(1); return 0; }
void FFS_Ioctl(struct IPCMessage *msg) { FIL fil; u8 *bufin = (u8*)msg->ioctl.buffer_in; u32 lenin = msg->ioctl.length_in; u8 *bufout = (u8*)msg->ioctl.buffer_io; u32 lenout = msg->ioctl.length_io; s32 ret; //if( ((((vu32)bufin>>24)==0)&&lenin) || ((((vu32)bufout>>24)==0)&&lenout) ) //{ // dbgprintf("FFS:in:0x%p\tout:0x%p\n", bufin, bufout ); //} #ifdef EDEBUG dbgprintf("FFS:IOS_Ioctl( %d 0x%x 0x%p 0x%x 0x%p 0x%x )\n", msg->fd, msg->ioctl.command, bufin, lenin, bufout, lenout); #endif ret = FS_SUCCESS; switch(msg->ioctl.command) { case IOCTL_IS_USB: { ret = FS_SUCCESS; } break; case IOCTL_NANDSTATS: { if( lenout < 0x1C ) ret = -1017; else { NANDStat fs; //TODO: get real stats from SD CARD? fs.BlockSize = 0x4000; fs.FreeBlocks = 0x5DEC; fs.UsedBlocks = 0x1DD4; fs.unk3 = 0x10; fs.unk4 = 0x02F0; fs.Free_INodes = 0x146B; fs.unk5 = 0x0394; memcpy( bufout, &fs, sizeof(NANDStat) ); } ret = FS_SUCCESS; #ifdef DEBUG dbgprintf("FFS:GetNANDStats( %d, %p ):%d\n", msg->fd, msg->ioctl.buffer_io, ret ); #endif } break; case IOCTL_CREATEDIR: { ret = FS_CreateDir( (char*)(bufin+6) ); #ifdef USEATTR if( ret == FS_SUCCESS ) { //create attribute file char *path = (char*)heap_alloc_aligned( 0, 0x40, 32 ); _sprintf( path, "%s.attr", (char*)(bufin+6) ); if( f_open( &fil, path, FA_CREATE_ALWAYS | FA_WRITE ) == FR_OK ) { u32 wrote; f_lseek( &fil, 6 ); f_write( &fil, bufin+0x46, 4, &wrote); f_close( &fil ); } heap_free( 0, path ); } #endif #ifdef DEBUG dbgprintf("FFS:CreateDir(\"%s\", %02X, %02X, %02X, %02X ):%d\n", (char*)(bufin+6), *(u8*)(bufin+0x46), *(u8*)(bufin+0x47), *(u8*)(bufin+0x48), *(u8*)(bufin+0x49), ret ); #endif } break; case IOCTL_SETATTR: { if( lenin != 0x4A && lenin != 0x4C ) { ret = FS_EFATAL; } else { ret = FS_SetAttr( (char*)(bufin+6) ); } #ifdef USEATTR if( ret == FS_SUCCESS ) { //create attribute file char *path = (char*)heap_alloc_aligned( 0, 0x40, 32 ); _sprintf( path, "%s.attr", (char*)(bufin+6) ); if( f_open( &fil, (char*)path, FA_CREATE_ALWAYS | FA_WRITE ) == FR_OK ) { u32 wrote; if( lenin == 0x4A ) f_write( &fil, bufin, 4+2, &wrote); else f_lseek( &fil, 6 ); f_write( &fil, bufin+0x46, 4, &wrote); f_close( &fil ); } heap_free( 0, path ); } #endif #ifdef DEBUG dbgprintf("FFS:SetAttr(\"%s\", %08X, %04X, %02X, %02X, %02X, %02X):%d in:%X out:%X\n", (char*)(bufin+6), *(u32*)(bufin), *(u16*)(bufin+4), *(u8*)(bufin+0x46), *(u8*)(bufin+0x47), *(u8*)(bufin+0x48), *(u8*)(bufin+0x49), ret, lenin, lenout ); #endif } break; case IOCTL_GETATTR: { char *s=NULL; switch( lenin ) { case 0x40: s = (char*)(bufin); ret= FS_SUCCESS; break; case 0x4A: hexdump( bufin, lenin ); s = (char*)(bufin+6); ret= FS_SUCCESS; break; default: hexdump( bufin, lenin ); ret = FS_EFATAL; break; } if( ret != FS_EFATAL ) { if( f_open( &fil, s, FA_READ ) == FR_OK ) { f_close( &fil ); ret = FS_SUCCESS; } else { DIR d; if( f_opendir( &d, s ) == FR_OK ) { ret = FS_SUCCESS; } else { ret = FS_ENOENT2; } } #ifdef USEATTR //read attribute file char *path = (char*)heap_alloc_aligned( 0, 0x40, 32 ); _sprintf( path, "%s.attr", s ); if( f_open( &fil, path, FA_OPEN_EXISTING | FA_READ ) == FR_OK ) { u32 read; f_read( &fil, bufout, 4+2, &read); f_read( &fil, bufout+0x46, 4, &read); f_close( &fil ); } heap_free( 0, path ); #else *(u32*)(bufout) = 0x0000; *(u16*)(bufout) = 0x1000; *(u8*)(bufout+0x46) = 3; *(u8*)(bufout+0x47) = 3; *(u8*)(bufout+0x48) = 3; *(u8*)(bufout+0x49) = 3; #endif } #ifdef DEBUG dbgprintf("FFS:GetAttr(\"%s\", %02X, %02X, %02X, %02X ):%d in:%X out:%X\n", s, *(u8*)(bufout+0x46), *(u8*)(bufout+0x47), *(u8*)(bufout+0x48), *(u8*)(bufout+0x49), ret, lenin, lenout ); #endif } break; case IOCTL_DELETE: { ret = FS_DeleteFile( (char*)bufin ) ; #ifdef DEBUG dbgprintf("FFS:Delete(\"%s\"):%d\n", (char*)bufin, ret ); #endif } break; case IOCTL_RENAME: { ret = FS_Move( (char*)bufin, (char*)(bufin + 0x40) ); #ifdef USEATTR if( ret == FS_SUCCESS ) { //move attribute file char *src = (char*)heap_alloc_aligned( 0, 0x80, 32 ); char *dst = (char*)heap_alloc_aligned( 0, 0x80, 32 ); _sprintf( src, "%s.attr", (char*)bufin ); _sprintf( dst, "%s.attr", (char*)(bufin + 0x40) ); ret = FS_Move( src, dst ); //dbgprintf("FFS:Rename(\"%s\", \"%s\"):%d\n", src, dst, ret ); heap_free( 0, src ); heap_free( 0, dst ); } #endif #ifdef DEBUG dbgprintf("FFS:Rename(\"%s\", \"%s\"):%d\n", (char*)bufin, (char*)(bufin + 0x40), ret ); #endif } break; case IOCTL_CREATEFILE: { ret = FS_CreateFile( (char*)(bufin+6) ); #ifdef USEATTR if( ret == FS_SUCCESS ) { //create attribute file char *path = (char*)heap_alloc_aligned( 0, 0x40, 32 ); _sprintf( path, "%s.attr", (char*)(bufin+6) ); if( f_open( &fil, path, FA_CREATE_ALWAYS | FA_WRITE ) == FR_OK ) { u32 wrote; if( lenin == 0x4A ) f_write( &fil, bufin, 4+2, &wrote); else f_lseek( &fil, 6 ); f_write( &fil, bufin+0x46, 4, &wrote); f_close( &fil ); } heap_free( 0, path ); } #endif #ifdef DEBUG //if( ret != -105 ) dbgprintf("FFS:CreateFile(\"%s\", %02X, %02X, %02X, %02X):%d\n", (char*)(bufin+6), *(u8*)(bufin+0x46), *(u8*)(bufin+0x47), *(u8*)(bufin+0x48), *(u8*)(bufin+0x49), ret ); #endif } break; case IOCTL_GETSTATS: { ret = FS_GetStats( msg->fd, (FDStat*)bufout ); #ifdef DEBUG dbgprintf("FFS:GetStats( %d, %d, %d ):%d\n", msg->fd, ((FDStat*)bufout)->file_length, ((FDStat*)bufout)->file_length, ret ); #endif } break; case IOCTL_SHUTDOWN: //dbgprintf("FFS:Shutdown()\n"); //Close all open FS handles //u32 i; //for( i=0; i < MAX_FILE; ++i) //{ // if( fd_stack[i].fs != NULL ) // f_close( &fd_stack[i] ); //} ret = FS_SUCCESS; break; default: #ifdef EDEBUG dbgprintf("FFS:Unknown IOS_Ioctl( %d 0x%x 0x%p 0x%x 0x%p 0x%x )\n", msg->fd, msg->ioctl.command, bufin, lenin, bufout, lenout); #endif ret = FS_EFATAL; break; } #ifdef EDEBUG dbgprintf("FFS:IOS_Ioctl():%d\n", ret); #endif mqueue_ack( (void *)msg, ret); }
int main(void) { //******************************************************************* Init(); TMRInit(2); // Initialize Timer interface with Priority=2 BLIInit(); // Initialize Signal interface //******************************************************************* //_T1IE = 0; // Temporarily disable Timer1 interrupt //******************************************************************* LogInit(); // Initialize Data Logger //******************************************************************* uint i; char RData[30]; char CData[30]; for (i = 0; i < 30; i++) { RData[i] = CData[i] = 0; } char* pFN = "log1.txt"; void* pWData = &"01 02 03 04 05 06 07 08 09"; //------------------------------------------------------------------- CETYPE RC; uint ReadCnt; FSFILE File; SEARCH_STATE Search; //******************************************************************* BLISignalON(); //******************************************************************* // Search for every entry in the Root RC = FS_FindFirst( "*", ATTR_NONE, ATTR_NONE, &Search, &File); while (CE_GOOD == RC) { RC = FS_FindNext(&Search, &File); } // Search only for files RC = FS_FindFirst( "*", ATTR_NONE, ATTR_DIRECTORY, &Search, &File); while (CE_GOOD == RC) { RC = FS_FindNext(&Search, &File); } // Search only for directories Deeper: RC = FS_FindFirst( "*", ATTR_DIRECTORY, ATTR_NONE, &Search, &File); if (CE_GOOD == RC) { RC = FS_chDir(&File); if (CE_GOOD == RC) goto Deeper; } Higher: RC = FS_GetCWD(&File); RC = CE_GOOD; RC = FS_chDirUp(); if (CE_GOOD == RC) goto Higher; //******************************************************************* //------------------------------ // Create (Open) file for Writing and Reading //------------------------------ RC = FS_CreateFile(pFN, FS_CREATE_NEW, &File); while ( RC != CE_GOOD ); //------------------------------ // Write sample string to file //------------------------------ RC = FS_Write (&File, pWData, 26 ); while (CE_GOOD != RC); //------------------------------ // Check position in the file //------------------------------ i = FS_GetPosition (&File); while (i != 26); //------------------------------ // Seek to start //------------------------------ FS_SeekStart(&File); //------------------------------ // Reed from start //------------------------------ RC = FS_Read (&File, RData, 26, &ReadCnt); while (CE_GOOD != RC); //------------------------------ // Close the file - save changes //------------------------------ RC = FS_Flush(&File); while ( RC != CE_GOOD ); //------------------------------ //------------------------------ // Open file for Reading //------------------------------ RC = FS_CreateFile(pFN, FS_READ_ONLY, &File); while ( RC != CE_GOOD ); //------------------------------ // Reed from start //------------------------------ RC = FS_Read (&File, CData, 26, &ReadCnt); while (CE_GOOD != RC); //------------------------------ // Close the file - save changes //------------------------------ RC = FS_Flush(&File); while ( RC != CE_GOOD ); //------------------------------ i = 1 + i; //******************************************************************* BLISignalOFF(); //------------------------------ while(1); return 0; }
/** * @brief Receives an application from a sender and stores it in the fs. * @retval TRANSFER_OK if the receive and application store succeed. * -TRANSFER_CMD_FAIL if the receive or store fail. */ TRANSFER_STATUS Cmd_RCV_APP(void) { uint8_t checksum, name_len, id, numPages, *page; uint16_t i, j; char name[FS_FILE_MAX_NAME_LEN]; uint32_t size, remaining, read_amt; /*** Get # of characters in filename ***/ checksum = 0; name_len = USB_Receive(); checksum = USB_Receive(); if((checksum+name_len) != 0xFF || name_len > FS_FILE_MAX_NAME_LEN-1) { USB_Send(TRANSFER_NAK); return TRANSFER_CMD_FAIL; } USB_Send(TRANSFER_ACK); /*** Get filename ***/ checksum = 0; for(i = 0; i < name_len; i++) { name[i] = USB_Receive(); checksum += name[i]; } name[i] = '\0'; checksum += USB_Receive(); if(checksum != 0xFF) { USB_Send(TRANSFER_NAK); return TRANSFER_CMD_FAIL; } USB_Send(TRANSFER_ACK); /*** Get file size in pages ***/ checksum = 0; size = USB_Receive() | (USB_Receive() << 8) | (USB_Receive() << 16) | (USB_Receive() << 24); checksum += size & 0xFF; checksum += (size >> 8) & 0xFF; checksum += (size >> 16) & 0xFF; checksum += (size >> 24) & 0xFF; checksum += USB_Receive(); numPages = FS_RoundPageUp(size); id = FS_CreateFile(name, numPages); if (checksum != 0xFF || id == FS_MAX_FILES) { USB_Send(TRANSFER_NAK); return TRANSFER_CMD_FAIL; } USB_Send(TRANSFER_ACK); /*** Receive pages 1 at a time ***/ remaining = size; page = (uint8_t *)malloc(PAGE_SIZE); for(i = 0; i < numPages; i++) { memset(page, 0, PAGE_SIZE); //Zero for funs checksum = 0; if(remaining < PAGE_SIZE) read_amt = remaining; //Don't read too much on the last page else read_amt = PAGE_SIZE; for(j = 0; j < read_amt; j++) { page[j] = USB_Receive(); checksum += page[j]; } checksum += USB_Receive(); FS_WriteFilePage(id, (uint32_t *)page, i); if (checksum != 0xFF || id == FS_MAX_FILES) { USB_Send(TRANSFER_NAK); FS_DeleteFile(id); free(page); return TRANSFER_CMD_FAIL; } USB_Send(TRANSFER_ACK); remaining -= PAGE_SIZE; } free(page); return TRANSFER_OK; }