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
6566STATIC 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)
195196void 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
377380void common_hal_canio_listener_deinit (canio_listener_obj_t * self ) {
0 commit comments