4412——Linux驱动入门01

前言

作为一名嵌入式开发工程师,学习linux是必要的。当初我在校培训linux S3c2410开发板,结论:迷迷糊糊。所以打算从“根”上去学习,结果走上了单片机-》智能家居的路上。幸好目前有幸接触linux开发项目。从头把linux在搞一遍,成为一名合格的嵌入式开发工程师。
今天要使用的板子是4412,谈下为什么使用这个板子,网上对于很多学习linux的开发板,大多是关于s3c2410,等等。然而当时买的开发板是韦哥的最牛板子4412学习安卓framework准备的,目前工作不需要安卓,因此使用4412作为入门学习。

课题入门驱动

谈及驱动说说我当时遇到的坑。
学什么我都习惯从根上开始,我理解的驱动是仅仅是封装了硬件,提供出去api接口。那么对于单片机开始的我,来说 ,首先想到的是,我先不封装,仅仅是测试我需要的功能,功能完成后我在考虑封装。
语言学习第一节都是运行“hello world”,对于嵌入式来说第一节都是“点亮一盏灯”。首相我打开原理图,找到开发板led对于芯片引脚位置,如图
在这里插入图片描述
然后看下是如何点亮的,低电平导通
在这里插入图片描述
打开芯片数据手册,查询寄存器
在这里插入图片描述
拿出我单片思想,对物理地址进行寄存器的配置。
在这里插入图片描述
正所谓一顿操作猛如虎,下载运行,直接运行段错误。百度各种越界之类的错误,后来经韩老板(学习群大佬)指点,错误是必然的,对于可以直接操作物理地址来说那是裸机开发,对于这种驱动型开发,操作的都是虚拟地址,接下来驱动代码里面就有物理地址和虚拟地址映射函数,就是这个道理。

#include <stdio.h>

#define GPBCON      (*(volatile unsigned long *)0x110002E0)
#define GPBDAT      (*(volatile unsigned long *)0x110002E4)

#define	GPB0_out	(1<<(0*4))
#define	GPB1_out	(1<<(1*4))
#define	GPB2_out	(1<<(2*4))
#define	GPB3_out	(1<<(3*4))

void  wait(volatile unsigned long dly)
{
	for(; dly > 0; dly--);
}
int main(void)
{
	unsigned long i = 0;
	// LED1,LED2,LED4对应的4根引脚设为输出
	//unsigned long gaoyuan = GPB0_out | GPB1_out | GPB2_out | GPB3_out;
	 GPBCON = GPB0_out | GPB1_out | GPB2_out | GPB3_out;
	//printf("gaoyuan you hjahha %x\n",gaoyuan);
	printf("gaoyuan you hjahha %x\n",GPBDAT);
/*	
	while(1){
		wait(30000);
		GPBDAT = (~(i<<5));	 	// 根据i的值,点亮LED1,2,3,4
		if(++i == 16)
			i = 0;
	}
*/
	return 0;
}

上班没时间搞这些,周末加班继续干。
接下来呢,搞驱动把。一个完整的测试驱动分为3部分,1.是驱动源码。2.驱动对应makefile。3.测试驱动的应用程序。
1.首先是驱动源码的编写
第一步驱动框架的搭建

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/device.h>
#include <linux/miscdevice.h>


static struct class *firstdrv_class;
static struct class_device	*firstdrv_class_dev;
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;

static int gaoyuan_open(struct inode *inode, struct file *file)
{
	printk("gaoyuan_open\n");
	return 0;
}
static int gaoyuan_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
	printk("gaoyuan_write\n");
	return 0;
}
static struct file_operations gaoyuan_led_dev_fops = {
	.owner			= THIS_MODULE,
	.open = gaoyuan_open, //提供了接口,自己去实现
	.write = gaoyuan_write,	  
	
};
int major;
static int __init gaoyuan_led_init(void) {
	
	printk("iiiiiiiiiiiiiiiiii\n");
	//register_chrdev(231,"gao_leds",&gaoyuan_led_dev_fops);
	//事实证明通过手动方式创建节点的方式,卸载和运行都是正常的
	
	major = register_chrdev(0, "gao_leds", &gaoyuan_led_dev_fops); // 注册, 告诉内核  是系统自动设置主设备号的意思
/*在/sys/class/目录下创建设备类test_class别目录my_device_driver*/
	firstdrv_class = class_create(THIS_MODULE, "my_device_driver");   // 相当于手动的mknod 了
/*在/dev/目录和/sys/class/my_device_driver目录下分别创建设备文件my_device*/
	firstdrv_class_dev = device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "my_device");

	/* /dev/my_device 此设备号为0,其实这个次设备号,可以作为判断的以重依据的	这个设备节点是随便写的 自己想写啥写啥的*/
	return 0;
}

