首页 > 系统服务 > 详细

Linux 驱动——LED(驱动分离分层)

时间:2019-03-25 10:48:24      阅读:159      评论:0      收藏:0      [点我收藏+]

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.koled_drv.koled_app文件,先加载led_dev.ko再加载led_drv.ko,最后运行./led_app /dev/led on./led_app /dev/led off

 

 

 

 

 

 

 

 

 

 

 

 

Linux 驱动——LED(驱动分离分层)

原文:https://www.cnblogs.com/lian-meng/p/10592327.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!