linux驅(qū)動:[1]LED驅(qū)動/dev/led
LED Linux驅(qū)動程序
測試平臺: Xunlong Orange Pi Zero
代碼一覽(解析見下方)
驅(qū)動程序以及Makefile如下:
- sun8i_opizero_led.c:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <asm/uaccess.h>
#include <asm/io.h>
static struct class *sun8i_opizero_led_class;
//STATUS-LED:PA17
#define PIO_BASE 0x1C20800
volatile unsigned long *pacfg[4] = {NULL};
volatile unsigned long *padat = NULL;
static int sun8i_opizero_led_open(struct inode *inode, struct file *file)
{
//configure pa17 to output mode
*pacfg[2] &= ~(3 << 5);
return 0;
}
static ssize_t sun8i_opizero_led_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
int val;
copy_from_user(&val, buf, count);
if (val == 1)
*padat |= (1 << 17);
else
*padat &= ~(1 << 17);
return 0;
}
static struct file_operations sun8i_opizero_led_fops = {
.owner = THIS_MODULE,
.open = sun8i_opizero_led_open,
.write = sun8i_opizero_led_write,
};
int major;
int sun8i_opizero_led_init(void)
{
major = register_chrdev(0, "led", &sun8i_opizero_led_fops);
sun8i_opizero_led_class = class_create(THIS_MODULE, "led");
device_create(sun8i_opizero_led_class, NULL, MKDEV(major, 0), NULL, "led");
pacfg[0] = (volatile unsigned long *)ioremap(PIO_BASE, 0x20);
pacfg[1] = pacfg[0] + 1;
pacfg[2] = pacfg[1] + 1;
pacfg[3] = pacfg[2] + 1;
padat = pacfg[3] + 1;
return 0;
}
static void sun8i_opizero_led_exit(void)
{
unregister_chrdev(major, "led");
device_destroy(sun8i_opizero_led_class, MKDEV(major, 0));
class_destroy(sun8i_opizero_led_class);
iounmap(pacfg[0]);
}
module_init(sun8i_opizero_led_init);
module_exit(sun8i_opizero_led_exit);
MODULE_DESCRIPTION("LED driver for Xunlong Orange Pi Zero");
MODULE_AUTHOR("Techping Chan <techping.chan@gmail.com>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:orange-pi-zero-led");
- Makefile:
obj-m := sun8i_opizero_led.o #編譯進模塊
KERNELDIR := /lib/modules/3.4.113-sun8i/build #此處為linux內(nèi)核庫目錄
PWD := $(shell pwd) #獲取當前目錄
OUTPUT := $(obj-m) $(obj-m:.o=.ko) $(obj-m:.o=.mod.o) $(obj-m:.o=.mod.c) modules.order Module.symvers
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf $(OUTPUT)
在shell中使用以下命令裝載驅(qū)動程序:
$ make
$ insmod sun8i_opizero_led.ko
使用linux c進行測試:
- led_test.c:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int fd, val = 1;
fd = open("/dev/led", O_RDWR);
if (fd < 0)
printf("can't open led device");
if (argc != 2) {
printf("Usage:\n");
printf("%s <on|off>\n", argv[0]);
return 1;
}
if (strcmp(argv[1], "on") == 0)
val = 1;
else
val = 0;
write(fd, &val, 4);
return 0;
}
進行編譯统抬、測試:
$ gcc -o led_test led_test.c
$ ./led_test on
$ ./led_test off
沒問題亩歹,成功操作LED!
代碼解析:
寫Linux驅(qū)動程序的步驟無非是:
驅(qū)動框架
硬件操作
- 看原理圖
- 看數(shù)據(jù)手冊
- 編寫代碼
這里編寫的程序和單片機程序的區(qū)別就是:
單片機一般不具備MMU(內(nèi)存管理單元)晚吞,使用的是物理地址蚂会,而現(xiàn)在的SoC一般都帶有MMU勾效,使用虛擬地址茸苇。這時候我們就需要用Linux C庫提供的 ioremap 函數(shù)去將物理地址映射為虛擬地址各吨。
led_schematic
通過查看原理圖枝笨,我們得知LED(STATUS-LED)接在PA17處。
pio_datasheet_0
Port Controller Register 的物理基地址為0x01C20800揭蜒,在 sun8i_opizero_led.c 中使用:
pacfg[0] = (volatile unsigned long *)ioremap(PIO_BASE, 0x20);
把PA_CFG0伺帘、PA_CFG1、......PA_PUL1這0x20字節(jié)物理地址映射到pacfg[0]~(paccfg[0] + 8)忌锯。
之后的操作也是就跟操作單片機一樣的位操作了。
pio_datasheet_1
static int sun8i_opizero_led_open(struct inode *inode, struct file *file)
{
//configure pa17 to output
*pacfg[2] &= ~(3 << 5);
return 0;
}
pio_datasheet_2
if (val == 1)
*padat |= (1 << 17);
else
*padat &= ~(1 << 17);
對硬件操作封裝成固定的驅(qū)動程序框架格式领炫,經(jīng)過編譯之后就可以注冊到內(nèi)核以待使用了偶垮。
- 我的個人主頁:http://www.techping.cn/
- 我的個人站點博客:http://www.techping.cn/blog/wordpress/
- 我的CSDN博客:http://blog.csdn.net/techping
- 我的簡書:http://www.reibang.com/users/b2a36e431d5e/timeline
- 我的GitHub:https://github.com/techping
歡迎相互follow~