Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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#ifndef ZEPHYR_INCLUDE_ARCH_COMMON_SYS_BITOPS_H_
10#define ZEPHYR_INCLUDE_ARCH_COMMON_SYS_BITOPS_H_
11
12#ifndef _ASMLANGUAGE
13
14#include <zephyr/toolchain.h>
15#include <zephyr/types.h>
16#include <zephyr/sys/sys_io.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
26
27static ALWAYS_INLINE void sys_set_bit(mem_addr_t addr, unsigned int bit)
28{
29 uint32_t temp = *(volatile uint32_t *)addr;
30
31 *(volatile uint32_t *)addr = temp | (1U << bit);
32}
33
34static ALWAYS_INLINE void sys_clear_bit(mem_addr_t addr, unsigned int bit)
35{
36 uint32_t temp = *(volatile uint32_t *)addr;
37
38 *(volatile uint32_t *)addr = temp & ~(1U << bit);
39}
40
41static ALWAYS_INLINE int sys_test_bit(mem_addr_t addr, unsigned int bit)
42{
43 uint32_t temp = *(volatile uint32_t *)addr;
44
45 return temp & (1U << bit);
46}
47
48static ALWAYS_INLINE void sys_set_bits(mem_addr_t addr, unsigned int mask)
49{
50 uint32_t temp = *(volatile uint32_t *)addr;
51
52 *(volatile uint32_t *)addr = temp | mask;
53}
54
55static ALWAYS_INLINE void sys_clear_bits(mem_addr_t addr, unsigned int mask)
56{
57 uint32_t temp = *(volatile uint32_t *)addr;
58
59 *(volatile uint32_t *)addr = temp & ~mask;
60}
61
62static ALWAYS_INLINE
63 void sys_bitfield_set_bit(mem_addr_t addr, unsigned int bit)
64{
65 /* Doing memory offsets in terms of 32-bit values to prevent
66 * alignment issues
67 */
68 sys_set_bit(addr + ((bit >> 5) << 2), bit & 0x1F);
69}
70
71static ALWAYS_INLINE
72 void sys_bitfield_clear_bit(mem_addr_t addr, unsigned int bit)
73{
74 sys_clear_bit(addr + ((bit >> 5) << 2), bit & 0x1F);
75}
76
77static ALWAYS_INLINE
78 int sys_bitfield_test_bit(mem_addr_t addr, unsigned int bit)
79{
80 return sys_test_bit(addr + ((bit >> 5) << 2), bit & 0x1F);
81}
82
83static ALWAYS_INLINE
84 int sys_test_and_set_bit(mem_addr_t addr, unsigned int bit)
85{
86 int ret;
87
88 ret = sys_test_bit(addr, bit);
89 sys_set_bit(addr, bit);
90
91 return ret;
92}
93
94static ALWAYS_INLINE
95 int sys_test_and_clear_bit(mem_addr_t addr, unsigned int bit)
96{
97 int ret;
98
99 ret = sys_test_bit(addr, bit);
100 sys_clear_bit(addr, bit);
101
102 return ret;
103}
104
105static ALWAYS_INLINE
106 int sys_bitfield_test_and_set_bit(mem_addr_t addr, unsigned int bit)
107{
108 int ret;
109
110 ret = sys_bitfield_test_bit(addr, bit);
111 sys_bitfield_set_bit(addr, bit);
112
113 return ret;
114}
115
116static ALWAYS_INLINE
117 int sys_bitfield_test_and_clear_bit(mem_addr_t addr, unsigned int bit)
118{
119 int ret;
120
121 ret = sys_bitfield_test_bit(addr, bit);
122 sys_bitfield_clear_bit(addr, bit);
123
124 return ret;
125}
126
130
131#ifdef __cplusplus
132}
133#endif
134
135#endif /* _ASMLANGUAGE */
136
137#endif /* ZEPHYR_INCLUDE_ARCH_COMMON_SYS_BITOPS_H_ */
uintptr_t mem_addr_t
Memory address.
Definition sys_io.h:41
static ALWAYS_INLINE int sys_test_and_set_bit(mem_addr_t addr, unsigned int bit)
Test the bit and set it.
static ALWAYS_INLINE void sys_set_bit(mem_addr_t addr, unsigned int bit)
Set the designated bit from addr to 1.
static ALWAYS_INLINE int sys_bitfield_test_bit(mem_addr_t addr, unsigned int bit)
Test the bit if it is set or not.
static ALWAYS_INLINE void sys_clear_bit(mem_addr_t addr, unsigned int bit)
Clear the designated bit from addr to 0.
static ALWAYS_INLINE int sys_test_bit(mem_addr_t addr, unsigned int bit)
Test the bit if it is set or not.
static ALWAYS_INLINE void sys_bitfield_set_bit(mem_addr_t addr, unsigned int bit)
Set the designated bit from addr to 1.
static ALWAYS_INLINE void sys_clear_bits(mem_addr_t addr, unsigned int mask)
Masking the designated bits from addr to 0.
static ALWAYS_INLINE int sys_bitfield_test_and_clear_bit(mem_addr_t addr, unsigned int bit)
Test the bit and clear it.
static ALWAYS_INLINE int sys_bitfield_test_and_set_bit(mem_addr_t addr, unsigned int bit)
Test the bit and set it.
static ALWAYS_INLINE int sys_test_and_clear_bit(mem_addr_t addr, unsigned int bit)
Test the bit and clear it.
static ALWAYS_INLINE void sys_bitfield_clear_bit(mem_addr_t addr, unsigned int bit)
Clear the designated bit from addr to 0.
static ALWAYS_INLINE void sys_set_bits(mem_addr_t addr, unsigned int mask)
Masking the designated bits from addr to 1.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
Header file for port, memory-mapped register, and memory bit manipulation APIs.
Macros to abstract toolchain specific capabilities.