当前位置: 首页 > news >正文

分步注册方式 编写驱动

作业:通过分步注册方式,编写LED灯驱动:(驱动文件mycdev.c 测试文件test.c 头文件head.h)

mycdev.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include "head.h"
struct cdev *cdev;
unsigned int major=0;
unsigned int minor=0;
dev_t devno;
int major;
char kbuf[128] = {0};
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;
struct class *cls;
struct device *dev;
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
long mycdev_ioctl(struct file *file,unsigned int cmd, unsigned long arg)
{
    int which;
    int ret = copy_from_user(&which,(unsigned int *)arg,4);
    if(ret)
    {
        printk("copy_from_user err\n");
        return ret;
    }
    switch (cmd)
    {
        case LED_ON://which LED is turned on
        switch(which)
        {
            case 1: //LED1
            vir_led1->ODR |= (0x1 << 10);
            break;
            case 2: //LED2
            vir_led2->ODR |= (0x1 << 10);
            break;
            case 3: //LED3
            vir_led3->ODR |= (0x1 << 8);
            break;
        }
        break;
        case LED_OFF: //which LED is turned off
        switch (which)
        {
            case 1:
            vir_led1->ODR &= (~(0x1 << 10));
            break;
             case 2:
            vir_led2->ODR &= (~(0X1 << 10));
            break;
            case 3:
            vir_led3->ODR &= (~(0X1 << 8));
            break;
        }
        break;
        return 0;
    }
 
}
int all_leds_init(void)
{
    vir_led1 = ioremap(PHY_LED1_ADDR, sizeof(gpio_t));
    if (vir_led1 == NULL)
    {
        printk("ioremap filed:%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_led2 = ioremap(PHY_LED2_ADDR, sizeof(gpio_t));
    if (vir_led2 == NULL)
    {
        printk("ioremap filed:%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_led3 = vir_led1;
    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if (vir_rcc == NULL)
    {
        printk("ioremap filed:%d\n", __LINE__);
        return -ENOMEM;
    }
    printk("ioremap success\n");

    // init regestors
    
    (*vir_rcc) |= (3 << 4);

    vir_led1->MODER &= (~(3 << 20));
    vir_led1->MODER |= (1 << 20);
    vir_led1->ODR &= (~(1 << 10));
 
    vir_led2->MODER &= (~(3 << 20));
    vir_led2->MODER |= (1 << 20);
 
    vir_led1->MODER &= (~(3 << 16));
    vir_led1->MODER |= (1 << 16);
    vir_led1->ODR &= (~(1 << 8));
    printk("init regestors success\n");
 
    return 0;

ssize_t mycdev_read(struct file *file, char  *ubuf, size_t size, loff_t *lof)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    int ret;
    ret=copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("copy_to_user filad\n");
        return ret;
    }
    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)
{
    int ret;
    //cdev allocate space
    cdev=cdev_alloc();
    if(cdev==NULL)
    {
        printk("cdev allocate space failed\n");
        ret=-EFAULT;
        goto OUT1;
    }
    printk("cdev allocate space success\n");
    cdev_init(cdev,&fops);
    if(major>0)
    {
        ret=register_chrdev_region(MKDEV(major,minor),3,"mycdev");
        if(ret)
        {
            printk("apply for cdev Number failed\n");
            goto OUT2;
        }
    }
    else
    {
        ret=alloc_chrdev_region(&devno,minor,3,"mycdev");
        if(ret)
        {
            printk("apply for cdev Number failed\n");
            goto OUT2;
        }
        minor=MINOR(devno);//minor of cdev Number
        major=MAJOR(devno);//major of cdev Number
        
    }
    printk("cdev Number:major=%d\n", major);
    printk("apply for cdev Number success\n");
    ret=cdev_add(cdev,MKDEV(major,minor),3);
    if(ret)
    {
        printk("regest cdev failed\n");
       goto OUT3;
    }
    printk("regest cdev sucess\n");
    cls=class_create(THIS_MODULE,"mycdev");
    if(IS_ERR(cls))
    {
        printk("submit catalog list failed\n");
        ret=-PTR_ERR(cls);
        goto OUT4;
    }
    printk("submit catalog list success\n");
    int i;
    for(i=0;i<3;i++)
    {
        dev=device_create(cls,NULL,MKDEV(major,i),NULL,"mycdev%d",i);
        if(IS_ERR(dev))
        {
            printk("submit cdev inod failed\n");
            ret=-PTR_ERR(dev);
            goto OUT5;
        }
    }
    printk("submit cdev inod success\n");
    all_leds_init();
    return 0;
OUT5:
    for(--i;i>=0;i--)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    class_destroy(cls);
OUT4:
    cdev_del(cdev);
OUT3:
    unregister_chrdev_region(MKDEV(major,minor),3);
OUT2:
    kfree(cdev);
OUT1:
    return ret;
}
 
static void __exit mycdev_exit(void)
{
    iounmap(vir_led1);
    iounmap(vir_led2);
    iounmap(vir_rcc);
    int i;
    for(i=0;i<3;i++)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    class_destroy(cls);
    cdev_del(cdev);
    unregister_chrdev_region(MKDEV(major,minor),3);
    kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

test.c

#include<stdlib.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<unistd.h>
#include<string.h>
#include<sys/ioctl.h>
#include"head.h"
 
int main(int argc, char const *argv[])
{
    char buf[128]={0};
    int a,b;
    int fd=open("/dev/mycdev0",O_RDWR);
    if(fd<0)
    {
        printf("open cdev failed\n");
        exit(-1);
    }
    while(1)
    {
        printf("turn on or turn off:1(on)0(off)>)");
        scanf("%d",&a);
        printf("select the LED:1(LED1) 2(LED2) 3(LED3)>");
        scanf("%d",&b);
        switch(a)
        {
            case 1:
                ioctl(fd,LED_ON,&b);
                break;
            case 0:
                ioctl(fd,LED_OFF,&b);
                break;
        }
    }   
    close(fd);
    return 0;
}

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_t;
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR    0X50007000
#define PHY_LED3_ADDR 0X50006000
#define PHY_RCC_ADDR    0X50000A28
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)
#endif
 

相关文章:

分步注册方式 编写驱动

作业&#xff1a;通过分步注册方式&#xff0c;编写LED灯驱动&#xff1a;&#xff08;驱动文件mycdev.c 测试文件test.c 头文件head.h) mycdev.c #include <linux/init.h> #include <linux/module.h> #include <linux/fs.h> #include <linux/io.h> …...

repmgr出现双主,并且时间线分叉,删除了最新的时间线节点

遇到的问题如下&#xff1a; 2023-08-17 20:24:21.566 CST [1556001] LOG: database system was interrupted; last known up at 2023-08-17 20:21:41 CST 2023-08-17 20:24:21.770 CST [1556001] LOG: restored log file "00000009.history" from archive cp: 无法…...

ThinkPHP中实现IP地址定位

在网站开发中&#xff0c;我们经常需要获取用户的地理位置信息以提供个性化的服务。一种常见的方法是通过IP地址定位。在本文中&#xff0c;我们将介绍如何在ThinkPHP框架中实现IP地址定位。 一、IP地址定位的基本原理 IP地址是Internet上的设备在网络中的标识符。每个设备都有…...

使用Python批量将Word文件转为PDF文件

说明&#xff1a;在使用Minio服务器时&#xff0c;无法对word文件预览&#xff0c;如果有需要的话&#xff0c;可以将word文件转为pdf文件&#xff0c;再存储到Minio中&#xff0c;本文介绍如何批量将word文件&#xff0c;转为pdf格式的文件&#xff1b; 安装库 首先&#xff…...

XDR解决方案成为了新的安全趋势

和当今指数倍增长的安全数据相比&#xff0c;安全人才的短缺带来了潜在的风险。几乎所有的公司&#xff0c;无论规模大小&#xff0c;在安全资源能力上都有限&#xff0c;需要过滤各种告警才能将分析量保持在可接受范围。但这样一来&#xff0c;潜在的威胁线索就可能被埋没&…...

001-Nacos 服务注册

目录 Nacos介绍注册中心架构面临问题源码分析实例注册-接口实例注册-入口实例注册-创建一个(Nacos)Service实例注册-注册(Nacos)Service Nacos 介绍 Dynamic Naming and Configuration Service 动态的命名和配置服务 反正可以实现注册中心的功能 注册中心架构 服务提供者 …...

71 # 协商缓存的配置:通过内容

对比&#xff08;协商&#xff09;缓存 比较一下再去决定是用缓存还是重新获取数据&#xff0c;这样会减少网络请求&#xff0c;提高性能。 对比缓存的工作原理 客户端第一次请求服务器的时候&#xff0c;服务器会把数据进行缓存&#xff0c;同时会生成一个缓存标识符&#…...

【服务器】Strace显示后台进程输出

今天有小朋友遇到一个问题 她想把2331509和2854637这两个进程调到前台来&#xff0c;以便于在当前shell查看这两个python进程的实时输出 我第一反应是用jobs -l然后fg &#xff08;参考这里&#xff09; 但是发现jobs -l根本没有输出&#xff1a; 原因是jobs看的是当前ses…...

centos如何安装libssl-dev libsdl-dev libavcodec-dev libavutil-dev ffmpeg

在 CentOS 系统上安装这些包可以按照以下步骤进行&#xff1a; 打开终端&#xff0c;使用 root 或具有管理员权限的用户登录。 使用以下命令安装 libssl-dev 包&#xff1a; yum install openssl-devel使用以下命令安装 libsdl-dev 包&#xff1a; yum install SDL-devel使用以…...

2022年12月 C/C++(二级)真题解析#中国电子学会#全国青少年软件编程等级考试

第1题:数组逆序重放 将一个数组中的值按逆序重新存放。例如,原来的顺序为8,6,5,4,1。要求改为1,4,5,6,8。 输入 输入为两行:第一行数组中元素的个数n(1 输出 输出为一行:输出逆序后数组的整数,每两个整数之间用空格分隔。 样例输入 5 8 6 5 4 1 样例输出 1 4 5 6 8 以下是…...

详谈MongoDB的那些事

概念区分 什么是关系型数据库 关系型数据库&#xff08;Relational Database&#xff09;是一种基于关系模型的数据库管理系统&#xff08;DBMS&#xff09;。在关系型数据库中&#xff0c;数据以表格的形式存储&#xff0c;表格由行和列组成&#xff0c;行表示数据记录&…...

企业电子招投标采购系统源码之电子招投标的组成 tbms

​ 功能模块&#xff1a; 待办消息&#xff0c;招标公告&#xff0c;中标公告&#xff0c;信息发布 描述&#xff1a; 全过程数字化采购管理&#xff0c;打造从供应商管理到采购招投标、采购合同、采购执行的全过程数字化管理。通供应商门户具备内外协同的能力&#xff0c;为…...

Android 13 添加自定义分区,恢复出厂设置不被清除

需求: 客户有些文件或数据,需要做得恢复出厂设置还存在,故需新增一个分区存储客户数据。 要求: a) 分区大小为50M b) 应用层可读可写 c) 恢复出厂设置后不会被清除 d) 不需要打包.img e) 不影响OTA升级 缺点: 1).通过代码在分区创建目录和文件,会涉及到SeLinux权限的修…...

