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 } }
// 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: } }
// 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 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 ); } } } }
void __ISR(_UART_2_VECTOR, ipl1) UART2_ISR(void){ if(INTGetFlag(INT_U2RX)){ GPSData[GPSIndex] = UARTReceiveByte(UART2); GPSIndex++; newData = 1; INTClearFlag(INT_U2RX); } }
void __ISR(_TIMER_2_VECTOR, ipl2) Timer2_ISR(void) { if (INTGetFlag(INT_T2)){ millisec++; stopDelayCounter++; startDelayCounter++; INTClearFlag(INT_T2); // Acknowledge the interrupt source by clearing its flag. } }
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)); } }
// 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_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)); } }
void __attribute__((vector(_ADC_VECTOR), interrupt(ipl4), nomips16)) ADC_ISR(void) { if (INTGetFlag(INT_AD1)){ //when conversion is complete write ADC word to the variable so it can be displayed analogIn1 = ReadADC10(0); analogIn2 = ReadADC10(1); analogRead = TRUE; INTClearFlag(INT_AD1); } }
//****************************************************************************** //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 __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)); } }
/**************************************************************************** Function IntUart1Handler Parameters None. Returns None. Description Interrupt Handle for the uart. with the PIC32 architecture both send and receive are handled within the same interrupt Notes Author Max Dunne, 2011.11.10 ****************************************************************************/ void __ISR(_UART1_VECTOR, IPL4AUTO) IntUart1Handler(void) { static int transmitcount = 0; static int clearcount = 0; static int stallcount = 0; if (INTGetFlag(INT_U1RX)) { LATBbits.LATB8 ^= 1; writeBack(receiveBuffer, (unsigned char) U1RXREG); INTClearFlag(INT_U1RX); } if (INTGetFlag(INT_U1TX)) { //INTClearFlag(INT_U1TX); //IFS1bits.U1TXIF=0; //INTSTATbits. //Nop(); //LATBbits.LATB7=0; //LATBbits.LATB8=PORTCbits.RC3; //LATCbits.LATC3^=1; if (!(getLength(transmitBuffer) == 0)) { transmitcount++; U1TXREG = readFront(transmitBuffer); INTClearFlag(INT_U1TX); stallcount = 0; } else { clearcount++; //INTEnable(INT_U1TX, 0); stallcount++; Nop(); } if (stallcount > 1) { INTEnable(INT_U1TX, INT_DISABLED); INTClearFlag(INT_U1TX); } } }
/**************************************************************************** Function IntUart1Handler Parameters None. Returns None. Description Interrupt Handle for the uart. with the PIC32 architecture both send and receive are handled within the same interrupt Notes Author Max Dunne, 2011.11.10 ****************************************************************************/ void __ISR(_UART1_VECTOR, ipl4) IntUart1Handler(void) { if (INTGetFlag(INT_U1RX)) { INTClearFlag(INT_U1RX); if (!GettingFromReceive) { writeBack(receiveBuffer, (unsigned char) U1RXREG); } else { //acknowledge we have a collision and return ReceiveCollisionOccured = TRUE; } } if (INTGetFlag(INT_U1TX)) { INTClearFlag(INT_U1TX); if (!(getLength(transmitBuffer) == 0)) { if (!AddingToTransmit) { U1TXREG = readFront(transmitBuffer); } else { //acknowledge we have a collision and return TransmitCollisionOccured = TRUE; } } } }
/******************************************************************************* * 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; } }
// 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 __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
//****************************************************************************** //Interrupt Request Routines //****************************************************************************** void __ISR(_SPI_2_VECTOR, IPL4AUTO)__SPI2Interrupt(void) { //Receive interupt if (INTGetFlag(INT_SPI2RX)) { //If it's RX's turn if (isTransmitsTurn == 0) { //Set the flag so it's TX's turn next isTransmitsTurn = 1; //Read byte from buffer RxBuffer[RxBuffer_Index++] = SPI2BUF; //Clear Interrupt flag INTClearFlag(INT_SPI2RX); //If the current device we are sending to doesn't match the next byte's device if (TxBufferFlags[TxBuffer_TxIndex] != TxBufferFlags[TxBuffer_TxIndex -1]) { //TODO: decide if it's better just to deselect all devices. //Deselect the current device switch (TxBufferFlags[RxBuffer_Index - 1]) { case 0: FIFOSPI2_DeviceSSLine1_PortReg = 1; FIFOSPI2_DeviceSSLine2_PortReg = 1; break; case 1: FIFOSPI2_DeviceSSLine1_PortReg = 1; //Hi to deselect break; case 2: FIFOSPI2_DeviceSSLine2_PortReg = 1; //Hi to deselect break; } } //If their are bytes to send if (TxBuffer_Index > TxBuffer_TxIndex) { //Set the TX Interupt flag so that it can send them INTSetFlag(INT_SPI2TX); INTEnable(INT_SPI2TX, INT_ENABLED); } else { //Clear and disable the TX interupt INTEnable(INT_SPI2TX, INT_DISABLED); INTClearFlag(INT_SPI2TX); //update the flag that it's not running anymore. FIFOSPI2_isRunnning = 0; //Clear the Send Buffer indecies TxBuffer_TxIndex = 0; TxBuffer_Index = 0; } } } //Transmit interrupt and it is enabled. if (INTGetFlag(INT_SPI2TX) && INTGetEnable(INT_SPI2TX)) { //Clear Interrupt flag //INTClearFlag(INT_SPI2TX); INTEnable(INT_SPI2TX, INT_DISABLED); //If it's TX's turn if (isTransmitsTurn == 1) { //Set the flag so it's RX's turn next isTransmitsTurn = 0; // //Select the current device // FIFOSPI2_DeviceSSLine1 = 0; //Select the current device switch (TxBufferFlags[TxBuffer_TxIndex]) { case 1: FIFOSPI2_DeviceSSLine1_PortReg = 0; //Low to select break; case 2: FIFOSPI2_DeviceSSLine2_PortReg = 0; //Low to select break; } //Send the next byte SPI2BUF = TxBuffer[TxBuffer_TxIndex++]; } } }
//============================================= // Configure the I2C4 interrupt handler //============================================= void __ISR(_I2C_4_VECTOR, I2C4_INT_PRIORITY) I2c4InterruptHandler(void) { sI2cCmdBuffer_t masterData; if (INTGetFlag(INT_I2C4B)) //Bus Collision interrupt { INTClearFlag(INT_I2C4B); } if (INTGetFlag(INT_I2C4M)) // Master interrupt { INTClearFlag(INT_I2C4M); if (I2c.Var.oReadDataInNextInterrupt[I2C4]) // If a read was started last interrupt { masterData.data = I2C4RCV; // Read from I2C buffer masterData.state = I2C_MASTER_RECEIVE_DATA; I2cFifoWrite((void *) &I2c.Var.i2cUserFifo[I2C4], &masterData); I2c.Var.oRxDataAvailable[I2C4] = 1; I2c.Var.oReadDataInNextInterrupt[I2C4] = 0; } if (I2c.Var.oI2cWriteIsRunning[I2C4]) { I2cFifoRead((void *) &I2c.Var.i2cWriteQueue[I2C4], &masterData); switch (masterData.state) { //====================================================== case I2C_MASTER_RECEIVE_DATA : I2C4CONbits.RCEN = 1; //Receive byte sequence I2c.Var.oReadDataInNextInterrupt[I2C4] = 1; // Flag for the next interrupt to read the rx buffer break; //====================================================== //====================================================== case I2C_MASTER_START_CONDITION : I2C4CONbits.SEN = 1; //start condition sequence break; //====================================================== //====================================================== case I2C_MASTER_STOP_CONDITION : I2C4CONbits.PEN = 1; if (I2c.Var.oPoolSlaveAcknowledge[I2C4]) { if (!I2c.Var.oSecondStopAfterPooling[I2C4]) { masterData.state = I2C_MASTER_START_CONDITION; I2cFifoWrite((void *) &I2c.Var.i2cWriteQueue[I2C4], &masterData); masterData.state = I2C_MASTER_TRANSMIT_DATA; I2cFifoWrite((void *) &I2c.Var.i2cWriteQueue[I2C4], &masterData); masterData.state = I2C_MASTER_STOP_CONDITION; I2cFifoWrite((void *) &I2c.Var.i2cWriteQueue[I2C4], &masterData); I2c.Var.oSecondStopAfterPooling[I2C4] = 1; } else { if (!I2CByteWasAcknowledged(I2C4)) { masterData.state = I2C_MASTER_START_CONDITION; I2cFifoWrite((void *) &I2c.Var.i2cWriteQueue[I2C4], &masterData); masterData.state = I2C_MASTER_TRANSMIT_DATA; I2cFifoWrite((void *) &I2c.Var.i2cWriteQueue[I2C4], &masterData); masterData.state = I2C_MASTER_STOP_CONDITION; I2cFifoWrite((void *) &I2c.Var.i2cWriteQueue[I2C4], &masterData); } else { masterData.state = I2C_MASTER_DONE; I2cFifoWrite((void *) &I2c.Var.i2cWriteQueue[I2C4], &masterData); I2c.Var.oSecondStopAfterPooling[I2C4] = 0; } } } break; //====================================================== //====================================================== case I2C_MASTER_TRANSMIT_DATA : I2C4TRN = masterData.data; break; //====================================================== //====================================================== case I2C_MASTER_DONE : if (I2c.Var.i2cWriteQueue[I2C4].bufEmpty) // Nothing more to send { I2c.Var.oI2cWriteIsRunning[I2C4] = 0; // Turn off writing process } else { INTSetFlag(INT_I2C4M); // Start another writing process } break; //====================================================== //====================================================== case I2C_MASTER_REPEAT_START : I2C4CONbits.RSEN = 1; //repeated start condition sequence break; //====================================================== //====================================================== case I2C_MASTER_SLAVE_SENT_STOP : LED_ERROR_ON; break; //====================================================== //====================================================== case I2C_MASTER_SEND_ACK : I2C4CONbits.ACKDT = 0; //ACK I2C4CONbits.ACKEN = 1; //Send ACK sequence break; //====================================================== //====================================================== case I2C_MASTER_SEND_NACK : I2C4CONbits.ACKDT = 1; //NACK I2C4CONbits.ACKEN = 1; //Send NACK sequence break; //====================================================== //====================================================== case I2C_CMD_ERROR : LED_ERROR_ON; break; //====================================================== //====================================================== default : break; //====================================================== } // end switch } // end if if (I2c.Var.oI2cReadIsRunning[I2C4]) { I2cFifoRead((void *) &I2c.Var.i2cReadQueue[I2C4], &masterData); switch (masterData.state) { //====================================================== case I2C_MASTER_RECEIVE_DATA : I2C4CONbits.RCEN = 1; //Receive byte sequence I2c.Var.oReadDataInNextInterrupt[I2C4] = 1; break; //====================================================== //====================================================== case I2C_MASTER_START_CONDITION : I2C4CONbits.SEN = 1; //start condition sequence break; //====================================================== //====================================================== case I2C_MASTER_REPEAT_START : I2C4CONbits.RSEN = 1; //repeated start condition sequence break; //====================================================== //====================================================== case I2C_MASTER_STOP_CONDITION : I2C4CONbits.PEN = 1; break; //====================================================== //====================================================== case I2C_MASTER_TRANSMIT_DATA : I2C4TRN = masterData.data; break; //====================================================== //====================================================== case I2C_MASTER_DONE : if (I2c.Var.i2cReadQueue[I2C4].bufEmpty) // Nothing more to send { I2c.Var.oI2cReadIsRunning[I2C4] = 0; // Turn off reading process } else { INTSetFlag(INT_I2C4M); // Start another reading process } break; //====================================================== //====================================================== case I2C_MASTER_SLAVE_SENT_STOP : LED_ERROR_ON; break; //====================================================== //====================================================== case I2C_MASTER_SEND_ACK : I2C4CONbits.ACKDT = 0; //ACK I2C4CONbits.ACKEN = 1; //Send ACK sequence break; //====================================================== //====================================================== case I2C_MASTER_SEND_NACK : I2C4CONbits.ACKDT = 1; //NACK I2C4CONbits.ACKEN = 1; //Send NACK sequence break; //====================================================== //====================================================== case I2C_CMD_ERROR : LED_ERROR_ON; break; //====================================================== //====================================================== default : break; //====================================================== } // end switch } // end if } // end if } // end if
/******************************************************************************* 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(RPI_SPI_INTERRUPT , RPI_COMMS_INT_PRIORITY) RPiSPIInterrupt(void) { static uint8_t RXTemp; /* check for receive interrupt */ if(INTGetEnable(INT_SOURCE_SPI_RX(RPI_SPI_CHANNEL))&& INTGetFlag(INT_SOURCE_SPI_RX(RPI_SPI_CHANNEL))) { INTClearFlag(INT_SOURCE_SPI_RX(RPI_SPI_CHANNEL)); if(RPI_SPI_RX_BUF_FULL) { RXTemp=RPI_SPI_BUF; /* data in the buffer, read it */ switch(SPI.RXState) { case STATE_SPI_RX_COMMAND: { /* want to detect when CE is de-asserted */ CNTemp=CE_PORT; /* read for change notice */ INTClearFlag(INT_CN); INTEnable(INT_CN,INT_ENABLED); if(SPI.status.RXDataReady) { SPI.status.RXOverflow=TRUE; } SPI.status.RXDataReady=FALSE; SPI.command=RXTemp; SPI.TXIndex=0; RPI_SPI_BUF=RXTemp; SPI.RXState=STATE_SPI_RX_ADDRESS_MSB; break; } case STATE_SPI_RX_ADDRESS_MSB: { RPI_SPI_BUF=RXTemp; RXTemp=RPI_SPI_BUF; SPI.TXIndex=0; SPI.RXState=STATE_SPI_RX_ADDRESS_LSB; SPI.address=(0xff00)&(RXTemp<<8); break; } case STATE_SPI_RX_ADDRESS_LSB: { SPI.TXIndex=0; SPI.address|=((0x00ff)&RXTemp); /* now that we have address, what to do with it? */ switch(SPI.command) { case TRISTHIS_SPI_READ_COMMAND: { /* master is reading data from us, that is, we, */ /* as the slave are transmitting */ unsigned int index=0; /* the master is requesting data, make a copy and */ /* have it ready */ /* only really can use the low byte of the address*/ SPI.TXIndex=(SPI.address&0x00ff); /* copy data into outgoing array and bounds check */ while((index<sizeof(TRISThisData))&& (index<sizeof(SPI.TXData))) { SPI.TXData[index]=TRISThisData.data[index++]; } SPI.status.TXEnd=FALSE; SPI.status.TXDataReady=TRUE; SPI.status.TXInProgress=TRUE; INTEnable(INT_SOURCE_SPI_TX(RPI_SPI_CHANNEL),INT_ENABLED); /* have to continue to look for change notice as */ /* that is the only way to see that the */ /* transaction is over */ SPI.RXState=STATE_SPI_RX_MASTER_READING; break; } case TRISTHIS_SPI_WRITE_COMMAND: { /* master is writing data (slave is receiving) */ SPI.status.RXOverflow=FALSE; SPI.status.RXOverrun=FALSE; SPI.status.RXInProgress=TRUE; /* the data to send on next TX interrupt */ SPI.RXState=STATE_SPI_RX_DATA; break; } default: { /* dunno what happened. */ break; } } break; } case STATE_SPI_RX_DATA: { /* master is sending data, slave receiving */ if(!SPI.status.RXOverrun) { SPI.RXData[SPI.RXCount++]=RXTemp; if(SPI.RXCount==SPI_RX_BUFFER_SIZE) { SPI.RXState=STATE_SPI_RX_COMPLETE; } } RPI_SPI_BUF=0x00; break; } case STATE_SPI_RX_COMPLETE: { /* error-- went too long*/ SPI.status.RXOverrun=TRUE; break; } case STATE_SPI_RX_MASTER_READING: /* if the master is reading, nothing to do here */ case STATE_SPI_RX_SPI_WRITE_COMPLETE: { break; } default: { SPI.status.RXMysteryState=TRUE; break; } } } } /* and then check for transmit interrupt */ if(INTGetEnable(INT_SOURCE_SPI_TX(RPI_SPI_CHANNEL))&& INTGetFlag(INT_SOURCE_SPI_TX(RPI_SPI_CHANNEL))) { if(SPI.status.TXEnd||(SPI.status.TXDataReady==FALSE)) { INTEnable(INT_SOURCE_SPI_TX(RPI_SPI_CHANNEL),INT_DISABLED); } else { while(SPI1STATbits.SPITBE&&SPI.status.TXDataReady) { /* send data and point at the next */ RPI_SPI_BUF=SPI.TXData[SPI.TXIndex++]; /* bounds check */ if(SPI.TXIndex<sizeof(SPI.TXData)) { /* all is good, no bounds violation */ SPI.TXCount++; } else { /* there can be no more! */ INTEnable(INT_SOURCE_SPI_TX(RPI_SPI_CHANNEL),INT_DISABLED); SPI.status.TXEnd=TRUE; SPI.status.TXDataReady=FALSE; } } } } }
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 } } //=========================================================== }
//****************************************************************************** //Interrupt Request Routines //****************************************************************************** void __ISR(_I2C_2_VECTOR, IPL5AUTO) __I2C2Interrupt(void) { int i = 0; int ind = 0, ind2 = 0; FIFOI2C2_RX_Byte rxb; if (INTGetFlag(INT_I2C2B)) //Bus Collision { INTClearFlag(INT_I2C2B); //TODO: Set an error, or reset the transmission. } if (INTGetFlag(INT_I2C2M)) //Master Intterupt { INTClearFlag(INT_I2C2M); //If we have received a byte, read it. if (FIFOI2C2_nextInterruptReceive == 1) { //Setup indexes (easier to read) ind = FIFOI2C2_Devices_List[FIFOI2C2_currentDevice].transmit_buffer_current; ind2 = FIFOI2C2_Devices_List[FIFOI2C2_currentDevice].receive_buffer_length; //Read the received byte and the current command we are on. rxb.device_command = FIFOI2C2_Devices_List[FIFOI2C2_currentDevice].transmit_buffer[ind].device_command; //read the transmitted byte rxb.rx_byte = I2C2RCV; //Write rxb to the receive buffer FIFOI2C2_Devices_List[FIFOI2C2_currentDevice].receive_buffer[ind2] = rxb; //increment indexes FIFOI2C2_Devices_List[FIFOI2C2_currentDevice].receive_buffer_length++; FIFOI2C2_nextInterruptReceive = 0; } //Switch statement for the current_device's transmit byte's device_state ind = FIFOI2C2_Devices_List[FIFOI2C2_currentDevice].transmit_buffer_current; switch(FIFOI2C2_Devices_List[FIFOI2C2_currentDevice].transmit_buffer[ind].device_command) { case FIFOI2C2_DEVICE_COMMAND_CMDERROR: ind = 0; //filler break; case FIFOI2C2_DEVICE_COMMAND_START: I2C2CONbits.SEN = 1; //start condition sequence break; case FIFOI2C2_DEVICE_COMMAND_TX_BYTE: I2C2TRN = FIFOI2C2_Devices_List[FIFOI2C2_currentDevice].transmit_buffer[ind].tx_byte; //transmit byte break; case FIFOI2C2_DEVICE_COMMAND_RESTART: I2C2CONbits.RSEN = 1; //repeated start condition sequence break; case FIFOI2C2_DEVICE_COMMAND_RX_BYTE: I2C2CONbits.RCEN = 1; //Receive byte sequence //Flag for next intr to be for receiving said byte FIFOI2C2_nextInterruptReceive = 1; break; case FIFOI2C2_DEVICE_COMMAND_ACK: I2C2CONbits.ACKDT = 0; //ACK I2C2CONbits.ACKEN = 1; //Send ACK sequence break; case FIFOI2C2_DEVICE_COMMAND_NACK: I2C2CONbits.ACKDT = 1; //NACK I2C2CONbits.ACKEN = 1; //Send NACK sequence break; case FIFOI2C2_DEVICE_COMMAND_STOP: I2C2CONbits.PEN = 1; //Stop routine break; case FIFOI2C2_DEVICE_COMMAND_CMDEND: //Loop through all devices and check if they have something (or more somethings) to send. //Check Current Device first if ((FIFOI2C2_Devices_List[FIFOI2C2_currentDevice].transmit_buffer_current + 1) >= FIFOI2C2_Devices_List[FIFOI2C2_currentDevice].transmit_buffer_length) { //Turn off I2C IRq routine FIFOI2C2_isRunning = 0; //Reset transmit indexes FIFOI2C2_Devices_List[FIFOI2C2_currentDevice].transmit_buffer_current = 0; FIFOI2C2_Devices_List[FIFOI2C2_currentDevice].transmit_buffer_length = 0; //Check other devices now while (i < FIFOI2C2_DEVICES_COUNT) { //If not this device (since we already checked it) if (i != FIFOI2C2_currentDevice) { if(FIFOI2C2_Devices_List[i].transmit_buffer_length > FIFOI2C2_Devices_List[i].transmit_buffer_current) //If they have something to send { //Set device as the current device FIFOI2C2_currentDevice = i; //Turn back on FIFOI2C2 FIFOI2C2_isRunning = 1; //Kick-Start Operation INTSetFlag(INT_I2C2M); } } i++; } //Done so quit routine. return; } else { //Keep it running INTSetFlag(INT_I2C2M); } break; default: break; } //Increment next cmd to tx FIFOI2C2_Devices_List[FIFOI2C2_currentDevice].transmit_buffer_current++; } }