本文主要是介绍linux驱动开发FL2440开发板-LED驱动及其测试程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
———————————————————————————————————————
交叉编译器环境:arm-linux-gcc-4.5.4
开发板平台: FL2440
Linux内核版本: linux-3.0
开发模块: LED
邮箱:leiyuxing205@gmail.com
———————————————————————————————————————
1.1vim编写LED驱动代码
[leiyuxing@centos6 ~]$ mkdir LED[leiyuxing@centos6 LED]$ vim s3c_led
/********************************************************************************** Copyright: (C) 2016 leiyuxing <leiyuxing@gmail.com>* All rights reserved.** Filename: s3c_led.c* Description: This file** Version: 1.0.0(07/25/2016~)* Author: leiyuxing <leiyuxing@gmail.com>* ChangeLog: 1, Release initial version on "07/25/2016 16:46:40 PM"*********************************************************************************/
#include <linux/module.h> /* Every Linux kernel module must include this head */
#include <linux/init.h> /* Every Linux kernel module must include this head */
#include <linux/kernel.h> /* printk() */
#include <linux/fs.h> /* struct fops */
#include <linux/errno.h> /* error codes */
#include <linux/cdev.h> /* cdev_alloc() */
#include <asm/io.h> /* ioremap() */
#include <linux/ioport.h> /* request_mem_region() */#include <asm/ioctl.h> /* Linux kernel space head file for macro _IO() to generate ioctl command */
#ifndef __KERNEL__
#include <sys/ioctl.h> /* User space head file for macro _IO() to generate ioctl command */
#endif
//#include <linux/printk.h> /* Define log level KERN_DEBUG, no need include here */#define DRV_AUTHOR "leiyuxing <leiyuxing@gmail.com>"
#define DRV_DESC "S3C24XX LED driver"#define DEV_NAME "led" //定义设备名称
#define LED_NUM 4 //定义设备数量/* Set the LED dev major number */
//#define LED_MAJOR 79
#ifndef LED_MAJOR
#define LED_MAJOR 0 //定义主设备号为0,一般这个定义设备号是固定不可用的,但是 这为自动分配主设备号。#endif#define DRV_MAJOR_VER 1
#define DRV_MINOR_VER 0
#define DRV_REVER_VER 0#define DISABLE 0//出现错误定义为0
#define ENABLE 1//出现错误定义为1(两个错误这样定义应该是不同函数错误返回值不同)#define GPIO_INPUT 0x00//宏定义 GPIO输入模式用0代替
#define GPIO_OUTPUT 0x01//宏定义 GPIO输出模式用1代替#define PLATDRV_MAGIC 0x60 //魔术字
#define LED_OFF _IO (PLATDRV_MAGIC, 0x18)//_IO是Core_cm3.h中被重定义:#define _IO volatile的作用就是指示因优化而省略此指令
#define LED_ON _IO (PLATDRV_MAGIC, 0x19)#define S3C_GPB_BASE 0x56000010 //GPB基地址及偏移地址
#define GPBCON_OFFSET 0
#define GPBDAT_OFFSET 4
#define GPBUP_OFFSET 8
#define S3C_GPB_LEN 0x10 /* 0x56000010~0x56000020 */int led[LED_NUM] = {5,6,8,10}; /* Four LEDs use GPB5,GPB6,GPB8,GPB10 */static void __iomem *s3c_gpb_membase; //定义iomem*类型变量#define s3c_gpio_write(val, reg) __raw_writel((val), (reg)+s3c_gpb_membase)//虚拟地址读取到物理地址
#define s3c_gpio_read(reg) __raw_readl((reg)+s3c_gpb_membase)int dev_count = ARRAY_SIZE(led); //设备总数
int dev_major = LED_MAJOR; //主设备
int dev_minor = 0; //次设备
int debug = DISABLE;static struct cdev *led_cdev; //定义结构体指针
static int s3c_hw_init(void) //初始化函数
{int i;volatile unsigned long gpb_con, gpb_dat, gpb_up;if(!request_mem_region(S3C_GPB_BASE, S3C_GPB_LEN, "s3c2440 led"))//判断是否成功申请一段内存空间{return -EBUSY;}if( !(s3c_gpb_membase=ioremap(S3C_GPB_BASE, S3C_GPB_LEN)) )//判断虚拟地址到物理地址的映射{release_mem_region(S3C_GPB_BASE, S3C_GPB_LEN);return -ENOMEM;}for(i=0; i<dev_count; i++){/* Set GPBCON register, set correspond GPIO port as input or output mode */gpb_con = s3c_gpio_read(GPBCON_OFFSET);gpb_con &= ~(0x3<<(2*led[i])); /* Clear the currespond LED GPIO configure register */gpb_con |= GPIO_OUTPUT<<(2*led[i]); /* Set the currespond LED GPIO as output mode */s3c_gpio_write(gpb_con, GPBCON_OFFSET);/* Set GPBUP register, set correspond GPIO port pull up resister as enable or disable */gpb_up = s3c_gpio_read(GPBUP_OFFSET);//gpb_up &= ~(0x1<<led[i]); /* Enable pull up resister */gpb_up |= (0x1<<led[i]); /* Disable pull up resister */s3c_gpio_write(gpb_up, GPBUP_OFFSET);/* Set GPBDAT register, set correspond GPIO port power level as high level or low level */gpb_dat = s3c_gpio_read(GPBDAT_OFFSET);//gpb_dat &= ~(0x1<<led[i]); /* This port set to low level, then turn LED on */gpb_dat |= (0x1<<led[i]); /* This port set to high level, then turn LED off */s3c_gpio_write(gpb_dat, GPBDAT_OFFSET);}return 0;
}static void turn_led(int which, unsigned int cmd) //led函数的操作
{volatile unsigned long gpb_dat;gpb_dat = s3c_gpio_read(GPBDAT_OFFSET);if(LED_ON == cmd){gpb_dat &= ~(0x1<<led[which]); /* Turn LED On */}else if(LED_OFF == cmd){gpb_dat |= (0x1<<led[which]); /* Turn LED off */}s3c_gpio_write(gpb_dat, GPBDAT_OFFSET);
}static void s3c_hw_term(void) //清除led操作函数
{int i;volatile unsigned long gpb_dat;for(i=0; i<dev_count; i++){gpb_dat = s3c_gpio_read(GPBDAT_OFFSET);gpb_dat |= (0x1<<led[i]); /* Turn LED off */s3c_gpio_write(gpb_dat, GPBDAT_OFFSET);}release_mem_region(S3C_GPB_BASE, S3C_GPB_LEN); //释放I/O内存iounmap(s3c_gpb_membase); //iounmap函数取消iounmap()做的映射
}static int led_open(struct inode *inode, struct file *file)//led开启函数
{int minor = iminor(inode); //获取次设备号file->private_data = (void *)minor; //保存到一个全局变量printk(KERN_DEBUG "/dev/led%d opened.\n", minor);return 0;
}static int led_release(struct inode *inode, struct file *file) //led关闭函数
{printk(KERN_DEBUG "/dev/led%d closed.\n", iminor(inode));return 0;
}static void print_help(void) //帮助信息
{printk("Follow is the ioctl() commands for %s driver:\n", DEV_NAME);//printk("Enable Driver debug command: %u\n", SET_DRV_DEBUG);printk("Turn LED on command : %u\n", LED_ON);printk("Turn LED off command : %u\n", LED_OFF);return;
}static long led_ioctl(struct file *file, unsigned int cmd, unsigned long arg) //ioctl函数
{int which = (int)file->private_data; //接收次设备号switch (cmd) //判断led开关{case LED_ON:turn_led(which, LED_ON);break;case LED_OFF:turn_led(which, LED_OFF);break;default:printk(KERN_ERR "%s driver don't support ioctl command=%d\n", DEV_NAME, cmd);print_help();break;}return 0;
}static struct file_operations led_fops = //fops函数
{.owner = THIS_MODULE,.open = led_open,.release = led_release,.unlocked_ioctl = led_ioctl,
};static int __init s3c_led_init(void) //init函数
{int result;dev_t devno;if( 0 != s3c_hw_init() ) //做初始化的判断{printk(KERN_ERR "s3c2440 LED hardware initialize failure.\n");return -ENODEV;}/* Alloc the device for driver */if (0 != dev_major) /* Static */ //静态分配主,次设备号转化为dev_t设备编号{devno = MKDEV(dev_major, 0);result = register_chrdev_region (devno, dev_count, DEV_NAME);}else{result = alloc_chrdev_region(&devno, dev_minor, dev_count, DEV_NAME);//动态分配主,次设备号dev_major = MAJOR(devno);}/* Alloc for device major failure */if (result < 0) //分配失败则打印{printk(KERN_ERR "S3C %s driver can't use major %d\n", DEV_NAME, dev_major);return -ENODEV;
}
printk(KERN_DEBUG "S3C %s driver use major %d\n", DEV_NAME, dev_major);if(NULL == (led_cdev=cdev_alloc()) ) //led_cddv分配失败打印{printk(KERN_ERR "S3C %s driver can't alloc for the cdev.\n", DEV_NAME);unregister_chrdev_region(devno, dev_count);return -ENOMEM;}led_cdev->owner = THIS_MODULE; //分配成功,进行参数定义cdev_init(led_cdev, &led_fops); //链接led_cdev和led_fopsresult = cdev_add(led_cdev, devno, dev_count);//添加内存if (0 != result) //出错打印{printk(KERN_INFO "S3C %s driver can't reigster cdev: result=%d\n", DEV_NAME, result);goto ERROR;}printk(KERN_ERR "S3C %s driver[major=%d] version %d.%d.%d installed successfully!\n",DEV_NAME, dev_major, DRV_MAJOR_VER, DRV_MINOR_VER,DRV_REVER_VER); //打印成功return 0;ERROR: //内存出错执行printk(KERN_ERR "S3C %s driver installed failure.\n", DEV_NAME);cdev_del(led_cdev);unregister_chrdev_region(devno, dev_count);return result;
}
static void __exit s3c_led_exit(void) //led退出函数
{dev_t devno = MKDEV(dev_major, dev_minor);s3c_hw_term(); //调用清除led操作函数cdev_del(led_cdev); //撤销unregister_chrdev_region(devno, dev_count);printk(KERN_ERR "S3C %s driver version %d.%d.%d removed!\n",DEV_NAME, DRV_MAJOR_VER, DRV_MINOR_VER,DRV_REVER_VER);return ;
}/* These two functions defined in <linux/init.h> */
module_init(s3c_led_init);
module_exit(s3c_led_exit);module_param(debug, int, S_IRUGO);
module_param(dev_major, int, S_IRUGO);MODULE_AUTHOR(DRV_AUTHOR);
MODULE_DESCRIPTION(DRV_DESC);
MODULE_LICENSE("GPL");
以上就是我们的LED相关驱动模块函数。
先是调用module_init函数,在调用s3c_led_init函数,这个函数会调用 s3c_hw_init进行初始化;接着s3c_led_init函数会进行分配主次设备号,按照传入的参数判断是动态分配或者是静态分配;再者是分配结构体,如果链接owner成功就可接着连接fops;然后是ON 和OFF的实现功能,使用ioctl与装入卸载次设备号驱动以及模块信息和fops中与主次设备号结构体链接,ioctl通过传参来实现相应的功能;最后进行撤销。
1.2编写Makefile
[leiyuxing@centos6 LED]$ vim Makefile
C=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc
KDIR?=/home/leiyuxing/fl2440/kernel/linux-3.0
obj-m:=s3c_led.odefault:$(MAKE) -C $(KDIR) M=`pwd` modulesmake cleanclean:rm -f *.o *mod.c *.order *.symvers
make会生 .ko 文件 这就是我们所需要加载的驱动模块。
[leiyuxing@centos6 LED]$ lsMakefile s3c_led.c s3c_led.ko
2.1编写LED灯程序,点亮LED灯
[leiyuxing@centos6 LED_test]$ vim led_test.c
/********************************************************************************** Copyright: (C) 2016 leiyuxing <leiyuxing@gmail.com>* All rights reserved.** Filename: led_test.c* Description: This file* * Version: 1.0.0(07/25/2016)* Author: leiyuxing <leiyuxing@gmail.com>* ChangeLog: 1, Release initial version on "07/25/2016 19:36:22 PM"* ********************************************************************************/#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdarg.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>#define LED_NUM 4
#define DEVNAME_LEN 10#define PLATDRV_MAGIC 0x60
#define LED_OFF _IO (PLATDRV_MAGIC, 0x18)
#define LED_ON _IO (PLATDRV_MAGIC, 0x19)int main(int argc, char **argv)
{int fd[LED_NUM];int i;int j;char devname[DEVNAME_LEN] = {0};for(i = 0; i < LED_NUM; i++){snprintf(devname, sizeof(devname), "/dev/led%i",i);fd[i] = open(devname, O_RDWR,755);if(fd[i] < 0){printf("Can not open %s: %s", devname, strerror(errno));for(j = 0; j < i; j++){if(fd[j] > 0)close(fd[j]);}return -1;}}while(1){for(i = 0; i < LED_NUM; i++){ioctl(fd[i], LED_ON);sleep(1);ioctl(fd[i], LED_OFF);}}for(i = 0; i < LED_NUM; i++){close(fd[i]);}return 0;
}
[leiyuxing@centos6 LED_test]$/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc led_test.c[leiyuxing@centos6 LED_test]$ lsa.out led_test.c
应用程序使用交叉编译器编译得到a.out。
在我们的开发板上将 .ko文件和 a.out 文件 tftp 过去。这是板子上会有我们这两个文件,注意这两个文件的权限需要改成可执行。
insmod 文件名.ko 就可以看到我们的主设备号。 如major=251
在开发板上执行的操作:
>: tftp -gr s3c_led.ko 192.168.1.2s3c_led.ko 100% |*******************************| 6210 0:00:00 ETA>: tftp -gr a.out 192.168.1.2a.out 100% |*******************************| 6007 0:00:00 ETA>: insmod s3c_led.koS3C led driver[major=251] version 1.0.0 installed successfully!>: mknod /dev/led0 c 251 0 //手动创建结点>: mknod /dev/led1 c 251 1>: mknod /dev/led2 c 251 2>: mknod /dev/led3 c 251 3>: ls -l dev/led* //查看我们的设备crw-r--r-- 1 root root 251, 0 Dec 31 17:20 dev/led0crw-r--r-- 1 root root 251, 1 Dec 31 17:20 dev/led1crw-r--r-- 1 root root 251, 2 Dec 31 17:21 dev/led2crw-r--r-- 1 root root 251, 3 Dec 31 17:21 dev/led3>: ll a.out-rw-r--r-- 1 root root 6007 Dec 31 17:17 a.out>: chmod 777 a.out>: ./a.out
最后 ./a.out LED灯就能在开发板上跑起来,这就可以称为跑马灯了
出现问题:
问题一:
没有权限
解决方法:
>: ll a.out-rw-r--r-- 1 root root 6007 Dec 31 17:17 a.out>: chmod 777 a.out
这篇关于linux驱动开发FL2440开发板-LED驱动及其测试程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!