led_dev.c文件:

#include <linux/module.h>
#include <linux/version.h>

#include <linux/init.h>

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/timer.h>
#include <linux/init.h>
#include <linux/serial_core.h>
#include <linux/platform_device.h>

static struct resource led_resource[] = {
  [0] = {
        .start = 0x56000050,                  
        .end = 0x56000050 + 8 - 1,
        .flags = IORESOURCE_MEM,
      },

  [1] = {
        .start = 5,
        .end = 5,
        .flags = IORESOURCE_IRQ,
      }

};

static void led_release(struct device * dev)
{

}

static struct platform_device led_dev = {
  .name = "myled",
  .id = -1,                                                //只有一个设备时为-1
  .num_resources = ARRAY_SIZE(led_resource),
  .resource = led_resource,
  .dev = {
        .release = led_release,
  },
};

static int led_dev_init(void)
{
  platform_device_register(&led_dev);                      //设备注册
  return 0;
}

static void led_dev_exit(void)
{
  platform_device_unregister(&led_dev);        //设备卸载
}

module_init(led_dev_init);
module_exit(led_dev_exit);

MODULE_LICENSE("GPL");

led_drv.c文件:

#include <linux/module.h>
#include <linux/version.h>

#include <linux/init.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/io.h>

static int major;

static struct class *cls;
static volatile unsigned long *gpio_con;
static volatile unsigned long *gpio_dat;
static int pin;

static int led_open(struct inode *inode, struct file *file)
{
  *gpio_con &= ~(0x3<<(pin*2));
  *gpio_con |= (0x1<<(pin*2));
  return 0;
}

static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
  int val;

  copy_from_user(&val, buf, count);

  if (val == 1)
  {
    *gpio_dat &= ~(1<<pin);    //点灯
  }
  else
  {
    *gpio_dat |= (1<<pin);     //灭灯
  }
  return 0;
}

static struct file_operations led_fops = {
  .owner = THIS_MODULE, 
  .open = led_open,
  .write = led_write,
};

static int led_probe(struct platform_device *pdev)
{
  struct resource *res;

  res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  gpio_con = ioremap(res->start, res->end - res->start + 1);
  gpio_dat = gpio_con + 1;

  res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  pin = res->start;

  printk("led_probe, found led\n");

  major = register_chrdev(0, "myled", &led_fops);

  cls = class_create(THIS_MODULE, "myled");

  class_device_create(cls, NULL, MKDEV(major, 0), NULL, "led"); /* /dev/led */
  return 0;
}

static int led_remove(struct platform_device *pdev)
{

  class_device_destroy(cls, MKDEV(major, 0));
  class_destroy(cls);
  unregister_chrdev(major, "myled");
  iounmap(gpio_con);
  return 0;
}

struct platform_driver led_drv = {
  .probe = led_probe,
  .remove = led_remove,
  .driver = {
    .name = "myled",
  }
};

static int led_drv_init(void)
{
  platform_driver_register(&led_drv);
  return 0;
}

static void led_drv_exit(void)
{
  platform_driver_unregister(&led_drv);
}

module_init(led_drv_init);
module_exit(led_drv_exit);

MODULE_LICENSE("GPL");

Makefile文件:

obj-m += button_drv.o

KERN_DIR = /work/system/linux-2.6.22.6

all:
make -C $(KERN_DIR) M=`pwd` modules 
clean:
rm -rf *.o *.ko *.order *.symvers *.mod.c

led_app.c文件:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

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

char *filename;
  int val = 1;
  fd = open("/dev/led", O_RDWR);
  if (fd < 0)
  {
    printf("can't open \n");
  }

  if (strcmp(argv[2], "on") == 0)
  {
    val = 1;
  }
  else
  {
    val = 0;
  }
  write(fd, &val, 4);
  return 0;
}

编译生成led_dev.ko和led_drv.ko和led_app文件,先加载led_dev.ko再加载led_drv.ko,最后运行./led_app /dev/led on或./led_app /dev/led off

