# GPIO - Erlang GPIO interface

**URL:** <https://erlangforums.com/t/gpio-erlang-gpio-interface/2262>\
**Category:** Libraries\
**Tags:** gpio\
**Created:** [January 12, 2023, 10:46am UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262 "2023-01-12T10:46:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![asabil](https://erlangforums.com/user_avatar/erlangforums.com/asabil/32/381_2.png) [@asabil](https://erlangforums.com/u/asabil)\
**Post date:** [January 12, 2023, 10:46am UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/1 "2023-01-12T10:46:31Z")

</div>

Hi,

I just published an Erlang GPIO library (for Linux) here: [gpio | Hex](https://hex.pm/packages/gpio)

We have been using it for some customer projects that have been in the field for a while now, and it was written using the modern gpio interface on Linux, not the old one based on sysfs which allows for cleaner APIs without resource leaks.

We also have an SPI library that needs to be extracted, an i2c one can be easily built based on what the SPI library does. I can try to get that one extracted and published if that would be helpful.

Happy to help with any of these, just let me know.

---

<div class="post-metadata">

**Author:** ![asabil](https://erlangforums.com/user_avatar/erlangforums.com/asabil/32/381_2.png) [@asabil](https://erlangforums.com/u/asabil)\
**Post date:** [January 12, 2023, 10:48am UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/2 "2023-01-12T10:48:32Z")

</div>

There is not much documentation, but hopefully the README here can get you started:

> <https://github.com/kopera/erlang-gpio/blob/master/README.md>

---

<div class="post-metadata">

**Author:** ![dischoen](https://erlangforums.com/user_avatar/erlangforums.com/dischoen/32/695_2.png) [@dischoen](https://erlangforums.com/u/dischoen)\
**Post date:** [January 12, 2023, 1:51pm UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/3 "2023-01-12T13:51:20Z")

</div>

Dear Ali,

that seems to be exactly what I have been looking for!  
In the meantime since I wrote this posting I did a little port of circuits\_gpio to erlang,  
this is working so far.  
As I just saw, it uses the sysfs interface. I was not aware of the deprecation yet.

I tried the library on a RPi4, but was not very successful yet.

I can open a chip, but opening lines does not work yet on my side:

I want to use GPIO16, 20 and 21. According to gpioinfo, the offsets should be identical to the  
numbers in the GPIO name:

```erlang
pi@r40:~ $ gpioinfo 
gpiochip0 - 58 lines:
	line 0: "ID_SDA" unused input active-high 
[..]
	line 16: "GPIO16" unused input active-high 
	line 17: "GPIO17" "onewire@11" output active-high [used open-drain]
	line 18: "GPIO18" unused input active-high 
	line 19: "GPIO19" unused input active-high 
	line 20: "GPIO20" unused input active-high 
	line 21: "GPIO21" unused input active-high 
[..]

```

```erlang
1> {ok,Chip0}=gpio:open_chip("/dev/gpiochip0").
{ok,#Ref<0.2094800486.2528772108.122490>}
2> {ok, L3} = gpio:open_lines(Chip0, [16,20,21], [output], [0,0,0], "my-application"). 
** exception error: bad argument
     in function gpio:open_lines_nif/5
        called as gpio:open_lines_nif(#Ref<0.2094800486.2528772108.122490>,
                                      [16,20,21],
                                      [output],
                                      [0,0,0],
                                      "my-application")

```

It seems to fail in enif\_get\_resource.  
Have you tried the library on a Raspberry, by any chance?  
If not, I could try some changes, if you have an idea…

If I use the gpiod tools, all works fine (also with gpiochip0), so the hardware seems to be fine.

---

<div class="post-metadata">

**Author:** ![asabil](https://erlangforums.com/user_avatar/erlangforums.com/asabil/32/381_2.png) [@asabil](https://erlangforums.com/u/asabil)\
**Post date:** [January 12, 2023, 2:05pm UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/4 "2023-01-12T14:05:30Z")

</div>

This was used on an i.MX8 system as well as an RPI CM3, I extracted the code from a project so I might have messed something up during the extraction. I am not able to test immediately, but it shouldn’t be hard to figure out the issue/fix.

The easiest could be to sprinkle some printf calls before every `enif_make_badarg` in `gpio_nif.c`

---

<div class="post-metadata">

**Author:** ![dischoen](https://erlangforums.com/user_avatar/erlangforums.com/dischoen/32/695_2.png) [@dischoen](https://erlangforums.com/u/dischoen)\
**Post date:** [January 12, 2023, 2:09pm UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/5 "2023-01-12T14:09:25Z")

</div>

ok, so in principle it should also work on the RPi4.  
I will play around some more.

Thanks!

---

<div class="post-metadata">

**Author:** ![asabil](https://erlangforums.com/user_avatar/erlangforums.com/asabil/32/381_2.png) [@asabil](https://erlangforums.com/u/asabil)\
**Post date:** [January 12, 2023, 10:30pm UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/6 "2023-01-12T22:30:25Z")

</div>

I realise we never really tested the opening of multiple lines for reading/writing. I just pushed a fix to the git repo, but haven’t made a new release yet.

```erlang
1> {ok, Chip0} = gpio:open_chip("/dev/gpiochip0").
{ok,#Ref<0.2675552943.3685613578.35853>}
2> {ok, L3} = gpio:open_lines(Chip0, [16,20,21], [output], [0,0,0], "my-application").
{ok,#Ref<0.2675552943.3685613578.35855>}

```

Checking using `debugfs`:

```bash
cat /sys/kernel/debug/gpio | grep my-application
 gpio-16 (GPIO16 |my-application ) out lo
 gpio-20 (GPIO20 |my-application ) out lo
 gpio-21 (GPIO21 |my-application ) out lo

```

From the erlang shell:

```erlang
3> gpio:write_lines(L3, {1,0,0}).
ok

```

Then checking again using `debugfs`

```bash
cat /sys/kernel/debug/gpio | grep my-application
 gpio-16 (GPIO16 |my-application ) out hi
 gpio-20 (GPIO20 |my-application ) out lo
 gpio-21 (GPIO21 |my-application ) out lo

```

Please let me know how it goes on your end so I can publish a new version on hex.pm

---

<div class="post-metadata">

**Author:** ![dischoen](https://erlangforums.com/user_avatar/erlangforums.com/dischoen/32/695_2.png) [@dischoen](https://erlangforums.com/u/dischoen)\
**Post date:** [January 13, 2023, 7:41am UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/7 "2023-01-13T07:41:54Z")

</div>

Good morning!

Thank you for the update and the info about the debugfs info!  
I think I found the reason for yesterday’s problems. I was testing in parallel with my circuits\_gpio\_erl library, which uses sysfs.  
And it seems that the gpio interface respects that a line is already in use by another driver.  
That seems reasonable.

On the other hand, a GPIO line, which has been used by sysfs is not freed when the sysfs program ends, even when I use gpio:close().

This might not be the case in general, that’s just what I have seen on my RPi4.

So I think I will switch my application to use your library and run some long-duration stress tests.  
Yesterday in the beginning I experienced some seg faults. Since you used the library in production I guess that were some user errors as well on my side.

---

<div class="post-metadata">

**Author:** ![asabil](https://erlangforums.com/user_avatar/erlangforums.com/asabil/32/381_2.png) [@asabil](https://erlangforums.com/u/asabil)\
**Post date:** [January 13, 2023, 9:10am UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/8 "2023-01-13T09:10:13Z")

</div>

> [@dischoen](#):
>
> And it seems that the gpio interface respects that a line is already in use by another driver.  
> That seems reasonable.
> 
> On the other hand, a GPIO line, which has been used by sysfs is not freed when the sysfs program ends, even when I use gpio:close().

That’s the main problem with the sysfs API and the main reason for the existence of the gpio chardev Linux API:

The sysfs API treated the GPIO access as a global shared resource that was not attached to any particular Linux process, this meant that when for example a program who used the sysfs GPIO interface crashed, the GPIOs were left in an undefined state, whereas with the chardev API, the GPIOs get reset to their initial state upon program termination. I personally avoid using the gpio sysfs API unless working with old Linux kernels (\< 4.8).

> [@dischoen](#):
>
> So I think I will switch my application to use your library and run some long-duration stress tests.  
> Yesterday in the beginning I experienced some seg faults. Since you used the library in production I guess that were some user errors as well on my side.

Please let me know if you experience any segfaults as I would like to get them fixed. To give you a bit more background about the gpio library I published yesterday, It has been in use since 2018 in different shapes and forms, we had different “forks” of it spread out across multiple projects (Yes, I know…). What I tried doing some time ago was to consolidate these forks into a single library.

I will go ahead and release a 0.6.1 that fixes the multi line issue.

---

<div class="post-metadata">

**Author:** ![schnef](https://erlangforums.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@schnef](https://erlangforums.com/u/schnef)\
**Post date:** [January 13, 2023, 9:20am UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/9 "2023-01-13T09:20:53Z")

</div>

Thanks for this library! Works on an old Rpi2B as a charm.  
Had to install the cli tools first with:

```erlang
apt-get install gpiod libgpiod-dev libgpiod-doc

```

I would be very interested in the SPI lib as well.

---

<div class="post-metadata">

**Author:** ![asabil](https://erlangforums.com/user_avatar/erlangforums.com/asabil/32/381_2.png) [@asabil](https://erlangforums.com/u/asabil)\
**Post date:** [January 13, 2023, 9:51am UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/10 "2023-01-13T09:51:04Z")

</div>

> [@schnef](#):
>
> Thanks for this library! Works on an old Rpi2B as a charm.  
> Had to install the cli tools first with:
> 
> ```erlang
> apt-get install gpiod libgpiod-dev libgpiod-doc
> 
> ```

Glad to hear 🙂 However, the library doesn’t depend on libgpiod, as it directly uses the necessary ioctl calls to interact with the device, so you don’t need to install them unless you want to use the debugging tools.

> [@schnef](#):
>
> I would be very interested in the SPI lib as well.

I will see what I can do, hopefully I can get the SPI and USB libraries open sourced as well.

---

<div class="post-metadata">

**Author:** ![maxlapshin](https://erlangforums.com/user_avatar/erlangforums.com/maxlapshin/32/1593_2.png) [@maxlapshin](https://erlangforums.com/u/maxlapshin)\
**Post date:** [January 17, 2023, 3:17pm UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/12 "2023-01-17T15:17:07Z")

</div>

it is not very clear: what is the problem with sysfs? Why have you decided to make a NIF instead of using 'old 'good plain files?

---

<div class="post-metadata">

**Author:** ![asabil](https://erlangforums.com/user_avatar/erlangforums.com/asabil/32/381_2.png) [@asabil](https://erlangforums.com/u/asabil)\
**Post date:** [January 17, 2023, 3:33pm UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/13 "2023-01-17T15:33:41Z")

</div>

from [https://www.kernel.org/doc/html/latest/admin-guide/gpio/sysfs.html:](https://www.kernel.org/doc/html/latest/admin-guide/gpio/sysfs.html:)

> THIS ABI IS DEPRECATED, THE ABI DOCUMENTATION HAS BEEN MOVED TO Documentation/ABI/obsolete/sysfs-gpio AND NEW USERSPACE CONSUMERS ARE SUPPOSED TO USE THE CHARACTER DEVICE ABI. THIS OLD SYSFS ABI WILL NOT BE DEVELOPED (NO NEW FEATURES), IT WILL JUST BE MAINTAINED.

The reason for deprecation is that it is inherently unsafe. The new chardev based interface ensures:

1. exclusive access to a GPIO pin(s), including restricting access to GPIO pins that have already been claimed by in-kernel device drivers;
2. restoration of initial state whenever the owning process terminates.

I would have avoided using a NIF, if there was a way of using `ioctl` and `select/poll/...` calls from erlang.

---

<div class="post-metadata">

**Author:** ![maxlapshin](https://erlangforums.com/user_avatar/erlangforums.com/maxlapshin/32/1593_2.png) [@maxlapshin](https://erlangforums.com/u/maxlapshin)\
**Post date:** [January 17, 2023, 4:07pm UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/14 "2023-01-17T16:07:12Z")

</div>

wow! Thank you for this documentation link. I haven’t seen it yet =(

---

<div class="post-metadata">

**Author:** ![joaohf](https://erlangforums.com/user_avatar/erlangforums.com/joaohf/32/427_2.png) [@joaohf](https://erlangforums.com/u/joaohf)\
**Post date:** [January 23, 2023, 11:23pm UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/15 "2023-01-23T23:23:01Z")

</div>

Hello @asabil

Your library looks good. I’ll update one of my blog posts related to embedded buses library state ([Buses | BEAM Mignon](https://beam-mignon.netlify.app/en/posts/mignon-33/)).

We (the company that I’m working for) had a similar approach too. One of our main source of pain was reference counting when open/close gpio lines. e.g.: when the code has many process using the same gpiochip with many opened lines from different process.

BTW, there is an EEF WG for such libraries which interfacing with buses (likes I2C, SPI, GPIO, …) here: [Erlang Ecosystem Foundation - Supporting the BEAM community](https://erlef.org/wg/embedded) we could start some discussion around there.

---

<div class="post-metadata">

**Author:** ![asabil](https://erlangforums.com/user_avatar/erlangforums.com/asabil/32/381_2.png) [@asabil](https://erlangforums.com/u/asabil)\
**Post date:** [January 30, 2023, 10:17am UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/16 "2023-01-30T10:17:49Z")

</div>

> [@joaohf](#):
>
> Your library looks good. I’ll update one of my blog posts related to embedded buses library state ([Buses | BEAM Mignon](https://beam-mignon.netlify.app/en/posts/mignon-33/)).

Thank you 🙂

> [@joaohf](#):
>
> We (the company that I’m working for) had a similar approach too. One of our main source of pain was reference counting when open/close gpio lines. e.g.: when the code has many process using the same gpiochip with many opened lines from different process.

Out of curiosity, why do you share the gpiochip? why not open the gpiochip many times from the different processes that needs it? You are only restricted when it comes to gpiolines.

> [@joaohf](#):
>
> BTW, there is an EEF WG for such libraries which interfacing with buses (likes I2C, SPI, GPIO, …) here: [Erlang Ecosystem Foundation - Supporting the BEAM community](https://erlef.org/wg/embedded) we could start some discussion around there.

How can one join such working group? I would love to help with whatever I can.

---

<div class="post-metadata">

**Author:** ![joaohf](https://erlangforums.com/user_avatar/erlangforums.com/joaohf/32/427_2.png) [@joaohf](https://erlangforums.com/u/joaohf)\
**Post date:** [January 31, 2023, 12:30am UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/17 "2023-01-31T00:30:59Z")

</div>

> [@asabil](#):
>
> How can one join such working group? I would love to help with whatever I can.

That is easy. Please, follow the procedures from here: [Erlang Ecosystem Foundation - Supporting the BEAM community](https://erlef.org/slack-invite/erlef) it’s just about getting a slack invite. Once get there you can join the Embedded WG (or any other that you want to). At the main EEF website, bottom page, there is a calendar with all meetings from all WGs.

> [@asabil](#):
>
> Out of curiosity, why do you share the gpiochip? why not open the gpiochip many times from the different processes that needs it? You are only restricted when it comes to gpiolines.

That is a nicer approach as well. I didn’t consider that. It could solve the question. Thanks.

---

<div class="post-metadata">

**Author:** ![asabil](https://erlangforums.com/user_avatar/erlangforums.com/asabil/32/381_2.png) [@asabil](https://erlangforums.com/u/asabil)\
**Post date:** [February 5, 2023, 9:58am UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/18 "2023-02-05T09:58:03Z")

</div>

Just published [0.6.2](https://hex.pm/packages/gpio/0.6.2) with hopefully better documentation thanks to the [ex\_doc](https://github.com/elixir-lang/ex_doc) and [rebar3\_ex\_doc](https://github.com/starbelly/rebar3_ex_doc)

---

<div class="post-metadata">

**Author:** ![schnef](https://erlangforums.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@schnef](https://erlangforums.com/u/schnef)\
**Post date:** [March 21, 2023, 8:30am UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/19 "2023-03-21T08:30:42Z")

</div>

Do you think you can still publish the SPI library? I am currently using erlang-ale for SPI and am now running into a name clash with erlang-gpio.

---

<div class="post-metadata">

**Author:** ![asabil](https://erlangforums.com/user_avatar/erlangforums.com/asabil/32/381_2.png) [@asabil](https://erlangforums.com/u/asabil)\
**Post date:** [March 21, 2023, 10:42pm UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/20 "2023-03-21T22:42:44Z")

</div>

Thank you for the reminder, I just published it today: [SPI - Erlang SPI interface](https://erlangforums.com/t/spi-erlang-spi-interface/2488)

---

<div class="post-metadata">

**Author:** ![schnef](https://erlangforums.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@schnef](https://erlangforums.com/u/schnef)\
**Post date:** [March 22, 2023, 3:40pm UTC](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262/21 "2023-03-22T15:40:36Z")

</div>

Thanks for publishing it.

[Next page](https://erlangforums.com/t/gpio-erlang-gpio-interface/2262.md?page=2)
