设为首页 加入收藏

TOP

Linux下GPIO驱动详解文章(一)
2014-11-24 11:39:33 来源: 作者: 【 】 浏览:0
Tags:Linux GPIO 驱动 详解 文章

***************************************************************************


注意:在/arch/arm/mach-s3c2410/include/mach/gpio-fns.h源代码中有如下说明:


16/* These functions are in the to-be-removed category and it is strongly
17 * encouraged not to use these in new code. They will be marked deprecated
18 * very soon.
19 *
20 * Most of the functionality can be either replaced by the gpiocfg calls
21 * for the s3c platform or by the generic GPIOlib API.
22 *
23 * As of 2.6.35-rc, these will be removed, with the few drivers using them
24 * either replaced or given a wrapper until the calls can be removed.
25*/



该头文件包括:


static inline void s3c2410_gpio_cfgpin(unsigned int pin, unsigned int cfg)


该函数直接使用


linux/arch/arm/plat-s3c/gpio-config.c中的


int s3c_gpio_cfgpin(unsigned int pin, unsigned int config)


即可



***************************************************************************



首先看一下设备初始化程序:


85 /*
86 * 设备初始化
87 */

88 static int __init dev_init(void)
89 {
90 int ret;
91 int i;
92 for (i = 0; i < 4; i++) {
93 //设置 LED 对应的端口寄存器为输出(OUTPUT)
94
if (s3c_gpio_cfgpin(led_table[i], led_cfg_table[i])<0)


printk(KERN_INFO "config pin %d failed", i);



95 printk(KERN_INFO "config pin %d failed", i);

95 //设置 LED 对应的端口寄存器为低电平输出,在模块加载> 结束后,四个 LED 应该是全部都是发光
96 状态
97 s3c2410_gpio_setpin(led_table[i], 0);
98 }
99 ret = misc_register(&misc); //注册设备
100 printk (DEVICE_NAME"/tinitialized/n"); //打印初始化信息
101 return ret;
102 }



可以看到,这里涉及到两个函数,分别是s3c2410_gpio_cfgpin,s3c2410_gpio_setpin,这两个函数分别对四个LED进行配置,从函数名来看,cfgpin对引脚寄存器状态进行配置,而setpin应该是对寄存器数据值进行配置,我们在分析函数之前先弄清楚传入的参数到底是什么。


led_table[i]
28 //LED 对应的 GPIO 端口列表
29 static unsigned long led_table [] = {
30 S3C2410_GPB(5),
31 S3C2410_GPB(6),
32 S3C2410_GPB(7),
33 S3C2410_GPB(8),
34 };


这里S3C2410_GPB宏定义在mach/gpio-nrs.h中
/* 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)
#define S3C2410_GPIO_J_NR (32) /* technically 16. */
#define S3C2410_GPIO_K_NR (32) /* technically 16. */
#define S3C2410_GPIO_L_NR (32) /* technically 15. */
#define S3C2410_GPIO_M_NR (32) /* technically 2. */



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



//这里的CONFIG_S3C_GPIO_SPAC是内核配置选项,在.config中可以找到,我的配置为:


CONFIG_S3C_GPIO_SPACE = 0



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),
S3C2410_GPIO_J_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_H),
S3C2410_GPIO_K_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_J),
S3C2410_GPIO_L_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_K),
S3C2410_GPIO_M_START = S3C2410_GPIO_NEXT(S3C2410_GPIO_L),
};


#define S3C2410_GPB(_nr) (S3C2410_GPIO_B_START + (_nr))


因此,以S3C2410_GPB(5)为例,其宏展开为:


S3C2410_GPIO_NEXT(S3C2410_GPIO_A) +

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇编译Android 2.3.1 源代码心得 下一篇Linux 0.12中bread函数流程

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·求navicat for mysql (2025-12-26 13:21:33)
·有哪位大哥推荐一下m (2025-12-26 13:21:30)
·MySQL下载与安装教程 (2025-12-26 13:21:26)
·Linux_百度百科 (2025-12-26 12:51:52)
·Shell 流程控制 | 菜 (2025-12-26 12:51:49)