改进YOLO系列:1.添加SE注意力机制

添加SE注意力机制 1. SE注意力机制论文&#xff12;. SE注意力机制原理&#xff13;. SE注意力机制的配置&#xff13;.&#xff11;common.py配置&#xff13;.&#xff12;yolo.py配置&#xff13;.&#xff13;yaml文件配置 1. SE注意力机制论文 论文题目&#xff1a;Squee…...

RP2040开发板自制树莓派逻辑分析仪

目录 前言 1 准备工作和前提条件 1.1 Raspberry Pi Pico RP2040板子一个 1.2 Firmware-LogicAnalyzer-5.0.0.0-PICO.uf2固件 1.3 LogicAnalyzer-5.0.0.0-win-x64软件 2 操作指南 2.1 按住Raspberry Pi Pico开发板的BOOTSEL按键&#xff0c;再接上USB接口到电脑 2.2 刷入…...

git clone -b与git pull origin <branch_name>的区别

git clone -b 和 git pull origin <branch_name> 都是用于在 Git 中操作分支的命令&#xff0c;但它们有不同的用途和行为。 git clone -b 这是在克隆仓库时指定要克隆的特定分支的命令。它用于在克隆一个仓库的同时指定要克隆的分支。例如&#xff0c;如果你只想克隆一…...

