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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
// Generic handling of serial over CAN support
//
// Copyright (C) 2019 Eug Krashtan <eug.krashtan@gmail.com>
// Copyright (C) 2020 Pontus Borg <glpontus@gmail.com>
// Copyright (C) 2021-2025 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
#include <string.h> // memcpy
#include "autoconf.h" // CONFIG_HAVE_BOOTLOADER_REQUEST
#include "board/io.h" // readb
#include "board/irq.h" // irq_save
#include "board/misc.h" // console_sendf
#include "canbus.h" // canbus_send
#include "canserial.h" // canserial_notify_tx
#include "command.h" // DECL_CONSTANT
#include "fasthash.h" // fasthash64
#include "sched.h" // sched_wake_task
#define CANBUS_UUID_LEN 6
// Global storage
static struct canbus_data {
uint32_t assigned_id;
uint8_t uuid[CANBUS_UUID_LEN];
// Tx data
struct task_wake tx_wake;
uint8_t transmit_pos, transmit_max;
// Rx data
struct task_wake rx_wake;
uint8_t receive_pos;
uint32_t admin_pull_pos, admin_push_pos;
// Transfer buffers
struct canbus_msg admin_queue[8];
uint8_t transmit_buf[96];
uint8_t receive_buf[192];
} CanData;
/****************************************************************
* Data transmission over CAN
****************************************************************/
void
canserial_notify_tx(void)
{
sched_wake_task(&CanData.tx_wake);
}
void
canserial_tx_task(void)
{
if (!sched_check_wake(&CanData.tx_wake))
return;
uint32_t id = CanData.assigned_id;
if (!id) {
CanData.transmit_pos = CanData.transmit_max = 0;
return;
}
struct canbus_msg msg;
msg.id = id + 1;
uint32_t tpos = CanData.transmit_pos, tmax = CanData.transmit_max;
for (;;) {
int avail = tmax - tpos, now = avail > 8 ? 8 : avail;
if (avail <= 0)
break;
msg.dlc = now;
memcpy(msg.data, &CanData.transmit_buf[tpos], now);
int ret = canbus_send(&msg);
if (ret <= 0)
break;
tpos += now;
}
CanData.transmit_pos = tpos;
}
DECL_TASK(canserial_tx_task);
// Encode and transmit a "response" message
void
console_sendf(const struct command_encoder *ce, va_list args)
{
// Verify space for message
uint32_t tpos = CanData.transmit_pos, tmax = CanData.transmit_max;
if (tpos >= tmax)
CanData.transmit_pos = CanData.transmit_max = tpos = tmax = 0;
uint32_t max_size = ce->max_size;
if (tmax + max_size > sizeof(CanData.transmit_buf)) {
if (tmax + max_size - tpos > sizeof(CanData.transmit_buf))
// Not enough space for message
return;
// Move buffer
tmax -= tpos;
memmove(&CanData.transmit_buf[0], &CanData.transmit_buf[tpos], tmax);
CanData.transmit_pos = tpos = 0;
CanData.transmit_max = tmax;
}
// Generate message
uint32_t msglen = command_encode_and_frame(&CanData.transmit_buf[tmax]
, ce, args);
// Start message transmit
CanData.transmit_max = tmax + msglen;
canserial_notify_tx();
}
/****************************************************************
* CAN "admin" command handling
****************************************************************/
// Available commands and responses
#define CANBUS_CMD_QUERY_UNASSIGNED 0x00
#define CANBUS_CMD_SET_KUTTER_NODEID 0x01
#define CANBUS_CMD_REQUEST_BOOTLOADER 0x02
#define CANBUS_RESP_NEED_NODEID 0x20
// Helper to verify a UUID in a command matches this chip's UUID
static int
can_check_uuid(struct canbus_msg *msg)
{
return (msg->dlc >= 7
&& memcmp(&msg->data[1], CanData.uuid, sizeof(CanData.uuid)) == 0);
}
// Helpers to encode/decode a CAN identifier to a 1-byte "nodeid"
static int
can_get_nodeid(void)
{
if (!CanData.assigned_id)
return 0;
return (CanData.assigned_id - 0x100) >> 1;
}
static uint32_t
can_decode_nodeid(int nodeid)
{
return (nodeid << 1) + 0x100;
}
static void
can_process_query_unassigned(struct canbus_msg *msg)
{
if (CanData.assigned_id)
return;
struct canbus_msg send;
send.id = CANBUS_ID_ADMIN_RESP;
send.dlc = 8;
send.data[0] = CANBUS_RESP_NEED_NODEID;
memcpy(&send.data[1], CanData.uuid, sizeof(CanData.uuid));
send.data[7] = CANBUS_CMD_SET_KUTTER_NODEID;
// Send with retry
for (;;) {
int ret = canbus_send(&send);
if (ret >= 0)
return;
}
}
static void
can_id_conflict(void)
{
CanData.assigned_id = 0;
canbus_set_filter(CanData.assigned_id);
shutdown("Another CAN node assigned this ID");
}
static void
can_process_set_kutter_nodeid(struct canbus_msg *msg)
{
if (msg->dlc < 8)
return;
uint32_t newid = can_decode_nodeid(msg->data[7]);
if (can_check_uuid(msg)) {
if (newid != CanData.assigned_id) {
CanData.assigned_id = newid;
canbus_set_filter(CanData.assigned_id);
}
} else if (newid == CanData.assigned_id) {
can_id_conflict();
}
}
static void
can_process_request_bootloader(struct canbus_msg *msg)
{
if (!CONFIG_HAVE_BOOTLOADER_REQUEST || !can_check_uuid(msg))
return;
bootloader_request();
}
// Handle an "admin" command
static void
can_process_admin(struct canbus_msg *msg)
{
if (!msg->dlc)
return;
switch (msg->data[0]) {
case CANBUS_CMD_QUERY_UNASSIGNED:
can_process_query_unassigned(msg);
break;
case CANBUS_CMD_SET_KUTTER_NODEID:
can_process_set_kutter_nodeid(msg);
break;
case CANBUS_CMD_REQUEST_BOOTLOADER:
can_process_request_bootloader(msg);
break;
}
}
/****************************************************************
* CAN packet reading
****************************************************************/
static void
canserial_notify_rx(void)
{
sched_wake_task(&CanData.rx_wake);
}
DECL_CONSTANT("RECEIVE_WINDOW", ARRAY_SIZE(CanData.receive_buf));
// Handle incoming data (called from IRQ handler)
void
canserial_process_data(struct canbus_msg *msg)
{
uint32_t id = msg->id;
if (CanData.assigned_id && id == CanData.assigned_id) {
// Add to incoming data buffer
int rpos = CanData.receive_pos;
uint32_t len = CANMSG_DATA_LEN(msg);
if (len > sizeof(CanData.receive_buf) - rpos)
return;
memcpy(&CanData.receive_buf[rpos], msg->data, len);
CanData.receive_pos = rpos + len;
canserial_notify_rx();
} else if (id == CANBUS_ID_ADMIN
|| (CanData.assigned_id && id == CanData.assigned_id + 1)) {
// Add to admin command queue
uint32_t pushp = CanData.admin_push_pos;
if (pushp >= CanData.admin_pull_pos + ARRAY_SIZE(CanData.admin_queue))
// No space - drop message
return;
uint32_t pos = pushp % ARRAY_SIZE(CanData.admin_queue);
memcpy(&CanData.admin_queue[pos], msg, sizeof(*msg));
CanData.admin_push_pos = pushp + 1;
canserial_notify_rx();
}
}
// Remove from the receive buffer the given number of bytes
static void
console_pop_input(int len)
{
int copied = 0;
for (;;) {
int rpos = readb(&CanData.receive_pos);
int needcopy = rpos - len;
if (needcopy) {
memmove(&CanData.receive_buf[copied]
, &CanData.receive_buf[copied + len], needcopy - copied);
copied = needcopy;
canserial_notify_rx();
}
irqstatus_t flag = irq_save();
if (rpos != readb(&CanData.receive_pos)) {
// Raced with irq handler - retry
irq_restore(flag);
continue;
}
CanData.receive_pos = needcopy;
irq_restore(flag);
break;
}
}
// Task to process incoming commands and admin messages
void
canserial_rx_task(void)
{
if (!sched_check_wake(&CanData.rx_wake))
return;
// Process pending admin messages
for (;;) {
uint32_t pushp = readl(&CanData.admin_push_pos);
uint32_t pullp = CanData.admin_pull_pos;
if (pushp == pullp)
break;
uint32_t pos = pullp % ARRAY_SIZE(CanData.admin_queue);
struct canbus_msg *msg = &CanData.admin_queue[pos];
uint32_t id = msg->id;
if (CanData.assigned_id && id == CanData.assigned_id + 1)
can_id_conflict();
else if (id == CANBUS_ID_ADMIN)
can_process_admin(msg);
CanData.admin_pull_pos = pullp + 1;
}
// Check for a complete message block and process it
uint_fast8_t rpos = readb(&CanData.receive_pos), pop_count;
int ret = command_find_block(CanData.receive_buf, rpos, &pop_count);
if (ret > 0)
command_dispatch(CanData.receive_buf, pop_count);
if (ret) {
console_pop_input(pop_count);
if (ret > 0)
command_send_ack();
}
}
DECL_TASK(canserial_rx_task);
/****************************************************************
* Setup and shutdown
****************************************************************/
DECL_ENUMERATION("canbus_bus_state", "active", CANBUS_STATE_ACTIVE);
DECL_ENUMERATION("canbus_bus_state", "warn", CANBUS_STATE_WARN);
DECL_ENUMERATION("canbus_bus_state", "passive", CANBUS_STATE_PASSIVE);
DECL_ENUMERATION("canbus_bus_state", "off", CANBUS_STATE_OFF);
void
command_get_canbus_status(uint32_t *args)
{
struct canbus_status status;
memset(&status, 0, sizeof(status));
canhw_get_status(&status);
sendf("canbus_status rx_error=%u tx_error=%u tx_retries=%u"
" canbus_bus_state=%u"
, status.rx_error, status.tx_error, status.tx_retries
, status.bus_state);
}
DECL_COMMAND_FLAGS(command_get_canbus_status, HF_IN_SHUTDOWN
, "get_canbus_status");
void
command_get_canbus_id(uint32_t *args)
{
sendf("canbus_id canbus_uuid=%.*s canbus_nodeid=%u"
, sizeof(CanData.uuid), CanData.uuid, can_get_nodeid());
}
DECL_COMMAND_FLAGS(command_get_canbus_id, HF_IN_SHUTDOWN, "get_canbus_id");
void
canserial_set_uuid(uint8_t *raw_uuid, uint32_t raw_uuid_len)
{
uint64_t hash = fasthash64(raw_uuid, raw_uuid_len, 0xA16231A7);
memcpy(CanData.uuid, &hash, sizeof(CanData.uuid));
canserial_notify_rx();
}
void
canserial_shutdown(void)
{
canserial_notify_tx();
canserial_notify_rx();
}
DECL_SHUTDOWN(canserial_shutdown);
|