#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include "led.h"
struct cdev *cdev;
struct class *cls;
struct device *dev;
unsigned int major=500;
unsigned int minor=0;
dev_t devno;
char kbuf[128]={0};
gpio_t *vir_gpioe;
gpio_t *vir_gpiof;
unsigned int *vir_rcc;
int mycdev_open(struct inode *inode, struct file *file)
{
int aa = inode->i_rdev; //得到设备号
file->private_data = (void *)MINOR(aa);
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int aa = (int)file->private_data; //次设备号
switch(cmd)
{
case LED_ON:
switch (aa)
{
case 0:
vir_gpioe->odr |= (0x1 << 10);
break;
}
break;
case LED_OFF:
switch (aa)
{
case 0:
vir_gpioe->odr &= (~(0x1 << 10));
break;
}
break;
}
return 0;
}
ssize_t mycdev_read(struct file *file, char *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 *ubuf, size_t size, loff_t *lof)
{
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//定义操作方法结构体变量并赋值
struct file_operations fops={
.open=mycdev_open,
.read=mycdev_read,
.write=mycdev_write,
.release=mycdev_close,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
//物理地址映射
vir_gpioe = ioremap(GPIOE, sizeof(gpio_t));
if(vir_gpioe == NULL)
{
printk("MODER寄存器映射失败\n");
return -EFAULT;
}
vir_gpiof = ioremap(GPIOF, sizeof(gpio_t));
if (vir_gpiof == NULL)
{
printk("MODER寄存器映射失败\n");
return -EFAULT;
}
vir_rcc = ioremap(RCC, 4);
if (vir_rcc == NULL)
{
printk("RCC寄存器映射失败\n");
return -EFAULT;
}
printk("寄存器映射成功\n");
int ret;
//1.申请对象空间
cdev = cdev_alloc();
if(NULL == cdev)
{
printk("申请对象空间失败\n");
ret = -EFAULT;
goto OUT1;
}
printk("申请对象空间成功\n");
//2.初始化对象
cdev_init(cdev, &fops);
//3.申请设备号
if(major == 0)
{
ret = alloc_chrdev_region(&devno,minor,1,"led0");
if(ret)
{
printk("动态申请设备号失败\n");
goto OUT2;
}
major = MAJOR(devno);
minor = MINOR(devno);
}
else
{
ret=register_chrdev_region(MKDEV(major,minor),1,"led0");
if(ret)
{
printk("静态指定设备号失败\n");
goto OUT2;
}
}
printk("设备号申请成功\n");
//4.注册驱动对象
ret=cdev_add(cdev,MKDEV(major,minor),3);
if(ret)
{
printk("注册设备驱动对象失败\n");
goto OUT3;
}
//5.向上提交目录
cls=class_create(THIS_MODULE,"led0");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret = -PTR_ERR(cls);
goto OUT4;
}
printk("向上提交目录成功\n");
//6.向上提交设备信息
dev = device_create(cls,NULL,MKDEV(major,0),NULL,"led0");
if(IS_ERR(dev))
{
printk("向上提交设备节点失败\n");
goto OUT5;
}
printk("向上提交设备节点成功\n");
//初始化led灯
vir_gpioe->moder &= (~(0x3 << 20));
vir_gpioe->moder |= (0x1 << 20);
vir_gpioe->odr &= (~(0x1 << 10));
printk("LED0硬件寄存器初始化成功\n");
return 0;
//跳转标签
OUT5:
device_destroy(cls,MKDEV(major,0));
class_destroy(cls);
OUT4:
cdev_del(cdev);
OUT3:
unregister_chrdev_region(MKDEV(major,minor),1);
OUT2:
kfree(cdev);
OUT1:
return ret;
}
static void __exit mycdev_exit(void)
{
//1.销毁设备信息
device_destroy(cls,MKDEV(major,0));
//2.销毁目录
class_destroy(cls);
//3.注销驱动对象
cdev_del(cdev);
//4.释放设备号
unregister_chrdev_region(MKDEV(major,minor),1);
//5.释放对象空间
kfree(cdev);
// 取消寄存器地址映射
iounmap(vir_gpioe);
iounmap(vir_rcc);
iounmap(vir_gpiof);
// 字符设备驱动的注销
unregister_chrdev(major, "led0");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
//声明
MODULE_LICENSE("GPL");
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include "led.h"
struct cdev *cdev;
struct class *cls;
struct device *dev;
unsigned int major=500;
unsigned int minor=1;
dev_t devno;
char kbuf[128]={0};
gpio_t *vir_gpioe;
gpio_t *vir_gpiof;
unsigned int *vir_rcc;
int mycdev_open(struct inode *inode, struct file *file)
{
int aa = inode->i_rdev; //得到设备号
file->private_data = (void *)MINOR(aa);
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int aa = (int)file->private_data; //次设备号
switch(cmd)
{
case LED_ON:
switch (aa)
{
case 1:
vir_gpiof->odr |= (0x1 << 10);
break;
}
break;
case LED_OFF:
switch (aa)
{
case 1:
vir_gpiof->odr &= (~(0x1 << 10));
break;
}
break;
}
return 0;
}
ssize_t mycdev_read(struct file *file, char *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 *ubuf, size_t size, loff_t *lof)
{
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//定义操作方法结构体变量并赋值
struct file_operations fops={
.open=mycdev_open,
.read=mycdev_read,
.write=mycdev_write,
.release=mycdev_close,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
//物理地址映射
vir_gpioe = ioremap(GPIOE, sizeof(gpio_t));
if(vir_gpioe == NULL)
{
printk("MODER寄存器映射失败\n");
return -EFAULT;
}
vir_gpiof = ioremap(GPIOF, sizeof(gpio_t));
if (vir_gpiof == NULL)
{
printk("MODER寄存器映射失败\n");
return -EFAULT;
}
vir_rcc = ioremap(RCC, 4);
if (vir_rcc == NULL)
{
printk("RCC寄存器映射失败\n");
return -EFAULT;
}
printk("寄存器映射成功\n");
int ret;
//1.申请对象空间
cdev = cdev_alloc();
if(NULL == cdev)
{
printk("申请对象空间失败\n");
ret = -EFAULT;
goto OUT1;
}
printk("申请对象空间成功\n");
//2.初始化对象
cdev_init(cdev, &fops);
//3.申请设备号
if(major == 0)
{
ret = alloc_chrdev_region(&devno,minor,1,"led1");
if(ret)
{
printk("动态申请设备号失败\n");
goto OUT2;
}
major = MAJOR(devno);
minor = MINOR(devno);
}
else
{
ret=register_chrdev_region(MKDEV(major,minor),1,"led1");
if(ret)
{
printk("静态指定设备号失败\n");
goto OUT2;
}
}
printk("设备号申请成功\n");
//4.注册驱动对象
ret=cdev_add(cdev,MKDEV(major,minor),3);
if(ret)
{
printk("注册设备驱动对象失败\n");
goto OUT3;
}
//5.向上提交目录
cls=class_create(THIS_MODULE,"led1");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret = -PTR_ERR(cls);
goto OUT4;
}
printk("向上提交目录成功\n");
//6.向上提交设备信息
dev = device_create(cls,NULL,MKDEV(major,1),NULL,"led1");
if(IS_ERR(dev))
{
printk("向上提交设备节点失败\n");
goto OUT5;
}
printk("向上提交设备节点成功\n");
//初始化led灯
vir_gpiof->moder &= (~(0x3 << 20));
vir_gpiof->moder |= (0x1 << 20);
vir_gpiof->odr &= (~(0x1 << 10));
printk("LED1硬件寄存器初始化成功\n");
return 0;
//跳转标签
OUT5:
device_destroy(cls,MKDEV(major,1));
class_destroy(cls);
OUT4:
cdev_del(cdev);
OUT3:
unregister_chrdev_region(MKDEV(major,minor),1);
OUT2:
kfree(cdev);
OUT1:
return ret;
}
static void __exit mycdev_exit(void)
{
//1.销毁设备信息
device_destroy(cls,MKDEV(major,1));
//2.销毁目录
class_destroy(cls);
//3.注销驱动对象
cdev_del(cdev);
//4.释放设备号
unregister_chrdev_region(MKDEV(major,minor),1);
//5.释放对象空间
kfree(cdev);
// 取消寄存器地址映射
iounmap(vir_gpioe);
iounmap(vir_rcc);
iounmap(vir_gpiof);
// 字符设备驱动的注销
unregister_chrdev(major, "led1");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
//声明
MODULE_LICENSE("GPL");
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include "led.h"
struct cdev *cdev;
struct class *cls;
struct device *dev;
unsigned int major=500;
unsigned int minor=2;
dev_t devno;
char kbuf[128]={0};
gpio_t *vir_gpioe;
gpio_t *vir_gpiof;
unsigned int *vir_rcc;
int mycdev_open(struct inode *inode, struct file *file)
{
int aa = inode->i_rdev; //得到设备号
file->private_data = (void *)MINOR(aa);
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int aa = (int)file->private_data; //次设备号
switch(cmd)
{
case LED_ON:
switch (aa)
{
case 2:
vir_gpioe->odr |= (0x1 << 8);
break;
}
break;
case LED_OFF:
switch (aa)
{
case 2:
vir_gpioe->odr &= (~(0x1 << 8));
break;
}
break;
}
return 0;
}
ssize_t mycdev_read(struct file *file, char *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 *ubuf, size_t size, loff_t *lof)
{
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//定义操作方法结构体变量并赋值
struct file_operations fops={
.open=mycdev_open,
.read=mycdev_read,
.write=mycdev_write,
.release=mycdev_close,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
//物理地址映射
vir_gpioe = ioremap(GPIOE, sizeof(gpio_t));
if(vir_gpioe == NULL)
{
printk("MODER寄存器映射失败\n");
return -EFAULT;
}
vir_gpiof = ioremap(GPIOF, sizeof(gpio_t));
if (vir_gpiof == NULL)
{
printk("MODER寄存器映射失败\n");
return -EFAULT;
}
vir_rcc = ioremap(RCC, 4);
if (vir_rcc == NULL)
{
printk("RCC寄存器映射失败\n");
return -EFAULT;
}
printk("寄存器映射成功\n");
int ret;
//1.申请对象空间
cdev = cdev_alloc();
if(NULL == cdev)
{
printk("申请对象空间失败\n");
ret = -EFAULT;
goto OUT1;
}
printk("申请对象空间成功\n");
//2.初始化对象
cdev_init(cdev, &fops);
//3.申请设备号
if(major == 0)
{
ret = alloc_chrdev_region(&devno,minor,1,"led2");
if(ret)
{
printk("动态申请设备号失败\n");
goto OUT2;
}
major = MAJOR(devno);
minor = MINOR(devno);
}
else
{
ret=register_chrdev_region(MKDEV(major,minor),1,"led2");
if(ret)
{
printk("静态指定设备号失败\n");
goto OUT2;
}
}
printk("设备号申请成功\n");
//4.注册驱动对象
ret=cdev_add(cdev,MKDEV(major,minor),1);
if(ret)
{
printk("注册设备驱动对象失败\n");
goto OUT3;
}
//5.向上提交目录
cls=class_create(THIS_MODULE,"led2");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret = -PTR_ERR(cls);
goto OUT4;
}
printk("向上提交目录成功\n");
//6.向上提交设备信息
dev = device_create(cls,NULL,MKDEV(major,2),NULL,"led2");
if(IS_ERR(dev))
{
printk("向上提交设备节点失败\n");
goto OUT5;
}
printk("向上提交设备节点成功\n");
//初始化led灯
vir_gpioe->moder &= (~(0x3 << 16));
vir_gpioe->moder |= (0x1 << 16);
vir_gpioe->odr &= (~(0x1 << 8));
printk("LED2硬件寄存器初始化成功\n");
return 0;
//跳转标签
OUT5:
device_destroy(cls,MKDEV(major,2));
class_destroy(cls);
OUT4:
cdev_del(cdev);
OUT3:
unregister_chrdev_region(MKDEV(major,minor),1);
OUT2:
kfree(cdev);
OUT1:
return ret;
}
static void __exit mycdev_exit(void)
{
//1.销毁设备信息
device_destroy(cls,MKDEV(major,2));
//2.销毁目录
class_destroy(cls);
//3.注销驱动对象
cdev_del(cdev);
//4.释放设备号
unregister_chrdev_region(MKDEV(major,minor),1);
//5.释放对象空间
kfree(cdev);
// 取消寄存器地址映射
iounmap(vir_gpioe);
iounmap(vir_rcc);
iounmap(vir_gpiof);
// 字符设备驱动的注销
unregister_chrdev(major, "led2");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
//声明
MODULE_LICENSE("GPL");
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include "led.h"
int main(int argc, char const *argv[])
{
int a, b;
char buf[128] = {0};
int fd_led1 = open("/dev/led0", O_RDWR);
if (fd_led1 < 0)
{
printf("打开LED1设备文件失败\n");
exit(-1);
}
int fd_led2 = open("/dev/led1", O_RDWR);
if (fd_led2 < 0)
{
printf("打开LED2设备文件失败\n");
exit(-1);
}
int fd_led3 = open("/dev/led2", O_RDWR);
if (fd_led3 < 0)
{
printf("打开LED3设备文件失败\n");
exit(-1);
}
while (1)
{
// 从终端读取
printf("请选择LED1灯功能\n");
printf("0(关) 1(开)>");
scanf("%d", &a);
printf("请输入要控制的灯\n");
printf("0(LED1) 1(LED2) 2(LED3)>");
scanf("%d", &b);
if (a == 1) // 开灯
{
switch (b)
{
case 0:
ioctl(fd_led1, LED_ON, b);
break;
case 1:
ioctl(fd_led2, LED_ON, b);
break;
case 2:
ioctl(fd_led3, LED_ON, b);
break;
}
}
else if (a == 0) // 关灯
{
switch (b)
{
case 0:
ioctl(fd_led1, LED_OFF, b);
break;
case 1:
ioctl(fd_led2, LED_OFF, b);
break;
case 2:
ioctl(fd_led3, LED_OFF, b);
break;
}
}
}
close(fd_led1);
close(fd_led2);
close(fd_led3);
return 0;
}