中期国际:MT4数据挖掘与分析方法:以数据为导向,制定有效的交易策略

在金融市场中&#xff0c;制定有效的交易策略是成功交易的关键。而要制定一份可靠的交易策略&#xff0c;数据挖掘与分析方法是不可或缺的工具。本文将介绍如何以数据为导向&#xff0c;利用MT4进行数据挖掘与分析&#xff0c;从而制定有效的交易策略。 首先&#xff0c;我们需…...

Linux命令(70)之bzip2

linux命令之bzip2 1.bzip2介绍 linux命令bzip2是用来压缩或解压缩文件名后缀为".bz2"的文件 2.bzip2用法 bzip2 [参数] filename bzip2常用参数 参数说明-d解压缩文件-t测试压缩文件是否正确-k压缩后&#xff0c;保留源文件-z强制压缩-f强制覆盖已存在的文件-v显…...

ubuntu下gif动态图片的制作

Gif图片比视频小, 比静态JPG图片形象生动, 更适用于产品展示和步骤演示等。各种各样的gif动图为大家交流提供很大的乐趣. 这里简单介绍ubuntu系统下gif图的制作。 一、工具安装: kazam和ffmpeg kazam是linux下的一款简单但是功能强大的屏幕录制工具. 它可录制声音并选择全屏录…...

