diff --git a/ports/atmel-samd/common-hal/canio/CAN.c b/ports/atmel-samd/common-hal/canio/CAN.c index 4f28698fba..ec6b759510 100644 --- a/ports/atmel-samd/common-hal/canio/CAN.c +++ b/ports/atmel-samd/common-hal/canio/CAN.c @@ -328,16 +328,18 @@ static void maybe_auto_restart(canio_can_obj_t *self) { } } -void common_hal_canio_can_send(canio_can_obj_t *self, canio_message_obj_t *message) +void common_hal_canio_can_send(canio_can_obj_t *self, mp_obj_t message_in) { maybe_auto_restart(self); + canio_message_obj_t *message = message_in;; // We have just one dedicated TX buffer, use it! canio_can_tx_buffer_t *ent = &self->state->tx_buffer[0]; + bool rtr = message->base.type == &canio_remote_transmission_request_type; ent->txb0.bit.ESI = false; ent->txb0.bit.XTD = message->extended; - ent->txb0.bit.RTR = message->rtr; + ent->txb0.bit.RTR = rtr; if (message->extended) { ent->txb0.bit.ID = message->id; } else { @@ -350,7 +352,7 @@ void common_hal_canio_can_send(canio_can_obj_t *self, canio_message_obj_t *messa ent->txb1.bit.BRS = 0; // No bit rate switching ent->txb1.bit.DLC = message->size; - if (!message->rtr) { + if (!rtr) { memcpy(ent->data, message->data, message->size); } diff --git a/ports/atmel-samd/common-hal/canio/Listener.c b/ports/atmel-samd/common-hal/canio/Listener.c index 483802fa4a..b27c8cd1b8 100644 --- a/ports/atmel-samd/common-hal/canio/Listener.c +++ b/ports/atmel-samd/common-hal/canio/Listener.c @@ -349,30 +349,32 @@ int common_hal_canio_listener_in_waiting(canio_listener_obj_t *self) { return self->hw->RXFS.bit.F0FL; } -bool common_hal_canio_listener_receiveinto(canio_listener_obj_t *self, canio_message_obj_t *message) { +mp_obj_t common_hal_canio_listener_receive(canio_listener_obj_t *self) { if (!common_hal_canio_listener_in_waiting(self)) { uint64_t deadline = supervisor_ticks_ms64() + self->timeout_ms; do { if (supervisor_ticks_ms64() > deadline) { - return false; + return NULL; } } while (!common_hal_canio_listener_in_waiting(self)); } int index = self->hw->RXFS.bit.F0GI; canio_can_rx_fifo_t *hw_message = &self->fifo[index]; + bool rtr = hw_message->rxf0.bit.RTR; + canio_message_obj_t *message = m_new_obj(canio_message_obj_t); + message->base.type = rtr ? &canio_remote_transmission_request_type : &canio_message_type; message->extended = hw_message->rxf0.bit.XTD; if (message->extended) { message->id = hw_message->rxf0.bit.ID; } else { message->id = hw_message->rxf0.bit.ID >> 18; // short ids are left-justified } - message->rtr = hw_message->rxf0.bit.RTR; message->size = hw_message->rxf1.bit.DLC; - if (!message->rtr) { + if (!rtr) { memcpy(message->data, hw_message->data, message->size); } self->hw->RXFA.bit.F0AI = index; - return true; + return message; } void common_hal_canio_listener_deinit(canio_listener_obj_t *self) { diff --git a/shared-bindings/canio/CAN.h b/shared-bindings/canio/CAN.h index 08166dfd42..8d61bc8ed5 100644 --- a/shared-bindings/canio/CAN.h +++ b/shared-bindings/canio/CAN.h @@ -51,5 +51,5 @@ void common_hal_canio_can_auto_restart_set(canio_can_obj_t *self, bool auto_rest void common_hal_canio_can_check_for_deinit(canio_can_obj_t *self); void common_hal_canio_can_deinit(canio_can_obj_t *self); void common_hal_canio_can_restart(canio_can_obj_t *self); -void common_hal_canio_can_send(canio_can_obj_t *self, canio_message_obj_t *message); +void common_hal_canio_can_send(canio_can_obj_t *self, mp_obj_t message); void common_hal_canio_reset(void); diff --git a/shared-bindings/canio/Listener.c b/shared-bindings/canio/Listener.c index 7b0e59063c..e5a9a2f83f 100644 --- a/shared-bindings/canio/Listener.c +++ b/shared-bindings/canio/Listener.c @@ -49,17 +49,11 @@ STATIC mp_obj_t canio_listener_receive(mp_obj_t self_in) { canio_listener_obj_t *self = MP_OBJ_TO_PTR(self_in); common_hal_canio_listener_check_for_deinit(self); - canio_message_obj_t *message = m_new_obj(canio_message_obj_t); + mp_obj_t message = common_hal_canio_listener_receive(self); + // note: receive fills out the type field of the message - if (common_hal_canio_listener_receiveinto(self, message)) { - if (message->rtr) { - message->base.type = &canio_remote_transmission_request_type; - } else { - message->base.type = &canio_message_type; - } + if (message) { return message; - } else { - m_free(message); // message did not escape into vm } return mp_const_none; } diff --git a/shared-bindings/canio/Listener.h b/shared-bindings/canio/Listener.h index d925fa2652..527ffe4cbb 100644 --- a/shared-bindings/canio/Listener.h +++ b/shared-bindings/canio/Listener.h @@ -37,7 +37,7 @@ typedef struct canio_listener_obj canio_listener_obj_t; void common_hal_canio_listener_construct(canio_listener_obj_t *self, canio_can_obj_t *can, size_t nmatch, canio_match_obj_t **matches, float timeout); void common_hal_canio_listener_check_for_deinit(canio_listener_obj_t *self); void common_hal_canio_listener_deinit(canio_listener_obj_t *self); -bool common_hal_canio_listener_receiveinto(canio_listener_obj_t *self, canio_message_obj_t *message); +mp_obj_t common_hal_canio_listener_receive(canio_listener_obj_t *self); int common_hal_canio_listener_in_waiting(canio_listener_obj_t *self); float common_hal_canio_listener_get_timeout(canio_listener_obj_t *self); void common_hal_canio_listener_set_timeout(canio_listener_obj_t *self, float timeout); diff --git a/shared-module/canio/Message.c b/shared-module/canio/Message.c index 34c2e61f76..a1df4f693d 100644 --- a/shared-module/canio/Message.c +++ b/shared-module/canio/Message.c @@ -32,7 +32,6 @@ void common_hal_canio_message_construct(canio_message_obj_t *self, int id, void { self->id = id; self->size = size; - self->rtr = !data; self->extended = extended; if (data) { memcpy(self->data, data, size); @@ -57,7 +56,6 @@ const void *common_hal_canio_message_get_data(const canio_message_obj_t *self) const void common_hal_canio_message_set_data(canio_message_obj_t *self, const void *data, size_t size) { - self->rtr = false; self->size = size; memcpy(self->data, data, size); } diff --git a/shared-module/canio/Message.h b/shared-module/canio/Message.h index b99c5c48e3..1ca0d42e2c 100644 --- a/shared-module/canio/Message.h +++ b/shared-module/canio/Message.h @@ -33,6 +33,5 @@ typedef struct { int id; uint8_t data[8]; size_t size:4; - bool rtr:1; bool extended:1; } canio_message_obj_t; diff --git a/shared-module/canio/RemoteTransmissionRequest.c b/shared-module/canio/RemoteTransmissionRequest.c index 00c9334b0a..9b4d5632f8 100644 --- a/shared-module/canio/RemoteTransmissionRequest.c +++ b/shared-module/canio/RemoteTransmissionRequest.c @@ -33,7 +33,6 @@ void common_hal_canio_remote_transmission_request_construct(canio_remote_transmi { self->id = id; self->size = size; - self->rtr = true; self->extended = extended; }