MCU
Loading...
Searching...
No Matches
Functions
CAN_testing_utilities.h File Reference
#include <Arduino.h>
Include dependency graph for CAN_testing_utilities.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

CAN_message_t generate_can_msg_from_uint_16s (uint16_t first, uint16_t second, uint16_t third, uint16_t fourth, bool use_little_endian)
 
CAN_message_t generate_can_msg_from_uint_32s (uint32_t first, uint32_t second, bool use_little_endian)
 

Function Documentation

◆ generate_can_msg_from_uint_16s()

CAN_message_t generate_can_msg_from_uint_16s ( uint16_t  first,
uint16_t  second,
uint16_t  third,
uint16_t  fourth,
bool  use_little_endian 
)
inline

Generates a CAN message containing the given data. This handles the byte-swapping (because of big-endian) automatically.

Parameters
firstThe leftmost 16 bits of this CAN message's 64 bits.
secondThe second-leftmost 16 bits of this CAN message's 64 bits.
thirdThe second-rightmost 16 bits of this CAN message's 64 bits.
fourthThe rightmost 16 bits of this CAN message's 64 bits.
use_little_endianTrue if the bytes will be READ OFF OF THE CAN LINE using little-endian. If the unpack function expects the number 0013 to be on the CAN line as 00 13, then set this to true. If the unpack function expects the number 0013 to be on the CAN line as 13 00, then set this to false.
Returns
A CAN_message_t with ONLY its data set (not the ID or any other bits)

Definition at line 16 of file CAN_testing_utilities.h.

17{
18
19 CAN_message_t can_msg;
20
21 uint16_t actual_first = first, actual_second = second, actual_third = third, actual_fourth = fourth;
22
23 // By default, uint16s are saved in memory using big-endian, by byte. This means that if you say "uint16_t x = 0x0013", this will be saved
24 // in memory as 1300. If the bits SHOULD be on the CAN line using big-endian, then this is fine. However, if the bits SHOULD be on the CAN
25 // line as little-endian, then we need to invert the bytes before copying the memory into the CAN buffer.
26 if (use_little_endian) {
27 actual_first = (uint16_t) ( (0x00FFU & first) << 8U | (0xFF00U & first) >> 8U );
28 actual_second = (uint16_t) ( (0x00FFU & second) << 8U | (0xFF00U & second) >> 8U );
29 actual_third = (uint16_t) ( (0x00FFU & third) << 8U | (0xFF00U & third) >> 8U );
30 actual_fourth = (uint16_t) ( (0x00FFU & fourth) << 8U | (0xFF00U & fourth) >> 8U );
31 }
32
33 // Places expected values into buffer
34 memcpy(&can_msg.buf, &actual_first, sizeof(uint16_t));
35 memcpy(&can_msg.buf[2], &actual_second, sizeof(uint16_t));
36 memcpy(&can_msg.buf[4], &actual_third, sizeof(uint16_t));
37 memcpy(&can_msg.buf[6], &actual_fourth, sizeof(uint16_t));
38
39 return can_msg;
40
41}

◆ generate_can_msg_from_uint_32s()

CAN_message_t generate_can_msg_from_uint_32s ( uint32_t  first,
uint32_t  second,
bool  use_little_endian 
)
inline

Generates a CAN message containing the given data. This handles the byte-swapping (because of big-endian) automatically.

Parameters
firstThe leftmost 32 bits of this CAN message's 64 bits.
secondThe second-leftmost 32 bits of this CAN message's 64 bits.
use_little_endianTrue if the bytes will be READ OFF OF THE CAN LINE using little-endian. If the unpack function expects the number 00000013 (hex) to be on the CAN line as 00 00 00 13, then set this to true. If the unpack function expects the number 00000013 to be on the CAN line as 13 00 00 00, then set this to false.
Returns
A CAN_message_t with ONLY its data set (not the ID or any other bits)

Definition at line 54 of file CAN_testing_utilities.h.

55{
56
57 CAN_message_t can_msg;
58
59 uint32_t actual_first = first, actual_second = second;
60
61 // Expected values in CAN message. These are BIG-ENDIAN, by BYTE. The number 19 in hex
62 // is 0013, but we must declare it as 0x1300U with the least-sig byte and most-sig byte switched.
63 if (use_little_endian) {
64 actual_first = (uint32_t) ( (0x000000FFU & first) << 24U | (0x0000FF00U & first) << 8U | (0x00FF0000U & first) >> 8U | (0xFF000000U & first) >> 24U);
65 actual_second = (uint32_t) ( (0x000000FFU & second) << 24U | (0x0000FF00U & second) << 8U | (0x00FF0000U & second) >> 8U | (0xFF000000U & second) >> 24U);
66 }
67
68 // Places expected values into buffer
69 memcpy(&can_msg.buf, &actual_first, sizeof(uint32_t));
70 memcpy(&can_msg.buf[4], &actual_second, sizeof(uint32_t));
71
72 return can_msg;
73
74}