Driver for LED lamp control mounted to/sys

Source: Internet
Author: User

Mount a directory under/sys/power to control LED lights of the Development Board

1. driver source code

#include <linux/kobject.h>#include <linux/sysfs.h>#include <linux/init.h>#include <linux/kernel.h>#include <linux/platform_device.h>#include <linux/device.h>#include <linux/module.h>#include <asm/uaccess.h>#include <linux/gpio.h>#include <mach/regs-gpio.h>MODULE_LICENSE("Dual BSD/GPL");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,};static ssize_t leds_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t len){int status = 0,i;sscanf(buf, "%d", &status);for(i=0;i<4;i++){s3c2410_gpio_setpin(led_table[i],!status);}//Todo.printk("store1 stauts %d\n", status);return len;}static ssize_t leds_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf){int status = 0, len = 0;len += sprintf(buf + len, "%d\n",status);//Todo.printk("show1 stauts %d\n", status);return len;}static struct kobj_attribute leds_attr = {.attr= {.name = __stringify(leds),.mode = 0644,},.show = leds_show,.store = leds_store,};static struct attribute * extend_power_ctrl[] = {&leds_attr.attr,NULL,};static struct attribute_group attr_extend_ctrl_group = {.attrs = extend_power_ctrl,};static void extend_power_ctrl_default(){//Todo.//init gpio for leds, etc.}extern struct kobject *power_kobj;static int extend_power_ctrl_init(void){extend_power_ctrl_default();//power_kobj = sysfs_create_and_add("leds", NULL);if (sysfs_create_group(power_kobj, &attr_extend_ctrl_group)){printk("extend power interface init is failed!\n");return 1;}return 0;}static void extend_power_ctrl_exit(void){sysfs_remove_group(power_kobj, &attr_extend_ctrl_group);}static int __init obj_sys_init(void){int i;for( i=0;i<4;i++){s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);}int ret;printk("obj sys init!\n");ret = extend_power_ctrl_init();return ret;}static void obj_sys_exit(void){printk("obj sys exit!\n");extend_power_ctrl_exit();}module_init(obj_sys_init);module_exit(obj_sys_exit);

2. Load the kernel to compile and generate the zImage file, and then download it to the Development Board.

Execute echo 1>/sys/power/leds. The light is on.

Run echo 0>/sys/power/leds to turn off all 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

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.