作业需求:
通过字符设备驱动分布操作进行6个灯的操作(ioctl)
实现过程:
头文件:
#ifndef __MYLED_H__
#define __MYLED_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
volatile unsigned int BSRR; // 0x18
volatile unsigned int LCKR; // 0x1C
volatile unsigned int AFRL; // 0x20
volatile unsigned int AFRH; // 0x24
volatile unsigned int BRR; // 0x28
volatile unsigned int res;
volatile unsigned int SECCFGR; // 0x30
}gpio_t;
#define PHY_RCC_LED1 0x50000A28
#define PHY_RCC_LED2 0x50000210
#define PHY_GPIOE_LED1 0x50006000
#define PHY_GPIOF_LED2 0x50007000
#define PHY_GPIOZ_LED3 0x54004000
#define UACCESS_BUF _IOW('a',1,char [128])
#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)
enum{
LED1,
LED2,
LED3,
LED4,
LED5,
LED6,
};
#endif // __MYLED_H__
源文件:
#include <linux/init.h>
#include <linux/module.h>
#include<linux/cdev.h>
#include<linux/fs.h>
#include<linux/slab.h>
#include<linux/uaccess.h>
#include<linux/io.h>
#include"mycdev.h"
struct cdev *cdev;
dev_t devno;
unsigned int minor=0;
#if 0
unsigned int major=0;
#else
unsigned int major=500;
#endif
struct class *cls;
struct device *dev;
char kbuf[128]={0};
#define CNAME "mycdev"
unsigned int* virt_rcc=NULL;
unsigned int* virt_rcc2=NULL;
gpio_t* virt_gpioe;
gpio_t* virt_gpiof;
gpio_t* virt_gpioz;
#define LED1_ON (virt_gpioe->ODR |= (0x1 << 10))
#define LED1_OFF (virt_gpioe->ODR &= (~(0x1 << 10)))
#define LED2_ON (virt_gpiof->ODR |= (0x1 << 10))
#define LED2_OFF (virt_gpiof->ODR &= (~(0x1 << 10)))
#define LED3_ON (virt_gpioe->ODR |= (0x1 << 8))
#define LED3_OFF (virt_gpioe->ODR &= (~(0x1 << 8)))
#define LED4_ON (virt_gpioz->ODR |= (0x1 << 5))
#define LED4_OFF (virt_gpioz->ODR &= (~(0x1 << 5)))
#define LED5_ON (virt_gpioz->ODR |= (0x1 << 6))
#define LED5_OFF (virt_gpioz->ODR &= (~(0x1 << 6)))
#define LED6_ON (virt_gpioz->ODR |= (0x1 << 7))
#define LED6_OFF (virt_gpioz->ODR &= (~(0x1 << 7)))
int mycdev_open(struct inode *inode, struct file *file)
{
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 *loff)
{
int ret;
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
if(size >sizeof(kbuf)) size=sizeof(kbuf);
ret = copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy to user is error\n");
return -EIO;
}
return size; //5.返回拷贝数据大小
}
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
int ret;
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
if(size >sizeof(kbuf)) size=sizeof(kbuf);
ret = copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy from user is error\n");
return -EIO;
}
printk("copy from user kbuf:%s\n",kbuf);
if(kbuf[0]==0)
{
if(kbuf[1]==1)
{
virt_gpioe->ODR |=(0x1<<10);
}else
{
virt_gpioe->ODR &=(~(0x1<<10));
}
}else if(kbuf[0]==1)
{
if(kbuf[1]==1)
{
virt_gpiof->ODR |=(0x1<<10);
}else
{
virt_gpiof->ODR &=(~(0x1<<10));
}
}else if(kbuf[0]==2)
{
if(kbuf[1]==1)
{
virt_gpioe->ODR |=(0x1<<8);
}else
{
virt_gpioe->ODR &=(~(0x1<<8));
}
}else if(kbuf[0]==3)
{
if(kbuf[1]==1)
{
virt_gpioz->ODR |=(0x1<<5);
}else
{
virt_gpioz->ODR &=(~(0x1<<5));
}
}else if(kbuf[0]==4)
{
if(kbuf[1]==1)
{
virt_gpioz->ODR |=(0x1<<6);
}else
{
virt_gpioz->ODR &=(~(0x1<<6));
}
}else if(kbuf[0]==5)
{
if(kbuf[1]==1)
{
virt_gpioz->ODR |=(0x1<<7);
}else
{
virt_gpioz->ODR &=(~(0x1<<7));
}
}
return size; //5.返回拷贝数据大小
}
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 args)
{
int whitch;
int ret;
switch(cmd)
{
case LED_ON:
ret = copy_from_user(&whitch,(void*)args,sizeof(int));
if(ret)
{
printk("copy from user is error\n");
return -EIO;
}
switch (whitch)
{
case LED1:
LED1_ON;
break;
case LED2:
LED2_ON;
break;
case LED3:
LED3_ON;
break;
case LED4:
LED4_ON;
break;
case LED5:
LED5_ON;
break;
case LED6:
LED6_ON;
break;
}
break;
case LED_OFF:
ret = copy_from_user(&whitch,(void*)args,sizeof(int));
if(ret)
{
printk("copy from user is error\n");
return -EIO;
}
switch (whitch)
{
case LED1:
LED1_OFF;
break;
case LED2:
LED2_OFF;
break;
case LED3:
LED3_OFF;
break;
case LED4:
LED4_OFF;
break;
case LED5:
LED5_OFF;
break;
case LED6:
LED6_OFF;
break;
}
break;
}
return 0;
}
const struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.unlocked_ioctl=mycdev_ioctl,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
/*
1.分配对象
2.对象初始化
3.设备资源的申请(设备号)
4.注册
5.向上提交目录
6.向上提交设备节点信息
*/
int ret,i;
cdev=cdev_alloc();
if(cdev==NULL)
{
printk("分配对象空间失败\n");
ret=-ENOMEM;
goto ERR1;
}
printk("分配对象空间成功\n");
cdev_init(cdev,&fops);
if(major==0)
{
ret=alloc_chrdev_region(&devno,minor,6,"mycfev");
if(ret)
{
printk("动态申请设备号失败\n");
goto ERR2;
}
major=MAJOR(devno);
minor=MINOR(devno);
}else if(major>0)
{
ret=register_chrdev_region(MKDEV(major,minor),6,"mycdev");
if(ret)
{
printk("静态申请设备号失败\n");
goto ERR2;
}
}
ret=cdev_add(cdev,MKDEV(major,minor),6);
if(ret)
{
printk("驱动对象注册进内核失败\n");
goto ERR3;
}
printk("驱动对象注册进内核成功\n");
cls = class_create(THIS_MODULE,"mycdev");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
goto ERR4;
}
printk("向上提交目录成功\n");
for(i=0;i<6;i++)
{
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"mycdev%d",i);
if(IS_ERR(dev))
{
printk("向上提交节点信息失败\n");
goto ERR5;
}
}
printk("向上提交设备节点成功\n");
virt_rcc = ioremap(PHY_RCC_LED1,4);
if(virt_rcc == NULL)
{
printk("rcc ioremap is error\n");
return -ENOMEM;
}
virt_rcc2 = ioremap(PHY_RCC_LED2,4);
if(virt_rcc2 == NULL)
{
printk("rcc2 ioremap is error\n");
return -ENOMEM;
}
virt_gpioe=ioremap(PHY_GPIOE_LED1,sizeof(gpio_t));
if(virt_gpioe== NULL)
{
printk("gpioe ioremap is error\n");
return -ENOMEM;
}
virt_gpiof=ioremap(PHY_GPIOF_LED2,sizeof(gpio_t));
if(virt_gpiof == NULL)
{
printk("odr ioremap is error\n");
return -ENOMEM;
}
virt_gpioz=ioremap(PHY_GPIOZ_LED3,sizeof(gpio_t));
if(virt_gpiof == NULL)
{
printk("odr ioremap is error\n");
return -ENOMEM;
}
*virt_rcc |=(0x3<<4);
*virt_rcc2 |=(0x1);
virt_gpioe->MODER &= (~(0x3<<20));
virt_gpioe->MODER |=(0x1<<20);
virt_gpioe->ODR&=(~(0x1<<10));
virt_gpiof->MODER &= (~(0x3<<20));
virt_gpiof->MODER |=(0x1<<20);
virt_gpiof->ODR &=(~(0x1<<10));
virt_gpioe->MODER &= (~(0x3<<16));
virt_gpioe->MODER |=(0x1<<16);
virt_gpioe->ODR &=(~(0x1<<8));
virt_gpioz->MODER &= (~(0x3<<10));
virt_gpioz->MODER |=(0x1<<10);
virt_gpioz->ODR &=(~(0x1<<5));
virt_gpioz->MODER &= (~(0x3<<12));
virt_gpioz->MODER |=(0x1<<12);
virt_gpioz->ODR &=(~(0x1<<6));
virt_gpioz->MODER &= (~(0x3<<14));
virt_gpioz->MODER |=(0x1<<14);
virt_gpioz->ODR &=(~(0x1<<7));
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),6);
ERR2:
kfree(cdev);
ERR1:
return ret;
}
static void __exit mycdev_exit(void)
{
/*
1.销毁设备节点
2.销毁目录
3.注册驱动对象
4.释放设备资源
5.释放对象空间
*/
int i;
for(i=0;i<6;i++)
{
device_destroy(cls,MKDEV(major,i));
}
class_destroy(cls);
cdev_del(cdev);
unregister_chrdev_region(MKDEV(major,minor),6);
kfree(cdev);
iounmap(virt_rcc);
iounmap(virt_rcc2);
iounmap(virt_gpioe);
iounmap(virt_gpiof);
unregister_chrdev(major,CNAME);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
测试文件:
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include<string.h>
#include "mycdev.h"
#include<sys/ioctl.h>
int main(int argc, char const *argv[])
{
char buf[128] = {0};
int fd=-1;
int whitch;
fd = open("/dev/mycdev0",O_RDWR);
if(fd==-1)
{
perror("open is error\n");
return -1;
}
while(1)
{
whitch = LED1;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
whitch = LED2;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
whitch = LED3;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
whitch = LED4;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
whitch = LED5;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
whitch = LED6;
ioctl(fd,LED_ON,&whitch);
sleep(1);
ioctl(fd,LED_OFF,&whitch);
sleep(1);
}
close(fd);
return 0;
}
实验结果:
![](https://i-blog.csdnimg.cn/blog_migrate/d3a8880e922f0ed191b1e7886180498a.png)