-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.c
120 lines (98 loc) · 2.64 KB
/
serial.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
/*
* This file is part of doorduino.
*
* doorduino is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or(at your option) any later version.
*
* doorduino is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with doorduino. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SERIAL_INBUF
#define SERIAL_INBUF 128
#endif
#ifndef SERIAL_OUTBUF
#define SERIAL_OUTBUF 128
#endif
#define serial_init(baud, mode)\
serial_baud_##baud();\
serial_mode_##mode();\
serial_receiver_enable();\
serial_transmitter_enable();\
serial_interrupt_rx_enable()
static volatile struct {
uint8_t buf[SERIAL_INBUF];
uint8_t start;
uint8_t end;
} serial_input;
static volatile struct {
uint8_t buf[SERIAL_OUTBUF];
uint8_t start;
uint8_t end;
} serial_output;
serial_interrupt_rx()
{
uint8_t end = serial_input.end;
serial_input.buf[end] = serial_read();
serial_input.end = (end + 1) & (SERIAL_INBUF - 1);
}
serial_interrupt_dre()
{
uint8_t start = serial_output.start;
if (start == serial_output.end)
serial_interrupt_dre_disable();
else {
serial_write(serial_output.buf[start]);
serial_output.start = (start + 1) & (SERIAL_OUTBUF - 1);
}
}
static uint8_t
serial_available()
{
return serial_input.start != serial_input.end;
}
static char
serial_getchar()
{
uint8_t start = serial_input.start;
char r;
if (start == serial_input.end)
return '\0';
r = (char)serial_input.buf[start];
serial_input.start = (start + 1) & (SERIAL_INBUF - 1);
return r;
}
static void __attribute__((unused))
serial_print(const char *str)
{
uint8_t end = serial_output.end;
while ((serial_output.buf[end] = *str++))
end = (end + 1) & (SERIAL_OUTBUF - 1);
serial_output.end = end;
serial_interrupt_dre_enable();
}
static char serial_hexdigit[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
static void __attribute__((unused))
serial_hexdump(const void *data, uint8_t len)
{
const uint8_t *p = data;
uint8_t end = serial_output.end;
for (; len > 0; len--, p++) {
uint8_t c = *p;
serial_output.buf[end] = serial_hexdigit[c >> 4];
end = (end + 1) & (SERIAL_OUTBUF - 1);
serial_output.buf[end] = serial_hexdigit[c & 0x0F];
end = (end + 1) & (SERIAL_OUTBUF - 1);
}
serial_output.end = end;
serial_interrupt_dre_enable();
}