tiny4412 andriod灯光系统_led_class驱动

对于andriod的灯光系统解析:

首先别人的APP要能直接访问到你写的硬件或者不经过任何修改的APP能直接使用你的驱动,就需要使用系统自带的JNI,所以我们需要写出符合系统自带JNI文件的HAL文件和驱动程序.

写出leds_4412.c
1.分配led_classdev
2设置:
led_devs[i].cdev.max_brightness = LED_FULL;
led_devs[i].cdev.brightness_set = brightness_set_4412;
led_devs[i].cdev.flags = LED_CORE_SUSPENDRESUME;
led_devs[i].cdev.brightness = LED_OFF;
led_devs[i].cdev.name = led_gpios[i].name;
//led_devs[i].cdev.default_trigger = “timer”;
led_devs[i].gpio = led_gpios[i].gpio;

3注册:led_classdev_register


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
#include 
#include 
#include 

#include 

struct le d_desc {
	int gpio;
	char *name;
};

static struct led_desc led_gpios[] = {
	{EXYNOS4212_GPM4(0), "led1"},
	{EXYNOS4212_GPM4(1), "led2"},
	{EXYNOS4212_GPM4(2), "led3"},
	{EXYNOS4212_GPM4(3), "led4"},
};

struct led_classdev_4412 {
	struct led_classdev cdev;
	int gpio;
};


static struct led_classdev_4412 *led_devs;
static void	 brightness_set_4412(struct led_classdev *led_cdev,
			  enum led_brightness brightness)
{
	struct led_classdev_4412 *dev = (struct led_classdev_4412 *)led_cdev;

	led_cdev->brightness = brightness;

	if (brightness != LED_OFF)
		gpio_set_value(dev->gpio, 0);
	else
		gpio_set_value(dev->gpio, 1);
}


static int leds_init(void)
{
	int i;
	int ret;
	
	/* 1. alloc led_classdev */
	led_devs = kzalloc(sizeof(struct led_classdev_4412) * sizeof(led_gpios)/sizeof(led_gpios[0]), GFP_KERNEL);
	if (led_devs == NULL) {
		printk("No memory for device\n");
		return -ENOMEM;
	}
	
	for (i = 0; i < sizeof(led_gpios)/sizeof(led_gpios[0]); i++)
	{

		s3c_gpio_cfgpin(led_gpios[i].gpio, S3C_GPIO_OUTPUT);
		gpio_set_value(led_gpios[i].gpio, 1);

		/* 2. set */
		led_devs[i].cdev.max_brightness = LED_FULL;
		led_devs[i].cdev.brightness_set = brightness_set_4412;
		led_devs[i].cdev.flags = LED_CORE_SUSPENDRESUME;
		led_devs[i].cdev.brightness = LED_OFF;
		led_devs[i].cdev.name = led_gpios[i].name;
		//led_devs[i].cdev.default_trigger = "timer";
		led_devs[i].gpio = led_gpios[i].gpio;

		/* 3. led_classdev_register */
		ret = led_classdev_register(NULL, &led_devs[i].cdev);
		if (ret) {
			i--;
			while (i >= 0) {
				led_classdev_unregister(&led_devs[i].cdev);
				i--;
			}
			kfree(led_devs);
			return -EIO;
		}
	}
	
	return 0;
}

static void leds_exit(void)
{
	int i;
	for (i = 0; i < sizeof(led_gpios)/sizeof(led_gpios[0]); i++)
	{
		led_classdev_unregister(&led_devs[i].cdev);
	}
	kfree(led_devs);
}

module_init(leds_init);
module_exit(leds_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("www.100ask.net");



把 leds_4412.c 放到drivers/leds
修改 drivers/leds/Makefile:
obj-y += leds_4412.o

tiny4412 andriod灯光系统_led_class驱动_第1张图片

重新配置内核
make menuconfig
在device driver->
led_suppot->
->选中led_trigger_support

tiny4412 andriod灯光系统_led_class驱动_第2张图片
配置好之后 make zImage

在 arch/arm/boot/找到
tiny4412 andriod灯光系统_led_class驱动_第3张图片

CONFIG_LEDS_CLASS
CONFIG_LEDS_TRIGGERS
CONFIG_LEDS_TRIGGER_TIMER

进入 shell 里
tiny4412 andriod灯光系统_led_class驱动_第4张图片
已经 看见 有4盏灯
在这里插入图片描述
进行点灯操作
进行闪烁操作
在这里插入图片描述
cat trigger
看见里面的几种模式
在这里插入图片描述
输入模式 后开始闪烁

你可能感兴趣的:(kernel,嵌入式,安卓)