예제 #1
0
/*******************************************************************************
 * FUNCTION: vUART2FlushRxBuffer
 *
 * PARAMETERS:
 * ~ void
 *
 * RETURN:
 * ~ void
 *
 * DESCRIPTIONS:
 * Flush all the data in the Rx buffer.
 *
 *******************************************************************************/
void vUART2FlushRxBuffer(void)
{
    unsigned char ucReceivedData;

    unsigned int iStatus = INTDisableInterrupts();
    
    prv_xRx.uiDataCount = 0;
    prv_xRx.uiReadPt = 0;
    prv_xRx.uiWritePt = 0;
    
    // Discard all data available in the UART receive buffer.
    while (UARTReceivedDataIsAvailable(UART2)) {
        // Read the received data.
        ucReceivedData = UARTGetDataByte(UART2);
    }

    // Clear the overrun flag.
    if (UARTGetLineStatus(UART2) & UART_OVERRUN_ERROR) {
        U2STAbits.OERR = 0;
    }

    // Clear the semaphore.
    xSemaphoreTake(xBluetoothRxSemaphore, 0);

    INTRestoreInterrupts(iStatus);
}
예제 #2
0
/*******************************************************************************
 * 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;
    }

}
예제 #3
0
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);
        }
    }
}
예제 #4
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