refine: move BIT Macro to nuttx/bits.h

The BIT macro is widely used in NuttX,
and to achieve a unified strategy,
we have placed the implementation of the BIT macro
in bits.h to simplify code implementation.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
This commit is contained in:
hujun5 2023-10-10 09:50:35 +08:00 committed by Xiang Xiao
parent 602c644f4d
commit 061be5f18e
15 changed files with 34 additions and 60 deletions

View file

@ -29,6 +29,7 @@
#include <stdbool.h>
#include <sys/param.h>
#include <nuttx/bits.h>
typedef signed char int8; /* !< Signed 8 bit integer */
typedef unsigned char uint8; /* !< Unsigned 8 bit integer */
@ -39,15 +40,11 @@ typedef unsigned short uint16; /* !< Unsigned 16 bit integer */
typedef signed long int32; /* !< Signed 32 bit integer */
typedef unsigned long uint32; /* !< Unsigned 32 bit integer */
typedef uint8 halDataAlign_t; /* !< Used for byte alignment */
#define ALIGN4_U8 __align(4) uint8
#define ALIGN4_U16 __align(4) uint16
#define ALIGN4_INT8 __align(4) int8
#define ALIGN4_INT16 __align(4) int16
#define BIT(n) (1ul << (n))
#define write_reg(addr,data) (*(volatile unsigned int *)(addr) = (unsigned int)(data))
#define read_reg(addr) (*(volatile unsigned int *)(addr))

View file

@ -28,6 +28,7 @@
#include <nuttx/config.h>
#include "arm_internal.h"
#include <nuttx/bits.h>
/****************************************************************************
* Pre-processor Definitions
@ -47,9 +48,7 @@
/* Common macros definition */
#define BIT(n) (1 << (n))
#define BIT_MASK_LEN(len) (BIT(len)-1)
#define BIT_RNG(s, e) (BIT_MASK_LEN((e) - (s) + 1) << (s))
#define BIT_RNG(s, e) (GENMASK(e, s))
#define BM_SET(x, m) ((x) |= (m))
#define BM_CLR(x, m) ((x) &= ~(m))
#define BM_IS_SET(x, m) ((x) & (m))

View file

@ -35,6 +35,7 @@
#endif
#include <sys/param.h>
#include <nuttx/bits.h>
#include "barriers.h"
@ -42,7 +43,6 @@
* Pre-processor Definitions
****************************************************************************/
#define BIT(n) ((1UL) << (n))
#define BIT64(n) ((1ULL) << (n))
/* Bit mask with bits 0 through n-1 (inclusive) set,

View file

@ -24,6 +24,7 @@
#include <stdint.h>
#include <stdbool.h>
#include <nuttx/bits.h>
#include "esp32c3_attr.h"
@ -254,8 +255,6 @@
#define SOC_INTERRUPT_LEVEL_MEDIUM 4
#define BIT(nr) (1UL << (nr))
/* Extract the field from the register and shift it to avoid wrong reading */
#define REG_MASK(_reg, _field) (((_reg) & (_field##_M)) >> (_field##_S))

View file

@ -29,6 +29,7 @@
#include <stdbool.h>
#include "xtensa_attr.h"
#include <nuttx/bits.h>
/****************************************************************************
* Pre-processor Definitions
@ -77,8 +78,6 @@
#define ETS_UNCACHED_ADDR(addr) (addr)
#define ETS_CACHED_ADDR(addr) (addr)
#define BIT(nr) (1UL << (nr))
/* Write value to register */
#define REG_WRITE(_r, _v) (*(volatile uint32_t *)(_r)) = (_v)

View file

@ -30,6 +30,7 @@
#include "xtensa.h"
#include "xtensa_attr.h"
#include <nuttx/bits.h>
/****************************************************************************
* Pre-processor Definitions
@ -79,8 +80,6 @@
#define ETS_UNCACHED_ADDR(addr) (addr)
#define ETS_CACHED_ADDR(addr) (addr)
#define BIT(nr) (1UL << (nr))
/* Write value to register */
#define REG_WRITE(_r, _v) (*(volatile uint32_t *)(_r)) = (_v)

