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 *
48 * @note Synchronization level: Thread safe
49 */
50class CANMessage : public CAN_Message {
51
52public:
53 /** Creates empty CAN message.
54 */
56 {
57 len = 8U;
58 type = CANData;
59 format = CANStandard;
60 id = 0U;
61 memset(data, 0, 8);
62 }
63
64 /** Creates CAN message with specific content.
65 *
66 * @param _id Message ID
67 * @param _data Mesaage Data
68 * @param _len Message Data length
69 * @param _type Type of Data: Use enum CANType for valid parameter values
70 * @param _format Data Format: Use enum CANFormat for valid parameter values
71 */
72 CANMessage(unsigned int _id, const unsigned char *_data, unsigned char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard)
73 {
74 len = (_len > 8) ? 8 : _len;
75 type = _type;
76 format = _format;
77 id = _id;
78 memcpy(data, _data, len);
79 }
80
81
82 /** Creates CAN message with specific content.
83 *
84 * @param _id Message ID
85 * @param _data Mesaage Data
86 * @param _len Message Data length
87 * @param _type Type of Data: Use enum CANType for valid parameter values
88 * @param _format Data Format: Use enum CANFormat for valid parameter values
89 */
90 CANMessage(unsigned int _id, const char *_data, unsigned char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard)
91 {
92 len = (_len > 8) ? 8 : _len;
93 type = _type;
94 format = _format;
95 id = _id;
96 memcpy(data, _data, len);
97 }
98
99 /** Creates CAN remote message.
100 *
101 * @param _id Message ID
102 * @param _format Data Format: Use enum CANType for valid parameter values
103 */
104 CANMessage(unsigned int _id, CANFormat _format = CANStandard)
105 {
106 len = 0;
107 type = CANRemote;
108 format = _format;
109 id = _id;
110 memset(data, 0, 8);
111 }
112
113 /**
114 * "Deep" comparison operator (ie: compare value of each data member)
115 */
116 bool operator ==(const CANMessage &b) const
117 {
118 if (id != b.id) {
119 return false;
120 }
121 if (len != b.len) {
122 return false;
123 }
124 if (format != b.format) {
125 return false;
126 }
127 if (type != b.type) {
128 return false;
129 }
130 if (memcmp(data, b.data, len) != 0) {
131 return false;
132 }
133
134 return true;
135 }
136
137 bool operator !=(const CANMessage &b) const
138 {
139 return !(*this == b);
140 }
141};
142
143/** @}*/
144
145namespace interface {
146
147/* Having this as a struct allows interface::CAN and/or mbed::CAN to inherit the enums */
148struct can {
149
150 enum Mode {
151 Reset = 0,
152 Normal,
153 Silent,
154 LocalTest,
155 GlobalTest,
156 SilentTest
157 };
158
159 enum IrqType {
160 RxIrq = 0,
161 TxIrq,
162 EwIrq,
163 DoIrq,
164 WuIrq,
165 EpIrq,
166 AlIrq,
167 BeIrq,
168 IdIrq,
169
170 IrqCnt
171 };
172
173// Prevent slicing and user creation of base class.
174protected:
175 can() = default;
176 ~can() = default;
177
178public:
179
180 /* Copy constructor and copy assignment operators will be deleted in subclasses as well */
181 can(const can &) = delete;
182 can &operator=(const can &) = delete;
183
184};
185
186#ifdef FEATURE_EXPERIMENTAL_API
187
188// Pure virtual interface for CAN
189struct CAN : public interface::can {
190
191 virtual ~CAN() = default;
192 virtual int frequency(int hz) = 0;
193 virtual int write(CANMessage msg) = 0;
194 virtual int read(CANMessage &msg, int handle = 0) = 0;
195 virtual void reset() = 0;
196 virtual void monitor(bool silent) = 0;
197 virtual int mode(Mode mode) = 0;
198 virtual int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0) = 0;
199 virtual unsigned char rderror() = 0;
200 virtual unsigned char tderror() = 0;
201 virtual void attach(Callback<void()> func, IrqType type = IrqType::RxIrq) = 0;
202};
203
204#else
205using CAN = ::mbed::CAN;
206#endif
207
208} // namespace interface
209
210#if defined(FEATURE_EXPERIMENTAL_API) && !DEVICE_CAN
211using CAN = interface::CAN;
212#endif
213
214} // namespace mbed
215
216#endif /* MBED_INTERFACE_CAN_H_ */
A can bus client, used for communicating with can devices.
Definition: CAN.h:45
CANMessage class.
Definition: InterfaceCAN.h:50
CANMessage(unsigned int _id, const char *_data, unsigned char _len=8, CANType _type=CANData, CANFormat _format=CANStandard)
Creates CAN message with specific content.
Definition: InterfaceCAN.h:90
bool operator==(const CANMessage &b) const
"Deep" comparison operator (ie: compare value of each data member)
Definition: InterfaceCAN.h:116
CANMessage(unsigned int _id, CANFormat _format=CANStandard)
Creates CAN remote message.
Definition: InterfaceCAN.h:104
CANMessage()
Creates empty CAN message.
Definition: InterfaceCAN.h:55
CANMessage(unsigned int _id, const unsigned char *_data, unsigned char _len=8, CANType _type=CANData, CANFormat _format=CANStandard)
Creates CAN message with specific content.
Definition: InterfaceCAN.h:72
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