Browse Source

Code and Makefile changes to date

pull/66/head
Jim-P 5 years ago
parent
commit
93300f8ab8
35 changed files with 2138 additions and 195 deletions
  1. +14
    -1
      examples/Makefile
  2. +87
    -48
      examples/blink.c
  3. +59
    -23
      examples/isr-osc.c
  4. +54
    -15
      examples/isr.c
  5. +149
    -0
      examples/isr1.c
  6. +6
    -3
      examples/okLed.c
  7. +62
    -17
      examples/pwm.c
  8. +93
    -0
      examples/pwm1.c
  9. +19
    -0
      examples/servo-reset.sh
  10. +87
    -20
      examples/servo.c
  11. +8
    -7
      examples/spiSpeed.c
  12. +111
    -0
      examples/test1.c
  13. +58
    -0
      examples/test2.c
  14. +59
    -0
      examples/tone.c
  15. +4
    -1
      examples/wfi.c
  16. +2
    -1
      gpio/Makefile
  17. +22
    -22
      gpio/gpio.c
  18. +26
    -0
      gpio/newVersion
  19. +0
    -0
      gpio/pintest
  20. +13
    -8
      gpio/readall.c
  21. +3
    -0
      gpio/test.sh
  22. +8
    -0
      wiringPi/README
  23. +44
    -0
      wiringPi/ds1302.h
  24. +380
    -0
      wiringPi/lcd.c
  25. +52
    -0
      wiringPi/lcd.h
  26. +39
    -0
      wiringPi/lcd128x64.h
  27. +40
    -0
      wiringPi/maxdetect.h
  28. +113
    -0
      wiringPi/piNes.c
  29. +45
    -0
      wiringPi/piNes.h
  30. +39
    -0
      wiringPi/scrollPhat.h
  31. +1
    -1
      wiringPi/softServo.c
  32. +58
    -19
      wiringPi/wiringPi.c
  33. +10
    -5
      wiringPi/wiringPi.h
  34. +362
    -0
      wiringPi/wiringPiFace.c
  35. +11
    -4
      wiringPi/wiringPiSPI.c

+ 14
- 1
examples/Makefile View File

@@ -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)


+ 87
- 48
examples/blink.c View File

@@ -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. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <stdio.h>
#include <wiringPi.h>

// 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. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <wiringPi.h>
#include <signal.h>
// 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;
}
//**********************************************************************************************************************

+ 59
- 23
examples/isr-osc.c View File

@@ -48,19 +48,25 @@
#include <errno.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <signal.h>


// 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;
}

//**********************************************************************************************************************

+ 54
- 15
examples/isr.c View File

@@ -36,14 +36,22 @@
#include <errno.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <signal.h>

//**********************************************************************************************************************

// 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;
}

//**********************************************************************************************************************

+ 149
- 0
examples/isr1.c View File

@@ -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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <signal.h>
#include <pthread.h>

//**********************************************************************************************************************

// 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 );
}

//**********************************************************************************************************************

+ 6
- 3
examples/okLed.c View File

@@ -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)) ;


+ 62
- 17
examples/pwm.c View File

@@ -27,32 +27,77 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <signal.h>
#include <unistd.h>

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;
}

//**********************************************************************************************************************

+ 93
- 0
examples/pwm1.c View File

@@ -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. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <stdio.h>
#include <errno.h>
#include <string.h>

#include <wiringPi.h>
#include <softPwm.h>

#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 ;
}
}

+ 19
- 0
examples/servo-reset.sh View File

@@ -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


+ 87
- 20
examples/servo.c View File

@@ -24,34 +24,101 @@
*/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>

#include <wiringPi.h>
#include <softServo.h>

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;
}

+ 8
- 7
examples/spiSpeed.c View File

@@ -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 ;


+ 111
- 0
examples/test1.c View File

@@ -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. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <wiringPi.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>


// 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 ;
}

+ 58
- 0
examples/test2.c View File

