思维导图设计
led.h
#define LED_MAGIC ‘L‘ #define LED_ON _IO(LED_MAGIC, 0) #define LED_OFF _IO(LED_MAGIC, 1)
led.c
#include <linux/module.h> #include <linux/init.h> #include <linux/cdev.h> #include <linux/fs.h> #include <linux/io.h> #include "led.h" #define LEDCON 0x7f008800 #define LEDDAT 0x7f008808 unsigned int *led_config; unsigned int *led_data; struct cdev cdev; dev_t devno; int led_open(struct inode *node, struct file *filp) { led_config = ioremap(LEDCON,4); writel(0x11110000,led_config); led_data = ioremap(LEDDAT,4); return 0; } long led_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { switch (cmd) { case LED_ON: writel(0x00,led_data); return 0; case LED_OFF: writel(0xff,led_data); return 0; default: return -EINVAL; } } static struct file_operations led_fops = { .open = led_open, .unlocked_ioctl = led_ioctl, }; static int led_init() { cdev_init(&cdev,&led_fops); alloc_chrdev_region(&devno, 0 , 1 , "myled"); cdev_add(&cdev, devno, 1); return 0; } static void led_exit() { cdev_del(&cdev); unregister_chrdev_region(devno,1); } module_init(led_init); module_exit(led_exit);Makefile
obj-m :=led.o KDIR :=/home/kernal_driver/linux-tiny6410/ all: make -C $(KDIR) M=$(PWD) modules CROSS_COMPILE=arm-linux- ARCH=arm clean: rm -f *.o *.bak *.order *.symversled_app.c
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/ioctl.h> #include "led.h" int main(int argc, char *argv[]) { int fd; int cmd; if (argc <2 ) { printf("please enter the second para!\n"); return 0; } cmd = atoi(argv[1]); fd = open("/dev/myled",O_RDWR); if (cmd == 1) ioctl(fd,LED_ON); else ioctl(fd,LED_OFF); return 0; }
ARM内核驱动—LED驱动程序设计,布布扣,bubuko.com
原文:http://blog.csdn.net/renren900207/article/details/22571297