Skip to content

Commit

Permalink
[libsimpleio] Update for RPi5 support
Browse files Browse the repository at this point in the history
jcelerier committed Jul 2, 2024
1 parent 4ef3f88 commit 7c86f7f
Showing 13 changed files with 93 additions and 27 deletions.
2 changes: 1 addition & 1 deletion 3rdparty/libsimpleio/libsimpleio/libadc.c
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@
#include <unistd.h>
#include <sys/param.h>

#include "errmsg.inc"
#include "macros.inc"
#include "libadc.h"

#define NAME_FILE "/sys/bus/iio/devices/iio:device%d/name"
2 changes: 1 addition & 1 deletion 3rdparty/libsimpleio/libsimpleio/libdac.c
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@
#include <unistd.h>
#include <sys/param.h>

#include "errmsg.inc"
#include "macros.inc"
#include "libadc.h"

#define NAME_FILE "/sys/bus/iio/devices/iio:device%d/name"
2 changes: 1 addition & 1 deletion 3rdparty/libsimpleio/libsimpleio/libevent.c
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@
#include <string.h>
#include <unistd.h>

#include "errmsg.inc"
#include "macros.inc"
#include "libevent.h"

void EVENT_open(int32_t *epfd, int32_t *error)
48 changes: 36 additions & 12 deletions 3rdparty/libsimpleio/libsimpleio/libgpio.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* GPIO services for Linux */

// Copyright (C)2016-2023, Philip Munts dba Munts Technologies.
// Copyright (C)2016-2024, Philip Munts dba Munts Technologies.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
@@ -32,8 +32,11 @@
#include <sys/ioctl.h>
#include <sys/param.h>

#include "errmsg.inc"
#include "macros.inc"
#include "libgpio.h"
#include "liblinux.h"

#define RASPBERRYPI_GPIOCHIP "/dev/gpiochip-rpi"

