The main learning in this chapter is the control of light emitting diodes, which in this chapter will accomplish a real Linux driver. The Linux driver is used to control 4 LED lights on the development version. That is, by sending data to the Linux driver, you can control the switch of the LED light.
Writing LED Drivers :
1. Initialize Cdev with cdev_init function, describe 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;
}
2, the specified device number with 1 int type, the first 12 digits represents the main device number, the last 20 bits indicates 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);
3. 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);
}
4. Create a struct class using the Class_create macro
struct class *leds_class = NULL;
Leds_class=class_create (This_module, "dev_name");
5. 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);
6. 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);
7. Allocation Register
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;
}
8, 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)
Test LED Driver
The final test led driver, there are several methods:
1. Writing a generic program for testing f/0 control commands
2. Test LED driver with NDK
3. Test LED driver using Java
LED-driven porting
The simplest porting of LED drivers is to compile them under different Lim versions.
Seventh Chapter Experience