bme.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 General
5  * Public License v2.1. See the file LICENSE in the top level directory for more
6  * details.
7  */
8 
22 #ifndef BME_H
23 #define BME_H
24 
25 #include <stdint.h>
26 
27 #ifdef __cplusplus
28 extern "C"
29 {
30 #endif
31 
35 #define BITBAND_FUNCTIONS_PROVIDED 1
36 
37 #define BME_AND_MASK (1 << 26)
38 #define BME_OR_MASK (1 << 27)
39 #define BME_XOR_MASK (3 << 26)
40 #define BME_LAC1_MASK(BIT) ((1 << 27) | ((BIT) << 21))
41 #define BME_LAS1_MASK(BIT) ((3 << 26) | ((BIT) << 21))
49 #define BME_BF_MASK(bit, width) ((1 << 28) | ((bit) << 23) | (((width) - 1 ) << 19))
50 
63 static inline volatile void *bme_bf_addr(volatile void *ptr, uintptr_t bit, uintptr_t width)
64 {
65  return (volatile void *)(((uintptr_t)ptr) | BME_BF_MASK(bit, width));
66 }
67 
83 static inline volatile uint32_t *bme_bitfield32(volatile uint32_t *ptr, uint8_t bit, uint8_t width)
84 {
85  return (volatile uint32_t *)(bme_bf_addr(ptr, bit, width));
86 }
87 
103 static inline volatile uint16_t *bme_bitfield16(volatile uint16_t *ptr, uint8_t bit, uint8_t width)
104 {
105  return (volatile uint16_t *)(bme_bf_addr(ptr, bit, width));
106 }
107 
123 static inline volatile uint8_t *bme_bitfield8(volatile uint8_t *ptr, uint8_t bit, uint8_t width)
124 {
125  return (volatile uint8_t *)(bme_bf_addr(ptr, bit, width));
126 }
127 
128 /* For compatibility with the M3/M4 bitbanding macros: */
129 
145 static inline void bit_set32(volatile uint32_t *ptr, uint8_t bit)
146 {
147  *((volatile uint32_t *)(((uintptr_t)ptr) | BME_OR_MASK)) = (uint32_t)((1ul << bit));
148 }
149 
165 static inline void bit_set16(volatile uint16_t *ptr, uint8_t bit)
166 {
167  *((volatile uint16_t *)(((uintptr_t)ptr) | BME_OR_MASK)) = (uint16_t)((1ul << bit));
168 }
169 
185 static inline void bit_set8(volatile uint8_t *ptr, uint8_t bit)
186 {
187  *((volatile uint8_t *)(((uintptr_t)ptr) | BME_OR_MASK)) = (uint8_t)((1ul << bit));
188 }
189 
205 static inline void bit_clear32(volatile uint32_t *ptr, uint8_t bit)
206 {
207  *((volatile uint32_t *)(((uintptr_t)ptr) | BME_AND_MASK)) = (uint32_t)(~(1ul << bit));
208 }
209 
225 static inline void bit_clear16(volatile uint16_t *ptr, uint8_t bit)
226 {
227  *((volatile uint16_t *)(((uintptr_t)ptr) | BME_AND_MASK)) = (uint16_t)(~(1ul << bit));
228 }
229 
245 static inline void bit_clear8(volatile uint8_t *ptr, uint8_t bit)
246 {
247  *((volatile uint8_t *)(((uintptr_t)ptr) | BME_AND_MASK)) = (uint8_t)(~(1ul << bit));
248 }
249 
250 #ifdef __cplusplus
251 }
252 #endif
253 
254 #endif /* BME_H */
255 
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: bme.h:205
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: bme.h:165
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: bme.h:245
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: bme.h:185
bme_bitfield32
static volatile uint32_t * bme_bitfield32(volatile uint32_t *ptr, uint8_t bit, uint8_t width)
Access a bitfield (32 bit load/store)
Definition: bme.h:83
BME_AND_MASK
#define BME_AND_MASK
AND decoration bitmask.
Definition: bme.h:37
bme_bf_addr
static volatile void * bme_bf_addr(volatile void *ptr, uintptr_t bit, uintptr_t width)
Bit field address macro.
Definition: bme.h:63
BME_OR_MASK
#define BME_OR_MASK
OR decoration bitmask.
Definition: bme.h:38
bme_bitfield8
static volatile uint8_t * bme_bitfield8(volatile uint8_t *ptr, uint8_t bit, uint8_t width)
Access a bitfield (8 bit load/store)
Definition: bme.h:123
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: bme.h:145
bme_bitfield16
static volatile uint16_t * bme_bitfield16(volatile uint16_t *ptr, uint8_t bit, uint8_t width)
Access a bitfield (16 bit load/store)
Definition: bme.h:103
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: bme.h:225
BME_BF_MASK
#define BME_BF_MASK(bit, width)
Bit field extraction bitmask.
Definition: bme.h:49