Skip to content

Commit 5f0ae73

Browse files
committed
stm32: canio: When Tx mailboxes are full, cancel an old message
.. also add the 8ms wait for transmission that the atmel sam port has
1 parent 2790629 commit 5f0ae73

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

ports/stm/common-hal/canio/CAN.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,28 @@ void common_hal_canio_can_send(canio_can_obj_t *self, mp_obj_t message_in)
225225
.RTR = rtr ? CAN_RTR_REMOTE : CAN_RTR_DATA,
226226
.DLC = message->size,
227227
};
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+
}
228240
HAL_StatusTypeDef status = HAL_CAN_AddTxMessage(&self->handle, &header, message->data, &mailbox);
229241
if (status != HAL_OK) {
230242
mp_raise_OSError(MP_ENOMEM);
231243
}
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+
}
232250
}
233251

234252
bool common_hal_canio_can_silent_get(canio_can_obj_t *self) {

ports/stm/common-hal/canio/CAN.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ typedef struct canio_can_obj {
5454
bool fifo0_in_use:1;
5555
bool fifo1_in_use:1;
5656
uint8_t periph_index:2;
57+
uint8_t cancel_mailbox;
5758
uint8_t start_filter_bank;
5859
uint8_t end_filter_bank;
5960
long filter_in_use; // bitmask for the 28 filter banks

0 commit comments

Comments
 (0)