编写LED驱动,每个LED绑定一个LED文件
驱动代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"
unsigned int major;
char kbuf[128]={0};
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;
struct class *cls;
struct device *dev;
int i;
int mycdev_open(struct inode *inode, struct file *file)
{
int a=inode->i_rdev;
file->private_data=(void *)MINOR(a);
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *lof)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *lof)
{
long ret;
ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
//判断用户输入的数据,进行不同的硬件逻辑操作
return 0;
}
int mycdev_close (struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
long mycdev_ioctl(struct file *file,unsigned int cmd,unsigned long arg)
{
unsigned int a=(unsigned int )file->private_data;
printk("a=%d,cmd=%d",a,cmd);
switch (a)
{
case 0:
if(cmd==LED_OFF)//关灯
{
(vir_led1->ODR) &=(~(0x1<<10));
}
else if(cmd==LED_ON)
{
//开灯逻辑
(vir_led1->ODR) |=(0x1<<10);
}
break;
case 1:
if(cmd==LED_OFF)//关灯
{
(vir_led2->ODR) &=(~(0x1<<10));
}
else if(cmd==LED_ON)
{
//开灯逻辑
(vir_led2->ODR) |=(0x1<<10);
}
break;
case 2:
if(cmd==LED_OFF)//关灯
{
(vir_led3->ODR) &=(~(0x1<<8));
}
else if(cmd==LED_ON)
{
//开灯逻辑
(vir_led3->ODR) |=(0x1<<8);
}
break;
default:
break;
}
return 0;
}
struct file_operations fops={
.open=mycdev_open,
.release=mycdev_close,
.read=mycdev_read,
.write=mycdev_write,
.unlocked_ioctl=mycdev_ioctl,
};
static int __init mycdev_init(void)
{
major=register_chrdev(0,"myled",&fops);
if(major<0)
{
printk("注册字符设备驱动失败");
return major;
}
printk("注册字符设备驱动成功major=%d\n",major);
//映射led1物理地址
vir_led1=ioremap(GPIOE,4);
if(vir_led1 == NULL)
{
printk("映射物理内存失败%d\n",__LINE__);
return -EFAULT;
}
vir_led2=ioremap(GPIOF,4);
if(vir_led2== NULL)
{
printk("映射物理内存失败%d\n",__LINE__);
return -EFAULT;
}
vir_led3=ioremap(GPIOE,4);
if(vir_led3== NULL)
{
printk("映射物理内存失败%d\n",__LINE__);
return -EFAULT;
}
//映射rcc地址
vir_rcc=ioremap(PHY_LED_RCC,4);
if(vir_rcc == NULL)
{
printk("映射物理内存失败%d\n",__LINE__);
return -EFAULT;
}
printk("映射物理内存成功\n");
//向上提交目录
cls = class_create(THIS_MODULE,"myled");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录信息成功\n");
//向上提交设备节点信息
for (i=0;i<3;i++)
{
dev=device_create(cls, NULL, MKDEV(major,i),NULL,"myled%d",i);
if(IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
return -PTR_ERR(dev);
}
}
printk("向上提交设备节点成功\n");
//led1寄存器的初始化
(*vir_rcc)|=(0x1<<4); //RCC使能
(*vir_rcc)|=(0x1<<5);
(vir_led1->MODER) &= (~(0x3<<20)); //设置为输出模式
(vir_led1->MODER) |=(0x1<<20);
(vir_led1->ODR) &= (~(0x1<<10)); //输出低电平
printk("led1寄存器初始化成功\n");
//led2寄存器初始化
(vir_led2->MODER) &=(~(0x3<<20));
(vir_led2->MODER) |=(0x1<<20);
(vir_led2->ODR) &= (~(0x1<<10));
printk("led2寄存器初始化成功\n");
//led3寄存器初始化
(vir_led3->MODER) &=(~(0x3<<16));
(vir_led3->MODER) |=(0x1<<16);
(vir_led3->ODR) &= (~(0x1<<8));
printk("led2寄存器初始化成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
iounmap(vir_led1);
iounmap(vir_led2);
iounmap(vir_led3);
iounmap(vir_rcc);
for(i=0;i<3;i++)
{
device_destroy(cls,MKDEV(major,i));
}
class_destroy(cls);
unregister_chrdev(major,"myled");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
应用层
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
#include<sys/ioctl.h>
#include "head.h"
int main(int argc, char const *argv[])
{
char buf[128]={0};
int a;
int b;
int fd,fd1,fd2;
while (1)
{
printf("从终端获取指令\n");
printf("请输入要控制哪个灯 1(led1),2(led2),3(led3)\n");
scanf("%d",&b);
printf("0(关灯) 1(开灯)\n");
scanf("%d",&a);
switch (b)
{
case 1:
fd =open("/dev/myled0",O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
if(a==0)
ioctl(fd,LED_OFF);
else
ioctl(fd,LED_ON);
break;
case 2:
fd1 =open("/dev/myled1",O_RDWR);
if (fd1 < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
if(a==0)
ioctl(fd1,LED_OFF);
else
ioctl(fd1,LED_ON);
break;
case 3:
fd2 =open("/dev/myled2",O_RDWR);
if (fd2 < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
if(a==0)
ioctl(fd2,LED_OFF);
else
ioctl(fd2,LED_ON);
break;
default:
break;
}
}
close(fd2);
return 0;
}
头文件
#ifndef __HEAD_H__
#define __HEAD_H__
typedef struct {
volatile unsigned int MODER; // 0x00
volatile unsigned int OTYPER; // 0x04
volatile unsigned int OSPEEDR; // 0x08
volatile unsigned int PUPDR; // 0x0C
volatile unsigned int IDR; // 0x10
volatile unsigned int ODR; // 0x14
}gpio_t;
#define GPIOE 0x50006000
#define GPIOF 0x50007000
#define PHY_LED_RCC 0x50000A28
#define PHY_LED1 ((gpio_t *)GPIOE)
#define PHY_LED2 ((gpio_t *)GPIOF)
#define PHY_LED3 ((gpio_t *)GPIOE)
#define PHY_LED1_MODER 0x50006000
#define PHY_LED1_ODR 0x50006014
#define PHY_LED1_RCC 0x50000A28
//功能码
#define LED_ON _IO('l',1)
#define LED_OFF _IO('l',0)
#endif