Skip to content

Commit 2ac2f62

Browse files
authored
Merge pull request adafruit#3466 from jepler/canbus-api-changes
Canbus: api changes
2 parents 5ca10e7 + c129c8f commit 2ac2f62

22 files changed

+462
-316
lines changed

locale/circuitpython.pot

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-09-29 11:11+0530\n"
11+
"POT-Creation-Date: 2020-09-29 20:14-0500\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -1530,6 +1530,10 @@ msgstr ""
15301530
msgid "Refresh too soon"
15311531
msgstr ""
15321532

1533+
#: shared-bindings/canio/RemoteTransmissionRequest.c
1534+
msgid "RemoteTransmissionRequests limited to 8 bytes"
1535+
msgstr ""
1536+
15331537
#: shared-bindings/aesio/aes.c
15341538
msgid "Requested AES mode is unsupported"
15351539
msgstr ""
@@ -2412,10 +2416,14 @@ msgstr ""
24122416
msgid "exceptions must derive from BaseException"
24132417
msgstr ""
24142418

2415-
#: shared-bindings/canio/CAN.c shared-bindings/canio/Listener.c
2419+
#: shared-bindings/canio/CAN.c
24162420
msgid "expected '%q' but got '%q'"
24172421
msgstr ""
24182422

2423+
#: shared-bindings/canio/CAN.c
2424+
msgid "expected '%q' or '%q' but got '%q'"
2425+
msgstr ""
2426+
24192427
#: py/objstr.c
24202428
msgid "expected ':' after format specifier"
24212429
msgstr ""
@@ -3275,10 +3283,6 @@ msgstr ""
32753283
msgid "source palette too large"
32763284
msgstr ""
32773285

3278-
#: shared-bindings/canio/Message.c
3279-
msgid "specify size or data, but not both"
3280-
msgstr ""
3281-
32823286
#: py/objstr.c
32833287
msgid "start/end indices"
32843288
msgstr ""

ports/atmel-samd/common-hal/canio/CAN.c

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -275,21 +275,6 @@ int common_hal_canio_can_receive_error_count_get(canio_can_obj_t *self)
275275
return self->hw->ECR.bit.REC;
276276
}
277277

