-
Notifications
You must be signed in to change notification settings - Fork 0
/
EUSART.c
140 lines (119 loc) · 3.56 KB
/
EUSART.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* 20200104.033
* ECG-TFT
*
* File: eusart.c
* Processor: PIC18F2455
* Author: wizlab.it
*/
#include "EUSART.h"
void EUSART_Init(void) {
//TX Register
TXSTAbits.TX9 = 0; //8-bit TX
TXSTAbits.TXEN = 1; //Enable TX
TXSTAbits.SYNC = 0; //Asynchronous
TXSTAbits.SENDB = 0; //Sync Break transmission completed
TXSTAbits.BRGH = 1; //High baud rate
//RX Register
RCSTAbits.SPEN = 1; //Enable EUSART
RCSTAbits.RX9 = 0; //8-bit RX
RCSTAbits.CREN = 1; //Enable RX
//Baud rate generator
BAUDCONbits.BRG16 = 1; //16-bit baud rate generator
BAUDCONbits.WUE = 0; //No wake-up
BAUDCONbits.ABDEN = 0; //No auto baud detect
SPBRGH = 0x04; //Set baud rate (9600bps)
SPBRG = 0xE1; //Set baud rate (9600bps)
//Interrupts
RCIE = 1; //EUSART RX
//Init RX Buffer
EUSART_RX.iRead = 0;
EUSART_RX.iWrite = 0;
memset(EUSART_RX.buffer, 0x00, EUSART_BUFFER_SIZE);
}
void EUSART_BaudRateSet(const uint32_t baudRate) {
TXSTAbits.SYNC = 0; //Asynchronous
TXSTAbits.BRGH = 1; //High baud rate
BAUDCONbits.BRG16 = 1; //16-bit baud rate generator
uint32_t tmp = (_XTAL_FREQ / baudRate);
tmp = tmp / 4;
tmp--;
SPBRG = (uint8_t) tmp;
SPBRGH = (uint8_t) (tmp >> 8);
return;
}
void EUSART_Interrupt(void) {
uint8_t c = RCREG;
if(OERR == 1) {
CREN = 0;
CREN = 1;
}
EUSART_RX.iWrite++;
if(EUSART_RX.iWrite == EUSART_BUFFER_SIZE) EUSART_RX.iWrite = 0;
EUSART_RX.buffer[EUSART_RX.iWrite] = c;
return;
}
void EUSART_Flush(void) {
EUSART_RX.iRead = EUSART_RX.iWrite;
}
void EUSART_Trim(uint8_t *arr) {
uint8_t len = strlen(arr);
while(1) {
len--;
if((arr[len] == '\r') || (arr[len] == '\n')) {
arr[len] = 0x00;
} else {
break;
}
}
}
void EUSART_SendByte(uint8_t b) {
while(TXIF == 0) { }
TXREG = b;
__asm("nop");
}
void EUSART_SendByteArray(const uint8_t *arr, uint8_t len) {
while(len--) {
EUSART_SendByte(*arr++);
}
}
uint8_t EUSART_Available(int16_t timeout) {
if(timeout == EUSART_USE_DEFAULT_TIMEOUT) timeout = EUSART_TIMEOUT_DEFAULT;
uint32_t t = MILLISECONDS + timeout;
while(EUSART_RX.iRead == EUSART_RX.iWrite) {
if(t < MILLISECONDS) return EUSART_RX_AVAILABLE_NO;
}
return EUSART_RX_AVAILABLE_YES;
}
int16_t EUSART_PeekByte(int16_t timeout) {
if(EUSART_Available(timeout) == EUSART_RX_AVAILABLE_NO) return EUSART_RX_NODATA;
uint16_t iPeek = EUSART_RX.iRead + 1;
if(iPeek == EUSART_BUFFER_SIZE) iPeek = 0;
return EUSART_RX.buffer[iPeek];
}
int16_t EUSART_ReadByte(int16_t timeout) {
if(EUSART_Available(timeout) == EUSART_RX_AVAILABLE_NO) return EUSART_RX_NODATA;
EUSART_RX.iRead++;
if(EUSART_RX.iRead == EUSART_BUFFER_SIZE) EUSART_RX.iRead = 0;
return EUSART_RX.buffer[EUSART_RX.iRead];
}
uint8_t EUSART_ReadLine(char *line, uint8_t len, int16_t timeout) {
line[0] = 0x00;
len--;
//Read data
int16_t b;
uint8_t iLine = 0;
while(b = EUSART_ReadByte(timeout)) {
if(b == EUSART_RX_NODATA) break;
line[iLine++] = b;
if(iLine == len) break;
if(b == '\r') {
if(EUSART_PeekByte(timeout) == '\n') {
line[iLine++] = EUSART_ReadByte(timeout);
break;
}
}
}
line[iLine] = 0x00;
return iLine;
}