MCU
Loading...
Searching...
No Matches
CAN_testing_utilities.h
Go to the documentation of this file.
1#include <Arduino.h>
2
16inline 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)
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}
42
54inline CAN_message_t generate_can_msg_from_uint_32s(uint32_t first, uint32_t second, bool use_little_endian)
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}
CAN_message_t generate_can_msg_from_uint_32s(uint32_t first, uint32_t second, bool use_little_endian)
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)