void WriteStringPC(const char *string){ while (*string != '\0'){ // If next char is not the end of the string. while(TxNotRdyUART()); // Wait for Debug UART Tx buffer to accept more data. WriteUART(*string); // Write a single char data. string++; // Prepare for next data while(BusyUART()); // Wait for the transmission to finish. } }
void putrsUART(const rom char *data) { do { // Transmit a byte while(BusyUART()); WriteUART(*data); } while( *data++ ); }
void Usart_Send ( unsigned char *MSG_SAUD ) { int i = 0; while (MSG_SAUD[i]) // envia mensagem de saudação { WriteUART( MSG_SAUD [i] ); i++; } }//
// Writes an IP address to the LCD display and the UART as available void DisplayIPValue(IP_ADDR IPVal) { // printf("%u.%u.%u.%u", IPVal.v[0], IPVal.v[1], IPVal.v[2], IPVal.v[3]); #if defined (__dsPIC33E__) || defined (__PIC24E__) static BYTE IPDigit[4]; /* Needs to be declared as static to avoid the array getting optimized by C30 v3.30 compiler for dsPIC33E/PIC24E. Otherwise the LCD displays corrupted IP address on Explorer 16. To be fixed in the future compiler release*/ #else BYTE IPDigit[4]; #endif BYTE i; #ifdef USE_LCD BYTE j; BYTE LCDPos=16; #endif for(i = 0; i < sizeof(IP_ADDR); i++) { uitoa((WORD)IPVal.v[i], IPDigit); #if defined(STACK_USE_UART) putsUART((char *) IPDigit); #endif #ifdef USE_LCD for(j = 0; j < strlen((char*)IPDigit); j++) { LCDText[LCDPos++] = IPDigit[j]; } if(i == sizeof(IP_ADDR)-1) break; LCDText[LCDPos++] = '.'; #else if(i == sizeof(IP_ADDR)-1) break; #endif #if defined(STACK_USE_UART) while(BusyUART()); WriteUART('.'); #endif } #ifdef USE_LCD if(LCDPos < 32u) LCDText[LCDPos] = 0; LCDUpdate(); #endif }
// Writes an IP address to the LCD display and the UART as available void DisplayIPValue(IP_ADDR IPVal) { // printf("%u.%u.%u.%u", IPVal.v[0], IPVal.v[1], IPVal.v[2], IPVal.v[3]); BYTE IPDigit[4]; BYTE i; #ifdef USE_LCD BYTE j; BYTE LCDPos=16; #endif for(i = 0; i < sizeof(IP_ADDR); i++) { uitoa((WORD)IPVal.v[i], IPDigit); #if defined(STACK_USE_UART) putsUART((char *) IPDigit); #endif #ifdef USE_LCD for(j = 0; j < strlen((char*)IPDigit); j++) { LCDText[LCDPos++] = IPDigit[j]; } if(i == sizeof(IP_ADDR)-1) break; LCDText[LCDPos++] = '.'; #else if(i == sizeof(IP_ADDR)-1) break; #endif #if defined(STACK_USE_UART) while(BusyUART()); WriteUART('.'); #endif } #ifdef USE_LCD if(LCDPos < 32u) LCDText[LCDPos] = 0; LCDUpdate(); #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; }
DRESULT disk_readp ( BYTE* rd, /* Pointer to the destination object */ DWORD sector, /* Sector number (LBA) */ UINT offset, /* Offset in the sector */ UINT count /* Byte count (bit15:destination) */ ) { DRESULT res; unsigned char ptr=0X00,buff; unsigned long int start_add; static unsigned char arr[512]; int length,i; /*Read Sector*/ if(offset == 0){ start_add = ((sector*512)); Delay_s(1); dummy_clocks(8); Command(0X52,start_add,0X00); //CMD18 proceed(); do{ buff = response(); }while(buff!=0x00); Delay_s(1); proceed(); do{ buff = response(); }while(buff!=0xFE); length = 0; while ( length < count ) { arr[length] = response(); length++; } Delay_s(1); dummy_clocks(8); Command(0X4C,0X00000000,0X00); //CMD12 proceed(); do{ buff = response(); }while(buff!=0xFF); length = 0; while(arr[length]!='\0') { //WriteUART(arr[length]); length++; } *rd = length; return RES_OK; } else { start_add = (sector*512); length = 0; while(length<512) //CLEAR ARRAY { arr[length] = 0; length++; } Delay_s(1); dummy_clocks(8); //SPI_cmd(sd_cmd17); Command(0X51,start_add,0X00); proceed(); do{ buff = response(); }while(buff!=0x00); do{ buff = response(); }while(buff!=0xFE); length = 0; while ( length < 512 ) { while(offset--) //RUN DOWN TILL THE OFFSET VALUE { arr[length] = response(); length++; } while(count--) //RUN DOWN TILL THE COUNT VALUE { *rd = response(); arr[length] = *rd; rd++; length++; } while(length<512) //FOR THE TRAILING BYTES { arr[length] = response(); length++; } } Delay_s(1); // dummy_clocks(8); //SPI_cmd(sd_cmd12); Command(0X4C,0X00000000,0X00); //COMMAND12 MANDATORY proceed(); do{ buff = response(); }while(buff!=0xFF); length = 0; if(flag == 1){ while(arr[length]!='\0') { WriteUART(arr[length]); length++; } } else putsUART(".."); Delay_s(2); return RES_OK; } }
bool AutoUpdate_UartXMODEM_24G(void) { enum SM_FIRMWARE_UPDATE { SM_FIRMWARE_UPDATE_SOH, SM_FIRMWARE_UPDATE_BLOCK, SM_FIRMWARE_UPDATE_BLOCK_CMP, SM_FIRMWARE_UPDATE_DATA, SM_FIRMWARE_UPDATE_CHECKSUM, SM_FIRMWARE_UPDATE_FINISH, } state; uint8_t c; // MPFS_HANDLE handle; bool lbDone; uint8_t blockLen=0; bool lResult = false; uint8_t BlockNumber=0, preBlockNum=0; uint8_t checksum=0; uint32_t lastTick; uint32_t currentTick; state = SM_FIRMWARE_UPDATE_SOH; lbDone = false; #if 0 //use button to begin update if( BUTTON3_IO == 1u) return false; putsUART("\n\rPress S2 (on Explorer16) to start the update.\n\r"); while(BUTTON2_IO == 1u); #else //use console command to begin update if(false == wf_update_begin_uart) return false; wf_update_begin_uart = false; #endif MACInit(); DelayMs(100); AutoUpdate_Initialize(); putsUART("Please initiate file transfer of firmware patch by XMODEM.\r\n"); putsUART("If S3 pressed (on Explorer16), update will stop and previous image will be restored.\r\n"); lastTick = TickGet(); do { currentTick = TickGet(); if ( currentTick - lastTick >= (TICK_SECOND*2) ) { lastTick = TickGet(); while(BusyUART()); WriteUART(XMODEM_NAK); } } while(!DataRdyUART()); while(!lbDone) { if (BUTTON3_IO == 0u) // If you want to cancel AutoUpdate, please press S3 { putsUART("You press S3 button, revert begin...\r\n"); AutoUpdate_Restore(); putsUART("revert done\r\n"); return false; } if(DataRdyUART()) { c = ReadUART(); lastTick = TickGet(); } else { // put some timeout to make sure that we do not wait forever. currentTick = TickGet(); if ( currentTick - lastTick >= (TICK_SECOND*10) ) { //if time out, copy old patch image from bank2 to bank1 putsUART("timeout, revert begin...\r\n"); AutoUpdate_Restore(); putsUART("revert done\r\n"); return false; } continue; } //dbgPrintf("(%02x) ",c); switch(state) { case SM_FIRMWARE_UPDATE_SOH: if(c == XMODEM_SOH) { state = SM_FIRMWARE_UPDATE_BLOCK; dbgPrintf("\r\n! "); checksum = c; lResult = true; } else if ( c == XMODEM_EOT ) { state = SM_FIRMWARE_UPDATE_FINISH; while(BusyUART()); WriteUART(XMODEM_ACK); lbDone = true; } else { dbgPrintf("\n!error\n"); while(1); } break; case SM_FIRMWARE_UPDATE_BLOCK: BlockNumber = c; dbgPrintf("BLK=%d ",BlockNumber); checksum += c; state = SM_FIRMWARE_UPDATE_BLOCK_CMP; break; case SM_FIRMWARE_UPDATE_BLOCK_CMP: dbgPrintf("%d ",c); dbgPrintf("@:"); //Judge: Is it correct ? if(c != (BlockNumber ^ 0xFF)) { lResult = false; dbgPrintf("\nBLOCK_CMP err: %x,%x\n", c, BlockNumber ^ 0xFF ); } else { if((uint8_t)(preBlockNum+1) != BlockNumber) { lResult = false; dbgPrintf("\nBLOCK err %x %x\n",preBlockNum+1,BlockNumber); } } checksum += c; blockLen = 0; state = SM_FIRMWARE_UPDATE_DATA; break; case SM_FIRMWARE_UPDATE_DATA: // Buffer block data until it is over. tempData[blockLen++] = c; if ( blockLen == XMODEM_BLOCK_LEN ) { state = SM_FIRMWARE_UPDATE_CHECKSUM; } checksum += c; break; case SM_FIRMWARE_UPDATE_CHECKSUM: dbgPrintf("Checksum=%x=%x ",checksum,c); if(checksum != c) { lResult = false; dbgPrintf("\nchecksum err\n"); } XMODEM_SendToModule(tempData); while(BusyUART()); if(lResult == true) { WriteUART(XMODEM_ACK); preBlockNum++; } else { WriteUART(XMODEM_NAK); } state = SM_FIRMWARE_UPDATE_SOH; break; default: dbgPrintf("\n!error\n"); while(1); break; } } AutoUpdate_Completed(); return true; }
void timeSync(void) { BYTE i; signed char j; static TICK Timer; static TICK perodicTick = 0; static TICK t = 0; static TCP_SOCKET MySocket = INVALID_SOCKET; static NODE_INFO Server; static int arp_tries = 0; static int tcp_tries = 0; BYTE rcnt=0; BYTE ncnt=0; char foundData=0; if ((tickGet()-t) >= TICK_1S ) { t = tickGet(); timeNow++; } switch(smTS) { case SM_START: #if defined(TIMESYNC_DEBUG) putrsUART("Start!\r\n"); #endif // Set IP adress to connect to. Server.IPAddr.v[0]=193; Server.IPAddr.v[1]=11; Server.IPAddr.v[2]=249; Server.IPAddr.v[3]=54; arp_tries = 0; tcp_tries = 0; smTS = SM_ARP_RESOLVE; break; case SM_ARP_RESOLVE: #if defined(TIMESYNC_DEBUG) putrsUART("Resolve..\r\n"); #endif // If ARP is redy.. if (ARPIsTxReady()) { // Resolve the IP adress.. ARPResolve(&Server.IPAddr); arp_tries++; Timer = tickGet(); smTS = SM_ARP_RESOLVED; } break; case SM_ARP_RESOLVED: #if defined(TIMESYNC_DEBUG) putrsUART("Resolved..\r\n"); #endif // If IP adress is resolved, go to next state if (ARPIsResolved(&Server.IPAddr, &Server.MACAddr)) { smTS = SM_CONNECT; } // If not resolved and spent long time here, // Go back to previous state and re-resolve. else if (tickGet()-Timer > 1*TICK_1S) { smTS = SM_ARP_RESOLVE; } else if (arp_tries>=MAX_ARP_TRIES) { //Abort smTS = SM_ABORT; } break; case SM_CONNECT: #if defined(TIMESYNC_DEBUG) putrsUART("Connect..\r\n"); #endif // We have an sucessfull ARP, connect.. MySocket = TCPConnect(&Server, ServerPort); tcp_tries++; if(MySocket == INVALID_SOCKET) { // Do something. } Timer = tickGet(); smTS = SM_CONNECT_WAIT; break; case SM_CONNECT_WAIT: #if defined(TIMESYNC_DEBUG) putrsUART("Connect wait..\r\n"); #endif // Wait for connection.. if (TCPIsConnected(MySocket)) { smTS = SM_CONNECTED; } // If not connected and spent long time here, // Go back to previous state and re-connect. else if (tickGet()-Timer > 5*TICK_1S) { TCPDisconnect(MySocket); MySocket = INVALID_SOCKET; smTS = SM_CONNECT; } else if (tcp_tries>=MAX_TCP_TRIES) { //Abort TCPDisconnect(MySocket); MySocket = INVALID_SOCKET; smTS = SM_ABORT; } break; case SM_CONNECTED: #if defined(TIMESYNC_DEBUG) putrsUART("Connected..\r\n"); #endif // Send data. Timer = tickGet(); if(TCPIsPutReady(MySocket)) { TCPPut(MySocket, 'G'); TCPPut(MySocket, 'E'); TCPPut(MySocket, 'T'); TCPPut(MySocket, ' '); TCPPut(MySocket, '/'); TCPPut(MySocket, 't'); TCPPut(MySocket, 'i'); TCPPut(MySocket, 'm'); TCPPut(MySocket, 'e'); TCPPut(MySocket, '.'); TCPPut(MySocket, 'p'); TCPPut(MySocket, 'h'); TCPPut(MySocket, 'p'); TCPPut(MySocket, ' '); TCPPut(MySocket, 'H'); TCPPut(MySocket, 'T'); TCPPut(MySocket, 'T'); TCPPut(MySocket, 'P'); TCPPut(MySocket, '/'); TCPPut(MySocket, '1'); TCPPut(MySocket, '.'); TCPPut(MySocket, '0'); TCPPut(MySocket, '\r'); TCPPut(MySocket, '\n'); TCPPut(MySocket, '\r'); TCPPut(MySocket, '\n'); // Send the packet TCPFlush(MySocket); smTS = SM_RECEIVE; } break; case SM_RECEIVE: #if defined(TIMESYNC_DEBUG) putrsUART("Receive..\r\n"); #endif // Client disconnected. if(!TCPIsConnected(MySocket)) { smTS = SM_ABORT; break; } if(TCPIsGetReady(MySocket)) { while(TCPGet(MySocket, &i)) { if (i==CR) rcnt++; else if(i==LF) ncnt++; else { rcnt=0; ncnt=0; } if (foundData==1) { if (j>=0) { timeNow=timeNow+(((DWORD)i)<<(8*j--)); #if defined(TIMESYNC_DEBUG) while(BusyUART()); WriteUART(i); #endif } } if(rcnt>1 && ncnt>1) {j=3; timeNow=0; foundData=1;} } smTS = SM_DISCONNECT; } break; case SM_DISCONNECT: #if defined(TIMESYNC_DEBUG) putrsUART("\r\nDisconnect\r\n"); #endif foundData=0; t = tickGet(); lastSync = timeNow; perodicTick=tickGet(); TCPDisconnect(MySocket); MySocket = INVALID_SOCKET; smTS = SM_DONE; break; case SM_ABORT: #if defined(TIMESYNC_DEBUG) putrsUART("Abort...\r\n"); #endif smTS = SM_START; break; case SM_DONE: if (tickGet()-perodicTick > SYNC_INTERVAL*TICK_1S) { #if defined(TIMESYNC_DEBUG) putrsUART("GO!\r\n"); #endif smTS = SM_START; } break; default: smTS = SM_START; break; } }
/********************************************************************* * Function: void GenericTCPClient(void) * * PreCondition: Stack is initialized() * * Input: None * * Output: None * * Side Effects: None * * Overview: None * * Note: None ********************************************************************/ void GenericTCPClient(void) { BYTE i; BYTE *StringPtr; static TICK Timer; static TCP_SOCKET MySocket = INVALID_SOCKET; static NODE_INFO Server; static enum _GenericTCPExampleState { SM_HOME = 0, SM_NAME_RESOLVE, SM_ARP_START_RESOLVE, SM_ARP_RESOLVE, SM_SOCKET_OBTAIN, SM_SOCKET_OBTAINED, SM_PROCESS_RESPONSE, SM_DISCONNECT, SM_DONE } GenericTCPExampleState = SM_DONE; switch(GenericTCPExampleState) { case SM_HOME: // Obtain ownership of the DNS resolution module if(!DNSBeginUsage()) break; // Obtain the IP address associated with the common ServerName DNSResolve(ServerName, DNS_TYPE_A); GenericTCPExampleState++; break; case SM_NAME_RESOLVE: // Wait for the DNS server to return the requested IP address if(!DNSIsResolved(&Server.IPAddr)) break; // Release the DNS module, we no longer need it if(!DNSEndUsage()) { // An invalid IP address was returned from the DNS // server. Quit and fail permanantly if host is not valid. GenericTCPExampleState = SM_DONE; break; } GenericTCPExampleState++; case SM_ARP_START_RESOLVE: // Obtain the MAC address associated with the server's IP address (either direct MAC address on same subnet, or the MAC address of the Gateway machine) ARPResolve(&Server.IPAddr); Timer = TickGet(); GenericTCPExampleState++; break; case SM_ARP_RESOLVE: // Wait for the MAC address to finish being obtained if(!ARPIsResolved(&Server.IPAddr, &Server.MACAddr)) { // Time out if too much time is spent in this state if(TickGet()-Timer > 1*TICK_SECOND) { // Retransmit ARP request GenericTCPExampleState--; } break; } GenericTCPExampleState++; case SM_SOCKET_OBTAIN: // Connect a socket to the remote TCP server MySocket = TCPConnect(&Server, ServerPort); // Abort operation if no TCP sockets are available // If this ever happens, incrementing MAX_TCP_SOCKETS in // StackTsk.h may help (at the expense of more global memory // resources). if(MySocket == INVALID_SOCKET) break; GenericTCPExampleState++; Timer = TickGet(); break; case SM_SOCKET_OBTAINED: // Wait for the remote server to accept our connection request if(!TCPIsConnected(MySocket)) { // Time out if too much time is spent in this state if(TickGet()-Timer > 5*TICK_SECOND) { // Close the socket so it can be used by other modules TCPDisconnect(MySocket); MySocket = INVALID_SOCKET; GenericTCPExampleState--; } break; } Timer = TickGet(); // Make certain the socket can be written to if(!TCPIsPutReady(MySocket)) break; // Place the application protocol data into the transmit buffer. For this example, we are connected to an HTTP server, so we'll send an HTTP GET request. TCPPutROMString(MySocket, (ROM BYTE*)"GET "); TCPPutROMString(MySocket, RemoteURL); TCPPutROMString(MySocket, (ROM BYTE*)" HTTP/1.1\r\nHost: "); TCPPutString(MySocket, ServerName); TCPPutROMString(MySocket, (ROM BYTE*)"\r\n\r\n"); // Send the packet TCPFlush(MySocket); GenericTCPExampleState++; case SM_PROCESS_RESPONSE: // Check to see if the remote node has disconnected from us or sent us any application data // If application data is available, write it to the UART if(!TCPIsConnected(MySocket)) { GenericTCPExampleState++; } if(!TCPIsGetReady(MySocket)) break; // Obtain the server reply while(TCPGet(MySocket, &i)) { while(BusyUART()); WriteUART(i); } break; case SM_DISCONNECT: // Close the socket so it can be used by other modules // For this application, we wish to stay connected, but this state will still get entered if the remote server decides to disconnect TCPDisconnect(MySocket); MySocket = INVALID_SOCKET; GenericTCPExampleState = SM_DONE; break; case SM_DONE: // Do nothing unless the user pushes BUTTON1 and wants to restart the whole connection/download process if(BUTTON1_IO == 0u) GenericTCPExampleState = SM_HOME; break; } }