驱动day2作业

编写应用程序控制三盏灯亮灭

head.h

#ifndef __HEAD_H__
#define __HEAD_H__
#define PHY_LED1_MODER 0x50006000
#define PHY_LED2_MODER 0x50007000
#define PHY_LED1_ODR 0x50006014
#define PHY_LED2_ODR 0x50007014
#define PHY_RCC 0x50000A28

#endif

demo1.c

#include 
#include 
#include 
#include 
#include 
#include "head.h"
unsigned int major;
char kbuf[128]={};
unsigned int *vir_moder;
unsigned int *vir_moder2;
unsigned int *vir_odr;
unsigned int *vir_odr2;
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)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    int ret;
    ret = copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("copy_to_user filed\n");
        return -EIO;
    }
    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;
    ret = copy_from_user(kbuf,ubuf,size);
    if(ret)
    {
        printk("copy_from_user filed\n");
        return -EIO;
    }
    if(kbuf[0] == '0')//全灭
    {
        (*vir_odr) &= (~(0x1 << 10));
        (*vir_odr2) &= (~(0x1 << 10));
        (*vir_odr) &= (~(0x1 << 8));
    }
    else if(kbuf[0] == '1')//LED1亮
    {
        (*vir_odr) |= (0x1 << 10);
    }
	else if(kbuf[0] == '2')//LED2亮
	{
        (*vir_odr2) |= (0x1 << 10);
	}
	else if(kbuf[0] == '3')//LED3亮
	{
        (*vir_odr) |= (0x1 << 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,"mychrdev",&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("物理内存地址映射失败%d\n", __LINE__);
        return -EFAULT;
    }
	vir_moder2 = ioremap(PHY_LED2_MODER,4);
	if(vir_moder == NULL)
    {
        printk("物理内存地址映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_odr = ioremap(PHY_LED1_ODR,4);
    if(vir_odr == NULL)
    {
        printk("物理内存地址映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_odr2 = ioremap(PHY_LED2_ODR,4);
    if(vir_odr == NULL)
    {
        printk("物理内存地址映射失败%d\n", __LINE__);
        return -EFAULT;
    }

    vir_rcc = ioremap(PHY_RCC,4);
    if(vir_rcc == NULL)
    {
        printk("物理内存地址映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    printk("寄存器内存映射成功\n");
    //rcc使能GPIOE、GPIOF
    (*vir_rcc) |= (0x1 << 4);
    (*vir_rcc) |= (0x1 << 5);
    //moder 设PE10 PF10 PE8为输出模式
    (*vir_moder) &= (~(0x3 << 20));
    (*vir_moder) |= (0x1 << 20);
    (*vir_moder) &= (~(0x3 << 16));
    (*vir_moder) |= (0x1 << 16);
    (*vir_moder2) &= (~(0x3 << 20));
    (*vir_moder2) |= (0x1 << 20);
    //odr 设PE10 PF10 PE8默认输出0
    (*vir_odr) &= (~(0x1 << 10));
    (*vir_odr) &= (~(0x1 << 8));
    (*vir_odr2) &= (~(0x1 << 10));
    return 0;
}
static void __exit mycdev_exit(void)
{
    iounmap(vir_moder);
    iounmap(vir_odr);
    iounmap(vir_rcc);
    iounmap(vir_moder2);
    iounmap(vir_odr2);
    unregister_chrdev(major,"mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

text.c

#include 
#include 
#include 
#include 
#include 
#include 
int main(int argc,const char * argv[])
{
    char buf[128] = {0};
    int fd = open("/dev/mychrdev",O_RDWR);
    if(fd < 0)
    {
        printf("打开设备文件失败\n");
        return -1;
    }
    printf("打开设备文件成功\n");
    while(1)
    {
        printf("請輸入要進行的操作:0(全关)1(LED1开)2(LED2开)3(LED3开)>");
        fgets(buf,sizeof(buf),stdin);
        buf[strlen(buf)-1]='\0';
        write(fd,buf,sizeof(buf));
    }
    close(fd);
    return 0;
}

驱动day2作业_第1张图片驱动day2作业_第2张图片驱动day2作业_第3张图片驱动day2作业_第4张图片驱动day2作业_第5张图片

你可能感兴趣的:(笔记)