驱动专题:源码编写 1 led设备驱动及测试程序

 汇总地址:https://blog.csdn.net/chichi123137/article/details/80946381

#include <linux/module.h>
#include <linux/ioport.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <linux/init.h>
#include <linux/serial_core.h>
#include <linux/serial.h>

#include <asm/irq.h>
#include <mach/hardware.h>

#include <plat/regs-serial.h>
#include <mach/regs-gpio.h>
#include <asm/uaccess.h>


/*
 *编写框架
 *1 入口函数
 *	 1.1 注册字符设备(存在于/dev目录下)
 *	1.2 创建一个类
 *	 1.3 在这个类下创建一个设备(存在于/class目录下,用来代替mknod命令)
 *	 1.4 映射物理基地址为虚拟地址,一个控制寄存器地址,一个数据寄存器地址
 *2 出口函数
 * 	 2.1 卸载字符设备
 *	 2.2 卸载class下的设备
 *	 2.3 销毁这个类
 *	 2.4 取消地址映射
 *3 构造file_operations
 *	3.1 实现first_drv_open函数
 *		配置GPF0,1,2,3为输出模式(用来点灯)
 *	3.2 实现first_drv_write函数
 *		拷贝用户空间数据
 *		如果用户空间传来1,输出低电平,点灯
 *		如果用户空间传来0,输出高电平,灭灯
 */

static struct class *firstdrv_class;
static struct class_device	*firstdrv_class_dev;

volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;


static int first_drv_open(struct inode *inode, struct file *file)
{
	//printk("first_drv_open\n");
	/* 配置GPF0,1,2,3为输出 */
	*gpfcon &= ~((0x3<<(0*2)) | (0x3<<(1*2)) | (0x3<<(2*2)) | (0x3<<(3*2)));
	*gpfcon |= ((0x1<<(0*2)) | (0x1<<(1*2)) | (0x1<<(2*2)) | (0x1<<(3*2)));
	return 0;
}

static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
	int val;

	//printk("first_drv_write\n");

	copy_from_user(&val, buf, count); //	copy_to_user();

	if (val == 1)
	{
		// 点灯
		*gpfdat &= ~((1<<0) | (1<<1) | (1<<2) | (1<<3));
	}
	else
	{
		// 灭灯
		*gpfdat |= (1<<0) | (1<<1) | (1<<2) | (1<<3);
	}
	
	return 0;
}

// 定义操作函数结构体
static struct file_operations first_drv_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   first_drv_open,     
	.write	=	first_drv_write,	   
};


int major;
//入口函数
static int first_drv_init(void)
{
	/*
	 * 1 注册字符设备(存在于/dev目录下)
	 * 2 创建一个类
	 * 3 在这个类下创建一个设备(存在于/class目录下,用来代替mknod命令)
	 * 4 映射物理基地址为虚拟地址,一个控制寄存器地址,一个数据寄存器地址
	 */
	major = register_chrdev(0, "first_drv", &first_drv_fops); // 注册, 告诉内核

	firstdrv_class = class_create(THIS_MODULE, "firstdrv");

	firstdrv_class_dev = device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */

	gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
	gpfdat = gpfcon + 1;

	return 0;
}
//出口函数
static void first_drv_exit(void)
{
	/* 主要是和出口函数的反向操作
	 * 1 卸载字符设备
	 * 2 卸载class下的设备
	 * 3 销毁这个类
	 * 4 取消地址映射
	 */
	unregister_chrdev(major, "first_drv"); // 卸载
	/* 2.6.31 上只有device_create 其他版本内核可能有class_device_create */
	device_unregister(firstdrv_class_dev);
	class_destroy(firstdrv_class);
	iounmap(gpfcon);
}

//修饰first_drv_init函数为入口函数
//修饰first_drv_exit函数为出口函数

module_init(first_drv_init);
module_exit(first_drv_exit);

//使得本模块为支持GPL协议
MODULE_LICENSE("GPL");

上面是led驱动程序

 

下面为led测试程序


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

/* firstdrvtest on
  * firstdrvtest off
  */
int main(int argc, char **argv)
{
	int fd;
	int val = 1;
	fd = open("/dev/xyz", 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;
}

makefile如下

KERN_DIR = /home/linux/tools/linux-2.6.31

all:
	make -C $(KERN_DIR) M=`pwd` modules 

clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order

obj-m	+= driver_module.o

整体目录结构如下:

1 在menuconfig中配置好内核源码的目标系统为s3c2440,在makefile中配置好交叉编译器为arm-linux-

2 在pc上将驱动程序编译生成.ko

3 在pc上将测试程序编译生成elf可执行文件

4 挂载nfs,这样就可以在开发板上看到pc端的.ko文件和测试文件

mount -t nfs -o nolock,vers=2 192.168.0.103:/home/linux/nfs_root  /mnt

 

执行结果如下:

加载模块后,执行./firstdrvtest on 命令,灯亮,执行./firstdrvtest off命令,灯灭

 

如何根据原理图查询寄存器

1 先看主板的原理图:Tx2440底板 V3.pdf

再看核心板的原理图:COREBOARD .pdf

查询 s3c2440 手册GPF的寄存器地址,主要关注控制寄存器和数据寄存器

控制寄存器用来控制是输入、输出还是中断模式

数据寄存器用来收发数据

TX2440A用的GPF0,1,2,3,只要配置成输出模式,根据用户输入往数据寄存器写数据就ok了。

查询如何设置GPF引脚为输出引脚


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值