Seventh chapter LED will blink for me: Control curtain J Light emitting two-stage tube

Source: Internet
Author: User

First, the realization principle of LED drive

Although the Linux driver deals directly with hardware, it is not the Linux driver that writes data directly to the memory in the hardware, but interacts with the native I/O memory. Every hardware connected to Linux has a map header address in I/O memory, and the LEDs on the board have their first address.

Second, write led driver

First step: Initialize the Cdev with the Cdev_init function, describe the device file

struct cdev{

struct Kobject kobj; Objects that encapsulate device files

struct module *owner; Pointers to kernel modules

const struct File_operations *ops; Pointer to file_operations struct body

struct List_head list; Pointer to previous and next Cdev struct

dev_t Dev; Represents the device number, int type, first 12-bit main device number, and the last 20-bit device number

unsigned int count; The requested connection device number range (maximum), which is used when setting up multiple device files

}

void Cdev_init (struct cdev *cdev,const struct file_operations *fops)

{

memset (cdev,0,sizeof *cdev);

Init_list_head (&cdev->list);

Kobject_init (&cdev->kobj,&ktype_cdev_default);

Cdev->ops = FoPs;

}

The second step: Specify the device number with 1 int, the first 12 indicates the main device number, and the last 20 bits indicate the secondary device number.

int Dev_number=mkdev (Major,minor); Major represents the main device number, minor indicates the secondary device number

int major=major (dev_number);

int minor=major (dev_number);

Step three: Use the Cdev_add function to add a character device to the character device array in the kernel

int Cdev_add (struct Cdev *p,dev_t dev,unsigned count)

{

p->dec=dev;

p->count-count;

Return Kobj_map (CDEV_MAP,DEV,COUNT,NULL,EXACT_MATCH,EXACT_LOCK,P);

}

Fourth step: Create a struct class using the Class_create macro

struct class *leds_class = NULL;

Leds_class=class_create (This_module, "dev_name");

Fifth step: Create a device file using the Device_create function

The Device_create function is prototyped as follows: struct device *device_create (struct class *class,struct device *parent,dev_t devt,void *drvdata, Const CHAHR*FMT,...)

The Leds_init function is an initialization function of the LED driver:

Static in Leds_init (void)

{

int ret;

Ret=leds_create_device ();//Create a device file

PRINTK (device_name "\tinintialized\n");

}

Module_init (Leds_init);

Sixth step: Uninstalling the device files

static void Leds_destroy_device (void)

{

Device_destroy (Leds_class,dev_number);//Remove the character device created by the Device_create function

if (LEDS_CALSS)//Destroy struct class

Class_destroy (Leds_class);

Unregister_chrdev_region (Dec_number,device_number);//unregister character device area

Return

}

static void Leds_exit (void)

{

Leds_destroy_device ();//Uninstall the LED driver's device file

PRINTK (device_name "\texit!\n");

}

Module_exit (Leds_exit);

Seventh Step: Assigning registers

LEDS_INIT_GPM Function Initialization Register

Call the LEDS_INIT_GPM function in the Leds_init function to complete the initialization of the Register

static int leds_init (void)

{

int ret;

Ret=leds_create_device ();

LEDS_INIT_GPM (0xE);//Initialize Register

PRINTK (device_name "tinitialized\n");

return ret;

}

Eighth step: Control LED

Two ways: Through string control (using the File_operations.write function)

and control LEDs via I/o command (using the FILE_OPERATIONS.IOCTL function)

Third, Test led driver

The final test led driver, the method has a few

(i) General procedures for writing test f/0 control commands

(ii) test LED driver with NDK

(iii) test LED driver using Java

Four, LED-driven transplant

The simplest porting of LED drivers is to compile them under different lim muscle cores.

For example, the LED drivers in this book are compiled under Linux 2.6.36, and if you need to install the LED driver onto a system on a Linux 3.x kernel (Android 4.x and above), first get Linux 3. The source code for the X kernel is at least the Linux kernel's header file, and then recompiled under the new kernel.

http://www.cnblogs.com/JOKERLAU/

Seventh chapter LED will blink for me: Control curtain J Light emitting two-stage tube

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.