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

本文介绍了一个简单的S3C2440 LED驱动程序设计与测试过程,包括字符设备注册、GPIO配置、文件操作接口实现及测试程序编写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 汇总地址:https://blog.youkuaiyun.com/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引脚为输出引脚


 

HD2010操作说明(HD2010 operating instructions) ⅰ.软件安装(Installed software) 双击光盘HD2010 V2.0目录下的安装文件图标 ,将控制系统安装到个人电脑.如下图2-1 (Open Profile HD2010 V2.0 in the compact disk,Dblclick Installed the HD2010 V2.0 in your personal computer.As shown in Figure 2-1) 图(Figure)2-1 点击确定后进入下一步如图2-2 (Click确定intro- next step.As shown in Figure 2-2 ) 图(Figure)2-2 点击下一步后进入下一步(英文环境)在中文环境直接点击下一步完成安装)如图2-3 2-4 2-5 2-6 2-7 (Click next intro- next step.As shown in Figure 2-3 2-4 2-5 2-6 2-7) 图(Figure)2-3 注意:发送的文件保存在安装目录下的ProjFile文件夹里,点击下一步完成安装 (Announcements:The file what you send saved in the ProjFile in path of Install.And click next to install and finish) 图(Figure)2-4 图(Figure)2-5 图(Figure)2-6 安装最后一步如图2-7 (The final step of install. As shown in Figure 2-7) 图(Figure)2-7 点击完成打开软件主界面如图2-8 (Click finish open main interface of the software. As shown in Figure 2-8) ⅱ.软件设置(Setting software) ⅱ.ⅰ主界面如图2-8(Main interface.As shown in Figure 2-8) 图(Figure)2-8 ⅱ.ⅱ软件属性(Software properties) 1.文件菜单(File menu) a.新建--新建一个新的显示屏(New—Create a new screen) b.打开--打开一个显示屏(Open—Open a exist screen) c.保存--保存建立的文件(Save—Save file) d.另存为--保存副本(Save as—save a copy) e.导出.hds--导出.hds文件,用于u盘读取文件 (Export.hds—Export.hds file,for usb reading) f.退出--退出软件(Exit—Exit the software) 2.设置菜单(Settings menu) a.屏参设置--设置显示屏属性 (Screen settings—Display Screen configuration attributes) b.通信设置--通信端口/方式设置 (Communication settings—Set Communication prot and fashion) c.系统设置--配置系统默认项 (System settings—Set the acquice properties of software) 3.操作菜单(Operate menu) a.发送项目--发送节目(Send project—send the programmes) b.导出到U盘--把节目导入到U盘 (Export to U disk—send the programme to your U disk) c.时间设置--时间校对 (Time setting—get the right time im your computer) d.亮度设置--选择亮度模式 (Luminance setting—Choice the module of Luminance) e.固件更新--固件升级(Update Firmware—Update your controller Firmware) 4.语言菜单(Language menu) a.简体中文(Simplified Chinese) b.繁体中文(Chinese Traditional) c.英语(English) ⅱ.ⅲ节目编辑(Edit
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值