AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
flex_can_t4_plugin.cpp
Go to the documentation of this file.
1//================================================================================================
8//================================================================================================
9
11#include "FlexCAN_T4.hpp"
13
14namespace isobus
15{
16#if defined(__IMXRT1062__)
17 FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_512> FlexCANT4Plugin::can0;
18 FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_512> FlexCANT4Plugin::can1;
19 FlexCAN_T4<CAN3, RX_SIZE_256, TX_SIZE_512> FlexCANT4Plugin::can2;
20#elif defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
21 FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_512> FlexCANT4Plugin::can0;
22 FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_512> FlexCANT4Plugin::can1;
23#endif
24
25 FlexCANT4Plugin::FlexCANT4Plugin(std::uint8_t channel) :
26 selectedChannel(channel)
27 {
28 }
29
31 {
32 return isOpen;
33 }
34
36 {
37 // Flex CAN doesn't have a way to stop it...
38 isOpen = false;
39 }
40
42 {
43 if (0 == selectedChannel)
44 {
45 can0.begin();
46 can0.setBaudRate(250000);
47 isOpen = true;
48 }
49 else if (1 == selectedChannel)
50 {
51 can1.begin();
52 can1.setBaudRate(250000);
53 isOpen = true;
54 }
55#if defined(__IMXRT1062__)
56 else if (2 == selectedChannel)
57 {
58 can2.begin();
59 can2.setBaudRate(250000);
60 isOpen = true;
61 }
62#endif
63 else
64 {
65 LOG_CRITICAL("[FlexCAN]: Invalid Channel Selected");
66 }
67 }
68
70 {
71 CAN_message_t message;
72 bool retVal = false;
73
74 if (0 == selectedChannel)
75 {
76 retVal = can0.read(message);
77 canFrame.channel = 0;
78 }
79 else if (1 == selectedChannel)
80 {
81 retVal = can1.read(message);
82 canFrame.channel = 1;
83 }
84#if defined(__IMXRT1062__)
85 else if (2 == selectedChannel)
86 {
87 retVal = can2.read(message);
88 canFrame.channel = 2;
89 }
90#endif
91
92 memcpy(canFrame.data, message.buf, 8);
93 canFrame.identifier = message.id;
94 canFrame.dataLength = message.len;
95 canFrame.isExtendedFrame = message.flags.extended;
96 return retVal;
97 }
98
100 {
101 CAN_message_t message;
102 bool retVal = false;
103
104 message.id = canFrame.identifier;
105 message.len = canFrame.dataLength;
106 message.flags.extended = true;
107 message.seq = true; // Ask for sequential transmission
108 memcpy(message.buf, canFrame.data, canFrame.dataLength);
109
110 if (0 == selectedChannel)
111 {
112 retVal = can0.write(message);
113 }
114 else if (1 == selectedChannel)
115 {
116 retVal = can1.write(message);
117 }
118#if defined(__IMXRT1062__)
119 else if (2 == selectedChannel)
120 {
121 retVal = can2.write(message);
122 }
123#endif
124 return retVal;
125 }
126}
A class that acts as a logging sink. The intent is that someone could make their own derived class of...
A CAN frame for interfacing with a hardware layer, like socket CAN or other interface.
std::uint8_t dataLength
The length of the data used in the frame.
std::uint32_t identifier
The 32 bit identifier of the frame.
bool isExtendedFrame
Denotes if the frame is extended format.
std::uint8_t data[8]
The data payload of the frame.
std::uint8_t channel
The CAN channel index associated with the frame.
bool write_frame(const isobus::CANMessageFrame &canFrame) override
Writes a frame to the bus (synchronous)
bool isOpen
Tracks if the connection is open/connected.
bool get_is_valid() const override
Returns if the connection with the hardware is valid.
void close() override
Closes the connection to the hardware.
FlexCANT4Plugin(std::uint8_t channel)
Constructor for the FlexCANT4Plugin.
std::uint8_t selectedChannel
The channel that this plugin is assigned to.
bool read_frame(isobus::CANMessageFrame &canFrame) override
Returns a frame from the hardware (synchronous), or false if no frame can be read.
void open() override
Connects to the hardware you specified in the constructor's channel argument.
An interface for using FlexCAN_T4 on a Teensy4/4.1 device.
This namespace encompasses all of the ISO11783 stack's functionality to reduce global namespace pollu...