Skip to content

Commit 9e12807

Browse files
Alexander Wachtercarlescufi
authored andcommitted
API: can: Add API for Controller Area Network driver
This API defines following calls - can_configure - can_send - can_attach_isr - can_attach_msgq - can_detach Signed-off-by: Alexander Wachter <[email protected]>
1 parent 5eb8829 commit 9e12807

File tree

1 file changed

+381
-0
lines changed

1 file changed

+381
-0
lines changed

include/can.h

Lines changed: 381 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,381 @@
1+
/**
2+
* @file
3+
*
4+
* @brief Public APIs for the CAN drivers.
5+
*/
6+
7+
/*
8+
* Copyright (c) 2018 Alexander Wachter
9+
*
10+
* SPDX-License-Identifier: Apache-2.0
11+
*/
12+
13+
#ifndef __DRIVERS_CAN_H
14+
#define __DRIVERS_CAN_H
15+
16+
/**
17+
* @brief CAN Interface
18+
* @defgroup can_interface CAN Interface
19+
* @ingroup io_interfaces
20+
* @{
21+
*/
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
#include <zephyr/types.h>
28+
#include <device.h>
29+
#include <string.h>
30+
31+
#define CAN_EX_ID (1 << 31)
32+
#define CAN_MAX_STD_ID (0x7FF)
33+
#define CAN_STD_ID_MASK CAN_MAX_STD_ID
34+
#define CAN_EXT_ID_MASK (0x1FFFFFFF)
35+
#define CAN_MAX_DLC (8)
36+
37+
/* CAN_TX_* are the error flags from tx_callback and send.*/
38+
/** send successfully */
39+
#define CAN_TX_OK (0)
40+
/** general send error */
41+
#define CAN_TX_ERR (1)
42+
/** bus arbitration lost during sending */
43+
#define CAN_TX_ARB_LOST (2)
44+
/** controller is in bus off state */
45+
#define CAN_TX_BUS_OFF (3)
46+
/** unexpected error */
47+
#define CAN_TX_UNKNOWN (4)
48+
49+
/** attach_* failed because there is no unused filter left*/
50+
#define CAN_NO_FREE_FILTER (-1)
51+
52+
/** operation timed out*/
53+
#define CAN_TIMEOUT (1)
54+
55+
/**
56+
* @brief Statically define and initialize a can message queue.
57+
*
58+
* The message queue's ring buffer contains space for @a size messages.
59+
*
60+
* @param name Name of the message queue.
61+
* @param size Number of can messages.
62+
*/
63+
#define CAN_DEFINE_MSGQ(name, size) K_MSGQ_DEFINE(name, sizeof(struct can_msg), size, 4)
64+
65+
/**
66+
* @brief can_ide enum
67+
* Define if the message has a standard (11bit) or extended (29bit)
68+
* identifier
69+
*/
70+
enum can_ide {
71+
CAN_STANDARD_IDENTIFIER,
72+
CAN_EXTENDED_IDENTIFIER
73+
};
74+
75+
/**
76+
* @brief can_rtr enum
77+
* Define if the message is a data or remote frame
78+
*/
79+
enum can_rtr {
80+
CAN_DATAFRAME,
81+
CAN_REMOTEREQUEST
82+
};
83+
84+
/**
85+
* @brief can_mode enum
86+
* Defines the mode of the can controller
87+
*/
88+
enum can_mode {
89+
/*Normal mode*/
90+
CAN_NORMAL_MODE,
91+
/*Controller is not allowed to send dominant bits*/
92+
CAN_SILENT_MODE,
93+
/*Controller is in loopback mode (receive own messages)*/
94+
CAN_LOOPBACK_MODE,
95+
/*Combination of loopback and silent*/
96+
CAN_SILENT_LOOPBACK_MODE
97+
};
98+
99+
/**
100+
* @brief can message structure
101+
*
102+
* Used to pass can messages from userspace to the driver and
103+
* from driver to userspace
104+
*
105+
*/
106+
struct can_msg {
107+
/** Indicates the identifier type (standard or extended)
108+
* use can_ide enum for assignment
109+
*/
110+
u32_t id_type : 1;
111+
/** Set the message to a transmission request instead of data frame
112+
* use can_rtr enum for assignment
113+
*/
114+
u32_t rtr : 1;
115+
/** Message identifier*/
116+
union {
117+
u32_t std_id : 11;
118+
u32_t ext_id : 29;
119+
};
120+
/** The length of the message (max. 8) in byte */
121+
u8_t dlc;
122+
/** The message data*/
123+
union {
124+
u8_t data[8];
125+
u32_t data_32[2];
126+
};
127+
} __packed;
128+
129+
/**
130+
* @brief can filter structure
131+
*
132+
* Used to pass can identifier filter information to the driver.
133+
* rtr_mask and *_id_mask are used to mask bits of the rtr and id fields.
134+
* If the mask bit is 0, the value of the corresponding bit in the id or rtr
135+
* field don't care for the filter matching.
136+
*
137+
*/
138+
struct can_filter {
139+
/** Indicates the identifier type (standard or extended)
140+
* use can_ide enum for assignment
141+
*/
142+
u32_t id_type : 1;
143+
/** target state of the rtr bit */
144+
u32_t rtr : 1;
145+
/** target state of the identifier */
146+
union {
147+
u32_t std_id : 11;
148+
u32_t ext_id : 29;
149+
};
150+
/** rtr bit mask */
151+
u32_t rtr_mask : 1;
152+
/** identifier mask*/
153+
union {
154+
u32_t std_id_mask : 11;
155+
u32_t ext_id_mask : 29;
156+
};
157+
} __packed;
158+
159+
/**
160+
* @typedef can_tx_callback_t
161+
* @brief Define the application callback handler function signature
162+
*
163+
* @param error_flags status of the preformed send operation
164+
*/
165+
typedef void (*can_tx_callback_t)(u32_t error_flags);
166+
167+
/**
168+
* @typedef can_rx_callback_t
169+
* @brief Define the application callback handler function signature
170+
* for receiving.
171+
*
172+
* @param received message
173+
*/
174+
typedef void (*can_rx_callback_t)(struct can_msg *msg);
175+
176+
/**
177+
* @brief Configure operation of a host controller.
178+
*
179+
* @param dev Pointer to the device structure for the driver instance.
180+
* @param mode Operation mode
181+
* @param bitrate bus-speed in Baud/s
182+
*
183+
* @retval 0 If successful.
184+
* @retval -EIO General input / output error, failed to configure device.
185+
*/
186+
typedef int (*can_configure_t)(struct device *dev, enum can_mode mode,
187+
u32_t bitrate);
188+
189+
/**
190+
* @brief Perform data transfer to CAN bus.
191+
*
192+
* This routine provides a generic interface to perform data transfer
193+
* to the can bus. Use can_write() for simple write.
194+
* *
195+
* @param dev Pointer to the device structure for the driver instance.
196+
* @param msg Message to transfer.
197+
* @param timeout Waiting for empty tx mailbox timeout in ms or K_FOREVER.
198+
* @param callback_isr Is called when message was sent or a transmission error
199+
* occurred. If null, this function is blocking until
200+
* message is sent.
201+
*
202+
* @retval 0 If successful.
203+
* @retval CAN_TX_* on failure.
204+
*/
205+
typedef int (*can_send_t)(struct device *dev, struct can_msg *msg,
206+
s32_t timeout, can_tx_callback_t callback_isr);
207+
208+
209+
/**
210+
* @brief Attach a message queue to a single or group of identifiers.
211+
*
212+
* This routine attaches a message queue to identifiers specified by
213+
* a filter. Whenever the filter matches, the message is pushed to the queue
214+
* If a message passes more than one filter the priotity of the match
215+
* is hardware dependent.
216+
* A message queue can be attached to more than one filter.
217+
* The message queue must me initialized before.
218+
* *
219+
* @param dev Pointer to the device structure for the driver instance.
220+
* @param msgq Pointer to the already initialized message queue.
221+
* @param filter Pointer to a can_filter structure defining the id filtering.
222+
*
223+
* @retval filter id on success.
224+
* @retval CAN_NO_FREE_FILTER if there is no filter left.
225+
*/
226+
typedef int (*can_attach_msgq_t)(struct device *dev, struct k_msgq *msg_q,
227+
const struct can_filter *filter);
228+
229+
/**
230+
* @brief Attach an isr callback function to a single or group of identifiers.
231+
*
232+
* This routine attaches an isr callback to identifiers specified by
233+
* a filter. Whenever the filter matches, the callback function is called
234+
* with isr context.
235+
* If a message passes more than one filter the priotity of the match
236+
* is hardware dependent.
237+
* A callback function can be attached to more than one filter.
238+
* *
239+
* @param dev Pointer to the device structure for the driver instance.
240+
* @param isr Callback functionpointer.
241+
* @param filter Pointer to a can_filter structure defining the id filtering.
242+
*
243+
* @retval filter id on success.
244+
* @retval CAN_NO_FREE_FILTER if there is no filter left.
245+
*/
246+
typedef int (*can_attach_isr_t)(struct device *dev, can_rx_callback_t isr,
247+
const struct can_filter *filter);
248+
249+
/**
250+
* @brief Detach an isr or message queue from the identifier filtering.
251+
*
252+
* This routine detaches an isr callback or message queue from the identifier
253+
* filtering.
254+
* *
255+
* @param dev Pointer to the device structure for the driver instance.
256+
* @param filter_id filter id returned by can_attach_isr or can_attach_msgq.
257+
*
258+
* @retval none
259+
*/
260+
typedef void (*can_detach_t)(struct device *dev, int filter_id);
261+
262+
struct can_driver_api {
263+
can_configure_t configure;
264+
can_send_t send;
265+
can_attach_isr_t attach_isr;
266+
can_attach_msgq_t attach_msgq;
267+
can_detach_t detach;
268+
};
269+
270+
271+
__syscall int can_send(struct device *dev, struct can_msg *msg,
272+
s32_t timeout, can_tx_callback_t callback_isr);
273+
274+
static inline int _impl_can_send(struct device *dev, struct can_msg *msg,
275+
s32_t timeout, can_tx_callback_t callback_isr)
276+
{
277+
const struct can_driver_api *api = dev->driver_api;
278+
279+
return api->send(dev, msg, timeout, callback_isr);
280+
}
281+
282+
/*
283+
* Derived can APIs -- all implemented in terms of can_send()
284+
*/
285+
286+
/**
287+
* @brief Write a set amount of data to the can bus.
288+
*
289+
* This routine writes a set amount of data synchronously.
290+
*
291+
* @param dev Pointer to the device structure for the driver instance.
292+
* @param data Data to send.
293+
* @param length Number of bytes to write (max. 8).
294+
* @param id Identifier of the can message.
295+
* @param rtr Send remote transmission request or data frame
296+
* @param timeout Waiting for empty tx mailbox timeout in ms or K_FOREVER
297+
*
298+
* @retval 0 If successful.
299+
* @retval -EIO General input / output error.
300+
* @retval -EINVAL if length > 8.
301+
*/
302+
static inline int can_write(struct device *dev, u8_t *data, u8_t length,
303+
u32_t id, enum can_rtr rtr, s32_t timeout)
304+
{
305+
struct can_msg msg;
306+
307+
if (length > 8) {
308+
return -EINVAL;
309+
}
310+
311+
if (id > CAN_MAX_STD_ID) {
312+
msg.id_type = CAN_EXTENDED_IDENTIFIER;
313+
msg.ext_id = id & CAN_EXT_ID_MASK;
314+
} else {
315+
msg.id_type = CAN_STANDARD_IDENTIFIER;
316+
msg.std_id = id;
317+
}
318+
319+
msg.dlc = length;
320+
msg.rtr = rtr;
321+
memcpy(msg.data, data, length);
322+
323+
return can_send(dev, &msg, timeout, NULL);
324+
}
325+
326+
327+
__syscall int can_attach_msgq(struct device *dev, struct k_msgq *msg_q,
328+
const struct can_filter *filter);
329+
330+
static inline int _impl_can_attach_msgq(struct device *dev,
331+
struct k_msgq *msg_q,
332+
struct can_filter *filter)
333+
{
334+
const struct can_driver_api *api = dev->driver_api;
335+
336+
return api->attach_msgq(dev, msg_q, filter);
337+
}
338+
339+
340+
__syscall int can_attach_isr(struct device *dev, can_rx_callback_t isr,
341+
const struct can_filter *filter);
342+
static inline int _impl_can_attach_isr(struct device *dev,
343+
can_rx_callback_t isr,
344+
const struct can_filter *filter)
345+
{
346+
const struct can_driver_api *api = dev->driver_api;
347+
348+
return api->attach_isr(dev, isr, filter);
349+
}
350+
351+
352+
__syscall void can_detach(struct device *dev, int filter_id);
353+
354+
static inline void _impl_can_detach(struct device *dev, int filter_id)
355+
{
356+
const struct can_driver_api *api = dev->driver_api;
357+
358+
return api->detach(dev, filter_id);
359+
}
360+
361+
362+
__syscall int can_configure(struct device *dev, enum can_mode mode,
363+
u32_t bitrate);
364+
365+
static inline int _impl_can_configure(struct device *dev, enum can_mode mode,
366+
u32_t bitrate)
367+
{
368+
const struct can_driver_api *api = dev->driver_api;
369+
370+
return api->configure(dev, mode, bitrate);
371+
}
372+
373+
#ifdef __cplusplus
374+
}
375+
#endif
376+
/**
377+
* @}
378+
*/
379+
#include <syscalls/can.h>
380+
381+
#endif /*__DRIVERS_CAN_H*/

0 commit comments

Comments
 (0)