本来打算写一下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>