×ðÁú¿­Ê±¹ÙÍøµÇ¼

LinuxÇý¶¯ | ÔÚÇý¶¯Öн¨Éèsysfs½Ó¿Ú

Ç°ÑÔ

ÔÚһЩlinux¿ª·¢°åÖУ¬¾­³£¿ÉÒÔ¿´µ½Í¨¹ýechoµÄ·½·¨À´Ö±½Ó¿ØÖÆÓ²¼þ»òÕßÐÞ¸ÄÇý¶¯£¬ÀýÈ磺

//µÆÃð
echo 0 >/sys/class/leds/firefly:blue:power/brightness 
//µÆÁÁ
echo 1 >/sys/class/leds/firefly:blue:power/brightness

µÇ¼ºó¸´ÖÆ

ÕâÊÇÔõô×öµ½ÄØ£¿

ÏÖʵÉÏ£¬ÕâÊÇÓÉÓÚÔÚÇý¶¯ÖÐÌṩÁËsysfs½Ó¿Ú¸øÓû§Ê¹Óã¬Ê¹µÃÓû§¿ÉÒÔͨ¹ýcat»òÕßechoÏÂÁîÀ´Éó²éºÍÐÞ¸ÄÇý¶¯ÖÐijЩ±äÁ¿µÄÖµ¡£

ÏÂÃæÏÈÈÝÇý¶¯Öн¨Éèsysfs½Ó¿ÚµÄÒªÁì¡£

sysfs½Ó¿Ú½¨Éè

»ù±¾°ì·¨£º

1¡¢Ê¹ÓÃDEVICE_ATTRÉùÃ÷Ò»¸ösys½Úµã

static DEVICE_ATTR(led_status, 0600, led_status_show, led_status_store);

µÇ¼ºó¸´ÖÆ

led_status£ºÔÚsys½Ó¿ÚÖÐÏÔʾµÄ½ÚµãÃû×Ö

0600£ºÌåÏÖ²Ù×÷Õâ¸öled_status½ÚµãµÄȨÏÞ

led_status_show£ºÊ¹ÓÃcatÏÂÁîÉó²ésys½Ó¿ÚʱŲÓõĺ¯Êý

led_status_store£ºÊ¹ÓÃechoÏÂÁîÍùsys½Ó¿ÚдÈëÄÚÈÝʱŲÓõĺ¯Êý

2¡¢Íê³Ésys½ÚµãµÄ¶Áдº¯Êý

static unsigned int led = 0;
/*
*  sys½ÚµãµÄ¶Áº¯Êý
*  Ö´ÐÐ cat /sys/devices/platform/leds/led_statusʱ»áŲÓÃ
*/
static ssize_t led_status_show(struct device *dev, struct device_attribute *attr, char *buf)
{
  //bufÊÇͨ¹ýcatÏÂÁîÏÔʾµ½Öն˵ÄÄÚÈÝ£¬ÕâÀïÏÔʾled±äÁ¿
 return sprintf(buf, "%s:%d.\n", "led", led);
}

/**
*  sys½ÚµãµÄдº¯Êý
*  ÓÃechoÏÂÁîÍùsys½ÚµãдÈëÄÚÈÝʱ£¬»áŲÓøú¯Êý
*/
static ssize_t led_status_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
  //дÈëµÄÄÚÈÝ»á´æ·Åµ½bufÖУ¬ÕâÀォbufÄÚÈݸ³Öµ¸øled±äÁ¿
 sscanf(buf, "%d", &led);

 return count;
}

µÇ¼ºó¸´ÖÆ

ʾÀýÖУ¬led_status_show()º¯ÊýºÍled_status_store()º¯ÊýµÄ×÷Ó÷ÖΪ´òÓ¡led±äÁ¿µÄÖµºÍÐÞ¸Äled±äÁ¿µÄÖµ.

3¡¢½ç˵struct attributeºÍstruct attribute_groupÊý×é

