驱动02 简单字符设备驱动点灯

 准备:stm32mp157芯片

已知led灯引脚,pe10,pe8,pf10

 字符驱动代码

#include 
#include 
#include
#include
#include
#include"led.h"
unsigned int major;
char kbuf[128]={0};
//定义三个指针指向映射后的虚拟内存首地址
unsigned int *vir_rcc;
unsigned int *vir_moder_e;
unsigned int *vir_odr_e;
unsigned int *vir_moder_f;
unsigned int *vir_odr_f;

 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 *iof)
 {
     int ret;
    //向用户拷贝
    if(size>sizeof(kbuf))
        size=sizeof(kbuf);
    ret=copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("copy to user filed\n");
        return -EIO;
    }
    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 *iof)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    int ret;
    //从用户拷贝
    if(size>sizeof(kbuf))
        size=sizeof(kbuf);
    ret=copy_from_user(kbuf,ubuf,size);
    if(ret)
    {
        printk("copy from user filed\n");
        return -EIO;
    }
    //根据用户空间发送来的指令实现灯的亮灭控制
    if(kbuf[0]=='1')//亮灯
    {
        (*vir_odr_e) |= (0X1<<10);
        (*vir_odr_e) |= (0X1<<8);
        (*vir_odr_f) |= (0X1<<10);
    }
    else if(kbuf[0]=='0')//灭灯
    {
        (*vir_odr_e) &= (~(0X1<<10));
        (*vir_odr_e) &= (~(0X1<<8));
        (*vir_odr_f) &= (~(0X1<<10));
    }
    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,"mycdev",&fops);
    if(major<0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功major=%d\n",major);
    //进行LED相关寄存器地址映射
    vir_rcc=ioremap(PHY_RCC,4);
    vir_moder_e=ioremap(PHY_MODER_E,4);//E
    vir_odr_e=ioremap(PHY_ODR_E,4);//E

    vir_moder_f=ioremap(PHY_MODER_F,4);//F
    vir_odr_f=ioremap(PHY_ODR_F,4);//F
    if((vir_moder_e==NULL) |(vir_odr_e==NULL) | (vir_rcc==NULL))
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }
    if((vir_moder_f==NULL) | (vir_odr_f==NULL) | (vir_rcc==NULL))
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }

    
    printk("物理内存映射为虚拟内存成功\n");
    //相关寄存器初始化
    (*vir_rcc) |= (0X1<<4);//GPIOE使能
    (*vir_rcc) |= (0X1<<5);//GPIOF使能

    (*vir_moder_e) &= (~(0X3<<20));//pe10设置为输出
    (*vir_moder_e) |=(0X1<<20);
    (*vir_moder_e) &= (~(0X3<<16));//pe8设置为输出
    (*vir_moder_e) |=(0X1<<16);
    (*vir_moder_f) &= (~(0X3<<20));//pf10设置为输出
    (*vir_moder_f) |=(0X1<<20);

    
    (*vir_odr_e) &= (~(0X1<<10));//pe10灭灯
    (*vir_odr_e) &= (~(0X1<<8));//pe8灭灯
    (*vir_odr_f) &= (~(0X1<<10));//pf10灭灯
    printk("相关寄存器初始化成功\n");
    return 0;
}
static void __exit mycdev_exit(void)
{
    //内存取消映射
    iounmap(vir_rcc);
    iounmap(vir_odr_e);
    iounmap(vir_moder_e);

    iounmap(vir_odr_f);
    iounmap(vir_moder_f);
    //字符设备驱动的注销
    unregister_chrdev(major,"mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

led.c代码

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

int main(int argc,const char * argv[])
{

    
    char buf[128] = {0};
    int fd=open("/dev/mycdev",O_RDWR);
    if(fd < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while(1)
    {
        printf("请输入控制码: 1(亮灯): 0(灭灯)");
        fgets(buf,sizeof(buf),stdin);//从终端获取字符串

        buf[strlen(buf)-1]='\0';//用‘\0’替换末尾的‘\n’
        write(fd,buf,sizeof(buf));
        }
    close(fd);  
    return 0;
}

led.h代码

#ifndef __LED_H__
#define __LED_H__


#define PHY_RCC    0X50000A28
#define PHY_MODER_E  0X50006000
#define PHY_ODR_E   0X50006014

#define PHY_MODER_F  0X50007000
#define PHY_ODR_F   0X50007014

#endif

你可能感兴趣的:(学习日报,c语言,linux)