Subversion Repositories Code-Repo

Rev

Rev 113 | Details | Compare with Previous | 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
114 Kevin 9
#ifdef _BASE_STATION
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, 51);  // Set UART speed to 38400 baud (32MHz)
113 Kevin 18
 
114 Kevin 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, 51);  // Set UART speed to 38400 baud (32MHz)
30
#endif
31
#ifdef _REMOTE
113 Kevin 32
    // UART1 TX RC6
33
    // UART1 RX RC7
34
    Open1USART(USART_TX_INT_OFF &   // Interrupt on TX off
35
            USART_RX_INT_OFF &      // Interrupt on RX on
36
            USART_ASYNCH_MODE &     // Operate in async mode
37
            USART_EIGHT_BIT &       // Operate in 8-bit mode
38
            USART_CONT_RX &         // Continuously recieve messages
39
            USART_BRGH_HIGH, 64);  // Set UART speed to 38400 baud
40
 
41
    // UART2 RX Pin RP5
42
    RPINR16 = 5;    // 5 is PPS RP5
43
    // UART2 TX Pin RP6
44
    RPOR6 = 5;  // 5 is TX2/CK2 (EUSART2 Asynchronous Transmit/Asynchronous Clock Output)
45
 
46
    Open2USART(USART_TX_INT_OFF &   // Interrupt on TX off
47
            USART_RX_INT_ON &      // Interrupt on RX on
48
            USART_ASYNCH_MODE &     // Operate in async mode
49
            USART_EIGHT_BIT &       // Operate in 8-bit mode
50
            USART_CONT_RX &         // Continuously recieve messages
114 Kevin 51
            USART_BRGH_HIGH, 64);  // Set UART speed to 38400 baud
52
#endif
113 Kevin 53
}
54
 
55
void uart_recv_interrupt_handler() {
56
    if (DataRdy2USART()) {
57
        xbee_read_serial(Read2USART());
58
    }
59
 
60
    if (USART2_Status.OVERRUN_ERROR == 1) {
61
        // We've overrun the USART and must reset
62
        RCSTA1bits.CREN = 0;
63
        RCSTA1bits.CREN = 1;
64
        RCSTA2bits.CREN = 0;
65
        RCSTA2bits.CREN = 1;
66
        MQ_sendmsg_ToMainFromHigh(0, MSGTYPE_OVERRUN, (void *) 0);
67
    }
68
}