56.linux 进程管理命令和用户管理命令

目录 一、进程管理命令 1.ps 2.pstree 3.kill 4.pkill 5.&后台运行程序 6.jobs 7.fg bg 8.top 二、用户管理命令 1.系统存储用户信息的文件 2.添加新用户 3.修改用户密码 4.删除用户 一、进程管理命令 1.ps 用于查看当前系统中运行的进程信息。它可以…...

Mac os 上的apt-get install 就是brew install

Mac os 上面不支持apt-get install ,但是有个 brew install可以代替。 Homebrew是Mac OS的包管理器&#xff0c;可以方便地安装各种需要的软件。 1.1 安装Homebrew 如果没有安装Homebrew&#xff0c;需要在终端输入以下命令进行安装&#xff1a; /usr/bin/ruby -e "$(…...

vue watch监听对象 新旧值一样

vue3中watch监听新旧值一样的处理方式 废话不多说&#xff0c;直接上代码 const objectReactive reactive({user: {id: 1,name: zhangsan,age: 18,}, }) watch(() > objectReactive.user,(n, o) > {console.log(n, o)if (JSON.stringify(n) JSON.stringify(o)) {retu…...

学习Vue:测试与调试

在Vue.js开发过程中&#xff0c;测试与调试是确保代码质量和稳定性的重要环节。本文将介绍单元测试与集成测试的概念&#xff0c;以及如何使用Vue Devtools进行调试。同时&#xff0c;也会分享一些常见问题的排查与解决方法&#xff0c;帮助您更好地进行测试和调试工作。 单元测…...

pg使用sql将文本字符串转换成时间格式

使用 PostgreSQL 数据库的 SQL 查询语句将文本字符串转换为时间格式&#xff0c;可以使用 to_timestamp 函数。 假设您的文本字符串时间格式为 “yyyy-MM-dd HH:mm:ss”&#xff0c;您可以使用以下 SQL 查询来转换&#xff1a; SELECT to_timestamp(2023-08-13 19:05:22, YYY…...

WordPress中实现层级文章的访问权限继承

这篇文章也可以在我的博客中查看 本文内容 在WordPress中存在层级文章的设定&#xff0c;常见于&#xff1a;Page、Custom Post Type 有时候我们需要让子文章的访问权“继承”于父文章&#xff0c;即&#xff1a; 当父文章为私有、草稿时&#xff0c;子文章也无法被公开访问…...

CSS常见单位汇总

像素&#xff08;px&#xff09;&#xff1a; 绝对单位&#xff0c;以屏幕上的实际像素为基准&#xff0c;最常用于具体的尺寸和位置表示。 百分比&#xff08;%&#xff09;&#xff1a; 相对单位&#xff0c;基于父元素的属性计算大小&#xff0c;如宽度、高度、边距等。 自适…...

LLM - 大模型评估指标之 BLEU

目录 一.引言 二.BLEU 简介 1.Simple BLEU 2.Modified BLEU 3.Modified n-gram precision 4.Sentence brevity penalty 三.BLEU 计算 1.计算句子与单个 reference 2.计算句子与多个 reference 四.总结 一.引言 机器翻译的人工评价广泛而昂贵&#xff0c;且人工评估可…...

http学习笔记3

第 11 章 Web 的攻击技术 11.1 针对 Web 的攻击技术 简单的 HTTP 协议本身并不存在安全性问题&#xff0c;因此协议本身几乎不会成为攻击的对象。应用 HTTP 协议的服务器和客户端&#xff0c;以及运行在服务器上的 Web 应用等资源才是攻击目标。目前&#xff0c;来自互联网的攻…...

【Redis】Redis 的主从同步

【Redis】Redis 的主从同步 很多企业都没有使用 Redis 的集群&#xff0c;但是至少都做了主从。有了主从&#xff0c;当主节点(Master) 挂掉的时候&#xff0c;运维让从节点 (Slave) 过来接管&#xff0c;服务就可以继续&#xff0c;否则主节点需要经过数据恢复和重启的过程&a…...

文本图片怎么转Excel?分享一些好用的方法

在处理数据时&#xff0c;Excel 是一个非常强大的工具&#xff0c;但有时候需要将文本和图片转换为 Excel 格式&#xff0c;这可能会让人感到困惑。在本文中&#xff0c;我们将介绍一些好用的方法&#xff0c;以便您能够轻松地将文本和图片转换成 Excel 格式。 将文本图片为Exc…...