Mistake on this page?
Report an issue in GitHub or email us
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 
29 namespace mbed {
30 
31 #ifndef FEATURE_EXPERIMENTAL_API
32 // Forward declare CAN
33 class 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  */
50 class CANMessage : public CAN_Message {
51 
52 public:
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 /** @}*/
115 
116 namespace interface {
117 
118 /* Having this as a struct allows interface::CAN and/or mbed::CAN to inherit the enums */
119 struct can {
120 
121  enum Mode {
122  Reset = 0,
123  Normal,
124  Silent,
125  LocalTest,
126  GlobalTest,
127  SilentTest
128  };
129 
130  enum IrqType {
131  RxIrq = 0,
132  TxIrq,
133  EwIrq,
134  DoIrq,
135  WuIrq,
136  EpIrq,
137  AlIrq,
138  BeIrq,
139  IdIrq,
140 
141  IrqCnt
142  };
143 
144 // Prevent slicing and user creation of base class.
145 protected:
146  can() = default;
147  ~can() = default;
148 
149 public:
150 
151  /* Copy constructor and copy assignment operators will be deleted in subclasses as well */
152  can(const can &) = delete;
153  can &operator=(const can &) = delete;
154 
155 };
156 
157 #ifdef FEATURE_EXPERIMENTAL_API
158 
159 // Pure virtual interface for CAN
160 struct CAN : public interface::can {
161 
162  virtual ~CAN() = default;
163  virtual int frequency(int hz) = 0;
164  virtual int write(CANMessage msg) = 0;
165  virtual int read(CANMessage &msg, int handle = 0) = 0;
166  virtual void reset() = 0;
167  virtual void monitor(bool silent) = 0;
168  virtual int mode(Mode mode) = 0;
169  virtual int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0) = 0;
170  virtual unsigned char rderror() = 0;
171  virtual unsigned char tderror() = 0;
172  virtual void attach(Callback<void()> func, IrqType type = IrqType::RxIrq) = 0;
173 };
174 
175 #else
176 using CAN = ::mbed::CAN;
177 #endif
178 
179 } // namespace interface
180 
181 #if defined(FEATURE_EXPERIMENTAL_API) && !DEVICE_CAN
182 using CAN = interface::CAN;
183 #endif
184 
185 } // namespace mbed
186 
187 #endif /* MBED_INTERFACE_CAN_H_ */
CANType
Values that represent CAN Type.
Definition: can_helper.h:48
CANMessage class.
Definition: InterfaceCAN.h:50
CANMessage()
Creates empty CAN message.
Definition: InterfaceCAN.h:55
Transmit Data Register Empty.
Definition: serial_api.h:77
Receive Data Register Full.
Definition: serial_api.h:76
CANFormat
Values that represent CAN Format.
Definition: can_helper.h:35
CANMessage(unsigned int _id, CANFormat _format=CANStandard)
Creates CAN remote message.
Definition: InterfaceCAN.h:104
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
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
Callback class based on template specialization.
Definition: Callback.h:53
Definition: ATHandler.h:46
A can bus client, used for communicating with can devices.
Definition: CAN.h:39
Holder for single CAN message.
Definition: can_helper.h:61
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.