278-
int common_hal_canio_can_error_warning_state_count_get(canio_can_obj_t *self)
279-
{
280-
return self->error_warning_state_count;
281-
}
282-
283-
int common_hal_canio_can_error_passive_state_count_get(canio_can_obj_t *self)
284-
{
285-
return self->error_passive_state_count;
286-
}
287-
288-
int common_hal_canio_can_bus_off_state_count_get(canio_can_obj_t *self)
289-
{
290-
return self->bus_off_state_count;
291-
}
292-
293278
canio_bus_state_t common_hal_canio_can_state_get(canio_can_obj_t *self) {
294279
CAN_PSR_Type psr = self->hw->PSR;
295280
if (psr.bit.BO) {
@@ -328,16 +313,18 @@ static void maybe_auto_restart(canio_can_obj_t *self) {
328313
}
329314
}
330315

331-
void common_hal_canio_can_send(canio_can_obj_t *self, canio_message_obj_t *message)
316+
void common_hal_canio_can_send(canio_can_obj_t *self, mp_obj_t message_in)
332317
{
333318
maybe_auto_restart(self);
334319

320+
canio_message_obj_t *message = message_in;;
335321
// We have just one dedicated TX buffer, use it!
336322
canio_can_tx_buffer_t *ent = &self->state->tx_buffer[0];
337323

324+
bool rtr = message->base.type == &canio_remote_transmission_request_type;
338325
ent->txb0.bit.ESI = false;
339326
ent->txb0.bit.XTD = message->extended;
340-
ent->txb0.bit.RTR = message->rtr;
327+
ent->txb0.bit.RTR = rtr;
341328
if (message->extended) {
342329
ent->txb0.bit.ID = message->id;
343330
} else {
@@ -350,7 +337,7 @@ void common_hal_canio_can_send(canio_can_obj_t *self, canio_message_obj_t *messa
350337
ent->txb1.bit.BRS = 0; // No bit rate switching
351338
ent->txb1.bit.DLC = message->size;
352339

353-
if (!message->rtr) {
340+
if (!rtr) {
354341
memcpy(ent->data, message->data, message->size);
355342
}
356343

@@ -417,17 +404,6 @@ STATIC void can_handler(int i) {
417404
Can *hw = can_insts[i];
418405
uint32_t ir = hri_can_read_IR_reg(hw);
419406

420-
/* Count up errors*/
421-
if (ir & CAN_IE_EWE) {
422-
self->error_warning_state_count += 1;
423-
}
424-
if (ir & CAN_IE_EPE) {
425-
self->error_passive_state_count += 1;
426-
}
427-
if (ir & CAN_IE_BOE) {
428-
self->bus_off_state_count += 1;
429-
}
430-
431407
/* Acknowledge interrupt */
432408
hri_can_write_IR_reg(hw, ir);
433409
}

ports/atmel-samd/common-hal/canio/Listener.c

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "common-hal/canio/__init__.h"
3434
#include "common-hal/canio/Listener.h"
35+
#include "shared-bindings/canio/Listener.h"
3536
#include "shared-bindings/util.h"
3637
#include "supervisor/shared/tick.h"
3738
#include "component/can.h"
@@ -58,8 +59,8 @@ STATIC void static_assertions(void) {
5859
MP_STATIC_ASSERT(CAN_XIDFE_0_EFEC_STF0M_Val + 1 == CAN_XIDFE_0_EFEC_STF1M_Val);
5960
}
6061

61-
STATIC bool single_address_filter(canio_match_obj_t *match) {
62-
return match->mask == 0 || match->mask == match->address;
62+
STATIC bool single_id_filter(canio_match_obj_t *match) {
63+
return match->mask == 0 || match->mask == match->id;
6364
}
6465

6566
STATIC bool standard_filter_in_use(CanMramSidfe *filter) {
@@ -76,7 +77,7 @@ STATIC size_t num_filters_needed(size_t nmatch, canio_match_obj_t **matches, boo
7677
if (extended != matches[i]->extended) {
7778
continue;
7879
}
79-
if (single_address_filter(matches[i])) {
80+
if (single_id_filter(matches[i])) {
8081
num_half_filters_needed += 1;
8182
} else {
8283
num_half_filters_needed += 2;
@@ -191,7 +192,7 @@ STATIC void install_extended_filter(CanMramXidfe *extended, int id1, int id2, in
191192
}
192193

193194

194-
#define NO_ADDRESS (-1)
195+
#define NO_ID (-1)
195196
void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t **matches) {
196197
int fifo = self->fifo_idx;
197198

@@ -207,31 +208,31 @@ void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t **
207208
CanMramSidfe *standard = next_standard_filter(self, NULL);
208209
CanMramXidfe *extended = next_extended_filter(self, NULL);
209210

210-
int first_address = NO_ADDRESS;
211+
int first_id = NO_ID;
211212

212-
// step 1: single address standard matches
213+
// step 1: single id standard matches
213214
// we have to gather up pairs and stuff them in a single filter entry
214215
for(size_t i = 0; i<nmatch; i++) {
215216
canio_match_obj_t *match = matches[i];
216217
if (match->extended) {
217218
continue;
218219
}
219-
if (!single_address_filter(match)) {
220+
if (!single_id_filter(match)) {
220221
continue;
221222
}
222-
if (first_address != NO_ADDRESS) {
223-
install_standard_filter(standard, first_address, match->address, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_DUAL_Val);
224-
first_address = NO_ADDRESS;
223+
if (first_id != NO_ID) {
224+
install_standard_filter(standard, first_id, match->id, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_DUAL_Val);
225+
first_id = NO_ID;
225226
standard = next_standard_filter(self, standard);
226227
} else {
227-
first_address = match->address;
228+
first_id = match->id;
228229
}
229230
}
230-
// step 1.5. odd single address standard match
231-
if (first_address != NO_ADDRESS) {
232-
install_standard_filter(standard, first_address, first_address, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_DUAL_Val);
231+
// step 1.5. odd single id standard match
232+
if (first_id != NO_ID) {
233+
install_standard_filter(standard, first_id, first_id, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_DUAL_Val);
233234
standard = next_standard_filter(self, standard);
234-
first_address = NO_ADDRESS;
235+
first_id = NO_ID;
235236
}
236237

237238
// step 2: standard mask filter
@@ -240,36 +241,36 @@ void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t **
240241
if (match->extended) {
241242
continue;
242243
}
243-
if (single_address_filter(match)) {
244+
if (single_id_filter(match)) {
244245
continue;
245246
}
246-
install_standard_filter(standard, match->address, match->mask, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_CLASSIC_Val);
247+
install_standard_filter(standard, match->id, match->mask, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_CLASSIC_Val);
247248
standard = next_standard_filter(self, standard);
248249
}
249250

250-
// step 3: single address extended matches
251+
// step 3: single id extended matches
251252
// we have to gather up pairs and stuff them in a single filter entry
252253
for(size_t i = 0; i<nmatch; i++) {
253254
canio_match_obj_t *match = matches[i];
254255
if (!match->extended) {
255256
continue;
256257
}
257-
if (!single_address_filter(match)) {
258+
if (!single_id_filter(match)) {
258259
continue;
259260
}
260-
if (first_address != NO_ADDRESS) {
261-
install_extended_filter(extended, first_address, match->address, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_DUAL_Val);
262-
first_address = NO_ADDRESS;
261+
if (first_id != NO_ID) {
262+
install_extended_filter(extended, first_id, match->id, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_DUAL_Val);
263+
first_id = NO_ID;
263264
extended = next_extended_filter(self, extended);
264265
} else {
265-
first_address = match->address;
266+
first_id = match->id;
266267
}
267268
}
268-
// step 3.5. odd single address standard match
269-
if (first_address != NO_ADDRESS) {
270-
install_extended_filter(extended, first_address, first_address, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_DUAL_Val);
269+
// step 3.5. odd single id standard match
270+
if (first_id != NO_ID) {
271+
install_extended_filter(extended, first_id, first_id, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_DUAL_Val);
271272
extended = next_extended_filter(self, extended);
272-
first_address = NO_ADDRESS;
273+
first_id = NO_ID;
273274
}
274275

275276
// step 4: extended mask filters
@@ -278,10 +279,10 @@ void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t **
278279
if (!match->extended) {
279280
continue;
280281
}
281-
if (single_address_filter(match)) {
282+
if (single_id_filter(match)) {
282283
continue;
283284
}
284-
install_extended_filter(extended, match->address, match->mask, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_CLASSIC_Val);
285+
install_extended_filter(extended, match->id, match->mask, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_CLASSIC_Val);
285286
extended = next_extended_filter(self, extended);
286287
}
287288

@@ -348,30 +349,32 @@ int common_hal_canio_listener_in_waiting(canio_listener_obj_t *self) {
348349
return self->hw->RXFS.bit.F0FL;
349350
}
350351

351-
bool common_hal_canio_listener_readinto(canio_listener_obj_t *self, canio_message_obj_t *message) {
352+
mp_obj_t common_hal_canio_listener_receive(canio_listener_obj_t *self) {
352353
if (!common_hal_canio_listener_in_waiting(self)) {
353354
uint64_t deadline = supervisor_ticks_ms64() + self->timeout_ms;
354355
do {
355356
if (supervisor_ticks_ms64() > deadline) {
356-
return false;
357+
return NULL;
357358
}
358359
} while (!common_hal_canio_listener_in_waiting(self));
359360
}
360361
int index = self->hw->RXFS.bit.F0GI;
361362
canio_can_rx_fifo_t *hw_message = &self->fifo[index];
363+
bool rtr = hw_message->rxf0.bit.RTR;
364+
canio_message_obj_t *message = m_new_obj(canio_message_obj_t);
365+
message->base.type = rtr ? &canio_remote_transmission_request_type : &canio_message_type;
362366
message->extended = hw_message->rxf0.bit.XTD;
363367
if (message->extended) {
364368
message->id = hw_message->rxf0.bit.ID;
365369
} else {
366-
message->id = hw_message->rxf0.bit.ID >> 18; // short addresses are left-justified
370+
message->id = hw_message->rxf0.bit.ID >> 18; // short ids are left-justified
367371
}
368-
message->rtr = hw_message->rxf0.bit.RTR;
369372
message->size = hw_message->rxf1.bit.DLC;
370-
if (!message->rtr) {
373+
if (!rtr) {
371374
memcpy(message->data, hw_message->data, message->size);
372375
}
373376
self->hw->RXFA.bit.F0AI = index;
374-
return true;
377+
return message;
375378
}
376379

377380
void common_hal_canio_listener_deinit(canio_listener_obj_t *self) {

ports/atmel-samd/common-hal/canio/Listener.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,11 @@ typedef struct {
3535
__IO CAN_RXF0A_Type RXFA; /**< \brief (R/W 32) Rx FIFO n Acknowledge */
3636
} canio_rxfifo_reg_t;
3737

38-
typedef struct {
38+
typedef struct canio_listener_obj {
3939
mp_obj_base_t base;
4040
canio_can_obj_t *can;
4141
canio_can_rx_fifo_t *fifo;
4242
canio_rxfifo_reg_t *hw;
4343
uint32_t timeout_ms;
4444
uint8_t fifo_idx;
4545
} canio_listener_obj_t;
46-
47-
void common_hal_canio_listener_construct(canio_listener_obj_t *self, canio_can_obj_t *can, size_t nmatch, canio_match_obj_t **matches, float timeout);
48-
void common_hal_canio_listener_check_for_deinit(canio_listener_obj_t *self);
49-
void common_hal_canio_listener_deinit(canio_listener_obj_t *self);
50-
bool common_hal_canio_listener_readinto(canio_listener_obj_t *self, canio_message_obj_t *message);
51-
int common_hal_canio_listener_in_waiting(canio_listener_obj_t *self);
52-
float common_hal_canio_listener_get_timeout(canio_listener_obj_t *self);
53-
void common_hal_canio_listener_set_timeout(canio_listener_obj_t *self, float timeout);

py/circuitpy_defns.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ SRC_SHARED_MODULE_ALL = \
411411
_bleio/ScanResults.c \
412412
canio/Match.c \
413413
canio/Message.c \
414+
canio/RemoteTransmissionRequest.c \
414415
_eve/__init__.c \
415416
_pixelbuf/PixelBuf.c \
416417
_pixelbuf/__init__.c \

0 commit comments

Comments
 (0)