Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The package identifies itself as `mandeye-multisensor`, architecture `arm64`. Re
sudoedit /etc/mandeye/device.json
```

Check the GPIO chip and line offsets on this Pi (`gpiodetect` and `gpioinfo` from the `gpiod` package). Match each LED, buzzer and button role to its wire and button pull. For an unwired role, keep its key and set its value to `null` (for example `"buzzer": null` or `"stationary": null` inside `buttons`). Enable the UARTs used by the chosen carrier in `/boot/firmware/config.txt`. Use `dtoverlay -h <overlay-name>` for that board's pin choices. Disable the serial login console on any UART used by the scanner. Reboot after changing overlays, then check the device paths in `/dev`. In particular the historical Pi 4 example specifies `/dev/S0` for GNSS. Enter the actual UART or set `gnss` to `null` if absent. [Raspberry Pi UART configuration](https://www.raspberrypi.com/documentation/computers/configuration.html#configure-uarts)
The examples name the GPIO chip by its label (`pinctrl-rp1` on Pi 5 and CM5, `pinctrl-bcm2711` on Pi 4 and CM4) because kernel updates renumber `/dev/gpiochipN`. A device path still works. Check the chip label and line offsets on this Pi (`gpiodetect` and `gpioinfo` from the `gpiod` package). Match each LED, buzzer and button role to its wire and button pull. For an unwired role, keep its key and set its value to `null` (for example `"buzzer": null` or `"stationary": null` inside `buttons`). Enable the UARTs used by the chosen carrier in `/boot/firmware/config.txt`. Use `dtoverlay -h <overlay-name>` for that board's pin choices. Disable the serial login console on any UART used by the scanner. Reboot after changing overlays, then check the device paths in `/dev`. In particular the historical Pi 4 example specifies `/dev/S0` for GNSS. Enter the actual UART or set `gnss` to `null` if absent. [Raspberry Pi UART configuration](https://www.raspberrypi.com/documentation/computers/configuration.html#configure-uarts)

**Two-head wiring example.** CaveTronic's scanner uses the Pro CM4 example and the Linux account `mandeye`. Set `gnss` to `null` and `operation.benchmark_usb` to `true`. Verify stationary/write/continuous LEDs on GPIO **20/16/21**, buzzer **24**, and stationary/continuous buttons **25/8**, both pull-down. Keep both FakePPS pairs, **3 → /dev/ttyAMA2** and **2 → /dev/ttyAMA5**. Check `gpiochip` against this unit.

Expand Down
6 changes: 4 additions & 2 deletions code/gpios.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ GpioClient::GpioClient(const DeviceConfig& device, bool sim)
if(!sim && (!device.leds.empty() || !device.buttons.empty()))
{
std::cout << "Opening GPIO chip " << device.gpiochip << std::endl;
chip.reset(gpiod_chip_open(device.gpiochip.c_str()));
// Accepts a path, a gpiochipN name or a stable label such as pinctrl-rp1.
chip.reset(gpiod_chip_open_lookup(device.gpiochip.c_str()));
if(!chip)
throw std::runtime_error(device.gpiochip + ": " + std::strerror(errno));
throw std::runtime_error(device.gpiochip + ": GPIO chip not found (" + std::strerror(errno) +
"); compare with the labels listed by gpiodetect");
}
auto lineError = [&](const std::string& role, int gpio) {
return std::runtime_error(device.gpiochip + " GPIO " + std::to_string(gpio) + " (" + role + "): " + std::strerror(errno));
Expand Down
12 changes: 11 additions & 1 deletion code/hardware_config/device_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ struct Field
return result;
}

// Kernels renumber /dev/gpiochipN between releases (the Pi 5 header has been 4, 0 and 15),
// so a stable chip label such as "pinctrl-rp1" is accepted as well as a device path.
std::string gpioChip() const
{
auto result = string();
if(result.front() != '/' && result.find_first_of(" \t\r\n/") != std::string::npos)
error("expected a /dev/gpiochipN path or a GPIO chip label such as pinctrl-rp1 (see gpiodetect)");
return result;
}

int integer() const
{
if(!value.is_number_integer() || value < 0 || value > std::numeric_limits<int>::max())
Expand Down Expand Up @@ -96,7 +106,7 @@ DeviceConfig parseDevice(nlohmann::json document, const std::string& path)
const Field root{result.document, ""};
root.object({"name", "gpiochip", "leds", "buttons", "gnss", "fake_pps", "slave_pps", "operation"});
result.name = root.member("name").string();
result.gpiochip = root.member("gpiochip").devicePath();
result.gpiochip = root.member("gpiochip").gpioChip();

std::map<int, std::string> usedPins;
auto gpio = [&](const Field& field) {
Expand Down
8 changes: 6 additions & 2 deletions extras/FakePPS/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <SerialPort.h>
#include <SerialStream.h>
#include <atomic>
#include <cerrno>
#include <chrono>
#include <cmath>
#include <cstring>
#include <gpiod.h>
#include <hardware_config/device_config.h>
#include <iostream>
Expand Down Expand Up @@ -122,10 +124,12 @@ void sendPulses(const hardware::DeviceConfig& device)
const auto& chipPath = device.gpiochip;
std::cout << "Opening GPIO chip " << chipPath << std::endl;

gpiod_chip* chip = gpiod_chip_open(chipPath.c_str());
// Accepts a path, a gpiochipN name or a stable label such as pinctrl-rp1.
gpiod_chip* chip = gpiod_chip_open_lookup(chipPath.c_str());
if(chip == nullptr)
{
std::cerr << "Error: Unable to open GPIO chip." << std::endl;
std::cerr << "Error: Unable to open GPIO chip " << chipPath << " (" << std::strerror(errno)
<< "); compare with the labels listed by gpiodetect." << std::endl;
std::abort();
}

Expand Down
2 changes: 1 addition & 1 deletion packing/examples/cm5-direct.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cm5-direct",
"gpiochip": "/dev/gpiochip4",
"gpiochip": "pinctrl-rp1",
"leds": {
"stationary": 26,
"write": 19,
Expand Down
2 changes: 1 addition & 1 deletion packing/examples/pro-cm4-bookworm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pro-cm4-bookworm",
"gpiochip": "/dev/gpiochip0",
"gpiochip": "pinctrl-bcm2711",
"leds": {
"stationary": 20,
"write": 16,
Expand Down
2 changes: 1 addition & 1 deletion packing/examples/rpi4.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rpi4",
"gpiochip": "/dev/gpiochip0",
"gpiochip": "pinctrl-bcm2711",
"leds": {
"stationary": 26,
"write": 19,
Expand Down
2 changes: 1 addition & 1 deletion packing/examples/rpi5.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rpi5",
"gpiochip": "/dev/gpiochip4",
"gpiochip": "pinctrl-rp1",
"leds": {
"stationary": 26,
"write": 19,
Expand Down
34 changes: 32 additions & 2 deletions packing/mandeye-activate
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,36 @@ def check_device(user, path):
raise ValueError(f"{path}: {user} needs read and write access") from None


GPIO_GET_CHIPINFO_IOCTL = 0x8044B401 # _IOR(0xB4, 0x01, struct gpiochip_info), 68 bytes


def gpio_chip_label(path):
with open(path, "rb", buffering=0) as chip:
info = fcntl.ioctl(chip, GPIO_GET_CHIPINFO_IOCTL, bytes(68))
return info[32:64].split(b"\0", 1)[0].decode(errors="replace")


def gpio_chip_path(chip):
"""Resolve the device.json gpiochip value the way libgpiod's gpiod_chip_open_lookup does."""
if chip.startswith("/"):
return chip
if chip.isdigit():
chip = "gpiochip" + chip
if Path("/dev", chip).is_char_device():
return str(Path("/dev", chip))
found = []
for path in sorted(Path("/dev").glob("gpiochip*")):
try:
label = gpio_chip_label(path)
except OSError:
continue
if label == chip:
print(f"GPIO chip {chip}: {path}", flush=True)
return str(path)
found.append(f"{path.name} [{label}]")
raise ValueError(f"GPIO chip {chip!r} not found. Available: {', '.join(found) or 'none'}")


def uses_controller_gpio(config):
return any(value is not None for group in ("leds", "buttons") for value in config[group].values())

Expand Down Expand Up @@ -178,12 +208,12 @@ def activate(unit, repo_override, user):
repo = repo_override if repo_override is not None else recording_directory(unit, user)
as_account(user, "/usr/lib/mandeye/mandeye-storage", repo)
if uses_controller_gpio(config):
check_device(user, config["gpiochip"])
check_device(user, gpio_chip_path(config["gpiochip"]))
if config["gnss"]:
check_device(user, config["gnss"]["uart"])
if config["fake_pps"]:
fake_user = property_of(FAKE, "User") or "root"
check_device(fake_user, config["gpiochip"])
check_device(fake_user, gpio_chip_path(config["gpiochip"]))
for output in config["fake_pps"]:
check_device(fake_user, output["uart"])
if config["slave_pps"]:
Expand Down
12 changes: 12 additions & 0 deletions tests/package/test-device-config.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ def check_optional_gpio(device, path):
config[field]["stationary"] = None


def check_gpiochip(device, path):
# A chip label survives kernel renumbering of /dev/gpiochipN; a device path still works.
cmd = [PROGRAMS[0], "--validate-config", path]
for chip in ("pinctrl-rp1", "pinctrl-bcm2711", "/dev/gpiochip0"):
config = device | {"gpiochip": chip}
path.write_text(json.dumps(config))
assert run(cmd) == config
path.write_text(json.dumps(device | {"gpiochip": "pinctrl rp1"}))
run(cmd, error="/gpiochip: expected a /dev/gpiochipN path or a GPIO chip label")


def check_slave_pps(device, path):
cmd = [SLAVE, "--validate-config", path]
path.write_text(json.dumps(device))
Expand Down Expand Up @@ -91,5 +102,6 @@ def check_slave_pps(device, path):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "device.json"
check_optional_gpio(device, path)
check_gpiochip(device, path)
check_slave_pps(device, path)
print("All five consumers rejected an invalid device before I/O; SlavePPS overrides passed.")
Loading