Course 2-led

Source: Internet
Author: User

After the hello World Driver in the previous article, I am familiar with the general framework of the driver program. The driver of the second lesson is led! Pay attention to how it operates gpio and how it is associated with the user layer (Application Layer. Code attached:

 

# Include <Linux/miscdevice. h>
# Include <Linux/delay. h>
# Include <ASM/IRQ. h>
# Include <Mach/regs-gpio.h>
# Include <Mach/hardware. h>
# Include <Linux/kernel. h>
# Include <Linux/module. h>
# Include <Linux/init. h>
# Include <Linux/mm. h>
# Include <Linux/fs. h>
# Include <Linux/types. h>
# Include <Linux/delay. h>
# Include <Linux/moduleparam. h>
# Include <Linux/slab. h>
# Include <Linux/errno. h>
# Include <Linux/IOCTL. h>
# Include <Linux/cdev. h>
# Include <Linux/string. h>
# Include <Linux/list. h>
# Include <Linux/PCI. h>
# Include <Linux/gpio. h>
# Include <ASM/uaccess. h>
# Include <ASM/Atomic. h>
# Include <ASM/unistd. h>

# Define device_name "LEDs" // driver name

 

Static unsigned long led_table [] =

{// Set the corresponding gpio port to an array to facilitate the For Loop
S3c2410_gpb (5 ),
S3c2410_gpb (6 ),
S3c2410_gpb (7 ),
S3c2410_gpb (8 ),
};

 

Static unsigned int led_cfg_table [] =

{// Output mode of the gpio Port
S3c2410_gpio_output,
S3c2410_gpio_output,
S3c2410_gpio_output,
S3c2410_gpio_output,
};

 

Static int sbc2440_leds_ioctl (struct inode * inode, struct file * file, unsigned int cmd, unsigned long Arg)

{// Function for executing operations on gpio
Switch (CMD)

{
Case 0:
Case 1: If (Arg> 4)

{
Return-einval;
}
S3c2410_gpio_setpin (led_table [Arg],! CMD); // output on the led_table [Arg] pin (! CMD)
Return 0; // Where cmd controls the LED switch (0, 1) and Arg Control

// Specify the number of LEDs (0, 1, 2, and 3)

Default: Return-einval;
}
}

 

Static struct file_operations dev_fops =

{// Associate the functions (functions) in the driver with the application layer

// Common examples include open, close, read, and write.

// Functions available at the application layer are provided here
. Owner = this_module,
. IOCTL = sbc2440_leds_ioctl,
};

 

Static struct miscdevice MISC =

{// Miscellaneous device, a 10-character device with an English letter
. Minor = misc_dynamic_minor,
. Name = device_name,
. Fops = & dev_fops,
};

 

Static int _ init dev_init (void)
{
Int ret;

Int I;
 
For (I = 0; I <4; I ++)

{
S3c2410_gpio_cfgpin (led_table [I], led_cfg_table [I]); // sets the gpio Function
S3c2410_gpio_setpin (led_table [I], 0); // The initial output is low.
}

Ret = misc_register (& MISC); // misc_register is to call register_chrdev ()

// While register_chrdev () is a character device registration function

Printk (device_name "/tinitialized/N ");

Return ret;
}

 

Static void _ exit dev_exit (void)
{
Misc_deregister (& MISC); // same as above
}

 

Module_init (dev_init );
Module_exit (dev_exit );
Module_license ("GPL"); // The information of the compliant agreement
Module_author ("friendlyarm Inc."); // code author information

 

Note:

1. Static struct file_operations dev_fops = {.........}

This is critical. All the driver functions called in high-level applications must be defined here.

 

2. IOCTL () function (from http://hi.baidu.com/760159/blog/item/75c225f3dea26d19b17ec525.html)

Static int sbc2440_leds_ioctl (struct inode * inode, struct file * file, unsigned int cmd, unsigned long Arg)

The ioctl function is an attribute component in the file structure. IOCTL is a function used by the device driver to manage the device's I/O channels. The management of the I/O channel is to control some features of the device, such as the Serial Transmission baud rate and the motor speed.
Struct inode * inode, which is the device node number. FD indicates the File Identifier returned by the open function when the user program opens the device. CMD indicates the control command of the user program on the device. Unsigned long Arg indicates the number of control commands.
The driver provides IOCTL support, and you can use the ioctl function in your program to control the I/O channel of the device. If the function returns a non-negative value, the value is returned to the calling program, indicating success. Generally, switch {Case} is used to control some features of the device. Switch {Case} structure. Each case corresponds to a command code and some operations are performed accordingly. In this example, CMD has two optional values: 0 and 1.0, indicating that the light is off, and 1 indicates that the light is on. Therefore, Case 0, 1 must be operated. Because the actual hardware connection is low-level light. Therefore, when assigning values to the pins, we need to reverse them. S3c2410_gpio_setpin (led_table [Arg],! CMD)

 

3. device function registration (in _ init) and uninstallation (in _ exit) are usually paired.

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.