Linux 驱动——LED(驱动分离分层)的更多相关文章

  1. (笔记)linux设备驱动--LED驱动

    linux设备驱动--LED驱动 最近正在学习设备驱动开发,因此打算写一个系列博客,即是对自己学习的一个总结,也是对自己的一个督促,有不对,不足,需要改正的地方还望大家指出,而且希望结识志同道合的朋友 ...

  2. Linux 驱动——Led驱动2

    led_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/init ...

  3. Linux 驱动——Led驱动1

    led_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/init ...

  4. 自己动手写最简单的Android驱动---LED驱动的编写【转】

    本文转载自:http://blog.csdn.net/k_linux_man/article/details/7023824 转载注明出处,作者:K_Linux_Man, 薛凯 山东中医药大学,给文章 ...

  5. Linux驱动之平台设备驱动模型简析(驱动分离分层概念的建立)

    Linux设备模型的目的:为内核建立一个统一的设备模型,从而有一个对系统结构的一般性抽象描述.换句话说,Linux设备模型提取了设备操作的共同属性,进行抽象,并将这部分共同的属性在内核中实现,而为需要 ...

  6. 【linux】驱动-5-驱动框架分层分离&实战

    目录 前言 5. 分离分层 5.1 回顾-设备驱动实现 5.2 分离分层 5.3 设备 5.4 驱动 5.5 系统,模块 5.6 Makefile 参考: 前言 5. 分离分层 本章节记录实现LED驱 ...

  7. linux驱动分离分层的概念

    这个分离分层的概念和输入子系统有点像,但不是完全一样的.为什么会再弄一个这个模型出来我也没有搞懂,现在我的学习还停留在把知识学懂的层面上.至于为什么会产生这种知识,现在我还无从解释,还需时日成长. 这 ...

  8. 四、分离分层的 platform驱动

    学习目标: 学习实现platform机制的分层分离,并基于platform机制,编写led设备和驱动程序: 一.分离分层 输入子系统.usb设备比驱动以及platform类型的驱动等都体现出分离分层机 ...

  9. linux设备驱动归纳总结(五):4.写个简单的LED驱动【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-84693.html linux设备驱动归纳总结(五):4.写个简单的LED驱动 xxxxxxxxxxx ...

  10. 全志A33 linux led驱动编程(附实测参考代码)

    开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 开发平台 * 芯灵思Sinl ...

随机推荐

  1. JS回调函数中的this指向(详细)

    首先先说下正常的this指向问题 什么是this:自动引用正在调用当前方法的.前的对象. this指向的三种情况 1. obj.fun()     fun中的this->obj,自动指向.前的对 ...

  2. XVI Open Cup named after E.V. Pankratiev. GP of Ekaterinburg--I.Iron man

    n个服务器,k类任务,每个服务器完成一个任务都有特定的花费$cost_{i,j}$,但是你设置好某台机器去完成某项任务时他只能去完成这类任务,除非你可以花费$C$去更改配置.第$i$天要求去完成$q_ ...

  3. vue-cli使用swiper插件

    使用的教程https://blog.csdn.net/lbpro0412/article/details/82465067

  4. mysql 执行sql流程

    客户端发送sql 语句后的堆栈 #0 0x0000000100370565 in do_command(THD*) at percona-server-Percona-Server-5.6.37-82 ...

  5. VS2017开发.net core 时默认发布路径文件夹多个BPC

    新安装的VS2017,编译后进行发布,结果在bin文件夹下多了个BPC文件夹,很是费解,查了资料才知道是VS2017默认设置了环境变量.在此记录下,如果不需要默认路径可修改环境变量,具体操作如下: 我 ...

  6. win10下安装vs2013无法安装解决方案

    win10下安装vs2013无法安装解决方案 win+r,输入cmd进入命令行 进入界面后选择修复 进入vs_ultimate文件所在目录,输入: vs_ultimate /Uninstall    ...

  7. ES6笔记(二)

    一.字符串的扩展1. 用于从码点返回到对应字符. String.fromCodePoint(xx)2. for...of可以遍历字符串3. includes():返回布尔值,表示是否找到了参数字符串. ...

  8. View事件体系

    View事件体系 文章目录 View事件体系 一.Android View基础知识 1.1 View简介 1.2 View分类 1.3 View的结构 1.4 View的坐标 1.4.1 Androi ...

  9. 【C语言】数组知识点总结

    [C语言]数组知识点总结 标签: 数组 2018年04月12日 17:44:4481人阅读 评论(0) 收藏 举报  分类: C语言知识总结(4)  版权声明:本文为博主原创文章,未经博主允许不得转载 ...

  10. mysql语句将日期转换为时间戳的方法

    mysql将日期转换为时间戳更新数据库: update test set creattime=UNIX_TIMESTAMP('2018-04-19') 替换字段为当前日期: update test s ...