-
Notifications
You must be signed in to change notification settings - Fork 67
/
atomic.h
384 lines (355 loc) · 13.7 KB
/
atomic.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* Teensy 4.x, 3.x, LC ADC library
* https://github.com/pedvide/ADC
* Copyright (c) 2020 Pedro Villanueva
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef ADC_ATOMIC_H
#define ADC_ATOMIC_H
/* int __builtin_ctz (unsigned int x):
Returns the number of trailing 0-bits in x,
starting at the least significant bit position.
If x is 0, the result is undefined.
*/
/* int __builtin_clz (unsigned int x)
Returns the number of leading 0-bits in x,
starting at the most significant bit position.
If x is 0, the result is undefined.
*/
/* int __builtin_popcount (unsigned int x)
Returns the number of 1-bits in x.
*/
// kinetis.h has the following types for addresses: uint32_t, uint16_t, uint8_t,
// int32_t, int16_t
/**
* @brief Atomic set, clear, change, or get bit in a register
*
* Clear bit in address (make it zero), set bit (make it one), or return the
* value of that bit. `changeBitFlag` can change up to 2 bits in a flag at the
* same time
*
* We can change this functions depending on the board:
* - Teensy 3.x use bitband.
* - Teensy LC has a more advanced bit manipulation engine.
* - Teensy 4 also has bitband capabilities, but are not yet implemented,
* instead registers are set and cleared manually. TODO: fix this.
*/
namespace atomic {
#if defined(KINETISK) // Teensy 3.x
/**
* @brief Bitband address: Gets the aliased address of the bit-band register
* @tparam T Address type: uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param bit Bit number of reg to read/modify
* @return A pointer to the aliased address of the bit of reg
*/
template <typename T>
__attribute__((always_inline)) inline volatile T &
bitband_address(volatile T ®, uint8_t bit) {
return (*(volatile T *)(((uint32_t)® - 0x40000000) * 32 + bit * 4 +
0x42000000));
}
/**
* @brief Set the bit in the register
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param bit Bit number of reg to set
*/
template <typename T>
__attribute__((always_inline)) inline void setBit(volatile T ®,
uint8_t bit) {
bitband_address(reg, bit) = 1;
}
/**
* @brief Set the mutibit flag in the register
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param flag mutibit flag of reg to set
*/
template <typename T>
__attribute__((always_inline)) inline void setBitFlag(volatile T ®, T flag) {
// 31-__builtin_clzl(flag) = gets bit number in flag
// __builtin_clzl works for long ints, which are guaranteed by standard to be
// at least 32 bit wide. there's no difference in the asm emitted.
bitband_address(reg, 31 - __builtin_clzl(flag)) = 1;
if (__builtin_popcount(flag) > 1) {
// __builtin_ctzl returns the number of trailing 0-bits in x, starting at
// the least significant bit position
bitband_address(reg, __builtin_ctzl(flag)) = 1;
}
}
/**
* @brief Clear the bit in the register
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param bit Bit number of reg to clear
*/
template <typename T>
__attribute__((always_inline)) inline void clearBit(volatile T ®,
uint8_t bit) {
bitband_address(reg, bit) = 0;
}
/**
* @brief Clear the mutibit flag in the register
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param flag mutibit flag of reg to clear
*/
template <typename T>
__attribute__((always_inline)) inline void clearBitFlag(volatile T ®,
T flag) {
bitband_address(reg, 31 - __builtin_clzl(flag)) = 0;
if (__builtin_popcount(flag) > 1) {
bitband_address(reg, __builtin_ctzl(flag)) = 0;
}
}
/**
* @brief Change the bit in the register to state
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param bit Bit number of reg to clear
* @param state State of bit
*/
template <typename T>
__attribute__((always_inline)) inline void changeBit(volatile T ®,
uint8_t bit, bool state) {
bitband_address(reg, bit) = state;
}
/**
* @brief Change the bit in the register to state
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param flag mutibit flag of reg to clear
* @param state State of bit
*/
template <typename T>
__attribute__((always_inline)) inline void changeBitFlag(volatile T ®,
T flag, T state) {
bitband_address(reg, __builtin_ctzl(flag)) =
(state >> __builtin_ctzl(flag)) & 0x1;
if (__builtin_popcount(flag) > 1) {
bitband_address(reg, 31 - __builtin_clzl(flag)) =
(state >> (31 - __builtin_clzl(flag))) & 0x1;
}
}
/**
* @brief Get the bit in the register
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param bit Bit number of reg to get
* @return State of bit in reg
*/
template <typename T>
__attribute__((always_inline)) inline volatile bool getBit(volatile T ®,
uint8_t bit) {
return (volatile bool)bitband_address(reg, bit);
}
/**
* @brief Get the multibit flag in the register
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param flag mutibit flag of reg to clear
* @return State of bit in reg
*/
template <typename T>
__attribute__((always_inline)) inline volatile bool getBitFlag(volatile T ®,
T flag) {
return (volatile bool)bitband_address(reg, 31 - __builtin_clzl(flag));
}
#elif defined(__IMXRT1062__) // Teensy 4
/**
* @brief Set the bits in the flag for reg
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param flag Mutibit flag of reg to set
*/
template <typename T>
__attribute__((always_inline)) inline void setBitFlag(volatile T ®, T flag) {
__disable_irq();
reg |= flag;
__enable_irq();
}
/**
* @brief Clear the bits in the flag for reg
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param flag Mutibit flag of reg to clear
*/
template <typename T>
__attribute__((always_inline)) inline void clearBitFlag(volatile T ®,
T flag) {
__disable_irq();
reg &= ~flag;
__enable_irq();
}
/**
* @brief Change the bits in the flag for reg
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param flag 1 or 2 bit flag of reg to change
* @param state 1 or 2 bit state
*/
template <typename T>
__attribute__((always_inline)) inline void changeBitFlag(volatile T ®,
T flag, T state) {
// flag can be 1 or 2 bits wide
// state can have one or two bits set
if (__builtin_popcount(flag) == 1) { // 1 bit
if (state) {
setBitFlag(reg, flag);
} else {
clearBitFlag(reg, flag);
}
} else { // 2 bits
// lsb first
if ((state >> __builtin_ctzl(flag)) & 0x1) { // lsb of state is 1
setBitFlag(reg, (uint32_t)(1 << __builtin_ctzl(flag)));
} else { // lsb is 0
clearBitFlag(reg, (uint32_t)(1 << __builtin_ctzl(flag)));
}
// msb
if ((state >> (31 - __builtin_clzl(flag))) & 0x1) { // msb of state is 1
setBitFlag(reg, (uint32_t)(1 << (31 - __builtin_clzl(flag))));
} else { // msb is 0
clearBitFlag(reg, (uint32_t)(1 << (31 - __builtin_clzl(flag))));
}
}
}
/**
* @brief Get the bits in the flag for reg
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param flag Mutibit flag of reg to get
* @return The state of the bits in flag in reg
*/
template <typename T>
__attribute__((always_inline)) inline volatile bool getBitFlag(volatile T ®,
T flag) {
return (volatile bool)((reg)&flag) >> (31 - __builtin_clzl(flag));
}
#elif defined(KINETISL) // Teensy LC
// bit manipulation engine
/**
* @brief Set the bit in the register
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param bit Bit number of reg to set
*/
template <typename T>
__attribute__((always_inline)) inline void setBit(volatile T ®,
uint8_t bit) {
// temp = *(uint32_t *)((uint32_t)(reg) | (1<<26) | (bit<<21)); // LAS
*(volatile T *)((uint32_t)(®) | (1 << 27)) = 1 << bit; // OR
}
/**
* @brief Set the mutibit flag in the register
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param flag mutibit flag of reg to set
*/
template <typename T>
__attribute__((always_inline)) inline void setBitFlag(volatile T ®,
uint32_t flag) {
*(volatile T *)((uint32_t)® | (1 << 27)) = flag; // OR
}
/**
* @brief Clear the bit in the register
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param bit Bit number of reg to clear
*/
template <typename T>
__attribute__((always_inline)) inline void clearBit(volatile T ®,
uint8_t bit) {
// temp = *(uint32_t *)((uint32_t)(reg) | (3<<27) | (bit<<21)); // LAC
*(volatile T *)((uint32_t)(®) | (1 << 26)) = ~(1 << bit); // AND
}
/**
* @brief Clear the mutibit flag in the register
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param flag mutibit flag of reg to clear
*/
template <typename T>
__attribute__((always_inline)) inline void clearBitFlag(volatile T ®,
uint32_t flag) {
// temp = *(uint32_t *)((uint32_t)(reg) | (3<<27) | (bit<<21)); // LAC
*(volatile T *)((uint32_t)(®) | (1 << 26)) = ~flag; // AND
}
/**
* @brief Change the bit in the register to state
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param bit Bit number of reg to clear
* @param state State of bit
*/
template <typename T>
__attribute__((always_inline)) inline void changeBit(volatile T ®,
uint8_t bit, bool state) {
// temp = *(uint32_t *)((uint32_t)(reg) | ((3-2*!!state)<<27) | (bit<<21)); //
// LAS/LAC
state ? setBit(reg, bit) : clearBit(reg, bit);
}
/**
* @brief Change the bit in the register to state
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param flag mutibit flag of reg to clear
* @param state State of bit
*/
template <typename T>
__attribute__((always_inline)) inline void changeBitFlag(volatile T ®,
T flag, T state) {
// BFI, bitfield width set to __builtin_popcount(flag)
// least significant bit set to __builtin_ctzl(flag)
*(volatile T *)((uint32_t)(®) | (1 << 28) | (__builtin_ctzl(flag) << 23) |
((__builtin_popcount(flag) - 1) << 19)) = state;
}
/**
* @brief Get the bit in the register
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param bit Bit number of reg to get
* @return State of bit in reg
*/
template <typename T>
__attribute__((always_inline)) inline volatile bool getBit(volatile T ®,
uint8_t bit) {
return (volatile bool)*(volatile T *)((uint32_t)(®) | (1 << 28) |
(bit << 23)); // UBFX
}
/**
* @brief Get the multibit flag in the register
* @tparam T Address type uint32_t, uint16_t, uint8_t, int32_t, int16_t
* @param reg Register in the bit-band area
* @param flag mutibit flag of reg to clear
* @return State of bit in reg
*/
template <typename T>
__attribute__((always_inline)) inline volatile bool getBitFlag(volatile T ®,
T flag) {
return (volatile bool)*(
volatile T *)((uint32_t)(®) | (1 << 28) |
((31 - __builtin_clzl(flag)) << 23)); // UBFX
}
#endif
} // namespace atomic
#endif // ADC_ATOMIC_H