2023.10.20 LED驱动

驱动程序

cs 复制代码
#include<linux/init.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<linux/uaccess.h>
#include<linux/io.h>
#include"head.h"

unsigned int major;
unsigned int *vir_moder;
unsigned int *vir_odr;
unsigned int *vir_rcc;

char kbuf[128] = {0};
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){
    if( copy_to_user(ubuf, kbuf, size) ){
        printk("copy_to_user fail\n");
    }
    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){
    if( copy_from_user(kbuf, ubuf, size) ){
        printk("copy_from_user fail\n");
    }
    printk("%s:%s:%d\n",__FILE__, __func__, __LINE__);
    if(kbuf[0] == '0'){
        //灭灯
        (*vir_odr) &= ~(0x1 << 10);
    }
    else if(kbuf[0] == '1'){
        //亮灯
        (*vir_odr) |= (0x1 << 10);
    }
    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,
    
};

static int __init mycdev_init(void){
    major = register_chrdev(0,"mychrdev",&fops);
    if(major < 0){
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功major = %d\n",major);
    //进行寄存器的地址映射
    vir_moder = ioremap(PHY_LED1_MODER, 4);
    if(vir_moder == NULL){
        printk("物理内存地址映射失败%d\n", __LINE__);
        return -1;
    }
    vir_odr = ioremap(PHY_LED1_ODR, 4);
    if(vir_odr == NULL){
        printk("物理内存地址映射失败%d\n", __LINE__);
        return -1;
    }
    vir_rcc = ioremap(PHY_RCC, 4);
    if(vir_rcc == NULL){
        printk("物理内存地址映射失败%d\n", __LINE__);
        return -1;
    }
    //初始化寄存器
    (*vir_rcc) |= (0x1 << 4);
    (*vir_moder) &= ~(0x3 << 20);
    (*vir_moder) |= (0x1 << 20);
    (*vir_odr) &= ~(0x1 << 10); //默认关灯
    return 0;
}

static void __exit mycdev_exit(void){
    //取消内存映射
    iounmap(vir_moder);
    iounmap(vir_odr);
    iounmap(vir_rcc);
    //注销字符设备驱
    unregister_chrdev(major, "mychrdev");
}

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

应用程序

cs 复制代码
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<string.h>
int main(int argc, void const *argv[]){
    char buf[128] = {0};
    int fd = open("/dev/mychrdev", O_RDWR);
    if(fd < 0){
        printf("打开设备文件失败\n");
        return -1;
    }
    printf("打开设备文件成功\n");
    while(1){
        printf("输入0灭灯,输入1亮灯>>");
        fgets(buf,sizeof(buf),stdin);
        buf[strlen(buf)-1] = 0;

        write(fd, buf, sizeof(buf));
/*
        bzero(buf,sizeof(buf));
        read(fd, buf, sizeof(buf));
        printf("buf:%s\n", buf);
*/
    }
    close(fd);
    return 0;
}

头文件

cs 复制代码
#ifndef __HEAD_H__
#define __HEAD_H__

#define PHY_LED1_MODER 0X50006000
#define PHY_LED1_ODR 0X50006014
#define PHY_RCC 0X50000A28

#endif
相关推荐
她说彩礼65万4 分钟前
C# 中的锁
开发语言·c#
敲代码的 蜡笔小新4 小时前
【行为型之访问者模式】游戏开发实战——Unity灵活数据操作与跨系统交互的架构秘诀
unity·设计模式·c#·访问者模式
明耀5 小时前
WPF C# 用WebView加载H5页面(uniapp项目,vue项目)
uni-app·c#·wpf
我不是程序猿儿9 小时前
【C#】 lock 关键字
java·开发语言·c#
动感光博13 小时前
Unity序列化字段、单例模式(Singleton Pattern)
unity·单例模式·c#
黑洞视界14 小时前
NCC Mocha v0.2.0 发布, 新增对 Metrics 的支持
c#·.net·可观测性·observability
FAREWELL0007515 小时前
Unity基础学习(十五)核心系统——音效系统
学习·unity·c#·游戏引擎
zimoyin16 小时前
Java 快速转 C# 教程
java·开发语言·c#
向宇it16 小时前
【unity游戏开发——编辑器扩展】使用MenuItem自定义菜单栏拓展
开发语言·ui·unity·c#·编辑器·游戏引擎