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 */
98 CAN(PinName rd, PinName td, int hz);
99
100 /** Initialize CAN interface
101 *
102 * @param pinmap reference to structure which holds static pinmap
103 */
104 CAN(const can_pinmap_t &pinmap);
105 CAN(const can_pinmap_t &&) = delete; // prevent passing of temporary objects
106
107 /** Initialize CAN interface and set the frequency
108 *
109 * @param pinmap reference to structure which holds static pinmap
110 * @param hz the bus frequency in hertz
111 */
112 CAN(const can_pinmap_t &pinmap, int hz);
113 CAN(const can_pinmap_t &&, int) = delete; // prevent passing of temporary objects
114
115 virtual ~CAN();
116
117 /** Set the frequency of the CAN interface
118 *
119 * @param hz The bus frequency in hertz
120 *
121 * @returns
122 * 1 if successful,
123 * 0 otherwise
124 */
125 int frequency(int hz);
126
127 /** Write a CANMessage to the bus.
128 *
129 * @param msg The CANMessage to write.
130 *
131 * @returns
132 * 0 if write failed,
133 * 1 if write was successful
134 */
136
137 /** Read a CANMessage from the bus.
138 *
139 * @param msg A CANMessage to read to.
140 * @param handle message filter handle (0 for any message)
141 *
142 * @returns
143 * 0 if no message arrived,
144 * 1 if message arrived
145 */
146 int read(CANMessage &msg, int handle = 0);
147
148 /** Reset CAN interface.
149 *
150 * To use after error overflow.
151 */
152 void reset();
153
154 /** Puts or removes the CAN interface into silent monitoring mode
155 *
156 * @param silent boolean indicating whether to go into silent mode or not
157 */
158 void monitor(bool silent);
159
160 /** Change CAN operation to the specified mode
161 *
162 * @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest)
163 *
164 * @returns
165 * 0 if mode change failed or unsupported,
166 * 1 if mode change was successful
167 */
168 int mode(Mode mode);
169
170 /** Filter out incoming messages
171 *
172 * @param id the id to filter on
173 * @param mask the mask applied to the id
174 * @param format format to filter on (Default CANAny)
175 * @param handle message filter handle (Optional)
176 *
177 * @returns
178 * 0 if filter change failed or unsupported,
179 * new filter handle if successful
180 */
181 int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0);
182
183 /** Detects read errors - Used to detect read overflow errors.
184 *
185 * @returns number of read errors
186 */
187 unsigned char rderror();
188
189 /** Detects write errors - Used to detect write overflow errors.
190 *
191 * @returns number of write errors
192 */
193 unsigned char tderror();
194
195 /** Attach a function to call whenever a CAN frame received interrupt is
196 * generated.
197 *
198 * This function locks the deep sleep while a callback is attached
199 *
200 * @param func A pointer to a void function, or 0 to set as none
201 * @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)
202 */
203 void attach(Callback<void()> func, IrqType type = IrqType::RxIrq);
204
205 static void _irq_handler(uintptr_t context, CanIrqType type);
206
207#if !defined(DOXYGEN_ONLY)
208protected:
209 virtual void lock();
210 virtual void unlock();
211
212 can_t _can;
213 Callback<void()> _irq[IrqType::IrqCnt];
214 rtos::Mutex _mutex;
215#endif
216};
217
218/** @}*/
219
220} // namespace mbed
221
222#endif
223
224#endif // MBED_CAN_H
A can bus client, used for communicating with can devices.
Definition: CAN.h:45
unsigned char rderror()
Detects read errors - Used to detect read overflow errors.
CAN(PinName rd, PinName td, int hz)
Initialize CAN interface and set the frequency.
CAN(PinName rd, PinName td)
Creates a CAN interface connected to specific pins.
CAN(const can_pinmap_t &pinmap, int hz)
Initialize CAN interface and set the frequency.
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 filter(unsigned int id, unsigned int mask, CANFormat format=CANAny, int handle=0)
Filter out incoming messages.
int frequency(int hz)
Set the frequency of the CAN interface.
void reset()
Reset CAN interface.
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.
Definition: InterfaceCAN.h:50
Callback class based on template specialization.
Definition: Callback.h:53
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:70
CANFormat
Values that represent CAN Format.
Definition: can_helper.h:35