字符设备驱动点灯实验

chrdev.c

#include 
#include 
#include 
#include 
#include 
#include "head.h"

//定义一些变量接收映射后的虚拟地址
unsigned int *vir_moder;
unsigned int *vir_odr;
unsigned int *vir_rcc;

unsigned int *vir_moder2;
unsigned int *vir_odr2;
unsigned int *vir_rcc2;

int major;
char kbuf[128] = {0};

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 *off)
{
    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 *off)
{
    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 |= (1<<10);
    }else if(kbuf[0]=='2')
    {
        *vir_odr2 |= (1<<10);
    }else if(kbuf[0]=='3')
    {
        *vir_odr |= (1<<8);
    }else if(kbuf[0]=='4')
    {
        *vir_odr &= ~(1<<10);
    }else if(kbuf[0]=='5')
    {
        *vir_odr2 &= ~(1<<10);
    }else if(kbuf[0]=='6')
    {
        *vir_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);

    //进行物理地址的映射
    vir_moder=ioremap(PHY_LED1_MODER,4);
    if(vir_moder==NULL)
    {
        printk("MODER寄存器映射失败\n");
        return -ENOMEM;
    }
    vir_odr=ioremap(PHY_LED1_ODR,4);
    if(vir_odr==NULL)
    {
        printk("ODR寄存器映射失败\n");
        return -ENOMEM;
    }
    vir_rcc=ioremap(PHY_LED1_RCC,4);
    if(vir_rcc==NULL)
    {
        printk("RCC寄存器映射失败\n");
        return -ENOMEM;
    }

     vir_moder2=ioremap(PHY_LED2_MODER,4);
    if(vir_moder2==NULL)
    {
        printk("MODER寄存器映射失败\n");
        return -ENOMEM;
    }
    vir_odr2=ioremap(PHY_LED2_ODR,4);
    if(vir_odr2==NULL)
    {
        printk("ODR寄存器映射失败\n");
        return -ENOMEM;
    }
    vir_rcc2=ioremap(PHY_LED2_RCC,4);
    if(vir_rcc2==NULL)
    {
        printk("RCC寄存器映射失败\n");
        return -ENOMEM;
    }
    printk("寄存器物理地址映射成功\n");

    //LED1寄存器的初始化
    *vir_moder &= (~(3<<20));//设置为输出模式
    *vir_moder |= (1<<20);
    *vir_rcc |= (1<<4);//rcc使能
    *vir_odr &= (~(1<<10));//输出低电平
    printk("LED1寄存器初始化成功\n");

    //LED2寄存器的初始化
    *vir_moder2 &= (~(3<<20));//设置为输出模式
    *vir_moder2 |= (1<<20);
    *vir_rcc2 |= (1<<5);//rcc使能
    *vir_odr2 &= (~(1<<10));//输出低电平
    printk("LED2寄存器初始化成功\n");

    //LED3寄存器的初始化
    *vir_moder &= (~(3<<16));//设置为输出模式
    *vir_moder |= (1<<16);
    *vir_rcc |= (1<<4);//rcc使能
    *vir_odr &= (~(1<<8));//输出低电平
    printk("LED3寄存器初始化成功\n");
    return 0;
}
static void __exit mycdev_exit(void)
{
     //字符设备驱动的注销
    unregister_chrdev(major,"mycdev");

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

test.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)
    {
        perror("open fail\n");
        return -1;
    }
    while(1)
    { 
        printf("请输入1-6令三盏灯依次亮灭:");   
        fgets(buf,sizeof(buf),stdin);
        buf[strlen(buf)-1] = '\0';
        write(fd,buf,sizeof(buf));
    }
    close(fd);
    return 0;
}

运行示例 

字符设备驱动点灯实验_第1张图片

 

你可能感兴趣的:(嵌入式硬件,stm32)