Mistake on this page?
Report an issue in GitHub or email us
CAN.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef MBED_CAN_H
18 #define MBED_CAN_H
19 
20 #include "platform/platform.h"
21 
22 #if DEVICE_CAN || defined(DOXYGEN_ONLY)
23 
24 #include "hal/can_api.h"
25 #include "platform/Callback.h"
26 #include "platform/PlatformMutex.h"
27 #include "platform/NonCopyable.h"
28 
29 namespace mbed {
30 /** \addtogroup drivers */
31 
32 /** CANMessage class
33  *
34  * @note Synchronization level: Thread safe
35  * @ingroup drivers
36  */
37 class CANMessage : public CAN_Message {
38 
39 public:
40  /** Creates empty CAN message.
41  */
43  {
44  len = 8U;
45  type = CANData;
46  format = CANStandard;
47  id = 0U;
48  memset(data, 0, 8);
49  }
50 
51  /** Creates CAN message with specific content.
52  *
53  * @param _id Message ID
54  * @param _data Mesaage Data
55  * @param _len Message Data length
56  * @param _type Type of Data: Use enum CANType for valid parameter values
57  * @param _format Data Format: Use enum CANFormat for valid parameter values
58  */
59  CANMessage(unsigned int _id, const unsigned char *_data, unsigned char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard)
60  {
61  len = _len & 0xF;
62  type = _type;
63  format = _format;
64  id = _id;
65  memcpy(data, _data, _len);
66  }
67 
68 
69  /** Creates CAN message with specific content.
70  *
71  * @param _id Message ID
72  * @param _data Mesaage Data
73  * @param _len Message Data length
74  * @param _type Type of Data: Use enum CANType for valid parameter values
75  * @param _format Data Format: Use enum CANFormat for valid parameter values
76  */
77  CANMessage(unsigned int _id, const char *_data, unsigned char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard)
78  {
79  len = _len & 0xF;
80  type = _type;
81  format = _format;
82  id = _id;
83  memcpy(data, _data, _len);
84  }
85 
86  /** Creates CAN remote message.
87  *
88  * @param _id Message ID
89  * @param _format Data Format: Use enum CANType for valid parameter values
90  */
91  CANMessage(unsigned int _id, CANFormat _format = CANStandard)
92  {
93  len = 0;
94  type = CANRemote;
95  format = _format;
96  id = _id;
97  memset(data, 0, 8);
98  }
99 };
100 
101 /** A can bus client, used for communicating with can devices
102  * @ingroup drivers
103  */
104 class CAN : private NonCopyable<CAN> {
105 
106 public:
107  /** Creates a CAN interface connected to specific pins.
108  *
109  * @param rd read from transmitter
110  * @param td transmit to transmitter
111  *
112  * Example:
113  * @code
114  * #include "mbed.h"
115  *
116  *
117  * Ticker ticker;
118  * DigitalOut led1(LED1);
119  * DigitalOut led2(LED2);
120  * //The constructor takes in RX, and TX pin respectively.
121  * //These pins, for this example, are defined in mbed_app.json
122  * CAN can1(MBED_CONF_APP_CAN1_RD, MBED_CONF_APP_CAN1_TD);
123  * CAN can2(MBED_CONF_APP_CAN2_RD, MBED_CONF_APP_CAN2_TD);
124  *
125  * unsigned char counter = 0;
126  *
127  * void send() {
128  * if(can1.write(CANMessage(1337U, &counter, 1))) {
129  * printf("Message sent: %d\n", counter);
130  * counter++;
131  * }
132  * led1 = !led1;
133  * }
134  *
135  * int main() {
136  * ticker.attach(&send, 1);
137  * CANMessage msg;
138  * while(1) {
139  * if(can2.read(msg)) {
140  * printf("Message received: %d\n\n", msg.data[0]);
141  * led2 = !led2;
142  * }
143  * wait(0.2);
144  * }
145  * }
146  *
147  * @endcode
148  */
149  CAN(PinName rd, PinName td);
150 
151  /** Initialize CAN interface and set the frequency
152  *
153  * @param rd the read pin
154  * @param td the transmit pin
155  * @param hz the bus frequency in hertz
156  */
157  CAN(PinName rd, PinName td, int hz);
158 
159  virtual ~CAN();
160 
161  /** Set the frequency of the CAN interface
162  *
163  * @param hz The bus frequency in hertz
164  *
165  * @returns
166  * 1 if successful,
167  * 0 otherwise
168  */
169  int frequency(int hz);
170 
171  /** Write a CANMessage to the bus.
172  *
173  * @param msg The CANMessage to write.
174  *
175  * @returns
176  * 0 if write failed,
177  * 1 if write was successful
178  */
179  int write(CANMessage msg);
180 
181  /** Read a CANMessage from the bus.
182  *
183  * @param msg A CANMessage to read to.
184  * @param handle message filter handle (0 for any message)
185  *
186  * @returns
187  * 0 if no message arrived,
188  * 1 if message arrived
189  */
190  int read(CANMessage &msg, int handle = 0);
191 
192  /** Reset CAN interface.
193  *
194  * To use after error overflow.
195  */
196  void reset();
197 
198  /** Puts or removes the CAN interface into silent monitoring mode
199  *
200  * @param silent boolean indicating whether to go into silent mode or not
201  */
202  void monitor(bool silent);
203 
204  enum Mode {
205  Reset = 0,
206  Normal,
207  Silent,
208  LocalTest,
209  GlobalTest,
210  SilentTest
211  };
212 
213  /** Change CAN operation to the specified mode
214  *
215  * @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest)
216  *
217  * @returns
218  * 0 if mode change failed or unsupported,
219  * 1 if mode change was successful
220  */
221  int mode(Mode mode);
222 
223  /** Filter out incoming messages
224  *
225  * @param id the id to filter on
226  * @param mask the mask applied to the id
227  * @param format format to filter on (Default CANAny)
228  * @param handle message filter handle (Optional)
229  *
230  * @returns
231  * 0 if filter change failed or unsupported,
232  * new filter handle if successful
233  */
234  int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0);
235 
236  /** Detects read errors - Used to detect read overflow errors.
237  *
238  * @returns number of read errors
239  */
240  unsigned char rderror();
241 
242  /** Detects write errors - Used to detect write overflow errors.
243  *
244  * @returns number of write errors
245  */
246  unsigned char tderror();
247 
248  enum IrqType {
249  RxIrq = 0,
250  TxIrq,
251  EwIrq,
252  DoIrq,
253  WuIrq,
254  EpIrq,
255  AlIrq,
256  BeIrq,
257  IdIrq,
258 
259  IrqCnt
260  };
261 
262  /** Attach a function to call whenever a CAN frame received interrupt is
263  * generated.
264  *
265  * This function locks the deep sleep while a callback is attached
266  *
267  * @param func A pointer to a void function, or 0 to set as none
268  * @param type Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error)
269  */
270  void attach(Callback<void()> func, IrqType type = RxIrq);
271 
272  /** Attach a member function to call whenever a CAN frame received interrupt
273  * is generated.
274  *
275  * @param obj pointer to the object to call the member function on
276  * @param method pointer to the member function to be called
277  * @param type Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error)
278  * @deprecated
279  * The attach function does not support cv-qualifiers. Replaced by
280  * attach(callback(obj, method), type).
281  */
282  template<typename T>
283  MBED_DEPRECATED_SINCE("mbed-os-5.1",
284  "The attach function does not support cv-qualifiers. Replaced by "
285  "attach(callback(obj, method), type).")
286  void attach(T *obj, void (T::*method)(), IrqType type = RxIrq)
287  {
288  // Underlying call thread safe
289  attach(callback(obj, method), type);
290  }
291 
292  /** Attach a member function to call whenever a CAN frame received interrupt
293  * is generated.
294  *
295  * @param obj pointer to the object to call the member function on
296  * @param method pointer to the member function to be called
297  * @param type Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error)
298  * @deprecated
299  * The attach function does not support cv-qualifiers. Replaced by
300  * attach(callback(obj, method), type).
301  */
302  template<typename T>
303  MBED_DEPRECATED_SINCE("mbed-os-5.1",
304  "The attach function does not support cv-qualifiers. Replaced by "
305  "attach(callback(obj, method), type).")
306  void attach(T *obj, void (*method)(T *), IrqType type = RxIrq)
307  {
308  // Underlying call thread safe
309  attach(callback(obj, method), type);
310  }
311 
312  static void _irq_handler(uint32_t id, CanIrqType type);
313 
314 #if !defined(DOXYGEN_ONLY)
315 protected:
316  virtual void lock();
317  virtual void unlock();
318  can_t _can;
319  Callback<void()> _irq[IrqCnt];
320  PlatformMutex _mutex;
321 #endif
322 };
323 
324 } // namespace mbed
325 
326 #endif
327 
328 #endif // MBED_CAN_H
329 
CANType
Values that represent CAN Type.
Definition: can_helper.h:48
CANMessage class.
Definition: CAN.h:37
CANMessage()
Creates empty CAN message.
Definition: CAN.h:42
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:168
Callback< R()> callback(R(*func)()=0)
Create a callback class with type inferred from the arguments.
Definition: Callback.h:3848
The PlatformMutex class is used to synchronize the execution of threads.
Definition: PlatformMutex.h:47
CANFormat
Values that represent CAN Format.
Definition: can_helper.h:35
CANMessage(unsigned int _id, CANFormat _format=CANStandard)
Creates CAN remote message.
Definition: CAN.h:91
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: CAN.h:59
CANMessage(unsigned int _id, const char *_data, unsigned char _len=8, CANType _type=CANData, CANFormat _format=CANStandard)
Creates CAN message with specific content.
Definition: CAN.h:77
Callback class based on template specialization.
Definition: Callback.h:39
A can bus client, used for communicating with can devices.
Definition: CAN.h:104
Holder for single CAN message.
Definition: can_helper.h:61
#define MBED_DEPRECATED_SINCE(D, M)
MBED_DEPRECATED("message string") Mark a function declaration as deprecated, if it used then a warnin...
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.