61 #if defined(KINETISK) // Teensy 3.x
70 __attribute__((always_inline))
inline volatile T &
71 bitband_address(
volatile T ®, uint8_t bit) {
72 return (*(
volatile T *)(((uint32_t)® - 0x40000000) * 32 + bit * 4 +
83 __attribute__((always_inline))
inline void setBit(
volatile T ®,
85 bitband_address(reg, bit) = 1;
95 __attribute__((always_inline))
inline void setBitFlag(
volatile T ®, T flag) {
99 bitband_address(reg, 31 - __builtin_clzl(flag)) = 1;
100 if (__builtin_popcount(flag) > 1) {
103 bitband_address(reg, __builtin_ctzl(flag)) = 1;
113 template <
typename T>
114 __attribute__((always_inline))
inline void clearBit(
volatile T ®,
116 bitband_address(reg, bit) = 0;
125 template <
typename T>
126 __attribute__((always_inline))
inline void clearBitFlag(
volatile T ®,
128 bitband_address(reg, 31 - __builtin_clzl(flag)) = 0;
129 if (__builtin_popcount(flag) > 1) {
130 bitband_address(reg, __builtin_ctzl(flag)) = 0;
141 template <
typename T>
142 __attribute__((always_inline))
inline void changeBit(
volatile T ®,
143 uint8_t bit,
bool state) {
144 bitband_address(reg, bit) = state;
154 template <
typename T>
155 __attribute__((always_inline))
inline void changeBitFlag(
volatile T ®,
157 bitband_address(reg, __builtin_ctzl(flag)) =
158 (state >> __builtin_ctzl(flag)) & 0x1;
159 if (__builtin_popcount(flag) > 1) {
160 bitband_address(reg, 31 - __builtin_clzl(flag)) =
161 (state >> (31 - __builtin_clzl(flag))) & 0x1;
172 template <
typename T>
173 __attribute__((always_inline))
inline volatile bool getBit(
volatile T ®,
175 return (
volatile bool)bitband_address(reg, bit);
185 template <
typename T>
186 __attribute__((always_inline))
inline volatile bool getBitFlag(
volatile T ®,
188 return (
volatile bool)bitband_address(reg, 31 - __builtin_clzl(flag));
191 #elif defined(__IMXRT1062__) // Teensy 4
198 template <
typename T>
199 __attribute__((always_inline))
inline void setBitFlag(
volatile T ®, T flag) {
211 template <
typename T>
212 __attribute__((always_inline))
inline void clearBitFlag(
volatile T ®,
226 template <
typename T>
227 __attribute__((always_inline))
inline void changeBitFlag(
volatile T ®,
231 if (__builtin_popcount(flag) == 1) {
233 setBitFlag(reg, flag);
235 clearBitFlag(reg, flag);
239 if ((state >> __builtin_ctzl(flag)) & 0x1) {
240 setBitFlag(reg, (uint32_t)(1 << __builtin_ctzl(flag)));
242 clearBitFlag(reg, (uint32_t)(1 << __builtin_ctzl(flag)));
245 if ((state >> (31 - __builtin_clzl(flag))) & 0x1) {
246 setBitFlag(reg, (uint32_t)(1 << (31 - __builtin_clzl(flag))));
248 clearBitFlag(reg, (uint32_t)(1 << (31 - __builtin_clzl(flag))));
260 template <
typename T>
261 __attribute__((always_inline))
inline volatile bool getBitFlag(
volatile T ®,
263 return (
volatile bool)((reg)&flag) >> (31 - __builtin_clzl(flag));
266 #elif defined(KINETISL) // Teensy LC
275 template <
typename T>
276 __attribute__((always_inline))
inline void setBit(
volatile T ®,
279 *(
volatile T *)((uint32_t)(®) | (1 << 27)) = 1 << bit;
288 template <
typename T>
289 __attribute__((always_inline))
inline void setBitFlag(
volatile T ®,
291 *(
volatile T *)((uint32_t)® | (1 << 27)) = flag;
300 template <
typename T>
301 __attribute__((always_inline))
inline void clearBit(
volatile T ®,
304 *(
volatile T *)((uint32_t)(®) | (1 << 26)) = ~(1 << bit);
313 template <
typename T>
314 __attribute__((always_inline))
inline void clearBitFlag(
volatile T ®,
317 *(
volatile T *)((uint32_t)(®) | (1 << 26)) = ~flag;
327 template <
typename T>
328 __attribute__((always_inline))
inline void changeBit(
volatile T ®,
329 uint8_t bit,
bool state) {
332 state ? setBit(reg, bit) : clearBit(reg, bit);
342 template <
typename T>
343 __attribute__((always_inline))
inline void changeBitFlag(
volatile T ®,
347 *(
volatile T *)((uint32_t)(®) | (1 << 28) | (__builtin_ctzl(flag) << 23) |
348 ((__builtin_popcount(flag) - 1) << 19)) = state;
358 template <
typename T>
359 __attribute__((always_inline))
inline volatile bool getBit(
volatile T ®,
361 return (
volatile bool)*(
volatile T *)((uint32_t)(®) | (1 << 28) |
372 template <
typename T>
373 __attribute__((always_inline))
inline volatile bool getBitFlag(
volatile T ®,
375 return (
volatile bool)*(
376 volatile T *)((uint32_t)(®) | (1 << 28) |
377 ((31 - __builtin_clzl(flag)) << 23));
384 #endif // ADC_ATOMIC_H