32
32
33
33
#include "common-hal/canio/__init__.h"
34
34
#include "common-hal/canio/Listener.h"
35
+ #include "shared-bindings/canio/Listener.h"
35
36
#include "shared-bindings/util.h"
36
37
#include "supervisor/shared/tick.h"
37
38
#include "component/can.h"
@@ -58,8 +59,8 @@ STATIC void static_assertions(void) {
58
59
MP_STATIC_ASSERT (CAN_XIDFE_0_EFEC_STF0M_Val + 1 == CAN_XIDFE_0_EFEC_STF1M_Val );
59
60
}
60
61
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 ;
63
64
}
64
65
65
66
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
76
77
if (extended != matches [i ]-> extended ) {
77
78
continue ;
78
79
}
79
- if (single_address_filter (matches [i ])) {
80
+ if (single_id_filter (matches [i ])) {
80
81
num_half_filters_needed += 1 ;
81
82
} else {
82
83
num_half_filters_needed += 2 ;
@@ -191,7 +192,7 @@ STATIC void install_extended_filter(CanMramXidfe *extended, int id1, int id2, in
191
192
}
192
193
193
194
194
- #define NO_ADDRESS (-1)
195
+ #define NO_ID (-1)
195
196
void set_filters (canio_listener_obj_t * self , size_t nmatch , canio_match_obj_t * * matches ) {
196
197
int fifo = self -> fifo_idx ;
197
198
@@ -207,31 +208,31 @@ void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t **
207
208
CanMramSidfe * standard = next_standard_filter (self , NULL );
208
209
CanMramXidfe * extended = next_extended_filter (self , NULL );
209
210
210
- int first_address = NO_ADDRESS ;
211
+ int first_id = NO_ID ;
211
212
212
- // step 1: single address standard matches
213
+ // step 1: single id standard matches
213
214
// we have to gather up pairs and stuff them in a single filter entry
214
215
for (size_t i = 0 ; i < nmatch ; i ++ ) {
215
216
canio_match_obj_t * match = matches [i ];
216
217
if (match -> extended ) {
217
218
continue ;
218
219
}
219
- if (!single_address_filter (match )) {
220
+ if (!single_id_filter (match )) {
220
221
continue ;
221
222
}
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 ;
225
226
standard = next_standard_filter (self , standard );
226
227
} else {
227
- first_address = match -> address ;
228
+ first_id = match -> id ;
228
229
}
229
230
}
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 );
233
234
standard = next_standard_filter (self , standard );
234
- first_address = NO_ADDRESS ;
235
+ first_id = NO_ID ;
235
236
}
236
237
237
238
// step 2: standard mask filter
@@ -240,36 +241,36 @@ void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t **
240
241
if (match -> extended ) {
241
242
continue ;
242
243
}
243
- if (single_address_filter (match )) {
244
+ if (single_id_filter (match )) {
244
245
continue ;
245
246
}
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 );
247
248
standard = next_standard_filter (self , standard );
248
249
}
249
250
250
- // step 3: single address extended matches
251
+ // step 3: single id extended matches
251
252
// we have to gather up pairs and stuff them in a single filter entry
252
253
for (size_t i = 0 ; i < nmatch ; i ++ ) {
253
254
canio_match_obj_t * match = matches [i ];
254
255
if (!match -> extended ) {
255
256
continue ;
256
257
}
257
- if (!single_address_filter (match )) {
258
+ if (!single_id_filter (match )) {
258
259
continue ;
259
260
}
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 ;
263
264
extended = next_extended_filter (self , extended );
264
265
} else {
265
- first_address = match -> address ;
266
+ first_id = match -> id ;
266
267
}
267
268
}
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 );
271
272
extended = next_extended_filter (self , extended );
272
- first_address = NO_ADDRESS ;
273
+ first_id = NO_ID ;
273
274
}
274
275
275
276
// step 4: extended mask filters
@@ -278,10 +279,10 @@ void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t **
278
279
if (!match -> extended ) {
279
280
continue ;
280
281
}
281
- if (single_address_filter (match )) {
282
+ if (single_id_filter (match )) {
282
283
continue ;
283
284
}
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 );
285
286
extended = next_extended_filter (self , extended );
286
287
}
287
288
@@ -348,30 +349,32 @@ int common_hal_canio_listener_in_waiting(canio_listener_obj_t *self) {
348
349
return self -> hw -> RXFS .bit .F0FL ;
349
350
}
350
351
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 ) {
352
353
if (!common_hal_canio_listener_in_waiting (self )) {
353
354
uint64_t deadline = supervisor_ticks_ms64 () + self -> timeout_ms ;
354
355
do {
355
356
if (supervisor_ticks_ms64 () > deadline ) {
356
- return false ;
357
+ return NULL ;
357
358
}
358
359
} while (!common_hal_canio_listener_in_waiting (self ));
359
360
}
360
361
int index = self -> hw -> RXFS .bit .F0GI ;
361
362
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 ;
362
366
message -> extended = hw_message -> rxf0 .bit .XTD ;
363
367
if (message -> extended ) {
364
368
message -> id = hw_message -> rxf0 .bit .ID ;
365
369
} 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
367
371
}
368
- message -> rtr = hw_message -> rxf0 .bit .RTR ;
369
372
message -> size = hw_message -> rxf1 .bit .DLC ;
370
- if (!message -> rtr ) {
373
+ if (!rtr ) {
371
374
memcpy (message -> data , hw_message -> data , message -> size );
372
375
}
373
376
self -> hw -> RXFA .bit .F0AI = index ;
374
- return true ;
377
+ return message ;
375
378
}
376
379
377
380
void common_hal_canio_listener_deinit (canio_listener_obj_t * self ) {
0 commit comments