View file

@ -31,6 +31,7 @@
#endif
#include "xtensa_attr.h"
#include <nuttx/bits.h>
/****************************************************************************
* Pre-processor Definitions
@ -185,8 +186,6 @@
#define ETS_UNCACHED_ADDR(addr) (addr)
#define ETS_CACHED_ADDR(addr) (addr)
#define BIT(nr) (1UL << (nr))
#ifndef __ASSEMBLY__
/* Write value to register */

View file

@ -30,6 +30,7 @@
#include <stdint.h>
#include <strings.h>
#include <sys/param.h>
#include <nuttx/bits.h>
#ifdef CONFIG_CLK
@ -37,7 +38,6 @@
* Pre-processor Definitions
****************************************************************************/
#define BIT(nr) (1ULL << (nr))
#define MASK(width) (BIT(width) - 1)
#define MULT_ROUND_UP(r, m) ((r) * (m) + (m) - 1)
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))

View file

@ -35,6 +35,7 @@
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/bits.h>
#include <nuttx/spi/spi.h>
#include <nuttx/lcd/lcd.h>
#include <nuttx/lcd/max7219.h>
@ -119,11 +120,6 @@
#define LS_BIT (1 << 0)
#define MS_BIT (1 << 7)
#define BIT(nr) (1 << (nr))
#define BITS_PER_BYTE 8
#define BIT_MASK(nr) (1 << ((nr) % BITS_PER_BYTE))
#define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE)
/****************************************************************************
* Private Type Definition
****************************************************************************/
@ -289,14 +285,14 @@ static struct max7219_dev_s g_max7219dev =
static inline void __set_bit(int nr, uint8_t * addr)
{
uint8_t mask = BIT_MASK(nr);
uint8_t mask = BIT_BYTE_MASK(nr);
uint8_t *p = ((uint8_t *) addr) + BIT_BYTE(nr);
*p |= mask;
}
static inline void __clear_bit(int nr, uint8_t * addr)
{
uint8_t mask = BIT_MASK(nr);
uint8_t mask = BIT_BYTE_MASK(nr);
uint8_t *p = ((uint8_t *) addr) + BIT_BYTE(nr);
*p &= ~mask;
}

View file

@ -33,8 +33,7 @@
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/wqueue.h>
#include <nuttx/clock.h>
#include <nuttx/bits.h>
#include <nuttx/spi/spi.h>
#include <nuttx/lcd/lcd.h>
#include <nuttx/lcd/memlcd.h>
@ -245,21 +244,16 @@ static struct memlcd_dev_s g_memlcddev =
*
****************************************************************************/
#define BIT(nr) (1 << (nr))
#define BITS_PER_BYTE 8
#define BIT_MASK(nr) (1 << ((nr) % BITS_PER_BYTE))
#define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE)
static inline void __set_bit(int nr, uint8_t * addr)
{
uint8_t mask = BIT_MASK(nr);
uint8_t mask = BIT_BYTE_MASK(nr);
uint8_t *p = ((uint8_t *) addr) + BIT_BYTE(nr);
*p |= mask;
}
static inline void __clear_bit(int nr, uint8_t * addr)
{
uint8_t mask = BIT_MASK(nr);
uint8_t mask = BIT_BYTE_MASK(nr);
uint8_t *p = ((uint8_t *) addr) + BIT_BYTE(nr);
*p &= ~mask;
}

View file

@ -32,6 +32,7 @@
#include <debug.h>
#include <string.h>
#include <limits.h>
#include <nuttx/bits.h>
#include <nuttx/mutex.h>
#include <nuttx/signal.h>
@ -50,10 +51,6 @@
* Pre-processor Definitions
****************************************************************************/
/* Sets bit @n */
#define BIT(n) (1 << (n))
/* Creates a mask of @m bits, i.e. MASK(2) -> 00000011 */
#define MASK(m) (BIT(m) - 1)

View file

