写了一个led驱动,四个led灯与引脚GPB5~GPB8连接,引脚输出低电平是点亮led灯;
想用led_driv_open函数配置引脚为输出并设初始值为低电平,但是只能将GPB5对应的led灯点亮,求大神看看
这是驱动代码,测试程序代码在下面
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/device.h> //for udev
#include <mach/regs-gpio.h> //gpio
MODULE_LICENSE("GPL");
static struct class *led_driv_class; //定义一个类
static unsigned int led_table[]=
{
S3C2410_GPB5,
S3C2410_GPB6,
S3C2410_GPB7,
S3C2410_GPB8
};
static int led_driv_open(struct inode *inode,struct file *file)
{
int i;
//配置GPB5,6,7,8为输出,且输出0,低电平
for(i=0; i<4; i++)
{
s3c2410_gpio_cfgpin(led_table[i], S3C2410_GPB5_OUTP);
s3c2410_gpio_setpin(led_table[i], 0);
}
return 0;
}
static int led_driv_write(struct file *file,const char __user *buf,size_t count,loff_t *ppos)
{
int val,i;
copy_from_user(&val, buf, count); //从用户空间拷贝数据
if(val==1)
{
//点灯
for(i=0;i<4;i++)
{
s3c2410_gpio_setpin(led_table[i], 0);
}
}
else
{
//灭灯
for(i=0;i<4;i++)
{
s3c2410_gpio_setpin(led_table[i], 1);
}
}
return 0;
}
static struct file_operations led_driv_fops={
.owner = THIS_MODULE,
.open = led_driv_open,
.write = led_driv_write,
};
int major;
static int __init led_driv_init(void)
{
printk("<1>\n Hello,Led drive!\n");
major=register_chrdev(0, "led_dev", &led_driv_fops);
//新建类
led_driv_class = class_create(THIS_MODULE, "led_dev");
if(IS_ERR(led_driv_class))
return PTR_ERR(led_driv_class);
//创建设备
device_create(led_driv_class,NULL,MKDEV(major, 0),NULL,"led_dev");
return 0;
}
static void __exit led_driv_exit(void)
{
printk("<1>\n Exit!\n");
//删除设备结点
device_destroy(led_driv_class,MKDEV(major, 0));
class_destroy(led_driv_class);
unregister_chrdev(major, "led_dev");
}
module_init(led_driv_init);
module_exit(led_driv_exit);
MODULE_LICENSE("GPL");
测试程序代码
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
int fd,val;
if((fd=open("/dev/led_dev",O_RDWR))<0)
{
printf("open device failed");
}
printf("type 1 to turn on the led,0 to turn of it!");
scanf("%d",&val);
write(fd,&val,4);
return 0;
}
------解决思路----------------------
//配置GPB5,6,7,8为输出,且输出0,低电平你只把GPB5配置成了输出,没配置其它引脚,当然只有5会亮了
for(i=0; i<4; i++)
{
s3c2410_gpio_cfgpin(led_table[i], S3C2410_GPB5_OUTP);
s3c2410_gpio_setpin(led_table[i], 0);
}