Hi3519v101gpio字符驱动-点亮第一个LED灯

25 篇文章 0 订阅
10 篇文章 0 订阅

1、led_drv.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <asm/io.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <linux/ioport.h>
#include <linux/of.h>
#include <linux/uaccess.h>

// 操作管脚为gpio3_2
// 管脚复用寄存器默认为gpio,这里就不去操作了
#define led_phy_base 0x12143000 //led管脚物理地址
#define led_phy_data 0x010		//led管脚数据偏移地址,0b00_0001_0000
#define led_phy_dir 0x400		//led管脚方向寄存器偏移地址

#define gpio_out 0x4 //gpio3_2 配置为输出,1输出 0输入def
#define gpio_out_h 0xFF
#define gpio_out_l 0x00

static struct class *leddrv_class;
static struct device *leddrv_class_dev;

static void __iomem *led_base; //寄存器基地址
static void __iomem *led_dir;  //方向

static int led_drv_open(struct inode *inode, struct file *filp)
{
	iowrite8(gpio_out, led_dir);
	printk("set gpio out mode\n");
	return 0;
}

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

	__copy_from_user(&val, buf, count); //用户到内核,__copy_to_user();//内核到用户
	if (val == 1)
	{
		iowrite8(gpio_out_h, led_base);
		printk("set gpio out h");
	}
	else
	{
		iowrite8(gpio_out_l, led_base);
		printk("set gpio out l");
	}

	return 0;
}

static struct file_operations led_drv_fops =
	{
		.owner = THIS_MODULE, //这是一个宏,推向编译模块时自动创建的__this_module变量
		.open = led_drv_open,
		.write = led_drv_write,
};

int major;

int led_drv_init(void)
{
	major = register_chrdev(0, "led_drv", &led_drv_fops); // 注册, 告诉内核
	leddrv_class = class_create(THIS_MODULE, "led_drv");
	leddrv_class_dev = device_create(leddrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); //dev/xyz

	//地址映射:把物理地址转换为虚拟地址
	led_base = ioremap(led_phy_base, 1); //读取1个字节长度够用了
	led_dir = led_base + led_phy_dir;
	led_base = led_base + led_phy_data;

	printk("led_base = 0x%x\n", (unsigned int)led_base);
	printk("led_dir = 0x%x\n", (unsigned int)led_dir);
	return 0;
}

void led_drv_exit(void)
{
	unregister_chrdev(major, "led_drv");
	device_unregister(leddrv_class_dev);
	class_destroy(leddrv_class);
	iounmap(led_base); //取消物理地址的映射
}

module_init(led_drv_init); //模块初始化接口
module_exit(led_drv_exit); //模块推出接口
MODULE_LICENSE("GPL");

2、led_drv_test.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
 
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;
}

3、Makefile

#内核路径
KERN_DIR = /home/osrc/Hi3519V101_SDK_V1.0.4.0/osdrv/opensource/kernel/linux-3.18.y
 
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

4、测试

/ # insmod led_drv.ko
led_base = 0xfea43010
led_dir = 0xfea43400
/ # ./led_drv_test on
set gpio out mode
/ # ./led_drv_test off
set gpio out mode

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值