题目:通过字符设备驱动分布操作进行6个灯的操作(ioctl)(优快云)
代码:
mycdev.h
#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;
enum{
LED1,
LED2,
LED3,
LED4,
LED5,
LED6
};
typedef struct
{
int width;
int high;
}image_t;
//GPIOE基地址
#define PHY_GPIOE_ADDR 0x50006000
#define PHY_GPIOF_ADDR 0x50007000
#define PHY_GPIOZ_ADDR 0x54004000
//RCC基地址:0x50000A28
#define PHY_RCC_LED_123 0x50000A28
#define PHY_RCC_LED_456 0x54000210
#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)
#define UACCESS_BUF _IOW('a',1,char[128])
#define UACCESS_STRUCT _IOW('a',1,image_t)
#endif
mycdev.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "myled.h"
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/slab.h>
//#define CNAME "myled"
struct class *cls;
struct device *dev;
char kbuf[128] = {0};
gpio_t *virt_gpioe = NULL;
gpio_t *virt_gpiof = NULL;
gpio_t *virt_gpioz = NULL;
unsigned int *virt_rcc_123 = NULL;
unsigned int *virt_rcc_456 = NULL;
#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)))
struct cdev *cdev;
dev_t devno;
unsigned int minor=0;
#if 0
unsigned int majour=0;
#else
unsigned int major=500;
#endif
int myled_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t myled_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);// 1.校验传输数据的大小,如果用户空间写的数据比内核空间数据大小大,需要更正大小
ret = copy_to_user(ubuf, kbuf, size);// 2.将数据从用户空间拷贝到内核空间
if (ret) // 3.判断是否错误
{
printk("copy to user is error\n");
return -EIO;
}
return size; // 5.返回拷贝数据大小
}
ssize_t myled_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);// 1.校验传输数据的大小,如果用户空间写的数据比内核空间数据大小大,需要更正大小
ret = copy_from_user(kbuf, ubuf, size);// 2.将数据从用户空间拷贝到内核空间
if (ret) // 3.判断是否错误
{
printk("copy from user is error\n");
return -EIO;
}
return size; // 5.返回拷贝数据大小
}
int myled_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
long myled_ioctl(struct file *file, unsigned int cmd, unsigned long args)
{
int whitch;
int ret;
char buf[128];
image_t image;
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 = myled_open,
.read = myled_read,
.write = myled_write,
.unlocked_ioctl = myled_ioctl,
.release = myled_close,
};
static int __init mycdev_init(void)
{
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,1,"mycdev");
if(ret)
{
printk("动态申请设备号失败\n");
goto ERR2;
}
major=MAJOR(devno);
minor=MINOR(devno);
}
else if(major>0)
{
ret=register_chrdev_region(MKDEV(major,minor),1,"mycdev");
if(ret)
{
printk("静态设备号失败\n");
goto ERR2;
}
}
ret=cdev_add(cdev,MKDEV(major,minor),1);
if(ret)
{
printk("驱动对象注册内核失败\n");
goto ERR3;
}
printk("驱动对象注册进内核失败\n");
cls=class_create(THIS_MODULE,"mycdev");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
goto ERR4;
}
printk("向上提交目录成功\n");
dev=device_create(cls,NULL,MKDEV(major,0),NULL,"mycdev");
if(IS_ERR(dev))
{
printk("向上提交节点信息失败\n");
goto ERR5;
}
printk("向上提交设备节点成功\n");
virt_rcc_123 = ioremap(PHY_RCC_LED_123, 4);
if (virt_rcc_123 == NULL)
{
printk("rcc ioremap is error\n");
return -ENOMEM;
}
virt_rcc_456 = ioremap(PHY_RCC_LED_456, 4);
if (virt_rcc_456 == NULL)
{
printk("rcc ioremap is error\n");
return -ENOMEM;
}
virt_gpioe = ioremap(PHY_GPIOE_ADDR, sizeof(gpio_t));
if (virt_gpioe == NULL)
{
printk("virt_gpioe ioremap is error\n");
return -ENOMEM;
}
virt_gpiof = ioremap(PHY_GPIOF_ADDR, sizeof(gpio_t));
if (virt_gpiof == NULL)
{
printk("virt_gpiof ioremap is error\n");
return -ENOMEM;
}
virt_gpioz = ioremap(PHY_GPIOZ_ADDR, sizeof(gpio_t));
if (virt_gpioz == NULL)
{
printk("virt_gpiof ioremap is error\n");
return -ENOMEM;
}
// 5.对led1---->PE10引脚初始化 对led3---->PE8引脚初始化
*virt_rcc_123 |= (0x1 << 4); // 5.1 使能GPIOE组时钟[4]=1
virt_gpioe->MODER &= (~(0x3 << 20)); // 5.2 设置PE10引脚为输出模式 [21:20] = 01
virt_gpioe->MODER |= (0x1 << 20);
virt_gpioe->ODR &= (~(0x1 << 10)); // 5.3 设置PE10引脚输出低电平
// 6. 对led2---->PF10引脚初始化
*virt_rcc_123 |= (0x1 << 5); // 5.1 使能GPIOF组时钟[5]=1
virt_gpiof->MODER &= (~(0x3 << 20)); // 5.2 设置PF10引脚为输出模式 [21:20] = 01
virt_gpiof->MODER |= (0x1 << 20);
virt_gpiof->ODR &= (~(0x1 << 10)); // 5.3 设置PF10引脚输出低电平
// 7.对led3---->PE8引脚初始化
virt_gpioe->MODER &= (~(0x3 << 16)); // 5.2 设置PE8引脚为输出模式 [17:16] = 01
virt_gpioe->MODER |= (0x1 << 16);
virt_gpioe->ODR &= (~(0x1 << 8)); // 5.3 设置PE8引脚输出低电平
// 6. 对led4---->PZ5引脚初始化
*virt_rcc_456 |= (0x1 << 0); // 5.1 使能GPIOF组时钟[5]=1
virt_gpioz->MODER &= (~(0x3 << 10)); // 5.2 设置PF10引脚为输出模式 [21:20] = 01
virt_gpioz->MODER |= (0x1 << 10);
virt_gpioz->ODR &= (~(0x1 << 5)); // 5.3 设置PF10引脚输出低电平
// 6. 对led5---->PZ6引脚初始化
virt_gpioz->MODER &= (~(0x3 << 12)); // 5.2 设置PF10引脚为输出模式 [21:20] = 01
virt_gpioz->MODER |= (0x1 << 12);
virt_gpioz->ODR &= (~(0x1 << 6)); // 5.3 设置PF10引脚输出低电平
// 6. 对led6---->PZ7引脚初始化
virt_gpioz->MODER &= (~(0x3 << 14)); // 5.2 设置PF10引脚为输出模式 [21:20] = 01
virt_gpioz->MODER |= (0x1 << 14);
virt_gpioz->ODR &= (~(0x1 << 7)); // 5.3 设置PF10引脚输出低电平
return 0;
ERR5:
device_destroy(cls,MKDEV(major,0));
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(MKDEV(major,minor),1);
ERR2:
kfree(cdev);
ERR1:
return ret;
}
static void __exit mycdev_exit(void)
{
device_destroy(cls,MKDEV(major,0));
class_destroy(cls);
cdev_del(cdev);
unregister_chrdev_region(MKDEV(major,minor),1);
kfree(cdev);
// 2.取消地址映射
iounmap(virt_rcc_123);
iounmap(virt_rcc_456);
iounmap(virt_gpioe);
iounmap(virt_gpiof);
iounmap(virt_gpioz);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
text.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include "myled.h"
/// @brief
/// @param argc
/// @param argv
/// @return
int main(int argc, const char *argv[])
{
int whitch;
char buf[128] = {0};
image_t image;
image.high=200;
image.width=10;
int fd = -1;
fd = open("/dev/mycdev", O_RDWR);
if (fd == -1)
{
perror("open is error\n");
return -1;
}
/*
ioctl(fd,UACCESS_STRUCT,&image);
printf("用户空间: width=%d high=%d\n",image.width,image.high);
printf("please input------>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
ioctl(fd,UACCESS_BUF,buf);
*/
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;
}