当前位置:首页 > 芯闻号 > 充电吧
[导读]写一个LED点灯驱动: ①框架(在Linux字符设备驱动开发基础已经搭建好) ②完善:硬件的操作 a.看原理图-确定引脚 b.看2440数据手册 c.写代码 写代码注意:裸机

写一个LED点灯驱动: ①框架(在Linux字符设备驱动开发基础已经搭建好) ②完善:硬件的操作

a.看原理图-确定引脚
b.看2440数据手册
c.写代码

写代码注意:裸机代码用的是物理地址PA,驱动代码用的是虚拟地址VA<虚拟地址需要用ioremap函数映射>

驱动程序first_drv.c

#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include 

static struct class *firstdrv_class;
static struct class_device *firstdrv_class_dev;
/*第一步 根据原理图定义操作寄存器*/
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;

static int first_drv_open(struct inode *inode,struct file *file)
{
    printk("first_drv_openn");
    //配置GPF456为输出引脚
    /*第四步 配置Open函数 将引脚配置为输出模式*/
    *gpfcon &= ~((0x3<<4*2) | (0x3<<5*2) | (0x3<<6*2));
    *gpfcon |=  ((1<<4*2) | (1<<5*2) | (1<<6*2));
    return 0;
}

static ssize_t first_drv_write(struct file *file,const char __user *buf, size_t count,loff_t *ppos)
{
    int val;
    printk("first_drv_writen");
    /*第五步 将用户空间的操作内容传给内核*/
    copy_from_user(&val,buf,count);
    if(1 == val)
    {
        *gpfdat &= ~((1<<4) | (1<<5) | (1<<6));
    }
    else
    {
        *gpfdat |= ((1<<4) | (1<<5) | (1<<6));
    }
    return 0;
}

static struct file_operations first_drv_fops = {
    .owner = THIS_MODULE,   /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open  = first_drv_open,
    .write = first_drv_write,
};

int major;
static int first_drv_init(void)
{
    major = register_chrdev(0,"first_drv",&first_drv_fops);// 注册,告诉内核
    //printk("first_drv_initn");
    firstdrv_class = class_create(THIS_MODULE, "firstdrv");
    firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */
    /*第二步 根据数据手册 入口函数映射VA*/
    gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);
    gpfdat = gpfcon + 1;
    return 0;
}

static void first_drv_exit(void)
{
    unregister_chrdev(major,"first_drv");//卸载

    class_device_unregister(firstdrv_class_dev);
    class_destroy(firstdrv_class);
    /*第三步 出口函数去掉映射*/
    iounmap(gpfcon);
}

module_init(first_drv_init);
module_exit(first_drv_exit);

MODULE_LICENSE("GPL");  

驱动测试程序firstdrvtest.c

#include   
#include   
#include   
#include   

/* firstdrvtest on 
 * firstdrvtest off 
 */

int main(int argc, char **argv)
{
    int fd;
    int val = 1;
    fd = open("/dev/xyz",O_RDWR);
    if(fd < 0)
    {
        printf("can't open!n");
    }
    if (argc != 2)
    {
        printf("Usage : n");
        printf("%s n", argv[0] );
        return 0;
    }
    if (strcmp(argv[1],"on") == 0)
    {
        val = 1 ;
    }
    else
    {
        val = 0 ;
    }
    write(fd,&val,4);
    return 0;
}
测试:

make -编译驱动程序
arm-linux-gcc -o firstdrvtest firstdrvtest.c -编译驱动测试程序
cp first_drv.ko /work/nfs_root/czg -拷贝至nfs文件夹
cp firstdrvtest /work/nfs_root/czg -拷贝至nfs文件夹
rmmod first_drv -卸载先前驱动//cat proc/devices 查看
insmod first_drv.ko -加载驱动
./firstdrvtest -测试驱动

实现对硬件的多种情况操作时:

(1)根据修改传入参数,执行多种操作
(2)通过此设备号的不同来实现,执行不同的硬件操作

驱动程序myleds.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define DEVICE_NAME     "leds"  /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */
#define LED_MAJOR       231     /* 主设备号 */


static struct class *leds_class;
static struct class_device  *leds_class_devs[4];


/* bit0<=>D10, 0:亮, 1:灭 
 *  bit1<=>D11, 0:亮, 1:灭 
 *  bit2<=>D12, 0:亮, 1:灭 
 */ 
