/********************************************************************* * Function: WORD MPFSPutArray(MPFS_HANDLE hMPFS, BYTE *data, WORD len) * * PreCondition: MPFSFormat() must have been called * * Input: hMPFS: the MPFS handle for writing * data: the data array to write * len: how many bytes to write * * Output: number of bytes successfully written * * Side Effects: None * * Overview: None * * Note: Actual write may not get started until internal * write page is full. To ensure that previously * data gets written, caller must call MPFSPutEnd() * after last call to MPFSPutArray(). ********************************************************************/ WORD MPFSPutArray(MPFS_HANDLE hMPFS, BYTE *data, WORD len) { #if defined(MPFS_USE_EEPROM) WORD count; for(count = 0; count < len; count++) { XEEWrite(data[count]); MPFSStubs[hMPFS].addr++; MPFSStubs[hMPFS].bytesRem--; if(MPFSStubs[hMPFS].bytesRem == 0) { MPFSPutEnd(); isMPFSLocked = TRUE; XEEBeginWrite(MPFSStubs[hMPFS].addr+MPFS_HEAD); MPFSStubs[hMPFS].bytesRem = MPFS_WRITE_PAGE_SIZE; } } return count; #else return 0; #endif }
WORD MPFSPutArray(MPFS_HANDLE hMPFS, BYTE* cData, WORD wLen) { #if defined(MPFS_USE_EEPROM) // Write to the EEPROM WORD count; for(count = 0; count < wLen; count++) { XEEWrite(cData[count]); MPFSStubs[hMPFS].addr++; MPFSStubs[hMPFS].bytesRem--; if(MPFSStubs[hMPFS].bytesRem == 0u) { MPFSPutEnd(FALSE); isMPFSLocked = TRUE; XEEBeginWrite(MPFSStubs[hMPFS].addr+MPFS_HEAD); MPFSStubs[hMPFS].bytesRem = MPFS_WRITE_PAGE_SIZE; } } return count; #else // Write to the SPI Flash SPIFlashWriteArray(cData, wLen); return wLen; #endif }
uint16_t MPFSPutArray(MPFS_HANDLE hMPFS, uint8_t* cData, uint16_t wLen) { #if defined(MPFS_USE_EEPROM) // Write to the EEPROM uint16_t count; for(count = 0; count < wLen; count++) { XEEWrite(cData[count]); MPFSStubs[hMPFS].addr++; MPFSStubs[hMPFS].bytesRem--; if(MPFSStubs[hMPFS].bytesRem == 0u) { MPFSPutEnd(false); isMPFSLocked = true; XEEBeginWrite(MPFSStubs[hMPFS].addr+MPFS_HEAD); MPFSStubs[hMPFS].bytesRem = MPFS_WRITE_PAGE_SIZE; } } return count; #else // Write to the SPI Flash SPIFlashWriteArray(cData, wLen); return wLen; #endif }
/********************************************************************* * Function: BOOL MPFSPut(BYTE b) * * PreCondition: MPFSFormat() or MPFSCreate() must be called * MPFSPutBegin() is already called. * * Input: b - data to write. * * Output: TRUE if successfull * !TRUE if failed. * * Side Effects: Original MPFS handle is no longer valid. * Updated MPFS handle must be obtained by calling * MPFSPutEnd(). * * Overview: None * * Note: Actual write may not get started until internal * write page is full. To ensure that previously * data gets written, caller must call MPFSPutEnd() * after last call to MPFSPut(). ********************************************************************/ BOOL MPFSPut(BYTE b) { #if defined(MPFS_USE_EEPROM) if ( XEEWrite(b) ) return FALSE; _currentCount++; _currentHandle++; if ( _currentCount >= MPFS_WRITE_PAGE_SIZE ) { MPFSPutEnd(); XEEBeginWrite(_currentHandle); } #endif return TRUE; }
WORD MPFSPutArray(MPFS_HANDLE hMPFS, BYTE* cData, WORD wLen) { #if defined(MPFS_USE_EEPROM) // Write to the EEPROM WORD count; for(count = 0; count < wLen; count++) { XEEWrite(cData[count]); MPFSStubs[hMPFS].addr++; MPFSStubs[hMPFS].bytesRem--; if(MPFSStubs[hMPFS].bytesRem == 0u) { MPFSPutEnd(FALSE); isMPFSLocked = TRUE; XEEBeginWrite(MPFSStubs[hMPFS].addr+MPFS_HEAD); MPFSStubs[hMPFS].bytesRem = MPFS_WRITE_PAGE_SIZE; } } return count; #else // Write to the SPI Flash #if (GRAPHICS_PICTAIL_VERSION == 3) SST25WriteIncrementalArray(cData, wLen); #else xSemaphoreTake(QVGASemaphore, portMAX_DELAY); SST39PMPInit(); SST39WriteIncrementalArray(cData, wLen); LCDPMPInit(); xSemaphoreGive(QVGASemaphore); #endif #endif }
////////////////////////////////////////////////////////////////////////////////////////// // NOTE: The following XMODEM code has been upgarded to MPFS2 from MPFS Classic. // Upgrading to HTTP2 and MPFS2 is *strongly* recommended for all new designs. // MPFS2 images can be uploaded directly using the MPFS2.exe tool. ////////////////////////////////////////////////////////////////////////////////////////// static BOOL DownloadMPFS(void) { enum SM_MPFS { SM_MPFS_SOH, SM_MPFS_BLOCK, SM_MPFS_BLOCK_CMP, SM_MPFS_DATA, } state; BYTE c; MPFS_HANDLE handle; BOOL lbDone; BYTE blockLen=0; BYTE lResult; BYTE tempData[XMODEM_BLOCK_LEN]; DWORD lastTick; DWORD currentTick; state = SM_MPFS_SOH; lbDone = FALSE; handle = MPFSFormat(); // Notify the host that we are ready to receive... lastTick = TickGet(); do { currentTick = TickGet(); if ( currentTick - lastTick >= (TICK_SECOND/2) ) { lastTick = TickGet(); while(BusyUART()); WriteUART(XMODEM_NAK); /* * Blink LED to indicate that we are waiting for * host to send the file. */ LED6_IO ^= 1; } } while(!DataRdyUART()); while(!lbDone) { if(DataRdyUART()) { // Toggle LED as we receive the data from host. LED6_IO ^= 1; c = ReadUART(); } else { // Real application should put some timeout to make sure // that we do not wait forever. continue; } switch(state) { default: if ( c == XMODEM_SOH ) { state = SM_MPFS_BLOCK; } else if ( c == XMODEM_EOT ) { // Turn off LED when we are done. LED6_IO = 1; MPFSClose(handle); while(BusyUART()); WriteUART(XMODEM_ACK); lbDone = TRUE; } else { while(BusyUART()); WriteUART(XMODEM_NAK); } break; case SM_MPFS_BLOCK: // We do not use block information. lResult = XMODEM_ACK; blockLen = 0; state = SM_MPFS_BLOCK_CMP; break; case SM_MPFS_BLOCK_CMP: // We do not use 1's comp. block value. state = SM_MPFS_DATA; break; case SM_MPFS_DATA: // Buffer block data until it is over. tempData[blockLen++] = c; if ( blockLen > XMODEM_BLOCK_LEN ) { lResult = XMODEM_ACK; for ( c = 0; c < XMODEM_BLOCK_LEN; c++ ) MPFSPutArray(handle,&tempData[c],1); MPFSPutEnd(handle); while(BusyUART()); WriteUART(lResult); state = SM_MPFS_SOH; } break; } } return TRUE; }
static BOOL DownloadMPFS(void) { enum SM_MPFS { SM_MPFS_SOH, SM_MPFS_BLOCK, SM_MPFS_BLOCK_CMP, SM_MPFS_DATA, } state; BYTE c; MPFS handle; BOOL lbDone; BYTE blockLen; BYTE lResult; BYTE tempData[XMODEM_BLOCK_LEN]; TICK lastTick; TICK currentTick; state = SM_MPFS_SOH; lbDone = FALSE; handle = MPFSFormat(); /* * Notify the host that we are ready to receive... */ lastTick = TickGet(); do { currentTick = TickGet(); if ( TickGetDiff(currentTick, lastTick) >= (TICK_SECOND/2) ) { lastTick = TickGet(); serPutByte(XMODEM_NAK); /* * Blink LED to indicate that we are waiting for * host to send the file. */ //LATA2 ^= 1; } } while( !serIsGetReady() ); while(!lbDone) { if ( serIsGetReady() ) { /* * Toggle LED as we receive the data from host. */ //LATA2 ^= 1; c = serGetByte(); } else { /* * Real application should put some timeout to make sure * that we do not wait forever. */ continue; } switch(state) { default: if ( c == XMODEM_SOH ) { state = SM_MPFS_BLOCK; } else if ( c == XMODEM_EOT ) { /* * Turn off LED when we are done. */ //LATA2 = 1; MPFSClose(); serPutByte(XMODEM_ACK); lbDone = TRUE; } else serPutByte(XMODEM_NAK); break; case SM_MPFS_BLOCK: /* * We do not use block information. */ lResult = XMODEM_ACK; blockLen = 0; state = SM_MPFS_BLOCK_CMP; break; case SM_MPFS_BLOCK_CMP: /* * We do not use 1's comp. block value. */ state = SM_MPFS_DATA; break; case SM_MPFS_DATA: /* * Buffer block data until it is over. */ tempData[blockLen++] = c; if ( blockLen > XMODEM_BLOCK_LEN ) { /* * We have one block data. * Write it to EEPROM. */ MPFSPutBegin(handle); lResult = XMODEM_ACK; for ( c = 0; c < XMODEM_BLOCK_LEN; c++ ) MPFSPut(tempData[c]); handle = MPFSPutEnd(); serPutByte(lResult); state = SM_MPFS_SOH; } break; } } /* * This small wait is required if SLIP is in use. * If this is not used, PC might misinterpret SLIP * module communication and never close file transfer * dialog box. */ #if defined(STACK_USE_SLIP) { BYTE i; i = 255; while( i-- ); } #endif return TRUE; }
static BOOL PutFile(void) { BYTE v; switch(smFTPCommand) { case SM_FTP_CMD_IDLE: if ( !FTPFlags.Bits.bLoggedIn ) { FTPResponse = FTP_RESP_LOGIN; return TRUE; } else { FTPResponse = FTP_RESP_DATA_OPEN; FTPDataSocket = TCPConnect(&REMOTE_HOST(FTPSocket), FTPDataPort.Val); // Make sure that a valid socket was available and returned // If not, return with an error if(FTPDataSocket != INVALID_SOCKET) { smFTPCommand = SM_FTP_CMD_WAIT; } else { FTPResponse = FTP_RESP_DATA_NO_SOCKET; return TRUE; } } break; case SM_FTP_CMD_WAIT: if ( TCPIsConnected(FTPDataSocket) ) { #if defined(FTP_PUT_ENABLED) if ( !MPFSIsInUse() ) #endif { #if defined(FTP_PUT_ENABLED) FTPFileHandle = MPFSFormat(); #endif smFTPCommand = SM_FTP_CMD_RECEIVE; } } break; case SM_FTP_CMD_RECEIVE: if ( TCPIsGetReady(FTPDataSocket) ) { // Reload timeout timer. lastActivity = TickGet(); MPFSPutBegin(FTPFileHandle); while( TCPGet(FTPDataSocket, &v) ) { USARTPut(v); #if defined(FTP_PUT_ENABLED) MPFSPut(v); #endif } FTPFileHandle = MPFSPutEnd(); TCPDiscard(FTPDataSocket); // Print hash characters on FTP client display if(TCPIsPutReady(FTPSocket)) { TCPPut(FTPSocket, '#'); TCPFlush(FTPSocket); } } else if ( !TCPIsConnected(FTPDataSocket) ) { #if defined(FTP_PUT_ENABLED) MPFSPutEnd(); MPFSClose(); #endif TCPDisconnect(FTPDataSocket); FTPDataSocket = INVALID_SOCKET; FTPResponse = FTP_RESP_DATA_CLOSE; return TRUE; } } return FALSE; }
static BOOL PutFile(void) { BYTE v; switch(smFTPCommand) { case SM_FTP_CMD_IDLE: if ( !FTPFlags.Bits.bLoggedIn ) { FTPResponse = FTP_RESP_LOGIN; return TRUE; } else { FTPResponse = FTP_RESP_DATA_OPEN; FTPDataSocket = TCPOpen((PTR_BASE)&TCPGetRemoteInfo(FTPSocket)->remote, TCP_OPEN_NODE_INFO, FTPDataPort.Val, TCP_PURPOSE_FTP_DATA); // Make sure that a valid socket was available and returned // If not, return with an error if(FTPDataSocket == INVALID_SOCKET) { FTPResponse = FTP_RESP_DATA_NO_SOCKET; return TRUE; } smFTPCommand = SM_FTP_CMD_WAIT; } break; case SM_FTP_CMD_WAIT: if ( TCPIsConnected(FTPDataSocket) ) { #if defined(FTP_PUT_ENABLED) FTPFileHandle = MPFSFormat(); #endif smFTPCommand = SM_FTP_CMD_RECEIVE; } break; case SM_FTP_CMD_RECEIVE: if ( TCPIsGetReady(FTPDataSocket) ) { // Reload timeout timer. lastActivity = TickGet(); MPFSPutBegin(FTPFileHandle); while( TCPGet(FTPDataSocket, &v) ) { #if defined(FTP_PUT_ENABLED) MPFSPut(v); #endif } FTPFileHandle = MPFSPutEnd(); } else if ( !TCPIsConnected(FTPDataSocket) ) { #if defined(FTP_PUT_ENABLED) MPFSClose(); #endif TCPDisconnect(FTPDataSocket); FTPDataSocket = INVALID_SOCKET; FTPResponse = FTP_RESP_DATA_CLOSE; return TRUE; } } return FALSE; }
static HTTP_IO_RESULT HTTPMPFSUpload(void) { BYTE c[16]; WORD lenA, lenB; switch(curHTTP.httpStatus) { // New upload, so look for the CRLFCRLF case HTTP_MPFS_UP: lenA = TCPFindROMArray(sktHTTP, (ROM BYTE*)"\r\n\r\n", 4, 0, FALSE); if(lenA != 0xffff) {// Found it, so remove all data up to and including lenA = TCPGetArray(sktHTTP, NULL, lenA); curHTTP.byteCount -= lenA; // Make sure first 6 bytes are also in if(TCPIsGetReady(sktHTTP) < (4 + 6) ) { lenA++; return HTTP_IO_NEED_DATA; } // Make sure it's an MPFS of the correct version lenA = TCPGetArray(sktHTTP, c, 10); curHTTP.byteCount -= lenA; if(memcmppgm2ram(c, (ROM void*)"\r\n\r\nMPFS\x02\x00", 10) == 0) {// Read as Ver 2.0 curHTTP.httpStatus = HTTP_MPFS_OK; // Format MPFS storage and put 6 byte tag curHTTP.file = MPFSFormat(); MPFSPutArray(curHTTP.file, &c[4], 6); } else {// Version is wrong curHTTP.httpStatus = HTTP_MPFS_ERROR; } } else {// Otherwise, remove as much as possible lenA = TCPGetArray(sktHTTP, NULL, TCPIsGetReady(sktHTTP) - 4); curHTTP.byteCount -= lenA; } break; // Received file is invalid case HTTP_MPFS_ERROR: curHTTP.byteCount -= TCPIsGetReady(sktHTTP); TCPDiscard(sktHTTP); if(curHTTP.byteCount < 100 || curHTTP.byteCount > 0x80000000) {// If almost all data was read, or if we overflowed, then return smHTTP = SM_HTTP_SERVE_HEADERS; return HTTP_IO_DONE; } break; // File is verified, so write the data case HTTP_MPFS_OK: // Determine how much to read lenA = TCPIsGetReady(sktHTTP); if(lenA > curHTTP.byteCount) lenA = curHTTP.byteCount; while(lenA > 0) { lenB = TCPGetArray(sktHTTP, c, mMIN(lenA,16)); curHTTP.byteCount -= lenB; lenA -= lenB; MPFSPutArray(curHTTP.file, c, lenB); } // If we've read all the data if(curHTTP.byteCount == 0) { MPFSPutEnd(); smHTTP = SM_HTTP_SERVE_HEADERS; return HTTP_IO_DONE; } } // Ask for more data return HTTP_IO_NEED_DATA; }