创建三个设备节点分别控制三个LED灯
/dev/led1 /dev/led2 /dev/led3
---------------------------------------------------
myled(驱动)
-------------------------------------------------
LED1 LED2 LED3
.h
#ifndef __MYLED_H__
#define __MYLED_H__typedef struct
{volatile unsigned int MODER;volatile unsigned int OTYPER;volatile unsigned int OSPEEDR;volatile unsigned int PUPDR;volatile unsigned int IDR;volatile unsigned int ODR;volatile unsigned int BSRR;volatile unsigned int LCKR;volatile unsigned int AFRL;
} gpio_t;
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR 0X50007000
#define PHY_LED3_ADDR 0X50006000
#define PHY_RCC_ADDR 0X50000A28//蜂鸣器 PB6
#define gpio_b 0X50003000
#define fengmingqi_rcc 0X50000A28//马达 PF6
#define gpio_f 0X50007000
#define mada_rcc 0X50000A28//风扇 PE9
#define gpio_e 0X50006000
#define fengshan_rcc 0X50000A28
enum LED
{LED1 = 1,LED2,LED3
};#define LED_ON _IO('a', 1)
#define LED_OFF _IO('a', 0)
#define LedON _IOW('a', 1, int)
#define LedOFF _IOW('a', 0, int)
#define fengmingqiON _IOW('a', 1, int)
#define fengmingqiOFF _IOW('a', 0, int)
#define madaON _IOW('a', 1, int)
#define madaOFF _IOW('a', 0,int)
#define fengshanON _IOW('a', 1,int)
#define fengshanOFF _IOW('a', 0,int)#endif
.ko
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/ioctl.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include<linux/slab.h>
#include"myled.h"
#define CNAME "myled"int minor=0;//次设备号从0开始
#if 0
unsigned int major = 0;//动态申请
#else
unsigned int major = 500;//静态指定设备号
#endif
char kbuf[128]={};//定义数组用于存放和用户之间拷贝的数据
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int * vir_rcc;
struct cdev *cdev;
struct class *cls;
struct device *dev;const int count=3;//指定设备节点的个数为3
//对应的是open()
int mycdev_open(struct inode *inode, struct file *file)
{return 0;
}
//read()
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{//size参数是用户期待读到的字节长度int ret;if(size>sizeof(kbuf))size=sizeof(kbuf);ret=copy_to_user(ubuf,kbuf,size);if(ret){printk("数据从内核向用户拷贝失败\n");return -EIO;}return size;
}
//write()
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{int ret;if(size>sizeof(kbuf))size=sizeof(kbuf);ret=copy_from_user(kbuf,ubuf,size);if(ret){printk("数据从内核向用户拷贝失败\n");return -EIO;}return size;
}
//close()long ioctl(struct file *file, unsigned int cmd, unsigned long arg){int ret,which;switch(cmd){case LedON:ret=copy_from_user(&which,(void *)arg,sizeof(int));if(ret){printk("用户向内核拷贝数据失败\n");return -EIO;}switch(which){case LED1:vir_led1->ODR |= (1<<10);break;case LED2:vir_led2->ODR |= (1<<10);break;case LED3:vir_led3->ODR |= (1<<8);break;}break;case LedOFF:ret=copy_from_user(&which,(void *)arg,sizeof(int));if(ret){printk("用户向内核拷贝数据失败\n");return -EIO;}switch(which){case LED1:vir_led1->ODR &= ~(1<<10);break;case LED2:vir_led2->ODR &= ~(1<<10);break;case LED3:vir_led3->ODR &= ~(1<<8);break;}break;default:printk("功能码错误\n");break;}return 0;}
int mycdev_close(struct inode *inode, struct file *file)
{return 0;
}
//操作方法结构体的初始化
struct file_operations fops=
{.open=mycdev_open,.read=mycdev_read,.write=mycdev_write,.unlocked_ioctl=ioctl,.release=mycdev_close,
};
int all_led_init(void)
{//进行物理地址的映射vir_led1=ioremap(PHY_LED1_ADDR,sizeof(gpio_t));if(vir_led1==NULL){printk("vir_led1 映射失败\n");return -ENOMEM;}printk("vir_led1 映射成功\n");vir_led2=ioremap(PHY_LED2_ADDR,sizeof(gpio_t));if(vir_led2==NULL){printk("vir_led2 映射失败\n");return -ENOMEM;}printk("vir_led2 映射成功\n");vir_led3=ioremap(PHY_LED3_ADDR,sizeof(gpio_t));if(vir_led3==NULL){printk("vir_led3 映射失败\n");return -ENOMEM;}printk("vir_led3 映射成功\n");vir_rcc=ioremap(PHY_RCC_ADDR,4);if(vir_rcc==NULL){printk("vir_rcc 映射失败\n");return -ENOMEM;}printk("vir_rcc 映射成功\n");//寄存器的初始化//led1vir_led1->MODER &= ~(3<<20);vir_led1->MODER |= (1<<20);vir_led1->ODR &= ~(1<<10);//led2vir_led2->MODER &= ~(3<<20);vir_led2->MODER |= (1<<20);vir_led2->ODR &= ~(1<<10);//led3vir_led3->MODER &= ~(3<<16);vir_led3->MODER |= (1<<16);vir_led3->ODR &= ~(1<<8);(*vir_rcc) |= (3<<4);return 0;
}
//入口函数,当驱动安装的时候执行
static int __init mycdev_init(void)
{ int ret,i;//分配对象dev_t devno;cdev=cdev_alloc();if(cdev==NULL){printk("cdev alloc memory err\n");ret = -ENOMEM;goto ERR1;}printk("对象分配成功\n");//对象的初始化cdev_init(cdev,&fops);//设备号的申请if(major==0)//动态申请{ret=alloc_chrdev_region(&devno,minor,count,"my_led");if(ret){printk("动态申请设备号失败\n");goto ERR2;}major = MAJOR(devno);minor = MINOR(devno);printk("动态申请设备号成功\n");}else{ret=register_chrdev_region(MKDEV(major,minor),count,"my_led");if(ret){printk("静态申请设备号失败\n");goto ERR2;}printk("静态申请设备号成功\n");}//注册字符设备驱动ret=cdev_add(cdev,MKDEV(major,minor),count);if(ret){printk("字符设备驱动注册失败\n");goto ERR3;}printk("注册字符设备驱动成功\n");//自动创建设备节点cls = class_create(THIS_MODULE,"led");if(IS_ERR(cls)){printk("创建逻辑节点目录失败\n");ret=PTR_ERR(cls);goto ERR4;}printk("创建逻辑节点目录成功\n");//向上提交节点信息for(i=0;i<3;i++){dev = device_create(cls,NULL,MKDEV(major,i),NULL,"my_led%d",i);if(IS_ERR(dev)){printk("创建逻辑节点失败\n");ret = PTR_ERR(dev);goto ERR5;}}printk("创建逻辑节点成功\n");//寄存器的初始化all_led_init();return 0;
ERR5:for(--i;i>=0;i--){device_destroy(cls,MKDEV(major,i));}class_destroy(cls);
ERR4:cdev_del(cdev);
ERR3:unregister_chrdev_region(MKDEV(major,minor),count);
ERR2:kfree(cdev);
ERR1:return ret;
}
static void __exit mycdev_exit(void)
{//1.销毁设备节点int i;for(i=0;i<count;i++){device_destroy(cls,MKDEV(major,i));}class_destroy(cls);//2.注销字符设备驱动cdev_del(cdev);//3.释放设备号unregister_chrdev_region(MKDEV(major,minor),count);//4.释放动态申请的空间kfree(cdev);}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
main.c
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include <sys/ioctl.h>
#include"myled.h"int main(int argc, char const *argv[])
{char buf[128]={};int fd1=open("/dev/my_led0",O_RDWR);int fd2=open("/dev/my_led1",O_RDWR);int fd3=open("/dev/my_led2",O_RDWR);int which;if(fd1<0 | fd2<0 | fd3<0){printf("打开设备文件失败\n");exit(-1);}printf("设备文件打开成功\n");//在终端输入while(1){printf("请输入你的操作: 1.开灯 2.关灯\n");scanf("%d", &which);getchar();switch(which){case 1:printf("请输入你要开的灯: 1.LED1 2.LED2 3.LED3\n");scanf("%d", &which);getchar();switch(which){case 1:ioctl(fd1,LedON,&which);break;case 2:ioctl(fd2,LedON,&which);break;case 3:ioctl(fd3,LedON,&which);break;default:printf("你输入的操作有误\n");}break;case 2:printf("请输入你要关的灯: 1.LED1 2.LED2 3.LED3\n");scanf("%d", &which);getchar();switch (which){case 1:ioctl(fd1,LedOFF,&which);case 2:ioctl(fd2,LedOFF,&which);case 3:ioctl(fd3,LedOFF,&which);default:printf("你输入的操作有误\n");}}}close(fd1);close(fd2);close(fd3);return 0;
}