From 93300f8ab83f49e198d451bb221c5d1d083a082f Mon Sep 17 00:00:00 2001 From: Jim-P Date: Sun, 17 Nov 2019 17:36:26 -0500 Subject: [PATCH] Code and Makefile changes to date --- examples/Makefile | 15 +- examples/blink.c | 135 +++++++++++------ examples/isr-osc.c | 82 ++++++++--- examples/isr.c | 69 +++++++-- examples/isr1.c | 149 +++++++++++++++++++ examples/okLed.c | 9 +- examples/pwm.c | 79 +++++++--- examples/pwm1.c | 93 ++++++++++++ examples/servo-reset.sh | 19 +++ examples/servo.c | 107 +++++++++++--- examples/spiSpeed.c | 15 +- examples/test1.c | 111 ++++++++++++++ examples/test2.c | 58 ++++++++ examples/tone.c | 59 ++++++++ examples/wfi.c | 5 +- gpio/Makefile | 3 +- gpio/gpio.c | 44 +++--- gpio/newVersion | 26 ++++ gpio/pintest | 0 gpio/readall.c | 21 ++- gpio/test.sh | 3 + wiringPi/README | 8 + wiringPi/ds1302.h | 44 ++++++ wiringPi/lcd.c | 380 ++++++++++++++++++++++++++++++++++++++++++++++++ wiringPi/lcd.h | 52 +++++++ wiringPi/lcd128x64.h | 39 +++++ wiringPi/maxdetect.h | 40 +++++ wiringPi/piNes.c | 113 ++++++++++++++ wiringPi/piNes.h | 45 ++++++ wiringPi/scrollPhat.h | 39 +++++ wiringPi/softServo.c | 2 +- wiringPi/wiringPi.c | 77 +++++++--- wiringPi/wiringPi.h | 15 +- wiringPi/wiringPiFace.c | 362 +++++++++++++++++++++++++++++++++++++++++++++ wiringPi/wiringPiSPI.c | 15 +- 35 files changed, 2138 insertions(+), 195 deletions(-) mode change 100644 => 100755 examples/Makefile create mode 100644 examples/isr1.c mode change 100644 => 100755 examples/pwm.c create mode 100644 examples/pwm1.c create mode 100755 examples/servo-reset.sh mode change 100644 => 100755 examples/servo.c create mode 100644 examples/test1.c create mode 100644 examples/test2.c create mode 100644 examples/tone.c mode change 100644 => 100755 gpio/Makefile mode change 100644 => 100755 gpio/gpio.c create mode 100755 gpio/newVersion mode change 100644 => 100755 gpio/pintest mode change 100644 => 100755 gpio/readall.c create mode 100755 wiringPi/README create mode 100755 wiringPi/ds1302.h create mode 100755 wiringPi/lcd.c create mode 100755 wiringPi/lcd.h create mode 100755 wiringPi/lcd128x64.h create mode 100755 wiringPi/maxdetect.h create mode 100755 wiringPi/piNes.c create mode 100755 wiringPi/piNes.h create mode 100755 wiringPi/scrollPhat.h mode change 100644 => 100755 wiringPi/softServo.c mode change 100644 => 100755 wiringPi/wiringPi.c mode change 100644 => 100755 wiringPi/wiringPi.h create mode 100755 wiringPi/wiringPiFace.c mode change 100644 => 100755 wiringPi/wiringPiSPI.c diff --git a/examples/Makefile b/examples/Makefile old mode 100644 new mode 100755 index 6d87885..5509dc6 --- a/examples/Makefile +++ b/examples/Makefile @@ -48,7 +48,9 @@ SRC = blink.c blink8.c blink12.c \ delayTest.c serialRead.c serialTest.c okLed.c ds1302.c \ lowPower.c \ max31855.c \ - rht03.c + rht03.c \ + servo.c \ + isr1.c test1.c test2.c OBJ = $(SRC:.c=.o) @@ -145,6 +147,14 @@ tone: tone.o $Q echo [link] $Q $(CC) -o $@ tone.o $(LDFLAGS) $(LDLIBS) +servo: servo.o softServo.o + $Q echo [link] + $Q $(CC) -o $@ servo.o softServo.o $(LDFLAGS) $(LDLIBS) + +softServo.o: ../wiringPi/softServo.c ../wiringPi/softServo.h + $Q echo [softServo] + $Q $(CC) -c $(CFLAGS) $< -o softServo.o + ds1302: ds1302.o $Q echo [link] $Q $(CC) -o $@ ds1302.o $(LDFLAGS) $(LDLIBS) @@ -161,6 +171,9 @@ clean: $Q echo "[Clean]" $Q rm -f $(OBJ) *~ core tags $(BINS) +echo: + $Q echo "[BINS] $(BINS)" + tags: $(SRC) $Q echo [ctags] $Q ctags $(SRC) diff --git a/examples/blink.c b/examples/blink.c index c27a20e..4dc733f 100644 --- a/examples/blink.c +++ b/examples/blink.c @@ -1,48 +1,87 @@ -/* - * blink.c: - * Standard "blink" program in wiringPi. Blinks an LED connected - * to the first GPIO pin. - * - * Copyright (c) 2012-2013 Gordon Henderson. - *********************************************************************** - * This file is part of wiringPi: - * https://projects.drogon.net/raspberry-pi/wiringpi/ - * - * wiringPi is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * wiringPi is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with wiringPi. If not, see . - *********************************************************************** - */ - -#include -#include - -// LED Pin - wiringPi pin 0 is BCM_GPIO 17. - -#define LED 0 - -int main (void) -{ - printf ("Raspberry Pi blink\n") ; - - wiringPiSetup () ; - pinMode (LED, OUTPUT) ; - - for (;;) - { - digitalWrite (LED, HIGH) ; // On - delay (500) ; // mS - digitalWrite (LED, LOW) ; // Off - delay (500) ; - } - return 0 ; -} +/* + * blink.c: + * Standard "blink" program in wiringPi. Blinks an LED connected + * to the first GPIO pin. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include + +// LED Pin - wiringPi +// pin 0 is BCM_GPIO 17 +// pin 1 is BCM_GPIO 18 +// pin 2 is BCM_GPIO 27 +#define LED 2 + +static int terminate_process = 0; + +static void Signal_handler(int sig); + +int main (void) +{ + printf ("Raspberry Pi blink\n") ; + + if (wiringPiSetup () == -1) + return 1 ; + + pinMode (LED, OUTPUT) ; + + // Set the handler for SIGTERM (15) + signal(SIGTERM, Signal_handler); + signal(SIGHUP, Signal_handler); + signal(SIGINT, Signal_handler); + signal(SIGQUIT, Signal_handler); + signal(SIGTRAP, Signal_handler); + signal(SIGABRT, Signal_handler); + signal(SIGALRM, Signal_handler); + signal(SIGUSR1, Signal_handler); + signal(SIGUSR2, Signal_handler); + + while (!terminate_process) + { + digitalWrite (LED, HIGH) ; // On + delay (500) ; // mS + digitalWrite (LED, LOW) ; // Off + delay (500) ; + } + + digitalWrite (LED, LOW) ; // Off + + return 0 ; +} + +//********************************************************************************************************************** + +/** + * Intercepts and handles signals from QNX + * This function is called when the SIGTERM signal is raised by QNX + */ +void Signal_handler(int sig) +{ + printf("Received signal %d\n", sig); + + // Signal process to exit. + terminate_process = 1; +} + +//********************************************************************************************************************** diff --git a/examples/isr-osc.c b/examples/isr-osc.c index a872ee3..21995cc 100644 --- a/examples/isr-osc.c +++ b/examples/isr-osc.c @@ -48,19 +48,25 @@ #include #include #include +#include // What GPIO input are we using? // This is a wiringPi pin number -#define OUT_PIN 0 -#define IN_PIN 1 +#define OUT_PIN 2 +#define IN_PIN 3 // globalCounter: // Global variable to count interrupts // Should be declared volatile to make sure the compiler doesn't cache it. +static volatile int globalCounter = 0; -static volatile int globalCounter = 0 ; +static int terminate_process = 0; + +//********************************************************************************************************************** + +static void Signal_handler(int sig); /* * myInterrupt: @@ -69,9 +75,8 @@ static volatile int globalCounter = 0 ; void myInterrupt (void) { - digitalWrite (OUT_PIN, 1) ; - ++globalCounter ; - digitalWrite (OUT_PIN, 0) ; + ++globalCounter; + printf (" %d\n", globalCounter); } @@ -83,36 +88,67 @@ void myInterrupt (void) int main (void) { - int myCounter = 0 ; - int lastCounter = 0 ; + int myCounter = 0; if (wiringPiSetup () < 0) { - fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ; - return 1 ; + fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)); + return 1; } - pinMode (OUT_PIN, OUTPUT) ; - pinMode (IN_PIN, INPUT) ; + pinMode (OUT_PIN, OUTPUT); + pinMode (IN_PIN, INPUT); if (wiringPiISR (IN_PIN, INT_EDGE_FALLING, &myInterrupt) < 0) { - fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ; - return 1 ; + fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)); + return 1; } - for (;;) + // Set the handler for SIGTERM (15) + signal(SIGTERM, Signal_handler); + signal(SIGHUP, Signal_handler); + signal(SIGINT, Signal_handler); + signal(SIGQUIT, Signal_handler); + signal(SIGTRAP, Signal_handler); + signal(SIGABRT, Signal_handler); + signal(SIGALRM, Signal_handler); + signal(SIGUSR1, Signal_handler); + signal(SIGUSR2, Signal_handler); + + while (!terminate_process) { - printf ("Waiting ... ") ; fflush (stdout) ; + printf ("Waiting ...\n"); + fflush (stdout); + + while (!terminate_process && (myCounter == globalCounter)) + { + delay (250); + } - while (myCounter == globalCounter) - delay (1000) ; + printf ("Done. counter: %2d: %2d\n", globalCounter, globalCounter - myCounter); - printf (" Done. counter: %6d: %6d\n", - globalCounter, myCounter - lastCounter) ; - lastCounter = myCounter ; - myCounter = globalCounter ; + digitalWrite (OUT_PIN, 1); + delay (500); + digitalWrite (OUT_PIN, 0); + + myCounter = globalCounter; } - return 0 ; + return 0; +} + +//********************************************************************************************************************** + +/** + * Intercepts and handles signals + * This function is called when the SIGTERM signal is raised + */ +void Signal_handler(int sig) { + printf("Received signal %d\n", sig); + + // Signal process to exit. + terminate_process = 1; } + +//********************************************************************************************************************** diff --git a/examples/isr.c b/examples/isr.c index abc6aec..3e2c178 100644 --- a/examples/isr.c +++ b/examples/isr.c @@ -36,14 +36,22 @@ #include #include #include +#include +//********************************************************************************************************************** // globalCounter: // Global variable to count interrupts // Should be declared volatile to make sure the compiler doesn't cache it. - static volatile int globalCounter [8] ; +static int terminate_process = 0; + +//********************************************************************************************************************** + +static void Signal_handler(int sig); + +//********************************************************************************************************************** /* * myInterrupt: @@ -71,10 +79,15 @@ int main (void) int gotOne, pin ; int myCounter [8] ; - for (pin = 0 ; pin < 8 ; ++pin) + for (pin = 0 ; pin < 8 ; ++pin) { globalCounter [pin] = myCounter [pin] = 0 ; + } + + if (wiringPiSetup() < 0) { + fprintf(stderr, "Unable to setup wiringPi: %s\n", strerror(errno)); + return 1; + } - wiringPiSetup () ; wiringPiISR (0, INT_EDGE_FALLING, &myInterrupt0) ; wiringPiISR (1, INT_EDGE_FALLING, &myInterrupt1) ; @@ -85,26 +98,52 @@ int main (void) wiringPiISR (6, INT_EDGE_FALLING, &myInterrupt6) ; wiringPiISR (7, INT_EDGE_FALLING, &myInterrupt7) ; - for (;;) - { + // Set the handler for SIGTERM (15) + signal(SIGTERM, Signal_handler); + signal(SIGHUP, Signal_handler); + signal(SIGINT, Signal_handler); + signal(SIGQUIT, Signal_handler); + signal(SIGTRAP, Signal_handler); + signal(SIGABRT, Signal_handler); + signal(SIGALRM, Signal_handler); + signal(SIGUSR1, Signal_handler); + signal(SIGUSR2, Signal_handler); + + while (!terminate_process) { gotOne = 0 ; - printf ("Waiting ... ") ; fflush (stdout) ; + printf("Waiting ... "); + fflush(stdout); - for (;;) + while (!terminate_process) { for (pin = 0 ; pin < 8 ; ++pin) { - if (globalCounter [pin] != myCounter [pin]) - { - printf (" Int on pin %d: Counter: %5d\n", pin, globalCounter [pin]) ; - myCounter [pin] = globalCounter [pin] ; - ++gotOne ; - } + if (globalCounter [pin] != myCounter [pin]) + { + printf (" Int on pin %d: Counter: %5d\n", pin, globalCounter [pin]) ; + myCounter [pin] = globalCounter [pin] ; + ++gotOne ; + } } if (gotOne != 0) - break ; + break ; } } - return 0 ; + return 0; } + +//********************************************************************************************************************** + +/** + * Intercepts and handles signals from QNX + * This function is called when the SIGTERM signal is raised by QNX + */ +void Signal_handler(int sig) { + printf("Received signal %d\n", sig); + + // Signal process to exit. + terminate_process = 1; +} + +//********************************************************************************************************************** diff --git a/examples/isr1.c b/examples/isr1.c new file mode 100644 index 0000000..25e94e5 --- /dev/null +++ b/examples/isr1.c @@ -0,0 +1,149 @@ +/* + * isr.c: + * Wait for Interrupt test program - ISR method + * + * How to test: + * Use the SoC's pull-up and pull down resistors that are avalable + * on input pins. So compile & run this program (via sudo), then + * in another terminal: + * gpio mode 0 up + * gpio mode 0 down + * at which point it should trigger an interrupt. Toggle the pin + * up/down to generate more interrupts to test. + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include +#include +#include +#include +#include + +//********************************************************************************************************************** + +// What GPIO input are we using? +// This is a wiringPi pin number +#define BUTTON_PIN 3 + +// LED Pin - wiringPi pin 0 is BCM_GPIO 17. +#define LED 2 + +//********************************************************************************************************************** + +// Interrupt state +static volatile int isr_state = 0; + +static int terminate_process = 0; + +static pthread_mutex_t smb_mutex = PTHREAD_MUTEX_INITIALIZER; + +//********************************************************************************************************************** + +static void Signal_handler(int sig); + +//********************************************************************************************************************** + +/* + * myInterrupt: + ********************************************************************************* + */ + +void myInterrupt(void) { + // Only report button release on actual button release. + // This interrupt sometimes fires for the wrong edge! + + if (!isr_state) { + isr_state = 1; + pthread_mutex_unlock( &smb_mutex ); + } +} + +/* + ********************************************************************************* + * main + ********************************************************************************* + */ + +int main(void) { + int button_state = 0; + int led_state = 0; + + if (wiringPiSetup() < 0) { + fprintf(stderr, "Unable to setup wiringPi: %s\n", strerror(errno)); + return 1; + } + + pinMode(LED, OUTPUT); + + if (wiringPiISR(BUTTON_PIN, INT_EDGE_BOTH, &myInterrupt) < 0) { + fprintf(stderr, "Unable to setup ISR: %s\n", strerror(errno)); + return 1; + } + + // Set the handler for SIGTERM (15) + signal(SIGTERM, Signal_handler); + signal(SIGHUP, Signal_handler); + signal(SIGINT, Signal_handler); + signal(SIGQUIT, Signal_handler); + signal(SIGTRAP, Signal_handler); + signal(SIGABRT, Signal_handler); + signal(SIGALRM, Signal_handler); + signal(SIGUSR1, Signal_handler); + signal(SIGUSR2, Signal_handler); + + pthread_mutex_lock( &smb_mutex ); + while (!terminate_process) { + printf("Waiting ... "); + fflush(stdout); + + pthread_mutex_lock( &smb_mutex ); + delay(50); + isr_state = 0; + + button_state = digitalRead(BUTTON_PIN); + printf(" Button state: %d\n", button_state); + if (button_state == 1) { + digitalWrite (LED, led_state); + led_state = !led_state; + } + } + + return 0; +} + +//********************************************************************************************************************** + +/** + * Intercepts and handles signals from QNX + * This function is called when the SIGTERM signal is raised by QNX + */ +void Signal_handler(int sig) { + printf("Received signal %d\n", sig); + + // Signal process to exit. + terminate_process = 1; + + pthread_mutex_unlock( &smb_mutex ); +} + +//********************************************************************************************************************** diff --git a/examples/okLed.c b/examples/okLed.c index 930f266..9a17cce 100644 --- a/examples/okLed.c +++ b/examples/okLed.c @@ -47,10 +47,13 @@ int main () { int fd, i ; - wiringPiSetupGpio () ; - -// Change the trigger on the OK/Act LED to "none" + if (wiringPiSetupGpio() < 0) + { + fprintf (stderr, "Unable to setup GPIO: %s\n", strerror (errno)) ; + return 1 ; + } + // Change the trigger on the OK/Act LED to "none" if ((fd = open ("/sys/class/leds/led0/trigger", O_RDWR)) < 0) { fprintf (stderr, "Unable to change LED trigger: %s\n", strerror (errno)) ; diff --git a/examples/pwm.c b/examples/pwm.c old mode 100644 new mode 100755 index 816c832..6843ee3 --- a/examples/pwm.c +++ b/examples/pwm.c @@ -27,32 +27,77 @@ #include #include #include +#include +#include -int main (void) -{ - int bright ; +//********************************************************************************************************************** + +static int terminate_process = 0; - printf ("Raspberry Pi wiringPi PWM test program\n") ; +static void Signal_handler(int sig); - if (wiringPiSetup () == -1) - exit (1) ; +//********************************************************************************************************************** + +int main(void) +{ + int bright; - pinMode (1, PWM_OUTPUT) ; + printf("Raspberry Pi wiringPi PWM test program\n"); - for (;;) - { - for (bright = 0 ; bright < 1024 ; ++bright) + if (wiringPiSetup() == -1) { - pwmWrite (1, bright) ; - delay (1) ; + printf("ERROR: wiringPi setup failed\n"); + return EXIT_FAILURE; } - for (bright = 1023 ; bright >= 0 ; --bright) + // Set the handler for SIGTERM (15) + signal(SIGTERM, Signal_handler); + signal(SIGHUP, Signal_handler); + signal(SIGINT, Signal_handler); + signal(SIGQUIT, Signal_handler); + signal(SIGTRAP, Signal_handler); + signal(SIGABRT, Signal_handler); + signal(SIGALRM, Signal_handler); + signal(SIGUSR1, Signal_handler); + signal(SIGUSR2, Signal_handler); + + pinMode(1, PWM_OUTPUT); + + printf("Going into +/- loop...\n"); + while (!terminate_process) { - pwmWrite (1, bright) ; - delay (1) ; + for (bright = 0; !terminate_process && bright < 1024; ++bright) + { + printf("+"); fflush(stdout); + pwmWrite(1, bright); + //delay (1) ; + usleep(5 * 1000); + } + + for (bright = 1023; !terminate_process && bright >= 0; --bright) + { + printf("-"); fflush(stdout); + pwmWrite(1, bright); + //delay (1) ; + usleep(5 * 1000); + } } - } - return 0 ; + return 0; } + +//********************************************************************************************************************** + +/** + * Intercepts and handles signals + * This function is called when the SIGTERM signal is raised + */ +void Signal_handler(int sig) +{ + printf("Received signal %d\n", sig); + + // Signal process to exit. + terminate_process = 1; +} + +//********************************************************************************************************************** diff --git a/examples/pwm1.c b/examples/pwm1.c new file mode 100644 index 0000000..c1fc331 --- /dev/null +++ b/examples/pwm1.c @@ -0,0 +1,93 @@ +/* + * pwm.c: + * Test of the software PWM driver. Needs 12 LEDs connected + * to the Pi. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include + +#include +#include + +#define RANGE 100 +#define NUM_LEDS 12 + +int ledMap [NUM_LEDS] = { 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 } ; + +int values [NUM_LEDS] = { 0, 17, 32, 50, 67, 85, 100, 85, 67, 50, 32, 17 } ; + +int main () +{ + int i, j ; + char buf [80] ; + + if (wiringPiSetup () == -1) + { + fprintf (stdout, "oops: %s\n", strerror (errno)) ; + return 1 ; + } + + for (i = 0 ; i < NUM_LEDS ; ++i) + { + softPwmCreate (ledMap [i], 0, RANGE) ; + printf ("%3d, %3d, %3d\n", i, ledMap [i], values [i]) ; + } + + fgets (buf, 80, stdin) ; + +// Bring all up one by one: + + for (i = 0 ; i < NUM_LEDS ; ++i) + for (j = 0 ; j <= 100 ; ++j) + { + softPwmWrite (ledMap [i], j) ; + delay (10) ; + } + + fgets (buf, 80, stdin) ; + +// Down fast + + for (i = 100 ; i > 0 ; --i) + { + for (j = 0 ; j < NUM_LEDS ; ++j) + softPwmWrite (ledMap [j], i) ; + delay (10) ; + } + + fgets (buf, 80, stdin) ; + + for (;;) + { + for (i = 0 ; i < NUM_LEDS ; ++i) + softPwmWrite (ledMap [i], values [i]) ; + + delay (50) ; + + i = values [0] ; + for (j = 0 ; j < NUM_LEDS - 1 ; ++j) + values [j] = values [j + 1] ; + values [NUM_LEDS - 1] = i ; + } +} diff --git a/examples/servo-reset.sh b/examples/servo-reset.sh new file mode 100755 index 0000000..515609a --- /dev/null +++ b/examples/servo-reset.sh @@ -0,0 +1,19 @@ +gpio mode 1 pwm +gpio pwm-ms +gpio pwmc 192 +gpio pwmr 2000 +sleep 1 + +# 100% +gpio pwm 1 250 +sleep 1 + +# 0% +gpio pwm 1 50 +sleep 1 + +gpio mode 1 pwm +gpio pwm-ms +gpio pwmc 192 +gpio pwmr 2000 + diff --git a/examples/servo.c b/examples/servo.c old mode 100644 new mode 100755 index aa1ab05..b759572 --- a/examples/servo.c +++ b/examples/servo.c @@ -24,34 +24,101 @@ */ #include +#include #include #include +#include #include #include -int main () +// GPIO 1 (physical pin 12) +static const int cPin = 1; + +int main() { - if (wiringPiSetup () == -1) - { - fprintf (stdout, "oops: %s\n", strerror (errno)) ; - return 1 ; - } + if (wiringPiSetup() == -1) + { + fprintf(stdout, "oops: %s\n", strerror(errno)); + return 1; + } - softServoSetup (0, 1, 2, 3, 4, 5, 6, 7) ; + softServoSetup(cPin, -1, -1, -1, -1, -1, -1, -1); - softServoWrite (0, 0) ; -/* - softServoWrite (1, 1000) ; - softServoWrite (2, 1100) ; - softServoWrite (3, 1200) ; - softServoWrite (4, 1300) ; - softServoWrite (5, 1400) ; - softServoWrite (6, 1500) ; - softServoWrite (7, 2200) ; -*/ - - for (;;) - delay (10) ; +// softServoWrite(1, -250); +// delay(1000); +// softServoWrite(1, 0); +// delay(1000); +// softServoWrite(1, 50); +// delay(1000); +// softServoWrite(1, 150); +// delay(1000); +// softServoWrite(1, 200); +// delay(1000); +// softServoWrite(1, 250); +// delay(1000); +// softServoWrite(1, 1250); +// delay(1000); + /* + softServoWrite (1, 1000) ; delay(1000); + softServoWrite (2, 1100) ; delay(1000); + softServoWrite (3, 1200) ; delay(1000); + softServoWrite (4, 1300) ; delay(1000); + softServoWrite (5, 1400) ; delay(1000); + softServoWrite (6, 1500) ; delay(1000); + softServoWrite (7, 2200) ; delay(1000); + */ + + int i = 1; + int step = 0; + const int cMin = -250; + const int cMax = 1250; + const int cStep = 100; + + printf("\n"); + printf("Home, Max, Home...\n"); + softServoWrite(cPin, cMax); + softServoWrite(cPin, cMin); + softServoWrite(cPin, cMax); + delay(1000); + + printf("Stepping %d steps...\n", (cMax - cMin)/cStep); + for (step = cMin; step < cMax; step += cStep, ++i) + { + printf("%d...", i); fflush(stdout); + softServoWrite(cPin, step); + delay(300); + } + printf("\n"); + printf("Returning to home position.\n"); + softServoWrite(cPin, cMin); + delay(1000); + + const int cStep1 = 50; + const int cRange = (cMax - cMin) / cStep1; + time_t t; + /* Intializes random number generator */ + srand((unsigned) time(&t)); + const int cPositions = 20; + int pos = cMin; + int lastPos = pos; + printf("Setting %d random positions...\n", cPositions); + for (i = 1; i <= cPositions; ++i) + { + while (pos == lastPos) + { + pos = ((rand() % cRange) * cStep1) + cMin; + } + printf("%d...", i); fflush(stdout); + softServoWrite(cPin, pos); + lastPos = pos; + delay(200); + } + + printf("\n"); + printf("Returning to home position.\n"); + softServoWrite(cPin, cMin); + delay(1000); + return 0; } diff --git a/examples/spiSpeed.c b/examples/spiSpeed.c index 0208f0a..447b387 100644 --- a/examples/spiSpeed.c +++ b/examples/spiSpeed.c @@ -74,6 +74,7 @@ int main (void) for (speed = 1 ; speed <= 32 ; speed *= 2) { + printf ("SPEED: %d\n", speed) ; printf ("+-------+--------+----------+----------+-----------+------------+\n") ; printf ("| MHz | Size | mS/Trans | TpS | Mb/Sec | Latency mS |\n") ; printf ("+-------+--------+----------+----------+-----------+------------+\n") ; @@ -86,16 +87,16 @@ int main (void) start = millis () ; for (times = 0 ; times < NUM_TIMES ; ++times) - if (wiringPiSPIDataRW (SPI_CHAN, myData, size) == -1) - { - printf ("SPI failure: %s\n", strerror (errno)) ; - spiFail = TRUE ; - break ; - } + if (wiringPiSPIDataRW (SPI_CHAN, myData, size) == -1) + { + printf ("SPI failure: %s\n", strerror (errno)) ; + spiFail = TRUE ; + break ; + } end = millis () ; if (spiFail) - break ; + break ; timePerTransaction = ((double)(end - start) / (double)NUM_TIMES) / 1000.0 ; dataSpeed = (double)(size * 8) / (1024.0 * 1024.0) / timePerTransaction ; diff --git a/examples/test1.c b/examples/test1.c new file mode 100644 index 0000000..4c75711 --- /dev/null +++ b/examples/test1.c @@ -0,0 +1,111 @@ +/* + * test1.c: + * Simple test program to test the wiringPi functions + * This is a sequencer to make a patter appear on 8 LEDs + * connected to the GPIO pins. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include + +#include +#include +#include + + +// Simple sequencer data +// Triplets of LED, On/Off and delay + +uint8_t data [] = +{ + 0, 1, 1, + 1, 1, 1, + 0, 0, 0, 2, 1, 1, + 1, 0, 0, 3, 1, 1, + 2, 0, 0, 4, 1, 1, + 3, 0, 0, 5, 1, 1, + 4, 0, 0, 6, 1, 1, + 5, 0, 0, 7, 1, 1, + 6, 0, 1, + 7, 0, 1, + + 0, 0, 1, // Extra delay + +// Back again + + 7, 1, 1, + 6, 1, 1, + 7, 0, 0, 5, 1, 1, + 6, 0, 0, 4, 1, 1, + 5, 0, 0, 3, 1, 1, + 4, 0, 0, 2, 1, 1, + 3, 0, 0, 1, 1, 1, + 2, 0, 0, 0, 1, 1, + 1, 0, 1, + 0, 0, 1, + + 0, 0, 1, // Extra delay + + 9, 9, 9, // End marker + +} ; + + +int main (void) +{ + int pin ; + int dataPtr ; + int l, s, d ; + + printf ("Raspberry Pi wiringPi test program\n") ; + + if (wiringPiSetup () == -1) + exit (1) ; + + for (pin = 0 ; pin < 8 ; ++pin) + pinMode (pin, OUTPUT) ; + + pinMode (8, INPUT) ; // Pin 8 SDA0 - Has on-board 2k2 pull-up resistor + + dataPtr = 0 ; + + for (;;) + { + l = data [dataPtr++] ; // LED + s = data [dataPtr++] ; // State + d = data [dataPtr++] ; // Duration (10ths) + + if ((l + s + d) == 27) + { + dataPtr = 0 ; + continue ; + } + + digitalWrite (l, s) ; + + if (digitalRead (8) == 0) // Pressed as our switch shorts to ground + delay (d * 10) ; // Faster! + else + delay (d * 100) ; + } + + return 0 ; +} diff --git a/examples/test2.c b/examples/test2.c new file mode 100644 index 0000000..580591e --- /dev/null +++ b/examples/test2.c @@ -0,0 +1,58 @@ +/* + * test2.c: + * This tests the hardware PWM channel. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include + +#include +#include +#include + +int main (void) +{ + int bright ; + + printf ("Raspberry Pi wiringPi PWM test program\n") ; + + if (wiringPiSetup () == -1) + exit (1) ; + + pinMode (1, PWM_OUTPUT) ; + + for (;;) + { + for (bright = 0 ; bright < 1024 ; ++bright) + { + pwmWrite (1, bright) ; + delay (1) ; + } + + for (bright = 1023 ; bright >= 0 ; --bright) + { + pwmWrite (1, bright) ; + delay (1) ; + } + } + + return 0 ; +} diff --git a/examples/tone.c b/examples/tone.c new file mode 100644 index 0000000..0e8a47d --- /dev/null +++ b/examples/tone.c @@ -0,0 +1,59 @@ +/* + * tone.c: + * Test of the softTone module in wiringPi + * Plays a scale out on pin 3 - connect pizeo disc to pin 3 & 0v + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include + +#include +#include + +#define PIN 3 + +int scale [8] = { 262, 294, 330, 349, 392, 440, 494, 525 } ; + +int main () +{ + int i ; + + if (wiringPiSetup () == -1) + { + fprintf (stdout, "oops: %s\n", strerror (errno)) ; + return 1 ; + } + + softToneCreate (PIN) ; + + for (;;) + { + for (i = 0 ; i < 8 ; ++i) + { + printf ("%3d\n", i) ; + softToneWrite (PIN, scale [i]) ; + delay (500) ; + } + } + +} diff --git a/examples/wfi.c b/examples/wfi.c index 6bb6892..3216dcd 100644 --- a/examples/wfi.c +++ b/examples/wfi.c @@ -101,6 +101,8 @@ PI_THREAD (waitForIt) debounceTime = millis () + DEBOUNCE_TIME ; } } + + return (void *) NULL ; } @@ -143,7 +145,8 @@ int main (void) for (;;) { - printf ("Waiting ... ") ; fflush (stdout) ; + printf ("Waiting ... "); + fflush (stdout) ; while (myCounter == lastCounter) { diff --git a/gpio/Makefile b/gpio/Makefile old mode 100644 new mode 100755 index 9ec160d..8bff555 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -33,7 +33,8 @@ endif #DEBUG = -g -O0 DEBUG = -O2 CC = gcc -INCLUDE = -I$(DESTDIR)$(PREFIX)/include +#INCLUDE = -I$(DESTDIR)$(PREFIX)/include +INCLUDE = -I../wiringPi CFLAGS = $(DEBUG) -Wall -Wextra $(INCLUDE) -Winline -pipe LDFLAGS = -L$(DESTDIR)$(PREFIX)/lib diff --git a/gpio/gpio.c b/gpio/gpio.c old mode 100644 new mode 100755 index 714e790..99a5c77 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -62,31 +62,29 @@ extern void doQmode (int argc, char *argv []) ; int wpMode ; -char *usage = "Usage: gpio -v\n" - " gpio -h\n" - " gpio [-g|-1] ...\n" - " gpio [-d] ...\n" +char *usage = "Usage: gpio -v Show Version\n" + " gpio -h Show Help\n" + " gpio -V Show gpio Layout version\n" + " gpio [-g|-1|-p] ... Use bcm-gpio | physical | piFace pin numbering scheme...\n" " [-x extension:params] [[ -x ...]] ...\n" - " gpio [-p] ...\n" " gpio ...\n" " gpio \n" - " gpio readall\n" - " gpio unexportall/exports\n" - " gpio export/edge/unexport ...\n" - " gpio wfi \n" - " gpio drive \n" - " gpio pwm-bal/pwm-ms \n" - " gpio pwmr \n" - " gpio pwmc \n" - " gpio load spi/i2c\n" - " gpio unload spi/i2c\n" - " gpio i2cd/i2cdetect\n" - " gpio rbx/rbd\n" - " gpio wb \n" - " gpio usbp high/low\n" - " gpio gbr \n" - " gpio gbw " ; // No trailing newline needed here. - + " gpio readall\n" + " gpio unexportall/exports\n" + " gpio export/edge/unexport ...\n" + " gpio wfi \n" + " gpio drive \n" + " gpio pwm-bal/pwm-ms \n" + " gpio pwmr \n" + " gpio pwmc \n" + " gpio load spi/i2c\n" + " gpio unload spi/i2c\n" + " gpio i2cd/i2cdetect\n" + " gpio rbx/rbd\n" + " gpio wb \n" + " gpio usbp high/low\n" + " gpio gbr \n" + " gpio gbw " ; // No trailing newline needed here. #ifdef NOT_FOR_NOW /* @@ -124,6 +122,8 @@ static const char *searchPath [] = "/usr/sbin", "/bin", "/usr/bin", + "/usr/local/bin", + "/usr/local/sbin", NULL, } ; diff --git a/gpio/newVersion b/gpio/newVersion new file mode 100755 index 0000000..b8728a5 --- /dev/null +++ b/gpio/newVersion @@ -0,0 +1,26 @@ +#!/bin/sh +# +# newVersion: +# Utility to create the version.h include file for the gpio command. +# +# Copyright (c) 2012-2015 Gordon Henderson +################################################################################# +# This file is part of wiringPi: +# Wiring Compatable library for the Raspberry Pi +# +# wiringPi is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# wiringPi is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with wiringPi. If not, see . +################################################################################# + +rm -f version.h +echo "#define VERSION \"`cat ../VERSION`\"" > version.h diff --git a/gpio/pintest b/gpio/pintest old mode 100644 new mode 100755 diff --git a/gpio/readall.c b/gpio/readall.c old mode 100644 new mode 100755 index 9396c6d..d49ead6 --- a/gpio/readall.c +++ b/gpio/readall.c @@ -303,10 +303,14 @@ static void plus2header (int model) printf (" +-----+-----+---------+------+---+-Pi ZeroW-+---+------+---------+-----+-----+\n") ; else if (model == PI_MODEL_2) printf (" +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+\n") ; - else if (model == PI_MODEL_3) - printf (" +-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+\n") ; - else if (model == PI_MODEL_3P) - printf (" +-----+-----+---------+------+---+---Pi 3+--+---+------+---------+-----+-----+\n") ; + else if (model == PI_MODEL_3B) + printf (" +-----+-----+---------+------+---+---Pi 3B--+---+------+---------+-----+-----+\n") ; + else if (model == PI_MODEL_3BP) + printf (" +-----+-----+---------+------+---+---Pi 3B+-+---+------+---------+-----+-----+\n") ; + else if (model == PI_MODEL_3AP) + printf (" +-----+-----+---------+------+---+---Pi 3A--+---+------+---------+-----+-----+\n") ; + else if (model == PI_MODEL_4B) + printf (" +-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+\n") ; else printf (" +-----+-----+---------+------+---+---Pi ?---+---+------+---------+-----+-----+\n") ; } @@ -351,11 +355,12 @@ void doReadall (void) /**/ if ((model == PI_MODEL_A) || (model == PI_MODEL_B)) abReadall (model, rev) ; else if ((model == PI_MODEL_BP) || (model == PI_MODEL_AP) || - (model == PI_MODEL_2) || - (model == PI_MODEL_3) || (model == PI_MODEL_3P) || - (model == PI_MODEL_ZERO) || (model == PI_MODEL_ZERO_W)) + (model == PI_MODEL_2) || + (model == PI_MODEL_3AP) || (model == PI_MODEL_3B) || (model == PI_MODEL_3BP) || + (model == PI_MODEL_ZERO) || (model == PI_MODEL_ZERO_W) || + (model == PI_MODEL_4B)) piPlusReadall (model) ; - else if ((model == PI_MODEL_CM) || (model == PI_MODEL_CM3)) + else if ((model == PI_MODEL_CM) || (model == PI_MODEL_CM3) || ((model == PI_MODEL_CM3P))) allReadall () ; else printf ("Oops - unable to determine board type... model: %d\n", model) ; diff --git a/gpio/test.sh b/gpio/test.sh index 708f1d3..5208b8b 100755 --- a/gpio/test.sh +++ b/gpio/test.sh @@ -25,6 +25,7 @@ for i in `seq 0 7`; do + echo "gpio mode $i out" gpio mode $i out done @@ -32,12 +33,14 @@ while true; do for i in `seq 0 7`; do + echo "gpio write $i 1" gpio write $i 1 sleep 0.1 done for i in `seq 0 7`; do + echo "gpio write $i 0" gpio write $i 0 sleep 0.1 done diff --git a/wiringPi/README b/wiringPi/README new file mode 100755 index 0000000..8afde83 --- /dev/null +++ b/wiringPi/README @@ -0,0 +1,8 @@ + +WiringPi +An implementation of most of the Arduino Wiring functions for the Raspberry Pi, +along with many more features and libraries to support hardware, etc. on the Raspberry Pi + +Full details at: +https://projects.drogon.net/raspberry-pi/wiringpi/ + diff --git a/wiringPi/ds1302.h b/wiringPi/ds1302.h new file mode 100755 index 0000000..e82b3ed --- /dev/null +++ b/wiringPi/ds1302.h @@ -0,0 +1,44 @@ +/* + * ds1302.h: + * Real Time clock + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern unsigned int ds1302rtcRead (const int reg) ; +extern void ds1302rtcWrite (const int reg, const unsigned int data) ; + +extern unsigned int ds1302ramRead (const int addr) ; +extern void ds1302ramWrite (const int addr, const unsigned int data) ; + +extern void ds1302clockRead (int clockData [8]) ; +extern void ds1302clockWrite (const int clockData [8]) ; + +extern void ds1302trickleCharge (const int diodes, const int resistors) ; + +extern void ds1302setup (const int clockPin, const int dataPin, const int csPin) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/lcd.c b/wiringPi/lcd.c new file mode 100755 index 0000000..f123db2 --- /dev/null +++ b/wiringPi/lcd.c @@ -0,0 +1,380 @@ +/* + * lcd.c: + * Text-based LCD driver. + * This is designed to drive the parallel interface LCD drivers + * based in the Hitachi HD44780U controller and compatables. + * + * Copyright (c) 2012 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include +#include + +#include "wiringPi.h" +#include "lcd.h" + +// Commands + +#define LCD_CLEAR 0x01 +#define LCD_HOME 0x02 +#define LCD_ENTRY 0x04 +#define LCD_ON_OFF 0x08 +#define LCD_CDSHIFT 0x10 +#define LCD_FUNC 0x20 +#define LCD_CGRAM 0x40 +#define LCD_DGRAM 0x80 + +#define LCD_ENTRY_SH 0x01 +#define LCD_ENTRY_ID 0x02 + +#define LCD_ON_OFF_B 0x01 +#define LCD_ON_OFF_C 0x02 +#define LCD_ON_OFF_D 0x04 + +#define LCD_FUNC_F 0x04 +#define LCD_FUNC_N 0x08 +#define LCD_FUNC_DL 0x10 + +#define LCD_CDSHIFT_RL 0x04 + +struct lcdDataStruct +{ + uint8_t bits, rows, cols ; + uint8_t rsPin, strbPin ; + uint8_t dataPins [8] ; +} ; + +struct lcdDataStruct *lcds [MAX_LCDS] ; + + +/* + * strobe: + * Toggle the strobe (Really the "E") pin to the device. + * According to the docs, data is latched on the falling edge. + ********************************************************************************* + */ + +static void strobe (struct lcdDataStruct *lcd) +{ + +// Note timing changes for new version of delayMicroseconds () + + digitalWrite (lcd->strbPin, 1) ; delayMicroseconds (50) ; + digitalWrite (lcd->strbPin, 0) ; delayMicroseconds (50) ; +} + + +/* + * sentDataCmd: + * Send an data or command byte to the display. + ********************************************************************************* + */ + +static void sendDataCmd (struct lcdDataStruct *lcd, uint8_t data) +{ + uint8_t i, d4 ; + + if (lcd->bits == 4) + { + d4 = (data >> 4) & 0x0F; + for (i = 0 ; i < 4 ; ++i) + { + digitalWrite (lcd->dataPins [i], (d4 & 1)) ; + d4 >>= 1 ; + } + strobe (lcd) ; + + d4 = data & 0x0F ; + for (i = 0 ; i < 4 ; ++i) + { + digitalWrite (lcd->dataPins [i], (d4 & 1)) ; + d4 >>= 1 ; + } + } + else + { + for (i = 0 ; i < 8 ; ++i) + { + digitalWrite (lcd->dataPins [i], (data & 1)) ; + data >>= 1 ; + } + } + strobe (lcd) ; +} + + +/* + * putCommand: + * Send a command byte to the display + ********************************************************************************* + */ + +static void putCommand (struct lcdDataStruct *lcd, uint8_t command) +{ + digitalWrite (lcd->rsPin, 0) ; + sendDataCmd (lcd, command) ; +} + +static void put4Command (struct lcdDataStruct *lcd, uint8_t command) +{ + uint8_t i ; + + digitalWrite (lcd->rsPin, 0) ; + + for (i = 0 ; i < 4 ; ++i) + { + digitalWrite (lcd->dataPins [i], (command & 1)) ; + command >>= 1 ; + } + strobe (lcd) ; +} + + +/* + ********************************************************************************* + * User Code below here + ********************************************************************************* + */ + +/* + * lcdHome: lcdClear: + * Home the cursor or clear the screen. + ********************************************************************************* + */ + +void lcdHome (int fd) +{ + struct lcdDataStruct *lcd = lcds [fd] ; + putCommand (lcd, LCD_HOME) ; +} + +void lcdClear (int fd) +{ + struct lcdDataStruct *lcd = lcds [fd] ; + putCommand (lcd, LCD_CLEAR) ; +} + + +/* + * lcdSendCommand: + * Send any arbitary command to the display + ********************************************************************************* + */ + +void lcdSendCommand (int fd, uint8_t command) +{ + struct lcdDataStruct *lcd = lcds [fd] ; + putCommand (lcd, command) ; +} + +/* + * lcdPosition: + * Update the position of the cursor on the display + ********************************************************************************* + */ + + +void lcdPosition (int fd, int x, int y) +{ + static uint8_t rowOff [4] = { 0x00, 0x40, 0x14, 0x54 } ; + struct lcdDataStruct *lcd = lcds [fd] ; + + putCommand (lcd, x + (LCD_DGRAM | rowOff [y])) ; +} + + +/* + * lcdPutchar: + * Send a data byte to be displayed on the display + ********************************************************************************* + */ + +void lcdPutchar (int fd, uint8_t data) +{ + struct lcdDataStruct *lcd = lcds [fd] ; + + digitalWrite (lcd->rsPin, 1) ; + sendDataCmd (lcd, data) ; +} + + +/* + * lcdPuts: + * Send a string to be displayed on the display + ********************************************************************************* + */ + +void lcdPuts (int fd, char *string) +{ + while (*string) + lcdPutchar (fd, *string++) ; +} + + +/* + * lcdPrintf: + * Printf to an LCD display + ********************************************************************************* + */ + +void lcdPrintf (int fd, char *message, ...) +{ + va_list argp ; + char buffer [1024] ; + + va_start (argp, message) ; + vsnprintf (buffer, 1023, message, argp) ; + va_end (argp) ; + + lcdPuts (fd, buffer) ; +} + + +/* + * lcdInit: + * Take a lot of parameters and initialise the LCD, and return a handle to + * that LCD, or -1 if any error. + ********************************************************************************* + */ + +int lcdInit (int rows, int cols, int bits, int rs, int strb, + int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7) +{ + static int initialised = 0 ; + + uint8_t func ; + int i ; + int lcdFd = -1 ; + struct lcdDataStruct *lcd ; + + if (initialised == 0) + { + initialised = 1 ; + for (i = 0 ; i < MAX_LCDS ; ++i) + lcds [i] = NULL ; + } + +// Simple sanity checks + + if (! ((bits == 4) || (bits == 8))) + return -1 ; + + if ((rows < 0) || (rows > 20)) + return -1 ; + + if ((cols < 0) || (cols > 20)) + return -1 ; + +// Create a new LCD: + + for (i = 0 ; i < MAX_LCDS ; ++i) + { + if (lcds [i] == NULL) + { + lcdFd = i ; + break ; + } + } + + if (lcdFd == -1) + return -1 ; + + lcd = malloc (sizeof (struct lcdDataStruct)) ; + if (lcd == NULL) + return -1 ; + + lcd->rsPin = rs ; + lcd->strbPin = strb ; + lcd->bits = 8 ; // For now - we'll set it properly later. + lcd->rows = rows ; + lcd->cols = cols ; + + lcd->dataPins [0] = d0 ; + lcd->dataPins [1] = d1 ; + lcd->dataPins [2] = d2 ; + lcd->dataPins [3] = d3 ; + lcd->dataPins [4] = d4 ; + lcd->dataPins [5] = d5 ; + lcd->dataPins [6] = d6 ; + lcd->dataPins [7] = d7 ; + + lcds [lcdFd] = lcd ; + + digitalWrite (lcd->rsPin, 0) ; pinMode (lcd->rsPin, OUTPUT) ; + digitalWrite (lcd->strbPin, 0) ; pinMode (lcd->strbPin, OUTPUT) ; + + for (i = 0 ; i < bits ; ++i) + { + digitalWrite (lcd->dataPins [i], 0) ; + pinMode (lcd->dataPins [i], OUTPUT) ; + } + delay (35) ; // mS + + +// 4-bit mode? +// OK. This is a PIG and it's not at all obvious from the documentation I had, +// so I guess some others have worked through either with better documentation +// or more trial and error... Anyway here goes: +// +// It seems that the controller needs to see the FUNC command at least 3 times +// consecutively - in 8-bit mode. If you're only using 8-bit mode, then it appears +// that you can get away with one func-set, however I'd not rely on it... +// +// So to set 4-bit mode, you need to send the commands one nibble at a time, +// the same three times, but send the command to set it into 8-bit mode those +// three times, then send a final 4th command to set it into 4-bit mode, and only +// then can you flip the switch for the rest of the library to work in 4-bit +// mode which sends the commands as 2 x 4-bit values. + + if (bits == 4) + { + func = LCD_FUNC | LCD_FUNC_DL ; // Set 8-bit mode 3 times + put4Command (lcd, func >> 4) ; delay (35) ; + put4Command (lcd, func >> 4) ; delay (35) ; + put4Command (lcd, func >> 4) ; delay (35) ; + func = LCD_FUNC ; // 4th set: 4-bit mode + put4Command (lcd, func >> 4) ; delay (35) ; + lcd->bits = 4 ; + } + else + { + func = LCD_FUNC | LCD_FUNC_DL ; + putCommand (lcd, func ) ; delay (35) ; + putCommand (lcd, func ) ; delay (35) ; + putCommand (lcd, func ) ; delay (35) ; + } + + if (lcd->rows > 1) + { + func |= LCD_FUNC_N ; + putCommand (lcd, func) ; delay (35) ; + } + +// Rest of the initialisation sequence + + putCommand (lcd, LCD_ON_OFF | LCD_ON_OFF_D) ; delay (2) ; + putCommand (lcd, LCD_ENTRY | LCD_ENTRY_ID) ; delay (2) ; + putCommand (lcd, LCD_CDSHIFT | LCD_CDSHIFT_RL) ; delay (2) ; + putCommand (lcd, LCD_CLEAR) ; delay (5) ; + + return lcdFd ; +} diff --git a/wiringPi/lcd.h b/wiringPi/lcd.h new file mode 100755 index 0000000..0a0e598 --- /dev/null +++ b/wiringPi/lcd.h @@ -0,0 +1,52 @@ +/* + * lcd.h: + * Text-based LCD driver. + * This is designed to drive the parallel interface LCD drivers + * based in the Hitachi HD44780U controller and compatables. + * + * Copyright (c) 2012 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#define MAX_LCDS 8 + +#ifdef __cplusplus +extern "C" { +#endif + +extern void lcdHome (const int fd) ; +extern void lcdClear (const int fd) ; +extern void lcdDisplay (const int fd, int state) ; +extern void lcdCursor (const int fd, int state) ; +extern void lcdCursorBlink (const int fd, int state) ; +extern void lcdSendCommand (const int fd, unsigned char command) ; +extern void lcdPosition (const int fd, int x, int y) ; +extern void lcdCharDef (const int fd, int index, unsigned char data [8]) ; +extern void lcdPutchar (const int fd, unsigned char data) ; +extern void lcdPuts (const int fd, const char *string) ; +extern void lcdPrintf (const int fd, const char *message, ...) ; + +extern int lcdInit (const int rows, const int cols, const int bits, + const int rs, const int strb, + const int d0, const int d1, const int d2, const int d3, const int d4, + const int d5, const int d6, const int d7) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/lcd128x64.h b/wiringPi/lcd128x64.h new file mode 100755 index 0000000..b448bbc --- /dev/null +++ b/wiringPi/lcd128x64.h @@ -0,0 +1,39 @@ +/* + * lcd128x64.h: + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +extern void lcd128x64setOrigin (int x, int y) ; +extern void lcd128x64setOrientation (int orientation) ; +extern void lcd128x64orientCoordinates (int *x, int *y) ; +extern void lcd128x64getScreenSize (int *x, int *y) ; +extern void lcd128x64point (int x, int y, int colour) ; +extern void lcd128x64line (int x0, int y0, int x1, int y1, int colour) ; +extern void lcd128x64lineTo (int x, int y, int colour) ; +extern void lcd128x64rectangle (int x1, int y1, int x2, int y2, int colour, int filled) ; +extern void lcd128x64circle (int x, int y, int r, int colour, int filled) ; +extern void lcd128x64ellipse (int cx, int cy, int xRadius, int yRadius, int colour, int filled) ; +extern void lcd128x64putchar (int x, int y, int c, int bgCol, int fgCol) ; +extern void lcd128x64puts (int x, int y, const char *str, int bgCol, int fgCol) ; +extern void lcd128x64update (void) ; +extern void lcd128x64clear (int colour) ; + +extern int lcd128x64setup (void) ; diff --git a/wiringPi/maxdetect.h b/wiringPi/maxdetect.h new file mode 100755 index 0000000..a1fd742 --- /dev/null +++ b/wiringPi/maxdetect.h @@ -0,0 +1,40 @@ +/* + * maxdetect.h: + * Driver for the MaxDetect series sensors + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + + +#ifdef __cplusplus +extern "C" { +#endif + +// Main generic function + +int maxDetectRead (const int pin, unsigned char buffer [4]) ; + +// Individual sensors + +int readRHT03 (const int pin, int *temp, int *rh) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/piNes.c b/wiringPi/piNes.c new file mode 100755 index 0000000..a115050 --- /dev/null +++ b/wiringPi/piNes.c @@ -0,0 +1,113 @@ +/* + * piNes.c: + * Driver for the NES Joystick controller on the Raspberry Pi + * Copyright (c) 2012 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include + +#include "piNes.h" + +#define MAX_NES_JOYSTICKS 8 + +#define NES_RIGHT 0x01 +#define NES_LEFT 0x02 +#define NES_DOWN 0x04 +#define NES_UP 0x08 +#define NES_START 0x10 +#define NES_SELECT 0x20 +#define NES_B 0x40 +#define NES_A 0x80 + + +#define PULSE_TIME 25 + +// Data to store the pins for each controller + +struct nesPinsStruct +{ + unsigned int cPin, dPin, lPin ; +} ; + +static struct nesPinsStruct nesPins [MAX_NES_JOYSTICKS] ; + +static int joysticks = 0 ; + + +/* + * setupNesJoystick: + * Create a new NES joystick interface, program the pins, etc. + ********************************************************************************* + */ + +int setupNesJoystick (int dPin, int cPin, int lPin) +{ + if (joysticks == MAX_NES_JOYSTICKS) + return -1 ; + + nesPins [joysticks].dPin = dPin ; + nesPins [joysticks].cPin = cPin ; + nesPins [joysticks].lPin = lPin ; + + digitalWrite (lPin, LOW) ; + digitalWrite (cPin, LOW) ; + + pinMode (lPin, OUTPUT) ; + pinMode (cPin, OUTPUT) ; + pinMode (dPin, INPUT) ; + + return joysticks++ ; +} + + +/* + * readNesJoystick: + * Do a single scan of the NES Joystick. + ********************************************************************************* + */ + +unsigned int readNesJoystick (int joystick) +{ + unsigned int value = 0 ; + int i ; + + struct nesPinsStruct *pins = &nesPins [joystick] ; + +// Toggle Latch - which presents the first bit + + digitalWrite (pins->lPin, HIGH) ; delayMicroseconds (PULSE_TIME) ; + digitalWrite (pins->lPin, LOW) ; delayMicroseconds (PULSE_TIME) ; + +// Read first bit + + value = digitalRead (pins->dPin) ; + +// Now get the next 7 bits with the clock + + for (i = 0 ; i < 7 ; ++i) + { + digitalWrite (pins->cPin, HIGH) ; delayMicroseconds (PULSE_TIME) ; + digitalWrite (pins->cPin, LOW) ; delayMicroseconds (PULSE_TIME) ; + value = (value << 1) | digitalRead (pins->dPin) ; + } + + return value ^ 0xFF ; +} diff --git a/wiringPi/piNes.h b/wiringPi/piNes.h new file mode 100755 index 0000000..897f181 --- /dev/null +++ b/wiringPi/piNes.h @@ -0,0 +1,45 @@ +/* + * piNes.h: + * Driver for the NES Joystick controller on the Raspberry Pi + * Copyright (c) 2012 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#define MAX_NES_JOYSTICKS 8 + +#define NES_RIGHT 0x01 +#define NES_LEFT 0x02 +#define NES_DOWN 0x04 +#define NES_UP 0x08 +#define NES_START 0x10 +#define NES_SELECT 0x20 +#define NES_B 0x40 +#define NES_A 0x80 + +#ifdef __cplusplus +extern "C" { +#endif + +extern int setupNesJoystick (int dPin, int cPin, int lPin) ; +extern unsigned int readNesJoystick (int joystick) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/scrollPhat.h b/wiringPi/scrollPhat.h new file mode 100755 index 0000000..0e762b1 --- /dev/null +++ b/wiringPi/scrollPhat.h @@ -0,0 +1,39 @@ +/* + * scrollPhat.h: + * Simple driver for the Pimoroni Scroll Phat device + * + * Copyright (c) 2015 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +extern void scrollPhatPoint (int x, int y, int colour) ; +extern void scrollPhatLine (int x0, int y0, int x1, int y1, int colour) ; +extern void scrollPhatLineTo (int x, int y, int colour) ; +extern void scrollPhatRectangle (int x1, int y1, int x2, int y2, int colour, int filled) ; +extern void scrollPhatUpdate (void) ; +extern void scrollPhatClear (void) ; + +extern int scrollPhatPutchar (int c) ; +//extern void scrollPhatPutchar (int c) ; +extern void scrollPhatPuts (const char *str) ; +extern void scrollPhatPrintf (const char *message, ...) ; +extern void scrollPhatPrintSpeed (const int cps10) ; + +extern void scrollPhatIntensity (const int percent) ; +extern int scrollPhatSetup (void) ; diff --git a/wiringPi/softServo.c b/wiringPi/softServo.c old mode 100644 new mode 100755 index 9de9f4f..949de8b --- a/wiringPi/softServo.c +++ b/wiringPi/softServo.c @@ -165,7 +165,7 @@ void softServoWrite (int servoPin, int value) servoPin &= 63 ; - /**/ if (value < -250) + if (value < -250) value = -250 ; else if (value > 1250) value = 1250 ; diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c old mode 100644 new mode 100755 index 586b148..cad6e18 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -215,10 +215,12 @@ volatile unsigned int *_wiringPiTimerIrqRaw ; #define GPIO_PERI_BASE_OLD 0x20000000 #define GPIO_PERI_BASE_NEW 0x3F000000 +#define GPIO_PERI_BASE_PI4 0xFE000000 static volatile unsigned int piGpioBase = 0 ; +static volatile unsigned int piModel = 0 ; -const char *piModelNames [16] = +const char *piModelNames [20] = { "Model A", // 0 "Model B", // 1 @@ -228,17 +230,21 @@ const char *piModelNames [16] = "Alpha", // 5 "CM", // 6 "Unknown07", // 07 - "Pi 3", // 08 + "Pi 3B", // 08 "Pi Zero", // 09 "CM3", // 10 "Unknown11", // 11 "Pi Zero-W", // 12 - "Pi 3+", // 13 - "Unknown14", // 14 + "Pi 3B+", // 13 + "Pi 3A+", // 14 "Unknown15", // 15 + "CM3+", // 16 + "Pi 4B", // 17 + "Unknown18", // 18 + "Unknown19", // 19 } ; -const char *piRevisionNames [16] = +const char *piRevisionNames [18] = { "00", "01", @@ -256,6 +262,8 @@ const char *piRevisionNames [16] = "13", "14", "15", + "16", + "17", } ; const char *piMakerNames [16] = @@ -265,7 +273,7 @@ const char *piMakerNames [16] = "Embest", // 2 "Unknown", // 3 "Embest", // 4 - "Unknown05", // 5 + "Stadium", // 5 "Unknown06", // 6 "Unknown07", // 7 "Unknown08", // 8 @@ -283,8 +291,8 @@ const int piMemorySize [8] = 256, // 0 512, // 1 1024, // 2 - 0, // 3 - 0, // 4 + 2048, // 3 + 4096, // 4 0, // 5 0, // 6 0, // 7 @@ -537,6 +545,10 @@ static uint8_t gpioToFEN [] = // GPIO Pin pull up/down register #define GPPUD 37 +#define GPPUD0 57 //Pi4 Pins 15:0 +#define GPPUD1 58 //Pi4 Pins 31:16 +#define GPPUD2 59 //Pi4 Pins 47:32 +#define GPPUD3 60 //Pi4 Pins 57:48 // gpioToPUDCLK // (Word) offset to the Pull Up Down Clock regsiter @@ -1493,6 +1505,9 @@ void pullUpDnControl (int pin, int pud) { struct wiringPiNodeStruct *node = wiringPiNodes ; + uint32_t pull, bits; + int shift = (pin & 0xf) << 1; + setupCheck ("pullUpDnControl") ; if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin @@ -1504,21 +1519,41 @@ void pullUpDnControl (int pin, int pud) else if (wiringPiMode != WPI_MODE_GPIO) return ; - *(gpio + GPPUD) = pud & 3 ; delayMicroseconds (5) ; - *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ; - - *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ; - *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ; - } - else // Extension module - { + if ( piModel == PI_MODEL_4B ) + { + switch (pud) { // The PI4B uses different numbering than previous pi's + case PUD_OFF: //0 + pull = 0; + break; + case PUD_UP: //2 + pull = 1; + break; + case PUD_DOWN: //1 + pull = 2; + break; + default: + pull = 0; + break; + } + bits = *(gpio + GPPUD0 + (pin>>4)); + bits &= ~(3 << shift); + bits |= (pull << shift); + *(gpio + GPPUD0 + (pin>>4)) = bits; + + } else { + *(gpio + GPPUD) = pud & 3 ; delayMicroseconds (5) ; + *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ; + *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ; + *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ; + } + + } else { // Extension module if ((node = wiringPiFindNode (pin)) != NULL) node->pullUpDnControl (node, pin, pud) ; return ; } } - /* * digitalRead: * Read the value of a given Pin, returning HIGH or LOW @@ -2238,8 +2273,10 @@ int wiringPiSetup (void) // don't really many anything, so force native BCM mode anyway. piBoardId (&model, &rev, &mem, &maker, &overVolted) ; + // set global variable for model, as we need to reuse it later in the pull up/down routine. + piModel = model; - if ((model == PI_MODEL_CM) || (model == PI_MODEL_CM3)) + if ((model == PI_MODEL_CM) || (model == PI_MODEL_CM3) || (model == PI_MODEL_CM3P)) wiringPiMode = WPI_MODE_GPIO ; else wiringPiMode = WPI_MODE_PINS ; @@ -2265,7 +2302,9 @@ int wiringPiSetup (void) case PI_MODEL_ZERO: case PI_MODEL_ZERO_W: piGpioBase = GPIO_PERI_BASE_OLD ; break ; - + case PI_MODEL_4B: + piGpioBase = GPIO_PERI_BASE_PI4 ; + break ; default: piGpioBase = GPIO_PERI_BASE_NEW ; break ; diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h old mode 100644 new mode 100755 index ae5d647..7847e5d --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -95,11 +95,14 @@ #define PI_ALPHA 5 #define PI_MODEL_CM 6 #define PI_MODEL_07 7 -#define PI_MODEL_3 8 +#define PI_MODEL_3B 8 #define PI_MODEL_ZERO 9 #define PI_MODEL_CM3 10 #define PI_MODEL_ZERO_W 12 -#define PI_MODEL_3P 13 +#define PI_MODEL_3BP 13 +#define PI_MODEL_3AP 14 +#define PI_MODEL_CM3P 16 +#define PI_MODEL_4B 17 #define PI_VERSION_1 0 #define PI_VERSION_1_1 1 @@ -109,10 +112,12 @@ #define PI_MAKER_SONY 0 #define PI_MAKER_EGOMAN 1 #define PI_MAKER_EMBEST 2 -#define PI_MAKER_UNKNOWN 3 +#define PI_MAKER_SONYJAPAN 3 +#define PI_MAKER_EMBEST4 4 +#define PI_MAKER_STADIUM 5 -extern const char *piModelNames [16] ; -extern const char *piRevisionNames [16] ; +extern const char *piModelNames [20] ; +extern const char *piRevisionNames [18] ; extern const char *piMakerNames [16] ; extern const int piMemorySize [ 8] ; diff --git a/wiringPi/wiringPiFace.c b/wiringPi/wiringPiFace.c new file mode 100755 index 0000000..ac3c6fa --- /dev/null +++ b/wiringPi/wiringPiFace.c @@ -0,0 +1,362 @@ +/* + * wiringPiFace: + * Arduino compatable (ish) Wiring library for the Raspberry Pi + * Copyright (c) 2012 Gordon Henderson + * + * This file to interface with the PiFace peripheral device which + * has an MCP23S17 GPIO device connected via the SPI bus. + * + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + + +#include +#include +#include +#include +#include +#include + +#include "wiringPi.h" + + +// The SPI bus parameters +// Variables as they need to be passed as pointers later on + +static char *spiDevice = "/dev/spidev0.0" ; +static uint8_t spiMode = 0 ; +static uint8_t spiBPW = 8 ; +static uint32_t spiSpeed = 5000000 ; +static uint16_t spiDelay = 0; + +// Locals here to keep track of everything + +static int spiFd ; + +// The MCP23S17 doesn't have bit-set operations, so it's +// cheaper to keep a copy here than to read/modify/write it + +uint8_t dataOutRegister = 0 ; +uint8_t pudRegister = 0 ; + +// MCP23S17 Registers + +#define IOCON 0x0A + +#define IODIRA 0x00 +#define IPOLA 0x02 +#define GPINTENA 0x04 +#define DEFVALA 0x06 +#define INTCONA 0x08 +#define GPPUA 0x0C +#define INTFA 0x0E +#define INTCAPA 0x10 +#define GPIOA 0x12 +#define OLATA 0x14 + +#define IODIRB 0x01 +#define IPOLB 0x03 +#define GPINTENB 0x05 +#define DEFVALB 0x07 +#define INTCONB 0x09 +#define GPPUB 0x0D +#define INTFB 0x0F +#define INTCAPB 0x11 +#define GPIOB 0x13 +#define OLATB 0x15 + +// Bits in the IOCON register + +#define IOCON_BANK_MODE 0x80 +#define IOCON_MIRROR 0x40 +#define IOCON_SEQOP 0x20 +#define IOCON_DISSLW 0x10 +#define IOCON_HAEN 0x08 +#define IOCON_ODR 0x04 +#define IOCON_INTPOL 0x02 +#define IOCON_UNUSED 0x01 + +// Default initialisation mode + +#define IOCON_INIT (IOCON_SEQOP) + +// Command codes + +#define CMD_WRITE 0x40 +#define CMD_READ 0x41 + + +/* + * writeByte: + * Write a byte to a register on the MCP23S17 on the SPI bus. + * This is using the synchronous access mechanism. + ********************************************************************************* + */ + +static void writeByte (uint8_t reg, uint8_t data) +{ + uint8_t spiBufTx [3] ; + uint8_t spiBufRx [3] ; + struct spi_ioc_transfer spi ; + + spiBufTx [0] = CMD_WRITE ; + spiBufTx [1] = reg ; + spiBufTx [2] = data ; + + spi.tx_buf = (unsigned long)spiBufTx ; + spi.rx_buf = (unsigned long)spiBufRx ; + spi.len = 3 ; + spi.delay_usecs = spiDelay ; + spi.speed_hz = spiSpeed ; + spi.bits_per_word = spiBPW ; + + ioctl (spiFd, SPI_IOC_MESSAGE(1), &spi) ; +} + +/* + * readByte: + * Read a byte from a register on the MCP23S17 on the SPI bus. + * This is the synchronous access mechanism. + * What appears to happen is that the data returned is at + * the same offset as the number of bytes written to the device. So if we + * write 2 bytes (e.g. command then register number), then the data returned + * will by at the 3rd byte... + ********************************************************************************* + */ + +static uint8_t readByte (uint8_t reg) +{ + uint8_t tx [4] ; + uint8_t rx [4] ; + struct spi_ioc_transfer spi ; + + tx [0] = CMD_READ ; + tx [1] = reg ; + tx [2] = 0 ; + + spi.tx_buf = (unsigned long)tx ; + spi.rx_buf = (unsigned long)rx ; + spi.len = 3 ; + spi.delay_usecs = spiDelay ; + spi.speed_hz = spiSpeed ; + spi.bits_per_word = spiBPW ; + + ioctl (spiFd, SPI_IOC_MESSAGE(1), &spi) ; + + return rx [2] ; +} + + +/* + * digitalWritePiFace: + * Perform the digitalWrite function on the PiFace board + ********************************************************************************* + */ + +void digitalWritePiFace (int pin, int value) +{ + uint8_t mask = 1 << pin ; + + if (value == 0) + dataOutRegister &= (~mask) ; + else + dataOutRegister |= mask ; + + writeByte (GPIOA, dataOutRegister) ; +} + +void digitalWriteBytePiFace (int value) +{ + writeByte (GPIOA, value) ; +} + + +void digitalWritePiFaceSpecial (int pin, int value) +{ + uint8_t mask = 1 << pin ; + uint8_t old ; + + old = readByte (GPIOA) ; + + if (value == 0) + old &= (~mask) ; + else + old |= mask ; + + writeByte (GPIOA, old) ; +} + + +/* + * digitalReadPiFace: + * Perform the digitalRead function on the PiFace board + ********************************************************************************* + */ + +int digitalReadPiFace (int pin) +{ + uint8_t mask = 1 << pin ; + + if ((readByte (GPIOB) & mask) != 0) + return HIGH ; + else + return LOW ; +} + + +/* + * pullUpDnControlPiFace: + * Perform the pullUpDnControl function on the PiFace board + ********************************************************************************* + */ + +void pullUpDnControlPiFace (int pin, int pud) +{ + uint8_t mask = 1 << pin ; + + if (pud == PUD_UP) + pudRegister |= mask ; + else + pudRegister &= (~mask) ; + + writeByte (GPPUB, pudRegister) ; + +} + + +void pullUpDnControlPiFaceSpecial (int pin, int pud) +{ + uint8_t mask = 1 << pin ; + uint8_t old ; + + old = readByte (GPPUB) ; + + if (pud == PUD_UP) + old |= mask ; + else + old &= (~mask) ; + + writeByte (GPPUB, old) ; + +} + + + +/* + * Dummy functions that are not used in this mode + ********************************************************************************* + */ + +void pinModePiFace (int pin, int mode) {} +void pwmWritePiFace (int pin, int value) {} +int waitForInterruptPiFace (int pin, int mS) { return 0 ; } + + +/* + * wiringPiSetupPiFace + * Setup the SPI interface and initialise the MCP23S17 chip + ********************************************************************************* + */ + +static int _wiringPiSetupPiFace (void) +{ + if ((spiFd = open (spiDevice, O_RDWR)) < 0) + return -1 ; + +// Set SPI parameters +// Why are we doing a read after write? +// I don't know - just blindliy copying an example elsewhere... -GH- + + if (ioctl (spiFd, SPI_IOC_WR_MODE, &spiMode) < 0) + return -1 ; + + if (ioctl (spiFd, SPI_IOC_RD_MODE, &spiMode) < 0) + return -1 ; + + if (ioctl (spiFd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0) + return -1 ; + + if (ioctl (spiFd, SPI_IOC_RD_BITS_PER_WORD, &spiBPW) < 0) + return -1 ; + + if (ioctl (spiFd, SPI_IOC_WR_MAX_SPEED_HZ, &spiSpeed) < 0) + return -1 ; + + if (ioctl (spiFd, SPI_IOC_RD_MAX_SPEED_HZ, &spiSpeed) < 0) + return -1 ; + +// Setup the MCP23S17 + + writeByte (IOCON, IOCON_INIT) ; + + writeByte (IODIRA, 0x00) ; // Port A -> Outputs + writeByte (IODIRB, 0xFF) ; // Port B -> Inputs + + return 0 ; +} + + +int wiringPiSetupPiFace (void) +{ + int x = _wiringPiSetupPiFace () ; + + if (x != 0) + return x ; + + writeByte (GPIOA, 0x00) ; // Set all outptus off + writeByte (GPPUB, 0x00) ; // Disable any pull-ups on port B + + pinMode = pinModePiFace ; + pullUpDnControl = pullUpDnControlPiFace ; + digitalWrite = digitalWritePiFace ; + digitalWriteByte = digitalWriteBytePiFace ; + pwmWrite = pwmWritePiFace ; + digitalRead = digitalReadPiFace ; + waitForInterrupt = waitForInterruptPiFace ; + + return 0 ; +} + + +/* + * wiringPiSetupPiFaceForGpioProg: + * Setup the SPI interface and initialise the MCP23S17 chip + * Special version for the gpio program + ********************************************************************************* + */ + + +int wiringPiSetupPiFaceForGpioProg (void) +{ + int x = _wiringPiSetupPiFace () ; + + if (x != 0) + return x ; + + pinMode = pinModePiFace ; + pullUpDnControl = pullUpDnControlPiFaceSpecial ; + digitalWrite = digitalWritePiFaceSpecial ; + digitalWriteByte = digitalWriteBytePiFace ; + pwmWrite = pwmWritePiFace ; + digitalRead = digitalReadPiFace ; + waitForInterrupt = waitForInterruptPiFace ; + + return 0 ; +} diff --git a/wiringPi/wiringPiSPI.c b/wiringPi/wiringPiSPI.c old mode 100644 new mode 100755 index 022b99f..749c8fe --- a/wiringPi/wiringPiSPI.c +++ b/wiringPi/wiringPiSPI.c @@ -23,7 +23,9 @@ */ +#include #include +#include #include #include #include @@ -39,8 +41,8 @@ // The SPI bus parameters // Variables as they need to be passed as pointers later on -static const char *spiDev0 = "/dev/spidev0.0" ; -static const char *spiDev1 = "/dev/spidev0.1" ; +//static const char *spiDev0 = "/dev/spidev0.0" ; +//static const char *spiDev1 = "/dev/spidev0.1" ; static const uint8_t spiBPW = 8 ; static const uint16_t spiDelay = 0 ; @@ -100,11 +102,16 @@ int wiringPiSPIDataRW (int channel, unsigned char *data, int len) int wiringPiSPISetupMode (int channel, int speed, int mode) { int fd ; + char spiDev [32] ; mode &= 3 ; // Mode is 0, 1, 2 or 3 - channel &= 1 ; // Channel is 0 or 1 - if ((fd = open (channel == 0 ? spiDev0 : spiDev1, O_RDWR)) < 0) +// Channel can be anything - lets hope for the best +// channel &= 1 ; // Channel is 0 or 1 + + snprintf (spiDev, 31, "/dev/spidev0.%d", channel) ; + + if ((fd = open (spiDev, O_RDWR)) < 0) return wiringPiFailure (WPI_ALMOST, "Unable to open SPI device: %s\n", strerror (errno)) ; spiSpeeds [channel] = speed ;