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
相关推荐
安木夕6 小时前
C#-Visual Studio宇宙第一IDE使用实践
前端·c#·.net
gregmankiw9 小时前
C#调用Rust动态链接库DLL的案例
开发语言·rust·c#
阿蒙Amon10 小时前
06. C#入门系列【自定义类型】:从青铜到王者的进阶之路
开发语言·c#
钢铁男儿13 小时前
C# 表达式和运算符(表达式和字面量)
开发语言·c#
林鸿群14 小时前
C#子线程更新主线程UI及委托回调使用示例
开发语言·c#
o0向阳而生0o14 小时前
63、.NET 异常处理
c#·.net·异常处理
SteveDraw17 小时前
C++动态链接库封装,供C#/C++ 等编程语言使用——C++动态链接库概述(总)
开发语言·c++·c#·封装·动态链接库
Kookoos17 小时前
性能剖析:在 ABP 框架中集成 MiniProfiler 实现性能可视化诊断
后端·c#·.net·abp vnext·miniprofiler
阿翰19 小时前
自动 GitHub Readme 20 种语言翻译平台 - OpenAiTx 开源免费
c#·.net
枫叶kx1 天前
1Panel运行的.net程序无法读取系统字体(因为使用了docker)
c#