@ -38,6 +38,7 @@
#include <nuttx/irq.h>
#include <nuttx/arch.h>
#include <nuttx/bits.h>
#include <nuttx/spinlock.h>
#include <nuttx/init.h>
#include <nuttx/fs/ioctl.h>
@ -65,7 +66,6 @@
#endif
#define PL011_BIT_MASK(x, y) (((2 << (x)) - 1) << (y))
#define BIT(n) ((1UL) << (n))
/* PL011 Uart Flags Register */
#define PL011_FR_CTS BIT(0) /* clear to send - inverted */

View file

@ -74,6 +74,7 @@
#include <limits.h>
#include <nuttx/mutex.h>
#include <nuttx/bits.h>
#include <nuttx/compiler.h>
#include <nuttx/kmalloc.h>
#include <nuttx/spi/spi.h>
@ -88,10 +89,6 @@
#define DEBUG 1
/* Sets bit @n */
#define BIT(n) (1 << (n))
/* Creates a mask of @m bits, i.e. MASK(2) -> 00000011 */
#define MASK(m) (BIT((m) + 1) - 1)

View file

@ -26,31 +26,32 @@
****************************************************************************/
#include <assert.h>
#include <inttypes.h>
#include <stdint.h>
#include <limits.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef BITS_PER_BYTE
# define BITS_PER_BYTE 8
# define BITS_PER_BYTE CHAR_BIT
#endif
#if UINTPTR_MAX > UINT32_MAX
# define BITS_PER_LONG 64
#else
# define BITS_PER_LONG 32
#ifndef BITS_PER_LONG
# define BITS_PER_LONG (sizeof(unsigned long) * BITS_PER_BYTE)
#endif
#ifndef BITS_PER_LONG_LONG
# define BITS_PER_LONG_LONG 64
# define BITS_PER_LONG_LONG (sizeof(unsigned long long) * BITS_PER_BYTE)
#endif
#define BIT_MASK(nr) (UINT32_C(1) << ((nr) % BITS_PER_LONG))
#define BIT_BYTE_MASK(nr) (1ul << ((nr) % BITS_PER_BYTE))
#define BIT_WORD_MASK(nr) (1ul << ((nr) % BITS_PER_LONG))
#define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE)
#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
#define BIT_ULL_MASK(nr) (UINT64_C(1) << ((nr) % BITS_PER_LONG_LONG))
#define BIT_ULL_MASK(nr) (1ull << ((nr) % BITS_PER_LONG_LONG))
#define BIT_ULL_WORD(nr) ((nr) / BITS_PER_LONG_LONG)
#define BIT(nr) (1ul << (nr))
#define BIT_ULL(nr) (1ull << (nr))
/* Create a contiguous bitmask starting at bit position @l and ending at
* position @h. For example
@ -58,14 +59,14 @@
*/
#define __GENMASK(h, l) \
(((~UINT32_C(0)) - (UINT32_C(1) << (l)) + 1) & \
(~UINT32_C(0) >> (BITS_PER_LONG - 1 - (h))))
(((~0ul) - (1ul << (l)) + 1) & \
(~0ul >> (BITS_PER_LONG - 1 - (h))))
#define GENMASK(h, l) \
(BUILD_BUG_ON_ZERO((l) > (h)) + __GENMASK(h, l))
#define __GENMASK_ULL(h, l) \
(((~UINT64_C(0)) - (UINT64_C(1) << (l)) + 1) & \
(~UINT64_C(0) >> (BITS_PER_LONG_LONG - 1 - (h))))
(((~0ull) - (1ull << (l)) + 1) & \
(~0ull >> (BITS_PER_LONG_LONG - 1 - (h))))
#define GENMASK_ULL(h, l) \
(BUILD_BUG_ON_ZERO((l) > (h)) + __GENMASK_ULL(h, l))

View file

@ -28,15 +28,12 @@
#include <nuttx/config.h>
#include <nuttx/sensors/sensor.h>
#include <nuttx/sensors/ioctl.h>
#include <nuttx/bits.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Sets bit @n */
#define BIT(n) (1 << (n))
/* Creates a mask of @m bits, i.e. MASK(2) -> 00000011 */
#define MASK(m) (BIT(m) - 1)