The "No file directory" error when using
gpioget on an Adafruit MCP2111 typically stems from the device node not being created or accessible due to missing kernel drivers, incorrect permissions, or a failure in the sysfs interface initialization. This guide explains how to verify the SPI connection, ensure the
mcp211x driver is loaded, and correctly access the GPIO lines through the standard Linux sysfs interface to resolve the missing file path issue.
Understanding the MCP2111 Interface
It is crucial to first correct a common misconception: the Adafruit MCP2111 is an SPI-to-GPIO converter, not an I2C device. Unlike many other GPIO expanders that appear as I2C devices on the bus, the MCP2111 communicates over the Serial Peripheral Interface (SPI). Therefore, troubleshooting steps related to I2C tools like
i2cdetect will be ineffective and misleading. The system must recognize the chip via the SPI subsystem before any GPIO operations can occur. If you are attempting to use I2C commands, you will naturally fail because the hardware protocol is entirely different.
The Linux kernel handles this translation through a specific driver module. When the SPI bus detects the MCP2111 during initialization, it should automatically load the
mcp211x driver. This driver is responsible for creating the character device nodes or exposing the GPIO lines through the sysfs interface. If the driver fails to load, or if the SPI device is not properly registered, the user-space tools will have no endpoint to communicate with, resulting in the "No file directory" or similar file-not-found errors.
Verifying Kernel Driver Status
The first step in resolving this issue is to confirm that the operating system has successfully recognized the hardware at the kernel level. You need to check if the
mcp211x module is currently loaded into the running kernel. Open your terminal and execute the following command:
lsmod | grep mcp211x
If this command returns no output, the driver is not loaded. In some systems, the driver might be built into the kernel but simply not activated for your specific device ID. To force the loading of the module, you can manually insert it using:
sudo modprobe mcp211x
After loading the module, check the system logs to see if the kernel detected the device. Use the
dmesg command and filter for messages related to MCP2111 or SPI:
dmesg | grep -i mcp211x
You should see a message indicating that the MCP2111 device was found and initialized. If you see errors such as "spi_new_device failed" or "no spi controller," the issue lies with the SPI bus configuration rather than the GPIO tooling itself. Ensure that SPI is enabled in your system's firmware or bootloader settings, particularly on single-board computers like the Raspberry Pi where SPI might be disabled by default in the configuration menu.
Checking Device Nodes and Permissions
Once the driver is confirmed to be active, the next layer of verification involves checking the device nodes created by the kernel. The MCP2111 driver typically creates a character device, often located in
/dev. Depending on the kernel version and distribution, this might appear as
/dev/mcp211x0 or similar. However, modern Linux systems increasingly rely on the sysfs interface for GPIO access rather than direct character device manipulation.
To check if the sysfs GPIO interface is available, list the contents of the GPIO chip directory:
ls /sys/class/gpio/
If you do not see any gpiochip entries, the sysfs interface may not be exposed, or the driver may not be exporting the GPIO lines correctly. Some distributions require you to export the GPIO pins manually before they become accessible via the standard sysfs paths. For example, if the MCP2111 exposes GPIO lines 0 through 7, you might need to write the corresponding line number to the export file:
echo 0 | sudo tee /sys/class/gpio/export
However, note that the MCP2111 driver behavior can vary. Some versions expose all GPIOs directly under a specific gpiochip entry without manual export. Check the dmesg output again for hints on how the driver intends to expose the pins. Additionally, ensure that your user account has read and write permissions to the relevant sysfs files. If you encounter permission denied errors after confirming the file exists, adjust the ownership or use
sudo for the initial testing phase.
Testing with Correct Tools
The
gpioget command is part of the
libgpiod utilities, which interact with the gpiod interface in the kernel. This is distinct from the older sysfs-based
gpio commands. If you are using
gpioget, you must ensure that the
libgpiod tools are installed and that the kernel supports the gpiod interface. Verify the installation with:
gpioinfo
This command lists all available GPIO chips. Look for an entry labeled "mcp211x" or similar. If it appears here, the driver is working correctly at the gpiod level. You can then retrieve the value of a specific pin using:
gpioget gpiochipX Y
Replace
X with the chip number shown in the
gpioinfo output and
Y with the pin number. If
gpioget still reports a missing file or directory, it may indicate that the pin is not configured as an input or output in the current state. Check the direction of the pin using
gpioinfo and ensure you are querying a valid, active line.
For users who prefer the legacy sysfs method, you can also try reading the value directly from the sysfs file if the pin has been exported:
cat /sys/class/gpio/gpioYY/value
This method is less robust but can help isolate whether the issue is with the libgpiod library or the underlying kernel driver. If sysfs works but libgpiod does not, update your libgpiod libraries. If neither works, the problem is almost certainly at the driver or SPI bus level.
Frequently Asked Questions
Why does i2cdetect not find my MCP2111?
The MCP2111 uses the SPI protocol, not I2C, so it will never appear on an I2C bus scan. Using I2C diagnostic tools will always result in a timeout or empty result because the physical wiring and communication protocol are incompatible. You must use SPI-specific diagnostics like
lsmod and
dmesg instead.
How do I enable SPI on my Raspberry Pi?
On Raspberry Pi OS, SPI is often disabled by default. You can enable it by running
sudo raspi-config, navigating to Interface Options, selecting SPI, and choosing Yes to enable it. After enabling, reboot the system to allow the kernel to initialize the SPI controller and detect connected devices like the MCP2111.
What if gpioinfo shows the chip but gpioget fails?
This usually indicates a permissions issue or a mismatch between the requested pin and the actual hardware mapping. Verify that you are using the correct chip index and pin number. Additionally, ensure that your user has the necessary group memberships (like dialout or gpio) to access the device nodes, or run the command with sudo for testing.
Can I use the MCP2111 with Windows?
The MCP2111 is primarily designed for Linux and embedded environments where kernel drivers handle the low-level communication. While Windows drivers exist, they do not provide the same seamless sysfs or libgpiod integration. For development and troubleshooting, a Linux environment is strongly recommended to leverage the native kernel support.
Does the MCP2111 require external pull-up resistors?
The MCP2111 has internal pull-up and pull-down resistors that can be configured via its registers. However, for reliable signal integrity, especially on longer wires, external pull-ups may be necessary depending on your circuit design. Consult the datasheet for specific recommendations based on your voltage levels and cable length.