Mbed OS Reference
Loading...
Searching...
No Matches
can_api.h
1
2/** \addtogroup hal */
3/** @{*/
4/* mbed Microcontroller Library
5 * Copyright (c) 2006-2016 ARM Limited
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20#ifndef MBED_CAN_API_H
21#define MBED_CAN_API_H
22
23#include "device.h"
24#include "pinmap.h"
25
26#if DEVICE_CAN
27
28#include "PinNames.h"
29#include "PeripheralNames.h"
30#include "hal/can_helper.h"
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36typedef enum {
37 IRQ_RX,
38 IRQ_TX,
39 IRQ_ERROR,
40 IRQ_OVERRUN,
41 IRQ_WAKEUP,
42 IRQ_PASSIVE,
43 IRQ_ARB,
44 IRQ_BUS,
45 IRQ_READY
46} CanIrqType;
47
48
49typedef enum {
50 MODE_RESET,
51 MODE_NORMAL,
52 MODE_SILENT,
53 MODE_TEST_LOCAL,
54 MODE_TEST_GLOBAL,
55 MODE_TEST_SILENT
56} CanMode;
57
58typedef struct {
59 int peripheral;
60 PinName rd_pin;
61 int rd_function;
62 PinName td_pin;
63 int td_function;
65
66typedef void (*can_irq_handler)(uintptr_t context, CanIrqType type);
67
68typedef struct can_s can_t;
69
70/** Initialize the CAN peripheral. It sets the default parameters for CAN
71 * peripheral, and configures its specifieds pins.
72 *
73 * @param obj CAN object
74 * @param rd The CAN RD pin name
75 * @param td The CAN TD pin name
76 */
77void can_init(can_t *obj, PinName rd, PinName td);
78
79/** Initialize the CAN peripheral. It sets the default parameters for CAN
80 * peripheral, and configures its specifieds pins.
81 *
82 * @param obj The CAN object
83 * @param pinmap pointer to structure which holds static pinmap
84 */
85void can_init_direct(can_t *obj, const can_pinmap_t *pinmap);
86
87/** Initialize the CAN peripheral. It sets the default parameters for CAN
88 * peripheral, and configures its specifieds pins.
89 *
90 * @param obj CAN object
91 * @param rd The CAN RD pin name
92 * @param td The CAN TD pin name
93 * @param hz The bus frequency in classical CAN mode, or nominal phase frequency in CAN FD mode
94 * @param data_hz The data phase frequency in CAN FD mode, the CAN object is put into Classical CAN mode if this parameter is zero
95 */
96void can_init_freq(can_t *obj, PinName rd, PinName td, int hz
97#ifdef DEVICE_CAN_FD
98 , int data_hz
99#endif
100 );
101
102/** Initialize the CAN peripheral. It sets the default parameters for CAN
103 * peripheral, and configures its specifieds pins.
104 *
105 * @param obj CAN object
106 * @param pinmap pointer to structure which holds static pinmap
107 * @param hz The bus frequency in classical CAN mode, or nominal phase frequency in CAN FD mode
108 * @param data_hz The data phase frequency in CAN FD mode, the CAN object is put into Classical CAN mode if this parameter is zero
109 */
110void can_init_freq_direct(can_t *obj, const can_pinmap_t *pinmap, int hz
111#ifdef DEVICE_CAN_FD
112 , int data_hz
113#endif
114 );
115
116/** Release the CAN peripheral, not currently invoked. It requires further
117 * resource management.
118 *
119 * @param obj The CAN object
120 */
121void can_free(can_t *obj);
122
123/** Configure the CAN bus frequency
124 *
125 * @param obj The CAN object
126 * @param hz The bus frequency in classical CAN mode, or nominal phase frequency in CAN FD mode
127 * @param data_hz The data phase frequency in CAN FD mode, the CAN object is put into Classical CAN mode if this parameter is zero
128 */
129int can_frequency(can_t *obj, int hz
130#ifdef DEVICE_CAN_FD
131 , int data_hz
132#endif
133 );
134
135/** Initialize the CAN IRQ handler
136 *
137 * @param obj The CAN object
138 * @param handler The handler to be attached to CAN IRQ
139 * @param context The context to be passed back to the handler (context != 0, 0 is reserved)
140 */
141void can_irq_init(can_t *obj, can_irq_handler handler, uintptr_t context);
142
143/** Remove the CAN IRQ handler
144 *
145 * @param obj The CAN object
146 */
147void can_irq_free(can_t *obj);
148
149/** Enable/disable the CAN IRQ event
150 *
151 * @param obj The CAN object
152 * @param irq The CAN IRQ event
153 * @param enable The enable flag
154 */
155void can_irq_set(can_t *obj, CanIrqType irq, uint32_t enable);
156
157/** Write a CAN message to the bus.
158 *
159 * @param obj The CAN object
160 * @param msg The CAN message to write.
161 *
162 * @return 0 if write failed,
163 * 1 if write was successful
164 */
165int can_write(can_t *obj, CAN_Message msg);
166
167/** Read a CAN message from the bus.
168 *
169 * @param obj CAN object
170 * @param msg A CAN message to read to.
171 * @param handle message filter handle (0 for any message).
172 *
173 * @return 0 if no message arrived,
174 * 1 if message arrived
175 */
176int can_read(can_t *obj, CAN_Message *msg, int handle);
177
178/** Change CAN operation to the specified mode.
179 *
180 * @param obj The CAN object
181 * @param mode The new operation mode (MODE_NORMAL, MODE_SILENT, MODE_TEST_LOCAL, MODE_TEST_GLOBAL, MODE_TEST_SILENT).
182 *
183 * @return 0 if mode change failed or unsupported,
184 * 1 if mode change was successful
185 */
186int can_mode(can_t *obj, CanMode mode);
187
188/** Filter out incomming messages.
189 *
190 * @param obj The CAN object
191 * @param id the id to filter on.
192 * @param mask the mask applied to the id.
193 * @param format format to filter on (Default CANAny).
194 * @param handle message filter handle (Optional).
195 *
196 * @return 0 if filter change failed or unsupported,
197 * new filter handle if successful
198 */
199int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle);
200
201/** Reset CAN interface.
202 *
203 * @param obj CAN object
204 *
205 * To use after error overflow.
206 */
207void can_reset(can_t *obj);
208
209/** Detects read errors - Used to detect read overflow errors.
210 *
211 * @param obj CAN object
212 * @return number of read errors
213 */
214unsigned char can_rderror(can_t *obj);
215
216/** Detects write errors - Used to detect write overflow errors.
217 *
218 * @param obj CAN object
219 * @return number of write errors
220 */
221unsigned char can_tderror(can_t *obj);
222
223/** Puts or removes the CAN interface into silent monitoring mode.
224 *
225 * @param obj CAN object
226 * @param silent boolean indicating whether to go into silent mode or not.
227 */
228void can_monitor(can_t *obj, int silent);
229
230#if DEVICE_CAN_FD
231/** Write a CAN FD Message to the bus.
232 *
233 * @param obj The CAN object
234 * @param msg The CAN FD Message to write.
235 *
236 * @return 0 if write failed,
237 * 1 if write was successful
238 */
239int canfd_write(can_t *obj, CANFD_Message msg);
240
241/** Read a Classical CAN or CAN FD Message from the bus.
242 *
243 * @param obj CAN object
244 * @param msg A Classical CAN or CAN FD Message to read to.
245 * @param handle message filter handle (0 for any message).
246 *
247 * @return 0 if no message arrived,
248 * 1 if message arrived
249 */
250int canfd_read(can_t *obj, CANFD_Message *msg, int handle);
251#endif
252
253/** Get the pins that support CAN RD
254 *
255 * Return a PinMap array of pins that support CAN RD. The
256 * array is terminated with {NC, NC, 0}.
257 *
258 * @return PinMap array
259 */
261
262/** Get the pins that support CAN TD
263 *
264 * Return a PinMap array of pins that support CAN TD. The
265 * array is terminated with {NC, NC, 0}.
266 *
267 * @return PinMap array
268 */
270
271#ifdef __cplusplus
272}
273#endif
274
275#endif // MBED_CAN_API_H
276
277#endif
278
279/** @}*/
void can_init_freq_direct(can_t *obj, const can_pinmap_t *pinmap, int hz, int data_hz)
Initialize the CAN peripheral.
unsigned char can_rderror(can_t *obj)
Detects read errors - Used to detect read overflow errors.
int can_read(can_t *obj, CAN_Message *msg, int handle)
Read a CAN message from the bus.
int can_frequency(can_t *obj, int hz, int data_hz)
Configure the CAN bus frequency.
CANFormat
Values that represent CAN Format.
Definition can_helper.h:35
void can_reset(can_t *obj)
Reset CAN interface.
void can_irq_init(can_t *obj, can_irq_handler handler, uintptr_t context)
Initialize the CAN IRQ handler.
unsigned char can_tderror(can_t *obj)
Detects write errors - Used to detect write overflow errors.
void can_irq_set(can_t *obj, CanIrqType irq, uint32_t enable)
Enable/disable the CAN IRQ event.
void can_irq_free(can_t *obj)
Remove the CAN IRQ handler.
const PinMap * can_rd_pinmap(void)
Get the pins that support CAN RD.
const PinMap * can_td_pinmap(void)
Get the pins that support CAN TD.
int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle)
Filter out incomming messages.
void can_free(can_t *obj)
Release the CAN peripheral, not currently invoked.
int can_mode(can_t *obj, CanMode mode)
Change CAN operation to the specified mode.
int canfd_write(can_t *obj, CANFD_Message msg)
Write a CAN FD Message to the bus.
int canfd_read(can_t *obj, CANFD_Message *msg, int handle)
Read a Classical CAN or CAN FD Message from the bus.
void can_monitor(can_t *obj, int silent)
Puts or removes the CAN interface into silent monitoring mode.
void can_init_direct(can_t *obj, const can_pinmap_t *pinmap)
Initialize the CAN peripheral.
void can_init_freq(can_t *obj, PinName rd, PinName td, int hz, int data_hz)
Initialize the CAN peripheral.
int can_write(can_t *obj, CAN_Message msg)
Write a CAN message to the bus.
void can_init(can_t *obj, PinName rd, PinName td)
Initialize the CAN peripheral.
Holder for single CAN message.
Definition can_helper.h:61