-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflex_can_t4_plugin.cpp
126 lines (114 loc) · 2.85 KB
/
flex_can_t4_plugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//================================================================================================
/// @file flex_can_t4_plugin.cpp
///
/// @brief An interface for using Teensy4/4.1 CAN hardware
/// @author Adrian Del Grosso
///
/// @copyright 2023 The Open-Agriculture Developers
//================================================================================================
#include "flex_can_t4_plugin.hpp"
#include "FlexCAN_T4.hpp"
#include "can_stack_logger.hpp"
namespace isobus
{
#if defined(__IMXRT1062__)
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_512> FlexCANT4Plugin::can0;
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_512> FlexCANT4Plugin::can1;
FlexCAN_T4<CAN3, RX_SIZE_256, TX_SIZE_512> FlexCANT4Plugin::can2;
#elif defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_512> FlexCANT4Plugin::can0;
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_512> FlexCANT4Plugin::can1;
#endif
FlexCANT4Plugin::FlexCANT4Plugin(std::uint8_t channel) :
selectedChannel(channel)
{
}
bool FlexCANT4Plugin::get_is_valid() const
{
return isOpen;
}
void FlexCANT4Plugin::close()
{
// Flex CAN doesn't have a way to stop it...
isOpen = false;
}
void FlexCANT4Plugin::open()
{
if (0 == selectedChannel)
{
can0.begin();
can0.setBaudRate(250000);
isOpen = true;
}
else if (1 == selectedChannel)
{
can1.begin();
can1.setBaudRate(250000);
isOpen = true;
}
#if defined(__IMXRT1062__)
else if (2 == selectedChannel)
{
can2.begin();
can2.setBaudRate(250000);
isOpen = true;
}
#endif
else
{
LOG_CRITICAL("[FlexCAN]: Invalid Channel Selected");
}
}
bool FlexCANT4Plugin::read_frame(isobus::CANMessageFrame &canFrame)
{
CAN_message_t message;
bool retVal = false;
if (0 == selectedChannel)
{
retVal = can0.read(message);
canFrame.channel = 0;
}
else if (1 == selectedChannel)
{
retVal = can1.read(message);
canFrame.channel = 1;
}
#if defined(__IMXRT1062__)
else if (2 == selectedChannel)
{
retVal = can2.read(message);
canFrame.channel = 2;
}
#endif
memcpy(canFrame.data, message.buf, 8);
canFrame.identifier = message.id;
canFrame.dataLength = message.len;
canFrame.isExtendedFrame = message.flags.extended;
return retVal;
}
bool FlexCANT4Plugin::write_frame(const isobus::CANMessageFrame &canFrame)
{
CAN_message_t message;
bool retVal = false;
message.id = canFrame.identifier;
message.len = canFrame.dataLength;
message.flags.extended = true;
message.seq = true; // Ask for sequential transmission
memcpy(message.buf, canFrame.data, canFrame.dataLength);
if (0 == selectedChannel)
{
retVal = can0.write(message);
}
else if (1 == selectedChannel)
{
retVal = can1.write(message);
}
#if defined(__IMXRT1062__)
else if (2 == selectedChannel)
{
retVal = can2.write(message);
}
#endif
return retVal;
}
}