| 163 |
Kevin |
1 |
#include <xc.h>
|
|
|
2 |
#include "defines.h"
|
|
|
3 |
#include "base_UART.h"
|
|
|
4 |
|
|
|
5 |
static UART_DATA *uart_data_p;
|
|
|
6 |
|
|
|
7 |
void UART_Init(UART_DATA *data) {
|
|
|
8 |
uart_data_p = data;
|
|
|
9 |
|
|
|
10 |
UART_TX_TRIS = 0; // Tx pin set to output
|
|
|
11 |
#ifndef UART_TX_ONLY
|
|
|
12 |
UART_RX_TRIS = 1; // Rx pin set to input
|
|
|
13 |
#endif
|
|
|
14 |
LATAbits.LATA0 = 1; // Keep the line high at start
|
|
|
15 |
|
|
|
16 |
BAUDCONbits.BRG16 = 0; // 8-bit baud rate generator
|
|
|
17 |
SPBRGL = 25; // Set UART speed to 38400 baud
|
|
|
18 |
TXSTAbits.BRGH = 1; // High speed mode
|
|
|
19 |
|
|
|
20 |
TXSTAbits.SYNC = 0; // Async mode
|
|
|
21 |
RCSTAbits.SPEN = 1; // Serial port enable
|
|
|
22 |
|
|
|
23 |
TXSTAbits.TX9 = 0; // 8 bit transmission
|
|
|
24 |
|
|
|
25 |
TXSTAbits.TXEN = 1; // Transmission enabled
|
|
|
26 |
|
|
|
27 |
PIE1bits.TXIE = 0; // Disable TX interrupt
|
|
|
28 |
#ifndef UART_TX_ONLY
|
|
|
29 |
RCSTAbits.RX9 = 0; // 8 bit reception
|
|
|
30 |
RCSTAbits.CREN = 1; // Enables receiver
|
|
|
31 |
PIE1bits.RCIE = 1; // Enable RX interrupt
|
|
|
32 |
|
|
|
33 |
// Initialize the buffer that holds UART messages
|
|
|
34 |
uart_data_p->buffer_in_read_ind = 0;
|
|
|
35 |
uart_data_p->buffer_in_write_ind = 0;
|
|
|
36 |
uart_data_p->buffer_in_len = 0;
|
|
|
37 |
uart_data_p->buffer_in_len_tmp = 0;
|
|
|
38 |
#else
|
|
|
39 |
RCSTAbits.CREN = 0;
|
|
|
40 |
#endif
|
|
|
41 |
uart_data_p->buffer_out_ind = 0;
|
|
|
42 |
uart_data_p->buffer_out_len = 0;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
void UART_Send_Interrupt_Handler() {
|
|
|
46 |
// Put remaining data in TSR for transmit
|
|
|
47 |
if (uart_data_p->buffer_out_ind != uart_data_p->buffer_out_len) {
|
|
|
48 |
TXREG = uart_data_p->buffer_out[uart_data_p->buffer_out_ind];
|
|
|
49 |
uart_data_p->buffer_out_ind++;
|
|
|
50 |
} else {
|
|
|
51 |
while (!TXSTAbits.TRMT); // Wait for last byte to finish sending
|
|
|
52 |
PIE1bits.TXIE = 0;
|
|
|
53 |
uart_data_p->buffer_out_ind = 0;
|
|
|
54 |
uart_data_p->buffer_out_len = 0;
|
|
|
55 |
}
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
void UART_Write(const char *string, char length) {
|
|
|
59 |
while (PIE1bits.TXIE); // Wait for previous message to finish sending
|
|
|
60 |
uart_data_p->buffer_out_len = length;
|
|
|
61 |
uart_data_p->buffer_out_ind = 1;
|
|
|
62 |
for (char i = 0; i < length; i++) {
|
|
|
63 |
uart_data_p->buffer_out[i] = string[i];
|
|
|
64 |
}
|
|
|
65 |
TXREG = uart_data_p->buffer_out[0]; // Put first byte in TSR
|
|
|
66 |
PIE1bits.TXIE = 1;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
void UART_WriteD(const char* string, char length) {
|
|
|
70 |
PIE1bits.TXIE = 1;
|
|
|
71 |
for (char i = 0; i < length; i++) {
|
|
|
72 |
TXREG = string[i];
|
|
|
73 |
NOP();
|
|
|
74 |
while (!PIR1bits.TXIF);
|
|
|
75 |
}
|
|
|
76 |
PIE1bits.TXIE = 0;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
#ifndef UART_TX_ONLY
|
|
|
80 |
void UART_Recv_Interrupt_Handler() {
|
|
|
81 |
if (PIR1bits.RCIF) { // Check if data receive flag is set
|
|
|
82 |
char c = RCREG;
|
|
|
83 |
|
|
|
84 |
// Save received data into buffer
|
|
|
85 |
uart_data_p->buffer_in[uart_data_p->buffer_in_write_ind] = c;
|
|
|
86 |
if (uart_data_p->buffer_in_write_ind == MAXUARTBUF - 1) {
|
|
|
87 |
uart_data_p->buffer_in_write_ind = 0;
|
|
|
88 |
} else {
|
|
|
89 |
uart_data_p->buffer_in_write_ind++;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
// Store the last MAXUARTBUF values entered
|
|
|
93 |
if (uart_data_p->buffer_in_len_tmp < MAXUARTBUF) {
|
|
|
94 |
uart_data_p->buffer_in_len_tmp++;
|
|
|
95 |
} else {
|
|
|
96 |
if (uart_data_p->buffer_in_read_ind == MAXUARTBUF - 1) {
|
|
|
97 |
uart_data_p->buffer_in_read_ind = 0;
|
|
|
98 |
} else {
|
|
|
99 |
uart_data_p->buffer_in_read_ind++;
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
// Update buffer size upon receiving newline (0x0D)
|
|
|
104 |
if (c == UART_BREAK_CHAR) {
|
|
|
105 |
uart_data_p->buffer_in_len = uart_data_p->buffer_in_len_tmp;
|
|
|
106 |
uart_data_p->buffer_in_len_tmp = 0;
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
if (RCSTAbits.OERR == 1) {
|
|
|
111 |
// We've overrun the USART and must reset
|
|
|
112 |
TXSTAbits.TXEN = 0; // Kill anything currently sending
|
|
|
113 |
RCSTAbits.CREN = 0; // Reset UART1
|
|
|
114 |
RCSTAbits.CREN = 1;
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
char UART_Buffer_Len() {
|
|
|
119 |
return uart_data_p->buffer_in_len;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/* Reader interface to the UART buffer, returns the number of bytes read */
|
|
|
123 |
char UART_Read_Buffer(char *buffer) {
|
|
|
124 |
char i = 0;
|
|
|
125 |
while (uart_data_p->buffer_in_len != 0) {
|
|
|
126 |
buffer[i] = uart_data_p->buffer_in[uart_data_p->buffer_in_read_ind];
|
|
|
127 |
i++;
|
|
|
128 |
if (uart_data_p->buffer_in_read_ind == MAXUARTBUF - 1) {
|
|
|
129 |
uart_data_p->buffer_in_read_ind = 0;
|
|
|
130 |
} else {
|
|
|
131 |
uart_data_p->buffer_in_read_ind++;
|
|
|
132 |
}
|
|
|
133 |
uart_data_p->buffer_in_len--;
|
|
|
134 |
}
|
|
|
135 |
return i;
|
|
|
136 |
}
|
|
|
137 |
#endif
|