static char leds_status = 0x0;  
static DECLARE_MUTEX(leds_lock); // 定义赋值

//static int minor;
static unsigned long gpio_va;

#define GPIO_OFT(x) ((x) - 0x56000000)
#define GPFCON  (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000050)))
#define GPFDAT  (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000054)))


/* 应用程序对设备文件/dev/leds执行open(...)时,
 * 就会调用s3c24xx_leds_open函数
 */
static int s3c24xx_leds_open(struct inode *inode, struct file *file)
{
    int minor = MINOR(inode->i_rdev); //MINOR(inode->i_cdev);

    switch(minor)
    {
        case 0: /* /dev/leds */
        {
            // 配置3引脚为输出
            //s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);
            GPFCON &= ~(0x3<<(4*2));
            GPFCON |= (1<<(4*2));

            //s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);
            GPFCON &= ~(0x3<<(5*2));
            GPFCON |= (1<<(5*2));

            //s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);
            GPFCON &= ~(0x3<<(6*2));
            GPFCON |= (1<<(6*2));

            // 都输出0
            //s3c2410_gpio_setpin(S3C2410_GPF4, 0);
            GPFDAT &= ~(1<<4);

            //s3c2410_gpio_setpin(S3C2410_GPF5, 0);
            GPFDAT &= ~(1<<5);
            //s3c2410_gpio_setpin(S3C2410_GPF6, 0);
            GPFDAT &= ~(1<<6);

            down(&leds_lock);
            leds_status = 0x0;
            up(&leds_lock);

            break;
        }

        case 1: /* /dev/led1 */
        {
            s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);
            s3c2410_gpio_setpin(S3C2410_GPF4, 0);

            down(&leds_lock);
            leds_status &= ~(1<<0);
            up(&leds_lock);

            break;
        }

        case 2: /* /dev/led2 */
        {
            s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);
            s3c2410_gpio_setpin(S3C2410_GPF5, 0);
            leds_status &= ~(1<<1);
            break;
        }

        case 3: /* /dev/led3 */
        {
            s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);
            s3c2410_gpio_setpin(S3C2410_GPF6, 0);

            down(&leds_lock);
            leds_status &= ~(1<<2);
            up(&leds_lock);

            break;
        }

    }

    return 0;
}



static int s3c24xx_leds_read(struct file *filp, char __user *buff, 
                                         size_t count, loff_t *offp)
{
    int minor = MINOR(filp->f_dentry->d_inode->i_rdev);
    char val;

    switch (minor)
    {
        case 0: /* /dev/leds */
        {

            copy_to_user(buff, (const void *)&leds_status, 1);                    
            break;
        }

        case 1: /* /dev/led1 */
        {
            down(&leds_lock);
            val = leds_status & 0x1;
            up(&leds_lock);
            copy_to_user(buff, (const void *)&val, 1);
            break;
        }

        case 2: /* /dev/led2 */
        {
            down(&leds_lock);
            val = (leds_status>>1) & 0x1;
            up(&leds_lock);
            copy_to_user(buff, (const void *)&val, 1);
            break;
        }

        case 3: /* /dev/led3 */
        {
            down(&leds_lock);
            val = (leds_status>>2) & 0x1;
            up(&leds_lock);
            copy_to_user(buff, (const void *)&val, 1);
            break;
        }

    }

    return 1;
}




static ssize_t s3c24xx_leds_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
    //int minor = MINOR(inode->i_rdev); //MINOR(inode->i_cdev);
    int minor = MINOR(file->f_dentry->d_inode->i_rdev);
    char val;

    copy_from_user(&val, buf, 1);

    switch (minor)
    {
        case 0: /* /dev/leds */
        {            
            s3c2410_gpio_setpin(S3C2410_GPF4, (val & 0x1));
            s3c2410_gpio_setpin(S3C2410_GPF5, (val & 0x1));
            s3c2410_gpio_setpin(S3C2410_GPF6, (val & 0x1));
            down(&leds_lock);
            leds_status = val;
            up(&leds_lock);
            break;
        }

        case 1: /* /dev/led1 */
        {
            s3c2410_gpio_setpin(S3C2410_GPF4, val);

            if (val == 0)
            {
                down(&leds_lock);
                leds_status &= ~(1<<0);
                up(&leds_lock);
            }
            else
            {
                down(&leds_lock);
                leds_status |= (1<<0);                
                up(&leds_lock);
            }
            break;
        }

        case 2: /* /dev/led2 */
        {
            s3c2410_gpio_setpin(S3C2410_GPF5, val);
            if (val == 0)
            {
                down(&leds_lock);
                leds_status &= ~(1<<1);
                up(&leds_lock);
            }
            else
            {
                down(&leds_lock);
                leds_status |= (1<<1);                
                up(&leds_lock);
            }
            break;
        }

        case 3: /* /dev/led3 */
        {
            s3c2410_gpio_setpin(S3C2410_GPF6, val);
            if (val == 0)
            {
                down(&leds_lock);
                leds_status &= ~(1<<2);
                up(&leds_lock);
            }
            else
            {
                down(&leds_lock);
                leds_status |= (1<<2);                
                up(&leds_lock);
            }
            break;
        }

    }

    return 1;
}



