Skip to content

backported support for /dev/gpiomem #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 28 additions & 4 deletions wiringPi/wiringPi.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@

#define ENV_DEBUG "WIRINGPI_DEBUG"
#define ENV_CODES "WIRINGPI_CODES"
#define ENV_GPIOMEM "WIRINGPI_GPIOMEM"


// Mask for the bottom 64 pins which belong to the Raspberry Pi
Expand Down Expand Up @@ -250,6 +251,10 @@ static pthread_mutex_t pinMutex ;
int wiringPiDebug = FALSE ;
int wiringPiReturnCodes = FALSE ;

// Use /dev/gpiomem ?

int wiringPiTryGpioMem = FALSE ;

// sysFds:
// Map a file descriptor from the /sys/class/gpio/gpioX/value

Expand Down Expand Up @@ -1866,8 +1871,8 @@ int wiringPiSetup (void)
if (getenv (ENV_CODES) != NULL)
wiringPiReturnCodes = TRUE ;

if (geteuid () != 0)
(void)wiringPiFailure (WPI_FATAL, "wiringPiSetup: Must be root. (Did you forget sudo?)\n") ;
if (getenv (ENV_GPIOMEM) != NULL)
wiringPiTryGpioMem = TRUE ;

if (wiringPiDebug)
printf ("wiringPi: wiringPiSetup called\n") ;
Expand All @@ -1889,8 +1894,27 @@ int wiringPiSetup (void)

// Open the master /dev/memory device

if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
if (wiringPiTryGpioMem)
{
if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/gpiomem: %s\n", strerror (errno)) ;
BCM2708_PERI_BASE = 0;
}

// ... otherwise fall back to the original /dev/mem which requires root level access

else
{
// This check is here because people are too stupid to check for themselves or read
// error messages.

if (geteuid () != 0)
(void)wiringPiFailure (WPI_FATAL, "wiringPiSetup: Must be root. (Did you forget sudo?)\n") ;

if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
}


// GPIO:

Expand Down