Driver for LED lamp control mounted on/proc

Source: Internet
Author: User

Summarize the development, code, and process of LED lamp control mounted on/proc through one-day study.
1. driver code
[Cpp]
# Include <linux/module. h>
# Include <linux/init. h>
# Include <linux/version. h>
# Include <linux/proc_fs.h>
# Include <asm/uaccess. h>
# Include <linux/gpio. h>
# Include <mach/regs-gpio.h>
MODULE_LICENSE ("Dual BSD/GPL ");

# Define USER_ROOT_DIR "led"
# Define USER_ENTRY1 "led_entry1"

Static struct proc_dir_entry * led_root;
Static struct proc_dir_entry * led_entry1;
 
Static char msg [255];
Static unsigned long led_table [] =
{
S3C2410_GPB (5 ),
S3C2410_GPB (6 ),
S3C2410_GPB (7 ),
S3C2410_GPB (8 ),
};
Static unsigned int led_cfg_table [] =
{
S3C2410_GPIO_OUTPUT,
S3C2410_GPIO_OUTPUT,
S3C2410_GPIO_OUTPUT,
S3C2410_GPIO_OUTPUT,
};
 
// Int proc_read_information (char * page, char ** start, off_t off, int count, int * eof, void * data );
Int proc_write_information (struct file * file, const char * buffer, unsigned long count, void * data );

Static int proc_test_init (void)
{
Int I;
For (I = 0; I <4; I ++)
{
S3c2410_gpio_cfgpin (led_table [I], led_pai_table [I]);
}
// Create user root dir under/proc
Led_root = proc_mkdir (USER_ROOT_DIR, NULL );
If (NULL = led_root)
{
Printk (KERN_ALERT "Create dir/proc/% s error! \ N ", USER_ROOT_DIR );
Return-1;
}
Printk (KERN_INFO "Create dir/proc/% s \ n", USER_ROOT_DIR );
 
// Create a test entry under USER_ROOT_DIR
Led_entry1 = maid (USER_ENTRY1, 0666, led_root );
If (NULL = led_entry1)
{
Printk (KERN_ALERT "Create entry % s under/proc/% s error! \ N ", USER_ENTRY1, USER_ROOT_DIR );
Goto err_out;
}
Printk (KERN_INFO "Create/proc/% s \ n", USER_ROOT_DIR, USER_ENTRY1 );
 
// Led_entry1-> read_proc = proc_read_information;
Led_entry1-> write_proc = proc_write_information;
 
Return 0;
 
Err_out: remove_proc_entry (USER_ROOT_DIR, led_root );
Return-1;
}
 
Static void proc_test_exit (void)
{
// Remove all entries
Remove_proc_entry (USER_ENTRY1, led_root );
Remove_proc_entry (USER_ROOT_DIR, NULL );
Printk (KERN_INFO "All Proc Entry Removed! \ N ");
}
 
// Int proc_read_information (char * page, char ** start, off_t off, int count, int * eof, void * data)
//{
 
//}
Int proc_write_information (struct file * file, const char * buffer, unsigned long count, void * data)
{
Int count2 = 0, I;
If (copy_from_user (void *) msg, (const void _ user *) buffer, count ))
Return-EFAULT;
Count2 = simple_strtoul (msg, NULL, 0 );
 
// Printk ("information % d \ n", count2 );
Switch (count2)
{
Case 0:
Case 1:
For (I = 0; I <4; I ++)
{
S3c2410_gpio_setpin (led_table [I],! Count2 );
}
Break;
Default:
Return-EINVAL;
}
 
Return 0;
}

Module_init (proc_test_init );
Module_exit (proc_test_exit );

2. Drive Makefile
[Cpp]
Obj-m: = led2.o

CURRENT_PATH: = $ (shell pwd)

ARM_LINUX_KERNEL: =/opt/FriendlyARM/mini2440/linux-2.6.32.2

All:
$ (MAKE)-C $ (ARM_LINUX_KERNEL) SUBDIRS = $ (CURRENT_PATH) modules

Clean:
Rm-rf *. cmd *. o *. ko *. mod. c *. symvers *. order
3. Development Process
Compile the driver code and Makefile. After compilation on the PC, upload the led2.ko file to the Development Board through FTP and load the driver (insmod led2.ko ),
Run echo 1>/proc/led/led_entry1, and the light is on.
Execute echo 0>/proc/led/led_entry1 and turn off all the lights.
Note: The default file system of the Development Board already has an led test program. Therefore, run the following command on the command line terminal of the Development Board:

#/Etc/rc. d/init. d/leds stop

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.