File tree Expand file tree Collapse file tree 2 files changed +19
-0
lines changed
ports/stm/common-hal/canio Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -225,10 +225,28 @@ void common_hal_canio_can_send(canio_can_obj_t *self, mp_obj_t message_in)
225
225
.RTR = rtr ? CAN_RTR_REMOTE : CAN_RTR_DATA ,
226
226
.DLC = message -> size ,
227
227
};
228
+ uint32_t free_level = HAL_CAN_GetTxMailboxesFreeLevel (& self -> handle );
229
+ if (free_level == 0 ) {
230
+ // There's no free Tx mailbox. We need to cancel some message without
231
+ // transmitting it, because once the bus returns to active state it's
232
+ // preferable to transmit the newest messages instead of older messages.
233
+ //
234
+ // We don't strictly guarantee that we abort the oldest Tx request,
235
+ // rather we just abort a different index each time. This permits us
236
+ // to avoid tracking this information altogether.
237
+ HAL_CAN_AbortTxRequest (& self -> handle , 1 << (self -> cancel_mailbox ));
238
+ self -> cancel_mailbox = (self -> cancel_mailbox + 1 ) % 3 ;
239
+ }
228
240
HAL_StatusTypeDef status = HAL_CAN_AddTxMessage (& self -> handle , & header , message -> data , & mailbox );
229
241
if (status != HAL_OK ) {
230
242
mp_raise_OSError (MP_ENOMEM );
231
243
}
244
+
245
+ // wait 8ms (hard coded for now) for TX to occur
246
+ uint64_t deadline = port_get_raw_ticks (NULL ) + 8 ;
247
+ while (port_get_raw_ticks (NULL ) < deadline && HAL_CAN_IsTxMessagePending (& self -> handle , 1 << mailbox )) {
248
+ RUN_BACKGROUND_TASKS ;
249
+ }
232
250
}
233
251
234
252
bool common_hal_canio_can_silent_get (canio_can_obj_t * self ) {
Original file line number Diff line number Diff line change @@ -54,6 +54,7 @@ typedef struct canio_can_obj {
54
54
bool fifo0_in_use :1 ;
55
55
bool fifo1_in_use :1 ;
56
56
uint8_t periph_index :2 ;
57
+ uint8_t cancel_mailbox ;
57
58
uint8_t start_filter_bank ;
58
59
uint8_t end_filter_bank ;
59
60
long filter_in_use ; // bitmask for the 28 filter banks
You can’t perform that action at this time.
0 commit comments