驱动开发1

作者 : admin 本文共3189个字,预计阅读时间需要8分钟 发布时间: 2024-06-16 共1人阅读

控制三灯循环亮

demo.c

#include 
#include 
#include 
#include 
#include 
#include "myled.h"

#define CNAME "myled"
unsigned int major = 0;
char kbuf[100] = "";

unsigned int* rcc_virt = NULL; //定义rcc虚拟地址
gpio_t* gpioe_virt = NULL; //定义gpioe组虚拟地址
gpio_t* gpiof_virt = NULL; //定义gpiof组虚拟地址

#define LED1_ON (gpioe_virt->ODR |= (0x1 <ODR &= (~(0x1 <ODR |= (0x1 <ODR &= (~(0x1 <ODR |= (0x1 <ODR &= (~(0x1 < sizeof(kbuf)) size = sizeof(kbuf);
    //将用户空间的数据,拷贝到内核空间
    ret = copy_from_user(kbuf,ubuf,size);
    if(ret){
        printk("copy from user is error
");
        return -EIO;
    }
    //kbuf[0] = 0   kbuf[0] = 1  kbuf[0] = 2 操作哪一盏灯
    // kbuf[1] = 0 灯熄灭 kbuf[1] = 1 灯点亮
    switch(kbuf[0])
    {
        case LED1: //0
            kbuf[1] == 1 ? LED1_ON : LED1_OFF;
        break;
        case LED2: //1
            kbuf[1] == 1 ? LED2_ON : LED2_OFF;
        break;
        case LED3: //2
            kbuf[1] == 1 ? LED3_ON : LED3_OFF;
        break;
    }
    return size;
}

int myled_close (struct inode *inode, struct file *file)
{
    printk("%s:%s:%d
",__FILE__,__func__,__LINE__);
    return 0;
}

//操作方法结构体
const struct file_operations fops = {
    .open = myled_open,
    .write = myled_write,
    .release = myled_close,
};

//入口
static int __init demo_init(void)
{
    //注册字符设备驱动
    major = register_chrdev(0,CNAME,&fops);
    if(major < 0){
        printk("register chrdev is error
");
        return -EIO;
    }
    printk("major = %d
",major); //打印主设备号的值

    //rcc寄存器地址映射
    rcc_virt = ioremap(RCC_MP_AHB4ENSETR,4);
    if(rcc_virt == NULL){
        printk("ioremap rcc is error
");
        return -EIO;
    }
    //GPIOE组寄存器地址映射
    gpioe_virt = ioremap(GPIOE,sizeof(gpio_t));
    if(gpioe_virt == NULL){
        printk("ioremap gpioe is error
");
        return -EIO;
    }
    //GPIOF组寄存器地址映射
    gpiof_virt = ioremap(GPIOF,sizeof(gpio_t));
    if(gpiof_virt == NULL){
        printk("ioremap gpiof is error
");
        return -EIO;
    }

    //LED1灯初始化 PE10
    *rcc_virt |= (0x1 <MODER &= (~(0x3 <MODER |= (0x1 <ODR |= (0x1 << 10); //设置PE10引脚输出低电平,LED1熄灭 [10] = 0

    //LED2灯初始化 PF10
    *rcc_virt |= (0x1 <MODER &= (~(0x3 <MODER |= (0x1 <ODR |= (0x1 <MODER &= (~(0x3 <MODER |= (0x1 <ODR |= (0x1 << 8); //设置PE8引脚输出低电平,LED1熄灭 [8] = 0
    return 0;
}

//出口
static void __exit demo_exit(void)
{
    iounmap(gpioe_virt);//取消GPIOE组寄存器地址映射
    iounmap(gpiof_virt);//取消GPIOF寄存器地址映射
    iounmap(rcc_virt);//取消rcc寄存器地址映射
    //注销字符设备驱动
    unregister_chrdev(major,CNAME);
}

module_init(demo_init); //指定入口地址
module_exit(demo_exit); //指定出口地址
MODULE_LICENSE("GPL"); //许可证

test.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc,const char * argv[])
{
    char buf[128] = ""; 
    int fd = -1;
    fd = open("/dev/myled",O_RDWR); //打开
    if(fd == -1){
        perror("open is error");
        exit(1);
    }
    //buf[0] = 0  灯熄灭 buf[0] = 1 灯点亮
    while(1)
    {
        buf[0] = 0;
        write(fd,buf,sizeof(buf)); //写
        buf[1] = 1;
        write(fd,buf,sizeof(buf)); //写
        sleep(1);
        buf[1] = 0;
        write(fd,buf,sizeof(buf)); //写
        sleep(1);

        buf[0] = 1;
        write(fd,buf,sizeof(buf)); //写
        buf[1] = 1;
        write(fd,buf,sizeof(buf)); //写
        sleep(1);
        buf[1] = 0;
        write(fd,buf,sizeof(buf)); //写
        sleep(1);
    
        buf[0] = 2;
        write(fd,buf,sizeof(buf)); //写
        buf[1] = 1;
        write(fd,buf,sizeof(buf)); //写
        sleep(1);
        buf[1] = 0;
        write(fd,buf,sizeof(buf)); //写
        sleep(1);
    }
    
    close(fd); //关闭
    return 0;
}

myled.h

#ifndef __MYLED_H__
#define __MYLED_H__

enum led{
    LED1, //0
    LED2, //1
    LED3, //2
};

typedef struct {
    volatile unsigned int MODER;   // 0x00
    volatile unsigned int OTYPER;  // 0x04
    volatile unsigned int OSPEEDR; // 0x08
    volatile unsigned int PUPDR;   // 0x0C
    volatile unsigned int IDR;     // 0x10
    volatile unsigned int ODR;     // 0x14
    volatile unsigned int BSRR;    // 0x18
    volatile unsigned int LCKR;    // 0x1C 
    volatile unsigned int AFRL;    // 0x20 
    volatile unsigned int AFRH;    // 0x24
    volatile unsigned int BRR;     // 0x28
    volatile unsigned int res;
    volatile unsigned int SECCFGR; // 0x30
}gpio_t;

#define  GPIOE   (0x50006000) //GPIOE组基地址
#define  GPIOF   (0x50007000) //GPIOF组基地址 

#define RCC_MP_AHB4ENSETR 0x50000A28 //RCC寄存器的物理地址

#endif

本站无任何商业行为
个人在线分享 » 驱动开发1
E-->