From 3dd5f5c789db8d1b1e684fccba3ee93409a845db Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 2 Jun 2021 14:07:17 +0200 Subject: [PATCH] sys/bit: add bit_checkXX() functions --- sys/include/bit.h | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/sys/include/bit.h b/sys/include/bit.h index a0cb78dd4fed..2879bf92019e 100644 --- a/sys/include/bit.h +++ b/sys/include/bit.h @@ -197,6 +197,57 @@ static inline void bit_clear8(volatile uint8_t *ptr, uint8_t bit) *((volatile uint8_t *)bitband_addr(ptr, bit)) = 0; } +/** + * @brief Checks if a single bit in the 32 bit word pointed to by @p ptr is set + * + * The effect is the same as for the following snippet: + * + * @code{c} + * *ptr & (1 << bit); + * @endcode + * + * @param[in] ptr pointer to target word + * @param[in] bit bit number within the word + */ +static inline bool bit_check32(volatile uint32_t *ptr, uint8_t bit) +{ + return *((volatile uint32_t *)bitband_addr(ptr, bit)); +} + +/** + * @brief Checks if a single bit in the 16 bit word pointed to by @p ptr is set + * + * The effect is the same as for the following snippet: + * + * @code{c} + * *ptr & (1 << bit); + * @endcode + * + * @param[in] ptr pointer to target word + * @param[in] bit bit number within the word + */ +static inline bool bit_check16(volatile uint16_t *ptr, uint8_t bit) +{ + return *((volatile uint16_t *)bitband_addr(ptr, bit)); +} + +/** + * @brief Checks if a single bit in the 8 bit byte pointed to by @p ptr is set + * + * The effect is the same as for the following snippet: + * + * @code{c} + * *ptr & (1 << bit); + * @endcode + * + * @param[in] ptr pointer to target byte + * @param[in] bit bit number within the byte + */ +static inline bool bit_check8(volatile uint8_t *ptr, uint8_t bit) +{ + return *((volatile uint8_t *)bitband_addr(ptr, bit)); +} + /** @} */ #else /* CPU_HAS_BITBAND */ @@ -231,6 +282,21 @@ static inline void bit_clear8(volatile uint8_t *ptr, uint8_t bit) *ptr &= ~(1 << (bit)); } +static inline bool bit_check32(volatile uint32_t *ptr, uint8_t bit) +{ + return *ptr & (1 << bit); +} + +static inline bool bit_check16(volatile uint16_t *ptr, uint8_t bit) +{ + return *ptr & (1 << bit); +} + +static inline bool bit_check8(volatile uint8_t *ptr, uint8_t bit) +{ + return *ptr & (1 << bit); +} + #endif /* CPU_HAS_BITBAND */ #endif /* !BITBAND_FUNCTIONS_PROVIDED */