点灯试验(设备绑定版)

作业:设备与设备文件绑定,实际LED灯点亮

test.c

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "head.h"
 
void LED_CONTROL()
{
    int which_led, on_or_off, fd;
    while (1)
    {
        printf("需要控制的灯1(LED1) 2(LED2) 3(LED3) > \n");
        scanf("%d", &which_led);
        switch (which_led)
        {
        case 1:
            fd = open("/dev/mycdev0", O_RDWR);
            break;
        case 2:
            fd = open("/dev/mycdev1", O_RDWR);
            break;
        case 3:
            fd = open("/dev/mycdev2", O_RDWR);
            break;
        default:
            printf("没有你要选的灯\n");
        exit(-1);
        }
 
        if (fd < 0)
        {
            printf("打开设备失败\n");
            exit(-1);
        }
        printf("实现功能:1(开灯) 0(关灯) > \n");
        scanf("%d", &on_or_off);
        switch (on_or_off)
        {
        case 1:
            ioctl(fd, LED_ON, &a);
            break;
        case 0:
            ioctl(fd, LED_OFF, &a);
            break;
        default:
            printf("选择错误。退出\n");
        exit(-1);
        }
        close(fd);
    }
}
 
int main(int argc, const char *argv[])
{
    LED_CONTROL();
    return 0;
}

mycdev.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/device.h>
#include "head.h"
 
unsigned int major = 0;
unsigned int minor = 0;
 
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;
 
struct cdev *cdev;
dev_t devnum;
struct class *cls;
struct device *dev;
struct semaphore sem;
 
int mycdev_open(struct inode *inode, struct file *file)
{   
    down(&sem);
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    unsigned int minor = MINOR(inode->i_rdev); // 获取操作的文件的次设备号
    file->private_data = (void *)minor;        // 将次设备号当做file对象的私有数据存放
    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)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
 
int mycdev_close(struct inode *inode, struct file *file)
{
    up(&sem);
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
 
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    unsigned int which = (unsigned int)file->private_data; // 获取open中得到的文件次设备号
    int ret = copy_from_user(&which, (unsigned int *)arg, 4);
    if (ret)
    {
        printk("copy_from_user is ERR\n");
        return ret;
    }
    switch (which)
    {
    case 1:
        switch (cmd)
        {
        case LED_ON:
            vir_led1->ODR |= 0x1 << 10;
            break;
        case LED_OFF:
            vir_led1->ODR &= (~(0x1 << 10));
            break;
        default:
            break;
        }
        break;
    case 2:
        switch (cmd)
        {
        case LED_ON:
            vir_led2->ODR |= 0x1 << 10;
            break;
        case LED_OFF:
            vir_led2->ODR &= (~(0x1 << 10));
            break;
        default:
            break;
        }
        break;
    case 3:
        switch (cmd)
        {
        case LED_ON:
            vir_led3->ODR |= 0x1 << 8;
            break;
        case LED_OFF:
            vir_led3->ODR &= (~(0x1 << 8));
            break;
        default:
            break;
        }
        break;
    default:
        break;
    }
    return 0;
}
 
