字符设备驱动三盏LED灯实验

编写驱动代码(字符设备驱动注册与注销,用户空间与内核空间进行数据传递,映射地址,断开映射地址)

编译驱动代码(编写相关Makefile)

安装(insmod)

手动创建设备文件(mknod 设备文件路径及名字 c 主设备号 次设备号)

编写应用程序测试

相关代码:

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;

#define GPIOE 0x50006000
#define GPIOF 0x50007000
#define RCC 0x50000A28 

#endif

驱动代码(mycdev.c)

#include 
#include 
#include 
#include 
#include 
#include "head.h"

char kbuf[128] = {0};

// 虚拟地址
gpio *vir_gpioe;
gpio *vir_gpiof;
int *vir_rcc;

// 封装操作方法
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 *ubuf, size_t size, loff_t *lof)
{
    int ret;
    if (sizeof(kbuf) < size)
        size = sizeof(kbuf);
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret)
    {
        printk("copy_to_user failed\n");
        return -EIO;
    }
    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)
{
    int ret;
    if (sizeof(kbuf) < size)
        size = sizeof(kbuf);
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret)
    {
        printk("copy_from_user failed\n");
        return -EIO;
    }
    switch (kbuf[0])
    {
    case '1':               // 第一盏灯
        if (kbuf[1] == '0') // 灭灯
        {
            vir_gpioe->odr &= (~(0x1 << 10));
        }
        else if (kbuf[1] == '1') // 亮灯
        {
            vir_gpioe->odr |= (0x1 << 10);
        }
        break;
    case '2':               // 第二盏灯
        if (kbuf[1] == '0') // 灭灯
        {
            vir_gpiof->odr &= (~(0x1 << 10));
        }
        else if (kbuf[1] == '1') // 亮灯
        {
            vir_gpiof->odr |= (0x1 << 10);
        }
        break;
    case '3':               // 第三盏灯
        if (kbuf[1] == '0') // 灭灯
        {
            vir_gpioe->odr &= (~(0x1 << 8));
        }
        else if (kbuf[1] == '1') // 亮灯
        {
            vir_gpioe->odr |= (0x1 << 8);
        }
        break;
    default:
        break;
    }
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

// 存放主设备号
int major;
// 定义操作方法结构体变量并初始化
struct file_operations fops =
    {
        .open = mycdev_open,
        .read = mycdev_read,
        .write = mycdev_write,
        .release = mycdev_close,
};



static int __init mycdev_init(void)
{
    // 注册字符设备驱动
    major = register_chrdev(major, "mychrdev", &fops);
    if (major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return -1;
    }
    printk("字符设备驱动注册成功,major=%d\n", major);
    // 地址映射
    vir_gpioe = ioremap(GPIOE, sizeof(gpio));
    if (vir_gpioe == NULL)
    {
        printk("gpioe寄存器映射失败\n");
        return -EFAULT;
    }
    vir_gpiof = ioremap(GPIOF, sizeof(gpio));
    if (vir_gpiof == NULL)
    {
        printk("gpiof寄存器映射失败\n");
        return -EFAULT;
    }
    vir_rcc = ioremap(RCC, 4);
    if (vir_rcc == NULL)
    {
        printk("rcc寄存器映射失败\n");
        return -EFAULT;
    }
    printk("寄存器映射成功\n");

    // 寄存器初始化
    // rcc寄存器使能
    (*vir_rcc) |= (0x3 << 4);
    // 设置为输出
    // led1
    vir_gpioe->moder &= (~(0x3 << 20));
    vir_gpioe->moder |= (0x1 << 20);
    // led2
    vir_gpiof->moder &= (~(0x3 << 20));
    vir_gpiof->moder |= (0x1 << 20);
    // led3
    vir_gpioe->moder &= (~(0x3 << 16));
    vir_gpioe->moder |= (0x1 << 16);
    // 默认为灯灭
    vir_gpioe->odr &= (~(0x1 << 10));//led1
    vir_gpiof->odr &= (~(0x1 << 10));//led2
    vir_gpioe->odr &= (~(0x1 << 8));//led3

    printk("寄存器初始化成功\n");
    return 0;
}
static void __exit mycdev_exit(void)
{
    unregister_chrdev(major, "mychrdev");
    //断开映射
    iounmap(vir_gpioe);
    iounmap(vir_gpiof);
    iounmap(vir_rcc);
    printk("断开映射成功\n");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

应用程序代码(test.c)

#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, const char *argv[])
{
    char buf[128] = {0};
    int fd = open("/dev/mychrdev", O_RDWR);
    if (fd < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while (1)
    {
        printf("控制第几盏LED灯(一共三盏灯),亮1,灭0 ex:11>");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf) - 1] = '\0';
        write(fd, buf, sizeof(buf));
    }
    close(fd);

    return 0;
}

你可能感兴趣的:(c语言,stm32,开发语言)