static struct attribute *led_attributes[]={
 
  /*ÉÏÊöʹÓÃÁËDEVICE_ATTRÉùÃ÷½ÚµãÃû×ÖΪled_status£¬
  * Ôòstruct attributeÃû×ÖӦΪ£º
  *  dev_attr_ + (½ÚµãÃû) + .attr
  * ÒÔÊÇÃû×ÖΪdev_attr_led_status.attr
  */
  &dev_attr_led_status.attr,
 NULL,
};


static const struct attribute_group led_attrs={
 .attrs = led_attributes,//ÒýÓÃÉÏÊöstruct attributeÊý×é
};

µÇ¼ºó¸´ÖÆ

ÉÏÊöʹÓÃÁËDEVICE_ATTRÉùÃ÷½ÚµãÃû×ÖΪled_status£¬ Ôòstruct attributeÃû×ÖӦΪ£ºdev_attr_ + (½ÚµãÃû) + .attr¡£ÒÔÊÇÃû×ÖΪdev_attr_led_status.attr¡£

4¡¢ÔÚprobeº¯ÊýÖÐŲÓÃsysfs_create_group()º¯Êý×¢²ásysfs½Ó¿Ú

ÍêÕûÀý×Ó

×°±¸Ê÷£º

 leds:leds{
  compatible = "xx,xx-led";
 };

µÇ¼ºó¸´ÖÆ

Çý¶¯£º

static unsigned int led = 0;

static ssize_t led_status_show(struct device *dev, struct device_attribute *attr, char *buf)
{
 return sprintf(buf, "%s:%d.\n", "led", led);
}

static ssize_t led_status_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
 sscanf(buf, "%d", &led);

 return count;
}

static DEVICE_ATTR(led_status, 0600, led_status_show, led_status_store);

static struct attribute *led_attributes[]={
 &dev_attr_led_status.attr,
 NULL,
};


static const struct attribute_group led_attrs={
 .attrs = led_attributes,
};

static int xx_led_probe(struct platform_device *pdev)
{
 sysfs_create_group(&pdev->dev.kobj, &led_attrs);
 return 0;
}

static int xx_led_remove(struct platform_device *pdev)
{
 sysfs_remove_group(&pdev->dev.kobj, &led_attrs);
 return 0;
}

static const struct of_device_id xx_led_of_match[] = {
 {.compatible = "xx,xx-led"},
};


static struct platform_driver xx_led_driver = {
 .probe = xx_led_probe,
 .remove = xx_led_remove,
 .driver = {
  .name = "xx-led",
  .owner = THIS_MODULE,
  .of_match_table = xx_led_of_match,
 },
};

static int __init xx_led_init(void)
{
 return platform_driver_register(&xx_led_driver );
}

static void __exit xx_led_exit(void)
{
 platform_driver_unregister(&xx_led_driver);
}

module_init(xx_led_init);
module_exit(xx_led_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("xx led driver");
MODULE_AUTHOR("Vincent");
MODULE_VERSION("V1.0.00");

µÇ¼ºó¸´ÖÆ

Çý¶¯¼ÓÔغ󣬾ͿÉÒÔÔÚlinuxÖÕ¶ËÖУ¬Ê¹ÓÃcatºÍechoÏÂÁîÀ´Éó²éºÍÐÞ¸ÄÇý¶¯ÖÐled±äÁ¿µÄÖµ¡£ÀýÈ磺

//Éó²éled±äÁ¿µÄÖµ
cat /sys/devices/platform/leds/led_status
led:0.

//ÐÞ¸Äled±äÁ¿µÄֵΪ9
echo 9 > /sys/devices/platform/leds/led_status
//Éó²é
cat /sys/devices/platform/leds/led_status
led:9.

µÇ¼ºó¸´ÖÆ

ÒÔÉϾÍÊÇLinuxÇý¶¯ | ÔÚÇý¶¯Öн¨Éèsysfs½Ó¿ÚµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