驱动开发课程LED点亮

head.h

cs 复制代码
#ifndef __HEAD_H__
#define __HEAD_H__
#define PHY_LED1_MODER 0x50006000
#define PHY_LED1_ODR 0x50006014
#define PHY_RCC 0x50000A28

#define PHY_LED2_MODER 0x50007000
#define PHY_LED2_ODR 0x50007014

#define PHY_LED3_MODER 0x50006000
#define PHY_LED3_ODR 0x50006014

#endif

test.c

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,char 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));
    }
    close(fd);
    return 0;
}

demo.c

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;
char kbuf[128]={};
unsigned int *vir_led1_moder;
unsigned int *vir_led1_odr;
unsigned int *vir_led2_moder;
unsigned int *vir_led2_odr;
unsigned int *vir_led3_moder;
unsigned int *vir_led3_odr;
unsigned int *vir_rcc;
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)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    int ret;
    ret=copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("copy_to_user filed\n");
        return -EIO;
    }
    return 0;
}
ssize_t mycdev_write(struct file *file,const char *ubuf,size_t size,loff_t *lof)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    int ret;
    ret=copy_from_user(kbuf,ubuf,size);
    if(ret)
    {
        printk("copy_from_user filed\n");
        return -EIO;
    }
    if(kbuf[0] == '1')
    {
        if(kbuf[1] == '0')
        {
        (*vir_led1_odr) &= (~(0x1<<10));
        }
        else if(kbuf[1]=='1')
        {
        (*vir_led1_odr) |= (0x1<<10);
        }
    }
    else if(kbuf[0]=='2')
    {
        if(kbuf[1] == '0')
        {
        (*vir_led2_odr) &= (~(0x1<<10));
        }
        else if(kbuf[1]=='1')
        {
        (*vir_led2_odr) |= (0x1<<10);
        }
    }
    else if(kbuf[0]=='3')
    {
        if(kbuf[1] == '0')
        {
        (*vir_led3_odr) &= (~(0x1<<8));
        }
        else if(kbuf[1]=='1')
        {
        (*vir_led3_odr) |= (0x1<<8);
        }
    }
    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_led1_moder = ioremap(PHY_LED1_MODER,4);
    if(vir_led1_moder == NULL)
    {
        printk("物理内存地址映射失败%d\n",__LINE__);
        return -EFAULT;
    }
    vir_led1_odr = ioremap(PHY_LED1_ODR,4);
    if(vir_led1_odr == NULL)
    {
        printk("物理内存地址映射失败%d\n",__LINE__);
        return -EFAULT;
    }
    vir_led2_moder = ioremap(PHY_LED2_MODER,4);
    if(vir_led2_moder == NULL)
    {
        printk("物理内存地址映射失败%d\n",__LINE__);
        return -EFAULT;
    }
    vir_led2_odr = ioremap(PHY_LED2_ODR,4);
    if(vir_led2_odr == NULL)
    {
        printk("物理内存地址映射失败%d\n",__LINE__);
        return -EFAULT;
    }
    vir_led3_moder = ioremap(PHY_LED3_MODER,4);
    if(vir_led3_moder == NULL)
    {
        printk("物理内存地址映射失败%d\n",__LINE__);
        return -EFAULT;
    }
    vir_led3_odr = ioremap(PHY_LED3_ODR,4);
    if(vir_led3_odr == NULL)
    {
        printk("物理内存地址映射失败%d\n",__LINE__);
        return -EFAULT;
    }
    vir_rcc = ioremap(PHY_RCC,4);
    if(vir_rcc == NULL)
    {
        printk("物理内存地址映射失败%d\n",__LINE__);
        return -EFAULT;
    }
    printk("寄存器内存映射成功\n");

    (*vir_rcc) |= (0x1<<4);
    (*vir_rcc) |= (0x1<<5);
    (*vir_led1_moder) &= (~(0x3<<20));
    (*vir_led1_moder) |= (0x1<<20);
    (*vir_led1_odr) &= (~(0x1<<10));

    (*vir_led2_moder) &= (~(0x3<<20));
    (*vir_led2_moder) |= (0x1<<20);
    (*vir_led2_odr) &= (~(0x1<<10));

    (*vir_led3_moder) &= (~(0x3<<16));
    (*vir_led3_moder) |= (0x1<<16);
    (*vir_led3_odr) &= (~(0x1<<8));
    return 0;
}
static void __exit mycdev_exit(void)
{
    iounmap(vir_led1_moder);
    iounmap(vir_led1_odr);

    iounmap(vir_led2_moder);
    iounmap(vir_led2_odr);

    iounmap(vir_led3_moder);
    iounmap(vir_led3_odr);
    iounmap(vir_rcc);
    unregister_chrdev(major,"mychrdev");
}

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

现象

相关推荐
大鱼>1 小时前
因子挖掘+回测+RL交易:量化金融完整系统
人工智能·深度学习·算法·金融
c238562 小时前
《枚举算法 “翻译官”:用 enum class 给 “游游的礼盒” 算分》
c语言·c++·算法
noipp3 小时前
推荐题目:洛谷 P5843 [SCOI2012] Blinker 的噩梦
c语言·数据结构·c++·算法·游戏·洛谷·luogu
极客BIM工作室3 小时前
OpenCascade 倒角(Chamfer)算法完整逻辑解析
算法
冻柠檬飞冰走茶3 小时前
PTA基础编程题目集 7-20 打印九九口诀表(C语言实现)
c语言·开发语言·数据结构·算法
鱼子星_3 小时前
《算 · 法》题目解析篇(1):最近公共祖先问题,线段树,最长回文子序列
c++·算法·动态规划·递归
雾喔3 小时前
算法练习5
算法·代理模式
问商十三载3 小时前
RAG 检索召回越多越好?2026 信噪比模型深度解析
人工智能·算法·机器学习
额恩663 小时前
ResearchPilot 第三阶段总结报告
python·算法
Hi李耶3 小时前
【LeetCode】9-回文数
算法·leetcode·职场和发展