//********************************
//********************************
//********** 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))
Exemple #2
0
void PutChar(char ch) {
    if (getLength(transmitBuffer) != QUEUESIZE) {
        writeBack(transmitBuffer, ch);

        if (!INTGetEnable(INT_U1TX)) {
            INTEnable(INT_U1TX, INT_ENABLED);
            //INTSetFlag(INT_U1TX);
        }
        //        if (U1STAbits.TRMT) {
        //            INTEnable(INT_U1TX, INT_ENABLED);
        //            INTSetFlag(INT_U1TX);
        //        } else if (!INTGetEnable(INT_U1TX)) {
        //            INTEnable(INT_U1TX, INT_ENABLED);
        //            //INTSetFlag(INT_U1TX);
        //        }
    }
}
FIFOI2C2_RX_Byte FIFOI2C2_readQueue(uint16 device)
{
    int ind = 0;
    FIFOI2C2_RX_Byte rxb;

    //Checks for read-overflow error
    if (FIFOI2C2_Devices_List[device].receive_buffer_length >= (FIFOI2C2_RECEIVE_BUFFER_SIZE - 1))
    {
        //Sets the received byte to indicate an error
        rxb.device_command = FIFOI2C2_DEVICE_COMMAND_CMDERROR;
        rxb.rx_byte = 0;
    }
    else
    {
        //Sets the index to the current length of the receive buffer
        ind = FIFOI2C2_Devices_List[device].receive_buffer_current;
        //pulls the received byte to a local variable
        rxb.device_command = FIFOI2C2_Devices_List[device].receive_buffer[ind].device_command;
        rxb.rx_byte = FIFOI2C2_Devices_List[device].receive_buffer[ind].rx_byte;
        //increments how many bytes have been read by (for the device)
        FIFOI2C2_Devices_List[device].receive_buffer_current++;


        //If all the bytes have been read from the receive buffer reset the indexes
        if (FIFOI2C2_Devices_List[device].receive_buffer_current >= FIFOI2C2_Devices_List[device].receive_buffer_length)
        {
            //Sensitive Code. Disable temporarily master interrupt (if it's enabled.)
            if (INTGetEnable(INT_I2C2M) != 0)
            {
                INTEnable(INT_I2C2M, INT_DISABLED);
                FIFOI2C2_Devices_List[device].receive_buffer_length = 0;
                FIFOI2C2_Devices_List[device].receive_buffer_current = 0;
                INTEnable(INT_I2C2M, INT_ENABLED);
            }
            else
            {
                FIFOI2C2_Devices_List[device].receive_buffer_length = 0;
                FIFOI2C2_Devices_List[device].receive_buffer_current = 0;
            }

        }

        //Return the received byte
        return rxb;
    }
}
/*******************************************************************************
 * 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;
    }

}
Exemple #5
0
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
Exemple #6
0
//******************************************************************************
//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++];
        }
    }
}
Exemple #7
0
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

    }
	}
  //===========================================================
}
Exemple #8
0
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;
                }
            }
        }
    }
}