昨天弄完数码管驱动,也没别的事儿做,单位通知到15号还是在家办公,那就接着鼓动这块落灰板子吧。
忘说了,周立功这块拓展版datesheet有问题,引脚对应关系完全不对。得用万用表测量出引脚。接线短接见下图:
驱动代码直接贴出来了,代码注释的地方懒得补齐了,没写应用层代码实现,直接按键内核打印按下键值,如图:
#include
#include
#include
#include
#include"mach/../../mx28_pins.h"
#include
#include "mach/mx28.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define CHR_NAME "AP-283Demo-key"
#define CLS_NAME "AP-283Demo-gpio"
#define DEV_NAME "keys_int"
struct dev_keys{
dev_t devid; //设备号
int major_num; //主设备号
int minor_num; //次设备号
struct cdev cdev; //cdev
struct class *class; //类
struct device *device; //设备
};
struct gpio_info {
u32 pin;
char pin_name[20];
irqreturn_t (*func)(int irq,void *dev_id);
};
struct dev_keys keys;
static irqreturn_t key_1_handler(int irq,void *dev_id);
static irqreturn_t key_2_handler(int irq,void *dev_id);
static irqreturn_t key_3_handler(int irq,void *dev_id);
static irqreturn_t key_4_handler(int irq,void *dev_id);
static irqreturn_t key_5_handler(int irq,void *dev_id);
static struct gpio_info key[] ={
{PINID_SAIF0_BITCLK, "gpio-KEY1" , key_1_handler},
{PINID_SAIF0_SDATA0, "gpio-KEY2" , key_2_handler},
{PINID_SSP1_DATA0, "gpio-KEY3" , key_3_handler},
{PINID_SSP1_SCK, "gpio-KEY3" , key_4_handler},
{PINID_SSP1_CMD, "gpio-KEY4" , key_5_handler},
{0, "", NULL}, //the end
};
static int keys_open(struct inode *inode, struct file *file);
static int keys_release(struct inode *inode, struct file *file);
static long keys_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
static struct file_operations keys_fops = {
.owner = THIS_MODULE,
.open = keys_open,
.release = keys_release,
.unlocked_ioctl = keys_ioctl,
};
static irqreturn_t key_1_handler(int irq,void *dev_id)
{
printk("key1\n");
return IRQ_RETVAL(IRQ_HANDLED);
}
static irqreturn_t key_2_handler(int irq,void *dev_id)
{
printk("key2\n");
return IRQ_RETVAL(IRQ_HANDLED);
}
static irqreturn_t key_3_handler(int irq,void *dev_id)
{
printk("key3\n");
return IRQ_RETVAL(IRQ_HANDLED);
}
static irqreturn_t key_4_handler(int irq,void *dev_id)
{
printk("key4\n");
return IRQ_RETVAL(IRQ_HANDLED);
}
static irqreturn_t key_5_handler(int irq,void *dev_id)
{
printk("key5\n");
return IRQ_RETVAL(IRQ_HANDLED);
}
static int keys_open(struct inode *inode, struct file *file)
{/*
int i;
printk("pocky330_open \n");
for(i=0;i<4;i++)
{
gpio_request(gpios[i],"gpio");
s3c_gpio_cfgpin(gpios[i],S3C_GPIO_INPUT);
s3c_gpio_setpull(gpios[i],S3C_GPIO_PULL_NONE);
}
*/
return 0;
}
static int keys_release(struct inode *inode, struct file *file)
{/*
int i;
printk("pocky330_release \n");
for(i=0;i<4;i++)
{
gpio_free(gpios[i]);
}
*/
return 0;
}
static long keys_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
/*
if(cmd == 3)
{
return gpio_get_value(gpios[2]);
}
else if(cmd == 4)
{
return gpio_get_value(gpios[3]);
}
else
{
printk(KERN_EMERG "cmd err!! \n");
}
*/
return 0;
}
static int gpio_keys_init(void)
{
int irq_no;
int iRet;
int i;
printk("keys_gpio_init!!! \n");
for(i=0;key[i].pin != 0;i++){
gpio_free(MXS_PIN_TO_GPIO(key[i].pin));
iRet = gpio_request(MXS_PIN_TO_GPIO(key[i].pin), key[i].pin_name);
if (iRet != 0) {
printk("request %s failed \n",key[i].pin_name);
return -EBUSY;
}
gpio_direction_input(MXS_PIN_TO_GPIO(key[i].pin));
irq_no = gpio_to_irq(MXS_PIN_TO_GPIO(key[i].pin));
// printk("%s request irq _num [%d] success \n",key[i].pin_name,irq_no);
set_irq_type(irq_no, IRQF_TRIGGER_FALLING); //下降沿中断
iRet = request_irq(irq_no, key[i].func, IRQF_DISABLED, key[i].pin_name, NULL);
if (iRet != 0){
printk("request irq failed!! ret: %d irq:%d gpio:%d \n", iRet, irq_no,MXS_PIN_TO_GPIO(key[i].pin));
return -EBUSY;
}
printk("request irq success!! ret: %d irq:%d gpio:%d \n", iRet, irq_no,MXS_PIN_TO_GPIO(key[i].pin));
}
printk("Initialize all keys gpio success \n");
return 0;
}
static int __init keys_init(viod)
{
int ret;
dev_t devno;
printk("keys_mod_init!!! \n");
ret = alloc_chrdev_region(&devno,keys.minor_num,1,CHR_NAME);
keys.major_num = MAJOR(devno);
if(ret < 0){
printk("alloc_chrdev_region fail!!! \n");
return ret;
}else{
printk("keys register success dev-number[%d]\n",keys.major_num);
}
cdev_init(&keys.cdev,&keys_fops);
ret = cdev_add(&keys.cdev, devno, 1);
if(ret<0){
printk("add device failure\n");
}
keys.class = class_create(THIS_MODULE,CLS_NAME);
keys.device = device_create(keys.class,NULL,MKDEV(keys.major_num, 0),NULL,DEV_NAME);
ret = gpio_keys_init();
return 0;
}
static void keys_exit(void)
{
dev_t devno = MKDEV(keys.major_num, keys.minor_num);
cdev_del(&keys.cdev);
device_unregister(keys.device);
class_destroy(keys.class);
unregister_chrdev_region(devno,1);
//free_irq(IRQ_EINT(9),NULL);
//free_irq(IRQ_EINT(10),NULL);
printk("keys_int unregister success\n");
}
module_init(keys_init);
module_exit(keys_exit);
MODULE_AUTHOR("Rambo.zhang");