好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

S3C2440开发板上实现按键点亮LED驱动开发的详细过程.doc

11页
  • 卖家[上传人]:宝路
  • 文档编号:2470565
  • 上传时间:2017-07-24
  • 文档格式:DOC
  • 文档大小:875KB
  • / 11 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 文章记录了作者在 S3C2440开发板上实现按键点亮 LED 驱动开发的详细过程,还记录了一些容易出现的错误,以及怎么解决这些错误一、驱动开发流程Linux 驱动开发不同于应用程序的开发驱动开发是直接和硬件打交道的,通过对硬件的操作给应用程序提供一些接口函数,使得应用程序能够“间接”的控制硬件来工作对于按键点亮 LED 的驱动开发流程如下2、驱动开发具体步骤1、查看开发板 TQ2440 底板原理图,找到按键和 LED 模块,如下图: 图-2 按键和 LED 电路图从上图我们可以清楚地看到 K1~K4 对应的管脚是 ENT1~ENT4,LED1~LED4 对应的管脚是nLED_1~nLED_4.2、查看 TQ2440_核心板原理图,找到对应的 CPU 管脚,如下图:图-3 按键和 LED 对应 CPU 管脚电路图3、查看 s3c2440芯片手册,查看 CPU 管脚的模式,如下图从上图我们可以看出按键对应的 CPU 管脚 GPF0~GPF4都是占两位(如:GPF0[1:0]) 按键是一种中断,要想让按键工作在中断模式下,就要设置 GPF0~GPF4(GPF3除外)管脚都设置在中断模式下,即为10。

      对于 LED 对应的 CPU 管脚 GPB5~GPB8也是占两位要想让 LED 工作,就要让 LED 工作在输出模式下,即对应管脚设置为01.4、编写按键点亮 LED 驱动程序/*调用内核头文件,和应用程序调用的头文件不一样*/#include #include #include #include #include #include #include #include #include #include #include #include #define DEVICE_NAME "tope-buttons" //自定义驱动称为“ tope-buttons”define BUTTON_MAJOR 232 //自定义驱动的主设备号是 232注意:此处的主设备号不 能和系统已使用的一样,用 cat /proc/devices 查看该设备号是否已使用,如果已被使用,请换一个未使用的主设备号define IOCTL_LED_ON 1 //定义 LED 亮为1#define IOCTL_LED_OFF 0 //定义 LED 暗为0/* 定义含中断,管脚,管脚设置等信息的结构体 */struct button_irq_desc{int irq;int pin;int pin_setting;int number;char *name; };/* 用来指定按键所用的外部中断引脚及中断触发方式, 名字 */static struct button_irq_desc button_irqs [] ={{IRQ_EINT1, S3C2410_GPF1, S3C2410_GPF1_EINT1, 0, "KEY1"}, /* K1 */{IRQ_EINT4, S3C2410_GPF4, S3C2410_GPF4_EINT4, 1, "KEY2"}, /* K2 */{IRQ_EINT2, S3C2410_GPF2, S3C2410_GPF2_EINT2, 2, "KEY3"}, /* K3 */{IRQ_EINT0, S3C2410_GPF0, S3C2410_GPF0_EINT0, 3, "KEY4"}, /* K4 */};上面初始化成员里的 S3C2410_GPF0_EINT0在 Regs-gpio.h 中定义为#define S3C2410_GPF0_EINT0 (0x02 pin);//注册中断 if (up)key_values[button_irqs->number] = (button_irqs->number + 1) + 0x80;elsekey_values[button_irqs->number] = (button_irqs->number + 1);//根据中断注册情况设置按键的值ev_press = 1; /* 表示中断发生了 */wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */return IRQ_RETVAL(IRQ_HANDLED);//返回中断信息}/* 被上层应用程序调用的 open 函数在驱动程序里的实现*/static int tope_buttons_open(struct inode *inode, struct file *file){int i;int err;for (i = 0; i = 0; i--){/*释放已经注册的中断*/disable_irq(button_irqs[i].irq);free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);}return -EBUSY;}/* 配置 LED 管脚 */for (i = 0; i f_flags & O_NONBLOCK)return -EAGAIN;else/* 如果 ev_press 等于0,休眠 */wait_event_interruptible(button_waitq, ev_press);}ev_press = 0;/* 把按键值的信息从内核空间复制到用户空间*/err = copy_to_user(buff, (const void *)key_values, min(sizeof(key_values), count));memset((void *)key_values, 0, sizeof(key_values));//清零return err ? -EFAULT : min(sizeof(key_values), count);}/* 被上层应用程序调用的 select 函数在驱动程序里的实现*/static unsigned int tope_buttons_poll( struct file *file, struct poll_table_struct *wait){unsigned int mask = 0;poll_wait(file, &button_waitq, wait);if (ev_press)mask |= POLLIN | POLLRDNORM;return mask;}/* 被上层应用程序调用的 ioctl 函数在驱动程序里的实现*/static int tope_leds_ioctl( struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ if (arg > 4) { return -EINVAL; } switch(cmd) { case IOCTL_LED_ON: //如果是点亮s3c2410_gpio_setpin(led_table[arg], 0);//点亮相应的管脚return 0; case IOCTL_LED_OFF://如果是熄灭 s3c2410_gpio_setpin(led_table[arg], 1);//熄灭相应的管脚return 0; default: return -EINVAL; }}/*上层应用程序函数和驱动函数的关联*/static struct file_operations tope_buttons_fops ={.owner = THIS_MODULE, /* 这是一个宏,指向编译模块时自动创建的 __this_module 变量 */.open = tope_buttons_open,/*上层应用程序中的 open 对应为.open,驱动实现 tope_buttons_open 在本程序中*/.release = tope_buttons_close,.read = tope_buttons_read,.poll = tope_buttons_poll,.ioctl = tope_leds_ioctl,};static char __initdata banner[] = "TQ2440/SKY2440 LEDS, (c) 2008,2009www.top-e.org\n";//打印信息/*自动创键设备节点函数声明,上层应用程序都是通过对设备文件的操作来控制下层硬件*/static struct class *button_class;/*驱动程序加载函数的实现*/static int __init tope_buttons_init(void){int ret;printk(banner);ret = register_chrdev(BUTTON_MAJOR, DEVICE_NAME, &tope_buttons_fops);/*注册驱 动*/if (ret #include #include #include #include #include #include #include #include #include int main(void){int i;int buttons_fd;int key_value[4];int temp=0;/*打开键盘点亮 LED 设备文件 */buttons_fd = open("/dev/tope-buttons", 0);//这里的 open 的实现在驱动的tope_buttons_openif (buttons_fd < 0) {perror("open device buttons");exit(1);}for (;;) //一直循环来监听是否有按键按下{fd_set rds;int ret;FD_ZERO(&rds);FD_SET(buttons_fd, &rds);/*使用系统调用 select 检查是否能够从 /dev/buttons 设备读取数据*/ret = select(buttons_fd + 1, &rds, NULL, NULL, NULL);/*读取出错则退出程序*/if (ret < 0) {perror("select");exit(1);}if (ret == 0) {printf("Timeout.\n");}/*能够读取到数据*/else if (FD_ISSET(buttons_fd, &rds)) {/*开始读取键盘驱动发出的数据,注意 key_value 和键盘驱动中定义为一致的类型*/int ret = read(buttons_fd, key_value, sizeof key_value);/*read 也是在 tope_buttons_read 中实现的*/if (ret != sizeof key_value) {if (errno != EAGAIN)perror("read buttons\n");continue;}else{/*打印键值*/for (i = 0; i < 4; i++)if(key_value[i] != 0){printf("K%d %s, key value = 0x%02x\n", i+1, (key_value[i] & 0x80) ? "released" : key_value[i] ? "pressed down" : "", key_value[i]);if(temp==key_value[i])ioctl(buttons_fd,0,i);elseioctl(buttons_fd,1,i); //判断按键值,点亮对应的 LEDtemp=key_value[i];}}}}/*关闭设备文件句柄*/close(buttons_fd);。

      点击阅读更多内容
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.