void GPIO_chip_info(int32_t chip, char *name, int32_t namesize,
char *label, int32_t labelsize, int32_t *lines, int32_t *error)
@@ -86,10 +89,17 @@ void GPIO_chip_info(int32_t chip, char *name, int32_t namesize,

// Open the GPIO controller device

char nodename[32];
snprintf(nodename, sizeof(nodename), "/dev/gpiochip%d", chip);
int chipfd;

int chipfd = open(nodename, O_RDWR);
if (!access(RASPBERRYPI_GPIOCHIP, F_OK) && (chip == 0) &&
(strstr(LINUX_model_name(), "Raspberry Pi") != NULL))
chipfd = open(RASPBERRYPI_GPIOCHIP, O_RDWR);
else
{
char nodename[32];
snprintf(nodename, sizeof(nodename), "/dev/gpiochip%d", chip);
chipfd = open(nodename, O_RDWR);
}

if (chipfd < 0)
{
@@ -180,10 +190,17 @@ void GPIO_line_info(int32_t chip, int32_t line, int32_t *flags, char *name,

// Open the GPIO controller device

char nodename[32];
snprintf(nodename, sizeof(nodename), "/dev/gpiochip%d", chip);
int chipfd;

int chipfd = open(nodename, O_RDWR);
if (!access(RASPBERRYPI_GPIOCHIP, F_OK) && (chip == 0) &&
(strstr(LINUX_model_name(), "Raspberry Pi") != NULL))
chipfd = open(RASPBERRYPI_GPIOCHIP, O_RDWR);
else
{
char nodename[32];
snprintf(nodename, sizeof(nodename), "/dev/gpiochip%d", chip);
chipfd = open(nodename, O_RDWR);
}

if (chipfd < 0)
{
@@ -322,12 +339,19 @@ void GPIO_line_open(int32_t chip, int32_t line, int32_t flags, int32_t events,
return;
}

// Open GPIO controller device
// Open the GPIO controller device

char nodename[32];
snprintf(nodename, sizeof(nodename), "/dev/gpiochip%d", chip);
int chipfd;

int chipfd = open(nodename, O_RDWR);
if (!access(RASPBERRYPI_GPIOCHIP, F_OK) && (chip == 0) &&
(strstr(LINUX_model_name(), "Raspberry Pi") != NULL))
chipfd = open(RASPBERRYPI_GPIOCHIP, O_RDWR);
else
{
char nodename[32];
snprintf(nodename, sizeof(nodename), "/dev/gpiochip%d", chip);
chipfd = open(nodename, O_RDWR);
}

if (chipfd < 0)
{
2 changes: 1 addition & 1 deletion 3rdparty/libsimpleio/libsimpleio/libhidraw.c
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@
#include <sys/ioctl.h>
#include <sys/param.h>

#include "errmsg.inc"
#include "macros.inc"
#include "libhidraw.h"

// Compatibility shims
2 changes: 1 addition & 1 deletion 3rdparty/libsimpleio/libsimpleio/libi2c.c
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@
#include <sys/ioctl.h>
#include <sys/param.h>

#include "errmsg.inc"
#include "macros.inc"
#include "libi2c.h"

// Perform an I2C transaction
2 changes: 1 addition & 1 deletion 3rdparty/libsimpleio/libsimpleio/libipv4.c
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@
#include <sys/socket.h>
#include <netinet/in.h>

#include "errmsg.inc"
#include "macros.inc"
#include "libipv4.h"

// Resolve host name to IPV4 address
45 changes: 41 additions & 4 deletions 3rdparty/libsimpleio/libsimpleio/liblinux.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Linux syscall wrappers. These are primarily for the benefit of other */
/* programming languages, such as Ada, C#, Free Pascal, Go, etc. */

// Copyright (C)2016-2023, Philip Munts dba Munts Technologies.
// Copyright (C)2016-2024, Philip Munts dba Munts Technologies.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
@@ -31,7 +31,7 @@
#include <string.h>
#include <unistd.h>

#include "errmsg.inc"
#include "macros.inc"
#include "liblinux.h"

// Detach process from the controlling terminal and run it in the background
@@ -749,9 +749,46 @@ void *LINUX_indexpp(void **p, int32_t i)
return p[i];
}

// Function aliases
// Return the device tree model name from /proc/device-tree/model

#define MODELPATH "/proc/device-tree/model"

const char * const LINUX_model_name(void)
{
int fd;
ssize_t len;
static char failure[4096];
static char success[4096];

memset(failure, 0, sizeof(failure));
memset(success, 0, sizeof(success));

if (access(MODELPATH, R_OK))
return failure;

fd = open(MODELPATH, O_RDONLY);

if (fd < 0)
{
ERRORMSG("open() failed", errno, __LINE__ - 4);
return failure;
}

len = read(fd, success, sizeof(success) - 1);

if (len < 0)
{
ERRORMSG("read() failed", errno, __LINE__ - 4);
close(fd);
return failure;
}

close(fd);

#define ALIAS(orig) __attribute__((weak, alias(orig)))
return success;
}

// Function aliases

void ADC_close(int32_t fd, int32_t *error) ALIAS("LINUX_close");
void DAC_close(int32_t fd, int32_t *error) ALIAS("LINUX_close");
6 changes: 5 additions & 1 deletion 3rdparty/libsimpleio/libsimpleio/liblinux.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Linux syscall wrappers. These are primarily for the benefit of other */
/* programming languages, such as Ada, C#, Free Pascal, Go, etc. */

// Copyright (C)2016-2023, Philip Munts dba Munts Technologies.
// Copyright (C)2016-2024, Philip Munts dba Munts Technologies.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
@@ -127,6 +127,10 @@ extern void LINUX_pclose(void *stream, int32_t *error);

extern void *LINUX_indexpp(void **p, int32_t i);

// Return the device tree model name from /proc/device-tree/model

extern const char * const LINUX_model_name(void);

_END_STD_C

#endif
2 changes: 1 addition & 1 deletion 3rdparty/libsimpleio/libsimpleio/libpwm.c
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@
#include <unistd.h>
#include <sys/param.h>

#include "errmsg.inc"
#include "macros.inc"
#include "libpwm.h"

// Device nodes
2 changes: 1 addition & 1 deletion 3rdparty/libsimpleio/libsimpleio/libserial.c
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@
#include <termios.h>
#include <unistd.h>

#include "errmsg.inc"
#include "macros.inc"
#include "libserial.h"

void SERIAL_open(const char *name, int32_t baudrate, int32_t parity,
2 changes: 1 addition & 1 deletion 3rdparty/libsimpleio/libsimpleio/libspi.c
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@
#include <sys/ioctl.h>
#include <sys/param.h>

#include "errmsg.inc"
#include "macros.inc"
#include "libgpio.h"
#include "libspi.h"

3 changes: 2 additions & 1 deletion 3rdparty/libsimpleio/libsimpleio/libwatchdog.c
Original file line number Diff line number Diff line change
@@ -22,10 +22,11 @@

#include <assert.h>
#include <errno.h>
#include <stddef.h>
#include <linux/watchdog.h>
#include <sys/ioctl.h>

#include "errmsg.inc"
#include "macros.inc"
#include "libwatchdog.h"

void WATCHDOG_get_timeout(int32_t fd, int32_t *timeout, int32_t *error)

0 comments on commit 7c86f7f

Please sign in to comment.