Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CANBus api first revision #3

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
44 changes: 19 additions & 25 deletions lib/CAN/Can.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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
roeyb1 marked this conversation as resolved.
Show resolved Hide resolved
// #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;
Expand All @@ -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)
Expand Down Expand Up @@ -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;

Expand Down
11 changes: 9 additions & 2 deletions lib/CAN/Can.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};
};

/**
Expand All @@ -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);

/**
Expand All @@ -94,6 +100,7 @@ class CANBus

static void register_msg_received_callback(MessageReceivedCallbackType callback);
private:

enum State
{
None,
Expand Down
7 changes: 7 additions & 0 deletions lib/CAN/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down