MIP_SDK
v3.0.0-187-g93c7302
MicroStrain Communications Library for embedded systems
|
Go to the documentation of this file.
33 static_assert(std::is_unsigned<T>::value,
"Register type must be unsigned.");
34 assert(nbits <= std::numeric_limits<T>::digits);
36 nbits &= std::numeric_limits<T>::digits - 1;
37 return T( T(1u) << nbits ) - 1;
58 constexpr T
bitmaskN(
unsigned int bitI,
unsigned int nbits)
60 assert(bitI < std::numeric_limits<T>::digits);
61 return bitmaskFirstN<T>(nbits) << bitI;
83 return bitmaskFirstN<T>(bitJ) & ~bitmaskFirstN<T>(bitI);
106 return bitmaskFirstN<T>(bitJ+1) & ~bitmaskFirstN<T>(bitI);
128 template<
typename RegType>
129 constexpr
bool testBit(RegType reg,
unsigned int bit)
131 assert(bit < std::numeric_limits<RegType>::digits);
133 return (reg >> bit) & 1u;
146 template<
typename RegType>
147 constexpr
void setBit(RegType& reg,
unsigned int bit,
bool value=
true)
149 assert(bit < std::numeric_limits<RegType>::digits);
151 using T =
typename std::remove_volatile<RegType>::type;
153 tmp &= ~T(T(1) << bit);
154 tmp |= T(value) << bit;
177 template<
typename RegType>
178 constexpr RegType
getBitsN(RegType reg,
unsigned int bitI,
unsigned int nbits)
180 assert(bitI < std::numeric_limits<RegType>::digits);
182 return (reg >> bitI) & bitmaskFirstN<RegType>(nbits);
199 template<
typename RegType>
200 constexpr RegType
getBitsItoJ(RegType reg,
unsigned int bitI,
unsigned int bitJ)
202 assert(bitI < std::numeric_limits<RegType>::digits);
204 return (reg >> bitI) & bitmaskItoJ<RegType>(bitI, bitJ);
220 template<
typename RegType>
221 constexpr RegType
getBitsIthruJ(RegType reg,
unsigned int bitI,
unsigned int bitJ)
223 assert(bitI < std::numeric_limits<RegType>::digits);
225 return (reg >> bitI) & bitmaskIthruJ<RegType>(bitI, bitJ);
245 template<
typename RegType>
246 constexpr
void setBitsN(RegType& reg,
unsigned int bitI,
unsigned int nbits)
248 using T =
typename std::remove_volatile<RegType>::type;
251 tmp |= bitmaskN<T>(bitI, nbits);
264 template<
typename RegType>
265 constexpr
void setBitsItoJ(RegType& reg,
unsigned int bitI,
unsigned int bitJ)
267 using T =
typename std::remove_volatile<RegType>::type;
270 tmp |= bitmaskItoJ<T>(bitI, bitJ);
283 template<
typename RegType>
284 constexpr
void setBitsIthruJ(RegType& reg,
unsigned int bitI,
unsigned int bitJ)
286 using T =
typename std::remove_volatile<RegType>::type;
289 tmp |= bitmaskIthruJ<T>(bitI, bitJ);
310 template<
typename RegType>
311 constexpr
void clearBitsN(RegType& reg,
unsigned int bitI,
unsigned int nbits)
313 using T =
typename std::remove_volatile<RegType>::type;
316 tmp &= ~bitmaskN<T>(bitI, nbits);
329 template<
typename RegType>
330 constexpr
void clearBitsItoJ(RegType& reg,
unsigned int bitI,
unsigned int bitJ)
332 using T =
typename std::remove_volatile<RegType>::type;
335 tmp &= ~bitmaskItoJ<T>(bitI, bitJ);
348 template<
typename RegType>
351 using T =
typename std::remove_volatile<RegType>::type;
354 tmp &= ~bitmaskIthruJ<T>(bitI, bitJ);
378 template<
typename RegType,
typename ValType>
379 constexpr
void setBitsN(RegType& reg,
unsigned int bitI,
unsigned int nbits, ValType value)
381 static_assert(std::is_unsigned<RegType>::value,
"Register type must be unsigned.");
383 using T =
typename std::remove_volatile<RegType>::type;
387 assert(bitI < std::numeric_limits<T>::digits);
388 assert(nbits <= std::numeric_limits<T>::digits-bitI);
389 assert((valueT & ~bitmaskFirstN<T>(nbits)) == 0);
393 const T mask = bitmaskN<T>(bitI, nbits);
396 tmp |= (valueT << bitI) & mask;
413 template<
typename RegType,
typename ValType>
414 constexpr
void setBitsItoJ(RegType& reg,
unsigned int bitI,
unsigned int bitJ, ValType value)
416 assert(bitJ >= bitI);
418 return setBitsN(reg, bitI, bitJ-bitI, value);
433 template<
typename RegType,
typename ValType>
434 constexpr
void modifyBitsIthruJ(RegType& reg,
unsigned int bitI,
unsigned int bitJ, ValType value)
449 template<class TestType, bool IsEnum = std::is_enum<TestType>::value>
452 static_assert(std::is_integral<TestType>::value,
"Type must be an integer.");
457 template<
class TestType>
460 static_assert(std::is_enum<TestType>::value,
"Expected an enum.");
462 using type =
typename std::underlying_type<TestType>::type;
490 template<
class T,
unsigned int BitI,
unsigned int Nbits>
495 static constexpr
inline unsigned int bitI = BitI;
496 static constexpr
inline unsigned int nbits = Nbits;
498 static_assert(std::numeric_limits<BaseType>::digits >=
nbits,
"T doesn't have enough bits");
500 template<
typename RegType>
503 template<
typename RegType>
506 template<
typename RegType>
518 template<
class T,
unsigned int BitI,
unsigned int BitJ>
529 template<
class T,
unsigned int BitI,
unsigned int BitJ>
537 template<
unsigned int n
bytes>
569 template<
unsigned int nbits>
constexpr void modifyBitsIthruJ(RegType ®, unsigned int bitI, unsigned int bitJ, ValType value)
Writes a value to a specific set of bits in a variable as if it were a bitfield.
Definition: bits.hpp:434
constexpr T bitmaskN(unsigned int bitI, unsigned int nbits)
Creates a bitmask with N '1' bits starting at bit I.
Definition: bits.hpp:58
static constexpr void clear(RegType ®)
Definition: bits.hpp:507
constexpr T bitmaskIthruJ(unsigned int bitI, unsigned int bitJ)
Creates a bitmask with bits [bitI..bitJ] set to 1 (includes bit J).
Definition: bits.hpp:104
Helper type to get an integer of at least N bits in size.
Definition: bits.hpp:570
constexpr void setBitsN(RegType ®, unsigned int bitI, unsigned int nbits)
Sets a series of consecutive bits in a variable to 1.
Definition: bits.hpp:246
constexpr RegType getBitsIthruJ(RegType reg, unsigned int bitI, unsigned int bitJ)
Extracts a bitfield from a variable value.
Definition: bits.hpp:221
uint32_t Unsigned
Definition: bits.hpp:550
int64_t Signed
Definition: bits.hpp:543
Helper type to get an integer of at least N bytes in size.
Definition: bits.hpp:538
constexpr T bitmaskItoJ(unsigned int bitI, unsigned int bitJ)
Creates a bitmask with bits [bitI..bitJ) set to 1 (excludes bit J).
Definition: bits.hpp:80
int8_t Signed
Definition: bits.hpp:562
constexpr RegType getBitsItoJ(RegType reg, unsigned int bitI, unsigned int bitJ)
Extracts a bitfield from a variable value.
Definition: bits.hpp:200
typename std::underlying_type< TestType >::type type
Definition: bits.hpp:462
typename detail::BaseIntegerType< T >::type BaseType
Definition: bits.hpp:494
uint8_t Unsigned
Definition: bits.hpp:561
constexpr bool testBit(RegType reg, unsigned int bit)
Checks if a specific bit in a value is set.
Definition: bits.hpp:129
constexpr void setBitsIthruJ(RegType ®, unsigned int bitI, unsigned int bitJ)
Sets a series of consecutive bits in a variable to 1.
Definition: bits.hpp:284
A class which represents part of a bitfield (see BitfieldMemberN)
Definition: bits.hpp:519
constexpr void clearBitsIthruJ(RegType ®, unsigned int bitI, unsigned int bitJ)
Sets a series of consecutive bits in a variable to 0.
Definition: bits.hpp:349
TestType type
Definition: bits.hpp:454
uint16_t Unsigned
Definition: bits.hpp:556
A class which represents part of a bitfield (see BitfieldMemberN)
Definition: bits.hpp:530
T Type
Definition: bits.hpp:493
static constexpr void set(RegType ®, Type value)
Definition: bits.hpp:504
constexpr RegType getBitsN(RegType reg, unsigned int bitI, unsigned int nbits)
Extracts a bitfield from a variable value.
Definition: bits.hpp:178
constexpr void setBit(RegType ®, unsigned int bit, bool value=true)
Sets a specific bit in a variable to a specified value.
Definition: bits.hpp:147
int16_t Signed
Definition: bits.hpp:557
constexpr void clearBitsItoJ(RegType ®, unsigned int bitI, unsigned int bitJ)
Sets a series of consecutive bits in a variable to 0.
Definition: bits.hpp:330
int32_t Signed
Definition: bits.hpp:551
static constexpr T get(RegType reg)
Definition: bits.hpp:501
constexpr void setBitsItoJ(RegType ®, unsigned int bitI, unsigned int bitJ)
Sets a series of consecutive bits in a variable to 1.
Definition: bits.hpp:265
constexpr T bitmaskFirstN(unsigned int nbits)
Creates a bitmask with 1s in the first N bits, i.e. bits [0..N).
Definition: bits.hpp:31
A class which represents part of a bitfield.
Definition: bits.hpp:491
static constexpr unsigned int nbits
Definition: bits.hpp:496
static constexpr unsigned int bitI
Definition: bits.hpp:495
constexpr void clearBitsN(RegType ®, unsigned int bitI, unsigned int nbits)
Sets a series of consecutive bits in a variable to 0.
Definition: bits.hpp:311
uint64_t Unsigned
Definition: bits.hpp:542
Definition: embedded_time.h:8