Browse Source

Bugfix: bitfury: Handle SPI init failure cleanly

Luke Dashjr 12 years ago
parent
commit
3fd7d407d2
3 changed files with 19 additions and 5 deletions
  1. 2 1
      libbitfury.c
  2. 14 3
      spidevc.c
  3. 3 1
      spidevc.h

+ 2 - 1
libbitfury.c

@@ -145,7 +145,8 @@ int detect_chip(int chip_n) {
 	ms3_compute(&atrvec[0]);
 	ms3_compute(&atrvec[20]);
 	ms3_compute(&atrvec[40]);
-	spi_init();
+	if (!spi_init())
+		return 0;
 
 
 	spi_clear_buf();

+ 14 - 3
spidevc.c

@@ -21,6 +21,8 @@
  */
 
 #include "spidevc.h"
+
+#include <stdbool.h>
 #include <sys/mman.h>
 #include <stdint.h>
 #include <unistd.h>
@@ -42,14 +44,23 @@
 
 static volatile unsigned *gpio;
 
-void spi_init(void)
+bool spi_init(void)
 {
 	int fd;
 	fd = open("/dev/mem",O_RDWR|O_SYNC);
-	if (fd < 0) { perror("/dev/mem trouble"); exit(1); }
+	if (fd < 0)
+	{
+		perror("/dev/mem trouble");
+		return false;
+	}
 	gpio = mmap(0,4096,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0x20200000);
-	if (gpio == MAP_FAILED) { perror("gpio mmap trouble"); exit(1); }
+	if (gpio == MAP_FAILED)
+	{
+		perror("gpio mmap trouble");
+		return false;
+	}
 	close(fd);
+	return true;
 }
 
 #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))

+ 3 - 1
spidevc.h

@@ -1,8 +1,10 @@
 #ifndef SPIDEVC_H
 #define SPIDEVC_H
 
+#include <stdbool.h>
+
 /* Initialize SPI using this function */
-void spi_init(void);
+bool spi_init(void);
 
 /* TX-RX single frame */
 int spi_txrx(const char *wrbuf, char *rdbuf, int bufsz);