| 212 |
Kevin |
1 |
#include <xc.h>
|
|
|
2 |
#include <plib.h>
|
|
|
3 |
#include "defines.h"
|
|
|
4 |
#include "UART1.h"
|
|
|
5 |
|
|
|
6 |
static UART1_DATA *uart_data_ptr;
|
|
|
7 |
|
|
|
8 |
void UART1_Init(UART1_DATA *data, void (*rx_callback)(char)) {
|
|
|
9 |
uart_data_ptr = data;
|
|
|
10 |
uart_data_ptr->rx_callback = rx_callback;
|
|
|
11 |
uart_data_ptr->buffer_out_len = 0;
|
|
|
12 |
uart_data_ptr->buffer_out_ind = 0;
|
|
|
13 |
|
|
|
14 |
INTDisableInterrupts();
|
|
|
15 |
|
|
|
16 |
IEC0CLR = 0x1C000000; // Disable all UART1 interrupts
|
|
|
17 |
IFS0CLR = 0x1C000000; // Clear any existing events
|
|
|
18 |
IPC6SET = 0x00000009; // Set Priority = 2, Subpriority = 1
|
|
|
19 |
|
|
|
20 |
U1MODE = 0x00008000; // UART enabled, BRGH = 0
|
|
|
21 |
U1STA = 0x00009400; // TX interrupt on buffer empty, RX interrupt on buffer not empty
|
|
|
22 |
U1BRG = 19; // Set baud rate to 256000 @ 80MHz
|
|
|
23 |
IEC0SET = 0x0C000000; // Enable the RX and Error interrupts
|
|
|
24 |
|
|
|
25 |
INTEnableInterrupts();
|
|
|
26 |
|
|
|
27 |
TRISDbits.TRISD14 = 0;
|
|
|
28 |
PORTDbits.RD14 = 0;
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
int UART1_Write(char *string, int length) {
|
|
|
32 |
if (length > UART1_BUFFER_SIZE)
|
|
|
33 |
return 0;
|
|
|
34 |
if (uart_data_ptr->buffer_out_len != 0)
|
|
|
35 |
return 0;
|
|
|
36 |
|
|
|
37 |
// Put the data to send into the outbound buffer
|
|
|
38 |
uart_data_ptr->buffer_out_len = length;
|
|
|
39 |
uart_data_ptr->buffer_out_ind = 0;
|
|
|
40 |
int i;
|
|
|
41 |
for (i = 0; i < length; i++) {
|
|
|
42 |
uart_data_ptr->buffer_out[i] = string[i];
|
|
|
43 |
}
|
|
|
44 |
IEC0SET = 0x10000000; // Enable TX interrupt
|
|
|
45 |
return 1;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
void __ISR(_UART_1_VECTOR, ipl2) __UART_1_Interrupt_Handler(void) {
|
|
|
49 |
PORTDbits.RD14 = 1;
|
|
|
50 |
// Process UART1 error flag
|
|
|
51 |
if (IFS0bits.U1EIF) {
|
|
|
52 |
if (U1STAbits.PERR) { // Process parity error
|
|
|
53 |
|
|
|
54 |
}
|
|
|
55 |
if (U1STAbits.FERR) { // Process frame error
|
|
|
56 |
|
|
|
57 |
}
|
|
|
58 |
if (U1STAbits.OERR) { // Process receive buffer overrun error
|
|
|
59 |
U1STAbits.OERR = 0; // Clear the overrun error if set
|
|
|
60 |
}
|
|
|
61 |
IFS0CLR = 0x04000000; // Clear the error flag
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
// Process UART1 recieve flag
|
|
|
65 |
if (IFS0bits.U1RXIF) {
|
|
|
66 |
// Read the data received from the last transfer
|
|
|
67 |
while (U1STAbits.URXDA) {
|
|
|
68 |
char c = U1RXREG;
|
|
|
69 |
// Call the RX callback function on each received data
|
|
|
70 |
if (uart_data_ptr->rx_callback != NULL) {
|
|
|
71 |
(*uart_data_ptr->rx_callback)(c);
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
IFS0CLR = 0x08000000; // Clear the recieve flag
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
// Process UART1 transmit flag
|
|
|
78 |
if (IFS0bits.U1TXIF) {
|
|
|
79 |
// Disable the transmit interrupt if all data has been sent
|
|
|
80 |
if (uart_data_ptr->buffer_out_ind == uart_data_ptr->buffer_out_len) {
|
|
|
81 |
IEC0CLR = 0x10000000;
|
|
|
82 |
uart_data_ptr->buffer_out_len = 0;
|
|
|
83 |
} else {
|
|
|
84 |
// Start filling the transmit buffer
|
|
|
85 |
while (!U1STAbits.UTXBF) {
|
|
|
86 |
U1TXREG = uart_data_ptr->buffer_out[uart_data_ptr->buffer_out_ind];
|
|
|
87 |
uart_data_ptr->buffer_out_ind++;
|
|
|
88 |
if (uart_data_ptr->buffer_out_ind == uart_data_ptr->buffer_out_len)
|
|
|
89 |
break;
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
IFS0CLR = 0x10000000; // Clear the transmit flag
|
|
|
93 |
}
|
|
|
94 |
PORTDbits.RD14 = 0;
|
|
|
95 |
}
|