Skip to content

Commit

Permalink
refine: move BIT Macro to nuttx/bits.h
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
hujun260 committed Oct 12, 2023
1 parent 4967de8 commit 2b7a6d1
Show file tree
Hide file tree
Showing 15 changed files with 34 additions and 60 deletions.
5 changes: 1 addition & 4 deletions arch/arm/src/phy62xx/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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))

Expand Down
5 changes: 2 additions & 3 deletions arch/arm/src/tlsr82/hardware/tlsr82_register.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <nuttx/config.h>

#include "arm_internal.h"
#include <nuttx/bits.h>

/****************************************************************************
* Pre-processor Definitions
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion arch/arm64/src/common/arm64_arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
#endif

#include <sys/param.h>
#include <nuttx/bits.h>

#include "barriers.h"

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

#define BIT(n) ((1UL) << (n))
#define BIT64(n) ((1ULL) << (n))

/* Bit mask with bits 0 through n-1 (inclusive) set,
Expand Down
3 changes: 1 addition & 2 deletions arch/risc-v/src/esp32c3/hardware/esp32c3_soc.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <stdint.h>
#include <stdbool.h>
#include <nuttx/bits.h>

#include "esp32c3_attr.h"

Expand Down Expand Up @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions arch/xtensa/src/esp32/hardware/esp32_soc.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <stdbool.h>

#include "xtensa_attr.h"
#include <nuttx/bits.h>

/****************************************************************************
* Pre-processor Definitions
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions arch/xtensa/src/esp32s2/hardware/esp32s2_soc.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "xtensa.h"
#include "xtensa_attr.h"
#include <nuttx/bits.h>

/****************************************************************************
* Pre-processor Definitions
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions arch/xtensa/src/esp32s3/hardware/esp32s3_soc.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#endif

#include "xtensa_attr.h"
#include <nuttx/bits.h>

/****************************************************************************
* Pre-processor Definitions
Expand Down Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion drivers/clk/clk.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
#include <stdint.h>
#include <strings.h>
#include <sys/param.h>
#include <nuttx/bits.h>

#ifdef CONFIG_CLK

/****************************************************************************
* 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))
Expand Down
10 changes: 3 additions & 7 deletions drivers/lcd/max7219.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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
****************************************************************************/
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 3 additions & 9 deletions drivers/lcd/memlcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 1 addition & 4 deletions drivers/sensors/mpu60x0.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion drivers/serial/serial_pl011.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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 */
Expand Down
5 changes: 1 addition & 4 deletions drivers/video/max7456.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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)
Expand Down
29 changes: 15 additions & 14 deletions include/nuttx/bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,46 +26,47 @@
****************************************************************************/

#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
* GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
*/

#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))

Expand Down
5 changes: 1 addition & 4 deletions include/nuttx/sensors/mpu9250.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2b7a6d1

Please sign in to comment.