Go to the source code of this file.
|
| int | init_CANBus (CANBus bus) |
| | Initializes the CANBus struct. More...
|
| |
| int | load_dbc_file (CANBus bus, const unsigned char *filename) |
| | Loads a DBC file into the CAN database. Makes the messages and signals and stores them in the list. More...
|
| |
| int | add_message (CANBus bus, CAN_Message_Template message) |
| | Adds a message to the CAN message list. More...
|
| |
| int | add_message_lop (CANBus bus, int id, int dlc, int ide, int rtr, const char *name, const char *sender, int signal_count, CAN_Signal_Template *signals) |
| | Adds a message to the CAN message list by passing in the individual message parameters hence "LOP" (List of Parameters) More...
|
| |
| int | send_CAN_message (CANBus bus, CANProtocol protocol, uint32_t id, uint8_t *data, uint8_t len) |
| | Sends a CAN message. More...
|
| |
| void | receive_CAN_message (CAN_RxHeaderTypeDef *RxHeader, uint8_t *RxData, CANBus bus) |
| | Receives a CAN message. More...
|
| |
| void | parseMessage (CAN_MessageList *messages, CAN_Message *can_message) |
| | Parses a CAN message. More...
|
| |
| void | parseSignals (CAN_Message_Template *message, CAN_Message *can_message) |
| | Parses signals in a CAN message. More...
|
| |
| void | parseSignal (CAN_Signal_Template *signal, CAN_Signal *can_signal, CAN_Message *can_message) |
| | Parses a CAN signal. More...
|
| |
| void | print_CAN_Messages_Lists () |
| | Prints the CAN message list. More...
|
| |
◆ add_message()
Adds a message to the CAN message list.
- Parameters
-
| bus | The CAN bus to add the message to |
| message | The message to add |
- Returns
- int 0 if the message was added successfully, -1 if the bus is invalid
Definition at line 49 of file Can.c.
49 {
50
53 return 0;
54}
CAN_MessageList can_messages[MAX_BUS]
CAN_Message_Template messages[MAX_MESSAGES]
◆ add_message_lop()
| int add_message_lop |
( |
CANBus |
bus, |
|
|
int |
id, |
|
|
int |
dlc, |
|
|
int |
ide, |
|
|
int |
rtr, |
|
|
const char * |
name, |
|
|
const char * |
sender, |
|
|
int |
signal_count, |
|
|
CAN_Signal_Template * |
signals |
|
) |
| |
Adds a message to the CAN message list by passing in the individual message parameters hence "LOP" (List of Parameters)
- Parameters
-
| bus | The CAN bus to add the message to |
| id | The ID of the message |
| dlc | The Data Length Code of the message |
| ide | The Identifier Extension of the message |
| rtr | The Remote Transmission Request of the message |
| name | The name of the message |
| sender | The sender of the message |
| signal_count | The number of signals in the message |
| signals | The signals in the message |
- Returns
- int 0 if the message was added successfully, -1 if the bus is invalid
Definition at line 56 of file Can.c.
57{
58
64 strcpy(message.
name, name);
65 strcpy(message.
sender, sender);
68
69
70 for (int i = 0; i < signal_count; i++) {
73 }
74
75
77}
int add_message(CANBus bus, CAN_Message_Template message)
Adds a message to the CAN message list.
static TelemetrySignal signals[MAX_TELEMETRY_SIGNALS]
char sender[MAX_NODE_NAME_LENGTH]
CAN_Signal_Template signals[MAX_SIGNALS]
char name[MAX_MESSAGE_NAME_LENGTH]
◆ init_CANBus()
Initializes the CANBus struct.
- Parameters
-
| bus | The CAN bus to initialize |
- Returns
- int 0 if the CAN bus was initialized successfully, -1 if the bus is invalid
Definition at line 18 of file Can.c.
18 {
19
22
23 return 0;
24}
◆ load_dbc_file()
| int load_dbc_file |
( |
CANBus |
bus, |
|
|
const unsigned char * |
filename |
|
) |
| |
Loads a DBC file into the CAN database. Makes the messages and signals and stores them in the list.
- Parameters
-
| bus | The CAN bus to load the DBC file into |
| filename | The name of the DBC file to load |
- Returns
- int 0 if the DBC file was loaded successfully, -1 if the file could not be opened
Definition at line 26 of file Can.c.
27{
28
29 #ifdef TEST_MODE
30
31 #else
32 unsigned char* dbc_contents = filename;
33 #endif
34
35
36 #ifndef TEST_MODE
37
39 #endif
40
41 #ifdef TEST_MODE
42
43 #endif
44
46 return 0;
47}
int parseDbcFile(CAN_MessageList *messages, const unsigned char *dbc_contents)
Parses a DBC (CAN database) file and populates the DBC structure.
void print_CAN_MessageList(const CAN_MessageList *messages)
Prints the contents of a CAN message list (aka DBC file).
◆ parseMessage()
Parses a CAN message.
- Parameters
-
| messages | The CAN message list |
| can_message | The CAN message to parse |
Definition at line 147 of file Can.c.
147 {
148
149
152 if (msg->
id == can_message->
header.StdId || msg->
id == can_message->
header.ExtId) {
153
156 return;
157 }
158 }
159}
void parseSignals(CAN_Message_Template *message, CAN_Message *can_message)
Parses signals in a CAN message.
CAN_Message_Template * template
CAN_RxHeaderTypeDef header
◆ parseSignal()
Parses a CAN signal.
- Parameters
-
| signal | The CAN signal template |
| can_signal | The CAN signal to parse |
| can_message | The CAN message to parse |
Definition at line 172 of file Can.c.
172 {
174
175
176 uint64_t raw_data = 0;
177 for (
int i = signal->
start_bit; i < signal->start_bit + signal->
length; i++) {
178 raw_data |= ((can_message->
data[i / 8] >> (i % 8)) & 1) << (i - signal->
start_bit);
179 }
180
181
182 if (signal->
endian == 1) {
183
184 uint64_t reversed_data = 0;
185 for (
int i = 0; i < signal->
length; i++) {
186 reversed_data |= ((raw_data >> i) & 1) << (signal->
length - i - 1);
187 }
188 raw_data = reversed_data;
189 }
190
191
193
194 raw_data = (raw_data << (64 - signal->
length)) >> (64 - signal->
length);
195 }
196
197
199
200}
CAN_Signal_Template * template
◆ parseSignals()
Parses signals in a CAN message.
- Parameters
-
| message | The CAN message template |
| can_message | The CAN message to parse |
Definition at line 162 of file Can.c.
162 {
163
166
168 }
169}
void parseSignal(CAN_Signal_Template *signal, CAN_Signal *can_signal, CAN_Message *can_message)
Parses a CAN signal.
◆ print_CAN_Messages_Lists()
| void print_CAN_Messages_Lists |
( |
| ) |
|
Prints the CAN message list.
Definition at line 202 of file Can.c.
202 {
203 for (
int i = 0; i <
MAX_BUS; i++) {
204
207
210
211 }
212
213 }
215
216 }
217 }
218}
◆ receive_CAN_message()
| void receive_CAN_message |
( |
CAN_RxHeaderTypeDef * |
header, |
|
|
uint8_t * |
data, |
|
|
CANBus |
bus |
|
) |
| |
Receives a CAN message.
- Parameters
-
| RxHeader | The CAN Rx header |
| RxData | The CAN Rx data |
| bus | The CAN bus to receive the message on |
Definition at line 113 of file Can.c.
113 {
115 can_message->
header = *RxHeader;
116 memcpy(can_message->
data, RxData, 8);
117
118
119 char data_hex[17];
120 for (int i = 0; i < RxHeader->DLC && i < 8; i++) {
121 sprintf(&data_hex[i*2], "%02X", RxData[i]);
122 }
123 data_hex[RxHeader->DLC * 2] = '\0';
124
125
127 RxHeader->StdId, RxHeader->DLC, data_hex);
128
129
131
132
133
134
135
138
139 }
140
141
142
143 free(can_message);
144}
void parseMessage(CAN_MessageList *messages, CAN_Message *can_message)
Parses a CAN message.
◆ send_CAN_message()
| int send_CAN_message |
( |
CANBus |
bus, |
|
|
CANProtocol |
protocol, |
|
|
uint32_t |
id, |
|
|
uint8_t * |
data, |
|
|
uint8_t |
len |
|
) |
| |
Sends a CAN message.
- Parameters
-
| bus | The CAN bus to send the message on |
| protocol | The CAN protocol to use |
| id | The ID of the message |
| data | The data to send |
| len | The length of the data |
- Returns
- int 0 if the message was sent successfully, -1 if the bus is invalid
Definition at line 80 of file Can.c.
80 {
81 CAN_TxHeaderTypeDef TxHeader;
83 TxHeader.StdId = id;
84 TxHeader.IDE = CAN_ID_STD;
85 }
else if (protocol ==
CAN_2B) {
86 TxHeader.ExtId = id;
87 TxHeader.IDE = CAN_ID_EXT;
88 } else {
89
90 return -1;
91 }
92
93 TxHeader.RTR = CAN_RTR_DATA;
94 TxHeader.DLC = len;
95
96
98 for (int i = 0; i < len; i++) {
99 printf("%02X ", data[i]);
100 }
101 printf("\r\n");
102
103 #ifndef TEST_MODE
104
106 return -2;
107 }
108 #endif
109 return 0;
110}
#define ANSI_COLOR_YELLOW
int send_CAN_message_helper(CANBus bus, CAN_TxHeaderTypeDef *TxHeader, uint8_t *data)
◆ can_messages
Definition at line 16 of file Can.c.