bit.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Eistec AB
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
19 #ifndef BIT_H
20 #define BIT_H
21 
22 #include <stdint.h>
23 #include "cpu.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /* Define BITBAND_FUNCTIONS_PROVIDED 1 if the CPU provides its own
30  * implementations for bit manipulation */
31 #if !BITBAND_FUNCTIONS_PROVIDED
32 
33 #if DOXYGEN
34 
35 #define CPU_HAS_BITBAND 1 || 0 (1 for Cortex-M3 and up, 0 for Cortex-M0)
36 #endif
37 
38 #if CPU_HAS_BITBAND || DOXYGEN
39 /* Some MCUs provide a bitband address space for atomically accessing
40  * single bits of peripheral registers, and sometimes for RAM as well */
45 /* Generic bit band conversion routine */
54 static inline volatile void *bitband_addr(volatile void *ptr, uintptr_t bit)
55 {
56  return (volatile void *)((((uintptr_t)ptr) & 0xF0000000ul) + 0x2000000ul +
57  ((((uintptr_t)ptr) & 0xFFFFFul) << 5) + (bit << 2));
58 }
59 
75 static inline void bit_set32(volatile uint32_t *ptr, uint8_t bit)
76 {
77  *((volatile uint32_t *)bitband_addr(ptr, bit)) = 1;
78 }
79 
95 static inline void bit_set16(volatile uint16_t *ptr, uint8_t bit)
96 {
97  *((volatile uint16_t *)bitband_addr(ptr, bit)) = 1;
98 }
99 
115 static inline void bit_set8(volatile uint8_t *ptr, uint8_t bit)
116 {
117  *((volatile uint8_t *)bitband_addr(ptr, bit)) = 1;
118 }
119 
135 static inline void bit_clear32(volatile uint32_t *ptr, uint8_t bit)
136 {
137  *((volatile uint32_t *)bitband_addr(ptr, bit)) = 0;
138 }
139 
155 static inline void bit_clear16(volatile uint16_t *ptr, uint8_t bit)
156 {
157  *((volatile uint16_t *)bitband_addr(ptr, bit)) = 0;
158 }
159 
175 static inline void bit_clear8(volatile uint8_t *ptr, uint8_t bit)
176 {
177  *((volatile uint8_t *)bitband_addr(ptr, bit)) = 0;
178 }
179 
182 #else /* CPU_HAS_BITBAND */
183 /* CPU does not have bitbanding, fall back to plain C */
184 static inline void bit_set32(volatile uint32_t *ptr, uint8_t bit)
185 {
186  *ptr |= (1 << (bit));
187 }
188 
189 static inline void bit_set16(volatile uint16_t *ptr, uint8_t bit)
190 {
191  *ptr |= (1 << (bit));
192 }
193 
194 static inline void bit_set8(volatile uint8_t *ptr, uint8_t bit)
195 {
196  *ptr |= (1 << (bit));
197 }
198 
199 static inline void bit_clear32(volatile uint32_t *ptr, uint8_t bit)
200 {
201  *ptr &= ~(1 << (bit));
202 }
203 
204 static inline void bit_clear16(volatile uint16_t *ptr, uint8_t bit)
205 {
206  *ptr &= ~(1 << (bit));
207 }
208 
209 static inline void bit_clear8(volatile uint8_t *ptr, uint8_t bit)
210 {
211  *ptr &= ~(1 << (bit));
212 }
213 
214 #endif /* CPU_HAS_BITBAND */
215 
216 #endif /* !BITBAND_FUNCTIONS_PROVIDED */
217 
218 #ifdef __cplusplus
219 }
220 #endif
221 
222 #endif /* BIT_H */
223 
bit_clear16
static void bit_clear16(volatile uint16_t *ptr, uint8_t bit)
Clear a single bit in the 16 bit word pointed to by ptr.
Definition: bit.h:155
bitband_addr
static volatile void * bitband_addr(volatile void *ptr, uintptr_t bit)
Convert bit band region address and bit number to bit band alias address.
Definition: bit.h:54
bit_set8
static void bit_set8(volatile uint8_t *ptr, uint8_t bit)
Set a single bit in the 8 bit byte pointed to by ptr.
Definition: bit.h:115
bit_clear32
static void bit_clear32(volatile uint32_t *ptr, uint8_t bit)
Clear a single bit in the 32 bit word pointed to by ptr.
Definition: bit.h:135
bit_set16
static void bit_set16(volatile uint16_t *ptr, uint8_t bit)
Set a single bit in the 16 bit word pointed to by ptr.
Definition: bit.h:95
bit_clear8
static void bit_clear8(volatile uint8_t *ptr, uint8_t bit)
Clear a single bit in the 8 bit byte pointed to by ptr.
Definition: bit.h:175
bit_set32
static void bit_set32(volatile uint32_t *ptr, uint8_t bit)
Set a single bit in the 32 bit word pointed to by ptr.
Definition: bit.h:75