• linux4.10.8 内核移植(四)---字符设备驱动_led驱动程序


    一、字符设备驱动程序介绍

      

      app里面用 open、read、write等等函数出来操作底层硬件。驱动程序中也有对应的xxx_open等函数。怎么找到驱动程序中的函数依赖于驱动程序框架。

    二、搭建驱动程序框架

    2.1 初步框架

    2.1.1 Makefile

      

    2.1.2 jz2440_led.c

     1 #include <linux/module.h>
     2 #include <linux/of.h>
     3 #include <linux/of_device.h>
     4 #include <linux/regmap.h>
     5 #include <linux/fs.h>
     6 
     7 #define JZ2440_LEDS_MAJOR     111    /* 主设备号 */
     8 
     9 
    10 static int jz2440_drv_open(struct inode *inode, struct file *file)
    11 {
    12     printk("jz2440_drv_open");
    13     return 0;
    14 }
    15 
    16 static int jz2440_drv_close(struct inode *inode, struct file *file)
    17 {
    18     printk("jz2440_drv_close");
    19     return 0;    
    20 }
    21 
    22 static ssize_t jz2440__drv_read(struct file *file, char __user * buf, size_t count, loff_t * ppos)
    23 {
    24     printk("jz2440__drv_read");
    25     return 0;
    26 }
    27 
    28 static ssize_t jz2440_drv_write(struct file *file, const char __user * buf, size_t count, loff_t * ppos)
    29 {
    30     printk("jz2440_drv_write");
    31     return 0;
    32 }
    33 
    34 static struct file_operations jz2440_leds_ops = {
    35     .owner = THIS_MODULE,
    36     .read = jz2440__drv_read,
    37     .open = jz2440_drv_open,
    38     .write = jz2440_drv_write,
    39     .release = jz2440_drv_close,
    40 };
    41 
    42 static int __init jz2440_drv_leds_init(void)
    43 {
    44     register_chrdev(JZ2440_LEDS_MAJOR,"jz2440_drv_leds",&jz2440_leds_ops);//注册驱动程序,即告诉内核
    45     return 0;
    46 }
    47 
    48 static void __exit jz2440_drv_leds_exit(void)
    49 {
    50     unregister_chrdev(JZ2440_LEDS_MAJOR,"jz2440_drv_leds");
    51 }
    52 
    53 //指向入口函数 jz2440_drv_leds_init
    54 module_init(jz2440_drv_leds_init);
    55 //出口函数 卸载驱动
    56 module_exit(jz2440_drv_leds_exit);
    57 
    58 MODULE_LICENSE("GPL");
    59 MODULE_DESCRIPTION("jz2440 leds driver");

      编译:

      

      烧写运行:

      

      

      进入开发板u-boot界面:

      设置命令行启动参数

      set bootargs noinitrd root=/dev/nfs nfsroot=192.168.0.192:/home/ubuntu/work/nfs_root/fs_mini ip=192.168.0.191:192.168.0.192:192.168.0.1:255.255.255.0::eth0:off init=/linuxrc console=ttySAC0,115200 

      启动内核:boot

      

      nfs目录挂载成功。

      查看proc目录:

      

      设备号可用。

      加载模块:

      

      查看是否加载进去:

      

      测试程序

     1 #include <sys/types.h>
     2 #include <sys/stat.h>
     3 #include <fcntl.h>
     4 #include <stdio.h>
     5 #include <unistd.h>
     6 
     7 int main(int argc, char **argv)
     8 {
     9     int fd;
    10     int val = 1;
    11     
    12     fd = open("/dev/xxx", O_RDWR);
    13     if(fd < 0)
    14     {
    15         printf("can't open !
    ");
    16     }
    17     
    18     write(fd, &val, 4);
    19     return 0;
    20 }
     

      编译烧写进板子,执行

      

      创建设备节点;

      

      执行 a.out

      

       卸载驱动 rmmod jz2440_led

      删除节点: rm -rf /dev/xxx

      

      

  • 相关阅读:
    归档拷贝LogMiner 和 archived redo log,分析归档日志
    宋体测试液晶屏测试小程序
    宋体方法Unity3d通过苹果和google两种方法加入广告
    代码确认小米官方在线预定——对代码实现的分析
    分区启动Grub2配置详解
    临时表判断sqlite 判断表 或 临时表 是否存在
    解释升级JDeveloper&ADF的版本
    引用字符shell中的引用java教程
    枚举生成MVC3: Bind Enum To DropDownList ?java教程
    能力学习为什么你应聘不上或试用期被开?
  • 原文地址:https://www.cnblogs.com/kele-dad/p/7112197.html
Copyright © 2020-2023  润新知