LED驱动点灯实验

目录

led.h

mycdev.c

main.c


实验说明

在串口工具输入:

'1'点亮三盏灯

'0'熄灭三盏灯

led.h

#ifndef __LED_H__
#define __LED_H__

#define PYH_RCC 0x50000A28
// GPIOE
#define PYH_GPIOE_MODER 0x50006000
#define PYH_GPIOE_ODR 0x50006014
// GPIOF
#define PYH_GPIOF_MODER 0x50007000
#define PYH_GPIOF_ODR 0x50007014


#endif

mycdev.c

#include "led.h"
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/uaccess.h>

unsigned int major; // 主设备号
char kbuf[128] = { 0 }; // 存储用户空间数据
int ret = 0; // 存储拷贝函数的返回值

// 定义指向映射后的虚拟地址首地址
unsigned int* vir_rcc;
unsigned int* vir_gpioe_moder;
unsigned int* vir_gpioe_odr;
unsigned int* vir_gpiof_moder;
unsigned int* vir_gpiof_odr;

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* iof)
{
    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 failed\n");
        return -EIO;
    }
    return 0;
}
ssize_t mycdev_write(struct file* file, const char* ubuf, size_t size, loff_t* iof)
{
    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 form user failed\n");
        return -EIO;
    }
    // 判断用户输入的指令
    if (kbuf[0] == '0') {
        (*vir_gpioe_odr) &= ~(0x1 << 10); // LED1灭灯
        (*vir_gpiof_odr) &= ~(0x1 << 10); // LED2灭灯
        (*vir_gpioe_odr) &= ~(0x1 << 8); // LED3灭灯
    } else if (kbuf[0] == '1') {
        (*vir_gpioe_odr) |= (0x1 << 10); // LED1亮灯
        (*vir_gpiof_odr) |= (0x1 << 10); // LED2亮灯
        (*vir_gpioe_odr) |= (0x1 << 8); // LED3亮灯
    }
    return 0;
}
int mycdev_close(struct inode* inode, struct file* file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

// 定义一个操作方法结构体变量并初始化
struct file_operations fops = {
    .open = mycdev_open,
    .read = mycdev_read,
    .write = mycdev_write,
    .release = mycdev_close,
};

// led相关寄存器初始化
void led_init(void)
{
    (*vir_rcc) |= (0x3 << 4); // 对应GPIOE/GPIOF组使能

    (*vir_gpioe_moder) &= ~(0x3 << 20); // 设置PE10引脚为输出模式
    (*vir_gpioe_moder) |= (0x1 << 20);
    (*vir_gpioe_moder) &= ~(0x3 << 16); // 设置PE8引脚为输出模式
    (*vir_gpioe_moder) |= (0x1 << 16);
    (*vir_gpiof_moder) &= ~(0x3 << 20); // 设置PF10引脚为输出模式
    (*vir_gpiof_moder) |= (0x1 << 20);

    (*vir_gpioe_odr) &= ~(0x1 << 10); // 设置PE10引脚输出低电平,灭灯
    (*vir_gpioe_odr) &= ~(0x1 << 8); // 设置PE8引脚输出低电平,灭灯
    (*vir_gpiof_odr) &= ~(0x1 << 10); // 设置PF10引脚输出低电平,灭灯

    printk("相关寄存器初始化完成\n");
}

// LED相关寄存器物理地址映射
int led_ioremap(void)
{
    if (NULL == (vir_rcc = ioremap(PYH_RCC, 4))) {
        printk("RCC物理地址映射失败\n");
        return -EFAULT;
    }
    if (NULL == (vir_gpioe_moder = ioremap(PYH_GPIOE_MODER, 4))) {
        printk("GPIOE_MODER物理地址映射失败\n");
        return -EFAULT;
    }
    if (NULL == (vir_gpioe_odr = ioremap(PYH_GPIOE_ODR, 4))) {
        printk("GPIOE_ODR物理地址映射失败\n");
        return -EFAULT;
    }
    if (NULL == (vir_gpiof_moder = ioremap(PYH_GPIOF_MODER, 4))) {
        printk("GPIOF_MODER物理地址映射失败\n");
        return -EFAULT;
    }
    if (NULL == (vir_gpiof_odr = ioremap(PYH_GPIOF_ODR, 4))) {
        printk("GPIOF_ODR物理地址映射失败\n");
        return -EFAULT;
    }
    printk("物理地址映射成功\n");
    return 0;
}

// 取消物理地址映射
void led_iounmap(void){
    iounmap(vir_rcc); // 取消RCC寄存器物理地址映射
    iounmap(vir_gpioe_moder); // 取消GPIOE组MODER寄存器物理地址映射
    iounmap(vir_gpioe_odr); // 取消GPIOE组ODR寄存器物理地址映射
    iounmap(vir_gpiof_moder); // 取消GPIOF组MODER寄存器物理地址映射
    iounmap(vir_gpiof_odr); // 取消GPIOF组ODR寄存器物理地址映射
}

static int __init mycdev_init(void)
{
    // 注册字符设备驱动
    major = register_chrdev(0, "mycdev", &fops);
    if (major < 0) {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功major=%d\n", major);

    // LED相关寄存器物理地址映射
    if (led_ioremap()) {
        return -EFAULT;
    }

    // led相关寄存器初始化
    led_init();

    return 0;
}

static void __exit mycdev_exit(void)
{
    // 取消物理地址映射
    led_iounmap();
    // 字符设备驱动的注销
    unregister_chrdev(major, "mycdev");
}

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

main.c

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, const char* argv[])
{
    char buf[128] = { 0 };
    int fd = open("/dev/mycdev", O_RDWR);
    if (fd < 0) {
        printf("open 设备文件失败\n");
        exit(-1);
    }
    while (1) {
        printf("input 1(亮灯)/0(灭灯) >");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf) - 1] = '\0';
        write(fd, buf, sizeof(buf));

        // printf("buf: %s\n", buf);
    }

    close(fd);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/liu319293960_/article/details/131192887