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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
/*
* Host communication
*
* Copyright (C) 2013 Michael Buesch <m@bues.ch>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "comm.h"
#include <string.h>
#include <util/crc16.h>
#include <avr/io.h>
#include <avr/cpufunc.h>
struct rx_context {
struct comm_message queue[COMM_RX_QUEUE_SIZE];
uint8_t in_ptr;
uint8_t out_ptr;
uint8_t count;
uint8_t byte_ptr;
uint16_t timeout;
};
struct tx_context {
struct comm_message queue[COMM_TX_QUEUE_SIZE];
uint8_t in_ptr;
uint8_t out_ptr;
uint8_t count;
uint8_t byte_ptr;
uint8_t seq_count;
};
static struct rx_context rx;
static struct tx_context tx;
static void comm_reset(void)
{
memset(&rx, 0, sizeof(rx));
memset(&tx, 0, sizeof(tx));
}
static inline uint16_t to_little_endian_16(uint16_t v)
{
union {
uint16_t le;
uint8_t b[2];
} u = {
.b = { v & 0xFF, v >> 8, },
};
return u.le;
}
static comm_crc_t message_calc_crc(const struct comm_message *msg)
{
const uint8_t *data = (const uint8_t *)msg;
uint16_t crc = 0xFFFF, len;
comm_crc_t ret;
len = sizeof(*msg) - COMM_FCS_LEN;
do {
crc = _crc16_update(crc, *data++);
} while (--len);
crc ^= 0xFFFF;
ret = to_little_endian_16(crc);
return ret;
}
static void tx_try_put_next_byte(void)
{
const struct comm_message *msg;
const uint8_t *buf;
uint8_t data;
if (tx.count == 0)
return;
if (!(UCSRA & (1 << UDRE)))
return;
msg = &tx.queue[tx.out_ptr];
buf = (const uint8_t *)msg;
data = buf[tx.byte_ptr];
tx.byte_ptr++;
if (tx.byte_ptr >= sizeof(struct comm_message)) {
tx.byte_ptr = 0;
tx.out_ptr = (tx.out_ptr + 1) & COMM_TX_QUEUE_MASK;
tx.count--;
if (tx.count == 0)
UCSRB &= ~(1 << UDRIE);
}
UDR = data;
}
ISR(USART_UDRE_vect)
{
tx_try_put_next_byte();
}
void comm_drain_tx_queue(void)
{
uint8_t sreg;
sreg = irq_disable_save();
while (tx.count)
tx_try_put_next_byte();
irq_restore(sreg);
}
/* Called with IRQs disabled. */
static void handle_tx_queue_overflow(struct comm_message *msg,
bool may_enable_irqs)
{
/* TX queue is full. Notify the overflow condition
* to the serial control, once we get the message out. */
comm_msg_set_err(msg, COMM_ERR_Q);
msg->fcs = message_calc_crc(msg);
/* Manually push TX to get things going. */
do {
tx_try_put_next_byte();
if (may_enable_irqs) {
/* IRQs were enabled before we were called.
* Be nice to other interrupts. */
irq_enable();
_NOP();
irq_disable();
}
} while (tx.count >= COMM_TX_QUEUE_SIZE);
}
static uint8_t uart_rx(uint8_t *data_buf)
{
uint8_t status, data;
status = UCSRA;
if (!(status & (1 << RXC)))
return 0;
data = UDR;
if (data_buf)
*data_buf = data;
if (status & ((1 << FE) | (1 << PE) | (1 << DOR)))
return 2;
return 1;
}
void comm_message_send(struct comm_message *msg, uint8_t dest_addr)
{
uint8_t sreg;
comm_msg_set_da(msg, dest_addr);
sreg = irq_disable_save();
msg->seq = tx.seq_count++;
msg->fcs = message_calc_crc(msg);
if (tx.count >= COMM_TX_QUEUE_SIZE)
handle_tx_queue_overflow(msg, __irqs_enabled(sreg));
memcpy(&tx.queue[tx.in_ptr], msg, sizeof(*msg));
tx.in_ptr = (tx.in_ptr + 1) & COMM_TX_QUEUE_MASK;
tx.count++;
UCSRB |= (1 << UDRIE);
tx_try_put_next_byte();
irq_restore(sreg);
}
static void handle_rx(const struct comm_message *msg)
{
COMM_MSG(reply);
comm_crc_t crc;
bool ok;
if (comm_msg_da(msg) != COMM_LOCAL_ADDRESS) {
/* The message was not for us. */
return;
}
crc = message_calc_crc(msg);
if (crc != msg->fcs) {
/* CRC mismatch. */
comm_msg_set_err(&reply, COMM_ERR_FCS);
goto ack;
}
if (msg->fc & COMM_FC_RESET) {
comm_reset();
comm_msg_set_err(&reply, COMM_ERR_OK);
goto ack;
}
ok = comm_handle_rx_message(msg, comm_payload(&reply));
if (!ok) {
comm_msg_set_err(&reply, COMM_ERR_FAIL);
goto ack;
}
ack:
if (msg->fc & COMM_FC_REQ_ACK) {
reply.fc |= COMM_FC_ACK;
comm_message_send(&reply, comm_msg_sa(msg));
}
}
/* RX interrupt */
ISR(USART_RXC_vect)
{
uint8_t res, data, *rxbuf;
while (1) {
res = uart_rx(&data);
if (!res)
return;
if (rx.count >= COMM_RX_QUEUE_SIZE) {
/* Queue overflow. */
continue;//TODO
}
rxbuf = (uint8_t *)&rx.queue[rx.in_ptr];
rxbuf[rx.byte_ptr] = data;
rx.byte_ptr++;
if (rx.byte_ptr >= sizeof(struct comm_message)) {
rx.byte_ptr = 0;
rx.in_ptr = (rx.in_ptr + 1) & COMM_RX_QUEUE_MASK;
rx.timeout = 0;
mb();
rx.count++;
}
}
}
void comm_centisecond_tick(void)
{
uint8_t sreg;
sreg = irq_disable_save();
if (rx.byte_ptr > 0)
rx.timeout++;
if (rx.timeout > 50 /* 0.5 seconds */) {
/* Timeout! Reset the RX buffer. */
rx.byte_ptr = 0;
rx.timeout = 0;
}
irq_restore(sreg);
}
void comm_work(void)
{
uint8_t sreg;
mb();
if (rx.count) {
handle_rx(&rx.queue[rx.out_ptr]);
rx.out_ptr = (rx.out_ptr + 1) & COMM_RX_QUEUE_MASK;
sreg = irq_disable_save();
rx.count--;
irq_restore(sreg);
}
}
#define USE_2X (((uint64_t)F_CPU % (8ull * COMM_BAUDRATE)) < \
((uint64_t)F_CPU % (16ull * COMM_BAUDRATE)))
#define UBRRVAL ((uint64_t)F_CPU / ((USE_2X ? 8ull : 16ull) * COMM_BAUDRATE))
static void uart_init(void)
{
/* Set baud rate */
UBRRL = UBRRVAL & 0xFF;
UBRRH = (UBRRVAL >> 8) & 0xFF & ~(1 << URSEL);
UCSRA = (!!(USE_2X) << U2X);
/* 8 data bits, 1 stop bit, No parity */
UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
/* Enable transceiver and RX IRQs */
UCSRB = (1 << RXEN) | (1 << TXEN) | (1 << RXCIE);
/* Drain the RX buffer */
while (uart_rx(NULL))
mb();
}
void comm_init(void)
{
comm_reset();
uart_init();
}
|