led_drv.c驱动文件:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/uaccess.h>        //包含copy_from_user函数
#include <linux/device.h>       //包含class类相关的处理函数
#include <linux/fs.h>         //包含file_operations结构体
#include <asm/io.h>          //包含iomap函数iounmap函数

#define DRIVER_NAME "led_drv"

int major;

static struct class *led_drv_class;
static struct class_device *led_drv_class_dev[4];

volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;

static int led_drv_open(struct inode *inode, struct file *file)
{
  int minor = MINOR(inode->i_rdev);                //获取设备的次设备号

  switch(minor)
  {
    case 0:
    {
      *gpfcon &= ~(0x3<<(2*4));
      *gpfcon |= (0x1<<(2*4));
      *gpfcon &= ~(0x3<<(2*5));
      *gpfcon |= (0x1<<(2*5));
      *gpfcon &= ~(0x3<<(2*6));
      *gpfcon |= (0x1<<(2*6));
      *gpfdat &= ~((1<<4) | (1<<5) | (1<<6));       //输出低电平
      break;
    }
    case 1:
    {
      *gpfcon &= ~(0x3<<(2*4));
      *gpfcon |= (0x1<<(2*4));
      *gpfdat &= ~(0x1<<4);
      break;
    }
    case 2:
    {
      *gpfcon &= ~(0x3<<(2*5));
      *gpfcon |= (0x1<<(2*5));
      *gpfdat &= ~(0x1<<5);
      break;
    }
    case 3:
    {
      *gpfcon &= ~(0x3<<(2*6));
      *gpfcon |= (0x1<<(2*6));
      *gpfdat &= ~(0x1<<6);
      break;
    }
  }
  return 0;
}

static int led_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
  int minor = MINOR(file->f_dentry->d_inode->i_rdev);           //获取设备的次设备号

  char val, ret;

  ret = copy_from_user(&val, buf, 1);
  if(ret!=0)
  {
    printk("led_drv_write error 1 \n");
  }

  switch(minor)
  {
    case 0:
    {
      if(val==1)
      {
        *gpfdat &= ~((1<<4) | (1<<5) | (1<<6));           //输出低电平, 灯亮
      }
      else
      {
        *gpfdat |= ((1<<4) | (1<<5) | (1<<6));            //输出高电平, 灯灭
      }
      break;
    }

    case 1:
    {
      if(val==1)
      {
        *gpfdat &= ~(1<<4); 
      }
      else
      {
        *gpfdat |= (1<<4); 
      }
      break;
    }
    case 2:
    {
      if(val==1)
      {
        *gpfdat &= ~(1<<5); 
      }
      else
      {
        *gpfdat |= (1<<5); 
      }
      break;
    }
    case 3:
    {
      if(val==1)
      {
        *gpfdat &= ~(1<<6); 
      }
      else
      {
        *gpfdat |= (1<<6); 
      }
      break;
    }
  }
  return 0;
}

static struct file_operations led_drv_fopt = {
  .owner = THIS_MODULE,
  .open = led_drv_open,
  .write = led_drv_write,
};

static int led_drv_init(void)
{
  int i;
  gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
  gpfdat = gpfcon + 1;
  major = register_chrdev(0, DRIVER_NAME, &led_drv_fopt);   //向内核中注册驱动
  if(major<0)
  {
    printk(KERN_EMERG "can not obtain major number.\n");
    return major;
  }

//建立设备类别
  led_drv_class = class_create(THIS_MODULE, DRIVER_NAME);  

//分配次设备号,主设备号为major从设备号为0

  led_drv_class_dev[0] = class_device_create(led_drv_class, NULL, MKDEV(major, 0), NULL, "leds");

   //分配次设备号,主设备号为major从设备号为i
  for(i=1; i<4; i++)
  {
    led_drv_class_dev[i] = class_device_create(led_drv_class, NULL, MKDEV(major, i), NULL, "led%d", i);  

  }

  return 0;
}

static void led_drv_exit(void)
{
  int i;

  unregister_chrdev(major, DRIVER_NAME);

  for(i=0; i<4; i++)
  {
    class_device_unregister(led_drv_class_dev[i]);
  }
  class_destroy(led_drv_class);

  iounmap(gpfcon);
}

module_init(led_drv_init);
module_exit(led_drv_exit);

