Mistake on this page?
Report an issue in GitHub or email us
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 "platform/PlatformMutex.h"
28 
29 namespace 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  */
39 class CAN
40 #ifdef FEATURE_EXPERIMENTAL_API
41  final : public interface::CAN
42 #else
43  : public interface::can
44 #endif
45 {
46 
47 public:
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  * @param td the transmit pin
104  * @param hz the bus frequency in hertz
105  */
106  CAN(const can_pinmap_t &pinmap);
107  CAN(const can_pinmap_t &&) = delete; // prevent passing of temporary objects
108 
109  /** Initialize CAN interface and set the frequency
110  *
111  * @param pinmap reference to structure which holds static pinmap
112  * @param td the transmit pin
113  * @param hz the bus frequency in hertz
114  */
115  CAN(const can_pinmap_t &pinmap, int hz);
116  CAN(const can_pinmap_t &&, int) = delete; // prevent passing of temporary objects
117 
118  virtual ~CAN();
119 
120  /** Set the frequency of the CAN interface
121  *
122  * @param hz The bus frequency in hertz
123  *
124  * @returns
125  * 1 if successful,
126  * 0 otherwise
127  */
128  int frequency(int hz);
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  */
138  int write(CANMessage msg);
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  /** Reset CAN interface.
152  *
153  * To use after error overflow.
154  */
155  void reset();
156 
157  /** Puts or removes the CAN interface into silent monitoring mode
158  *
159  * @param silent boolean indicating whether to go into silent mode or not
160  */
161  void monitor(bool silent);
162 
163  /** Change CAN operation to the specified mode
164  *
165  * @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest)
166  *
167  * @returns
168  * 0 if mode change failed or unsupported,
169  * 1 if mode change was successful
170  */
171  int mode(Mode mode);
172 
173  /** Filter out incoming messages
174  *
175  * @param id the id to filter on
176  * @param mask the mask applied to the id
177  * @param format format to filter on (Default CANAny)
178  * @param handle message filter handle (Optional)
179  *
180  * @returns
181  * 0 if filter change failed or unsupported,
182  * new filter handle if successful
183  */
184  int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0);
185 
186  /** Detects read errors - Used to detect read overflow errors.
187  *
188  * @returns number of read errors
189  */
190  unsigned char rderror();
191 
192  /** Detects write errors - Used to detect write overflow errors.
193  *
194  * @returns number of write errors
195  */
196  unsigned char tderror();
197 
198  /** Attach a function to call whenever a CAN frame received interrupt is
199  * generated.
200  *
201  * This function locks the deep sleep while a callback is attached
202  *
203  * @param func A pointer to a void function, or 0 to set as none
204  * @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)
205  */
206  void attach(Callback<void()> func, IrqType type = IrqType::RxIrq);
207 
208  static void _irq_handler(uint32_t id, CanIrqType type);
209 
210 #if !defined(DOXYGEN_ONLY)
211 protected:
212  virtual void lock();
213  virtual void unlock();
214 
215  can_t _can;
216  Callback<void()> _irq[IrqType::IrqCnt];
217  PlatformMutex _mutex;
218 #endif
219 };
220 
221 /** @}*/
222 
223 } // namespace mbed
224 
225 #endif
226 
227 #endif // MBED_CAN_H
CANMessage class.
Definition: InterfaceCAN.h:50
int mode(Mode mode)
Change CAN operation to the specified mode.
int filter(unsigned int id, unsigned int mask, CANFormat format=CANAny, int handle=0)
Filter out incoming messages.
unsigned char tderror()
Detects write errors - Used to detect write overflow errors.
The PlatformMutex class is used to synchronize the execution of threads.
Definition: PlatformMutex.h:47
CAN(PinName rd, PinName td)
Creates a CAN interface connected to specific pins.
Receive Data Register Full.
Definition: serial_api.h:76
CANFormat
Values that represent CAN Format.
Definition: can_helper.h:35
unsigned char rderror()
Detects read errors - Used to detect read overflow errors.
int write(CANMessage msg)
Write a CANMessage to the bus.
void monitor(bool silent)
Puts or removes the CAN interface into silent monitoring mode.
void reset()
Reset CAN interface.
int frequency(int hz)
Set the frequency of the CAN interface.
int read(CANMessage &msg, int handle=0)
Read a CANMessage 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.
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
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.