在路上...

The development of life
我们一直都在努力,有您的支持,将走得更远...

站内搜索: Google

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

一、GPIO寄存器定义
1.#define GPCON(x)  __REG2(0x56000000, (x) * 0x10)
这句是定义2410的GPIO的控制寄存器,注意:__REG2的参数是寄存器的物理地址,这个物理地址经_REG2宏转换为虚拟地址,对照2410的手册可以得到一下对应关系:
GPCON(1) ------ PORT A  0x56000000
GPCON(2) ------ PORT B  0x56000010
GPCON(3) ------ PORT C  0x56000020
  .          .          .        .
  .          .          .        .
  .          .          .        .
GPCON(8) ------ PORT H  0x56000070
2.#define GPDAT(x)  __REG2(0x56000004, (x) * 0x10)
这句是定义2410的GPIO的数据寄存器,定义方法同GPCON宏。
GPDAT(1) ------ PORT A  0x56000004
GPDAT(2) ------ PORT B  0x56000014
GPDAT(3) ------ PORT C  0x56000024
  .          .          .        .
  .          .          .        .
  .          .          .        .
GPDAT(8) ------ PORT H  0x56000074
3.#define GPUP(x)      __REG2(0x56000008, (x) * 0x10)
这句是定义2410的GPIO的上拉电阻屏蔽/激活寄存器,定义方法同GPCON宏。
GPUP(1) ------ PORT A  0x56000008
GPUP(2) ------ PORT B  0x56000018
GPUP(3) ------ PORT C  0x56000028
  .          .          .        .
  .          .          .        .
  .          .          .        .
GPUP(8) ------ PORT H  0x56000078
二、GPIO端口号定义
以GPIO_G12来说明在内核头文件$(KERNEL_INCLUDE)/asm-arm/arch/s3c2410.h中是如何来定义IO port的端口号的。定义GPIO端口主要涉及到以下几个宏:
#define MAKE_GPIO_NUM(p, o) ((p << GPIO_PORT_SHIFTT) | (o << GPIO_OFS_SHIFT))

#define GPIO_G12              MAKE_GPIO_NUM(PORTG_OFS, 12)

GPIO_PORT_SHIFTT值为8,代表GPIO组号在整个GPIO端口号(如GPIO_G12)字段中的位移
GPIO_OFS_SHIFT值为0,代表GPIO组内偏移号在整个GPIO端口号(如GPIO_G12)字段中的位移
  s3c2410有117个多功能input/output port pins。分为以下八组:
— Port A (GPA): 23-output port                    #define PORTA_OFS                0
— Port B (GPB): 11-input/output port              #define PORTB_OFS                1
— Port C (GPC): 16-input/output port              #define PORTC_OFS            2
— Port D (GPD): 16-input/output port              #define PORTD_OFS                3
— Port E (GPE): 16-input/output port                #define PORTE_OFS          4
— Port F (GPF): 8-input/output port                #define PORTF_OFS          5
— Port G (GPG): 16-input/output port              #define PORTG_OFS            6
— Port H (GPH): 11-input/output port                #define PORTH_OFS          7

GPG12属于G组,组内偏移为12,从上述两个宏定义中,我们可以很清楚地看出GPIO_G12结构:


                    图1  GPIO端口号结构图
