// UART 2 interrupt handler // it is set at priority level 2 void __ISR(_UART2_VECTOR, ipl2) IntUart2Handler(void) { // Is this an RX interrupt? if(INTGetFlag(INT_SOURCE_UART_RX(UART2))) { unsigned char databyte; // Clear the RX interrupt Flag INTClearFlag(INT_SOURCE_UART_RX(UART2)); // Code to be executed on RX interrupt: databyte = UARTGetDataByte(UART2); databyte++; // Echo what we just received. PutCharacter(databyte); } // We don't care about TX interrupt if ( INTGetFlag(INT_SOURCE_UART_TX(UART2)) ) { // Clear the TX interrupt Flag INTClearFlag(INT_SOURCE_UART_TX(UART2)); // Code to be executed on TX interrupt: } }
void __ISR(_UART2_VECTOR, ipl2) IntUart2Handler(void) { // Is this an RX interrupt? if(INTGetFlag(INT_SOURCE_UART_RX(UART2))) { // Clear the RX interrupt Flag INTClearFlag(INT_SOURCE_UART_RX(UART2)); // Code to be executed on RX interrupt: COMMAND = UARTGetDataByte(UART2); // Echo what we just received. PutCharacter(COMMAND); } // We don't care about TX interrupt if ( INTGetFlag(INT_SOURCE_UART_TX(UART2)) ) { // Clear the TX interrupt Flag INTClearFlag(INT_SOURCE_UART_TX(UART2)); // Code to be executed on TX interrupt: //none } }
//************************************* //************************************* //********** 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); }
// UART 2 interrupt handler // it is set at priority level 2 void __ISR(_UART2_VECTOR, ipl2) IntUart2Handler(void) { // Is this an RX interrupt? if(INTGetFlag(INT_SOURCE_UART_RX(UART2))) { // Clear the RX interrupt Flag INTClearFlag(INT_SOURCE_UART_RX(UART2)); // Code to be executed on RX interrupt: // Echo what we just received. PutCharacter(UARTGetDataByte(UART2)); // Toggle LED to indicate UART activity mPORTAToggleBits(BIT_7); } // We don't care about TX interrupt if ( INTGetFlag(INT_SOURCE_UART_TX(UART2)) ) { // Clear the TX interrupt Flag INTClearFlag(INT_SOURCE_UART_TX(UART2)); // Code to be executed on TX interrupt: } }
//******************************** //******************************** //********** 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_async(hal_uart_port port, uint8_t size){ assert(port >= HAL_UART_PORT_1 && port <= HAL_UART_NUMBER_OF_PORTS ); /* select the respective output buffer */ struct txbuffer_struct* tx_buffer = get_tx_buffer(port); tx_buffer->nbytes = size; tx_buffer->counter = 0; UART_MODULE uart = logic_uart2phy_uart(port); INTClearFlag(INT_SOURCE_UART_TX(uart)); INTEnable(INT_SOURCE_UART_TX(uart), INT_ENABLED); }
void __ISR(_UART1_VECTOR, IPL2SOFT) IntUart1Handler(void) { if(INTGetFlag(INT_SOURCE_UART_RX(UART1))) { INTClearFlag(INT_SOURCE_UART_RX(UART1)); if (uart_rx_count<sizeof(uart_rx_buff)-1) uart_rx_count++; else uart_rx_count=0; // overflow uart_rx_buff[uart_rx_count]=UARTGetDataByte(UART1); } if(INTGetFlag(INT_SOURCE_UART_TX(UART1))) { INTClearFlag(INT_SOURCE_UART_TX(UART1)); } }
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)); } }
void __ISR(_UART_4_VECTOR, ipl1) IntUart4Handler(void) { // Is this an RX interrupt? if(INTGetFlag(INT_SOURCE_UART_RX(UART_WIFI_MODULE_ID))) { const char rchar = UARTGetDataByte(UART_WIFI_MODULE_ID); PutcToWifiReceivedBuffer(rchar, &DefaultWifiService); // Clear the RX interrupt Flag INTClearFlag(INT_SOURCE_UART_RX(UART_WIFI_MODULE_ID)); } // We don't care about TX interrupt if ( INTGetFlag(INT_SOURCE_UART_TX(UART_WIFI_MODULE_ID)) ) { INTClearFlag(INT_SOURCE_UART_TX(UART_WIFI_MODULE_ID)); } }
// UART interrupt handler, set at priority level 2 void __ISR(_UART_2_VECTOR, ipl2) IntUart2Handler(void) { // Is this an RX interrupt? if(INTGetFlag(INT_SOURCE_UART_RX(UART_CMD_MODULE_ID))) { char rchar = UARTGetDataByte(UART_CMD_MODULE_ID); AddKeystroke(&CurrentCommandEngine, rchar); // Clear the RX interrupt Flag INTClearFlag(INT_SOURCE_UART_RX(UART_CMD_MODULE_ID)); } // We don't care about TX interrupt if ( INTGetFlag(INT_SOURCE_UART_TX(UART_CMD_MODULE_ID)) ) { INTClearFlag(INT_SOURCE_UART_TX(UART_CMD_MODULE_ID)); } }
void __ISR(_UART_2_VECTOR, ipl2) IntUart2Handler(void) { // Is this an RX interrupt? if(INTGetFlag(INT_SOURCE_UART_RX(UART_MODULE_ID2))) { #ifdef DEBUG PutCharacterXbee(UARTGetDataByte(UART_MODULE_ID2)); #endif INTClearFlag(INT_SOURCE_UART_RX(UART_MODULE_ID2)); } // We don't care about TX interrupt if (INTGetFlag(INT_SOURCE_UART_TX(UART_MODULE_ID2))) { INTClearFlag(INT_SOURCE_UART_TX(UART_MODULE_ID2)); } }
// TODO: Maybe sanity check buffer not empty void hal_uart_send_buffer_async(hal_uart_port port, uint8_t* buffer, uint8_t size){ assert(port >= HAL_UART_PORT_1 && port <= HAL_UART_NUMBER_OF_PORTS ); assert(size > 0 && size < HAL_UART_BUFFER_SIZE ); assert(buffer != NULL); /* select the respective output buffer */ struct txbuffer_struct* tx_buffer = get_tx_buffer(port); /* buffer should be empty before sending a new frame */ assert(tx_buffer->nbytes == 0); /* copy buffer into output buffer */ memcpy(tx_buffer->buffer, buffer, size); /* enable the tx interrupt. It starts the augomagically data transmision * from tx_buffer. */ /* copy the size of the buffer.*/ tx_buffer->nbytes = size; /* set the framecounter at null */ tx_buffer->counter = 0; UART_MODULE uart = logic_uart2phy_uart(port); INTClearFlag(INT_SOURCE_UART_TX(uart)); INTEnable(INT_SOURCE_UART_TX(uart), INT_ENABLED); }
// ***************************************************************************** // Initialize the Console serial port (BAUDRATE) // ***************************************************************************** void InitConsole(UINT32 baud) { UARTConfigure(UART_CONSOLE, UART_ENABLE_PINS_TX_RX_ONLY | UART_ENABLE_HIGH_SPEED); UARTSetFifoMode(UART_CONSOLE, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART_CONSOLE, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART_CONSOLE, GetPeripheralClock(), baud); #ifdef DEBUG_ALLOW_USER_INPUT INTEnable(INT_SOURCE_UART_RX(UART_CONSOLE), INT_ENABLED); INTSetVectorPriority(INT_VECTOR_UART(UART_CONSOLE), INT_PRIORITY_LEVEL_2); INTSetVectorSubPriority(INT_VECTOR_UART(UART_CONSOLE), INT_SUB_PRIORITY_LEVEL_0); INTEnable(INT_SOURCE_UART_TX(UART_CONSOLE), INT_DISABLED);//Disable TX interrupt! #endif UARTEnable(UART_CONSOLE, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); }
void enableUartISR( UART_MODULE umPortNum ) { /* Enable Interrupts */ INTEnable( INT_SOURCE_UART_RX( umPortNum ), INT_ENABLED ); INTEnable( INT_SOURCE_UART_TX( umPortNum ), INT_ENABLED ); }
// UART3 interrupt handler, priority level 2 void __ISR(_UART_3_VECTOR, ipl2) IntUart3Handler(void) { // Is this an RX interrupt? if(INTGetFlag(INT_SOURCE_UART_RX(UART3))){ char data = UARTGetDataByte(UART3); LED1 = !LED1; if(data =='x') { //Stop program, both magnets ON start = 0; EMAG1 = EMAG2 = 1; } if(data =='e') { // Magnet 1 ON EMAG1 = 1; } if(data =='f') { // Magnet 1 OFF EMAG1 = 0; } if(data =='g') { // Magnet 2 ON EMAG2 = 1; } if(data =='h') { // Magnet 2 OFF EMAG2 = 0; } if(data =='s') { // start motion program start = 1; LED0=0; } if(data =='o') { delay1 = delay1+1; } if(data =='p') { delay1 = delay1-1; } if(data =='k') { delay2 = delay2+1; } if(data =='l') { delay2 = delay2-1; } if(data =='n') { delay3 = delay3+1; } if(data =='m') { delay3 = delay3-1; } // Clear the RX interrupt Flag INTClearFlag(INT_SOURCE_UART_RX(UART3)); } // We don't care about TX interrupt if(INTGetFlag(INT_SOURCE_UART_TX(UART3))) { INTClearFlag(INT_SOURCE_UART_TX(UART3)); } }
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 hal_uart_disable_interrupts(hal_uart_port port){ UART_MODULE uart = logic_uart2phy_uart(port); // INTEnable(INT_SOURCE_UART_RX(uart), INT_DISABLED); INTEnable(INT_SOURCE_UART_TX(uart), INT_DISABLED); }
void disableUartISR( UART_MODULE umPortNum ) { /* Disable Interrupts */ INTEnable( INT_SOURCE_UART_RX( umPortNum ), INT_DISABLED ); INTEnable( INT_SOURCE_UART_TX( umPortNum ), INT_DISABLED ); }
/******************************************************************************* 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 __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 } } //=========================================================== }