GPIO failed: No such file or directory

I keep running into this error when running a code after booting up. The code write to certain GPIO under /sys/class/gpio. It complains:

No such file or directory

I reset the BBB and run ls inside /sys/class/gpio. Apparently, the selected GPIO’s are not there. After the aforementioned code is executed (and the same errors are thrown), I run ls again. This time, the required GPIO’s are there. Could someone explain to me why they do not initially exist, and what makes them show up afterwards? Thank you in advance.

Have you turned off GPIO options in kernel settings?

This is the TI official guide (assuming you are running Linux):

It is a race condition. The GPIO pin files don't exist until the pin is exported by writing the pin number to /sys/class/gpio/export. The pin files are created neither instantaneously nor synchronously.

Your code or library is trying to open one of the pin files after exporting but before the pin file is ready.


I use a loop to retry the write operation up to 10 times, each with a pause of 5s if the initial write fails. However, that doesn’t seem to help. The last retry is 50s after the export operation. This is a vast amount of time for the export operation to complete. Obviously, it just didn’t work. How can I fix this?

After many more tries, I figure out the error. The GPIO’s are already available on boot up. The code tries exporting them again, causing the opposite effect (unexport). This causes the No such file or directory error. Anyways, any idea why this opposite effect is there? Shouldn’t it do nothing instead? We already have unexport for that purpose.