2022/3/22作业

作业:

        通过字符设备驱动分布操作进行6个灯的操作(ioctl)

头文件:

#ifndef __MYLED_H__
#define __MYLED_H__

//GPIO结构体封装
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;      //0x04
    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;

//RCC基地址:0x50000A28
#define PHY_RCC_LED 0x50000A28

//GPIOE基地址:0x50006000
#define GPIOE_ADDR 0x50006000

//GPIOF基地址:0x50007000
#define GPIOF_ADDR 0x50007000


#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)
enum
{
	LED1,
	LED2,
	LED3,
    /*
    LED4,
    LED5,
    LED6,
    */
};

#endif

应用层文件

#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 <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include "test.h"

typedef void(*sighander_t)(int);
int i = 0;
//进程处理函数
void fork_fun()
{
    int fd = 0;
    int whitch = 0;
    while(1)
    {
        fd = open("/dev/mycdev0",O_RDWR);
        if(fd == -1)
        {
            perror("open is error\n");
            return ;
        }
        printf("当前进程:%d\n",i+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);
        close(fd);
    }
}

void hander(int a)
{
    while(waitpid(-1,NULL,WNOHANG)>0);
}

int main(int argc,const char * argv[])
{
    //信号的方式回收僵尸进程
    sighander_t s = signal(17,hander);
    if(SIG_ERR == s)
    {
        perror("signal");
        return -1;
    }
    pid_t pid = 0;
    for(i=0;i<3;i++)
    {
        pid = fork();
        if(pid<0)
        {
            printf("进程创建失败");
            return -1;
        }
        if(0 == pid)
        {
            printf("[%d]:进程创建成功\n",i+1);
            fork_fun();
            exit(0);
        }
    }
    while(1);
    return 0;
}

内核文件

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/io.h>
#include "test.h"

#define CNAME "mycdv"

gpio_t* virt_gpioe = NULL;
gpio_t* virt_gpiof = NULL;
unsigned int* virt_rcc = 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)))

struct class * cls;
struct device * dev;
unsigned int minor = 0;
//定义信号量
struct semaphore sem;

#if 0
unsigned int major = 0; //动态申请主设备号
#else
unsigned int major = 500;   //静态申请主设备号
#endif
int myled_open(struct inode *inode, struct file *file)
{
    //获取信号量
    down(&sem);
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    printk("myled_open\n");
    return 0;
}
ssize_t myled_read (struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    printk("myled_read\n");
    return 0;
}
ssize_t myled_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    printk("myled_write\n");
    return 0;
}
int myled_close (struct inode *inode, struct file *file)
{
    //解锁
    up(&sem);
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    printk("myled_close\n");
    return 0;
}
 
long myled_ioctl (struct file *file, unsigned int cmd, unsigned long args)
{

    //1.判断cmd switch(cmd)
    //2.判断操作哪盏灯进行点亮 copy_from_user
    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;
        }
    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,
};

//对象指针
struct cdev *cdev_p = NULL;
int ret = 0;
int i = 0;
dev_t  dev_val = 0; 

static int __init mycdev_init(void)
{
    //初始化信号量
    sema_init(&sem,1);
    //1.分配对象
    cdev_p = cdev_alloc();
    if(NULL == cdev_p)
    {
        printk("分配对象空间失败\n");
        ret = -ENOMEM;
        goto ERR1;
    }
    printk("分配对象空间成功\n");

    //2.对象初始化
    cdev_init(cdev_p,&fops);

    //3.设备资源的申请(设备号)
    if(0 == major)
    {
        ret = alloc_chrdev_region(&dev_val,minor,3,"mycdev");
        if(ret)
        {
            printk("动态申请设备号失败\n");
            goto ERR2;
        }
        major = MAJOR(dev_val); //获取主设备号
        minor = MINOR(dev_val); //获取次设备号
    }
    else if(major>0)
    {
        ret = register_chrdev_region(MKDEV(major,minor),3,"mycdev");
        if(ret)
        {
            printk("静态申请设备号失败\n");
            goto ERR2;
        }
    }

    //4.注册
    ret = cdev_add(cdev_p,MKDEV(major,minor),3);
    if(ret)
    {
        printk("驱动对象注册进内核失败\n");
        goto ERR3;
    }
    printk("驱动对象注册进内核成功\n");

    //5.向上提交目录
    cls = class_create(THIS_MODULE,"mycdev");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        goto ERR4;
    }
    printk("向上提交目录成功\n");

    //6.向上提交设备节点信息
    for(i=0;i<3;i++)
    {
        dev = device_create(cls,NULL,MKDEV(major,i),NULL,"mycdev%d",i);
        if(IS_ERR(dev))
        {
            printk("向上提交节点信息失败\n");
            goto ERR5;
        }
    }
    printk("向上提交节点信息成功\n");

    //4.将物理地址映射为虚拟地址
    //4.1 将rcc地址映射
    virt_rcc = ioremap(PHY_RCC_LED,4);
    if(virt_rcc == NULL)
    {
        printk("rcc ioremap is error\n");
        return -ENOMEM;
    }
    //映射GPIOE地址
    virt_gpioe = ioremap(GPIOE_ADDR,sizeof(gpio_t));
    if(virt_gpioe== NULL)
    {
        printk("virt_gpioe ioremap is error\n");
        return -ENOMEM;
    }
    //映射GPIOF地址
    virt_gpiof = ioremap(GPIOF_ADDR,sizeof(gpio_t));
    if(virt_gpiof== NULL)
    {
        printk("virt_gpiof ioremap is error\n");
        return -ENOMEM;
    }

    //5.对led1---->PE10引脚初始化  对led3---->PE8引脚初始化 
    *virt_rcc |= (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 |= (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引脚输出低电平

    return 0;
ERR5:
    for(--i;i>=0;i--)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    class_destroy(cls);

ERR4:
    cdev_del(cdev_p);
ERR3:
    unregister_chrdev_region(MKDEV(major,minor),3);
ERR2:
    kfree(cdev_p);
ERR1:
    return ret;

}
 
static void __exit mycdev_exit(void)
{
    
    
    //1.销毁设备节点
    //2.销毁目录
    //3.注销驱动对象
    //4.释放设备资源(设备号)
    //5.释放对象空间
    int i = 0;
    //2.取消地址映射
    iounmap(virt_rcc);
    iounmap(virt_gpioe);
    iounmap(virt_gpiof);
    for(i=0;i<3;i++)
        {
            device_destroy(cls,MKDEV(major,i));
        }
    class_destroy(cls);

    cdev_del(cdev_p);
    unregister_chrdev_region(MKDEV(major,minor),3);
    kfree(cdev_p);

}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

实验现象:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大鱼YY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值