Linux LED driver test source code

Source: Internet
Author: User
Linux LED driver test source code-Linux general technology-Linux programming and kernel information, the following is a detailed description. // ■

// [Copyright] Copyright (c) 2006-2008 GLIETHTTP

//

// [File version] v1.x

//

// [Creation date] 2006/12/21

//

// [Description] LED flashlight drive test-led_driver.c

// ■

# Ifndef _ KERNEL __

# Define _ KERNEL __

# Endif

# Ifndef MODULE

# Define MODULE

# Endif

# Include

# Include

# Include

# Include

# Include

# Include /* Printk ()*/

# Include /* Kmalloc ()*/

# Include /* Everything ...*/

# Include /* Error codes */

# Include /* Size_t */

# Include /* O_ACCMODE */

# Include /* COPY_TO_USER */

# Include/* cli (), * _ flags */

# Include



# Include "led_driver.h"



# Define DEVICE_NAME "led"

# Define led_MAJOR 254

# Define led_MINOR 0



# Define DBG (x...) printk (DEVICE_NAME ":" x)

//

Static int led_ioctl (struct inode * inode, struct file * file, unsigned int cmd, unsigned long arg)

{

Switch (cmd)

{

Case 1: xledbench on (); break;

Case 2: xledshortoff (); break;

Case 3: xLED2_ON (); break;

Case 4: xLED2_OFF (); break;

Default: DBG ("error cmd number \ n"); break;

}

Return 0;

}

//

Static int led_open (struct inode * inode, struct file * file)

{

MOD_INC_USE_COUNT;

Xledbench on ();

DBG ("device open sucess! \ N ");

Return 0;

}

//

Static int led_release (struct inode * inode, struct file * filp)

{

MOD_DEC_USE_COUNT;

Xled0000off ();

DBG ("device release \ n ");

Return 0;

}

//

Static struct file_operations led_fops = {

Owner: THIS_MODULE,

Ioctl: led_ioctl,

Open: led_open,

Release: led_release,

};

//

Static int _ init led_init (void)

{Int result;

SET_MODULE_OWNER (& led_fops );

Result = register_chrdev (led_MAJOR, "led", & led_fops );

If (result <0) return result;

DBG ("initialized \ n ");

XLED_Install ();

Return 0;

}

//

Static void _ exit led_exit (void)

{

Unregister_chrdev (led_MAJOR, "led ");

DBG ("unloaded \ n ");

}

//

Module_init (led_init );

Module_exit (led_exit );

MODULE_LICENSE ("GPL ");

MODULE_AUTHOR ("gliethttp ");

MODULE_DESCRIPTION ("led driver test! ");


Compile:

[Root @ gliethttp] # arm-linux-gcc-c-I my_linux_head_dir led_driver.c

After compilation, an led_driver.o file is generated, and led_driver.o is sent to the target board through the rz three-line serial port, and a device node is created on the target board.

[Root@ B .J.xx/root] # mknod/dev/led c 254 0

Then install the driver

[Root@ B .J.xx/root] # insmod led_driver.o

You can use lsmod to query the driver module.

[Root@ B .J.xx/root] # lsmod

Uninstall the driver through rmmod

# Rmmod led_driver [root@ B .J.xx/root]


After the driver is installed, when the application calls fd = open ("/dev/led", O_RDWR), the LED indicator on the Development Board will light up. When the application calls close (fd) the LED on the Development Board is off.
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.