static void __exit gaoyuan_led_exit(void) {
	
	unregister_chrdev(major, "gao_leds"); // 卸载
	
	//device_destroy(firstdrv_class_dev,major);//  销毁 cao  竟然不是这个函数,引起的bug,那你到别编译过去呀,误导我
	//linux 3.10中,class_device_unregister替换为device_unregister
	device_unregister(firstdrv_class_dev);//  销毁
	class_destroy(firstdrv_class);
	printk("tttttttttttttt\n");
}

module_init(gaoyuan_led_init);
module_exit(gaoyuan_led_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("gaoyuan publish");

在测试的过程当中,每次rsmmod都失败,在多个群里发送问题,有冷嘲热讽的说是自己去看API等等,但是无论在哪里都有热心人,通过群里了解到,我使用的销毁函数有问题,这里我要多说一句,既然内核版本不同,api不同为什么老版本的api你却可以编译过去,纯纯的误导。
在这里插入图片描述
热心人链接解决办法

通过以上方法以后,驱动框架搭建已经完成。
接下来如何点灯。

框架搭建完成以后任务就简单了

static int gaoyuan_open(struct inode *inode, struct file *file)
{
	printk("gaoyuan_open\n");
	*gpfcon &= ~(   0xff<<(4*0) | 0xff<<(4*2) | 0xff<<(4*3) | 0xff<<(4*4)   );  // 对应位清0
	*gpfcon |= (    0x1<<(4*2) | 0x1<<(4*2) | 0x1<<(4*3)| 0x1<<(4*4));        // 对应位写1
	return 0;
}
static int gaoyuan_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
	int val;
	//int minor = MINOR(file->f_dentry->d_inode->i_rdev);  // 得到设备的次设备号,从而作为判断的以重依据
	printk("gaoyuan_write\n");
    copy_from_user(&val, buf, 4);  
	//从用户那里接收到的参数,如果要返还用户数据的话如按键需要 用到copy_to_user()
	if (1 == val)
	{
		// 点灯 对应位置写0,低电平亮灯
		*gpfdat &= ~((1<<0) | (1<<2) | (1<<3) | (1<<4));  
		printk("gaoyuan_write111111\n");
	}
	else
	{
		// 灭灯
		*gpfdat |= ((1<<0) | (1<<2) | (1<<3) | (1<<4));
		printk("gaoyuan_write2222\n");
	}
	return 0;
}

只需要替换open 和write
在init函数中加入上面说说的地址映射

gpfcon = (volatile unsigned long *)ioremap(0x110002E0, 16);  // 映射地址,在系统中是不允许直接操作物理地址的,必须重映射地址,后面的参数是大小的意思
gpfdat = gpfcon + 1;

第二部分是写makefile
随便粘贴复制过来就可以了,如下

ifeq ($(KERNELRELEASE),)
PWD = $(shell pwd)
#KERNEL_DIR = /home/yuan/linux/linux-3.16.2
KERNEL_DIR = /root/linux-3.5
modules:
	make -C $(KERNEL_DIR) M=$(PWD) modules
clean:
	make -C $(KERNEL_DIR) M=$(PWD) clean
else
obj-m += driver_app.o                                                      
endif

第三步就是最后测试的应用程序了


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

/* 
  *	app_mp_led on
  * app_mp_led off
 */
int main(int argc, char **argv)
{
	int fd;
	int val = 1;
	fd = open("/dev/my_device", O_RDWR);
	if (fd < 0)
	{
		printf("can't open!\n");
	}
	if (argc != 2)
	{
		printf("Usage :\n");
		printf("%s <on|off>\n", argv[0]);
		return 0;
	}

	if (strcmp(argv[1], "on") == 0)
	{
		val  = 1;
	}
	else
	{
		val = 0;
	}
	write(fd, &val, 4);
	return 0;
}

应用程序是不是很熟悉0.0,没错就是韦哥写的程序。哈哈。

最后说下做应用也是要多少了解驱动的。0.0.后续再聊

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值