Write a NuttX GPIO pin with the GPIOC_WRITE ioctl
Drive a NuttX output pin from an application with the GPIOC_WRITE ioctl on /dev/gpout0, and read the level back with GPIOC_READ, on NuttX 12.6.
NuttX exposes a GPIO pin as a character device once the board registers it, so an application sets an output with one ioctl(). With CONFIG_DEV_GPIO=y on NuttX 12.6, output pins appear as /dev/gpout0, /dev/gpout1 and so on, and inputs as /dev/gpin0.
#include <nuttx/config.h>
#include <nuttx/ioexpander/gpio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
bool level;
int fd = open("/dev/gpout0", O_RDWR);
if (fd < 0)
{
perror("open");
return 1;
}
ioctl(fd, GPIOC_WRITE, 1);
ioctl(fd, GPIOC_READ, (unsigned long)((uintptr_t)&level));
printf("wrote 1, read back %d\n", level);
close(fd);
return 0;
}wrote 1, read back 1The two commands take their argument differently, and that is the trap. For GPIOC_WRITE the third argument is the value itself, 0 or 1, cast to unsigned long. For GPIOC_READ it is the address of a bool, passed through the same cast. Swap them and you either write garbage or hand the kernel a number as a pointer. Reading an output pin returns the level last driven, not a sampled pad, so it will not catch a short to ground. Open the device O_RDWR, since a write-only open can reject the read. The pin type comes from the board's registration, so you cannot write a gpin device.