Linux驱动:LED驱动测试

时间:2022-10-11 23:37:07

Linux驱动:LED驱动测试


本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.


环境:

主机:Fedora12

目标板:MINI6410

目标板LINUX内核版本:2.6.38


实现功能:

驱动目标板4个LED.


说明:

LED与6410引脚对应连接:

LED1---GPK4

LED2---GPK5

LED3---GPK6

LED4---GPK7


驱动源代码:

led_driver.c:

#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <asm/irq.h>
//#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>
#include <linux/major.h>

#include <mach/map.h>
#include <mach/regs-clock.h>
#include <mach/regs-gpio.h>

#include <plat/gpio-cfg.h>
#include <mach/gpio-bank-e.h>
#include <mach/gpio-bank-k.h>
#include <mach/gpio-bank-h.h>
#include <mach/gpio-bank-n.h>
#include <mach/gpio-bank-l.h>
#include <mach/gpio-bank-p.h>

#include <linux/device.h>

#define DEVICE_NAME "led_driver"
#define T_MAJORS	700

static struct cdev fun_cdev;
static dev_t dev;
static struct class    *led_class;

//功能:初始化IO
static void init_led(void)
{
	unsigned temp;

	//GPK4-7设置为输出
	temp = readl(S3C64XX_GPKCON);
	temp &= ~((0xf << 4) | (0xf << 5) | (0xf << 6) | (0xf<< 7));
	temp |= (1 << 16) | (1 << 20) | (1 << 24) | (1 << 28);
	writel(temp, S3C64XX_GPKCON);
}

//功能:ioctl操作函数
//返回值:成功返回0
static long led_driver_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	unsigned int temp = 0;

	printk (DEVICE_NAME"\tjdh:led_driver cmd=%d arg=%d\n",cmd,arg);

	temp = readl(S3C64XX_GPKDAT);
	if (cmd == 0)
	{
		temp &= ~(1 << (arg + 3));
	}
	else
	{
		temp |= 1 << (arg + 3);
	}
	writel(temp,S3C64XX_GPKDAT);
	
	return 0;
}

static struct file_operations io_dev_fops = {
	.owner = THIS_MODULE,
	.unlocked_ioctl = led_driver_ioctl,
};

static int __init dev_init(void)
{
	int ret;
	unsigned temp;

	init_led();

	dev = MKDEV(T_MAJORS,0);
	cdev_init(&fun_cdev,&io_dev_fops);
	ret = register_chrdev_region(dev,1,DEVICE_NAME);
	if (ret < 0) return 0;
	ret = cdev_add(&fun_cdev,dev,1);
	if (ret < 0) return 0;

	printk (DEVICE_NAME"\tjdh:led_driver initialized!!\n");

	led_class = class_create(THIS_MODULE, "led_class1"); 
	if (IS_ERR(led_class)) 
	{ 
		printk(KERN_INFO "create class error\n"); 
		return -1; 
	} 
	device_create(led_class, NULL, dev, NULL, "led_driver"); 

	return ret;
}

static void __exit dev_exit(void)
{
	unregister_chrdev_region(dev,1);

    	device_destroy(led_class, dev); 
    	class_destroy(led_class);
}

module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("JDH");

用户程序:

test_led.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "stdio.h"
#include "sys/types.h"
#include "sys/ioctl.h"
#include "stdlib.h"
#include "termios.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "sys/time.h"

#include <string.h>  
#include <stdlib.h> 

int main(int argc, char** argv)
{
	int fd;
	int led_num = 0;
	int led_value = 0;	
	char temp[10];

	fd = open("/dev/led_driver",0);
	if (fd < 0)
	{
		perror("open led_driver error");
		exit(1);
	}
	
	strcpy(temp,argv[1]);  
      led_num = atoi(temp);  
	
	strcpy(temp,argv[2]);  
      led_value = atoi(temp); 

	printf("%d %d\n",led_num,led_value);

	ioctl(fd,led_num,led_value);

	close(fd);
	
	return 0;
}


测试说明:

将编译所得的可执行文件test_led上传到开发板

运行:

./test_led 0 1 打开LED1

./test_led 1 1 熄灭LED1