Mbed OS Reference
Loading...
Searching...
No Matches
CAN.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2006-2019 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 "interfaces/InterfaceCAN.h"
25#include "hal/can_api.h"
26#include "platform/Callback.h"
27#include "rtos/Mutex.h"
28
29namespace mbed {
30
31/**
32 * \defgroup drivers_CAN CAN class
33 * \ingroup drivers-public-api-can
34 * @{
35 */
36
37/** A can bus client, used for communicating with can devices
38 */
39class CAN
40#ifdef FEATURE_EXPERIMENTAL_API
41 final : public interface::CAN
42#else
43 : public interface::can
44#endif
45{
46
47public:
48 /** Creates a CAN interface connected to specific pins.
49 *
50 * @param rd read from transmitter
51 * @param td transmit to transmitter
52 *
53 * Example:
54 * @code
55 * #include "mbed.h"
56 *
57 *
58 * Ticker ticker;
59 * DigitalOut led1(LED1);
60 * DigitalOut led2(LED2);
61 * //The constructor takes in RX, and TX pin respectively.
62 * //These pins, for this example, are defined in mbed_app.json
63 * CAN can1(MBED_CONF_APP_CAN1_RD, MBED_CONF_APP_CAN1_TD);
64 * CAN can2(MBED_CONF_APP_CAN2_RD, MBED_CONF_APP_CAN2_TD);
65 *
66 * unsigned char counter = 0;
67 *
68 * void send() {
69 * if(can1.write(CANMessage(1337U, &counter, 1))) {
70 * printf("Message sent: %d\n", counter);
71 * counter++;
72 * }
73 * led1 = !led1;
74 * }
75 *
76 * int main() {
77 * ticker.attach(&send, 1);
78 * CANMessage msg;
79 * while(1) {
80 * if(can2.read(msg)) {
81 * printf("Message received: %d\n\n", msg.data[0]);
82 * led2 = !led2;
83 * }
84 * ThisThread::sleep_for(200);
85 * }
86 * }
87 *
88 * @endcode
89 */
90 CAN(PinName rd, PinName td);
91
92 /** Initialize CAN interface and set the frequency
93 *
94 * @param rd the read pin
95 * @param td the transmit pin
96 * @param hz the bus frequency in hertz
97 * @param data_hz the data frequency in hertz(CAN FD only)
98 */
99 CAN(PinName rd, PinName td, int hz, int data_hz = 0);
100
101 /** Initialize CAN interface
102 *
103 * @param pinmap reference to structure which holds static pinmap
104 */
105 CAN(const can_pinmap_t &pinmap);
106 CAN(const can_pinmap_t &&) = delete; // prevent passing of temporary objects
107
108 /** Initialize CAN interface and set the frequency
109 *
110 * @param pinmap reference to structure which holds static pinmap
111 * @param hz the bus frequency in hertz
112 * @param data_hz the data frequency in hertz(CAN FD only)
113 */
114 CAN(const can_pinmap_t &pinmap, int hz, int data_hz = 0);
115 CAN(const can_pinmap_t &&, int) = delete; // prevent passing of temporary objects
116
117 virtual ~CAN();
118
119 /** Set the frequency of the CAN interface
120 *
121 * @param hz The bus frequency in hertz
122 * @param data_hz the data frequency in hertz(CAN FD only)
123 *
124 * @returns
125 * 1 if successful,
126 * 0 otherwise
127 */
128 int frequency(int hz, int data_hz = 0);
129
130 /** Write a CANMessage to the bus.
131 *
132 * @param msg The CANMessage to write.
133 *
134 * @returns
135 * 0 if write failed,
136 * 1 if write was successful
137 */
139
140 /** Read a CANMessage from the bus.
141 *
142 * @param msg A CANMessage to read to.
143 * @param handle message filter handle (0 for any message)
144 *
145 * @returns
146 * 0 if no message arrived,
147 * 1 if message arrived
148 */
149 int read(CANMessage &msg, int handle = 0);
150
151#if DEVICE_CAN_FD
152
153 /** Write a CANFDMessage to the bus.
154 *
155 * @param msg The CANFDMessage to write.
156 *
157 * @returns
158 * 0 if write failed,
159 * 1 if write was successful
160 */
162
163 /** Read a CANFDMessage from the bus.
164 *
165 * @param msg A CANFDMessage to read to.
166 * @param handle message filter handle (0 for any message)
167 *
168 * @returns
169 * 0 if no message arrived,
170 * 1 if message arrived
171 */
172 int read(CANFDMessage &msg, int handle = 0);
173
174#endif
175
176 /** Reset CAN interface.
177 *
178 * To use after error overflow.
179 */
180 void reset();
181
182 /** Puts or removes the CAN interface into silent monitoring mode
183 *
184 * @param silent boolean indicating whether to go into silent mode or not
185 */
186 void monitor(bool silent);
187
188 /** Change CAN operation to the specified mode
189 *
190 * @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest)
191 *
192 * @returns
193 * 0 if mode change failed or unsupported,
194 * 1 if mode change was successful
195 */
196 int mode(Mode mode);
197
198 /** Filter out incoming messages
199 *
200 * @param id the id to filter on
201 * @param mask the mask applied to the id
202 * @param format format to filter on (Default CANAny)
203 * @param handle message filter handle (Optional)
204 *
205 * @returns
206 * 0 if filter change failed or unsupported,
207 * new filter handle if successful
208 */
209 int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0);
210
211 /** Detects read errors - Used to detect read overflow errors.
212 *
213 * @returns number of read errors
214 */
215 unsigned char rderror();
216
217 /** Detects write errors - Used to detect write overflow errors.
218 *
219 * @returns number of write errors
220 */
221 unsigned char tderror();
222
223 /** Attach a function to call whenever a CAN frame received interrupt is
224 * generated.
225 *
226 * This function locks the deep sleep while a callback is attached
227 *
228 * @param func A pointer to a void function, or 0 to set as none
229 * @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)
230 */
231 void attach(Callback<void()> func, IrqType type = IrqType::RxIrq);
232
233 static void _irq_handler(uintptr_t context, CanIrqType type);
234
235#if !defined(DOXYGEN_ONLY)
236protected:
237 virtual void lock();
238 virtual void unlock();
239
240 can_t _can;
241 Callback<void()> _irq[IrqType::IrqCnt];
242 rtos::Mutex _mutex;
243#endif
244};
245
246/** @}*/
247
248} // namespace mbed
249
250#endif
251
252#endif // MBED_CAN_H
CANFDMessage class.
A can bus client, used for communicating with can devices.
Definition CAN.h:45
CAN(PinName rd, PinName td, int hz, int data_hz=0)
Initialize CAN interface and set the frequency.
unsigned char rderror()
Detects read errors - Used to detect read overflow errors.
CAN(PinName rd, PinName td)
Creates a CAN interface connected to specific pins.
CAN(const can_pinmap_t &pinmap, int hz, int data_hz=0)
Initialize CAN interface and set the frequency.
int write(CANFDMessage msg)
Write a CANFDMessage to the bus.
int write(CANMessage msg)
Write a CANMessage to the bus.
int mode(Mode mode)
Change CAN operation to the specified mode.
void monitor(bool silent)
Puts or removes the CAN interface into silent monitoring mode.
unsigned char tderror()
Detects write errors - Used to detect write overflow errors.
int frequency(int hz, int data_hz=0)
Set the frequency of the CAN interface.
int filter(unsigned int id, unsigned int mask, CANFormat format=CANAny, int handle=0)
Filter out incoming messages.
void reset()
Reset CAN interface.
int read(CANFDMessage &msg, int handle=0)
Read a CANFDMessage from the bus.
void attach(Callback< void()> func, IrqType type=IrqType::RxIrq)
Attach a function to call whenever a CAN frame received interrupt is generated.
int read(CANMessage &msg, int handle=0)
Read a CANMessage from the bus.
CAN(const can_pinmap_t &pinmap)
Initialize CAN interface.
CANMessage class.
Callback class based on template specialization.
Definition Callback.h:53
The Mutex class is used to synchronize the execution of threads.
Definition Mutex.h:79
CANFormat
Values that represent CAN Format.
Definition can_helper.h:35