From 5b6c59397ea7c2a28219208119e0b9e994771c4d Mon Sep 17 00:00:00 2001 From: Siew Chin Lim Date: Wed, 21 Apr 2021 17:44:45 +0800 Subject: [PATCH] include: common: Add sys_set_bits and set_clear_bits inline functions Add new common inline functions sys_set_bits and set_clear_bits to set and clear multiple bits via bit mask in single function call. Signed-off-by: Siew Chin Lim --- include/arch/common/sys_bitops.h | 14 ++++++++++++++ include/sys/sys_io.h | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/include/arch/common/sys_bitops.h b/include/arch/common/sys_bitops.h index 04940569d28..723612334a4 100644 --- a/include/arch/common/sys_bitops.h +++ b/include/arch/common/sys_bitops.h @@ -42,6 +42,20 @@ static ALWAYS_INLINE int sys_test_bit(mem_addr_t addr, unsigned int bit) return temp & (1 << bit); } +static ALWAYS_INLINE void sys_set_bits(mem_addr_t addr, unsigned int mask) +{ + uint32_t temp = *(volatile uint32_t *)addr; + + *(volatile uint32_t *)addr = temp | mask; +} + +static ALWAYS_INLINE void sys_clear_bits(mem_addr_t addr, unsigned int mask) +{ + uint32_t temp = *(volatile uint32_t *)addr; + + *(volatile uint32_t *)addr = temp & ~mask; +} + static ALWAYS_INLINE void sys_bitfield_set_bit(mem_addr_t addr, unsigned int bit) { diff --git a/include/sys/sys_io.h b/include/sys/sys_io.h index 95b7d4ab950..fc3c1205d61 100644 --- a/include/sys/sys_io.h +++ b/include/sys/sys_io.h @@ -245,6 +245,26 @@ typedef uintptr_t mem_addr_t; * @param bit the designated bit to set (from 0 to 31) */ +/** + * @fn static inline void sys_set_bits(mem_addr_t addr, unsigned int mask) + * @brief Masking the designated bits from addr to 1 + * + * This functions masking designated bits from addr to 1. + * + * @param addr the memory address from where to look for the bits + * @param mask the bit mask of a 32 bits data to set + */ + +/** + * @fn static inline void sys_clear_bits(mem_addr_t addr, unsigned int mask) + * @brief Masking the designated bits from addr to 0 + * + * This functions masking designated bits from addr to 0. + * + * @param addr the memory address from where to look for the bits + * @param mask the bit mask of a 32 bits data to set + */ + /** * @fn static inline void sys_clear_bit(mem_addr_t addr, unsigned int bit) * @brief Clear the designated bit from addr to 0