int platform_uart_recv( unsigned id, unsigned timer_id, int timeout ) { AT91S_USART* base = id == 0 ? AT91C_BASE_US0 : AT91C_BASE_US1; timer_data_type tmr_start, tmr_crt; int res; if( timeout == 0 ) { // Return data only if already available if( USART_IsDataAvailable( base ) ) return USART_Read( base, 0 ); else return -1; } else if( timeout == PLATFORM_UART_INFINITE_TIMEOUT ) { // Wait for data return USART_Read( base, 0 ); } else { // Receive char with the specified timeout tmr_start = platform_timer_op( timer_id, PLATFORM_TIMER_OP_START,0 ); while( 1 ) { if( USART_IsDataAvailable( base ) ) { res = USART_Read( base, 0 ); break; } else res = -1; tmr_crt = platform_timer_op( timer_id, PLATFORM_TIMER_OP_READ, 0 ); if( platform_timer_get_diff_us( timer_id, tmr_crt, tmr_start ) >= timeout ) break; } return res; } }
int platform_s_uart_recv( unsigned id, s32 timeout ) { AT91S_USART* base = id == 0 ? AT91C_BASE_US0 : AT91C_BASE_US1; if( timeout == 0 ) { // Return data only if already available if( USART_IsDataAvailable( base ) ) return USART_Read( base, 0 ); else return -1; } return USART_Read( base, 0 ); }
/* Usart receive task */ void vUsartRx_Task( void ) { unsigned char pucFileName[30]; // File name unsigned char ucReceived; // Received char short i; short sFlag = 0; // State flag FIL Fil; // File object FRESULT rc; // Result code DIR dir; // Directory object FILINFO fno; // File information object UINT bw, br; // File counters // Usart receive loop for (;;) { // Data is available if (USART_IsDataAvailable(AT91C_BASE_US0)) { USART_ReadBuffer(AT91C_BASE_US0, &ucReceived, 1); // Stage 0: receive file name if (sFlag == 0 && ucReceived == '\x01') { i = 0; memset(pucFileName, 0, 30); sFlag = 1; //debug_printf("\r\n--"); // Stage 1: receiving file name } else if (sFlag == 1 && ucReceived != '\x02') { //debug_printf("%c", ucReceived); pucFileName[i] = ucReceived; i++; // Stage 1: end of file name } else if (sFlag == 1 && ucReceived == '\x02') { //debug_printf("\r\nRec file: %s\r\n", pucFileName); sFlag = 2; // Create the file rc = f_open(&Fil, pucFileName, FA_CREATE_ALWAYS | FA_WRITE); if (rc) { sprintf(ucRemoteBuffer, "File creation failure.\r\n"); remote_printf(ucRemoteBuffer); sFlag = 0; } // Stage 2: receive file } else if (sFlag == 2 && ucReceived != '\x03') { //debug_printf("%c", ucReceived); f_write(&Fil, &ucReceived, 1, &bw); // Stage 2: end of file } else if (sFlag == 2 && ucReceived == '\x03') { sFlag = 3; // Stage 3: end of transmission } else if (sFlag == 3 && ucReceived == '\x04') { f_close(&Fil); sprintf(ucRemoteBuffer, "\r\nReceived file: %s\r\n", pucFileName); remote_printf(ucRemoteBuffer); sFlag = 0; } } else { vTaskDelay(5); } } }