基于FL2440平台在linux内核上led驱动开发

一:开发说明
1:开发led驱动之前必须满足如下条件才能正式开发
FL2440开发板已经正确移植bootload与linux内核(包括根文件系统的移植),也就是说在开发该驱动之前你自己的开发板已经能启动bootload,并由bootload引导linux内核启动(包含根文件系统),在以上条件都满足的情况下,就可以愉快的开发led驱动。

2:本驱动是基于linux-3.0内核开发的,也就是开发板运行的linux内核必须与驱动所用的内核一致,并且内核必须是已经使用交叉编译器编译过的。

二:led驱动代码如下


#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 "hulu <[email protected]>
#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
#endif

#define DRV_MAJOR_VER 1
#define DRV_MINOR_VER 0
#define DRV_REVER_VER 0

#define DISABLE 0
#define ENABLE 1

#define GPIO_INPUT 0x00
#define GPIO_OUTPUT 0x01

//使用魔术转换产生独一无二的命令
define PLATDRV_MAGIC             0x60
#define LED_OFF _IO (PLATDRV_MAGIC, 0x18)
#define LED_ON _IO (PLATDRV_MAGIC, 0x19)

//fl2440上led使用的寄存器地址:GPBCON 0X56000010,GPBDAT 0X56000014,GPBUP 0X56000018
define S3C_GPB_BASE              0x56000010
#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;

//寄存器读写
#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;

//led硬件初始化,即寄存器初始化
static int s3c_hw_init(void)
{
    int          i;
    volatile unsigned long  gpb_con, gpb_dat, gpb_up;
    //申请一段长为S3C_GPB_LEN的内存
    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;
}

//led开关函数
static void turn_led(int which, unsigned int cmd)
{
    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)
{
    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);
    iounmap(s3c_gpb_membase);
}

//应用程序调用系统调用open通过设备文件名找到iNode结构,并且调用该函数
static int led_open(struct inode *inode, struct file *file)
{
    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)
{
    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;
}

//led控制函数,打开还是关闭led
static long led_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int which = (int)file->private_data;

    switch (cmd)
    {
        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;
}

//将led的各个操作用该结构体联系起来
static struct file_operations led_fops = 
{
    .owner = THIS_MODULE,
    .open = led_open,
    .release = led_release,
    .unlocked_ioctl = led_ioctl,
};

//安装驱动的时候调用该函数
static int __init s3c_led_init(void)
{
    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 */
    {
        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()) )
    {
        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);

    result = 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;
}

//卸载驱动默认调用此函数
tatic void __exit s3c_led_exit(void)
{
    dev_t devno = MKDEV(dev_major, dev_minor);

    s3c_hw_term();

    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");

三:编译该驱动使用的Makefile

ARCH = s3c2440
KERNEL_VER = linux-3.0
LINUX_SRC ?= /home/hulu/fl2440/kernel/$(KERNEL_VER)
PWD := $(shell pwd)

obj-m += s3c_led.o

modules:
     @make -C $(LINUX_SRC) M=$(PWD) modules
     @make clear

clear:
      @rm -f *.o *.cmd *.mod.c
      @rm -rf  *~ core .depend  .tmp_versions
      Module.symvers modules.order -f
      @rm -f .*ko.cmd .*.o.cmd .*.o.d

clean: clear
      @rm -f  *.ko

四:基于fl2440加载驱动
由于在该驱动文件中使用的是动态获取主设备号:

动态获取主设备号 result=alloc_chrdev_region(&devno, dev_minor, dev_count, DEV_NAME); dev_major = MAJOR(devno);

所以在安装驱动之前并不知道内核给我们分配哪一个主设备号,故只有在安装该驱动之后才能在/dev/目录下创建设备节点。因此,为了加载一个使用动态获取主设备号的设备驱动程序,对insmod的调用可替换为一个简单的脚本,该脚本在调用insmod之后读取/proc/devices以获得新分配的主设备号,然后创建对应的设备文件。
该脚本文件如下:

  #!/bin/sh
  insmod s3c_ied.ko

  major=`cat /proc/devices | grep led | awk '{print $1}'`

  //创建设备节点
  mknod -m 755 /dev/led0 c $major 0

  mknod -m 755 /dev/led1 c $major 1

  mknod -m 755 /dev/led2 c $major 2

  mknod -m 755 /dev/led3 c $major 3

驱动安装成功如下:

基于FL2440平台在linux内核上led驱动开发_第1张图片

五:编写测试文件led_test.c
vim led_test.c

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

#define buffer_num 20
#define PLATDRV_MAGIC 0x60
#define LED_ON _IO(PLATDRV_MAGIC, 0x19)
#define LED_OFF _IO(PLATDRV_MAGIC, 0x18)
int main(void)
{
         int  j,i;
         int   fd;
         char  buffer[buffer_num];

         for(i=0;i<4;i++)
          {
            snprintf(buffer,sizeof(buffer),"/dev/led%d",i);
              fd=open(buffer,O_RDWR);
              if(fd<0)
              {    printf("open error");
                   return -1;
              }
              ioctl(fd,LED_ON);

              close(fd);
          }
       return  0;
}

然后用自己的交叉编译器arm-linux-gcc编译该c文件会得到一个led的a.out文件,使用tftp服务器把它送到开发板,然后使用chmod 755 a.out (解权限)
在开发板下执行./a.out即可

这样就可以在自己的开发扳上看到四个led灯同时亮,哈哈!以上的代码编写折腾我好久,虽然已经编写成功,但可能还存在不足之处,欢迎各位指点,同时大家有什么疑问直接留言给我,咱们一起探讨!

你可能感兴趣的:(内核,驱动开发)