底层驱动day2作业

控制三盏灯亮灭

代码:

//head.h

#ifndef __HEAD_H__
#define __HEAD_H__
#define PHY_RCC 0x50000A28
#define PHY_GPIOE_MODER 0x50006000
#define PHY_GPIOF_MODER 0x50007000
#define PHY_GPIOE_ODR 0x50006014
#define PHY_GPIOF_ODR 0x50007014


#endif

//demo.c

#include<linux/init.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<linux/uaccess.h>
#include<linux/io.h>
#include"include/head.h"
char kbuf[128] = {0};
int a = 10;
unsigned int major;

unsigned int *vir_led1_odr;
unsigned int *vir_led1_moder;
unsigned int *vir_rcc;
unsigned int *vir_led2_odr;
unsigned int *vir_led2_moder;

int mycdev_open(struct inode *inode,struct file *file)
{
    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)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    int ret = copy_from_user(kbuf,ubuf,size);
    if(ret)
    {
        printk("failed\n");
        return -EIO;
    }
    switch(kbuf[0])     //kbuf的第0位为led灯,第2位为led灯的状态
    {
        case '1':
            if(kbuf[2] == '0')
            {
                //关灯
                (*vir_led1_odr) &= (~(0x1 << 10));
            }
            else if(kbuf[2] == '1')
            {
                //开灯
                (*vir_led1_odr) |= (0x1 << 10);
            }
            break;
        case '2':
            if(kbuf[2] == '0')
            {
                //关灯
                (*vir_led2_odr) &= (~(0x1 << 10));
            }
            else if(kbuf[2] == '1')
            {
                //开灯
                (*vir_led2_odr) |= (0x1 << 10);
            }
            break;
        case '3':
            if(kbuf[2] == '0')
            {
                //关灯
                (*vir_led1_odr) &= (~(0x1 << 8));
            }
            else if(kbuf[2] == '1')
            {
                //开灯
                (*vir_led1_odr) |= (0x1 << 8);
            }
            break;
        default:
            break;
    }
    
    
    
    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 = copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("failed\n");
        return -EIO;
    }
    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,
};

module_param(a,int,0664);
static int __init mycdev_init(void)
{
    printk("a = %d\n",a);
    major = register_chrdev(0,"mychrdev",&fops);
    if(major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功 major = %d\n",major);
    vir_led1_moder = ioremap(PHY_GPIOE_MODER,4); 
    if(NULL == vir_led1_moder)
    {
        printk("物理内存地址映射失败\n");
        return -EFAULT;
    } 
    vir_led1_odr = ioremap(PHY_GPIOE_ODR,4);
    if(NULL == vir_led1_odr)
    {
        printk("物理内存地址映射失败\n");
        return -EFAULT;
    } 

    vir_rcc = ioremap(PHY_RCC,4);
    if(NULL == vir_rcc)
    {
        printk("物理内存地址映射失败\n");
        return -EFAULT;
    }

    vir_led2_moder = ioremap(PHY_GPIOF_MODER,4); 
    if(NULL == vir_led2_moder)
    {
        printk("物理内存地址映射失败\n");
        return -EFAULT;
    } 

    vir_led2_odr = ioremap(PHY_GPIOF_ODR,4);
    if(NULL == vir_led2_odr)
    {
        printk("物理内存地址映射失败\n");
        return -EFAULT;
    } 
    printk("寄存器内存映射成功\n");
    (*vir_rcc) |= (0x1 << 4);
    (*vir_rcc) |= (0x1 << 5);
    (*vir_led1_moder) &= (~(0x3 << 20));    //led1_moder
    (*vir_led1_moder) |= (0x1 << 20);
    (*vir_led1_moder) &= (~(0x3 << 16));    //led3_moder
    (*vir_led1_moder) |= (0x1 << 16);
    (*vir_led1_odr) &= (~(0x1 << 10));      //led1_odr
    (*vir_led1_odr) &= (~(0x1 << 8));      //led3_odr
    (*vir_led2_moder) &= (~(0x3 << 20));    //led2_moder
    (*vir_led2_moder) |= (0x1 << 20);
    (*vir_led2_odr) &= (~(0X1 << 10));      //led2_odr


    return 0;
}

static void __exit mycdev_exit(void)
{
    unregister_chrdev(major,"mychrdev");

}

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

//test.c
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdio.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)
    {
        fgets(buf,sizeof(buf),stdin);
        buf[strlen(buf) - 1] = 0;
        write(fd,buf,sizeof(buf));
    }
    
    //read(fd,buf,sizeof(buf));
    
    memset(buf,0,sizeof(buf));
    read(fd,buf,sizeof(buf));
    printf("buf:%s\n",buf);
    close(fd);
    return 0;
}

实验现象:

相关推荐
嵌入式成长家2 个月前
[linux 驱动]i2c总线设备驱动详解与实战
linux驱动·i2c驱动·i2c总线·i2c设备
嵌入式成长家2 个月前
[linux 驱动]platform总线设备驱动详解与实战
linux·linux驱动·platform总线设备驱动
小白橘颂2 个月前
Linux驱动(三):字符设备驱动之杂项
linux·运维·驱动开发·microsoft·linux驱动·瑞芯微·rk3588s
托马斯-木3 个月前
7、杂项设备驱动
linux·linux驱动
_三分糖4 个月前
【Linux内核编程--模块机制】
android·linux·运维·服务器·linux内核·c·linux驱动
xiaoxilang6 个月前
解决RTC内核驱动的问题bm8563
c语言·软件开发·linux驱动·rtc·实时时钟
特立独行的猫a6 个月前
linux系统下操作I2C总线外设(imx6ull的oled显示屏i2c驱动笔记)
linux·linux驱动·i2c总线·oled屏
稚肩9 个月前
嵌入式linux移植篇之kernel
linux·嵌入式·linux驱动
努力的某某姚10 个月前
Linux驱动开发之杂项设备注册和Linux2.6设备注册
linux驱动·杂项设备注册·linux2.6设备注册·例程分享·api简介
mjmmm1 年前
linux驱动的学习 & 驱动开发初识
linux·驱动开发·系统架构·linux驱动·设备·linux系统结构