bme.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2017 Eistec AB
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
21 #include <stdint.h>
22 
23 #ifdef __cplusplus
24 extern "C"
25 {
26 #endif
27 
31 #define BITBAND_FUNCTIONS_PROVIDED 1
32 
33 #define BME_AND_MASK (1 << 26)
34 #define BME_OR_MASK (1 << 27)
35 #define BME_XOR_MASK (3 << 26)
36 #define BME_LAC1_MASK(BIT) ((1 << 27) | ((BIT) << 21))
37 #define BME_LAS1_MASK(BIT) ((3 << 26) | ((BIT) << 21))
45 #define BME_BF_MASK(bit, width) ((1 << 28) | ((bit) << 23) | (((width) - 1 ) << 19))
46 
59 static inline volatile void *bme_bf_addr(volatile void *ptr, uintptr_t bit, uintptr_t width)
60 {
61  return (volatile void *)(((uintptr_t)ptr) | BME_BF_MASK(bit, width));
62 }
63 
79 static inline volatile uint32_t *bme_bitfield32(volatile uint32_t *ptr, uint8_t bit, uint8_t width)
80 {
81  return (volatile uint32_t *)(bme_bf_addr(ptr, bit, width));
82 }
83 
99 static inline volatile uint16_t *bme_bitfield16(volatile uint16_t *ptr, uint8_t bit, uint8_t width)
100 {
101  return (volatile uint16_t *)(bme_bf_addr(ptr, bit, width));
102 }
103 
119 static inline volatile uint8_t *bme_bitfield8(volatile uint8_t *ptr, uint8_t bit, uint8_t width)
120 {
121  return (volatile uint8_t *)(bme_bf_addr(ptr, bit, width));
122 }
123 
124 /* For compatibility with the M3/M4 bitbanding macros: */
125 
141 static inline void bit_set32(volatile uint32_t *ptr, uint8_t bit)
142 {
143  *((volatile uint32_t *)(((uintptr_t)ptr) | BME_OR_MASK)) = (uint32_t)((1ul << bit));
144 }
145 
161 static inline void bit_set16(volatile uint16_t *ptr, uint8_t bit)
162 {
163  *((volatile uint16_t *)(((uintptr_t)ptr) | BME_OR_MASK)) = (uint16_t)((1ul << bit));
164 }
165 
181 static inline void bit_set8(volatile uint8_t *ptr, uint8_t bit)
182 {
183  *((volatile uint8_t *)(((uintptr_t)ptr) | BME_OR_MASK)) = (uint8_t)((1ul << bit));
184 }
185 
201 static inline void bit_clear32(volatile uint32_t *ptr, uint8_t bit)
202 {
203  *((volatile uint32_t *)(((uintptr_t)ptr) | BME_AND_MASK)) = (uint32_t)(~(1ul << bit));
204 }
205 
221 static inline void bit_clear16(volatile uint16_t *ptr, uint8_t bit)
222 {
223  *((volatile uint16_t *)(((uintptr_t)ptr) | BME_AND_MASK)) = (uint16_t)(~(1ul << bit));
224 }
225 
241 static inline void bit_clear8(volatile uint8_t *ptr, uint8_t bit)
242 {
243  *((volatile uint8_t *)(((uintptr_t)ptr) | BME_AND_MASK)) = (uint8_t)(~(1ul << bit));
244 }
245 
246 #ifdef __cplusplus
247 }
248 #endif
249 
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:241
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:79
static volatile void * bme_bf_addr(volatile void *ptr, uintptr_t bit, uintptr_t width)
Bit field address macro.
Definition: bme.h:59
#define BME_BF_MASK(bit, width)
Bit field extraction bitmask.
Definition: bme.h:45
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:161
#define BME_OR_MASK
OR decoration bitmask.
Definition: bme.h:34
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:201
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:181
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:141
#define BME_AND_MASK
AND decoration bitmask.
Definition: bme.h:33
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:99
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:221
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:119