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
相关推荐
Scout-leaf1 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530142 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
mudtools3 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的3 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21883 天前
.NET 本地Db数据库-技术方案选型
windows·c#
lindexi3 天前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice
qq_454245033 天前
基于组件与行为的树状节点系统
数据结构·c#
bugcome_com3 天前
C# 类的基础与进阶概念详解
c#
雪人不是菜鸡3 天前
简单工厂模式
开发语言·算法·c#
铸人3 天前
大数分解的Shor算法-C#
开发语言·算法·c#