Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
113 Kevin 1
#include "maindefs.h"
2
#include "msg_queues.h"
3
#include <usart.h>
4
#include "uart.h"
5
#include "xbee.h"
6
 
7
void uart_init() {
8
    // Configure the hardware USART device
9
 
10
    // UART1 TX RC6
11
    // UART1 RX RC7
12
    Open1USART(USART_TX_INT_OFF &   // Interrupt on TX off
13
            USART_RX_INT_OFF &      // Interrupt on RX on
14
            USART_ASYNCH_MODE &     // Operate in async mode
15
            USART_EIGHT_BIT &       // Operate in 8-bit mode
16
            USART_CONT_RX &         // Continuously recieve messages
17
            USART_BRGH_HIGH, 64);  // Set UART speed to 38400 baud
18
 
19
    // UART2 RX Pin RP5
20
    RPINR16 = 5;    // 5 is PPS RP5
21
    // UART2 TX Pin RP6
22
    RPOR6 = 5;  // 5 is TX2/CK2 (EUSART2 Asynchronous Transmit/Asynchronous Clock Output)
23
 
24
    Open2USART(USART_TX_INT_OFF &   // Interrupt on TX off
25
            USART_RX_INT_ON &      // Interrupt on RX on
26
            USART_ASYNCH_MODE &     // Operate in async mode
27
            USART_EIGHT_BIT &       // Operate in 8-bit mode
28
            USART_CONT_RX &         // Continuously recieve messages
29
            USART_BRGH_HIGH, 64);  // Set UART speed to 115200 baud
30
}
31
 
32
void uart_recv_interrupt_handler() {
33
    if (DataRdy2USART()) {
34
        xbee_read_serial(Read2USART());
35
    }
36
 
37
    if (USART2_Status.OVERRUN_ERROR == 1) {
38
        // We've overrun the USART and must reset
39
        RCSTA1bits.CREN = 0;
40
        RCSTA1bits.CREN = 1;
41
        RCSTA2bits.CREN = 0;
42
        RCSTA2bits.CREN = 1;
43
        MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_OVERRUN, (void *) 0);
44
    }
45
}