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