CAN: Use mp_obj_t insteaed of canio_message_obj_t, get rid of rtr field

This commit is contained in:
Jeff Epler 2020-09-28 17:14:39 -05:00
parent 13493730a1
commit f4e36fc7f6
8 changed files with 17 additions and 23 deletions

View File

@ -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); maybe_auto_restart(self);
canio_message_obj_t *message = message_in;;
// We have just one dedicated TX buffer, use it! // We have just one dedicated TX buffer, use it!
canio_can_tx_buffer_t *ent = &self->state->tx_buffer[0]; 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.ESI = false;
ent->txb0.bit.XTD = message->extended; ent->txb0.bit.XTD = message->extended;
ent->txb0.bit.RTR = message->rtr; ent->txb0.bit.RTR = rtr;
if (message->extended) { if (message->extended) {
ent->txb0.bit.ID = message->id; ent->txb0.bit.ID = message->id;
} else { } 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.BRS = 0; // No bit rate switching
ent->txb1.bit.DLC = message->size; ent->txb1.bit.DLC = message->size;
if (!message->rtr) { if (!rtr) {
memcpy(ent->data, message->data, message->size); memcpy(ent->data, message->data, message->size);
} }

View File

@ -349,30 +349,32 @@ int common_hal_canio_listener_in_waiting(canio_listener_obj_t *self) {
return self->hw->RXFS.bit.F0FL; 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)) { if (!common_hal_canio_listener_in_waiting(self)) {
uint64_t deadline = supervisor_ticks_ms64() + self->timeout_ms; uint64_t deadline = supervisor_ticks_ms64() + self->timeout_ms;
do { do {
if (supervisor_ticks_ms64() > deadline) { if (supervisor_ticks_ms64() > deadline) {
return false; return NULL;
} }
} while (!common_hal_canio_listener_in_waiting(self)); } while (!common_hal_canio_listener_in_waiting(self));
} }
int index = self->hw->RXFS.bit.F0GI; int index = self->hw->RXFS.bit.F0GI;
canio_can_rx_fifo_t *hw_message = &self->fifo[index]; 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; message->extended = hw_message->rxf0.bit.XTD;
if (message->extended) { if (message->extended) {
message->id = hw_message->rxf0.bit.ID; message->id = hw_message->rxf0.bit.ID;
} else { } else {
message->id = hw_message->rxf0.bit.ID >> 18; // short ids are left-justified 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; message->size = hw_message->rxf1.bit.DLC;
if (!message->rtr) { if (!rtr) {
memcpy(message->data, hw_message->data, message->size); memcpy(message->data, hw_message->data, message->size);
} }
self->hw->RXFA.bit.F0AI = index; self->hw->RXFA.bit.F0AI = index;
return true; return message;
} }
void common_hal_canio_listener_deinit(canio_listener_obj_t *self) { void common_hal_canio_listener_deinit(canio_listener_obj_t *self) {

View File

@ -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_check_for_deinit(canio_can_obj_t *self);
void common_hal_canio_can_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_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); void common_hal_canio_reset(void);

View File

@ -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); canio_listener_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_canio_listener_check_for_deinit(self); 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) {
if (message->rtr) {
message->base.type = &canio_remote_transmission_request_type;
} else {
message->base.type = &canio_message_type;
}
return message; return message;
} else {
m_free(message); // message did not escape into vm
} }
return mp_const_none; return mp_const_none;
} }

View File

@ -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_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_check_for_deinit(canio_listener_obj_t *self);
void common_hal_canio_listener_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); int common_hal_canio_listener_in_waiting(canio_listener_obj_t *self);
float common_hal_canio_listener_get_timeout(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); void common_hal_canio_listener_set_timeout(canio_listener_obj_t *self, float timeout);

View File

@ -32,7 +32,6 @@ void common_hal_canio_message_construct(canio_message_obj_t *self, int id, void
{ {
self->id = id; self->id = id;
self->size = size; self->size = size;
self->rtr = !data;
self->extended = extended; self->extended = extended;
if (data) { if (data) {
memcpy(self->data, data, size); 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) 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; self->size = size;
memcpy(self->data, data, size); memcpy(self->data, data, size);
} }

View File

@ -33,6 +33,5 @@ typedef struct {
int id; int id;
uint8_t data[8]; uint8_t data[8];
size_t size:4; size_t size:4;
bool rtr:1;
bool extended:1; bool extended:1;
} canio_message_obj_t; } canio_message_obj_t;

View File

@ -33,7 +33,6 @@ void common_hal_canio_remote_transmission_request_construct(canio_remote_transmi
{ {
self->id = id; self->id = id;
self->size = size; self->size = size;
self->rtr = true;
self->extended = extended; self->extended = extended;
} }