// 寄存器地址映射
int all_led_init(void)
{
    vir_led1 = ioremap(PHY_LED1_ADDR, sizeof(gpio_t));
    if (vir_led1 == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
 
    vir_led2 = ioremap(PHY_LED2_ADDR, sizeof(gpio_t));
    if (vir_led2 == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_led3 = vir_led1;
 
    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if (vir_rcc == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    printk("物理内存映射成功\n");
    // rcc
    (*vir_rcc) |= 0x3 << 4;
    // LED1
    vir_led1->MODER &= (~(0x3 << 20));
    vir_led1->MODER |= 0x1 << 20;
    vir_led1->ODR &= (~(0x1 << 10));
    // LED2
    vir_led2->MODER &= (~(0x3 << 20));
    vir_led2->MODER |= 0x1 << 20;
    vir_led2->ODR &= (~(0x1 << 10));
    // LED3
    vir_led3->MODER &= (~(0x3 << 16));
    vir_led3->MODER |= 0x1 << 16;
    vir_led3->ODR &= (~(0x1 << 8));
 
    return 0;
}
 
struct file_operations fops = {
    .open = mycdev_open,
    .release = mycdev_close,
    .read = mycdev_read,
    .write = mycdev_write,
    .unlocked_ioctl = mycdev_ioctl,
};
 
static int __init mycdev_init(void)
{
    int ret;
    sema_init(&sem,1);
    // 1.分配字符设备驱动对象
    cdev = cdev_alloc();
    if (cdev == NULL)
    {
        printk("申请字符设备驱动对象失败\n");
        ret = -EFAULT;
        goto OUT1;
    }
    printk("申请字符设备驱动成功\n");
    // 2.初始化字符设备驱动对象
    cdev_init(cdev, &fops);
    // 3.申请设备号
    if (major > 0) // 静态指定
    {
        ret = register_chrdev_region(MKDEV(major, minor), 3, "mycdev");
        if (ret)
        {
            printk("静态指定设备号失败\n");
            goto OUT2;
        }
    }
    else
    {
        ret = alloc_chrdev_region(&devnum, minor, 3, "mycdev");
        if (ret)
        {
            printk("静态指定设备号失败\n");
            goto OUT2;
        }
        minor = MINOR(devnum);
        major = MAJOR(devnum);
    }
    printk("申请设备号成功\n");
    // 4.注册字符设备驱动对象
    ret = cdev_add(cdev, MKDEV(major, minor), 3);
    if (ret)
    {
        printk("注册字符设备驱动对象失败\n");
        goto OUT3;
    }
    printk("注册字符设备驱动对象成功\n");
 
    // 寄存器映射函数
    all_led_init();
 
    // 向上提交目录信息
    cls = class_create(THIS_MODULE, "mycdev");
    if (IS_ERR(cls))
    {
        printk("向上提交目录信息失败\n");
        ret = -PTR_ERR(cls);
        goto OUT4;
    }
    printk("向上提交目录信息成功\n");
 
    // 向上提交设备信息
    int i;
    for (i = 0; i < 3; i++)
    {
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "mycdev%d", i);
        if (IS_ERR(dev))
        {
            printk("向上提交设备节点失败\n");
            ret = -PTR_ERR(cls);
            goto OUT5;
        }
    }
    printk("向上提交设备节点成功\n");
    return 0;
 
OUT5:
    for (--i; i >= 0; i++)
        device_destroy(cls, MKDEV(major, i));
    class_destroy(cls);
OUT4:
    cdev_del(cdev);
OUT3:
    unregister_chrdev_region(MKDEV(major, minor), 3);
OUT2:
    kfree(cdev);
OUT1:
    return ret;
}
 
static void __exit mycdev_exit(void)
{
    // 取消物理映射
    iounmap(vir_led1);
    iounmap(vir_led2);
    iounmap(vir_rcc);
 
    // 销毁设备信息
    int i;
    for (i = 0; i < 3; i++)
        device_destroy(cls, MKDEV(major, i));
 
    // 销毁目录信息
    class_destroy(cls);
    // 注销字符设备驱动对象
    unregister_chrdev_region(MKDEV(major, minor), 3);
    // 释放空间对象
    kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

head.h

#ifndef __HEAD_H__
#define __HEAD_H__ 
typedef struct{
    unsigned int MODER;
    unsigned int OTYPER;
    unsigned int OSPEEDR;
    unsigned int PUPDR;
    unsigned int IDR;
    unsigned int ODR;
}gpio_t;
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR    0X50007000
#define PHY_LED3_ADDR 0X50006000
#define PHY_RCC_ADDR    0X50000A28
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)
#endif
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值