Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
can.h
Go to the documentation of this file.
1
7/*
8 * Copyright (c) 2019 Alexander Wachter
9 *
10 * SPDX-License-Identifier: Apache-2.0
11 */
12
13#ifndef ZEPHYR_INCLUDE_NET_CAN_H_
14#define ZEPHYR_INCLUDE_NET_CAN_H_
15
16#include <zephyr/types.h>
17#include <net/net_ip.h>
18#include <net/net_if.h>
19#include <drivers/can.h>
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25
37/*
38 * Abbreviations
39 * BS Block Size
40 * CAN_DL CAN LL data size
41 * CF Consecutive Frame
42 * CTS Continue to send
43 * DLC Data length code
44 * FC Flow Control
45 * FF First Frame
46 * FS Flow Status
47 */
48
51#define NET_CAN_DL 8
52#define NET_CAN_MTU 0x0FFF
53
54/* 0x3DFF - bit 4 to 10 must not be zero. Also prevent stuffing bit*/
55#define NET_CAN_MULTICAST_ADDR 0x3DFF
56#define NET_CAN_DAD_ADDR 0x3DFE
57#define NET_CAN_ETH_TRANSLATOR_ADDR 0x3DF0
58#define NET_CAN_MAX_ADDR 0x3DEF
59#define NET_CAN_MIN_ADDR 0x0100
60
61#define CAN_NET_IF_ADDR_MASK 0x3FFF
62#define CAN_NET_IF_ADDR_BYTE_LEN 2U
63#define CAN_NET_IF_ADDR_DEST_POS 14U
64#define CAN_NET_IF_ADDR_DEST_MASK (CAN_NET_IF_ADDR_MASK << CAN_NET_IF_ADDR_DEST_POS)
65#define CAN_NET_IF_ADDR_SRC_POS 0U
66#define CAN_NET_IF_ADDR_SRC_MASK (CAN_NET_IF_ADDR_MASK << CAN_NET_IF_ADDR_SRC_POS)
67#define CAN_NET_IF_ADDR_MCAST_POS 28U
68#define CAN_NET_IF_ADDR_MCAST_MASK (1UL << CAN_NET_IF_ADDR_MCAST_POS)
69
70#define CAN_NET_IF_IS_MCAST_BIT (1U << 14)
71
72#define CAN_NET_FILTER_NOT_SET -1
73
76/*
77 * +-----------+ +-----------+
78 * | | | |
79 * | IPv6 | | IPv6 |
80 * | 6LoCAN | | 6LoCAN |
81 * | | | |
82 * +----+-+----+ +----+-+----+
83 * | | | | +---+
84 * +----+ | | | | / \ +-+
85 * | | | | | | +--+ / \_/ \
86 * +++ +---+ +--+----+ +-------+ +--+----+ +--/ \_/ \
87 * | | \ / | \ / \ / | \ / / |
88 * | | X | X CAN X | X | Internet |
89 * | | / \ | / \ / \ | / \ \ /
90 * +++ +---+ +----+--+ +-------+ +----+--+ +--+ /
91 * | | +-------------------+
92 * +----+
93 */
99 struct net_if_api iface_api;
100
102 int (*send)(const struct device *dev, const struct zcan_frame *frame,
103 can_tx_callback_t cb, void *cb_arg, k_timeout_t timeout);
105 int (*attach_filter)(const struct device *dev, can_rx_callback_t cb,
106 void *cb_arg, const struct zcan_filter *filter);
108 void (*detach_filter)(const struct device *dev, int filter_id);
110 int (*enable)(const struct device *dev, bool enable);
111};
112
113/* Make sure that the network interface API is properly setup inside
114 * net_can_api struct (it is the first one).
115 */
116BUILD_ASSERT(offsetof(struct net_can_api, iface_api) == 0);
117
120#define CANBUS_L2_CTX_TYPE struct net_canbus_context *
121
125struct canbus_net_ctx {
127 int dad_filter_id;
129 struct k_work dad_work;
131 struct net_if *iface;
133 uint16_t ll_addr;
135 struct k_fifo tx_queue;
137 struct k_fifo rx_err_queue;
139 struct k_thread queue_handler;
141 K_KERNEL_STACK_MEMBER(queue_stack, 512);
142};
143
148struct net_canbus_lladdr {
149 uint16_t addr : 14;
150};
151
159struct canbus_fc_opts {
161 uint8_t bs;
163 uint8_t stmin;
164};
165
172struct canbus_isotp_tx_ctx {
174 struct net_pkt *pkt;
176 struct _timeout timeout;
178 struct canbus_fc_opts opts;
180 struct net_canbus_lladdr dest_addr;
182 uint16_t rem_len;
184 int8_t tx_backlog;
188 uint8_t act_block_nr;
190 uint8_t wft;
192 uint8_t sn : 4;
194 uint8_t is_mcast : 1;
195};
196
201struct canbus_isotp_rx_ctx {
203 struct net_pkt *pkt;
205 struct _timeout timeout;
207 uint16_t rem_len;
211 uint8_t act_block_nr;
213 uint8_t wft;
215 uint8_t sn : 4;
216};
217
223void net_6locan_init(struct net_if *iface);
224
231enum net_verdict net_canbus_translate_eth_frame(struct net_if *iface,
232 struct net_pkt *pkt);
233
240#ifdef __cplusplus
241}
242#endif
243
244#endif /* ZEPHYR_INCLUDE_NET_CAN_H_ */
ZTEST_BMEM int timeout
Definition: main.c:31
Public APIs for the CAN drivers.
void
Definition: eswifi_shell.c:15
void(* can_rx_callback_t)(struct zcan_frame *msg, void *arg)
Define the application callback handler function signature for receiving.
Definition: can.h:318
void(* can_tx_callback_t)(uint32_t error_flags, void *arg)
Define the application callback handler function signature.
Definition: can.h:308
net_verdict
Net Verdict.
Definition: net_core.h:97
#define K_KERNEL_STACK_MEMBER(sym, size)
Declare an embedded stack memory region.
Definition: thread_stack.h:285
state
Definition: http_parser_state.h:30
Public API for network interface.
IPv6 and IPv4 definitions.
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
__UINT16_TYPE__ uint16_t
Definition: stdint.h:59
__INT8_TYPE__ int8_t
Definition: stdint.h:42
Runtime device structure (in ROM) per driver instance.
Definition: device.h:367
Definition: kernel.h:2153
Definition: thread.h:201
Kernel timeout type.
Definition: sys_clock.h:65
A structure used to submit work.
Definition: kernel.h:3623
Definition: can.h:94
int(* send)(const struct device *dev, const struct zcan_frame *frame, can_tx_callback_t cb, void *cb_arg, k_timeout_t timeout)
Definition: can.h:102
int(* attach_filter)(const struct device *dev, can_rx_callback_t cb, void *cb_arg, const struct zcan_filter *filter)
Definition: can.h:105
int(* enable)(const struct device *dev, bool enable)
Definition: can.h:110
struct net_if_api iface_api
Definition: can.h:99
void(* detach_filter)(const struct device *dev, int filter_id)
Definition: can.h:108
Network Interface structure.
Definition: net_if.h:468
Network packet.
Definition: net_pkt.h:62
CAN filter structure.
Definition: can.h:234
CAN message structure.
Definition: can.h:185