嵌入式:驱动开发 Day2

作业:字符设备驱动,完成三盏LED灯的控制

驱动代码: mychrdev.c

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

unsigned int major;   //保存主设备号
char kbuf[128] = "";

unsigned int *vir_moder_e;
unsigned int *vir_odr_e;
unsigned int *vir_rcc_e;

unsigned int *vir_moder_f;
unsigned int *vir_odr_f;
unsigned int *vir_rcc_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){
	int ret;
	ret = copy_to_user(ubuf, kbuf, size);
	if(ret){
		printk("copy_to_user err\n");
		return -EIO;
	}
	return 0;
}

ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof){
	int ret;
	ret = copy_from_user(kbuf, ubuf, size);
	if(ret){
		printk("copy_from_user err\n");
		return -EIO;
	}
	if(kbuf[0] == '1')  	//led1开灯
	{
		//开灯逻辑
		(*vir_odr_e) |= (0x1 << 10);
	}else if(kbuf[0] == '2'){   //led1关灯
		//关灯逻辑
		(*vir_odr_e) &= (~(0x1 << 10));
	}else if(kbuf[0] == '3'){   //led2开灯
		//开灯逻辑
		(*vir_odr_f) |= (0x1 << 10);
	}else if(kbuf[0] == '4'){   //led2关灯
		//关灯逻辑
		(*vir_odr_f) &= (~(0x1 << 10));
	}else if(kbuf[0] == '5'){   //led3开灯
		//开灯逻辑
		(*vir_odr_e) |= (0x1 << 8);
	}else if(kbuf[0] == '6'){   //led3关灯
		//关灯逻辑
		(*vir_odr_e) &= (~(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);

	//进行相关寄存器的内存映射
	//GPIOE组寄存器内存映射
	vir_odr_e = ioremap(GPIOE_ODR, 4);
	if(vir_odr_e == NULL){
		printk("物理内存映射失败%d\n", __LINE__);
		return -EFAULT;
	}
	vir_rcc_e = ioremap(GPIOE_RCC, 4);
	if(vir_rcc_e == NULL){
		printk("物理内存映射失败%d\n", __LINE__);
		return -EFAULT;
	}
	vir_moder_e = ioremap(GPIOE_MODER, 4);
	if(vir_moder_e == NULL){
		printk("物理内存映射失败%d\n", __LINE__);
		return -EFAULT;
	}

	//GPIOF组寄存器内存映射
	vir_odr_f = ioremap(GPIOF_ODR, 4);
	if(vir_odr_f == NULL){
		printk("物理内存映射失败%d\n", __LINE__);
		return -EFAULT;
	}
	vir_rcc_f = ioremap(GPIOF_RCC, 4);
	if(vir_rcc_f == NULL){
		printk("物理内存映射失败%d\n", __LINE__);
		return -EFAULT;
	}
	vir_moder_f = ioremap(GPIOF_MODER, 4);
	if(vir_moder_f == NULL){
		printk("物理内存映射失败%d\n", __LINE__);
		return -EFAULT;
	}
	printk("寄存器内存映射成功\n");

	//硬件寄存器的初始化
	//RCC使能
	(*vir_rcc_e) |= (0x1 << 4);     //GPIOE(PE10 PE8)使能
	(*vir_rcc_f) |= (0x1 << 5);     //GPIOF(PF10)使能

	//设置PE10为输出模式 --> LED1
	(*vir_moder_e) &= (~(0x3 << 20));
	(*vir_moder_e) |= (0x1 << 20);
	//设置PF10为输出模式 --> LED2
	(*vir_moder_f) &= (~(0x3 << 20));
	(*vir_moder_f) |= (0x1 << 20);
	//设置PE8为输出模式 --> LED3
	(*vir_moder_e) &= (~(0x3 << 16));
	(*vir_moder_e) |= (0x1 << 16);

	//默认LED1关灯
	(*vir_odr_e) &= (~(0x1 << 10));
	//默认LED2关灯
	(*vir_odr_f) &= (~(0x1 << 10));
	//默认LED1关灯
	(*vir_odr_e) &= (~(0x1 << 8));
	return 0;
}

static void __exit mycdev_exit(void){
	//取消物理内存映射
	iounmap(vir_moder_e);
	iounmap(vir_odr_e);
	iounmap(vir_rcc_e);

	iounmap(vir_moder_f);
	iounmap(vir_odr_f);
	iounmap(vir_rcc_f);

	//字符设备驱动注销
	unregister_chrdev(major, "mychrdev");
}

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

应用代码:test.c

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

int main(){
	char buf[128] = "";
	int fd = open("/dev/mychrdev", O_RDWR);
	if(fd < 0){
		printf("设备文件打开失败\n");
		exit(-1);
	}

	while(1){
		printf("请输入对LED的控制命令:1(LED1开灯) 2(LED1关灯) 3(LED2开灯) 4(LED2关灯) 5(LED3开灯) 6(LED3关灯) >");
		fgets(buf, sizeof(buf), stdin); //在终端输入数据传递到buf
		buf[strlen(buf) - 1] = '\0'; 	//替换末尾的'\n'
		write(fd, buf, sizeof(buf));
	}

	close(fd);

	return 0;
}

头文件:head.h

#ifndef __HEAD_H__
#define __HEAD_H__

#define GPIOE_MODER 0x50006000
#define GPIOE_ODR 0x50006014
#define GPIOE_RCC 0x50000A28

#define GPIOF_MODER 0x50007000
#define GPIOF_ODR 0x50007014
#define GPIOF_RCC 0x50000A28

#endif

嵌入式:驱动开发 Day2_第1张图片嵌入式:驱动开发 Day2_第2张图片

你可能感兴趣的:(驱动开发)