关于位域

在s3c2410的时钟和电源管理模块中有一个寄存器MPLLCON为MPLL锁相环的配置寄存器:,以下为其中的一些位定义:
MDIV [19:12]   Main divider control
PDIV  [9:4]       Pre-divider control  
SDIV  [1:0]       Post divider control
这里就涉及到如何从 MPLLCON中获取这三个值的问题,一种简单的办法是利用掩码,比如要获取MDIV的值:
        #define MDIV_MASK    0x00ff0000
        MDIV=MPLLCON & MDIV_MASK
               MDIV=MDIV >>12
在linux内核和vivi bootloader中使用了另外一种通用方法,那就是位域。

请看下面这个获取CPU时钟的函数:

unsigned  long s3c2410_get_cpu_clk(void)
{
    unsigned long val=MPLLCON;
    return(    (   (GET_MDIV(val)+8) *FIN    )/  (   ( GET_PDIV(val)+2 )  *   (  1<<GET_SDIV(val)  )     )    );
                
}
以下为宏定义:
#define GET_MDIV(x)        FExtr(x, fMPLL_MDIV)
#define GET_PDIV(x)        FExtr(x, fMPLL_PDIV)
#define GET_SDIV(x)        FExtr(x, fMPLL_SDIV)
#define fMPLL_MDIV        Fld(8,12)
#define fMPLL_PDIV           Fld(6,4)
#define fMPLL_SDIV            Fld(2,0)
其中的宏定义在一个处理位域的头文件中:
/*
*    FILE        bitfield.h
*
*    Version     1.1
*    Author      Copyright (c) Marc A. Viredaz, 1998
*                DEC Western Research Laboratory, Palo Alto, CA
*    Date        April 1998 (April 1997)
*    System      Advanced RISC Machine (ARM)
*    Language    C or ARM Assembly
*    Purpose     Definition of macros to operate on bit fields.
*/



#ifndef __BITFIELD_H
#define __BITFIELD_H

#ifndef __ASSEMBLY__
#define UData(Data)    ((unsigned long) (Data))
#else
#define UData(Data)    (Data)
#endif


/*
* MACRO: Fld
*
* Purpose
*    The macro "Fld" encodes a bit field, given its size and its shift value
*    with respect to bit 0.
*
* Note
*    A more intuitive way to encode bit fields would have been to use their
*    mask. However, extracting size and shift value information from a bit
*    field's mask is cumbersome and might break the assembler (255-character
*    line-size limit).
*
* Input
*    Size          Size of the bit field, in number of bits.
*    Shft          Shift value of the bit field with respect to bit 0.
*
* Output
*    Fld           Encoded bit field.
*/

#define Fld(Size, Shft)    (((Size) << 16) + (Shft))


/*
* MACROS: FSize, FShft, FMsk, FAlnMsk, F1stBit
*
* Purpose
*    The macros "FSize", "FShft", "FMsk", "FAlnMsk", and "F1stBit" return
*    the size, shift value, mask, aligned mask, and first bit of a
*    bit field.
*
* Input
*    Field         Encoded bit field (using the macro "Fld").
*
* Output
*    FSize         Size of the bit field, in number of bits.
*    FShft         Shift value of the bit field with respect to bit 0.
*    FMsk          Mask for the bit field.
*    FAlnMsk       Mask for the bit field, aligned on bit 0.
*    F1stBit       First bit of the bit field.
*/

#define FSize(Field)    ((Field) >> 16)
#define FShft(Field)    ((Field) & 0x0000FFFF)
#define FMsk(Field)    (((UData (1) << FSize (Field)) - 1) << FShft (Field))
#define FAlnMsk(Field)    ((UData (1) << FSize (Field)) - 1)
#define F1stBit(Field)    (UData (1) << FShft (Field))


/*
* MACRO: FInsrt
*
* Purpose
*    The macro "FInsrt" inserts a value into a bit field by shifting the
*    former appropriately.
*
* Input
*    Value         Bit-field value.
*    Field         Encoded bit field (using the macro "Fld").
*
* Output
*    FInsrt        Bit-field value positioned appropriately.
*/

#define FInsrt(Value, Field) \
                    (UData (Value) << FShft (Field))


/*
* MACRO: FExtr
*
* Purpose
*    The macro "FExtr" extracts the value of a bit field by masking and
*    shifting it appropriately.
*
* Input
*    Data          Data containing the bit-field to be extracted.
*    Field         Encoded bit field (using the macro "Fld").
*
* Output
*    FExtr         Bit-field value.
*/

#define FExtr(Data, Field) \
                    ((UData (Data) >> FShft (Field)) & FAlnMsk (Field))


#endif /* __BITFIELD_H */

 呵呵,位域果然强大,当初看《Programming In C》 时提到了位域,还专门用了一章来讲,现在终于明白为什么了。
以下以通过GET_MDIV(MPLLCON)获取MDIV为例,简要说明一下位域的处理过程:
#define  MPLLCON       0X4C000004
           x= (*(volatile unsigned long *)(MPLLCON));    x 取MPLLCON中的值
          ;此例中假设MPLLCON的值为0x0005C080

        #define GET_MDIV(x)        FExtr(x, fMPLL_MDIV)
      ==>          GET_MDIV(x) = FExtr(x,fMPLL_MDIV)
       #define fMPLL_MDIV        Fld(8,12)
         ==>        FExtr(x,fMPLL_MDIV)=FExtr(x,Fld(8,12) )
    
    #define Fld(Size, Shft)    (((Size) << 16) + (Shft))
      ==>    Fld(8,12)   = 0x0008000C
    
#define FExtr(Data, Field)                    ((UData (Data) >> FShft (Field)) & FAlnMsk (Field))
       ==>   FExtr(x, 0x0008000C)= (0x0005C080>>FShft(0x0008000C))&FAlnMsk(0x0008000C)

     #define FShft(Field)    ((Field) & 0x0000FFFF)
         ==>  Fshft(0x0008000C)=  0x0000000C=12
      #define FAlnMsk(Field)    ((UData (1) << FSize (Field)) - 1)
        ==>         FAlnMsk(0x0008000C)= ( 0x00000001<<FSize(0x0008000C) )  - 1
        #define    FSize(Field)    ((Field) >> 16)        
           ==> FSize(0x0008000C)= 0x00000008
       ==>     FAlnMsk(0x0008000C)=0x000000FF

       ==>FExtr(x, 0x0008000C)=(0x0005C080>>12)&0x000000FF
                                               =0x0000005C
正确的提取出了结果
研究具体实现细节比较累,但是使用起来确是很方便的,包含bitifield.h头文件,然后就可以使用预先定义好的良好的宏了,就像上面所做的那样。
下面是我对要使用这种通用方法的原因的理解:
              通用性,避免重复输入,避免手动移位,计算掩码等操作时因粗心引入错误。使用这种方法只需提供  位域的位数和偏移值,比较方便。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值