{"id":27862,"date":"2020-11-06T22:01:01","date_gmt":"2020-11-06T21:01:01","guid":{"rendered":"https:\/\/pi3g.com\/?p=27862"},"modified":"2020-11-06T22:01:01","modified_gmt":"2020-11-06T21:01:01","slug":"controlling-leds-on-alpine-linux-using-the-command-line","status":"publish","type":"post","link":"https:\/\/pi3g.com\/de\/controlling-leds-on-alpine-linux-using-the-command-line\/","title":{"rendered":"Steuerung von LEDs auf Alpine Linux \u00fcber die Kommandozeile"},"content":{"rendered":"<p>We have developed an embedded Raspberry Pi Compute Module carrier board for industrial use: the PCCB.<\/p>\n<p><a href=\"https:\/\/buyzero.de\/products\/pccb-pi-control-carrier-board-for-raspberry-pi-compute-module?variant=31494671335526\"><img loading=\"lazy\" decoding=\"async\" width=\"926\" height=\"603\" title=\"pccb-small\" style=\"display: inline; background-image: none;\" alt=\"pccb-small\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/11\/pccb-small.jpg\" border=\"0\"><\/a><\/p>\n<p><\/p>\n<p>As you can see, there are three (directly) user programmable LEDs on the PCCB: USER \/ INFO \/ ACT.<\/p>\n<p>These are defined in the device tree. For example, we can define the following in our device tree overlay:<\/p>\n<blockquote>\n<p>\/\/LEDs<br \/>\nfragment@11 {<br \/>&nbsp;&nbsp; target = &lt;&amp;leds&gt;;<br \/>&nbsp;&nbsp; __overlay__ {<br \/>&nbsp;&nbsp;&nbsp;&nbsp; led_act: ledact {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label = &#8220;myledact&#8221;;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gpios = &lt;&amp;gpio 5 0&gt;;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; linux,default-trigger = &#8220;mmc0&#8221;;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; };<br \/>&nbsp;&nbsp;&nbsp;&nbsp; led_info: ledinfo {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label = &#8220;myledinfo&#8221;;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gpios = &lt;&amp;gpio 16 0&gt;;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; linux,default-trigger = &#8220;cpu&#8221;;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; };<br \/>&nbsp;&nbsp;&nbsp;&nbsp; led_user: leduser {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label = &#8220;myleduser&#8221;;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gpios = &lt;&amp;gpio 22 0&gt;;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; linux,default-trigger = &#8220;heartbeat&#8221;;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; };<br \/>&nbsp;&nbsp; };<br \/>\n};\n<\/p>\n<p><\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/11\/image.png\"><img loading=\"lazy\" decoding=\"async\" width=\"552\" height=\"469\" title=\"image\" style=\"display: inline; background-image: none;\" alt=\"image\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/11\/image_thumb.png\" border=\"0\"><\/a><\/p>\n<p>The device tree overlay source file (.dts) will need to be compiled, and will yield a .dtbo file.<\/p>\n<p><a href=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/11\/image-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"575\" height=\"56\" title=\"image\" style=\"display: inline; background-image: none;\" alt=\"image\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/11\/image_thumb-1.png\" border=\"0\"><\/a><\/p>\n<p>This file is included as an overlay in the config.txt (in our case it actually is included in a subfile, usercfg.txt which is included from the config.txt)<\/p>\n<blockquote>\n<p># Pi Control Carrier Board Overlay<br \/>\ndtoverlay=pi-control-carrier-board<\/p>\n<\/blockquote>\n<p>This will tell the VideoCore OS which is loaded first to pass on the device tree for the particular Pi model including our overlay to the Linux kernel.<\/p>\n<h1>What is a device tree?<\/h1>\n<p>ARM devices have become increasingly more customized and diverse as compared to standard x86 devices which had standarized components.<\/p>\n<p>In order to avoid recompiling the kernel and having to match every single modified hardware, a system of describing the hardware layout to the Linux kernel was devised.<\/p>\n<p>The device tree.<\/p>\n<p>It is an OS independent concept, as it is loaded into the OSes Kernel. Any operating system which wishes to, can support it.<\/p>\n<p>The Linux kernel will receive the device tree, and thus knows which device is present, how it is connected, and how to talk to it (drivers are loaded depending on hints in the device tree, for example).<\/p>\n<p>As a Raspberry Pi is not static hardware, but it comes in different configurations &amp; can expose different pins, for example, and also have different add-ons (HATs), an additional add-on was required: device tree overlays.<\/p>\n<p>These files allow to modify the device tree, without having to have an individual new device tree for every possibly combination of hardware you can attach to the Pi \/ configure the Pi to.<\/p>\n<h1>How do I control the LEDs?<\/h1>\n<p>These LEDs will turn up in the system, in the <strong>\/sys\/class\/leds<\/strong> directory:<\/p>\n<p><a href=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/11\/image-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"849\" height=\"124\" title=\"image\" style=\"display: inline; background-image: none;\" alt=\"image\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/11\/image_thumb-2.png\" border=\"0\"><\/a><\/p>\n<p>As you can see, a total of four LEDs is exposed here, three of which (myledact, myledinfo, myleduser) were added by us using the overlay!<\/p>\n<p>Each of these LEDs is a directory in of itself,<\/p>\n<p><a href=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/11\/image-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"852\" height=\"61\" title=\"image\" style=\"display: inline; background-image: none;\" alt=\"image\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/11\/image_thumb-3.png\" border=\"0\"><\/a><\/p>\n<p>which gives you control over the LED.<\/p>\n<p>Remember, in Linux everything is a file \/ talking to the kernel is abstracted to doing operations on the filesystem (it\u2019s actually a UNIX paradigm).<\/p>\n<p>Run<\/p>\n<blockquote>\n<p>cat trigger<\/p>\n<\/blockquote>\n<p>for the LED.<\/p>\n<p>It will show you a list of which triggers can be set for it:<\/p>\n<p><em>none rc-feedback kbd-scrolllock kbd-numlock kbd-capslock kbd-kanalock kbd-shiftlock kbd-altgrlock kbd-ctrllock kbd-altlock kbd-shiftllock kbd-shiftrlock kbd-ctrlllock kbd-ctrlrlock timer oneshot heartbeat backlight gpio cpu cpu0 cpu1 cpu2 cpu3 default-on input panic [mmc0]<\/em><\/p>\n<p>Note that the last trigger, mmc0 is in square brackets. This is what the LED is currently set to!<\/p>\n<p>You can try setting it to any of the other triggers by writing this trigger to the LED (if you have sufficient rights <img decoding=\"async\" class=\"wlEmoticon wlEmoticon-smile\" style=\"\" alt=\"Smile\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/11\/wlEmoticon-smile.png\">):<\/p>\n<blockquote>\n<p>echo \u201ctimer\u201d &gt; trigger<\/p>\n<\/blockquote>\n<p>Will make the LED blink.<\/p>\n<p>Note, if you now list the LED control directory again, you\u2019ll see additional entries \u2013 new functionality which is exposed for the timer mode:<\/p>\n<p><a href=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/11\/image-4.png\"><img loading=\"lazy\" decoding=\"async\" width=\"896\" height=\"67\" title=\"image\" style=\"display: inline; background-image: none;\" alt=\"image\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/11\/image_thumb-4.png\" border=\"0\"><\/a><\/p>\n<p>delay_off and delay_on<\/p>\n<blockquote>\n<p>cat delay_off<\/p>\n<\/blockquote>\n<p>yields 500. These are 500 milliseconds.<\/p>\n<p>You can adjust these values to make the LED blink faster, for example:<\/p>\n<blockquote>\n<p>echo &#8220;150&#8221; &gt; delay_off<\/p>\n<p>echo &#8220;150&#8221; &gt; delay_on<\/p>\n<\/blockquote>\n<p>Or slower \u2026<\/p>\n<blockquote>\n<p>echo &#8220;1500&#8221; &gt; delay_on<\/p>\n<p>echo &#8220;1500&#8221; &gt; delay_off<\/p>\n<\/blockquote>\n<p>A couple of noteworthy triggers:<\/p>\n<ul>\n<li>none: there is no activity on the LED<\/li>\n<li>mmc0: is active if the Flash \/ microSD is being accessed <\/li>\n<li>cpu: is active with CPU activity (nice to monitor how busy the CPU is, as it will be kind of dimmer if there\u2019s less CPU activity, thanks to effectively a kind of PWM)<\/li>\n<li>heartbeat: will give a steady blink pattern to show the Pi is still live and the kernel is still online and doing it\u2019s work.<\/li>\n<\/ul>\n<h1>Controlling the LED GPIO from my own code?<\/h1>\n<p>To control the LEDs from your own code, using GPIO libraries, set the trigger to \u201cnone\u201d.<\/p>\n<blockquote>\n<p>echo &#8220;none&#8221; &gt; trigger<\/p>\n<\/blockquote>\n<p>You can still turn it on and off from the command line, by writing to brightness (1 to turn it on, 0 to turn it off):<\/p>\n<blockquote>\n<p>echo &#8220;1&#8221; &gt; brightness<\/p>\n<p>echo &#8220;0&#8221; &gt; brightness<\/p>\n<\/blockquote>\n<p>Now you\u2019re able to control an LED from the command line <img decoding=\"async\" class=\"wlEmoticon wlEmoticon-smile\" style=\"\" alt=\"Smile\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/11\/wlEmoticon-smile.png\"> And you\u2019ve learned something about device trees and device tree overlays, congratulations!<\/p>\n<h2>References:<\/h2>\n<ul>\n<li><a title=\"https:\/\/kofler.info\/on-board-leds-des-raspberry-pi-steuern\/\" href=\"https:\/\/kofler.info\/on-board-leds-des-raspberry-pi-steuern\/\">https:\/\/kofler.info\/on-board-leds-des-raspberry-pi-steuern\/<\/a> (German)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Wir haben ein eingebettetes Raspberry Pi Compute Module Tr\u00e4gerboard f\u00fcr den industriellen Einsatz entwickelt: das PCCB. Wie Sie sehen k\u00f6nnen, gibt es drei (direkt) benutzerprogrammierbare LEDs auf dem PCCB: USER \/ INFO \/ ACT. Diese werden im Ger\u00e4tebaum definiert. Wir k\u00f6nnen zum Beispiel folgendes in unserem Ger\u00e4tebaum-Overlay definieren: \/\/LEDs...<\/p>","protected":false},"author":830,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[401,1,400],"tags":[435,936,937,885,938,935,240],"class_list":["post-27862","post","type-post","status-publish","format-standard","hentry","category-alpine-linux","category-raspberrypi-blog","category-raspberry-pi-embedded-development","tag-alpine-linux","tag-cli","tag-command-line","tag-control","tag-device-tree","tag-leds","tag-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts\/27862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/users\/830"}],"replies":[{"embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/comments?post=27862"}],"version-history":[{"count":1,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts\/27862\/revisions"}],"predecessor-version":[{"id":27863,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts\/27862\/revisions\/27863"}],"wp:attachment":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/media?parent=27862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/categories?post=27862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/tags?post=27862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}