@@ -0,0 +1,58 @@
/*
* test2.c:
* This tests the hardware PWM channel.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <wiringPi.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

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 ;
}

+ 59
- 0
examples/tone.c View File

@@ -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. <projects@drogon.net>
***********************************************************************
* 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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <stdio.h>
#include <errno.h>
#include <string.h>

#include <wiringPi.h>
#include <softTone.h>

#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) ;
}
}

}

+ 4
- 1
examples/wfi.c View File

@@ -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)
{


+ 2
- 1
gpio/Makefile View File

@@ -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


+ 22
- 22
gpio/gpio.c View File

@@ -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] <read/write/wb> ...\n"
" gpio <mode/read/write/aread/awritewb/pwm/pwmTone/clock> ...\n"
" gpio <toggle/blink> <pin>\n"
" gpio readall\n"
" gpio unexportall/exports\n"
" gpio export/edge/unexport ...\n"
" gpio wfi <pin> <mode>\n"
" gpio drive <group> <value>\n"
" gpio pwm-bal/pwm-ms \n"
" gpio pwmr <range> \n"
" gpio pwmc <divider> \n"
" gpio load spi/i2c\n"
" gpio unload spi/i2c\n"
" gpio i2cd/i2cdetect\n"
" gpio rbx/rbd\n"
" gpio wb <value>\n"
" gpio usbp high/low\n"
" gpio gbr <channel>\n"
" gpio gbw <channel> <value>" ; // No trailing newline needed here.

" gpio readall\n"
" gpio unexportall/exports\n"
" gpio export/edge/unexport ...\n"
" gpio wfi <pin> <mode>\n"
" gpio drive <group> <value>\n"
" gpio pwm-bal/pwm-ms \n"
" gpio pwmr <range> \n"
" gpio pwmc <divider> \n"
" gpio load spi/i2c\n"
" gpio unload spi/i2c\n"
" gpio i2cd/i2cdetect\n"
" gpio rbx/rbd\n"
" gpio wb <value>\n"
" gpio usbp high/low\n"
" gpio gbr <channel>\n"
" gpio gbw <channel> <value>" ; // 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,
} ;



+ 26
- 0
gpio/newVersion View File

@@ -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 <http://www.gnu.org/licenses/>.
#################################################################################

rm -f version.h
echo "#define VERSION \"`cat ../VERSION`\"" > version.h

+ 0
- 0
gpio/pintest View File


+ 13
- 8
gpio/readall.c View File

@@ -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) ;


+ 3
- 0
gpio/test.sh View File

@@ -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


+ 8
- 0
wiringPi/README View File

@@ -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/


+ 44
- 0
wiringPi/ds1302.h View File

@@ -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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#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

+ 380
- 0
wiringPi/lcd.c View File

@@ -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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>

#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 ;
}

+ 52
- 0
wiringPi/lcd.h View File

@@ -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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#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

+ 39
- 0
wiringPi/lcd128x64.h View File

@@ -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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

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) ;

+ 40
- 0
wiringPi/maxdetect.h View File

@@ -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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/


#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

+ 113
- 0
wiringPi/piNes.c View File

@@ -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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <wiringPi.h>

#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 ;
}

+ 45
- 0
wiringPi/piNes.h View File

@@ -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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#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

+ 39
- 0
wiringPi/scrollPhat.h View File

@@ -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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

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) ;

+ 1
- 1
wiringPi/softServo.c View File

@@ -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 ;


+ 58
- 19
wiringPi/wiringPi.c View File

@@ -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 ;


+ 10
- 5
wiringPi/wiringPi.h View File

@@ -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] ;



+ 362
- 0
wiringPi/wiringPiFace.c View File

@@ -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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/


#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <linux/spi/spidev.h>

#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 ;
}

+ 11
- 4
wiringPi/wiringPiSPI.c View File

@@ -23,7 +23,9 @@
*/


#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
@@ -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 ;


Loading…
Cancel
Save