⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bitctrl.h

📁 ARM9基于WINDOWSCE的BSP源代码
💻 H
字号:
/******************************************************************************/
/* Copyright (C), 2003-2004, Shanghai Jade Technologies Co., Ltd.             */
/* File name: bitctrl.h                                                       */
/* Description:                                                               */
/*      bit control head.                                                     */
/* Version: Original version.                                                 */
/* History:                                                                   */
/*   <author>        <time>      <version>       <desc>                       */
/*                                                                            */
/*    Wang Wei      08/19/04        1.0         original  version             */                         
/******************************************************************************/


#ifndef _BITCTRL_H
#define _BITCTRL_H

typedef unsigned long      UWORD32;
typedef unsigned long           ULONG;
/*****************************
  One example of Usage                      
*****************************/
/*
for example,you can define:

    #define bwCLCD_TIMING2_CPL    10
    #define bsCLCD_TIMING2_CPL    16

they are respectively bit width and bit left shift.(bit 16-25)
   then:

    apBIT_MASK(CLCD_TIMING2_CPL);  //get mask
or
    apBIT_SET(tim2,CLCD_TIMING2_CPL,7); //set const 7 to CPL(bit 16-25),and store to value tim2

*/


// This macro is to create a mask with bw* wide and position at bs* bit left shift.
// for example, if bw* is 4, bs* is 8, then the result will be 0x00000F00
#define apBIT_MASK(__bws) ((ULONG)(((bw ## __bws)==32)?0xFFFFFFFF:\
        ((1U << (bw ## __bws)) - 1)) << (bs ## __bws))
        
// Before the "|" this macro clear specified bits. After the "|" the macro shift the value 
//specified bits.then clear other bits to 0. 
//Together the macro set the__datum with value __val by specified bit width and shift of __bws.
//for example,__datum is 0xFFFFFFFF,bw* is 4 (width),bs* is 4 (shift),val is 0,
//then __datum will be 0xFFFFFF0F
#define apBIT_SET(__datum, __bws, __val) ((__datum) = ((ULONG) \
        (__datum) & (ULONG)~(apBIT_MASK(__bws))) | \
        ((ULONG) ((ULONG)(__val) << (ULONG)(bs ## __bws)) & \
        (apBIT_MASK(__bws))))

/*
 * Description:
 * Shift the specified value into the desired bits
 *
 * Implementation:
 * __bws - a width/shift pair.
 * __val - the data value to be shifted into the specified bits.
 * 
 * Returns  The value shifted to the specified offset
 */
#define apBIT_BUILD(__bws, __val) ((UWORD32)(((UWORD32)(__val) << (UWORD32)(bs ## __bws)) & ((UWORD32) apBIT_MASK(__bws))))

//Get specified bits(width,shift) from __datum 
#define apBIT_GET(__datum, __bws) (ULONG) \
       ( ((ULONG)(__datum)& (apBIT_MASK(__bws)))>>(ULONG)(bs ## __bws))

//compare specified bits(width,shift) from __datum with __val
#define apBIT_CMP(__datum,__bws,__val) \
        (ULONG)(__val)==apBIT_GET(__datum,__bws)? (ULONG)1 : (ULONG)0

/*
 * Description:
 * Equivalent to apBIT_MASK, but using separate width and 
 * shift parameters.
 *
 */
#define apBIT_MASK_FIELD(__bw, __bs) ((UWORD32)((((__bw)==32)?0xFFFFFFFF:(UWORD32)((UWORD32)1U << (__bw)) - 1)) << (__bs))

/*
 * Description:
 * Equivalent to apBIT_CLEAR, but using separate width and 
 * shift parameters.
 */
#define apBIT_CLEAR_FIELD(__datum, __bw, __bs) ((__datum) = ((__datum) & ~((UWORD32) apBIT_MASK_FIELD(__bw,__bs))))

/*
 * Description:
 * Equivalent to apBIT_GET, but using separate width and 
 * shift parameters.
 */
#define apBIT_GET_FIELD(__datum, __bw, __bs) ((UWORD32)(((__datum) & ((UWORD32) apBIT_MASK_FIELD(__bw,__bs))) >> (__bs)))

/*
 * Description:
 * Equivalent to apBIT_SET, but using separate width and 
 * shift parameters.
 */
#define apBIT_SET_FIELD(__datum, __bw, __bs, __val) ((__datum) = ((__datum) & ~((UWORD32) apBIT_MASK_FIELD(__bw,__bs))) | \
                                                    ((UWORD32) ((UWORD32)(__val) << (__bs)) & ((UWORD32) apBIT_MASK_FIELD(__bw,__bs))))
// Bit control macros (sigle bit)
#define HW_REG(base,offset) *((volatile unsigned long *)((base) + (offset)))
#define EXTRACT_BIT(arg, bit) 	((arg >> (bit)) & 1)
#define SET_BIT(arg, bit)       ((arg) |= (1 << (bit)))
#define CLEAR_BIT(arg, bit)     ((arg) &= ~(1 << (bit)))
#define TEST_BIT(arg, bit)		((arg) & (1 << (bit)))

// Bit operation macro for regsters
#define READ_REG_ULONG(reg) \
           (*(volatile unsigned long * const)(reg))
#define WRITE_REG_ULONG(reg, val) \
           (*(volatile unsigned long * const)(reg)) = (val)
#define READ_REG_USHORT(reg) \
           (*(volatile unsigned short * const)(reg))
#define WRITE_REG_USHORT(reg, val) \
           (*(volatile unsigned short * const)(reg)) = (val)
#define READ_REG_UCHAR(reg) \
           (*(volatile unsigned char * const)(reg))
#define WRITE_REG_UCHAR(reg, val) \
           (*(volatile unsigned char * const)(reg)) = (val)
           
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -