Mbed OS Reference
Loading...
Searching...
No Matches
InterfaceCAN.h
1/*
2 * Mbed-OS Microcontroller Library
3 * Copyright (c) 2021 Embedded Planet
4 * Copyright (c) 2021 ARM Limited
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License
18 */
19
20#ifndef MBED_INTERFACE_CAN_H_
21#define MBED_INTERFACE_CAN_H_
22
23#include "hal/can_helper.h"
24
25#include <cstring>
26
27#include "platform/Callback.h"
28
29namespace mbed {
30
31#ifndef FEATURE_EXPERIMENTAL_API
32// Forward declare CAN
33class CAN;
34#endif
35
36/** \defgroup drivers-public-api-can CAN
37 * \ingroup drivers-public-api
38 */
39
40/**
41 * \defgroup drivers_CANMessage CANMessage class
42 * \ingroup drivers-public-api-can
43 * @{
44 */
45
46/** CANMessage class
47 */
48class CANMessage : public CAN_Message {
49
50public:
51 /** Creates empty CAN message.
52 */
54 {
55 len = 8U;
56 type = CANData;
57 format = CANStandard;
58 id = 0U;
59 memset(data, 0, 8);
60 }
61
62 /** Creates CAN message with specific content.
63 *
64 * @param _id Message ID
65 * @param _data Mesaage Data
66 * @param _len Message Data length
67 * @param _type Type of Data: Use enum CANType for valid parameter values
68 * @param _format Data Format: Use enum CANFormat for valid parameter values
69 */
70 CANMessage(unsigned int _id, const unsigned char *_data, unsigned char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard)
71 {
72 len = (_len > 8) ? 8 : _len;
73 type = _type;
74 format = _format;
75 id = _id;
76 memcpy(data, _data, len);
77 }
78
79
80 /** Creates CAN message with specific content.
81 *
82 * @param _id Message ID
83 * @param _data Mesaage Data
84 * @param _len Message Data length
85 * @param _type Type of Data: Use enum CANType for valid parameter values
86 * @param _format Data Format: Use enum CANFormat for valid parameter values
87 */
88 CANMessage(unsigned int _id, const char *_data, unsigned char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard)
89 {
90 len = (_len > 8) ? 8 : _len;
91 type = _type;
92 format = _format;
93 id = _id;
94 memcpy(data, _data, len);
95 }
96
97 /** Creates CAN remote message.
98 *
99 * @param _id Message ID
100 * @param _format Data Format: Use enum CANType for valid parameter values
101 */
102 CANMessage(unsigned int _id, CANFormat _format = CANStandard)
103 {
104 len = 0;
105 type = CANRemote;
106 format = _format;
107 id = _id;
108 memset(data, 0, 8);
109 }
110
111 /**
112 * "Deep" comparison operator (ie: compare value of each data member)
113 */
114 bool operator ==(const CANMessage &b) const
115 {
116 if (id != b.id) {
117 return false;
118 }
119 if (len != b.len) {
120 return false;
121 }
122 if (format != b.format) {
123 return false;
124 }
125 if (type != b.type) {
126 return false;
127 }
128 if (memcmp(data, b.data, len) != 0) {
129 return false;
130 }
131
132 return true;
133 }
134
135 bool operator !=(const CANMessage &b) const
136 {
137 return !(*this == b);
138 }
139};
140
141#if DEVICE_CAN_FD
142
143/** CANFDMessage class
144 */
146
147public:
148 /** Creates empty CANFD message.
149 */
151 {
152 len = 64U;
153 type = CANData;
154 format = CANStandard;
155 id = 0U;
156 memset(data, 0, 64);
157 }
158
159 /** Creates CANFD message with specific content.
160 *
161 * @param _id Message ID
162 * @param _data Mesaage Data
163 * @param _len Message Data length
164 * @param _type Type of Data: Use enum CANType for valid parameter values
165 * @param _format Data Format: Use enum CANFormat for valid parameter values
166 */
167 CANFDMessage(unsigned int _id, const unsigned char *_data, unsigned char _len = 64, CANType _type = CANData, CANFormat _format = CANStandard)
168 {
169 len = (_len > 64) ? 64 : _len;
170 type = _type;
171 format = _format;
172 id = _id;
173 memcpy(data, _data, len);
174 }
175
176
177 /** Creates CANFD message with specific content.
178 *
179 * @param _id Message ID
180 * @param _data Mesaage Data
181 * @param _len Message Data length
182 * @param _type Type of Data: Use enum CANType for valid parameter values
183 * @param _format Data Format: Use enum CANFormat for valid parameter values
184 */
185 CANFDMessage(unsigned int _id, const char *_data, unsigned char _len = 64, CANType _type = CANData, CANFormat _format = CANStandard)
186 {
187 len = (_len > 64) ? 64 : _len;
188 type = _type;
189 format = _format;
190 id = _id;
191 memcpy(data, _data, len);
192 }
193
194 /** Creates CANFD remote message.
195 *
196 * @param _id Message ID
197 * @param _format Data Format: Use enum CANType for valid parameter values
198 */
199 CANFDMessage(unsigned int _id, CANFormat _format = CANStandard)
200 {
201 len = 0;
202 type = CANRemote;
203 format = _format;
204 id = _id;
205 memset(data, 0, 64);
206 }
207
208 /**
209 * "Deep" comparison operator (ie: compare value of each data member)
210 */
211 bool operator ==(const CANFDMessage &b) const
212 {
213 if (id != b.id) {
214 return false;
215 }
216 if (len != b.len) {
217 return false;
218 }
219 if (format != b.format) {
220 return false;
221 }
222 if (type != b.type) {
223 return false;
224 }
225 if (memcmp(data, b.data, len) != 0) {
226 return false;
227 }
228
229 return true;
230 }
231
232 bool operator !=(const CANFDMessage &b) const
233 {
234 return !(*this == b);
235 }
236};
237
238#endif
239
240/** @}*/
241
242namespace interface {
243
244/* Having this as a struct allows interface::CAN and/or mbed::CAN to inherit the enums */
245struct can {
246
247 enum Mode {
248 Reset = 0,
249 Normal,
250 Silent,
251 LocalTest,
252 GlobalTest,
253 SilentTest
254 };
255
256 enum IrqType {
257 RxIrq = 0,
258 TxIrq,
259 EwIrq,
260 DoIrq,
261 WuIrq,
262 EpIrq,
263 AlIrq,
264 BeIrq,
265 IdIrq,
266
267 IrqCnt
268 };
269
270// Prevent slicing and user creation of base class.
271protected:
272 can() = default;
273 ~can() = default;
274
275public:
276
277 /* Copy constructor and copy assignment operators will be deleted in subclasses as well */
278 can(const can &) = delete;
279 can &operator=(const can &) = delete;
280
281};
282
283#ifdef FEATURE_EXPERIMENTAL_API
284
285// Pure virtual interface for CAN
286struct CAN : public interface::can {
287
288 virtual ~CAN() = default;
289 virtual int frequency(int hz) = 0;
290 virtual int write(CANMessage msg) = 0;
291 virtual int read(CANMessage &msg, int handle = 0) = 0;
292 virtual void reset() = 0;
293 virtual void monitor(bool silent) = 0;
294 virtual int mode(Mode mode) = 0;
295 virtual int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0) = 0;
296 virtual unsigned char rderror() = 0;
297 virtual unsigned char tderror() = 0;
298 virtual void attach(Callback<void()> func, IrqType type = IrqType::RxIrq) = 0;
299};
300
301#else
302using CAN = ::mbed::CAN;
303#endif
304
305} // namespace interface
306
307#if defined(FEATURE_EXPERIMENTAL_API) && !DEVICE_CAN
308using CAN = interface::CAN;
309#endif
310
311} // namespace mbed
312
313#endif /* MBED_INTERFACE_CAN_H_ */
CANFDMessage class.
CANFDMessage(unsigned int _id, const char *_data, unsigned char _len=64, CANType _type=CANData, CANFormat _format=CANStandard)
Creates CANFD message with specific content.
CANFDMessage(unsigned int _id, CANFormat _format=CANStandard)
Creates CANFD remote message.
bool operator==(const CANFDMessage &b) const
"Deep" comparison operator (ie: compare value of each data member)
CANFDMessage(unsigned int _id, const unsigned char *_data, unsigned char _len=64, CANType _type=CANData, CANFormat _format=CANStandard)
Creates CANFD message with specific content.
CANFDMessage()
Creates empty CANFD message.
A can bus client, used for communicating with can devices.
Definition CAN.h:45
CANMessage class.
CANMessage(unsigned int _id, const char *_data, unsigned char _len=8, CANType _type=CANData, CANFormat _format=CANStandard)
Creates CAN message with specific content.
bool operator==(const CANMessage &b) const
"Deep" comparison operator (ie: compare value of each data member)
CANMessage(unsigned int _id, CANFormat _format=CANStandard)
Creates CAN remote message.
CANMessage()
Creates empty CAN message.
CANMessage(unsigned int _id, const unsigned char *_data, unsigned char _len=8, CANType _type=CANData, CANFormat _format=CANStandard)
Creates CAN message with specific content.
Callback class based on template specialization.
Definition Callback.h:53
CANType
Values that represent CAN Type.
Definition can_helper.h:48
CANFormat
Values that represent CAN Format.
Definition can_helper.h:35
Holder for single CAN message.
Definition can_helper.h:61
unsigned char data[8]
Data field.
Definition can_helper.h:63
unsigned char len
Length of data field in bytes.
Definition can_helper.h:64
CANFormat format
Format CANFormat.
Definition can_helper.h:65
unsigned int id
29 bit identifier
Definition can_helper.h:62
CANType type
Type CANType.
Definition can_helper.h:66
unsigned char data[64]
Data field.
Definition can_helper.h:81
unsigned char len
Length of data field in bytes.
Definition can_helper.h:82
CANFormat format
Format CANFormat.
Definition can_helper.h:83
unsigned int id
29 bit identifier
Definition can_helper.h:80
CANType type
Type CANType.
Definition can_helper.h:84