steveryan

steveryan

Hello,

I’m getting started developing with Nerves and Circuits.GPIO.

When my program starts I’m calling a function like below, to open GPIO pin 26 for output:

@output_pin Application.compile_env(:Program, :output_pin, 26)

  def setup_gpio() do
    {:ok, output_pin} = GPIO.open(@output_pin, :output)
  end

This has so far worked fine, but when testing something, I crashed the program, and now when I go to re-run it I get the following error:

(MatchError) no match of right hand side value: {:error, :already_open}

Which I take to mean, since the program crashed, it didn’t clean up GPIO pin connection, so it’s still left open. My question is, how do I now do anything with that pin. I have tried closing it, but since I do not have access to the handle I can’t get it to close. (e.g. GPIO.close("GPIO26") gives me an error.

Is there any way, short of unplugging the raspberry pi and plugging it back in that I can either get the handle of this GPIO connection so I can write to it, OR close the connection, so that I can reopen it and capture the handle?

Showing Posts 1 to 6

lawik

lawik

Nerves Core Team

I have dealt with this at times by implementing the terminate callback (a GenServer owned the reference) and making sure it tidies up before it dies.

I would assume restarting the BEAM would help..

Hermanverschooten

Hermanverschooten

Lars, maybe a liitle bit of code to help out?

jjcarstens

jjcarstens

Nerves Core Team

Do you have reproducible code? Typically closed process reference is cleaned up by Circuits. The fact you’re getting that error tells me you may not be opening the GPIO from the same process(es) that crashes, you may be inadvertently trying to open it multiple times on process boot, or a potential bug in Circuits. But I’ll need a little bit more code example to be helpful in figuring that out :pray:

fhunleth

fhunleth

Co-author of Nerves

@jjcarstens A few people have had similar problems when Circuits.GPIO started requiring that there only be one reference per GPIO at a time. I think that Circuits.GPIO will need to be more helpful since it is turning out to affect more people than expected.

In case it’s helps, the happy path for using Circuits.GPIO is to wrap a GPIO or set of GPIOs in a GenServer. The GPIO reference returned by Circuits.GPIO.open/3 should only exist in that GenServer’s state. I.e., don’t return it. (This is what @jjcarstens and @lawik said, but it seemed good to repeat.) When the GenServer crashes, Erlang’s garbage collector will clean up the GPIO reference and that will free up the GPIO to be used when the Supervisor starts it up again. Adding a call to Circuits.GPIO.close/1 in the terminate callback like @lawik says seems nice. Erlang’s garbage collector is normally amazingly quick at cleaning dead processes up should terminate not get called.

The current unhappy path for working with GPIOs is to create an API that returns raw references. This seems like a completely reasonable thing to do, but once the GPIO references get stored in a couple processes, Erlang’s GC will probably think the GPIO is alive when it’s really not.

If the code isn’t performance critical, try using Circuits.GPIO.write_one/3 and Circuits.GPIO.read_one/2 to avoid processes owning references completely. This might be a quick fix.

I filed an issue on Circuits.GPIO since it should be possible to do something without rebooting.

steveryan

steveryan OP

Really appreciate all the responses, and especially the issue in the Circuits.GPIO.

Some additional information that might help:

  1. While I’m still prototyping my design, I’m doing this all in a live book running on the raspberry pi itself rather than running my program directly with Nerves on the board. That could potentially be impacting it I guess.
  2. I have not wrapped this in a GenServer, you are correct. The original function I posted is called once when the program starts, the reference is saved to a variable, and I pass that variable around for the rest of the programs run.
  3. The issue occurred when a simple, dumb mistake caused the program to crash (I think I called Float.parse on something it couldn’t understand or something). Then when I clicked “re-evaluate” it would crash at the setup_gpio function call because the GPIO was already open.
fhunleth

fhunleth

Co-author of Nerves

Thanks for the additional detail about using Livebook. I totally see how you ran into this, and everything you did seems completely reasonable. At the moment, I’m unsure how best to fix this, but it’s pretty clear that something is needed.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews