Exemplo n.º 1
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);
        }
    }
}
Exemplo n.º 2
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

    }
	}
  //===========================================================
}