驱动点灯实验【代码】

驱动文件myled.c

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



#define CNAME "myled"
int major = 0;
char kbuf[128] = {0};
unsigned int *virt_rcc;
unsigned int *virt_moder;
unsigned int *virt_odr;

unsigned int *virt_rcc2;
unsigned int *virt_moder2;
unsigned int *virt_odr2;

unsigned int *virt_rcc3;
unsigned int *virt_moder3;
unsigned int *virt_odr3;

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

ssize_t myled_read (struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    //1.校验传输数据的大小,如果用户空间写的数据比内核空间数据大小大,需要更正大小
    if(size > sizeof(kbuf)) size = sizeof(kbuf);
    //2.将数据从用户空间拷贝到内核空间
    ret = copy_to_user(ubuf,kbuf,size);
    if(ret) //3.判断是否错误
    {
        printk("copy to user is error\n");
        return -EIO;
    }
    return size; //5.返回拷贝数据大小
}

ssize_t myled_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    //1.校验传输数据的大小,如果用户空间写的数据比内核空间数据大小大,需要更正大小
    if(size > sizeof(kbuf)) 
        size = sizeof(kbuf);
    //2.将数据从用户空间拷贝到内核空间
    ret = copy_from_user(kbuf,ubuf,size);
    if(ret) //3.判断是否错误
    {
        printk("copy from user is error\n");
        return -EIO;
    }
    //4.打印传递数据内容
    printk("copy from user kbuf:%s\n",kbuf);
    if(kbuf[0] == '0')
    {
        *virt_odr |= (0x1 << 10); //LED1点亮
    }
    else if(kbuf[0] == '1')
    {
        *virt_odr2 |= (0x1 << 10); //LED2点亮 
    }
     if(kbuf[0] == '2')
    {
         *virt_odr3 |= (0x1 << 8); //LED3点亮
    }
    else if(kbuf[0] == '3')
    {
       *virt_odr &= (~(0x1 << 10)); //LED1写灭;
    }
     if(kbuf[0] == '4')
    {
        *virt_odr2 &= (~(0x1 << 10));//LED2写灭;
        
    else if(kbuf[0] == '5')
    {
        *virt_odr3 &= (~(0x1 << 8)); //LED3写灭;
    }
    return size; //5.返回拷贝数据大小
}
int myled_close(struct inode* inode,struct file* file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

const struct file_operations fops = {
    .open = myled_open,
    .read = myled_read,
    .write = myled_write,
    .release = myled_close,
};
static int __init mycdev_init(void)
{
    //1.注册字符设备驱动
    major = register_chrdev(0,CNAME,&fops);
    if(major < 0) //2.判断返回值
    {
        printk("register chrdev is error\n");
    }
    //3.打印主设备号
    printk("register chrdev major=%d\n",major);

    //4.将物理地址映射为虚拟地址
    //4.1 将rcc地址映射
    //PE10
    virt_rcc = ioremap(LED1_RCC,4);
    if(virt_rcc == NULL)
    {
        printk("rcc ioremap is error\n");
        return -ENOMEM;
    }
    //4.2 将MODER寄存器地址映射
    virt_moder = ioremap(LED1_MOD,4);
    if(virt_moder == NULL)
    {
        printk("moder ioremap is error\n");
        return -ENOMEM;
    }
    //4.3 将ODR寄存器地址映射
    virt_odr = ioremap(LED1_ODR,4);
    if(virt_odr == NULL)
    {
        printk("odr ioremap is error\n");
        return -ENOMEM;
    }

    //PF10
    virt_rcc = ioremap(LED2_RCC,4);
    if(virt_rcc2 == NULL)
    {
        printk("rcc ioremap is error\n");
        return -ENOMEM;
    }
    //4.2 将MODER寄存器地址映射
    virt_moder = ioremap(LED2_MOD,4);
    if(virt_moder2 == NULL)
    {
        printk("moder ioremap is error\n");
        return -ENOMEM;
    }
    //4.3 将ODR寄存器地址映射
    virt_odr = ioremap(LED2_ODR,4);
    if(virt_odr2 == NULL)
    {
        printk("odr ioremap is error\n");
        return -ENOMEM;
    }

    //PE8
    virt_rcc = ioremap(LED3_RCC,4);
    if(virt_rcc3 == NULL)
    {
        printk("rcc ioremap is error\n");
        return -ENOMEM;
    }
    //4.2 将MODER寄存器地址映射
    virt_moder = ioremap(LED3_MOD,4);
    if(virt_moder3 == NULL)
    {
        printk("moder ioremap is error\n");
        return -ENOMEM;
    }
    //4.3 将ODR寄存器地址映射
    virt_odr = ioremap(LED1_ODR,4);
    if(virt_odr3 == NULL)
    {
        printk("odr ioremap is error\n");
        return -ENOMEM;
    }
    //5.对led1---->PE10引脚初始化
    *virt_rcc |= (0x1 << 4);//5.1 使能GPIOE组时钟[4]=1
    *virt_moder &= (~(0x3 << 20));//5.2 设置PE10引脚为输出模式 [21:20] = 01
    *virt_moder |= (0x1 << 20);
    *virt_odr &= (~(0x1 << 10)); //5.3 设置PE10引脚输出低电平
    //5.对led2---->PF10引脚初始化
    *virt_rcc2 |= (0x1 << 5);//5.1 使能GPIOF组时钟[5]=1
    *virt_moder2 &= (~(0x3 << 20));//5.2 设置PF10引脚为输出模式 [21:20] = 01
    *virt_moder2 |= (0x1 << 20);
    *virt_odr2 &= (~(0x1 << 10)); //5.3 设置PF10引脚输出低电平
    //5.对led3---->PE8引脚初始化
    *virt_rcc3 |= (0x1 << 4);//5.1 使能GPIOE组时钟[4]=1
    *virt_moder3 &= (~(0x3 << 20));//5.2 设置PE8引脚为输出模式 [16] = 01
    *virt_moder3 |= (0x1 << 16);
    *virt_odr3 &= (~(0x1 << 8)); //5.3 设置PE8引脚输出低电平
    return 0;
}
static void __exit mycdev_exit(void)
{
    iounmap(virt_rcc);
    iounmap(virt_moder);
    iounmap(virt_odr);

    iounmap(virt_rcc2);
    iounmap(virt_moder2);
    iounmap(virt_odr2);

    iounmap(virt_rcc3);
    iounmap(virt_moder3);
    iounmap(virt_odr3);
    unregister_chrdev(major,CNAME);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

头文件myled.h

#ifndef __MYLED_H__
#define __MYLED_H__

//PE10
//RCC基地址:0x50000a28
#define LED1_RCC 0x50000a28
//GPIOE_MODER寄存器地址:0x50006000
#define LED1_MOD 0x50006000
//GPIOE_ODR寄存器地址:0x50006014
#define LED1_ODR 0x50006014

//PF10
#define LED2_RCC 0x50000a28
#define LED2_MOD 0x50007000
#define LED2_ODR 0X50007014
//PE8
#define LED3_RCC 0x50000a28
#define LED3_MOD 0x50006000
#define LED3_ODR 0x50006014
#endif

test.c

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


int main(int argc,const char * argv[])
{
    char buf[128] = {0};
    int fd = -1;
    fd = open("/dev/myled",O_RDWR);
    if(fd == -1)
    {
        printf("open error\n");
        return -1;
    }
    while(1)
    {
        buf[0] = !buf[0];
        write(fd,buf,1);
        sleep(1);
    }
    close(fd);
    return 0;
}

你可能感兴趣的:(Linux驱动开发,linux,c++,运维)