From d79506694d7ba1c3da865d095238289d6175057d Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Wed, 30 Sep 2015 16:11:42 +0100 Subject: [PATCH] Fixing a few issues - makefile goofs. Also adding in the stuff for /dev/gpiomem with warnings and information in the gpio manual page (who reads that though!) --- VERSION | 2 +- debian-template/wiringPi/DEBIAN/control | 2 +- examples/Makefile | 4 +-- examples/max31855.c | 17 ++++++++++--- gpio/Makefile | 2 +- gpio/gpio.1 | 14 +++++++++- gpio/gpio.c | 3 +++ gpio/version.h | 2 +- wiringPi/wiringPi.c | 45 ++++++++++++++++++++++++++++++++- 9 files changed, 80 insertions(+), 11 deletions(-) diff --git a/VERSION b/VERSION index 410158a..072e651 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.28 +2.29 diff --git a/debian-template/wiringPi/DEBIAN/control b/debian-template/wiringPi/DEBIAN/control index 9def8c5..3b15a24 100644 --- a/debian-template/wiringPi/DEBIAN/control +++ b/debian-template/wiringPi/DEBIAN/control @@ -1,5 +1,5 @@ Package: wiringpi -Version: 2.28 +Version: 2.29 Section: libraries Priority: optional Architecture: armhf diff --git a/examples/Makefile b/examples/Makefile index 04bcaec..e6b9b71 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -45,7 +45,7 @@ SRC = blink.c blink8.c blink12.c \ lcd.c lcd-adafruit.c clock.c \ nes.c \ softPwm.c softTone.c \ - delayTest.c serialRead.c serialTest.c okLed.c ds1302.c \ + delayTest.c serialRead.c serialTestc okLed.c ds1302.c \ lowPower.c \ max31855.c \ rht03.c @@ -151,7 +151,7 @@ ds1302: ds1302.o max31855: max31855.o $Q echo [link] - $Q $(CC) -o $@ max31855.c.o $(LDFLAGS) $(LDLIBS) + $Q $(CC) -o $@ max31855.o $(LDFLAGS) $(LDLIBS) .c.o: $Q echo [CC] $< diff --git a/examples/max31855.c b/examples/max31855.c index 2060a55..36b3cf6 100644 --- a/examples/max31855.c +++ b/examples/max31855.c @@ -33,7 +33,7 @@ int main (int argc, char *argv []) { - int i ; + int i = 0 ; wiringPiSetup () ; max31855Setup (200, 0) ; @@ -41,9 +41,20 @@ int main (int argc, char *argv []) for (;;) { - printf ("%6d, %6d, %6d, %6d ::::", analogRead (200), analogRead (201), analogRead (202), analogRead (203)) ; - printf ("%6d, %6d, %6d, %6d\n", analogRead (400), analogRead (401), analogRead (402), analogRead (403)) ; + if (i == 0) + { + printf ("+------+------+------+------++------+------+------+------+\n") ; + printf ("| Raw | Err | C | F || Raw | Err | C | F |\n") ; + printf ("+------+------+------+------++------+------+------+------+\n") ; + } + + printf ("| %4d | %4d | %4d | %4d |", analogRead (200), analogRead (201), analogRead (202), analogRead (203)) ; + printf ("| %4d | %4d | %4d | %4d |\n", analogRead (400), analogRead (401), analogRead (402), analogRead (403)) ; delay (500) ; + + if (++i == 10) + i = 0 ; + } } diff --git a/gpio/Makefile b/gpio/Makefile index e998d84..7dcd090 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -37,7 +37,7 @@ INCLUDE = -I$(DESTDIR)$(PREFIX)/include CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe LDFLAGS = -L$(DESTDIR)$(PREFIX)/lib -LIBS = -lwiringPi -lwiringPiDev -lpthread -lm +LIBS = -lwiringPi -lwiringPiDev -lpthread # May not need to alter anything below this line ############################################################################### diff --git a/gpio/gpio.1 b/gpio/gpio.1 index 673ebbe..e5fe181 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -260,7 +260,7 @@ raspi-config program to load/unload the I2C device at boot time. This loads the spi drivers into the kernel and changes the permissions on the associated /dev/ entries so that the current user has access to them. It used to have the ability to change the buffer size from the -default of 4096 bytes to an arbitary value, however for some time the +default of 4096 bytes to an arbitrary value, however for some time the Pi Foundation have compiled the SPI device driver into the kernel and this has fixed the buffer size. The way to change it now is to edit the /boot/cmdline.txt file and add on spdev.bufsiz=8192 to set it to @@ -321,6 +321,18 @@ When using the \fIexport\fR, \fIedge\fR or \fIunexport\fR commands, the pin numbers are \fBalways\fR native BCM_GPIO numbers and never wiringPi pin numbers. +As of kernels 4.1.7, a user-level GPIO access mechanism is available, +however wiringPi will not use this by default - because at this time +there appears to be issues when trying to program the PWM or clock output +hardware. If you can live without PWM or GPIO clocks and you want to use +the GPIO from a non-root program, then you need to make sure that the +module \fIbcm2835_gpiomem\fR is loaded at boot time. This should happen +automatically when you enable the device tree in raspi-config. You may +also need some additional information in /etc/udev/rules.d/ to change the +mode and ownership of the /dev/gpiomem file. Finally, you need to set +the environment variable \fIWIRINGPI_GPIOMEM\fR. This will go-away +in future releases once the /dev/gpiomem interface is fully operational. + .SH "SEE ALSO" .LP diff --git a/gpio/gpio.c b/gpio/gpio.c index 5c87a7e..4eef19d 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -1223,7 +1223,10 @@ int main (int argc, char *argv []) printf (" Device tree is enabled.\n") ; if (stat ("/dev/gpiomem", &statBuf) == 0) // User level GPIO is GO + { printf (" This Raspberry Pi supports user-level GPIO access.\n") ; + printf (" -> See the man-page for more details\n") ; + } else printf (" * Root or sudo required for GPIO access.\n") ; diff --git a/gpio/version.h b/gpio/version.h index 704c10d..ac09492 100644 --- a/gpio/version.h +++ b/gpio/version.h @@ -1 +1 @@ -#define VERSION "2.28" +#define VERSION "2.29" diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 7c899ea..32e5100 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -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 @@ -253,6 +254,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 @@ -913,6 +918,9 @@ void setPadDrive (int group, int value) if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO)) { + if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now + return ; + if ((group < 0) || (group > 2)) return ; @@ -986,6 +994,9 @@ void pwmSetRange (unsigned int range) { if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO)) { + if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now + return ; + *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ; *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ; } @@ -1007,6 +1018,9 @@ void pwmSetClock (int divisor) if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO)) { + if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now + return ; + if (wiringPiDebug) printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ; @@ -1059,6 +1073,9 @@ void gpioClockSet (int pin, int freq) else if (wiringPiMode != WPI_MODE_GPIO) return ; + if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now + return ; + divi = 19200000 / freq ; divr = 19200000 % freq ; divf = (int)((double)divr * 4096.0 / 19200000.0) ; @@ -1231,11 +1248,17 @@ void pinMode (int pin, int mode) softToneCreate (origPin) ; else if (mode == PWM_TONE_OUTPUT) { + if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now + return ; + pinMode (origPin, PWM_OUTPUT) ; // Call myself to enable PWM mode pwmSetMode (PWM_MODE_MS) ; } else if (mode == PWM_OUTPUT) { + if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now + return ; + if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin return ; @@ -1250,6 +1273,9 @@ void pinMode (int pin, int mode) } else if (mode == GPIO_CLOCK) { + if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now + return ; + if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin return ; @@ -1404,6 +1430,9 @@ void pwmWrite (int pin, int value) if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin { + if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now + return ; + /**/ if (wiringPiMode == WPI_MODE_PINS) pin = pinToGpio [pin] ; else if (wiringPiMode == WPI_MODE_PHYS) @@ -1470,6 +1499,9 @@ void pwmToneWrite (int pin, int freq) { int range ; + if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now + return ; + if (freq == 0) pwmWrite (pin, 0) ; // Off else @@ -1838,8 +1870,15 @@ int wiringPiSetup (void) if (getenv (ENV_CODES) != NULL) wiringPiReturnCodes = TRUE ; + if (getenv (ENV_GPIOMEM) != NULL) + wiringPiTryGpioMem = TRUE ; + if (wiringPiDebug) + { printf ("wiringPi: wiringPiSetup called\n") ; + if (wiringPiTryGpioMem) + printf ("wiringPi: Using /dev/gpiomem\n") ; + } boardRev = piBoardRev () ; @@ -1863,8 +1902,12 @@ int wiringPiSetup (void) // See if /dev/gpiomem exists and we can open it... - if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) >= 0) + 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)) ; RASPBERRY_PI_PERI_BASE = 0 ; + } // ... otherwise fall back to the original /dev/mem which requires root level access