//******************************** //******************************** //********** INTERRUPTS ********** //******************************** //******************************** void __ISR(_UART_1_VECTOR, ipl5) Uart1InterruptHandler (void) //(ipl# must match the priority level assigned to the irq where its enabled) { static BYTE data; static BYTE status; static WORD tx_checksum; static WORD rx_checksum; static WORD checksum_recevied; static WORD w_temp; static WORD rx_length_received; Nop(); if ( (INTGetEnable(INT_SOURCE_UART_TX(COMMS_UART_NAME))) && (INTGetFlag(INT_SOURCE_UART_TX(COMMS_UART_NAME))) ) { //---------------------------------- //---------------------------------- //---------- TX INTERRUPT ---------- //---------------------------------- //---------------------------------- while (UARTTransmitterIsReady(COMMS_UART_NAME)) { //if (comms_tx_byte == 0) //Done in start tx function //{ // //----- SEND BYTE 0 ----- // comms_tx_chksum = (WORD)comms_tx_buffer[0]; // COMMS_TX_REG = (WORD)comms_tx_buffer[comms_tx_byte++]; //} if (comms_tx_byte < comms_tx_no_of_bytes_to_tx) { //----- SEND NEXT DATA BYTE ----- comms_tx_chksum += (WORD)comms_tx_buffer[comms_tx_byte]; //Add to checksum UARTSendDataByte(COMMS_UART_NAME, comms_tx_buffer[comms_tx_byte++]); } else if (comms_tx_byte == comms_tx_no_of_bytes_to_tx) { //----- SEND CHECKSUM HIGH ----- UARTSendDataByte(COMMS_UART_NAME, (comms_tx_chksum >> 8)); comms_tx_byte++; } else if (comms_tx_byte == (comms_tx_no_of_bytes_to_tx + 1)) { //----- SEND CHECKSUM LOW ----- UARTSendDataByte(COMMS_UART_NAME, (comms_tx_chksum & 0x00ff)); comms_tx_byte++; //----- ALL BYTES SENT ----- comms_tx_byte = 0; //Reset tx byte counter (indicates we're done) comms_tx_no_of_bytes_to_tx = 0; //DISABLE TX IRQ INTEnable(INT_SOURCE_UART_TX(COMMS_UART_NAME), INT_DISABLED); } } //while (UARTTransmitterIsReady(COMMS_UART_NAME))
void hal_uart_send_byte(hal_uart_port port, uint8_t data){ assert(port >= HAL_UART_PORT_1 && port <= HAL_UART_NUMBER_OF_PORTS ); UART_MODULE uart = logic_uart2phy_uart(port); /* block until transmiter can send data */ while(!UARTTransmitterIsReady(uart)); UARTSendDataByte(uart, data); }
//************************************* //************************************* //********** COMMS TX PACKET ********** //************************************* //************************************* //Call with: // comms_tx_byte Check this is zero before loading comms_tx_buffer (confirms last tx is complete) // comms_tx_buffer[] The packet data to send with the command in byte 0:1. The Length will automatically be added to bytes 2:3 by this function // packet_length The number of data bytes excluding the checksum (which is automatically added by the tx function) void comms_tx_packet (WORD packet_length) { //Packet format: // CommandH | CommandL | LengthH | LengthL | 0-# Data Bytes | Checksum H | Checksum L //Check last tx is complete if (comms_tx_byte) return; if (packet_length > COMMS_TX_BUFFER_LENGTH) return; //----- START TX ----- comms_tx_no_of_bytes_to_tx = packet_length; //no of bytes to tx (excluding checksum) //Set the length bytes comms_tx_buffer[2] = (BYTE)(packet_length >> 8); comms_tx_buffer[3] = (BYTE)(packet_length & 0x00ff); //comms_rx_no_of_bytes_to_rx = 0xfffe; //If you want to reset rx //comms_rx_1ms_timeout_timer = ; comms_tx_byte = 0; comms_tx_chksum = (WORD)comms_tx_buffer[0]; INTClearFlag(INT_SOURCE_UART_TX(COMMS_UART_NAME)); UARTSendDataByte(COMMS_UART_NAME, comms_tx_buffer[comms_tx_byte++]); //Manually trigger the first tx INTEnable(INT_SOURCE_UART_TX(COMMS_UART_NAME), INT_ENABLED); }
// Write a string over the serial port int SendString(int id, const char *string) { UART_MODULE uartX; switch(id) { case 1: uartX = UART1; break; case 2: uartX = UART2; break; case 3: uartX = UART3; break; case 4: uartX = UART4; break; default: uartX = UART1; } while(*string != '\0') { while(!UARTTransmitterIsReady(uartX)); UARTSendDataByte(uartX, (char) *string); string++; while(!UARTTransmissionHasCompleted(uartX)); } return 1; }
void PutCharacterBluetooth(const char character) { while(!UARTTransmitterIsReady(UART_MODULE_ID2)); UARTSendDataByte(UART_MODULE_ID2, character); while(!UARTTransmissionHasCompleted(UART_MODULE_ID2)); }
void sendString(char* string){ int i = 0; while(string[i]){ while(!UARTTransmitterIsReady(UART1)); UARTSendDataByte(UART1, string[i]); i++; } }
void Serial2Write(uint8 data) // write to UART directly and wait to finish (hangs execution until finished) { if(SerialConnected) // dont write anything to serial until connection made from client { UARTSendDataByte(UART2, data); while(!UARTTransmissionHasCompleted(UART2)); } }
// Write a string over the serial port void WriteString(UART_MODULE id, const char *string) { while(*string != '\0') { while(!UARTTransmitterIsReady(id)); UARTSendDataByte(id, *string); string++; while(!UARTTransmissionHasCompleted(id)); } }
void tx(const void *src, size_t n) { size_t ui; for (ui = 0; ui < n; ++ui) { WDT::clear(); while (!UARTTransmitterIsReady(module)) Nop(); UARTSendDataByte(module, ((const BYTE *)src)[ui]); } // while (!UARTTransmissionHasCompleted(module)) Nop(); // TODO: async }
// ***************************************************************************** // void SendDataBuffer(const UARTx, const char *buffer) // Envoie sur le port serie la chaine de caractère // @param : UARTx : choix du port // UART1,UART2,UART3,UART4,UART5,UART6 // buffer : chaine de caractère // ***************************************************************************** void SendDataBuffer(const UARTx, const char *buffer) { while(*buffer != '\n') { while(!UARTTransmitterIsReady(UARTx)); UARTSendDataByte(UARTx, *buffer); buffer++; } while(!UARTTransmissionHasCompleted(UARTx)); }
void WriteString(const char *string) { while(*string != '\0') { while(!UARTTransmitterIsReady(UART_CMD_MODULE_ID)); UARTSendDataByte(UART_CMD_MODULE_ID, *string); string++; while(!UARTTransmissionHasCompleted(UART_CMD_MODULE_ID)); } }
/* * Function Name: void UARTiWriteBinaryData(void) * Description: Writes a block of binary data to he selected uart. * Initiator: Keith Suhoza * Date: 01/23/2012 */ void UARTiWriteBinaryData(unsigned char *data_block, int length, UART_MODULE iUART) { while( length) { while(!UARTTransmitterIsReady(iUART)); UARTSendDataByte(iUART, *data_block++); while(!UARTTransmissionHasCompleted(iUART)); length--; } }
void uart_print(BYTE *string) { while(!string) { while(!UARTTransmitterIsReady(UART1)); UARTSendDataByte(UART1, *string); string++; while(!UARTTransmissionHasCompleted(UART1)); } }
//****************************************************************************** //Interrupt Request Routines //****************************************************************************** void __ISR(_UART_1_VECTOR, IPL5AUTO) __UART1Interrupt(void) { UINT8 rxByte = 0; UINT8 txByte = 0; int rslt = 0; if (INTGetFlag(INT_U1RX)) { //Add received byte rxByte = UARTGetDataByte(UART1); FIFOUART1_pushRxQueue(&rxByte, 1); INTClearFlag(INT_U1RX); } if (INTGetFlag(INT_U1TX)) { rslt = FIFOUART1_popTxQueue(&txByte); switch (rslt) { case 2: //Success, non-empty buffer UARTSendDataByte(UART1, txByte); break; case 1: //Success, empty buffer UARTSendDataByte(UART1, txByte); INTEnable(INT_U1TX, INT_DISABLED); break; case -1: //Queue is Overflowing //Reset the indexs FIFOUART1_TxBuffer_TxIndex = 0; FIFOUART1_TxBuffer_Index = 0; case -2: //Queue is Empty default: //unknown result INTEnable(INT_U1TX, INT_DISABLED); break; } INTClearFlag(INT_U1TX); } }
void PutCharacter(const char character) { while(!UARTTransmitterIsReady(UART2)) ; UARTSendDataByte(UART2, character); while(!UARTTransmissionHasCompleted(UART2)) ; }
void UARTSendString(UART_MODULE uart, const char *string) { while(*string) { if (UARTTransmitterIsReady(uart))//Case where it doesn't matter whether if or while because location is only incremented when the byte is sent { UARTSendDataByte(uart, *string); while(!UARTTransmissionHasCompleted(uart)){} string++; //increment through the string } } }
void serial_transmit(void) { if (uart_tx_buf_written < uart_tx_buf_writing) { if (UARTTransmitterIsReady(UART1)) { // If the transmitter can handle another byte UARTSendDataByte(UART1, uart_tx_buf[uart_tx_buf_written++]); // Send a single byte from the output buffer uart_tx_buf_size--; // One less byte in the buffer } } else { // No data in the buffer, so reset the index uart_tx_buf_written = 0; uart_tx_buf_writing = 0; } }
void serialPutc(unsigned char serialPortIndex, char c) { UART_MODULE uart = getUartModule(serialPortIndex); while (!UARTTransmitterIsReady(uart)) { } UARTSendDataByte(uart, c); while (!UARTTransmissionHasCompleted(uart)) { } }
/* * Function Name: void UARTiWriteString(void) * Description: Writes a full string out the selected uart. * Initiator: Robert Scaccia * Date: 10/19/2011 */ void UARTiWriteString(const char *ccpString, UART_MODULE iUART) { static int ready_high_water = 0, completed_high_water = 0; int ready_count = 0, completed_count = 0; while(*ccpString != '\0') { while(!UARTTransmitterIsReady(iUART)) ready_count++; UARTSendDataByte(iUART, *ccpString); ccpString++; while(!UARTTransmissionHasCompleted(iUART)) completed_count++; } if(ready_count > ready_high_water) ready_high_water = ready_count; if(completed_count > completed_high_water) completed_high_water = completed_count; }
// ***************************************************************************** // void UARTTxBuffer(char *buffer, UINT32 size) // ***************************************************************************** void SendDataBuffer(const char *buffer, UINT32 size) { while(size) { while(!UARTTransmitterIsReady(UART2)) ; UARTSendDataByte(UART2, *buffer); buffer++; size--; } while(!UARTTransmissionHasCompleted(UART2)) ; }
void WriteStringBluetooth2(const char *string) { int i=0; while(i<17) { while(!UARTTransmitterIsReady(UART_MODULE_ID2)); UARTSendDataByte(UART_MODULE_ID2, *string); string++; i++; while(!UARTTransmissionHasCompleted(UART_MODULE_ID2)); } }
void vUART1_ISR( void ) { static portBASE_TYPE xHigherPriorityTaskWoken; UART_DATA rx_data; static int i = 0; if( INTGetFlag( INT_U1RX ) ) { INTClearFlag( INT_U1RX ); /*---------------------------------------------------------------------- Rx interrupt functionality ----------------------------------------------------------------------*/ rx_data = UARTGetData( TARGET_UART ); rx_buffer = rx_data.__data; xHigherPriorityTaskWoken = xTaskResumeFromISR( rx_task_handle ); xSemaphoreGiveFromISR( inputByteBuffer, &xHigherPriorityTaskWoken ); // set Rx task to resume portEND_SWITCHING_ISR( xHigherPriorityTaskWoken ); } else { if( INTGetFlag( INT_U1TX ) ) { INTClearFlag( INT_U1TX ); /*------------------------------------------------------------------ Tx interrupt functionality ------------------------------------------------------------------*/ // transmit tx_buffer if( tx_buffer[ i ] != '\0' ) UARTSendDataByte( TARGET_UART, tx_buffer[ i++ ] ); else { i = 0; xHigherPriorityTaskWoken = pdFALSE; xSemaphoreGiveFromISR( outputStringBuffer, &xHigherPriorityTaskWoken ); } } } }
int SendCharacter(int id, const char character) { UART_MODULE uartX; switch(id) { case 1: uartX = UART1; break; case 2: uartX = UART2; break; case 3: uartX = UART3; break; case 4: uartX = UART4; break; default: uartX = UART1; } while(!UARTTransmitterIsReady(uartX)); UARTSendDataByte(uartX, character); while(!UARTTransmissionHasCompleted(uartX)); }
void uart_putc(BYTE ch) { while(!UARTTransmitterIsReady(UART1)); UARTSendDataByte(UART1, ch); }
// ***************************************************************************** // Send a character to the Console serial port // ***************************************************************************** void putConsole(char c) { while (!UARTTransmitterIsReady(UART_CONSOLE)); UARTSendDataByte(UART_CONSOLE, c); }
/******************************************************************************* * 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; } }
// Put a character over the serial port, called by WriteString void PutCharacter(UART_MODULE id, const char character) { while(!UARTTransmitterIsReady(id)); UARTSendDataByte(id, character); while(!UARTTransmissionHasCompleted(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)); } }
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