RTEMS CAN/CAN FD Stack
Loading...
Searching...
No Matches
can-helpers.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0 OR GPL-2.0-or-later */
2
13/*
14 * Copyright (C) 2023-2024 Michal Lenc <michallenc@seznam.cz>
15 * Implementation is based on original LinCAN - Linux CAN bus driver
16 * Part of OrtCAN project https://ortcan.sourceforge.net/
17 * Copyright (C) 2002-2009 DCE FEE CTU Prague <http://control.fel.cvut.cz>
18 * Copyright (C) 2002-2024 Pavel Pisa <pisa@cmp.felk.cvut.cz>
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
33 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
40 */
41
42#ifndef _DEV_CAN_CAN_HELPERS_H
43#define _DEV_CAN_CAN_HELPERS_H
44
45#include <rtems.h>
46#include <stdatomic.h>
47
48#define CAN_DEFAULT_FIFO_SIZE 64 //TODO: should be configurable from RTEMS
49
54#define RTEMS_CAN_USER_MAGIC 0x05402033
55
56#ifndef BIT
57#define BIT(nr) (1UL << (nr))
58#endif
59
60#define GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (sizeof(long) * 8 - 1 - (h))))
61
62#ifndef __bf_shf
63#define __bf_shf(x) (__builtin_ffsll(x) - 1)
64#endif
65
66#ifndef FIELD_PREP
67#define FIELD_PREP(_mask, _val) \
68 ({ \
69 ((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \
70 })
71#endif
72
73#ifndef FIELD_GET
74#define FIELD_GET(_mask, _reg) \
75 ({ \
76 (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
77 })
78#endif
79
80enum can_state {
81 CAN_STATE_ERROR_ACTIVE = 0, /* RX/TX error count < 96 */
82 CAN_STATE_ERROR_WARNING, /* RX/TX error count < 128 */
83 CAN_STATE_ERROR_PASSIVE, /* RX/TX error count < 256 */
84 CAN_STATE_BUS_OFF, /* RX/TX error count >= 256 */
85 CAN_STATE_STOPPED, /* Device is stopped */
86 CAN_STATE_SLEEPING, /* Device is sleeping */
87 CAN_STATE_MAX
88};
89
90static const uint8_t rtems_len2dlc[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
91 9, 9, 9, 9, /* 9 - 12 */
92 10, 10, 10, 10, /* 13 - 16 */
93 11, 11, 11, 11, /* 17 - 20 */
94 12, 12, 12, 12, /* 21 - 24 */
95 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
96 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
97 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
98 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
99 15, 15, 15, 15, 15, 15, 15, 15 /* 57 - 64 */
100};
101
109static inline uint8_t rtems_canfd_len2dlc( uint8_t len )
110{
111 if ( len > 64 ) {
112 return 0xF;
113 }
114
115 return rtems_len2dlc[len];
116}
117
128static inline void can_set_bit( int nr, atomic_uint *addr )
129{
130 atomic_fetch_or( addr, 1 << nr );
131}
132
143static inline void can_clear_bit( int nr, atomic_uint *addr )
144{
145 atomic_fetch_and( addr, ~( 1 << nr ) );
146}
147
158static inline int can_test_bit( int nr, atomic_uint *addr )
159{
160 return ( atomic_load( addr ) ) & ( 1 << nr ) ? 1 : 0;
161}
162
173static inline int can_test_and_set_bit( int nr, atomic_uint *addr )
174{
175 return ( atomic_fetch_or( addr, 1 << nr ) & ( 1 << nr ) ) ? 1 : 0;
176}
177
178#endif /* _DEV_CAN_CAN_HELPERS_H */