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
16/*
17 * Copyright (C) 2023-2024 Michal Lenc <michallenc@seznam.cz>
18 * Copyright (C) 2002-2009 DCE FEE CTU Prague
19 * Copyright (C) 2002-2024 Pavel Pisa <pisa@cmp.felk.cvut.cz>
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
34 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
41 */
42
43#ifndef _DEV_CAN_CAN_HELPERS_H
44#define _DEV_CAN_CAN_HELPERS_H
45
46#include <rtems.h>
47#include <stdatomic.h>
48
53#define RTEMS_CAN_USER_MAGIC 0x05402033
54
55#ifndef BIT
56#define BIT( nr ) ( 1UL << ( nr ) )
57#endif
58
59#define GENMASK( h, l ) \
60 ( ( ( ~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 ( { ( (typeof( _mask )) ( _val ) << __bf_shf( _mask ) ) & ( _mask ); } )
69#endif
70
71#ifndef FIELD_GET
72#define FIELD_GET( _mask, _reg ) \
73 ( { (typeof( _mask )) ( ( ( _reg ) & ( _mask ) ) >> __bf_shf( _mask ) ); } )
74#endif
75
76// clang-format off
77static const uint8_t rtems_can_len2dlc[] = {
78 0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
79 9, 9, 9, 9, /* 9 - 12 */
80 10, 10, 10, 10, /* 13 - 16 */
81 11, 11, 11, 11, /* 17 - 20 */
82 12, 12, 12, 12, /* 21 - 24 */
83 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
84 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
85 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
86 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
87 15, 15, 15, 15, 15, 15, 15, 15 /* 57 - 64 */
88};
89// clang-format on
90
98static inline uint8_t rtems_canfd_len2dlc( uint8_t len )
99{
100 if ( len > 64 ) {
101 return 0xF;
102 }
103
104 return rtems_can_len2dlc[ len ];
105}
106
117static inline void rtems_can_set_bit( int nr, atomic_uint *addr )
118{
119 atomic_fetch_or( addr, 1 << nr );
120}
121
132static inline void rtems_can_clear_bit( int nr, atomic_uint *addr )
133{
134 atomic_fetch_and( addr, ~( 1 << nr ) );
135}
136
148static inline int rtems_can_test_bit( int nr, atomic_uint *addr )
149{
150 return ( atomic_load( addr ) ) & ( 1 << nr ) ? 1 : 0;
151}
152
163static inline int rtems_can_test_and_set_bit( int nr, atomic_uint *addr )
164{
165 return ( atomic_fetch_or( addr, 1 << nr ) & ( 1 << nr ) ) ? 1 : 0;
166}
167
168#endif /* _DEV_CAN_CAN_HELPERS_H */