From eed3387f4e625335a5d2f6582946ec75d1c1ed10 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 6 Oct 2020 10:31:38 -0500 Subject: [PATCH] stm32: canio: Fix message cancellation .. it's necessary to wait for a cancellation request to actually free the respective Tx mailbox --- ports/stm/common-hal/canio/CAN.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ports/stm/common-hal/canio/CAN.c b/ports/stm/common-hal/canio/CAN.c index 62c4e9935f..fe1c3f2b14 100644 --- a/ports/stm/common-hal/canio/CAN.c +++ b/ports/stm/common-hal/canio/CAN.c @@ -241,13 +241,21 @@ void common_hal_canio_can_send(canio_can_obj_t *self, mp_obj_t message_in) // // We don't strictly guarantee that we abort the oldest Tx request, // rather we just abort a different index each time. This permits us - // to avoid tracking this information altogether. + // to just track a single cancel index HAL_CAN_AbortTxRequest(&self->handle, 1 << (self->cancel_mailbox)); self->cancel_mailbox = (self->cancel_mailbox + 1) % 3; + // The abort request may not have completed immediately, so wait for + // the Tx mailbox to become free + do { + free_level = HAL_CAN_GetTxMailboxesFreeLevel(&self->handle); + } while (!free_level); } HAL_StatusTypeDef status = HAL_CAN_AddTxMessage(&self->handle, &header, message->data, &mailbox); if (status != HAL_OK) { - mp_raise_OSError(MP_ENOMEM); + // this is a "shouldn't happen" condition. we don't throw because the + // contract of send() is that it queues the packet to be sent if + // possible and does not signal success or failure to actually send. + return; } // wait 8ms (hard coded for now) for TX to occur