MODULE_LICENSE("GPL");

Makefile文件:

obj-m += led_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文件:

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

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

  filename = argv[1];

  fd = open(filename, O_RDWR);
  if(fd<0)
  {
    printf("error, can't open %s\n", filename);
    return 0;
  }
  if(strcmp("on", argv[2])==0)
  {
    val = 1;
    write(fd, &val, 1);
  }
  else if(strcmp("off", argv[2])==0)
  {
    val = 0;
    write(fd, &val, 1);
  }
  return 0;
}

编译生成led_drv.ko和led_app, 运行./led_app /dev/leds on

./led_app /dev/leds off

./led_app /dev/led1 on

./led_app /dev/led1 off

./led_app /dev/led2 on

./led_app /dev/led2 off

./led_app /dev/led3 on

./led_app /dev/led3 off

可以看到不同的效果

Linux 驱动——Led驱动2的更多相关文章

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

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

  2. Linux 驱动——Led驱动1

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

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

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

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

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

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

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

  6. Linux驱动之LED驱动编写

    从上到下,一个软件系统可以分为:应用程序.操作系统(内核).驱动程序.结构图如下:我们需要做的就是写出open.read.write等驱动层的函数.一个LED驱动的步骤如下: 1.查看原理图,确定需要 ...

  7. 【Linux开发】linux设备驱动归纳总结(五):4.写个简单的LED驱动

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

  8. 嵌入式Linux学习笔记(三) 字符型设备驱动--LED的驱动开发

    在成功构建了一个能够运行在开发板平台的系统后,下一步就要正式开始应用的开发(这里前提是有一定的C语言基础,对ARM体系的软/硬件,这部分有疑问可能要参考其它教程),根据需求仔细分解任务,可以发现包含的 ...

  9. linux驱动之LED驱动

    通过之前的学习,了解到linux驱动编写的流程是:先通过注册函数注册我们编写的入口函数,然后在入口函数中获取设备号->注册字符设备->自动创建设备节点->获取设备树信息,最后通过销毁 ...

随机推荐

  1. Bootstrap4 导航栏

    Bootstrap4 导航栏 目录 Bootstrap4 导航栏 动态选项卡 标准的导航栏 导航对齐方式 导航栏的组成 ul 元素中包含navbar-nav 类 表示导航栏中ul li元素中包含nav ...

  2. spring reference

    Spring框架概述 Spring可以轻松创建Java企业应用程序.它提供了在企业环境中使用Java语言所需的一切,支持Groovy和Kotlin作为JVM上的替代语言,并可根据应用程序的需要灵活地创 ...

  3. ELK原理与介绍

    为什么用到ELK: 一般我们需要进行日志分析场景:直接在日志文件中 grep.awk 就可以获得自己想要的信息.但在规模较大的场景中,此方法效率低下,面临问题包括日志量太大如何归档.文本搜索太慢怎么办 ...

  4. C# WPF上位机实现和下位机TCP通讯

    下位机使用北京大华程控电源DH1766-1,上位机使用WPF.实现了电压电流实时采集,曲线显示.上午在公司调试成功,手头没有程控电源,使用TCP服务端模拟.昨天写的TCP服务端正好排上用场. 界面如下 ...

  5. 强大的金融类图表库 TradingView 使用分享

    这段时间刚好做币圈交易所,运用到了现在最火的金融类图表库 -- TradingView ,就是强大,基本上现在的火币网(https://www.huobi.com),币安网(https://www.b ...

  6. Delphi下 Winsock 函数

    用于初始化Winsock[声明]int WSAStarup(WORD wVersionRequested,LPWSADATA lpWSAData);[参数]wVersionRequested - 要求 ...

  7. <input>标签单、复选相关查询地址

    http://www.w3school.com.cn/tags/tag_input.asp 以下是相关示例,转载 lfx_web: <html><head><script ...

  8. mybatis使用接口联合查询

    一.先建立两个实体类和配置文件 配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE c ...

  9. 星星闪烁+多边形移动 canvas

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. Python 实现清屏

    使用Python的IDLE到某个程序节点时,需要清屏以提高清晰度. 但IDLE本身并没有这个功能,我们可以通过扩展来实现类似于Ctrl + L的清屏 资料来自于百度经验的 BinnLZeng 先制作一 ...