用户程序控制3盏LED灯亮灭

mycdev.c

#include 
#include 
#include 
#include 
#include 
#include "led.h"

unsigned int major;
char kbuf[128]={0};
unsigned int *vire_moder;
unsigned int *vire_odr;
unsigned int *virf_moder;
unsigned int *virf_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){
    int ret;
    if(size>sizeof(kbuf))//判断用户的需求,如果内核满足不了,给内核能满足的最大值
        size=sizeof(kbuf);
    ret=copy_to_user(ubuf,kbuf,size);//内核向用户拷贝数据
    if(ret){
        printk("copy_to_user filed%d\n",__LINE__);
        return ret;
    }
    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;
    if(size>sizeof(kbuf))//判断用户的需求,如果内核满足不了,给内核能满足的最大值
        size=sizeof(kbuf);
    ret=copy_from_user(kbuf,ubuf,size);//从用户拷贝数据
    if(ret){
        printk("copy_from_user filed%d\n",__LINE__);
        return ret;
    }
   if(kbuf[0]=='1'){
    (*vire_odr) |= (1<<10);
   }else if(kbuf[0]=='2'){
    (*vire_odr) &= (~(1<<10));
   }else if(kbuf[0]=='3'){
    (*virf_odr) |= (1<<10);
   }else if(kbuf[0]=='4'){
    (*virf_odr) &= (~(1<<10));
   }else if(kbuf[0]=='5'){
    (*vire_odr) |= (1<<8);
   }else if(kbuf[0]=='6'){
    (*vire_odr) &= (~(1<<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,"mycdev",&fops);
    if(major<0){
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功,major=%d\n",major);
    //进行LED1的相关寄存器映射
    vire_moder=ioremap(PHY_E_MODER,4);
    if(vire_moder==NULL){
        printk("寄存器映射失败%d\n",__LINE__);
        return -ENOMEM;
    }
    vire_odr=ioremap(PHY_E_ODR,4);
    if(vire_odr==NULL){
        printk("寄存器映射失败%d\n",__LINE__);
        return -ENOMEM;
    }
    vir_rcc=ioremap(PHY_RCC,4);
    if(vir_rcc==NULL){
        printk("寄存器映射失败%d\n",__LINE__);
        return -ENOMEM;
    }
    //进行LED2的相关寄存器映射
    virf_moder=ioremap(PHY_F_MODER,4);
    if(virf_moder==NULL){
        printk("寄存器映射失败%d\n",__LINE__);
        return -ENOMEM;
    }
    virf_odr=ioremap(PHY_F_ODR,4);
    if(virf_odr==NULL){
        printk("寄存器映射失败%d\n",__LINE__);
        return -ENOMEM;
    }
    vir_rcc=ioremap(PHY_RCC,4);
    if(vir_rcc==NULL){
        printk("寄存器映射失败%d\n",__LINE__);
        return -ENOMEM;
    }
    //进行LED3的相关寄存器映射
    vire_moder=ioremap(PHY_E_MODER,4);
    if(vire_moder==NULL){
        printk("寄存器映射失败%d\n",__LINE__);
        return -ENOMEM;
    }
    vire_odr=ioremap(PHY_E_ODR,4);
    if(vire_odr==NULL){
        printk("寄存器映射失败%d\n",__LINE__);
        return -ENOMEM;
    }
    vir_rcc=ioremap(PHY_RCC,4);
    if(vir_rcc==NULL){
        printk("寄存器映射失败%d\n",__LINE__);
        return -ENOMEM;
    }
    printk("寄存器映射成功\n");
    //RCC层寄存器的初始化
    (*vir_rcc) |= (1<<4);
    (*vir_rcc) |= (1<<5);
    //LED1寄存器的初始化
    (*vire_moder) &= (~(3<<20));
    (*vire_moder) |= (1<<20);
    (*vire_odr) &= (~(1<<10));
    //LED2寄存器的初始化
    (*virf_moder) &= (~(3<<20));
    (*virf_moder) |= (1<<20);
    (*virf_odr) &= (~(1<<10));
    //LED3寄存器的初始化
    (*vire_moder) &= (~(3<<16));
    (*vire_moder) |= (1<<16);
    (*vire_odr) &= (~(1<<8));
    printk("寄存器初始化成功\n");
    return 0;
}
static void __exit mycdev_exit(void)
{
    //取消内存映射
    iounmap(vire_moder);
    iounmap(vire_odr);
    iounmap(virf_moder);
    iounmap(virf_odr);
    iounmap(vir_rcc);
    //字符设备驱动的注销
    unregister_chrdev(major,"mycdev");

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

led.h

#ifndef __LED_H__
#define __LED_H__

#define PHY_E_MODER 0X50006000
#define PHY_E_ODR 0X50006014
#define PHY_F_MODER 0X50007000
#define PHY_F_ODR 0X50007014
#define PHY_RCC 0X50000A28

#endif

test.c

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

int main(int argc, char const *argv[])
{
    char buf[128]={0};
    int fd=open("/dev/mycdev",O_RDWR);
    if(fd<0){
        printf("设备文件打开失败\n");
        exit(-1);
    }
    while(1){
        printf("请输入要进行的操作>>> \n 1(LED1开)\t 2(LED1关)\t \n 3(LED2开)\t 4(LED2关)\t \n 5(LED3开)\t 6(LED3关)\t\n");
        fgets(buf,sizeof(buf),stdin);//从终端获取一个字符串
        buf[strlen(buf)-1]='\0';
        write(fd,buf,sizeof(buf));
    }
    close(fd);
    return 0;
}

你可能感兴趣的:(stm32,arm)