Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added check for board revision, added test program for button polling

  • Loading branch information...
commit 8ae779cafbbfd81da682b652181fb90a5fd59a70 1 parent 4926c7f
petrockblog authored
View
7 Makefile
@@ -1,11 +1,11 @@
CC=gcc
CFLAGS=-c -Wall -O3
-LIBS=-lbcm2835
+LIBS=-lbcm2835 -lrt
INCLUDES=-Iinclude
EXECUTABLE=SNESDev
SNESDev: obj/main.o obj/SNESpad.o
- $(CC) obj/main.o obj/SNESpad.o $(LIBS) -o bin/$(EXECUTABLE)
+ $(CC) obj/main.o obj/SNESpad.o obj/cpuinfo.o $(LIBS) -o bin/$(EXECUTABLE)
obj/main.o: src/main.c
$(CC) $(CFLAGS) $(INCLUDES) $(LIBS) src/main.c -o obj/main.o
@@ -13,6 +13,9 @@ obj/main.o: src/main.c
obj/SNESpad.o: src/SNESpad.c
$(CC) $(CFLAGS) $(INCLUDES) $(LIBS) src/SNESpad.c -o obj/SNESpad.o
+obj/cpuinfo.o: src/cpuinfo.c
+ $(CC) $(CFLAGS) $(INCLUDES) $(LIBS) src/cpuinfo.c -o obj/cpuinfo.o
+
clean:
rm obj/*.o; rm bin/$(EXECUTABLE)
View
23 include/cpuinfo.h
@@ -0,0 +1,23 @@
+/*
+Copyright (c) 2012 Ben Croston
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+int get_rpi_revision(void);
View
65 src/cpuinfo.c
@@ -0,0 +1,65 @@
+/*
+Copyright (c) 2012 Ben Croston
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include "cpuinfo.h"
+
+char *get_cpuinfo_revision(char *revision)
+{
+ FILE *fp;
+ char buffer[1024];
+ char hardware[1024];
+ int rpi_found = 0;
+
+ if ((fp = fopen("/proc/cpuinfo", "r")) == NULL)
+ return 0;
+
+ while(!feof(fp)) {
+ fgets(buffer, sizeof(buffer) , fp);
+ sscanf(buffer, "Hardware : %s", hardware);
+ if (strcmp(hardware, "BCM2708") == 0)
+ rpi_found = 1;
+ sscanf(buffer, "Revision : %s", revision);
+ }
+ fclose(fp);
+
+ if (!rpi_found)
+ revision = NULL;
+ return revision;
+}
+
+int get_rpi_revision(void)
+{
+ char revision[1024] = {'\0'};
+
+ if (get_cpuinfo_revision(revision) == NULL)
+ return -1;
+
+ if ((strcmp(revision, "0002") == 0) ||
+ (strcmp(revision, "1000002") == 0 ) ||
+ (strcmp(revision, "0003") == 0) ||
+ (strcmp(revision, "1000003") == 0 ))
+ return 1;
+ else // assume rev 2 (0004 0005 0006 1000004 1000005 1000006)
+ return 2;
+}
View
34 src/main.c
@@ -39,13 +39,15 @@
#include <time.h>
#include "SNESpad.h"
+#include "cpuinfo.h"
/* time to wait after each cycle */
#define FRAMEWAIT 20
#define FRAMEWAITLONG 100
/* set the GPIO pins of the button and the LEDs. */
-#define BUTTONPIN RPI_GPIO_P1_11
+#define BUTTONPIN RPI_GPIO_P1_11
+#define BUTTONPIN_V2 RPI_V2_GPIO_P1_11
#define BTNSTATE_IDLE 0
#define BTNSTATE_PRESS 1
#define BTNSTATE_RELEASE 2
@@ -56,6 +58,7 @@ int doRun, pollButton, pollPads;
time_t btnLastTime;
uint8_t btnState;
uint8_t btnPressCtr;
+int buttonPin;
/* Signal callback function */
void sig_handler(int signo) {
@@ -131,7 +134,7 @@ void send_key_event(int fd, unsigned int keycode, int keyvalue) {
void checkButton(int uinh) {
// read the state of the button into a local variable
- uint8_t buttonState = bcm2835_gpio_lev(BUTTONPIN);
+ uint8_t buttonState = bcm2835_gpio_lev(buttonPin);
// three-state machine:
// - press and hold: send "r" key (for rewind function of RetroArch)
@@ -207,6 +210,12 @@ int main(int argc, char *argv[]) {
btnState = BTNSTATE_IDLE;
btnPressCtr = 0;
+ if (get_rpi_revision()==1) {
+ buttonPin = BUTTONPIN;
+ } else {
+ buttonPin = BUTTONPIN_V2;
+ }
+
// check command line arguments
if (argc > 1) {
@@ -240,7 +249,7 @@ int main(int argc, char *argv[]) {
return 1;
// initialize button and LEDs
- bcm2835_gpio_fsel(BUTTONPIN, BCM2835_GPIO_FSEL_INPT);
+ bcm2835_gpio_fsel(buttonPin, BCM2835_GPIO_FSEL_INPT);
/* initialize controller structures with GPIO pin assignments */
@@ -250,11 +259,20 @@ int main(int argc, char *argv[]) {
// pads.data1 = RPI_GPIO_P1_22;
// pads.data2 = RPI_GPIO_P1_15;
- // pin out used pi gamecon GPIO driver
- pads.clock = RPI_GPIO_P1_19;
- pads.strobe = RPI_GPIO_P1_23;
- pads.data1 = RPI_GPIO_P1_07;
- pads.data2 = RPI_GPIO_P1_05;
+ // check board revision and set pins to be used
+ // these are acutally also used by the gamecon driver
+ if (get_rpi_revision()==1)
+ {
+ pads.clock = RPI_GPIO_P1_19;
+ pads.strobe = RPI_GPIO_P1_23;
+ pads.data1 = RPI_GPIO_P1_07;
+ pads.data2 = RPI_GPIO_P1_05;
+ } else {
+ pads.clock = RPI_V2_GPIO_P1_19;
+ pads.strobe = RPI_V2_GPIO_P1_23;
+ pads.data1 = RPI_V2_GPIO_P1_07;
+ pads.data2 = RPI_V2_GPIO_P1_05;
+ }
/* set GPIO pins as input or output pins */
initializePads( &pads );
View
18 supplementary/testButton/Makefile
@@ -0,0 +1,18 @@
+CC=gcc
+CFLAGS=-c -Wall -O3
+LIBS=-lbcm2835
+INCLUDES=-Iinclude
+EXECUTABLE=testButton
+
+testButton: main.o cpuinfo.o
+ $(CC) main.o cpuinfo.o $(LIBS) -o $(EXECUTABLE)
+
+main.o: main.c
+ $(CC) $(CFLAGS) $(INCLUDES) $(LIBS) main.c -o main.o
+
+cpuinfo.o: cpuinfo.c
+ $(CC) $(CFLAGS) $(INCLUDES) $(LIBS) cpuinfo.c -o cpuinfo.o
+
+clean:
+ rm *.o; rm $(EXECUTABLE)
+
View
65 supplementary/testButton/cpuinfo.c
@@ -0,0 +1,65 @@
+/*
+Copyright (c) 2012 Ben Croston
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include "cpuinfo.h"
+
+char *get_cpuinfo_revision(char *revision)
+{
+ FILE *fp;
+ char buffer[1024];
+ char hardware[1024];
+ int rpi_found = 0;
+
+ if ((fp = fopen("/proc/cpuinfo", "r")) == NULL)
+ return 0;
+
+ while(!feof(fp)) {
+ fgets(buffer, sizeof(buffer) , fp);
+ sscanf(buffer, "Hardware : %s", hardware);
+ if (strcmp(hardware, "BCM2708") == 0)
+ rpi_found = 1;
+ sscanf(buffer, "Revision : %s", revision);
+ }
+ fclose(fp);
+
+ if (!rpi_found)
+ revision = NULL;
+ return revision;
+}
+
+int get_rpi_revision(void)
+{
+ char revision[1024] = {'\0'};
+
+ if (get_cpuinfo_revision(revision) == NULL)
+ return -1;
+
+ if ((strcmp(revision, "0002") == 0) ||
+ (strcmp(revision, "1000002") == 0 ) ||
+ (strcmp(revision, "0003") == 0) ||
+ (strcmp(revision, "1000003") == 0 ))
+ return 1;
+ else // assume rev 2 (0004 0005 0006 1000004 1000005 1000006)
+ return 2;
+}
View
23 supplementary/testButton/cpuinfo.h
@@ -0,0 +1,23 @@
+/*
+Copyright (c) 2012 Ben Croston
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+int get_rpi_revision(void);
View
38 supplementary/testButton/main.c
@@ -0,0 +1,38 @@
+#include <bcm2835.h>
+#include "cpuinfo.h"
+
+// Button on RPi pin GPIO 11
+#define PIN RPI_GPIO_P1_11
+#define PIN_V2 RPI_GPIO_V2_P1_11
+
+int main(int argc, char **argv)
+{
+ int usepin;
+
+ if (!bcm2835_init())
+ return 1;
+
+ // check board revision and set the pin to be an output
+ if (get_rpi_revision()==1)
+ {
+ usepin = PIN;
+ } else {
+ usepin = PIN_V2;
+ }
+ bcm2835_gpio_fsel(usepin, BCM2835_GPIO_FSEL_INP);
+
+ while (1)
+ {
+ if (bcm2835_gpio_read(usepin)==LOW)
+ {
+ printf("Button state: NOT PRESSED\n");
+ } else {
+ printf("Button state: PRESSED\n");
+ }
+
+ // wait a bit
+ delay(500);
+ }
+
+ return 0;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.