Linux下arch/arm/mach-s3c2410/include/mach/gpio-nrs.h的理解

  刚刚开始学习Linux下设备驱动程序,可能很多新手现在和毛毛一样对这个文件(arch/arm/mach-s3c2410/include/mach/gpio-nrs.h)有很多的不理解,在理解这个文件的同时,毛毛建议和linux/arch/arm/plat-s3c24xx/gpio.c这个文件一起理解。好吧,下面就和毛毛一起来学习吧!

1:首先毛毛将gpio-nrs.h的源码贴出来。

/* arch/arm/mach-s3c2410/include/mach/gpio-nrs.h
 *
 * Copyright (c) 2008 Simtec Electronics
 * http://armlinux.simtec.co.uk/
 * Ben Dooks <ben@simtec.co.uk>
 *
 * S3C2410 - GPIO bank numbering
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
*/

#ifndef __MACH_GPIONRS_H
#define __MACH_GPIONRS_H

#define S3C2410_GPIONO(bank,offset) ((bank) + (offset))

#define S3C2410_GPIO_BANKA   (32*0)
#define S3C2410_GPIO_BANKB   (32*1)
#define S3C2410_GPIO_BANKC   (32*2)
#define S3C2410_GPIO_BANKD   (32*3)
#define S3C2410_GPIO_BANKE   (32*4)
#define S3C2410_GPIO_BANKF   (32*5)
#define S3C2410_GPIO_BANKG   (32*6)
#define S3C2410_GPIO_BANKH   (32*7)

/* GPIO bank sizes */
#define S3C2410_GPIO_A_NR (32)
#define S3C2410_GPIO_B_NR (32)
#define S3C2410_GPIO_C_NR (32)
#define S3C2410_GPIO_D_NR (32)
#define S3C2410_GPIO_E_NR (32)
#define S3C2410_GPIO_F_NR (32)
#define S3C2410_GPIO_G_NR (32)
#define S3C2410_GPIO_H_NR (32)

#if CONFIG_S3C_GPIO_SPACE != 0
#error CONFIG_S3C_GPIO_SPACE cannot be zero at the moment
#endif

#define S3C2410_GPIO_NEXT(__gpio) \
 ((__gpio##_START) + (__gpio##_NR) + CONFIG_S3C_GPIO_SPACE + 0)

#ifndef __ASSEMBLY__

enum s3c_gpio_number {
 S3C2410_GPIO_A_START = 0,
 S3C2410_GPIO_B_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_A),
 S3C2410_GPIO_C_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_B),
 S3C2410_GPIO_D_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_C),
 S3C2410_GPIO_E_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_D),
 S3C2410_GPIO_F_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_E),
 S3C2410_GPIO_G_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_F),
 S3C2410_GPIO_H_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_G),
};

#endif /* __ASSEMBLY__ */

/* S3C2410 GPIO number definitions. */

#define S3C2410_GPA(_nr) (S3C2410_GPIO_A_START + (_nr))
#define S3C2410_GPB(_nr) (S3C2410_GPIO_B_START + (_nr))
#define S3C2410_GPC(_nr) (S3C2410_GPIO_C_START + (_nr))
#define S3C2410_GPD(_nr) (S3C2410_GPIO_D_START + (_nr))
#define S3C2410_GPE(_nr) (S3C2410_GPIO_E_START + (_nr))
#define S3C2410_GPF(_nr) (S3C2410_GPIO_F_START + (_nr))
#define S3C2410_GPG(_nr) (S3C2410_GPIO_G_START + (_nr))
#define S3C2410_GPH(_nr) (S3C2410_GPIO_H_START + (_nr))

/* compatibility until drivers can be modified */

#define S3C2410_GPA0 S3C2410_GPA(0)
#define S3C2410_GPA1 S3C2410_GPA(1)
#define S3C2410_GPA3 S3C2410_GPA(3)
#define S3C2410_GPA7 S3C2410_GPA(7)

#define S3C2410_GPE0 S3C2410_GPE(0)
#define S3C2410_GPE1 S3C2410_GPE(1)
#define S3C2410_GPE2 S3C2410_GPE(2)
#define S3C2410_GPE3 S3C2410_GPE(3)
#define S3C2410_GPE4 S3C2410_GPE(4)
#define S3C2410_GPE5 S3C2410_GPE(5)
#define S3C2410_GPE6 S3C2410_GPE(6)
#define S3C2410_GPE7 S3C2410_GPE(7)
#define S3C2410_GPE8 S3C2410_GPE(8)
#define S3C2410_GPE9 S3C2410_GPE(9)
#define S3C2410_GPE10 S3C2410_GPE(10)

#define S3C2410_GPH10 S3C2410_GPH(10)

#endif /* __MACH_GPIONRS_H */

2:我们开始来解析这个源程序。

毛毛发现这个源程序基本上全部是宏定义,其中有一个宏定义毛毛不太明白

#define S3C2410_GPIO_NEXT(__gpio) \
 ((__gpio##_START) + (__gpio##_NR) + CONFIG_S3C_GPIO_SPACE + 0)

#ifndef __ASSEMBLY__

毛毛是个努力的小孩,通过查阅资料毛毛发现这段宏定义中的##表示把前后程序连接起来,

也就是这样的:S3C2410_GPIO_NEXT(S3C2410_GPIO_B)就等同于

((S3C2410_GPIO_B_START)+(S3C2410_GPIO_B_NR)+CONFIG_S3C_GPIO_SPACE + 0)

在阅读程序的过程中毛毛还发现,S3C2410_GPx(_nr)这个宏是一个很重要的宏,在接下来的IO控制程序中都有应用。

毛毛是比较乖的,他尝试把这个宏展开是什么样子的,最后展开得到是:

S3C2410_GPx(_nr)=(S3C2410_GPIO_x_START + (_nr))

S3C2410_GPx(_nr)=(S3C2410_GPIO_NEXT(S3C2410_GPIO_x) + (_nr))

 S3C2410_GPx(_nr)=(((S3C2410_GPIO_(x-1)_START)+(S3C2410_GPIO_x_NR)+CONFIG_S3C_GPIO_SPACE + 0) + (_nr))

说明:(x-1)是毛毛个人的意思。即D-1=C,C-1=B,B-1=A;大家懂意思即可。

这个就是最后展开的宏:S3C2410_GPB(5)=0+32+0+0+5=37

最后我们最最想问的问题是Linux这样做是为了什么呢?

毛毛他说,在他的下一篇文章“Linux下arch/arm/mach-s3c2410/include/mach/gpio-nrs.h的理解”里面会讲到的。

细心的毛毛还发现,程序里面也这么一句

#define S3C2410_GPIONO(bank,offset) ((bank) + (offset))

我们按相同的方法将宏拆开S3C2410_GPIONO(S3C2410_GPIO_BANKA ,offset)=((S3C2410_GPIO_BANKA)+(offset))

S3C2410_GPB(5)=37,在上面我们已经得到啦。

S3C2410_GPIONO(S3C2410_GPIO_BANKB ,5)=S3C2410_GPIO_BANKB+5=32*1+5=37

在这里我们可以发现,S3C2410_GPx(_nr)和S3C2410_GPIONO(S3C2410_GPIO_BANKx ,_nr)是一样的作用。

毛毛个人估计是Linux系统在升级的过程中为了向下兼容才这样实现的

转载于:https://www.cnblogs.com/taomaomao/archive/2011/12/31/2308562.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值