/* 这个结构是字符设备驱动程序的核心
 * 当应用程序操作设备文件时所调用的open、read、write等函数,
 * 最终会调用这个结构中指定的对应函数
 */
static struct file_operations s3c24xx_leds_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   s3c24xx_leds_open,     
    .read   =   s3c24xx_leds_read,     
    .write  =   s3c24xx_leds_write,    
};

/*
 * 执行insmod命令时就会调用这个函数 
 */
static int __init s3c24xx_leds_init(void)
//static int __init init_module(void)

{
    int ret;
    int minor = 0;

    gpio_va = ioremap(0x56000000, 0x100000);
    if (!gpio_va) {
        return -EIO;
    }

    /* 注册字符设备
     * 参数为主设备号、设备名字、file_operations结构;
     * 这样,主设备号就和具体的file_operations结构联系起来了,
     * 操作主设备为LED_MAJOR的设备文件时,就会调用s3c24xx_leds_fops中的相关成员函数
     * LED_MAJOR可以设为0,表示由内核自动分配主设备号
     */
    ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &s3c24xx_leds_fops);
    if (ret < 0) {
      printk(DEVICE_NAME " can't register major numbern");
      return ret;
    }

    leds_class = class_create(THIS_MODULE, "leds");
    if (IS_ERR(leds_class))
        return PTR_ERR(leds_class);



    leds_class_devs[0] = class_device_create(leds_class, NULL, MKDEV(LED_MAJOR, 0), NULL, "leds"); /* /dev/leds */

    for (minor = 1; minor < 4; minor++)  /* /dev/led1,2,3 */
    {
        leds_class_devs[minor] = class_device_create(leds_class, NULL, MKDEV(LED_MAJOR, minor), NULL, "led%d", minor);
        if (unlikely(IS_ERR(leds_class_devs[minor])))
            return PTR_ERR(leds_class_devs[minor]);
    }

    printk(DEVICE_NAME " initializedn");
    return 0;
}

/*
 * 执行rmmod命令时就会调用这个函数 
 */
static void __exit s3c24xx_leds_exit(void)
{
    int minor;
    /* 卸载驱动程序 */
    unregister_chrdev(LED_MAJOR, DEVICE_NAME);

    for (minor = 0; minor < 4; minor++)
    {
        class_device_unregister(leds_class_devs[minor]);
    }
    class_destroy(leds_class);
    iounmap(gpio_va);
}

/* 这两行指定驱动程序的初始化函数和卸载函数 */
module_init(s3c24xx_leds_init);
module_exit(s3c24xx_leds_exit);

/* 描述驱动程序的一些信息,不是必须的 */
MODULE_AUTHOR("http://www.100ask.net");
MODULE_VERSION("0.1.0");
MODULE_DESCRIPTION("S3C2410/S3C2440 LED Driver");
MODULE_LICENSE("GPL");

驱动测试程序ledtest.c

#include 
#include 
#include 
#include 

/*
  *  ledtest  
  */

void print_usage(char *file)
{
    printf("Usage:n");
    printf("%s  n",file);
    printf("eg. n");
    printf("%s /dev/leds onn", file);
    printf("%s /dev/leds offn", file);
    printf("%s /dev/led1 onn", file);
    printf("%s /dev/led1 offn", file);
}

