Subversion Repositories Code-Repo

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include "maindefs.h"
#include "msg_queues.h"
#include <usart.h>
#include "uart.h"
#include "xbee.h"

void uart_init() {
    // Configure the hardware USART device

    // UART1 TX RC6
    // UART1 RX RC7
    Open1USART(USART_TX_INT_OFF &   // Interrupt on TX off
            USART_RX_INT_OFF &      // Interrupt on RX on
            USART_ASYNCH_MODE &     // Operate in async mode
            USART_EIGHT_BIT &       // Operate in 8-bit mode
            USART_CONT_RX &         // Continuously recieve messages
            USART_BRGH_HIGH, 64);  // Set UART speed to 38400 baud

    // UART2 RX Pin RP5
    RPINR16 = 5;    // 5 is PPS RP5
    // UART2 TX Pin RP6
    RPOR6 = 5;  // 5 is TX2/CK2 (EUSART2 Asynchronous Transmit/Asynchronous Clock Output)

    Open2USART(USART_TX_INT_OFF &   // Interrupt on TX off
            USART_RX_INT_ON &      // Interrupt on RX on
            USART_ASYNCH_MODE &     // Operate in async mode
            USART_EIGHT_BIT &       // Operate in 8-bit mode
            USART_CONT_RX &         // Continuously recieve messages
            USART_BRGH_HIGH, 64);  // Set UART speed to 115200 baud
}

void uart_recv_interrupt_handler() {
    if (DataRdy2USART()) {
        xbee_read_serial(Read2USART());
    }

    if (USART2_Status.OVERRUN_ERROR == 1) {
        // We've overrun the USART and must reset
        RCSTA1bits.CREN = 0;
        RCSTA1bits.CREN = 1;
        RCSTA2bits.CREN = 0;
        RCSTA2bits.CREN = 1;
        MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_OVERRUN, (void *) 0);
    }
}