linux驱动程序 ---- led


前言

本文基于S3C2440开发板。

一、原理图

在这里插入图片描述

在这里插入图片描述

二、芯片手册

在这里插入图片描述

三、驱动程序

led_drv.c

#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 <asm/arch/regs-gpio.h>
#include <asm/hardware.h>

static struct class *leddev_class;
static struct class_device	*leddev_class_dev;

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

static int s3c2440_led_open(struct inode *inode, struct file *file)
{
	//open核心代码,一般都是对一些硬件的初始化
	//printk("first_drv_open\n");
	/* 配置GPF4,5,6为输出 */
	*gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));
	*gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));
	return 0;
}
static ssize_t S3C2440_led_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
	//read核心代码,一般都是对一些硬件数据寄存器,状态寄存器的读取
}
static ssize_t s3c2440_led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
	//write核心代码,一般都是对一些输出引脚进行输出高低电平
	int val;

	//printk("first_drv_write\n");

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

	if (val == 1)
	{
		// 点灯
		*gpfdat &= ~((1<<4) | (1<<5) | (1<<6));
	}
	else
	{
		// 灭灯
		*gpfdat |= (1<<4) | (1<<5) | (1<<6);
	}
	
	return 0;
}
static struct file_operations led_drv_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   s3c2440_led_open,     
	.write	=	s3c2440_led_write,	   
};


int major;
static int s3c2440_led_init(void)
{
	major = register_chrdev(0, "led_drv", &led_drv_fops ); // 注册, 告诉内核

	leddev_class = class_create(THIS_MODULE, "leddrv");

	leddev_class_dev = class_device_create(leddev_class, NULL, MKDEV(major, 0), NULL, "led"); /* /dev/led */

	gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);//将一个IO地址空间映射到内核的虚拟地址空间上去,便于访问;从0X56000050开始16个字节 
	gpfdat = gpfcon + 1;

	return 0;
}
static void s3c2440_led_exit(void)
{
	unregister_chrdev(major, "led_drv"); // 卸载
	class_device_unregister(leddev_class_dev);
	class_destroy(leddev_class);
	iounmap(gpfcon);
}

module_init(s3c2440_led_init);
module_exit(s3c2440_led_exit);
MODULE_LICENSE("GPL");

makefile文件:

KERN_DIR = /home/book/system/linux-2.6.22.6

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

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

obj-m	+= led_drv.o

/home/book/system/linux-2.6.22.6 编译过的内核目录

四、驱动程序的测试

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

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

五、结果分析

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值