Led test今天完成了嵌入式linux的第一个驱动的编写和测试,虽然是个简单的程序,但是麻雀虽小,五脏俱全,希望可以给刚开始接触驱动编写的人一些提示,共同进步。
源代码:分析如下:下面是我的驱动程序:#include <linux/config.h>//配置头文件#include <linux/kernel.h>/*内核头文件,作为系统核心的一部分,设备驱动程序在申请和释放内存时,不是调用malloc和free,而是调用kmalloc和kfree*/#include <linux/sched.h>//调度,进程睡眠,唤醒,中断申请,中断释放#include <linux/timer.h>//时钟头文件#include <linux/init.h>//用户定义模块初始函数名需引用的头文件#include <linux/module.h>//模块加载的头文件#include <asm/hardware.h>#include <asm/arch/S3C2440.h> //这个是2440的寄存器头文件,asm/srch只是个链接//实际根据自己的情况查找,一般是../../linux2.*.*/include/asm/arch-s3c2440里编译器//自己会查询链接,以前不知道,找了半天// GPIO_LED DEVICE MAJOR#define GPIO_LED_MAJOR 97 //定义主设备号//define LED STATUS 我的板子 LED在GPB0 与GPB1 处大家根据自己情况改#define LED_ON 0 //定义LED灯的状态开#define LED_OFF 1 //// ------------------- READ ------------------------ 这个前面要加static 否则警告static ssize_t GPIO_LED_read (struct file * file ,char * buf, size_t count, loff_t * f_ops){return count;}// ------------------- WRITE -----------------------static ssize_t GPIO_LED_write (struct file * file ,const char * buf, size_t count, loff_t * f_ops){return count;}// ------------------- IOCTL -----------------------static ssize_t GPIO_LED_ioctl (struct inode * inode ,struct file * file, unsigned int cmd, long data) //这个函数实现了led灯亮灭的接口{switch (cmd){case LED_ON : { GPBDAT =0x01; break;} //根据自己情况修改一个亮一个灭case LED_OFF: { GPBDAT =0x02; break;} //交替闪烁default :{printk ("lcd control : no cmd run [ --kernel-- ]\n"); return (-EINVAL);} }return 0;}// ------------------- OPEN ------------------------static ssize_t GPIO_LED_open (struct inode * inode ,struct file * file) {MOD_INC_USE_COUNT;return 0;}// ------------------- RELEASE/CLOSE ---------------static ssize_t GPIO_LED_release (struct inode * inode ,struct file * file){MOD_DEC_USE_COUNT;return 0;}// -------------------------------------------------struct file_operations GPIO_LED_ctl_ops ={open: GPIO_LED_open, //这段赋值代码必须放在接口函数申明之后read: GPIO_LED_read, //否则编译不过去write: GPIO_LED_write,ioctl: GPIO_LED_ioctl,release: GPIO_LED_release,};// ------------------- INIT ------------------------static int GPIO_LED_CTL_init(void){int ret = -ENODEV;printk("--------------------------------------------\n\n");GPBCON = 0x0005; // 设置端口为I/O输出模式GPBUP = 0xff; // 关闭上拉功能GPBDAT = 0xf; //初始值为高电平熄灭LED灯ret = register_chrdev(GPIO_LED_MAJOR, "gpio_led_ctl",&GPIO_LED_ctl_ops);//这个驱动注册函数必须放在复制接口的那个结构体之后if( ret < 0 ){printk (" S3C2410: init_module failed with %d\n", ret);return ret;}else{printk("S3C2410 gpio_led_driver register success!!! \n");}return ret;}static int __init S3C2410_GPIO_LED_CTL_init(void)int ret = -ENODEV;ret = GPIO_LED_CTL_init();if (ret)return ret;return 0;}static void __exit cleanup_GPIO_LED_ctl(void){unregister_chrdev (GPIO_LED_MAJOR, "gpio_led_ctl" );}module_init(S3C2410_GPIO_LED_CTL_init);module_exit(cleanup_GPIO_LED_ctl);完了编译这个驱动函数makefile如下:################################################## config# where the kernel sources are located 这是我的内核头文件的路径根据自己的修改KERNEL_DIR := ../../../linux-2.4.20################################################## some magic for using linux kernel settings# when compiling module(s)# for new-style kernel Makefiles (2.4)export-objs := led.o //要编译好的对象list-multi :=obj-m := led.ohere:(cd $(KERNEL_DIR); make SUBDIRS=$(PWD) modules) //makeclean:-rm -f *.o .*.o.flags *~include $(KERNEL_DIR)/Rules.make //make的规则根据自己的情况修改编译好以后,接下来就是测试是否可以使用驱动了测试函数如下:#include <stdio.h>#include <string.h>#include <stdlib.h>#include <fcntl.h> // open() close()#include <unistd.h> // read() write()#define DEVICE_NAME "/dev/gpio_led_ctl" //这是设备驱动名字,一会要建立//define LED STATUS#define LED_ON 0#define LED_OFF 1//------------------------------------- main---------------------------------------------int main(void){int fd;int ret;char *i;printf("\nstart gpio_led_driver test\n\n");fd = open(DEVICE_NAME, O_RDWR);printf("fd = %d\n",fd);if (fd == -1){printf("open device %s error\n",DEVICE_NAME);}else{while(1){ ioctl(fd,LED_OFF); //GPB0亮 GPB1灭sleep(1); //等待1秒再做下一步操作 ioctl(fd,LED_ON); //反过来sleep(1);}// closeret = close(fd);printf ("ret=%d\n",ret);printf ("close gpio_led_driver test\n");}return 0;}// end mainmakefile如下:CROSS = /opt/host/armv4l/bin/armv4l-unknown-linux-//交叉编译工具路径根据自己修改CC = $(CROSS)gccAR = $(CROSS)arSTRIP = $(CROSS)stripEXEC = test //生成的可执行文件OBJS = test.oall: $(EXEC)$(EXEC): $(OBJS)$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBM) $(LDLIBS) $(LIBGCC) -lm //编译clean:-rm -f $(EXEC) *.elf *.gdb *.o接下来就是最后的调试了:首先把生成的led.o和test载到板子上然后:insmod led.o //成功的话,会打印sucesslsmod //查看内核里面是否已经有led驱动模块mknod /dev/gpio_led_ctl c 97 1 //新建LED的测试设备节点,给test.c使用// /dev/gpio_led_ctl 是打开的设备名称,要和测试代码匹配// c代表字符设备// 97是主设备好,与驱动程序匹配 1是从设备号只有一个选1最后执行:./test //成功了会打印一些信息这是你会看到你的板子上 LED交替亮灭间隔1s补上一点“/opt/FriendlyARM/mini2440/linux-2.6.29/arch/arm/plat-s3c24xx/include /plat/map.h这个是linux2.6.69内核下的24X0寄存器定义头文件,里面定义了特殊功能寄存器的PA向VA的映射。