From 5f6ba69611605ab0a81830c749645bb57da6eccd Mon Sep 17 00:00:00 2001 From: Roey Borsteinas Date: Sat, 26 Nov 2022 13:01:49 -0500 Subject: [PATCH] Fixed data length size so that it actually uses the size parameter --- lib/CAN/Can.cpp | 44 +++++++++++++++++++------------------------- lib/CAN/Can.h | 11 +++++++++-- lib/CAN/README.md | 7 +++++++ 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/lib/CAN/Can.cpp b/lib/CAN/Can.cpp index b5c015b..4818259 100644 --- a/lib/CAN/Can.cpp +++ b/lib/CAN/Can.cpp @@ -241,7 +241,7 @@ uint32_t CANBus::getAvailableForWrite() return free_mailboxes; } -Status CANBus::write(uint16_t message_id, const uint8_t data[], uint32_t size) +Status CANBus::write(const Message& message) { if (state != CANBus::State::Ready) { @@ -254,20 +254,20 @@ Status CANBus::write(uint16_t message_id, const uint8_t data[], uint32_t size) // mask for the first open transmission mailbox const uint32_t transmit_mailbox = (hcan->TSR & (0b11ul << CAN_TSR_CODE)) >> 24; - hcan->sTxMailBox[transmit_mailbox].TIR = (message_id << CAN_TI0R_STID) | CAN_RTR_DATA; - hcan->sTxMailBox[transmit_mailbox].TDTR = 8; // always only transmit 8 bytes for simplicity + hcan->sTxMailBox[transmit_mailbox].TIR = (message.id << CAN_TI0R_STID) | CAN_RTR_DATA; + hcan->sTxMailBox[transmit_mailbox].TDTR = message.size; // always only transmit 8 bytes for simplicity // #todo: set TDTR time bit? hcan->sTxMailBox[transmit_mailbox].TDHR = - (uint32_t)((uint32_t)data[7] << CAN_TDH0R_DATA7_Pos) | - (uint32_t)((uint32_t)data[6] << CAN_TDH0R_DATA6_Pos) | - (uint32_t)((uint32_t)data[5] << CAN_TDH0R_DATA5_Pos) | - (uint32_t)((uint32_t)data[4] << CAN_TDH0R_DATA4_Pos); + (uint32_t)((uint32_t)message.data_8[7] << CAN_TDH0R_DATA7_Pos) | + (uint32_t)((uint32_t)message.data_8[6] << CAN_TDH0R_DATA6_Pos) | + (uint32_t)((uint32_t)message.data_8[5] << CAN_TDH0R_DATA5_Pos) | + (uint32_t)((uint32_t)message.data_8[4] << CAN_TDH0R_DATA4_Pos); hcan->sTxMailBox[transmit_mailbox].TDLR = - (uint32_t)((uint32_t)data[3] << CAN_TDL0R_DATA3_Pos) | - (uint32_t)((uint32_t)data[2] << CAN_TDL0R_DATA2_Pos) | - (uint32_t)((uint32_t)data[1] << CAN_TDL0R_DATA1_Pos) | - (uint32_t)((uint32_t)data[0] << CAN_TDL0R_DATA0_Pos); + (uint32_t)((uint32_t)message.data_8[3] << CAN_TDL0R_DATA3_Pos) | + (uint32_t)((uint32_t)message.data_8[2] << CAN_TDL0R_DATA2_Pos) | + (uint32_t)((uint32_t)message.data_8[1] << CAN_TDL0R_DATA1_Pos) | + (uint32_t)((uint32_t)message.data_8[0] << CAN_TDL0R_DATA0_Pos); SET_BIT(hcan->sTxMailBox[transmit_mailbox].TIR, CAN_TI0R_TXRQ); return Status::Error; @@ -281,12 +281,6 @@ Status CANBus::write(uint16_t message_id, const uint8_t data[], uint32_t size) okay(); } -Status CANBus::write(const Message& message) -{ - return write(message.id, message.data, message.size); -} - - uint32_t CANBus::getAvailableForRead(uint32_t rx_fifo) { if (state != CANBus::State::Ready) @@ -332,14 +326,14 @@ Status CANBus::read(Message& message_out, uint32_t rx_fifo) warn("No mailboxes with available messages"); } - message_out.data[0] = (uint8_t)((CAN_RDL0R_DATA0 & hcan->sFIFOMailBox[rx_fifo].RDLR) >> CAN_RDL0R_DATA0_Pos); - message_out.data[1] = (uint8_t)((CAN_RDL0R_DATA1 & hcan->sFIFOMailBox[rx_fifo].RDLR) >> CAN_RDL0R_DATA1_Pos); - message_out.data[2] = (uint8_t)((CAN_RDL0R_DATA2 & hcan->sFIFOMailBox[rx_fifo].RDLR) >> CAN_RDL0R_DATA2_Pos); - message_out.data[3] = (uint8_t)((CAN_RDL0R_DATA3 & hcan->sFIFOMailBox[rx_fifo].RDLR) >> CAN_RDL0R_DATA3_Pos); - message_out.data[4] = (uint8_t)((CAN_RDH0R_DATA4 & hcan->sFIFOMailBox[rx_fifo].RDHR) >> CAN_RDH0R_DATA4_Pos); - message_out.data[5] = (uint8_t)((CAN_RDH0R_DATA5 & hcan->sFIFOMailBox[rx_fifo].RDHR) >> CAN_RDH0R_DATA5_Pos); - message_out.data[6] = (uint8_t)((CAN_RDH0R_DATA6 & hcan->sFIFOMailBox[rx_fifo].RDHR) >> CAN_RDH0R_DATA6_Pos); - message_out.data[7] = (uint8_t)((CAN_RDH0R_DATA7 & hcan->sFIFOMailBox[rx_fifo].RDHR) >> CAN_RDH0R_DATA7_Pos); + message_out.data_8[0] = (uint8_t)((CAN_RDL0R_DATA0 & hcan->sFIFOMailBox[rx_fifo].RDLR) >> CAN_RDL0R_DATA0_Pos); + message_out.data_8[1] = (uint8_t)((CAN_RDL0R_DATA1 & hcan->sFIFOMailBox[rx_fifo].RDLR) >> CAN_RDL0R_DATA1_Pos); + message_out.data_8[2] = (uint8_t)((CAN_RDL0R_DATA2 & hcan->sFIFOMailBox[rx_fifo].RDLR) >> CAN_RDL0R_DATA2_Pos); + message_out.data_8[3] = (uint8_t)((CAN_RDL0R_DATA3 & hcan->sFIFOMailBox[rx_fifo].RDLR) >> CAN_RDL0R_DATA3_Pos); + message_out.data_8[4] = (uint8_t)((CAN_RDH0R_DATA4 & hcan->sFIFOMailBox[rx_fifo].RDHR) >> CAN_RDH0R_DATA4_Pos); + message_out.data_8[5] = (uint8_t)((CAN_RDH0R_DATA5 & hcan->sFIFOMailBox[rx_fifo].RDHR) >> CAN_RDH0R_DATA5_Pos); + message_out.data_8[6] = (uint8_t)((CAN_RDH0R_DATA6 & hcan->sFIFOMailBox[rx_fifo].RDHR) >> CAN_RDH0R_DATA6_Pos); + message_out.data_8[7] = (uint8_t)((CAN_RDH0R_DATA7 & hcan->sFIFOMailBox[rx_fifo].RDHR) >> CAN_RDH0R_DATA7_Pos); message_out.id = CAN_RI0R_STID & hcan->sFIFOMailBox[rx_fifo].RIR >> CAN_RI0R_STID_Pos; diff --git a/lib/CAN/Can.h b/lib/CAN/Can.h index 9dcb64b..4f78cd2 100644 --- a/lib/CAN/Can.h +++ b/lib/CAN/Can.h @@ -66,7 +66,14 @@ class CANBus public: uint32_t id = 0; uint32_t size = 0; - uint8_t data[8] = {0}; + + union + { + uint64_t data_64; + uint32_t data_32[2]; + uint16_t data_16[4]; + uint8_t data_8[8]; + }; }; /** @@ -75,7 +82,6 @@ class CANBus * @param size number of bytes to write. * @returns Okay if the message was successfully posted to a mailbox. If Error, must retry when `getAvailableForWrite()` returns > 0 */ - static Status write(uint16_t message_id, const uint8_t data[], uint32_t size); static Status write(const Message& message); /** @@ -94,6 +100,7 @@ class CANBus static void register_msg_received_callback(MessageReceivedCallbackType callback); private: + enum State { None, diff --git a/lib/CAN/README.md b/lib/CAN/README.md index 79de75b..edf7b47 100644 --- a/lib/CAN/README.md +++ b/lib/CAN/README.md @@ -39,6 +39,13 @@ void loop(){ // do something with the message... // ... } + + // checks if there are spaces available to transmit + if (CANBus::getAvailableForWrite() > 0) + { + CANBus::write({.id = 0xde, .size = 8, .data_32 = {1, 2}}); + } + delay(100); }