static int gpio_read(struct file *file, char *buffer, size_t size, loff_t *pos) //从内核中读取GPIO引脚的值
{
int ret
char key_buf[4]
if (gpio_get_value(RK30_PIN4_PD1)==0){
key_buf[0]=0
}else{
key_buf[0]=1
}
if (gpio_get_value(RK30_PIN4_PD2)==0){
key_buf[1]=0
}else{
key_buf[1]=1
}
if (gpio_get_value(RK30_PIN4_PD3)==0){
key_buf[2]=0
}else{
key_buf[2]=1
}
if (gpio_get_value(RK30_PIN4_PD4)==0){
key_buf[3]=0
}else{
key_buf[3]=1
}
ret=copy_to_user(buffer,key_buf,4)//拷贝数据到用户区 ,成功为 0,失败为字节数
return ret
}
static long gpio_ioctl(struct file *file ,unsigned int cmd,unsigned long arg){
switch(cmd){//改变引脚的电平值
case 00: gpio_set_value(RK30_PIN4_PD1,0)
printk("GPIO_D1 is low!\n")
break
case 01: gpio_set_value(RK30_PIN4_PD1,1)
printk("GPIO_D1 is high!\n")
break
case 10: gpio_set_value(RK30_PIN4_PD2,0)
printk("GPIO_D2 is low!\n")
break
case 11: gpio_set_value(RK30_PIN4_PD2,1)
printk("GPIO_D2 is high!\n")
break
case 20: gpio_set_value(RK30_PIN4_PD3,0)
printk("GPIO_D3 is low!\n")
break
case 21: gpio_set_value(RK30_PIN4_PD3,1)
printk("GPIO_D3 is high!\n")
break
case 30: gpio_set_value(RK30_PIN4_PD4,0)
printk("GPIO_D4 is low!\n")
break
case 31: gpio_set_value(RK30_PIN4_PD4,1)
printk("GPIO_D4 is high!\n")
break
/*初始化设备,配置对应的IO,以及注册设备*/
static int __init dev_init(void)
{
int ret
ret=0
ret = register_chrdev(RKPX2_GPIO_MAJOR,"rkpx2_GPIO",&dev_fops)//注册
if (ret<0) {
printk("rkpx2 GPIO for test unable to get major%d \n",ret)
return ret
}
dev_class = class_create(THIS_MODULE,"rkpx2_GPIO")//初始化
if (IS_ERR(dev_class)){
unregister_chrdev(RKPX2_GPIO_MAJOR,"rkpx2_GPIO")
return PTR_ERR(dev_class)
}
device_create(dev_class,NULL,MKDEV(RKPX2_GPIO_MAJOR,0),NULL,"rkpx2_GPIO")//创建设备
printk(KERN_INFO "RKPX2 GPIO driver successfully probed!\n")
int fd
int ret
int flag
int pin
int ch
printf("\n start test gpio_drivers\n")
if(strcmp(argv[1],"open")==0){
fd=open(DEVICE_NAME,O_RDWR)
if (fd==-1){
printf("open devices %s error\n",DEVICE_NAME)
}
printf("input the pin you want to operate")
scanf("%d",&pin)
printf("\n")
printf("it will be set ?(1=on or 0=off):")
scanf("%d",&ch)
switch(pin){
case 0: (ch==1?ioctl(fd,1):ioctl(fd,2))break
case 1: (ch==1?ioctl(fd,3):ioctl(fd,4))break
case 2: (ch==1?ioctl(fd,5):ioctl(fd,6))break
case 3: (ch==1?ioctl(fd,7):ioctl(fd,8))break