Zephyr API Documentation 4.0.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
can_sja1000.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Henrik Brix Andersen <henrik@brixandersen.dk>
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
12#ifndef ZEPHYR_INCLUDE_DRIVERS_CAN_CAN_SJA1000_H_
13#define ZEPHYR_INCLUDE_DRIVERS_CAN_CAN_SJA1000_H_
14
15#include <zephyr/drivers/can.h>
16
22#define CAN_SJA1000_OCR_OCMODE_MASK GENMASK(1, 0)
23#define CAN_SJA1000_OCR_OCPOL0 BIT(2)
24#define CAN_SJA1000_OCR_OCTN0 BIT(3)
25#define CAN_SJA1000_OCR_OCTP0 BIT(4)
26#define CAN_SJA1000_OCR_OCPOL1 BIT(5)
27#define CAN_SJA1000_OCR_OCTN1 BIT(6)
28#define CAN_SJA1000_OCR_OCTP1 BIT(7)
29
30#define CAN_SJA1000_OCR_OCMODE_BIPHASE FIELD_PREP(CAN_SJA1000_OCR_OCMODE_MASK, 0U)
31#define CAN_SJA1000_OCR_OCMODE_TEST FIELD_PREP(CAN_SJA1000_OCR_OCMODE_MASK, 1U)
32#define CAN_SJA1000_OCR_OCMODE_NORMAL FIELD_PREP(CAN_SJA1000_OCR_OCMODE_MASK, 2U)
33#define CAN_SJA1000_OCR_OCMODE_CLOCK FIELD_PREP(CAN_SJA1000_OCR_OCMODE_MASK, 3U)
34
42#define CAN_SJA1000_CDR_CD_MASK GENMASK(2, 0)
43#define CAN_SJA1000_CDR_CLOCK_OFF BIT(3)
44#define CAN_SJA1000_CDR_RXINTEN BIT(5)
45#define CAN_SJA1000_CDR_CBP BIT(6)
46#define CAN_SJA1000_CDR_CAN_MODE BIT(7)
47
48#define CAN_SJA1000_CDR_CD_DIV1 FIELD_PREP(CAN_SJA1000_CDR_CD_MASK, 7U)
49#define CAN_SJA1000_CDR_CD_DIV2 FIELD_PREP(CAN_SJA1000_CDR_CD_MASK, 0U)
50#define CAN_SJA1000_CDR_CD_DIV4 FIELD_PREP(CAN_SJA1000_CDR_CD_MASK, 1U)
51#define CAN_SJA1000_CDR_CD_DIV6 FIELD_PREP(CAN_SJA1000_CDR_CD_MASK, 2U)
52#define CAN_SJA1000_CDR_CD_DIV8 FIELD_PREP(CAN_SJA1000_CDR_CD_MASK, 3U)
53#define CAN_SJA1000_CDR_CD_DIV10 FIELD_PREP(CAN_SJA1000_CDR_CD_MASK, 4U)
54#define CAN_SJA1000_CDR_CD_DIV12 FIELD_PREP(CAN_SJA1000_CDR_CD_MASK, 5U)
55#define CAN_SJA1000_CDR_CD_DIV14 FIELD_PREP(CAN_SJA1000_CDR_CD_MASK, 6U)
56
62#define CAN_SJA1000_TIMING_MIN_INITIALIZER \
63 { \
64 .sjw = 1, \
65 .prop_seg = 0, \
66 .phase_seg1 = 1, \
67 .phase_seg2 = 1, \
68 .prescaler = 1 \
69 }
70
74#define CAN_SJA1000_TIMING_MAX_INITIALIZER \
75 { \
76 .sjw = 4, \
77 .prop_seg = 0, \
78 .phase_seg1 = 16, \
79 .phase_seg2 = 8, \
80 .prescaler = 64 \
81 }
82
90typedef void (*can_sja1000_write_reg_t)(const struct device *dev, uint8_t reg, uint8_t val);
91
99typedef uint8_t (*can_sja1000_read_reg_t)(const struct device *dev, uint8_t reg);
100
112
124#define CAN_SJA1000_DT_CONFIG_GET(node_id, _custom, _read_reg, _write_reg, _ocr, _cdr, \
125 _min_bitrate) \
126 { \
127 .common = CAN_DT_DRIVER_CONFIG_GET(node_id, _min_bitrate, 1000000), \
128 .read_reg = _read_reg, \
129 .write_reg = _write_reg, \
130 .ocr = _ocr, \
131 .cdr = _cdr, \
132 .custom = _custom, \
133 }
134
147#define CAN_SJA1000_DT_CONFIG_INST_GET(inst, _custom, _read_reg, _write_reg, _ocr, _cdr, \
148 _min_bitrate) \
149 CAN_SJA1000_DT_CONFIG_GET(DT_DRV_INST(inst), _custom, _read_reg, _write_reg, _ocr, _cdr, \
150 _min_bitrate)
151
160
165 struct can_driver_data common;
166 ATOMIC_DEFINE(rx_allocs, CONFIG_CAN_MAX_FILTER);
167 struct can_sja1000_rx_filter filters[CONFIG_CAN_MAX_FILTER];
170 struct k_sem tx_idle;
173 void *custom;
174};
175
180#define CAN_SJA1000_DATA_INITIALIZER(_custom) \
181 { \
182 .custom = _custom, \
183 }
184
189int can_sja1000_set_timing(const struct device *dev, const struct can_timing *timing);
190
195int can_sja1000_get_capabilities(const struct device *dev, can_mode_t *cap);
196
201int can_sja1000_start(const struct device *dev);
202
207int can_sja1000_stop(const struct device *dev);
208
213int can_sja1000_set_mode(const struct device *dev, can_mode_t mode);
214
219int can_sja1000_send(const struct device *dev, const struct can_frame *frame, k_timeout_t timeout,
220 can_tx_callback_t callback, void *user_data);
221
226int can_sja1000_add_rx_filter(const struct device *dev, can_rx_callback_t callback, void *user_data,
227 const struct can_filter *filter);
228
233void can_sja1000_remove_rx_filter(const struct device *dev, int filter_id);
234
235#ifdef CONFIG_CAN_MANUAL_RECOVERY_MODE
240int can_sja1000_recover(const struct device *dev, k_timeout_t timeout);
241#endif /* CONFIG_CAN_MANUAL_RECOVERY_MODE */
242
247int can_sja1000_get_state(const struct device *dev, enum can_state *state,
248 struct can_bus_err_cnt *err_cnt);
249
255 can_state_change_callback_t callback, void *user_data);
256
261int can_sja1000_get_max_filters(const struct device *dev, bool ide);
262
268void can_sja1000_isr(const struct device *dev);
269
275int can_sja1000_init(const struct device *dev);
276
277#endif /* ZEPHYR_INCLUDE_DRIVERS_CAN_CAN_SJA1000_H_ */
int can_sja1000_get_max_filters(const struct device *dev, bool ide)
SJA1000 callback API upon getting the maximum number of concurrent CAN RX filters See can_get_max_fil...
void can_sja1000_set_state_change_callback(const struct device *dev, can_state_change_callback_t callback, void *user_data)
SJA1000 callback API upon setting a state change callback See can_set_state_change_callback() for arg...
int can_sja1000_init(const struct device *dev)
SJA1000 driver initialization callback.
void can_sja1000_remove_rx_filter(const struct device *dev, int filter_id)
SJA1000 callback API upon removing an RX filter See can_remove_rx_filter() for argument description.
int can_sja1000_add_rx_filter(const struct device *dev, can_rx_callback_t callback, void *user_data, const struct can_filter *filter)
SJA1000 callback API upon adding an RX filter See can_add_rx_callback() for argument description.
uint8_t(* can_sja1000_read_reg_t)(const struct device *dev, uint8_t reg)
SJA1000 driver front-end callback for reading a register value.
Definition can_sja1000.h:99
void can_sja1000_isr(const struct device *dev)
SJA1000 IRQ handler callback.
void(* can_sja1000_write_reg_t)(const struct device *dev, uint8_t reg, uint8_t val)
SJA1000 driver front-end callback for writing a register value.
Definition can_sja1000.h:90
int can_sja1000_get_capabilities(const struct device *dev, can_mode_t *cap)
SJA1000 callback API upon getting CAN controller capabilities See can_get_capabilities() for argument...
int can_sja1000_set_timing(const struct device *dev, const struct can_timing *timing)
SJA1000 callback API upon setting CAN bus timing See can_set_timing() for argument description.
int can_sja1000_stop(const struct device *dev)
SJA1000 callback API upon stopping CAN controller See can_stop() for argument description.
int can_sja1000_get_state(const struct device *dev, enum can_state *state, struct can_bus_err_cnt *err_cnt)
SJA1000 callback API upon getting the CAN controller state See can_get_state() for argument descripti...
int can_sja1000_send(const struct device *dev, const struct can_frame *frame, k_timeout_t timeout, can_tx_callback_t callback, void *user_data)
SJA1000 callback API upon sending a CAN frame See can_send() for argument description.
int can_sja1000_start(const struct device *dev)
SJA1000 callback API upon starting CAN controller See can_start() for argument description.
int can_sja1000_set_mode(const struct device *dev, can_mode_t mode)
SJA1000 callback API upon setting CAN controller mode See can_set_mode() for argument description.
Controller Area Network (CAN) driver API.
#define ATOMIC_DEFINE(name, num_bits)
Define an array of atomic variables.
Definition atomic.h:111
void(* can_state_change_callback_t)(const struct device *dev, enum can_state state, struct can_bus_err_cnt err_cnt, void *user_data)
Defines the state change callback handler function signature.
Definition can.h:312
uint32_t can_mode_t
Provides a type to hold CAN controller configuration flags.
Definition can.h:125
void(* can_rx_callback_t)(const struct device *dev, struct can_frame *frame, void *user_data)
Defines the application callback handler function signature for receiving.
Definition can.h:301
void(* can_tx_callback_t)(const struct device *dev, int error, void *user_data)
Defines the application callback handler function signature.
Definition can.h:292
can_state
Defines the state of the CAN controller.
Definition can.h:130
state
Definition parser_state.h:29
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
CAN controller error counters.
Definition can.h:232
CAN filter structure.
Definition can.h:218
CAN frame structure.
Definition can.h:172
SJA1000 driver internal configuration structure.
Definition can_sja1000.h:104
const void * custom
Definition can_sja1000.h:110
const struct can_driver_config common
Definition can_sja1000.h:105
uint8_t cdr
Definition can_sja1000.h:109
uint8_t ocr
Definition can_sja1000.h:108
can_sja1000_read_reg_t read_reg
Definition can_sja1000.h:106
can_sja1000_write_reg_t write_reg
Definition can_sja1000.h:107
SJA1000 driver internal data structure.
Definition can_sja1000.h:164
void * custom
Definition can_sja1000.h:173
can_tx_callback_t tx_callback
Definition can_sja1000.h:171
struct k_sem tx_idle
Definition can_sja1000.h:170
struct can_driver_data common
Definition can_sja1000.h:165
enum can_state state
Definition can_sja1000.h:169
struct can_sja1000_rx_filter filters[CONFIG_CAN_MAX_FILTER]
Definition can_sja1000.h:167
void * tx_user_data
Definition can_sja1000.h:172
struct k_mutex mod_lock
Definition can_sja1000.h:168
atomic_t rx_allocs[ATOMIC_BITMAP_SIZE(CONFIG_CAN_MAX_FILTER)]
Definition can_sja1000.h:166
SJA1000 driver internal RX filter structure.
Definition can_sja1000.h:155
can_rx_callback_t callback
Definition can_sja1000.h:157
void * user_data
Definition can_sja1000.h:158
struct can_filter filter
Definition can_sja1000.h:156
CAN bus timing structure.
Definition can.h:271
Runtime device structure (in ROM) per driver instance.
Definition device.h:412
Mutex Structure.
Definition kernel.h:2994
Kernel timeout type.
Definition sys_clock.h:65