int main(int argc, char **argv)
{
    int fd;
    char* filename;
    char val;

    if (argc != 3)
    {
        print_usage(argv[0]);
        return 0;
    }

    filename = argv[1];

    fd = open(filename, O_RDWR);
    if (fd < 0)
    {
        printf("error, can't open %sn", filename);
        return 0;
    }

    if (!strcmp("on", argv[2]))
    {
        // 亮灯
        val = 0;
        write(fd, &val, 1);
    }
    else if (!strcmp("off", argv[2]))
    {
        // 灭灯
        val = 1;
        write(fd, &val, 1);
    }
    else
    {
        print_usage(argv[0]);
        return 0;
    }


    return 0;
}

Makefile

KERN_DIR = /work/system/linux-2.6.22.6

all:
    make -C $(KERN_DIR) M=`pwd` modules 

clean:
    make -C $(KERN_DIR) M=`pwd` modules clean
    rm -rf modules.order

obj-m   += myleds.o

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

在软件架构设计领域,系统与子系统、模块与组件、框架与架构是一组既紧密相关又容易混淆的核心概念。这些概念如同建筑设计中的地基、梁柱、墙体和整体蓝图,共同构成了软件系统的骨架和灵魂。很多开发者在架构设计过程中,常常因为对这些...

关键字: 架构 框架

在嵌入式系统开发中,SPI和I2C作为最常用的同步串行通信协议,其驱动实现直接影响硬件交互的稳定性。本文以STM32 HAL库为基础,阐述从协议栈架构设计到错误处理的完整开发流程,实现微秒级时序控制与毫秒级错误恢复。

关键字: 驱动开发 SPI I2C

在嵌入式系统开发中,DMA(直接内存访问)控制器作为硬件加速的核心模块,通过独立于CPU的数据搬运能力显著提升系统性能。以STM32H7系列为例,其双DMA控制器(各含8通道)可实现高达480MHz总线频率下的数据传输,...

关键字: 驱动开发 DMA 寄存器

在嵌入式Linux系统中,字符设备驱动是连接硬件与用户空间的核心桥梁。从LED控制到传感器数据采集,字符设备驱动通过标准文件接口(open/read/write/close)实现硬件操作。本文将以实战视角,解析字符设备驱...

关键字: 嵌入式Linux 驱动开发

在嵌入式系统和底层驱动开发中,C语言因其高效性和可控性成为主流选择,但缺乏原生单元测试支持成为开发痛点。本文提出一种基于宏定义和测试用例管理的轻量级单元测试框架方案,通过自定义断言宏和测试注册机制,实现无需外部依赖的嵌入...

关键字: C语言 嵌入式系统 驱动开发

在Linux设备驱动开发中,等待队列(Wait Queue)是实现进程睡眠与唤醒的核心机制,它允许进程在资源不可用时主动放弃CPU,进入可中断睡眠状态,待资源就绪后再被唤醒。本文通过C语言模型解析等待队列的实现原理,结合...

关键字: 驱动开发 C语言 Linux

在数字化时代,电子墨水屏(E-Ink)因其独特的显示效果和低功耗特性,在电子书、智能手写本等领域得到了广泛应用。然而,电子墨水屏的刷新率一直是其发展的瓶颈,如何在保证低功耗的同时提高刷新率,成为了驱动开发中的一个重要课题...

关键字: 电子墨水屏 E-Ink 驱动开发

在Linux驱动开发中,设备树(Device Tree)作为一种描述硬件信息的数据结构,扮演着至关重要的角色。它使得操作系统能够以一种更加灵活和标准化的方式识别和管理硬件设备。然而,在实际的开发过程中,设备树配置错误或理...

关键字: Linux 驱动开发 Debug

在嵌入式系统与设备驱动开发的广阔领域中,时钟、定时器以及延时函数扮演着至关重要的角色。它们不仅是系统时间管理的基石,更是实现高效、精确控制硬件行为的关键工具。本文将深入探讨这三种机制在驱动开发中的具体应用、实现方式及注意...

关键字: 驱动开发 嵌入式系统 延时函数

在Linux内核的广阔领域中,驱动开发是连接硬件与软件、实现设备功能的关键环节。在这个过程中,文件操作函数与I/O操作函数作为两大核心工具,各自扮演着不可或缺的角色。本文旨在深入探讨这两种函数在Linux驱动开发中的区别...

关键字: I/O操作函数 文件操作函数 Linux 驱动开发
关闭