Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
sys_bitops.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020, Wind River Systems, Inc.
3 * Copyright (c) 2017, Oticon A/S
4 * Copyright (c) 2020, Synopsys
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9/* Memory bits manipulation functions in non-arch-specific C code */
10
11#ifndef ZEPHYR_INCLUDE_ARCH_COMMON_SYS_BITOPS_H_
12#define ZEPHYR_INCLUDE_ARCH_COMMON_SYS_BITOPS_H_
13
14#ifndef _ASMLANGUAGE
15
16#include <toolchain.h>
17#include <zephyr/types.h>
18#include <sys/sys_io.h>
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24static ALWAYS_INLINE void sys_set_bit(mem_addr_t addr, unsigned int bit)
25{
26 uint32_t temp = *(volatile uint32_t *)addr;
27
28 *(volatile uint32_t *)addr = temp | (1 << bit);
29}
30
31static ALWAYS_INLINE void sys_clear_bit(mem_addr_t addr, unsigned int bit)
32{
33 uint32_t temp = *(volatile uint32_t *)addr;
34
35 *(volatile uint32_t *)addr = temp & ~(1 << bit);
36}
37
38static ALWAYS_INLINE int sys_test_bit(mem_addr_t addr, unsigned int bit)
39{
40 uint32_t temp = *(volatile uint32_t *)addr;
41
42 return temp & (1 << bit);
43}
44
45static ALWAYS_INLINE
46 void sys_bitfield_set_bit(mem_addr_t addr, unsigned int bit)
47{
48 /* Doing memory offsets in terms of 32-bit values to prevent
49 * alignment issues
50 */
51 sys_set_bit(addr + ((bit >> 5) << 2), bit & 0x1F);
52}
53
54static ALWAYS_INLINE
55 void sys_bitfield_clear_bit(mem_addr_t addr, unsigned int bit)
56{
57 sys_clear_bit(addr + ((bit >> 5) << 2), bit & 0x1F);
58}
59
60static ALWAYS_INLINE
61 int sys_bitfield_test_bit(mem_addr_t addr, unsigned int bit)
62{
63 return sys_test_bit(addr + ((bit >> 5) << 2), bit & 0x1F);
64}
65
66static ALWAYS_INLINE
67 int sys_test_and_set_bit(mem_addr_t addr, unsigned int bit)
68{
69 int ret;
70
71 ret = sys_test_bit(addr, bit);
72 sys_set_bit(addr, bit);
73
74 return ret;
75}
76
77static ALWAYS_INLINE
78 int sys_test_and_clear_bit(mem_addr_t addr, unsigned int bit)
79{
80 int ret;
81
82 ret = sys_test_bit(addr, bit);
83 sys_clear_bit(addr, bit);
84
85 return ret;
86}
87
88static ALWAYS_INLINE
89 int sys_bitfield_test_and_set_bit(mem_addr_t addr, unsigned int bit)
90{
91 int ret;
92
93 ret = sys_bitfield_test_bit(addr, bit);
94 sys_bitfield_set_bit(addr, bit);
95
96 return ret;
97}
98
99static ALWAYS_INLINE
100 int sys_bitfield_test_and_clear_bit(mem_addr_t addr, unsigned int bit)
101{
102 int ret;
103
104 ret = sys_bitfield_test_bit(addr, bit);
105 sys_bitfield_clear_bit(addr, bit);
106
107 return ret;
108}
109
110#ifdef __cplusplus
111}
112#endif
113
114#endif /* _ASMLANGUAGE */
115
116#endif /* ZEPHYR_INCLUDE_ARCH_COMMON_SYS_BITOPS_H_ */
#define ALWAYS_INLINE
Definition: common.h:116
static ZTEST_BMEM volatile int ret
Definition: k_float_disable.c:28
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
uintptr_t mem_addr_t
Definition: sys_io.h:21
static ALWAYS_INLINE int sys_test_and_set_bit(mem_addr_t addr, unsigned int bit)
Definition: sys_bitops.h:67
static ALWAYS_INLINE void sys_set_bit(mem_addr_t addr, unsigned int bit)
Definition: sys_bitops.h:24
static ALWAYS_INLINE int sys_bitfield_test_bit(mem_addr_t addr, unsigned int bit)
Definition: sys_bitops.h:61
static ALWAYS_INLINE void sys_clear_bit(mem_addr_t addr, unsigned int bit)
Definition: sys_bitops.h:31
static ALWAYS_INLINE int sys_test_bit(mem_addr_t addr, unsigned int bit)
Definition: sys_bitops.h:38
static ALWAYS_INLINE void sys_bitfield_set_bit(mem_addr_t addr, unsigned int bit)
Definition: sys_bitops.h:46
static ALWAYS_INLINE int sys_bitfield_test_and_clear_bit(mem_addr_t addr, unsigned int bit)
Definition: sys_bitops.h:100
static ALWAYS_INLINE int sys_bitfield_test_and_set_bit(mem_addr_t addr, unsigned int bit)
Definition: sys_bitops.h:89
static ALWAYS_INLINE int sys_test_and_clear_bit(mem_addr_t addr, unsigned int bit)
Definition: sys_bitops.h:78
static ALWAYS_INLINE void sys_bitfield_clear_bit(mem_addr_t addr, unsigned int bit)
Definition: sys_bitops.h:55
Macros to abstract toolchain specific capabilities.