端口一共有8组,从上面的宏定义可以看出,端口组号p的范围:0~7。而组内偏移各组不尽相同,Port A有23个输出口,因此它的组内偏移o为0~22,Port G有16个IO口,它的组内偏移o为0~15,其他组的GPIO以此类推。
三、write_gpio_bit(x,v)宏分析
write_gpio_bit宏传入两个参数,第一个为GPIO端口号,如GPIO_G12;第二个参数为1或0,为相应IO口设置高电平或低电平输出。具体宏展开如下:
#define write_gpio_bit(x, v) \
                            ({  \
GPDAT(GRAB_PORT((x))) &= ~(0x1 x))); \
                              GPDAT(GRAB_PORT((x))) |= ((v) x))); \
})
GRAB_PORT宏的参数是GPIO端口号,功能是从GPIO端口号中解析出组号,具体定义如下:
#define GRAB_PORT(x)              (((x) & GPIO_PORT_MASK) >> GPIO_PORT_SHIFTT)
其中GPIO_PORT_MASK是组号的掩码,值为0x0000ff00,从图1中也可看出。
GRAB_OFS宏和GRAB_PORT类似,它的功能是从GPIO端口号中解析出组内偏移:
#define GRAB_OFS(x)            (((x) & GPIO_OFS_MASK) >> GPIO_OFS_SHIFT)
其中偏移值掩码GPIO_OFS_MASK=0x000000ff。
现在我们结合上述说明来分析write_gpio_bit(GPIO_G12,1)这条语句:由GPIO_G12的宏定义可计算出其值为0x0000060C,GRAB_PORT(GPIO_G12)解析得到所操作的IO属于G组,组号为6;GRAB_OFS(GPIO_G12)解析得到此IO口为G组的第12个引脚(从0开始算起),为GPG12,表达式值为12。则write_gpio_bit(GPIO_G12,1)等价于下面两条语句:
GPDAT(6) &= ~(0x112);  //GPGDAT寄存器第12位清零
GPDAT(6) | = 112;      // 向GPGDAT寄存器第12位写入‘1’
到此,我们知道了write_gpio_bit(GPIO_G12,1)这条语句是将GPG12这个引脚拉成高电平。
四、set_gpio_ctrl(x)宏分析
#define set_gpio_ctrl(x) \
      ({ GPCON(GRAB_PORT((x))) &= ~(0x3
          GPCON(GRAB_PORT(x)) |= (GRAB_MODE(x)
          GPUP(GRAB_PORT((x))) &= ~(1
          GPUP(GRAB_PORT((x))) |= (GRAB_PULLUP((x))
完成了对write_gpio_bit宏的分析,现在来看set_gpio_ctrl就很简单了!在它的宏展开中只多了GRAB_MODE(x)和 GRAB_PULLUP(x)分别表示从参数x中解析出IO口的模式和使能/屏蔽此端口的上拉电阻。值得注意的是set_gpio_ctrl的参数x不仅仅表示GPIO端口号,其高16位还带有模式状态和上拉电阻控制信息,参数x的结构如下图:

 

图2 set_gpio_ctrl的参数字段结构图

    低16位即为前面所述的GPIO的端口号,高16位中的R字段用来屏蔽/使能IO口的上拉电阻功能。R=0,上拉电阻使能;R=1,上拉电阻失效。M字段用来设置IO口的工作模式,M=0,IO口为输入端口;M=1,IO口为输出端口;M=2,可选功能1;M=3,可选功能2。
set_gpio_ctrl宏就是通过写相应GPIO所在组的GPXCON(X为A~H)的相应位来设置IO口模式(GPACON每一个位控制一个IO口,而GPBCON~GPHCON都是两个位控制一个IO口的模式),通过写GPXUP(X为A~H)来决定是否启用上拉电阻。典型的set_gpio_ctrl调用方式如下:
set_gpio_ctrl(GPIO_MODE_OUT | GPIO_PULLUP_DIS | GPIO_G12);
这条语句是将GPG12设置成输出模式,并且不使用端口的上拉电阻。
五、结束
以上主要结合《S3C2410X 32-BIT RISC MICROPROCESSOR USER'S MANUAL》分析了$(LINUX_KERNEL_INCLUDE)/asm-arm/arch/s3c2410.h中所定义的对2410GPIO进行操作的几个宏,除了文中提及的几个宏,除此还有read_gpio_bit(x)、read_gpio_reg(x) 、write_gpio_reg(x, v)等,实现方法和上述类似,在此不再一一赘述!
///////////////////////////////////////////////
view plaincopy to clipboardprint?
#define GPCON(x)    __REG2(0x56000000, (x) * 0x10)  
#define GPDAT(x)    __REG2(0x56000004, (x) * 0x10)  
#define GPUP(x)         __REG2(0x56000008, (x) * 0x10)  
 
#define GPIO_OFS_SHIFT      0  
#define GPIO_PORT_SHIFTT    8  
#define GPIO_PULLUP_SHIFT   16   
#define GPIO_MODE_SHIFT     24  
#define GPIO_OFS_MASK       0x000000ff  
#define GPIO_PORT_MASK      0x0000ff00  
#define GPIO_PULLUP_MASK    0x00ff0000  
#define GPIO_MODE_MASK      0xff000000  
#define GPIO_MODE_IN        (0 << GPIO_MODE_SHIFT)  
#define GPIO_MODE_OUT       (1 << GPIO_MODE_SHIFT)  
#define GPIO_MODE_ALT0      (2 << GPIO_MODE_SHIFT)  
#define GPIO_MODE_ALT1      (3 << GPIO_MODE_SHIFT)  
#define GPIO_PULLUP_EN      (0 << GPIO_PULLUP_SHIFT)  
#define GPIO_PULLUP_DIS     (1 << GPIO_PULLUP_SHIFT)   
 
#define PORTA_OFS       0  
#define PORTB_OFS       1  
#define PORTC_OFS       2  
#define PORTD_OFS       3  
#define PORTE_OFS       4  
#define PORTF_OFS       5  
#define PORTG_OFS       6  
#define PORTH_OFS       7  
 
#define MAKE_GPIO_NUM(p, o) ((p << GPIO_PORT_SHIFTT) | (o << GPIO_OFS_SHIFT))  
 
#define GRAB_MODE(x)        (((x) & GPIO_MODE_MASK) >> GPIO_MODE_SHIFT)  
#define GRAB_PULLUP(x)      (((x) & GPIO_PULLUP_MASK) >> GPIO_PULLUP_SHIFT)  
#define GRAB_PORT(x)        (((x) & GPIO_PORT_MASK) >> GPIO_PORT_SHIFTT)  
#define GRAB_OFS(x)     (((x) & GPIO_OFS_MASK) >> GPIO_OFS_SHIFT)  
 
#define set_gpio_ctrl(x) \  
    ({ GPCON(GRAB_PORT((x))) &= ~(0x3 << (GRAB_OFS((x))*2)); \  
       GPCON(GRAB_PORT(x)) |= (GRAB_MODE(x) << (GRAB_OFS((x))*2)); \  
       GPUP(GRAB_PORT((x))) &= ~(1 << GRAB_OFS((x))); \  
       GPUP(GRAB_PORT((x))) |= (GRAB_PULLUP((x)) << GRAB_OFS((x))); })  
#define set_gpio_pullup(x) \  
    ({ GPUP(GRAB_PORT((x))) &= ~(1 << GRAB_OFS((x))); \  
       GPUP(GRAB_PORT((x))) |= (GRAB_PULLUP((x)) << GRAB_OFS((x))); })  
#define set_gpio_pullup_user(x, v) \  
    ({ GPUP(GRAB_PORT((x))) &= ~(1 << GRAB_OFS((x))); \  
       GPUP(GRAB_PORT((x))) |= ((v) << GRAB_OFS((x))); })  
#define set_gpio_mode(x) \  
    ({ GPCON(GRAB_PORT((x))) &= ~(0x3 << (GRAB_OFS((x))*2)); \  
       GPCON(GRAB_PORT((x))) |= (GRAB_MODE((x)) << (GRAB_OFS((x))*2)); })  
#define set_gpio_mode_user(x, v) \  
    ({ GPCON(GRAB_PORT((x))) &= ~(0x3 << (GRAB_OFS((x))*2)); \  
       GPCON(GRAB_PORT((x))) |= ((v) << (GRAB_OFS((x))*2)); })  
#define set_gpioA_mode(x) \  
    ({ GPCON(GRAB_PORT((x))) &= ~(0x1 << GRAB_OFS((x))); \  
       GPCON(GRAB_PORT((x))) |= (GRAB_MODE((x)) << GRAB_OFS((x))); })  
#define read_gpio_bit(x)    ((GPDAT(GRAB_PORT((x))) & (1<<GRAB_OFS((x)))) >> GRAB_OFS((x)))  
#define read_gpio_reg(x)    (GPDAT(GRAB_PORT((x)))  
#define write_gpio_bit(x, v) \  
    ({ GPDAT(GRAB_PORT((x))) &= ~(0x1 << GRAB_OFS((x))); \  
       GPDAT(GRAB_PORT((x))) |= ((v) << GRAB_OFS((x))); })  
#define write_gpio_reg(x, v)    (GPDAT(GRAB_PORT((x))) = (v))  
      
 
#define GPIO_A0             MAKE_GPIO_NUM(PORTA_OFS, 0)  
#define GPIO_A1             MAKE_GPIO_NUM(PORTA_OFS, 1)  
#define GPIO_A2             MAKE_GPIO_NUM(PORTA_OFS, 2)  
#define GPIO_A3             MAKE_GPIO_NUM(PORTA_OFS, 3)  
#define GPIO_A4             MAKE_GPIO_NUM(PORTA_OFS, 4)  
#define GPIO_A5             MAKE_GPIO_NUM(PORTA_OFS, 5)  
#define GPIO_A6             MAKE_GPIO_NUM(PORTA_OFS, 6)  
#define GPIO_A7             MAKE_GPIO_NUM(PORTA_OFS, 7)  
#define GPIO_A8             MAKE_GPIO_NUM(PORTA_OFS, 8)  
#define GPIO_A9             MAKE_GPIO_NUM(PORTA_OFS, 9)  
#define GPIO_A10            MAKE_GPIO_NUM(PORTA_OFS, 10)  
#define GPIO_A11            MAKE_GPIO_NUM(PORTA_OFS, 11)  
#define GPIO_A12            MAKE_GPIO_NUM(PORTA_OFS, 12)  
#define GPIO_A13            MAKE_GPIO_NUM(PORTA_OFS, 13)  
#define GPIO_A14            MAKE_GPIO_NUM(PORTA_OFS, 14)  
#define GPIO_A15            MAKE_GPIO_NUM(PORTA_OFS, 15)  
#define GPIO_A16            MAKE_GPIO_NUM(PORTA_OFS, 16)  
#define GPIO_A17            MAKE_GPIO_NUM(PORTA_OFS, 17)  
#define GPIO_A18            MAKE_GPIO_NUM(PORTA_OFS, 18)  
#define GPIO_A19            MAKE_GPIO_NUM(PORTA_OFS, 19)  
#define GPIO_A20            MAKE_GPIO_NUM(PORTA_OFS, 20)  
#define GPIO_A21            MAKE_GPIO_NUM(PORTA_OFS, 21)  
#define GPIO_A22            MAKE_GPIO_NUM(PORTA_OFS, 22)  
 
#define GPIO_B0             MAKE_GPIO_NUM(PORTB_OFS, 0)  
#define GPIO_B1             MAKE_GPIO_NUM(PORTB_OFS, 1)  
#define GPIO_B2             MAKE_GPIO_NUM(PORTB_OFS, 2)  
#define GPIO_B3             MAKE_GPIO_NUM(PORTB_OFS, 3)  
#define GPIO_B4             MAKE_GPIO_NUM(PORTB_OFS, 4)  
#define GPIO_B5             MAKE_GPIO_NUM(PORTB_OFS, 5)  
#define GPIO_B6             MAKE_GPIO_NUM(PORTB_OFS, 6)  
#define GPIO_B7             MAKE_GPIO_NUM(PORTB_OFS, 7)  
#define GPIO_B8             MAKE_GPIO_NUM(PORTB_OFS, 8)  
#define GPIO_B9             MAKE_GPIO_NUM(PORTB_OFS, 9)  
#define GPIO_B10            MAKE_GPIO_NUM(PORTB_OFS, 10)  
 
#define GPIO_C0             MAKE_GPIO_NUM(PORTC_OFS, 0)  
#define GPIO_C1             MAKE_GPIO_NUM(PORTC_OFS, 1)  
#define GPIO_C2             MAKE_GPIO_NUM(PORTC_OFS, 2)  
#define GPIO_C3             MAKE_GPIO_NUM(PORTC_OFS, 3)  
#define GPIO_C4             MAKE_GPIO_NUM(PORTC_OFS, 4)  
#define GPIO_C5             MAKE_GPIO_NUM(PORTC_OFS, 5)  
#define GPIO_C6             MAKE_GPIO_NUM(PORTC_OFS, 6)  
#define GPIO_C7             MAKE_GPIO_NUM(PORTC_OFS, 7)  
#define GPIO_C8             MAKE_GPIO_NUM(PORTC_OFS, 8)  
#define GPIO_C9             MAKE_GPIO_NUM(PORTC_OFS, 9)  
#define GPIO_C10            MAKE_GPIO_NUM(PORTC_OFS, 10)  
#define GPIO_C11            MAKE_GPIO_NUM(PORTC_OFS, 11)  
#define GPIO_C12            MAKE_GPIO_NUM(PORTC_OFS, 12)  
#define GPIO_C13            MAKE_GPIO_NUM(PORTC_OFS, 13)  
#define GPIO_C14            MAKE_GPIO_NUM(PORTC_OFS, 14)  
#define GPIO_C15            MAKE_GPIO_NUM(PORTC_OFS, 15)  
 
#define GPIO_D0             MAKE_GPIO_NUM(PORTD_OFS, 0)  
#define GPIO_D1             MAKE_GPIO_NUM(PORTD_OFS, 1)  
#define GPIO_D2             MAKE_GPIO_NUM(PORTD_OFS, 2)  
#define GPIO_D3             MAKE_GPIO_NUM(PORTD_OFS, 3)  
#define GPIO_D4             MAKE_GPIO_NUM(PORTD_OFS, 4)  
#define GPIO_D5             MAKE_GPIO_NUM(PORTD_OFS, 5)  
#define GPIO_D6             MAKE_GPIO_NUM(PORTD_OFS, 6)  
#define GPIO_D7             MAKE_GPIO_NUM(PORTD_OFS, 7)  
#define GPIO_D8             MAKE_GPIO_NUM(PORTD_OFS, 8)  
#define GPIO_D9             MAKE_GPIO_NUM(PORTD_OFS, 9)  
#define GPIO_D10            MAKE_GPIO_NUM(PORTD_OFS, 10)  
#define GPIO_D11            MAKE_GPIO_NUM(PORTD_OFS, 11)  
#define GPIO_D12            MAKE_GPIO_NUM(PORTD_OFS, 12)  
#define GPIO_D13            MAKE_GPIO_NUM(PORTD_OFS, 13)  
#define GPIO_D14            MAKE_GPIO_NUM(PORTD_OFS, 14)  
#define GPIO_D15            MAKE_GPIO_NUM(PORTD_OFS, 15)  
 
#define GPIO_E0             MAKE_GPIO_NUM(PORTE_OFS, 0)  
#define GPIO_E1             MAKE_GPIO_NUM(PORTE_OFS, 1)  
#define GPIO_E2             MAKE_GPIO_NUM(PORTE_OFS, 2)  
#define GPIO_E3             MAKE_GPIO_NUM(PORTE_OFS, 3)  
#define GPIO_E4             MAKE_GPIO_NUM(PORTE_OFS, 4)  
#define GPIO_E5             MAKE_GPIO_NUM(PORTE_OFS, 5)  
#define GPIO_E6             MAKE_GPIO_NUM(PORTE_OFS, 6)  
#define GPIO_E7             MAKE_GPIO_NUM(PORTE_OFS, 7)  
#define GPIO_E8             MAKE_GPIO_NUM(PORTE_OFS, 8)  
#define GPIO_E9             MAKE_GPIO_NUM(PORTE_OFS, 9)  
#define GPIO_E10            MAKE_GPIO_NUM(PORTE_OFS, 10)  
#define GPIO_E11            MAKE_GPIO_NUM(PORTE_OFS, 11)  
#define GPIO_E12            MAKE_GPIO_NUM(PORTE_OFS, 12)  
#define GPIO_E13            MAKE_GPIO_NUM(PORTE_OFS, 13)  
#define GPIO_E14            MAKE_GPIO_NUM(PORTE_OFS, 14)  
#define GPIO_E15            MAKE_GPIO_NUM(PORTE_OFS, 15)  
 
#define GPIO_F0             MAKE_GPIO_NUM(PORTF_OFS, 0)  
#define GPIO_F1             MAKE_GPIO_NUM(PORTF_OFS, 1)  
#define GPIO_F2             MAKE_GPIO_NUM(PORTF_OFS, 2)  
#define GPIO_F3             MAKE_GPIO_NUM(PORTF_OFS, 3)  
#define GPIO_F4             MAKE_GPIO_NUM(PORTF_OFS, 4)  
#define GPIO_F5             MAKE_GPIO_NUM(PORTF_OFS, 5)  
#define GPIO_F6             MAKE_GPIO_NUM(PORTF_OFS, 6)  
#define GPIO_F7             MAKE_GPIO_NUM(PORTF_OFS, 7)  
 
#define GPIO_G0             MAKE_GPIO_NUM(PORTG_OFS, 0)  
#define GPIO_G1             MAKE_GPIO_NUM(PORTG_OFS, 1)  
#define GPIO_G2             MAKE_GPIO_NUM(PORTG_OFS, 2)  
#define GPIO_G3             MAKE_GPIO_NUM(PORTG_OFS, 3)  
#define GPIO_G4             MAKE_GPIO_NUM(PORTG_OFS, 4)  
#define GPIO_G5             MAKE_GPIO_NUM(PORTG_OFS, 5)  
#define GPIO_G6             MAKE_GPIO_NUM(PORTG_OFS, 6)  
#define GPIO_G7             MAKE_GPIO_NUM(PORTG_OFS, 7)  
#define GPIO_G8             MAKE_GPIO_NUM(PORTG_OFS, 8)  
#define GPIO_G9             MAKE_GPIO_NUM(PORTG_OFS, 9)  
#define GPIO_G10            MAKE_GPIO_NUM(PORTG_OFS, 10)  
#define GPIO_G11            MAKE_GPIO_NUM(PORTG_OFS, 11)  
#define GPIO_G12            MAKE_GPIO_NUM(PORTG_OFS, 12)  
#define GPIO_G13            MAKE_GPIO_NUM(PORTG_OFS, 13)  
#define GPIO_G14            MAKE_GPIO_NUM(PORTG_OFS, 14)  
#define GPIO_G15            MAKE_GPIO_NUM(PORTG_OFS, 15)  
 
#define GPIO_H0             MAKE_GPIO_NUM(PORTH_OFS, 0)  
#define GPIO_H1             MAKE_GPIO_NUM(PORTH_OFS, 1)  
#define GPIO_H2             MAKE_GPIO_NUM(PORTH_OFS, 2)  
#define GPIO_H3             MAKE_GPIO_NUM(PORTH_OFS, 3)  
#define GPIO_H4             MAKE_GPIO_NUM(PORTH_OFS, 4)  
#define GPIO_H5             MAKE_GPIO_NUM(PORTH_OFS, 5)  
#define GPIO_H6             MAKE_GPIO_NUM(PORTH_OFS, 6)  
#define GPIO_H7             MAKE_GPIO_NUM(PORTH_OFS, 7)  
#define GPIO_H8             MAKE_GPIO_NUM(PORTH_OFS, 8)  
#define GPIO_H9             MAKE_GPIO_NUM(PORTH_OFS, 9)  
#define GPIO_H10            MAKE_GPIO_NUM(PORTH_OFS, 10)  
 
#define GPIO_MODE_TOUT          GPIO_MODE_ALT0  
#define GPIO_MODE_nXBACK        GPIO_MODE_ALT0  
#define GPIO_MODE_nXBREQ        GPIO_MODE_ALT0  
#define GPIO_MODE_nXDACK        GPIO_MODE_ALT0  
#define GPIO_MODE_nXDREQ        GPIO_MODE_ALT0  
#define GPIO_MODE_LEND          GPIO_MODE_ALT0  
#define GPIO_MODE_VCLK          GPIO_MODE_ALT0  
#define GPIO_MODE_VLINE         GPIO_MODE_ALT0  
#define GPIO_MODE_VFRAME        GPIO_MODE_ALT0  
#define GPIO_MODE_VM            GPIO_MODE_ALT0  
#define GPIO_MODE_LCDVF         GPIO_MODE_ALT0  
#define GPIO_MODE_VD            GPIO_MODE_ALT0  
#define GPIO_MODE_IICSDA        GPIO_MODE_ALT0  
#define GPIO_MODE_IICSCL        GPIO_MODE_ALT0  
#define GPIO_MODE_SPICLK        GPIO_MODE_ALT0  
#define GPIO_MODE_SPIMOSI       GPIO_MODE_ALT0  
#define GPIO_MODE_SPIMISO       GPIO_MODE_ALT0  
#define GPIO_MODE_SDDAT         GPIO_MODE_ALT0  
#define GPIO_MODE_SDCMD         GPIO_MODE_ALT0  
#define GPIO_MODE_SDCLK         GPIO_MODE_ALT0  
#define GPIO_MODE_I2SSDO        GPIO_MODE_ALT0  
#define GPIO_MODE_I2SSDI        GPIO_MODE_ALT0  
#define GPIO_MODE_CDCLK         GPIO_MODE_ALT0  
#define GPIO_MODE_I2SSCLK       GPIO_MODE_ALT0  
#define GPIO_MODE_I2SLRCK       GPIO_MODE_ALT0  
#define GPIO_MODE_I2SSDI_ABNORMAL   GPIO_MODE_ALT1  
#define GPIO_MODE_nSS           GPIO_MODE_ALT1  
#define GPIO_MODE_EINT          GPIO_MODE_ALT0  
#define GPIO_MODE_nYPON         GPIO_MODE_ALT1  
#define GPIO_MODE_YMON          GPIO_MODE_ALT1  
#define GPIO_MODE_nXPON         GPIO_MODE_ALT1  
#define GPIO_MODE_XMON          GPIO_MODE_ALT1  
#define GPIO_MODE_UART          GPIO_MODE_ALT0    
#define GPIO_MODE_TCLK_ABNORMAL     GPIO_MODE_ALT1  
#define GPIO_MODE_SPICLK_ABNORMAL   GPIO_MODE_ALT1  
#define GPIO_MODE_SPIMOSI_ABNORMAL  GPIO_MODE_ALT1  
#define GPIO_MODE_SPIMISO_ABNORMAL  GPIO_MODE_ALT1  
#define GPIO_MODE_LCD_PWRDN     GPIO_MODE_ALT1 
#define GPCON(x) __REG2(0x56000000, (x) * 0x10)
#define GPDAT(x) __REG2(0x56000004, (x) * 0x10)
#define GPUP(x)         __REG2(0x56000008, (x) * 0x10)

#define GPIO_OFS_SHIFT  0
#define GPIO_PORT_SHIFTT 8
#define GPIO_PULLUP_SHIFT 16
#define GPIO_MODE_SHIFT  24
#define GPIO_OFS_MASK  0x000000ff
#define GPIO_PORT_MASK  0x0000ff00
#define GPIO_PULLUP_MASK 0x00ff0000
#define GPIO_MODE_MASK  0xff000000
#define GPIO_MODE_IN  (0 << GPIO_MODE_SHIFT)
#define GPIO_MODE_OUT  (1 << GPIO_MODE_SHIFT)
#define GPIO_MODE_ALT0  (2 << GPIO_MODE_SHIFT)
#define GPIO_MODE_ALT1  (3 << GPIO_MODE_SHIFT)
#define GPIO_PULLUP_EN  (0 << GPIO_PULLUP_SHIFT)
#define GPIO_PULLUP_DIS  (1 << GPIO_PULLUP_SHIFT)

#define PORTA_OFS  0
#define PORTB_OFS  1
#define PORTC_OFS  2
#define PORTD_OFS  3
#define PORTE_OFS  4
#define PORTF_OFS  5
#define PORTG_OFS  6
#define PORTH_OFS  7

#define MAKE_GPIO_NUM(p, o) ((p << GPIO_PORT_SHIFTT) | (o << GPIO_OFS_SHIFT))

#define GRAB_MODE(x)  (((x) & GPIO_MODE_MASK) >> GPIO_MODE_SHIFT)
#define GRAB_PULLUP(x)  (((x) & GPIO_PULLUP_MASK) >> GPIO_PULLUP_SHIFT)
#define GRAB_PORT(x)  (((x) & GPIO_PORT_MASK) >> GPIO_PORT_SHIFTT)
#define GRAB_OFS(x)  (((x) & GPIO_OFS_MASK) >> GPIO_OFS_SHIFT)

#define set_gpio_ctrl(x) \
 ({ GPCON(GRAB_PORT((x))) &= ~(0x3 << (GRAB_OFS((x))*2)); \
    GPCON(GRAB_PORT(x)) |= (GRAB_MODE(x) << (GRAB_OFS((x))*2)); \
    GPUP(GRAB_PORT((x))) &= ~(1 << GRAB_OFS((x))); \
    GPUP(GRAB_PORT((x))) |= (GRAB_PULLUP((x)) << GRAB_OFS((x))); })
#define set_gpio_pullup(x) \
 ({ GPUP(GRAB_PORT((x))) &= ~(1 << GRAB_OFS((x))); \
    GPUP(GRAB_PORT((x))) |= (GRAB_PULLUP((x)) << GRAB_OFS((x))); })
#define set_gpio_pullup_user(x, v) \
 ({ GPUP(GRAB_PORT((x))) &= ~(1 << GRAB_OFS((x))); \
    GPUP(GRAB_PORT((x))) |= ((v) << GRAB_OFS((x))); })
#define set_gpio_mode(x) \
 ({ GPCON(GRAB_PORT((x))) &= ~(0x3 << (GRAB_OFS((x))*2)); \
    GPCON(GRAB_PORT((x))) |= (GRAB_MODE((x)) << (GRAB_OFS((x))*2)); })
#define set_gpio_mode_user(x, v) \
 ({ GPCON(GRAB_PORT((x))) &= ~(0x3 << (GRAB_OFS((x))*2)); \
    GPCON(GRAB_PORT((x))) |= ((v) << (GRAB_OFS((x))*2)); })
#define set_gpioA_mode(x) \
 ({ GPCON(GRAB_PORT((x))) &= ~(0x1 << GRAB_OFS((x))); \
    GPCON(GRAB_PORT((x))) |= (GRAB_MODE((x)) << GRAB_OFS((x))); })
#define read_gpio_bit(x) ((GPDAT(GRAB_PORT((x))) & (1<<GRAB_OFS((x)))) >> GRAB_OFS((x)))
#define read_gpio_reg(x) (GPDAT(GRAB_PORT((x)))
#define write_gpio_bit(x, v) \
 ({ GPDAT(GRAB_PORT((x))) &= ~(0x1 << GRAB_OFS((x))); \
    GPDAT(GRAB_PORT((x))) |= ((v) << GRAB_OFS((x))); })
#define write_gpio_reg(x, v) (GPDAT(GRAB_PORT((x))) = (v))
 

#define GPIO_A0    MAKE_GPIO_NUM(PORTA_OFS, 0)
#define GPIO_A1    MAKE_GPIO_NUM(PORTA_OFS, 1)
#define GPIO_A2    MAKE_GPIO_NUM(PORTA_OFS, 2)
#define GPIO_A3    MAKE_GPIO_NUM(PORTA_OFS, 3)
#define GPIO_A4    MAKE_GPIO_NUM(PORTA_OFS, 4)
#define GPIO_A5    MAKE_GPIO_NUM(PORTA_OFS, 5)
#define GPIO_A6    MAKE_GPIO_NUM(PORTA_OFS, 6)
#define GPIO_A7    MAKE_GPIO_NUM(PORTA_OFS, 7)
#define GPIO_A8    MAKE_GPIO_NUM(PORTA_OFS, 8)
#define GPIO_A9    MAKE_GPIO_NUM(PORTA_OFS, 9)
#define GPIO_A10   MAKE_GPIO_NUM(PORTA_OFS, 10)
#define GPIO_A11   MAKE_GPIO_NUM(PORTA_OFS, 11)
#define GPIO_A12   MAKE_GPIO_NUM(PORTA_OFS, 12)
#define GPIO_A13   MAKE_GPIO_NUM(PORTA_OFS, 13)
#define GPIO_A14   MAKE_GPIO_NUM(PORTA_OFS, 14)
#define GPIO_A15   MAKE_GPIO_NUM(PORTA_OFS, 15)
#define GPIO_A16   MAKE_GPIO_NUM(PORTA_OFS, 16)
#define GPIO_A17   MAKE_GPIO_NUM(PORTA_OFS, 17)
#define GPIO_A18   MAKE_GPIO_NUM(PORTA_OFS, 18)
#define GPIO_A19   MAKE_GPIO_NUM(PORTA_OFS, 19)
#define GPIO_A20   MAKE_GPIO_NUM(PORTA_OFS, 20)
#define GPIO_A21   MAKE_GPIO_NUM(PORTA_OFS, 21)
#define GPIO_A22   MAKE_GPIO_NUM(PORTA_OFS, 22)

#define GPIO_B0    MAKE_GPIO_NUM(PORTB_OFS, 0)
#define GPIO_B1    MAKE_GPIO_NUM(PORTB_OFS, 1)
#define GPIO_B2    MAKE_GPIO_NUM(PORTB_OFS, 2)
#define GPIO_B3    MAKE_GPIO_NUM(PORTB_OFS, 3)
#define GPIO_B4    MAKE_GPIO_NUM(PORTB_OFS, 4)
#define GPIO_B5    MAKE_GPIO_NUM(PORTB_OFS, 5)
#define GPIO_B6    MAKE_GPIO_NUM(PORTB_OFS, 6)
#define GPIO_B7    MAKE_GPIO_NUM(PORTB_OFS, 7)
#define GPIO_B8    MAKE_GPIO_NUM(PORTB_OFS, 8)
#define GPIO_B9    MAKE_GPIO_NUM(PORTB_OFS, 9)
#define GPIO_B10   MAKE_GPIO_NUM(PORTB_OFS, 10)

#define GPIO_C0    MAKE_GPIO_NUM(PORTC_OFS, 0)
#define GPIO_C1    MAKE_GPIO_NUM(PORTC_OFS, 1)
#define GPIO_C2    MAKE_GPIO_NUM(PORTC_OFS, 2)
#define GPIO_C3    MAKE_GPIO_NUM(PORTC_OFS, 3)
#define GPIO_C4    MAKE_GPIO_NUM(PORTC_OFS, 4)
#define GPIO_C5    MAKE_GPIO_NUM(PORTC_OFS, 5)
#define GPIO_C6    MAKE_GPIO_NUM(PORTC_OFS, 6)
#define GPIO_C7    MAKE_GPIO_NUM(PORTC_OFS, 7)
#define GPIO_C8    MAKE_GPIO_NUM(PORTC_OFS, 8)
#define GPIO_C9    MAKE_GPIO_NUM(PORTC_OFS, 9)
#define GPIO_C10   MAKE_GPIO_NUM(PORTC_OFS, 10)
#define GPIO_C11   MAKE_GPIO_NUM(PORTC_OFS, 11)
#define GPIO_C12   MAKE_GPIO_NUM(PORTC_OFS, 12)
#define GPIO_C13   MAKE_GPIO_NUM(PORTC_OFS, 13)
#define GPIO_C14   MAKE_GPIO_NUM(PORTC_OFS, 14)
#define GPIO_C15   MAKE_GPIO_NUM(PORTC_OFS, 15)

#define GPIO_D0    MAKE_GPIO_NUM(PORTD_OFS, 0)
#define GPIO_D1    MAKE_GPIO_NUM(PORTD_OFS, 1)
#define GPIO_D2    MAKE_GPIO_NUM(PORTD_OFS, 2)
#define GPIO_D3    MAKE_GPIO_NUM(PORTD_OFS, 3)
#define GPIO_D4    MAKE_GPIO_NUM(PORTD_OFS, 4)
#define GPIO_D5    MAKE_GPIO_NUM(PORTD_OFS, 5)
#define GPIO_D6    MAKE_GPIO_NUM(PORTD_OFS, 6)
#define GPIO_D7    MAKE_GPIO_NUM(PORTD_OFS, 7)
#define GPIO_D8    MAKE_GPIO_NUM(PORTD_OFS, 8)
#define GPIO_D9    MAKE_GPIO_NUM(PORTD_OFS, 9)
#define GPIO_D10   MAKE_GPIO_NUM(PORTD_OFS, 10)
#define GPIO_D11   MAKE_GPIO_NUM(PORTD_OFS, 11)
#define GPIO_D12   MAKE_GPIO_NUM(PORTD_OFS, 12)
#define GPIO_D13   MAKE_GPIO_NUM(PORTD_OFS, 13)
#define GPIO_D14   MAKE_GPIO_NUM(PORTD_OFS, 14)
#define GPIO_D15   MAKE_GPIO_NUM(PORTD_OFS, 15)

#define GPIO_E0    MAKE_GPIO_NUM(PORTE_OFS, 0)
#define GPIO_E1    MAKE_GPIO_NUM(PORTE_OFS, 1)
#define GPIO_E2    MAKE_GPIO_NUM(PORTE_OFS, 2)
#define GPIO_E3    MAKE_GPIO_NUM(PORTE_OFS, 3)
#define GPIO_E4    MAKE_GPIO_NUM(PORTE_OFS, 4)
#define GPIO_E5    MAKE_GPIO_NUM(PORTE_OFS, 5)
#define GPIO_E6    MAKE_GPIO_NUM(PORTE_OFS, 6)
#define GPIO_E7    MAKE_GPIO_NUM(PORTE_OFS, 7)
#define GPIO_E8    MAKE_GPIO_NUM(PORTE_OFS, 8)
#define GPIO_E9    MAKE_GPIO_NUM(PORTE_OFS, 9)
#define GPIO_E10   MAKE_GPIO_NUM(PORTE_OFS, 10)
#define GPIO_E11   MAKE_GPIO_NUM(PORTE_OFS, 11)
#define GPIO_E12   MAKE_GPIO_NUM(PORTE_OFS, 12)
#define GPIO_E13   MAKE_GPIO_NUM(PORTE_OFS, 13)
#define GPIO_E14   MAKE_GPIO_NUM(PORTE_OFS, 14)
#define GPIO_E15   MAKE_GPIO_NUM(PORTE_OFS, 15)

#define GPIO_F0    MAKE_GPIO_NUM(PORTF_OFS, 0)
#define GPIO_F1    MAKE_GPIO_NUM(PORTF_OFS, 1)
#define GPIO_F2    MAKE_GPIO_NUM(PORTF_OFS, 2)
#define GPIO_F3    MAKE_GPIO_NUM(PORTF_OFS, 3)
#define GPIO_F4    MAKE_GPIO_NUM(PORTF_OFS, 4)
#define GPIO_F5    MAKE_GPIO_NUM(PORTF_OFS, 5)
#define GPIO_F6    MAKE_GPIO_NUM(PORTF_OFS, 6)
#define GPIO_F7    MAKE_GPIO_NUM(PORTF_OFS, 7)

#define GPIO_G0    MAKE_GPIO_NUM(PORTG_OFS, 0)
#define GPIO_G1    MAKE_GPIO_NUM(PORTG_OFS, 1)
#define GPIO_G2    MAKE_GPIO_NUM(PORTG_OFS, 2)
#define GPIO_G3    MAKE_GPIO_NUM(PORTG_OFS, 3)
#define GPIO_G4    MAKE_GPIO_NUM(PORTG_OFS, 4)
#define GPIO_G5    MAKE_GPIO_NUM(PORTG_OFS, 5)
#define GPIO_G6    MAKE_GPIO_NUM(PORTG_OFS, 6)
#define GPIO_G7    MAKE_GPIO_NUM(PORTG_OFS, 7)
#define GPIO_G8    MAKE_GPIO_NUM(PORTG_OFS, 8)
#define GPIO_G9    MAKE_GPIO_NUM(PORTG_OFS, 9)
#define GPIO_G10   MAKE_GPIO_NUM(PORTG_OFS, 10)
#define GPIO_G11   MAKE_GPIO_NUM(PORTG_OFS, 11)
#define GPIO_G12   MAKE_GPIO_NUM(PORTG_OFS, 12)
#define GPIO_G13   MAKE_GPIO_NUM(PORTG_OFS, 13)
#define GPIO_G14   MAKE_GPIO_NUM(PORTG_OFS, 14)
#define GPIO_G15   MAKE_GPIO_NUM(PORTG_OFS, 15)

#define GPIO_H0    MAKE_GPIO_NUM(PORTH_OFS, 0)
#define GPIO_H1    MAKE_GPIO_NUM(PORTH_OFS, 1)
#define GPIO_H2    MAKE_GPIO_NUM(PORTH_OFS, 2)
#define GPIO_H3    MAKE_GPIO_NUM(PORTH_OFS, 3)
#define GPIO_H4    MAKE_GPIO_NUM(PORTH_OFS, 4)
#define GPIO_H5    MAKE_GPIO_NUM(PORTH_OFS, 5)
#define GPIO_H6    MAKE_GPIO_NUM(PORTH_OFS, 6)
#define GPIO_H7    MAKE_GPIO_NUM(PORTH_OFS, 7)
#define GPIO_H8    MAKE_GPIO_NUM(PORTH_OFS, 8)
#define GPIO_H9    MAKE_GPIO_NUM(PORTH_OFS, 9)
#define GPIO_H10   MAKE_GPIO_NUM(PORTH_OFS, 10)

#define GPIO_MODE_TOUT   GPIO_MODE_ALT0
#define GPIO_MODE_nXBACK  GPIO_MODE_ALT0
#define GPIO_MODE_nXBREQ  GPIO_MODE_ALT0
#define GPIO_MODE_nXDACK  GPIO_MODE_ALT0
#define GPIO_MODE_nXDREQ  GPIO_MODE_ALT0
#define GPIO_MODE_LEND   GPIO_MODE_ALT0
#define GPIO_MODE_VCLK   GPIO_MODE_ALT0
#define GPIO_MODE_VLINE   GPIO_MODE_ALT0
#define GPIO_MODE_VFRAME  GPIO_MODE_ALT0
#define GPIO_MODE_VM   GPIO_MODE_ALT0
#define GPIO_MODE_LCDVF   GPIO_MODE_ALT0
#define GPIO_MODE_VD   GPIO_MODE_ALT0
#define GPIO_MODE_IICSDA  GPIO_MODE_ALT0
#define GPIO_MODE_IICSCL  GPIO_MODE_ALT0
#define GPIO_MODE_SPICLK  GPIO_MODE_ALT0
#define GPIO_MODE_SPIMOSI  GPIO_MODE_ALT0
#define GPIO_MODE_SPIMISO  GPIO_MODE_ALT0
#define GPIO_MODE_SDDAT   GPIO_MODE_ALT0
#define GPIO_MODE_SDCMD   GPIO_MODE_ALT0
#define GPIO_MODE_SDCLK   GPIO_MODE_ALT0
#define GPIO_MODE_I2SSDO  GPIO_MODE_ALT0
#define GPIO_MODE_I2SSDI  GPIO_MODE_ALT0
#define GPIO_MODE_CDCLK   GPIO_MODE_ALT0
#define GPIO_MODE_I2SSCLK  GPIO_MODE_ALT0
#define GPIO_MODE_I2SLRCK  GPIO_MODE_ALT0
#define GPIO_MODE_I2SSDI_ABNORMAL GPIO_MODE_ALT1
#define GPIO_MODE_nSS   GPIO_MODE_ALT1
#define GPIO_MODE_EINT   GPIO_MODE_ALT0
#define GPIO_MODE_nYPON   GPIO_MODE_ALT1
#define GPIO_MODE_YMON   GPIO_MODE_ALT1
#define GPIO_MODE_nXPON   GPIO_MODE_ALT1
#define GPIO_MODE_XMON   GPIO_MODE_ALT1
#define GPIO_MODE_UART   GPIO_MODE_ALT0 
#define GPIO_MODE_TCLK_ABNORMAL  GPIO_MODE_ALT1
#define GPIO_MODE_SPICLK_ABNORMAL GPIO_MODE_ALT1
#define GPIO_MODE_SPIMOSI_ABNORMAL GPIO_MODE_ALT1
#define GPIO_MODE_SPIMISO_ABNORMAL GPIO_MODE_ALT1
#define GPIO_MODE_LCD_PWRDN  GPIO_MODE_ALT1

posted on 2009-09-02 22:04  palam  阅读(521)  评论(0编辑  收藏  举报