LED灯驱动

mycdev.c

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

unsigned int major;
char kbuf[128]={0};
unsigned int *vir_rcc;
unsigned int *vir_moder;
unsigned int *vir_odr;
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 *lof)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
	//向用户拷贝
	int ret;
	if(size>sizeof(kbuf))
		size=sizeof(kbuf);
	ret=copy_to_user(ubuf,kbuf,size);
	if(ret)
	{
		printk("copy_to_user fail\n");
		return ret;
	}
    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 fail\n");
		return ret;
	}
	//根据用户空间发来的指令实现灯的亮灭控制
	if(kbuf[0]=='1')//亮灯
		(*vir_odr) |=(0x1<<10);
	else if(kbuf[0]=='0')//灭灯
		(*vir_odr) |=(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);


	//---------LED1相关寄存器映射--------
	vir_rcc=ioremap(PHY_RCC,4);//GPIOE寄存器使能
	if(vir_rcc==NULL)
	{
		printk("物理内存映射失败\n");
		return -EFAULT;
	}
	vir_moder=ioremap(PHY_MODER,4);
	if(NULL==vir_moder)
	{
		printk("物理内存映射失败\n");
		return -EFAULT;
	}
	vir_odr=ioremap(PHY_ODR,4);
	if(NULL==vir_odr)
	{
		printk("物理内存映射失败\n");
		return -EFAULT;
	}
	printk("物理内存映射成功\n");


	//LED1相关寄存器的初始化
	(*vir_rcc) |= (0x1<<4);//使能GPIOE
	(*vir_moder) &=(~(0x3<<20));//输出模式
	(*vir_moder) |=(0x1<<20);
	(*vir_odr) &=(~(0x1<<10));//灭灯
	printk("相关寄存器初始化成功\n");




	//---------LED2相关寄存器映射--------
	vir_moder_f=ioremap(PHY_MODER_F,4);
	if(NULL==vir_moder_f)
	{
		printk("物理内存映射失败\n");
		return -EFAULT;
	}
	vir_odr_f=ioremap(PHY_ODR_F,4);
	if(NULL==vir_odr_f)
	{
		printk("物理内存映射失败\n");
		return -EFAULT;
	}
	printk("物理内存映射成功\n");
	//LED2相关寄存器的初始化
	(*vir_rcc) |= (0x1<<5);//使能GPIOF
    (*vir_moder_f) &=(~(0x3<<20));//输出模式
    (*vir_moder_f) |=(0x1<<20);
    (*vir_odr_f) &=(~(0x1<<10));//灭灯
    printk("相关寄存器初始化成功\n");




	//---------LED3相关寄存器映射--------
	vir_moder=ioremap(PHY_MODER,4);
	if(NULL==vir_moder)
	{
		printk("物理内存映射失败\n");
		return -EFAULT;
	}
	vir_odr=ioremap(PHY_ODR,4);
	if(NULL==vir_odr)
	{
		printk("物理内存映射失败\n");
		return -EFAULT;
	}
	printk("物理内存映射成功\n");
	//LED2相关寄存器的初始化
	(*vir_rcc) |= (0x1<<4);//使能GPIOE
    (*vir_moder) &=(~(0x3<<16));//输出模式
    (*vir_moder) |=(0x1<<16);
    (*vir_odr) &=(~(0x1<<8));//灭灯
    printk("相关寄存器初始化成功\n");



    return 0;
}
static void __exit mycdev_exit(void)
{
	//内存取消映射
	iounmap(vir_rcc);
	iounmap(vir_moder);
	iounmap(vir_odr);
    //字符设备驱动的注销
    unregister_chrdev(major,"mycdev");

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

test.c

#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("请输入控制码 1(亮灯) 0(灭灯)\n");
		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 0x50006000
#define PHY_ODR 0x50006014
#define PHY_MODER_F 0x50007000
#define PHY_ODR_F 0x50007014


#endif

你可能感兴趣的:(linux)