Read "Android Deep Exploration (Volume 1) HAL and drive development" some thoughts 07

Source: Internet
Author: User

Seven, LED will blink for me: Control light-emitting secondary tube

1. Create LED-driven device files

1th Step: Initialize the Cdev with the Cdev_init function

Most of the member variables of Cdev do not need to be initialized by ourselves, as long as the Cdev init function is called to initialize most of the Cdev member variables.

2nd step: Specify the device number

The device number of the Linux device file is the primary device number and the secondary device number. represented by 1 int types (dev_t). The first 12 bits represent the main device number, and the last 20 bits represent the secondary device number. There are two ways to specify the device number.

Specified directly in the code (hard-coded).

Dynamic allocation.

3rd Step: Use the Cdev_add function to add the character device to the character device array in the kernel

The Cdev_add function is used to add a character device to the probes. Array (save the character device that you have built). The Cdev_add function is implemented in the ifs/char dev.c file.

4th step: Create a struct class using the Class_create macro

The struct class contains some variables related to device files and some callback function pointer variables. The code for creating a struct class using the Class_create macro is as follows:

struct C1ass *leds_class=null;

Leds_class = Class_create (This_module, "dev_name");

5th step: Create a device file using the Device_create function

The Device_creat function is used to create a device file, which is defined in the /inciude/1innx/device.b, in a file, and implemented in the /drivers/base/core.c file.

2. Uninstalling the LED-driven device files

Uninstalling Linux-powered device files is a little simpler--you need to call the Device_destroy, Class_destroy, and Unreg ister_chrdev_region methods in turn. The prototypes of these 3 methods are as follows:

void Device_destroy (struct class *class, dev_t devt)

void Class_destroy (struct class *els)

void Unregister_chrdev_region (dev_t from,unsigned count)

3. Setting the register and initializing the LED driver

It is important to understand how to set the register before the control LED is lit and extinguished. The ARM processor has multiple registers, by setting the values of different registers. You can set the status of the LED PIN, open or disable the pull-up (pull-up) circuit, and control the light and off of the LEDs.

4. Control LED

LED drivers can use the following two ways to control LEDs:

The LED is controlled by a string,

Control led by l/0 command;

To control LEDs, use both of these methods. The LED driver must receive the corresponding data. If the LED is controlled by a string. You need to use the File_operations.write function. If you control LEDs with the i/0 command, you need to use the FILE_OPERATIONS.IOCTL function. Where the File_operations.write function has been used in the Word_count driver of the previous one, the File_oprations.write function can receive data written to the device file. And since the FILE-OPERATIONS.IOCTL function we are the first contact, this function can receive commands and parameters sent to the character device.

Use the following command to control the LED's light and off through a string.

#adb Shell ' echo 1101 >/dev/s3c6410_leds '//from the side of the battery 3rd led off, other LEDs are lit

#adb Shell ' echo 1 >/dev/s3c6410 LEDs '//The nearest led on the battery is on, other LEDs are off

Read "Android Deep Exploration (Volume 1) HAL and drive development" some thoughts 07

Related Article

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.