|
本帖最后由 s1005056 于 2014-5-13 16:59 编辑
版大以及各位版友好,
目前想練習做兩個按鍵,接key 6x6的3.3V以及KP_ROW1(GPH3_5),然後利用KP_ROW2(GPH3_2)跟KP_ROW4(GPH3_4)讀取管腳電平。在參考LED驅動範本後,實作如下的程式碼:
- #include <linux/miscdevice.h>
- #include <linux/input.h>
- #include <linux/clk.h>
- #include <linux/delay.h>
- #include <asm/io.h>
- #include <asm/uaccess.h>
- #include <mach/map.h>
- #include <mach/gpio.h>
- //#include <mach/gpio-bank.h>
- #include <mach/regs-gpio.h>
- #include <plat/gpio-core.h>
- #include <plat/gpio-cfg.h>
- #include <plat/gpio-cfg-helpers.h>
- #define DEVICE_NAME "button"
- #define IOCTL_GPIO_READ_STATUS 1
- /*assign pins */
- static unsigned long gpio_table [] =
- {
- S5PV210_GPH3(5), /* high voltage output*/
- S5PV210_GPH3(2), /* input */
- S5PV210_GPH3(4), /* input */
- };
- static unsigned int gpio_cfg_table [] =
- {
- S3C_GPIO_SFN(1),
- S3C_GPIO_SFN(0),
- S3C_GPIO_SFN(0),
- };
- static long tq210_btn_ioctl(
- struct file *file,
- unsigned int cmd,
- unsigned long arg)
- {
- arg -= 1;
- if (arg > sizeof(gpio_table)/sizeof(unsigned long))
- {
- return -EINVAL;
- }
- switch(cmd)
- {
- case IOCTL_GPIO_READ_STATUS:
- printk(KERN_INFO "pin[%ld] status= %d\n", arg, gpio_direction_input(gpio_table[arg]));
- return 0;
- default:
- return -EINVAL;
- }
- }
- static int tq210_btn_open(struct inode *inode, struct file *file)
- {
- int i;
- int err;
- /* high voltage supply */
- err = gpio_request(gpio_table[0], "GPH3_5");
- if(err)
- {
- printk(KERN_ERR "failed to request GPH3_5 pin and name\n");
- return err;
- }
-
- /* set as input pin */
- err = gpio_request(gpio_table[1], "GPH3_2");
- if(err)
- {
- printk(KERN_ERR "failed to request GPH3_2 pin and name\n");
- return err;
- }
- err = gpio_request(gpio_table[2], "GPH3_4");
- if(err)
- {
- printk(KERN_ERR "failed to request GPH3_4 pin and name\n");
- return err;
- }
- printk(KERN_INFO " buttons opened\n");
-
- for (i = 0; i < sizeof(gpio_table)/sizeof(unsigned long); i++)
- {
- s3c_gpio_cfgpin(gpio_table[i], gpio_cfg_table[i]);
- }
- return 0;
- }
- static int tq210_btn_close(struct inode *inode, struct file *file)
- {
- int i;
-
- for (i = 0; i < sizeof(gpio_table)/sizeof(unsigned long); i++)
- {
- gpio_free(gpio_table[i]);
- }
- printk(KERN_INFO "TQ210 BTNs driver successfully close\n");
- return 0;
- }
- static struct file_operations dev_fops = {
- .owner = THIS_MODULE,
- .unlocked_ioctl = tq210_btn_ioctl,
- .open = tq210_btn_open,
- // .read = tq210_btn_read,
- .release = tq210_btn_close,
- };
- static struct miscdevice misc_btn = {
- .minor = MISC_DYNAMIC_MINOR,
- .name = DEVICE_NAME,
- .fops = &dev_fops,
- };
- static int __init button_init(void){
- int ret;
- ret = misc_register(&misc_btn);
- if(ret < 0)
- {
-
- }else{
- printk(KERN_INFO "TQ210 buttons driver successfully probed\n");
- }
-
- return ret;
- }
- static void __exit button_exit(void)
- {
- misc_deregister(&misc_btn);
- int i;
- for (i = 0; i < sizeof(gpio_table)/sizeof(unsigned long); i++)
- {
- gpio_free(gpio_table[i]);
- }
- printk(KERN_INFO "TQ210 buttons driver successfully exit\n");
- }
- module_init(button_init);
- module_exit(button_exit);
- MODULE_LICENSE("GPL");
复制代码
應用程式則如下:
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <asm/types.h>
- #include <linux/fb.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <fcntl.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/mman.h>
- #include <sys/poll.h>
- #define DEV_FILE_NAME "/dev/button" //驱动提供
- #define IOCTL_GPIO_READ_STATUS 1 //驱动提供,表示亮
- int main(){
- int devfd;
- int i,err;
-
- devfd = open(DEV_FILE_NAME,O_RDWR);
- if(devfd < 0)
- {
- printf("can't open dev (%s)",DEV_FILE_NAME);
- return 0;
- }
-
- for(i=0;i<10;++i)
- {
- err = ioctl(devfd,IOCTL_GPIO_READ_STATUS,1);
- if(err<0)
- printf("GPIO_READ GPH3(2) faild! (%d)\n",err);
- sleep(1); //休眠1S
- err = ioctl(devfd,IOCTL_GPIO_READ_STATUS,2);
- if(err<0)
- printf("GPIO_READ GPH3(4) faild! (%d)\n",err);
- sleep(1); //休眠1S
- }
- close(devfd);//关闭设备。对应驱动中的tq210_gpio_close
-
- return 0;
- }
复制代码
但是在ternimal中的輸出都一直顯示為0,如下圖:
看了半天還是不知道哪裏出錯了,還請版上各位高手指點! 謝謝!
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|