字元裝置驅動-LED驅動
嵌入式Linux版本:linux2.6.24
驅動源碼:
//=================================================
//version: linux2.6.24
//led_pin:GB5/GB6/GB8/GB10
//=================================================
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <asm/irq.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#define DEVICE_NAME "leds" //定義裝置名
#define LED_MAJOR 231 //手動定義主裝置号
#define LED_MINOR 0
//定義要操作的裝置,把每一個led作為結構體的一個成員
static unsigned long led_table [] = {
S3C2410_GPB5,
S3C2410_GPB6,
S3C2410_GPB8,
S3C2410_GPB10,
};
//對裝置進行設定,結構體的每一個成員是對對應led的設定
static unsigned int led_cfg_table [] = {
S3C2410_GPB5_OUTP, //0x01<<10 defined in refg-gpio.h
S3C2410_GPB6_OUTP,
S3C2410_GPB8_OUTP,
S3C2410_GPB10_OUTP,
};
//裝置驅動程式中對裝置的I/O通道進行管理的函數,用來實作對led的操作
static int s3c2410_leds_ioctl(struct inode *inode,
struct file *file, unsigned int cmd, unsigned long arg)
{
switch(cmd) {
case 0:
case 1:
if (arg > 4)
return -EINVAL;
s3c2410_gpio_setpin(led_table[arg], !cmd);
return 0;
default:
return -EINVAL;
}
}
//
static struct file_operations s3c2410_leds_fops = {
.owner = THIS_MODULE,
.ioctl = s3c2410_leds_ioctl,
};
struct cdev *my_cdev;
struct class *my_class;
//=========================================
// 子產品加載函數
//========================================
static int __init s3c2410_leds_init(void)
{
int err, i;
int devno = MKDEV(LED_MAJOR, LED_MINOR);//Get device number .
my_cdev = cdev_alloc();
cdev_init(my_cdev, &s3c2410_leds_fops);
my_cdev->owner = THIS_MODULE;
err = cdev_add(my_cdev, devno, 1);
if (err != 0)
printk("led device register failed!\n");
my_class = class_create(THIS_MODULE, "led_class");
if(IS_ERR(my_class)) {
printk("Err: failed in creating class.\n");
return -1;
}
class_device_create(my_class, NULL, devno, NULL, DEVICE_NAME "%d", LED_MINOR );
//======led端口配置,數組不要超長,否則會導緻insmod時segment fault========
for (i = 0; i < 4; i++) {
s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
s3c2410_gpio_setpin(led_table[i], 1);
}
printk("Led_Driver insmod success!\r\n");
return 0;
}
//========================================
// 子產品解除安裝函數
//=======================================
static void __exit s3c2410_leds_exit(void)
{
cdev_del(my_cdev);
class_device_destroy(my_class, MKDEV(LED_MAJOR, LED_MINOR));
class_destroy(my_class);
printk("Led_Driver removed success!\r\n");
}
module_init(s3c2410_leds_init);
module_exit(s3c2410_leds_exit);
MODULE_LICENSE("GPL");
工程管理Makefile:
#Makefile for s3c2440_leds.c
obj-m :=led_driver.o
KERNELDIR ?=/home/zhw123/linux-2.6.24
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -r led_driver.mod.c led_driver.mod.o led_driver.o Module.symvers
disclean:
rm -r led_driver.mod.c led_driver.mod.o led_driver.o led_driver.ko Module.symvers
測試函數:
#include "stdio.h"
#include "unistd.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"
int main(void)
{
int on=1;
int led;
int fd;
fd=open("/dev/leds",0); //打開裝置檔案
if(fd<0)
{
perror("open device leds");
exit(1);
}
printf("leds test show,press ctrl+c to exit \n");
while(1)
{
for(led=0;led<4;led++)
{
ioctl(fd,on,led); //反轉LED
usleep(50000);
}
on=!on;
}
close(fd);
return 0;
}
測試:
1.将Makefile和驅動檔案放在同一目錄下,make産生insmod led_driver.ko檔案。
2.開發闆上:insmod led_driver.ko
列印出:Led_Driver insmod success!
lsmod檢視裝置驅動安裝情況
3.開發闆上:在/mnt/dev下面mknod leds c 231 0 建立裝置節點
4.開發闆上:./led
LED開始依次閃爍
5.開發闆上:rmmod led_driver.ko解除安裝子產品
列印出:Led_Driver removed success!