/******************************************************************************* * FUNCTION: vUART2FlushRxBuffer * * PARAMETERS: * ~ void * * RETURN: * ~ void * * DESCRIPTIONS: * Flush all the data in the Rx buffer. * *******************************************************************************/ void vUART2FlushRxBuffer(void) { unsigned char ucReceivedData; unsigned int iStatus = INTDisableInterrupts(); prv_xRx.uiDataCount = 0; prv_xRx.uiReadPt = 0; prv_xRx.uiWritePt = 0; // Discard all data available in the UART receive buffer. while (UARTReceivedDataIsAvailable(UART2)) { // Read the received data. ucReceivedData = UARTGetDataByte(UART2); } // Clear the overrun flag. if (UARTGetLineStatus(UART2) & UART_OVERRUN_ERROR) { U2STAbits.OERR = 0; } // Clear the semaphore. xSemaphoreTake(xBluetoothRxSemaphore, 0); INTRestoreInterrupts(iStatus); }
void ReadUART3(char *message, int max) { char data; int complete = 0, num_bytes = 0; // loop until you get a '\r' or '\n' while (!complete) { if (UARTReceivedDataIsAvailable(UART3)) { data = UARTGetDataByte(UART3); if ((data == '\n') || (data == '\r')) { complete = 1; } else { message[num_bytes] = data; num_bytes++; // roll over if the array is too small if (num_bytes >= max) { num_bytes = 0; } } } } // clear the remaining elements in the array int i; for (i = num_bytes; i < max; i++) { message[i] = '\0'; } }
// ***************************************************************************** // UINT32 GetDataBuffer(char *buffer, UINT32 max_size) // ***************************************************************************** UINT32 GetDataBuffer(char *buffer, UINT32 max_size) { UINT32 num_char; num_char = 0; while(num_char < max_size) { UINT8 character; while(!UARTReceivedDataIsAvailable(UART2)) ; character = UARTGetDataByte(UART2); if(character == '\r') break; *buffer = character; buffer++; num_char++; } return num_char; }
// read byte if one is ready. // if exists, return true and byte // if return false, byte = 0 is none avail, else // byte != 0 means error bool UARTReadByte(uint8_t * byte) { if (UARTReceivedDataIsAvailable(UART1)) { *byte = UARTGetDataByte(UART1); return true; } *byte = 0; return false; } // UARTReadByte
// ***************************************************************************** // UINT32 GetMenuChoice(void) // ***************************************************************************** UINT32 GetMenuChoice(void) { UINT8 menu_item; while(!UARTReceivedDataIsAvailable(UARTTEST)); menu_item = UARTGetDataByte(UARTTEST); return (UINT32)menu_item; }
// ***************************************************************************** // Wait for a new character to arrive on the Console serial port // ***************************************************************************** char getConsole(void) { char character; while(!UARTReceivedDataIsAvailable(UART_CONSOLE)); character = UARTGetDataByte(UART_CONSOLE); return character; // read the character from the receive buffer }
int rx(void *dst, size_t n) { size_t ui; for (ui = 0; ui < n; ++ui) { if (!UARTReceivedDataIsAvailable(module)) { return -1; } WDT::clear(); ((uint8_t *)dst)[ui] = UARTGetDataByte(module); } return 0; }
void serial_handling(void){ static int step = 0; static uint8_t msg[MESS_SIZE]; if(UARTReceivedDataIsAvailable(UART2)) { PutCharInFifo ( &descrFifoRX, UARTGetDataByte(UART2)); } if(GetReadSize(&descrFifoRX) < 1){ // Il n'y a rien à faire return ; } GetCharFromFifo(&descrFifoRX, &msg[step]); switch(step){ case 0: // Recherche du caractère '!' if(msg[0] == '!'){ step++; } break; case 1: case 2: step++; break; case 3: // Message complet msg_processing(msg); step = 0; default: step = 0; break; } if (GetReadSize(&descrFifoTX) > 0) { // Autorise int émission INTEnable(INT_U2TX, INT_ENABLED); } }
void handleUartInterrupt(UART_MODULE uart, Buffer* buffer) { // Is this an RX interrupt? if (INTGetFlag(INT_SOURCE_UART_RX(uart))) { if (UARTReceivedDataIsAvailable(uart)) { unsigned char c = UARTGetDataByte(uart); // BUG2018, BUG2019 (255 / 254 value) when Motor Power is too strong if (c != 'ÿ' && c != 'þ') { bufferWriteChar(buffer, c); } // Clear the RX interrupt Flag INTClearFlag(INT_SOURCE_UART_RX(uart)); } } // We don't care about TX interrupt if ( INTGetFlag(INT_SOURCE_UART_TX(uart)) ) { INTClearFlag(INT_SOURCE_UART_TX(uart)); } }
/******************************************************************************* * ISR: UART 2 Interrupt. * * DESCRIPTIONS: * Interrupt vector for UART 2. * *******************************************************************************/ void __ISR(_UART_2_VECTOR, IPL7AUTO) UART2Interrupt(void) { unsigned char ucReceivedData; xSystemState.bBluetoothBusy = 1; // Rx interrupt. if (INTGetEnable(INT_U2RX) && INTGetFlag(INT_U2RX)) { INTClearFlag(INT_U2RX); // Read out all data available. while (UARTReceivedDataIsAvailable(UART2)) { // Read the received data. ucReceivedData = UARTGetDataByte(UART2); // Make sure there is empty space in the buffer. if ((prv_xRx.uiBufferSize - prv_xRx.uiDataCount) > 0) { // Copy the data to the buffer. prv_xRx.pucBuffer[prv_xRx.uiWritePt] = ucReceivedData; // Increase the write pointer and data count. prv_vIncPointer(&prv_xRx.uiWritePt, prv_xRx.uiBufferSize); prv_xRx.uiDataCount++; portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE; xSemaphoreGiveFromISR(xBluetoothRxSemaphore, &xHigherPriorityTaskWoken); } else { xSystemError.bBluetoothError = 1; } } } // Tx interrupt. if (INTGetEnable(INT_U2TX) && INTGetFlag(INT_U2TX)) { // Loop until the Tx buffer is fully filled. while (UARTTransmitterIsReady(UART2)) { // If there is data to transmit... if (prv_xTx.uiDataCount > 0) { // Shift in the data to the transmit buffer. UARTSendDataByte(UART2, prv_xTx.pucBuffer[prv_xTx.uiReadPt]); // Increase the read pointer and decrease the data count. prv_vIncPointer(&prv_xTx.uiReadPt, prv_xTx.uiBufferSize); prv_xTx.uiDataCount--; } // Else, disable the transmit interrupt. else { INTEnable(INT_U2TX, INT_DISABLED); break; } } } // Error Interrupt. if (INTGetEnable(INT_U2E) && INTGetFlag(INT_U2E)) { INTClearFlag(INT_U2E); // Discard all data available. while (UARTReceivedDataIsAvailable(UART2)) { // Read the received data. ucReceivedData = UARTGetDataByte(UART2); } // Clear the overrun flag. if (UARTGetLineStatus(UART2) & UART_OVERRUN_ERROR) { U2STAbits.OERR = 0; } xSystemError.bBluetoothError = 1; } }
void hal_uart_interrupt_handler(hal_uart_port port) { UART_MODULE uart = logic_uart2phy_uart(port); assert(port >= HAL_UART_PORT_1 && port <= HAL_UART_NUMBER_OF_PORTS ); /* get the txbuffer to send */ struct txbuffer_struct *buffer = get_tx_buffer(port); /* If the source of interrupt is the TXInterrupt, start * transmitting the data in output queue. */ if (INTGetFlag(INT_SOURCE_UART_TX(uart))) { /* Clear interrupt to prevent reentry */ INTClearFlag(INT_SOURCE_UART_TX(uart)); /* if queue is not empty, send tx data to uart while available. */ while (buffer->nbytes > 0) { if (UARTTransmitterIsReady(uart)) { UARTSendDataByte(uart, buffer->buffer[buffer->counter++]); /* decrement buffer count after byte was transmitted */ buffer->nbytes --; } else { break; } } /* if queue is empty, disable tx interrupt. */ if (buffer->nbytes <= 0) { INTEnable(INT_SOURCE_UART_TX(uart), INT_DISABLED); if(on_data_sent[port]!=NULL) on_data_sent[port](port); } } /* detect uart rx errors */ if(INTGetFlag(INT_SOURCE_UART_ERROR(uart))){ uint8_t in_byte = 0x00; hal_uart_error error = HAL_UART_ERR_NONE; volatile UART_LINE_STATUS lineStatus = UARTGetLineStatus(uart); /* detect framming error. (break)*/ /* Framing error are only valid if data is available in buffer. */ if(UART_FRAMING_ERROR & lineStatus){ /* trigger an on_data_received_callback */ error = HAL_UART_ERR_FRAMMING; } /* detect uart overrun errors. */ if(UART_OVERRUN_ERROR & lineStatus) { error = HAL_UART_ERR_OVERRUN; /* TODO: Not sure what to do if buffer overruns (it means data * arrives faster than we are able to process. So just * clear error and continue with our lives as nothing happened. */ UARTClearOverrunError(uart); } if(UART_PARITY_ERROR & lineStatus){ error = HAL_UART_ERR_PARITY; } if(UARTReceivedDataIsAvailable(uart)){ in_byte = UARTGetDataByte(uart); } if(on_data_received[port]!=NULL) on_data_received[port](port,in_byte,error); /* clear the error flag. */ INTClearFlag(INT_SOURCE_UART_ERROR(uart)); } /* If receive interrupt was triggered, feed user apps with data. */ if (INTGetFlag(INT_SOURCE_UART_RX(uart))) { /* Clear interrupt to prevent reentry */ INTClearFlag(INT_SOURCE_UART_RX(uart)); /* Copy the received data into input buffer */ while (UARTReceivedDataIsAvailable(uart)) { uint8_t c = UARTGetDataByte(uart); if(on_data_received[port]!=NULL) on_data_received[port](port, c,HAL_UART_ERR_NONE); } } }
void __ISR(_UART_2_VECTOR, IPL5SOFT) UART2_isr(void) { uint8_t ErrFiFoFull = 0; uint8_t freeSize, TXsize; int8_t c; uint8_t i_cts = 0; BOOL TxPossible; UART_LINE_STATUS lineStatus; // Is this an RX interrupt ? if ( INTGetFlag(INT_U2RX) && INTGetEnable(INT_U2RX) ) { // oui Test si erreur comm lineStatus = UARTGetLineStatus(UART2); if ( (lineStatus & (UART_PARITY_ERROR | UART_FRAMING_ERROR | UART_OVERRUN_ERROR)) == 0) { // transfert dans le fifo de tous les caractères recu while (UARTReceivedDataIsAvailable(UART2)) { c = UARTGetDataByte(UART2); PutCharInFifo ( &descrFifoRX, c); } INTClearFlag(INT_U2RX); // buffer is empty, clear interrupt flag } else { UART2ClearAllErrors(); // Macro C32 } freeSize = GetWriteSpace ( &descrFifoRX); if (freeSize <= 6 ) // a cause d'un int pour 6 char { // Demande de ne plus émettre //RS232_RTS = 1; if (freeSize == 0) { ErrFiFoFull = 1; // pour debugging si probème ctrl flux } } } // end if RX // Is this an TX interrupt ? if(INTGetFlag(INT_U2TX) && INTGetEnable(INT_U2TX) ) { TXsize = GetReadSize (&descrFifoTX); // i_cts = input(RS232_CTS); // On vérifie 3 conditions : // Si CTS = 0 (autorisation d'émettre) // Si il y a un caratères à émettre // Si le txreg est bien disponible //i_cts = RS232_CTS; TxPossible = UARTTransmitterIsReady(UART2); //if ( (i_cts == 0) && ( TXsize > 0 ) && TxPossible ) { if ( ( TXsize > 0 ) && TxPossible ) { do { GetCharFromFifo(&descrFifoTX, &c); UARTSendDataByte(UART2, c); //i_cts = RS232_CTS; TXsize = GetReadSize (&descrFifoTX); TxPossible = UARTTransmitterIsReady(UART2); //} while ( (i_cts == 0) && ( TXsize > 0 ) && TxPossible ); } while ( ( TXsize > 0 ) && TxPossible ); // Clear the TX interrupt Flag (Seulement aprés TX) INTClearFlag(INT_U2TX); } else { // disable TX interrupt INTEnable(INT_U2TX, INT_DISABLED); } } } // UART_isr
void __ISR(_UART_6_VECTOR, U6_INTERRUPT_PRIORITY) Uart6InterruptHandler(void) { UINT8 i ,iMax // Read/write max 8 bytes/interrupt ,data // used in UartFifoWrite/Read functions ; if ( INTGetFlag ( INT_SOURCE_UART_ERROR(UART6)) ) { LED_ERROR_ON; INTClearFlag(INT_SOURCE_UART_ERROR(UART6)); } // TX interrupt handling //=========================================================== if ( INTGetEnable ( INT_SOURCE_UART_TX(UART6) ) ) // If TX interrupts enabled { if ( INTGetFlag ( INT_SOURCE_UART_TX(UART6) ) ) // If TX interrupt occured { if ( UARTTransmitterIsReady(UART6) && !Uart.Var.uartTxFifo[UART6].bufEmpty ) // If TX buffer is ready to receive data and the user's TX buffer is not empty { if (Uart.Var.uartTxFifo[UART6].lineBuffer.length < 8) // Write max 8 bytes/interrupt { iMax = Uart.Var.uartTxFifo[UART6].lineBuffer.length; } else { iMax = 8; } for (i = 0; i < iMax; i++) { UartFifoRead((void *) &Uart.Var.uartTxFifo[UART6], &data); // Copy from user U6TXREG = data; // Put data in PIC32's TX buffer } } if (Uart.Var.uartTxFifo[UART6].bufEmpty) // If User's TX buffer is empty { Uart.DisableTxInterrupts(UART6); // Disable TX interrupts } INTClearFlag(INT_SOURCE_UART_TX(UART6)); // Clear the TX interrupt Flag } } //=========================================================== // RX interrupt handling //=========================================================== if ( INTGetEnable ( INT_SOURCE_UART_RX(UART6) ) ) // If RX interrupts enabled { if ( INTGetFlag ( INT_SOURCE_UART_RX(UART6) ) ) // If RX interrupt occured { i = 0; iMax = 8; // Read max 8 bytes/interrupt while ( UARTReceivedDataIsAvailable(UART6) // While RX data available && !Uart.Var.uartRxFifo[UART6].bufFull // and user's RX buffer not full && (i < iMax) // and under 8 bytes read ) { // while ^ data = UARTGetDataByte(UART6); // Get data for PIC32's RX FIFO buffer and copy it to user (next line) if ( UartFifoWrite((void *) &Uart.Var.uartRxFifo[UART6], &data) < 0 ) // If copy to user did not work { break; // Exit while loop } i++; } // end while if (!Uart.Var.uartRxFifo[UART6].bufEmpty) // If there is data in the user's RX buffer { Uart.Var.oIsRxDataAvailable[UART6] = 1; // Set according flag } INTClearFlag (INT_SOURCE_UART_RX(UART6) ); // Clear the RX interrupt Flag } } //=========================================================== }
int GetCharacter(UART_MODULE id) { while (!UARTReceivedDataIsAvailable(id)); return UARTGetDataByte(id); }
/******************************************************************************* Function: void ConsoleUartIntHandler(void) Remarks: UART 1 interrupt handler is set at priority level 2 with software context saving */ void ConsoleUartIntHandler(void) { unsigned char byteReceived; // Is this an RX interrupt? if (INTGetFlag(INT_SOURCE_UART_RX(UART_CONSOLE))) { while (UARTReceivedDataIsAvailable(UART_CONSOLE) != 0) { byteReceived = UARTGetDataByte(UART_CONSOLE); //Console Interrupt - In Self Test Mode if(self_TESTMode == TRUE) { ConsoleMsgCallback_selfTest(); } //Console Interrupt - RN1723 Command Mode else if(RN_UartCmdMode == 1) { //Exit console mode if 'ESC' if(byteReceived == ESC) { RN_UartCmdMode = FALSE; RN_DATA_MODE(); memset(consoleMsg, '\0', sizeof(consoleMsg)); } else { while(!UARTTransmitterIsReady(UART_WIFLY)); UARTSendDataByte(UART_WIFLY, byteReceived); } } else { PIC32_ConsoleMode = TRUE; putConsole(byteReceived); if(byteReceived == CR) { PrintConsoleMenu(); } else if(byteReceived == LF) { //Ignore Line Feed } else if(byteReceived == BACKSPACE) { consoleMsg[(strlen(consoleMsg)-1)] = '\0'; } else if(byteReceived == ESC) { PIC32_ConsoleMode = FALSE; memset(consoleMsg, '\0', sizeof(consoleMsg)); } else { consoleMsg[strlen(consoleMsg)] = byteReceived; } } } // Clear the RX interrupt Flag INTClearFlag(INT_SOURCE_UART_RX(UART_CONSOLE)); } // We don't care about the TX interrupt if (INTGetFlag(INT_SOURCE_UART_TX(UART_CONSOLE)) ) { INTClearFlag(INT_SOURCE_UART_TX(UART_CONSOLE)); } }