This procedure uses the dynamic mapping method to control the LED, the hardware platform is the ok6410 of the Flying Ling
Led.h: Defining control commands
#ifndef _led_h#define _led_h#define led_magic ' M ' #define LED_ON _io (led_magic, 0) #define Led_off _io (LED _magic, 1) #endif
Driver LED.C
#include <linux/module.h> #include <linux/init.h> #include <linux/cdev.h> #include <linux/fs.h > #include <asm/uaccess.h> #include <asm/io.h> #include "led.h" #define LEDCON 0x7f008820//related to specific platforms # Define leddat 0x7f008824unsigned int *led_config;unsigned int *led_data;struct cdev leddev;dev_t devno;static int Led_ope N (struct inode *inode, struct file *filp) {return 0;} static int led_close (struct inode *inode, struct file *filp) {return 0;} Static long Led_ioctl (struct file* filp, unsigned int cmd, unsigned long arg) {switch (cmd) {case Led_on:writel (0x00, Led_da TA); break;case Led_off:writel (0xFF, led_data); break;default:return-einval;} return 0;} struct File_operations ledfops = {. open = Led_open,.unlocked_ioctl = Led_ioctl,.release = led_close,};static int Led_init ( void) {/* Register character device */cdev_init (&leddev, &ledfops); Alloc_chrdev_region (&devno, 0, 1, "Leddev"); Cdev_add ( &leddev, Devno, 1);/* Mapping configuration Register */led_config = Ioremap (Ledcon, 4);/* Low four bit set to output mode, here the brute force simple solution did not handle*/writel (0x00001111, led_config);/* Map data Register */led_data = Ioremap (Leddat, 4);p rintk ("led_init\n"); return 0;} static void Led_exit (void) {Iounmap (led_config); Iounmap (Led_data); Cdev_del (&leddev); Unregister_chrdev_region ( Devno, 1);p rintk ("led_exit\n");} Module_init (Led_init); Module_exit (Led_exit); Module_license ("GPL"); Module_author ("Liuwei"); Module_description ("char driver");
Application Led_app.c
#include <stdio.h> #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 cmd;if (ARGC < 2) {printf ("usage:%s <cmd >\n ", argv[0]); return 0;} cmd = atoi (argv[1]), int fd = open ("/dev/leddev", O_RDWR), if (fd = =-1) {perror ("open"); return-1;} if (cmd = = 1) ioctl (FD, led_on); Elseioctl (FD, Led_off); close (FD); return 0;}
Compile the program, use the command Mknod/dev/leddev C 252 0 To create the device node, the main device number through the Cat/proc/devices
Running the application
./led_app 0 Off LED
./led_app 1-Light LED
LED drivers under Linux, ok6410