分类: LINUX
2008-12-29 12:51:47
9261EK上led灯的简单驱动及测试-大聪
一、驱动函数如下:
#include
#include
#include
#include
#include
#include
#include
#include
#define NAME "four led test"
static int major =231;//定义主设备号
static unsigned long led_table [] = //定义不同的led控制引脚
{
AT91_PIN_PA15,
AT91_PIN_PA31,
AT91_PIN_PA16,
AT91_PIN_PA30,
};
static int myled_driver_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)
{
unsigned int regled1;
regled1=gpio_get_value(led_table[arg]);
printk("led1 reg is : %d",regled1);
at91_set_gpio_output(led_table[arg],1);
switch(cmd)//控制led_table[arg]的亮灭
{
case 0x00:at91_set_gpio_value(led_table[arg],0);
break;
case 0x01:gpio_set_value(led_table[arg],1);
break;
default:break;
}
return 1;
}
static int myled_driver_open(struct inode *inode,struct file *file)
{
unsigned m =iminor(inode);
if(m>63) return -EINVAL;
printk("led test driver opened!\n");
return nonseekable_open(inode,file);
}
static int myled_driver_release(struct inode *inode,struct file *file)
{
printk("my test driver released!\n");
return 0;
}
static struct file_operations myled_driver_fops =
{
.owner =THIS_MODULE,
.ioctl =myled_driver_ioctl,
.open =myled_driver_open,
.release =myled_driver_release,
};
static int __init myled_driver_init(void) //执行insmod的时候调用次函数
{
int ret;
gpio_set_value(led_table[0],0);
gpio_set_value(led_table[1],0);
gpio_set_value(led_table[2],0);
gpio_set_value(led_table[3],0);
printk(KERN_ALERT "led modules is install\n");
ret=register_chrdev(major,NAME,&myled_driver_fops);
if(ret<0)
{
printk("unable to register myled driver!\n");
return ret;
}
return 0;
}
static void __exit myled_driver_exit(void)//执行rmmod的时候调用次函数
{
unregister_chrdev(major,NAME);
}
module_init(myled_driver_init);
module_exit(myled_driver_exit);
MODULE_AUTHOR("lxs()");
MODULE_DESCRIPTION("this is test modules used to test at91sam9261 led");
MODULE_LICENSE("Dual BSD/GPL");
二、测试程序
次测试程序完成了一个简单的四个led的流水,每敲一次回车就流水一次,输入字符q退出流水。
#include
#include
#include
#include
#define IOCTL_LED_ON 0
#define IOCTL_LED_OFF 1
int delayLoopCount=1000;
void usage(char *exename)
{
printf("Usage:\n");
printf(" %s
printf(" led_no = 1, 2, 3 or 4\n"); //led_on 表示从那个灯开始亮
}
void Delay(int time)
{
int i;
for(;time>0;time--)
for(i=0;i
}
int main(int argc, char **argv)
{
unsigned int led_no;
int fd = -1;
char w;
if (argc != 3)
goto err;
fd = open("/dev/leds", 0); // 打开设备
if (fd < 0) {
printf("Can't open /dev/leds\n");
return -1;
}
led_no = strtoul(argv[1], 0, 0) - 1; //将字符型转换为整形
if (led_no > 3)
goto err;
if (!strcmp(argv[2], "on"))
{
do
{
ioctl(fd, IOCTL_LED_ON, led_no); //亮
Delay(10000);
ioctl(fd, IOCTL_LED_OFF, led_no); //灭
led_no++;
ioctl(fd, IOCTL_LED_ON, led_no);
Delay(10000);
ioctl(fd, IOCTL_LED_OFF, led_no);
led_no++;
ioctl(fd, IOCTL_LED_ON, led_no);
Delay(10000);
ioctl(fd, IOCTL_LED_OFF, led_no);
led_no++;
ioctl(fd, IOCTL_LED_ON, led_no);
Delay(10000);
ioctl(fd, IOCTL_LED_OFF, led_no);
printf("please enter any but q,when enter q ,quit the select\n");//输入的字符为
q则结束循环
w=getchar();
Delay(10000);
led_no=0;
}while('q'!=w);
} else {
goto err;
}
close(fd);
return 0;
err:
if (fd > 0)
close(fd);
usage(argv[0]);
return -1;
}
上面的程序很粗糙,大家多多指点!