Led驱动程序及应用程序编写

本文介绍了LED驱动程序的编写,首先从LED的硬件原理图出发,理解其工作原理,然后列出驱动程序中成对出现的函数,并展示了手动创建设备节点的版本。此外,还提供了测试程序和shell脚本,帮助读者进行实践操作,熟悉驱动开发流程。
摘要由CSDN通过智能技术生成

本来打算写一下NAND驱动和网卡驱动及USB驱动的,突然发现好长时间不写驱动,写驱动的套路都忘的差不多了,而且以前写的驱动程序都不够模块化,想复用的话还需要做些修改,所以,还是先写一下LED驱动,熟悉一下套路,顺便把程序模块化了,留着以后备用。

1.先看一下LED的硬件原理图


这里写图片描述


这里写图片描述

根据以上两张图片内容,可以列出如下表格:

LED-number LED-net GPIO
LED1 nLED_1 GPB5
LED2 nLED_2 GPB6
LED3 nLED_3 GPB7
LED4 nLED_4 GPB8

根据电路的基本知识,IO口低电平时,LED亮,IO口高电平时,LED灭.

CMD STATE GPIO-LEVEL
ON 0
OFF 1

用下列宏可以获得主设备号和次设备号

MAJOR(dev_t dev)
MANOR(dev_t dev)

用下列宏则可以通过主设备号和次设备号生成dev_t;

MKDEV(int major,int minor)

/**
Allocates a range of char device numbers.The major number will be chosen
dynamically ,and retured(along with the first minor number)in @dev.Returns zero or negative error code.
*/
int alloc_chrdev_region(dev_t *dev,unsigned baseminor,unsigned count,const char *name);
/*
This function will unregisters a range of @count device numbers,starting with @from,The
caller should normally be the one who allocated those numbers in the first place...
*/
void unregister_chrdev_region(dev_t from,unsigned count);

以上两个函数是动态分配设备号和注销设备号的函数,为了使函数可移植,使用上面两个函数处理设备号。

建立一个表格,列出成对出现的函数

Tables Are Cool
ioremap iounmap xxx

这里写图片描述
这里写图片描述

led驱动程序-手动创建设备节点版本

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/cdev.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值