chiark / gitweb /
wiringPi Version 2 - First commit (of v2)
[wiringPi.git] / wiringPi / wiringPiSPI.c
index 44414985318a3bcee99ebfd38173ea5bea18242e..ded07ba6797a315313316e9998b3b19b2cfdaf0f 100644 (file)
 
 #include <stdint.h>
 #include <fcntl.h>
+#include <errno.h>
+#include <string.h>
 #include <sys/ioctl.h>
 #include <linux/spi/spidev.h>
 
+#include "wiringPi.h"
+
 #include "wiringPiSPI.h"
 
 
 // The SPI bus parameters
 //     Variables as they need to be passed as pointers later on
 
-static char       *spiDev0 = "/dev/spidev0.0" ;
-static char       *spiDev1 = "/dev/spidev0.1" ;
-static uint8_t     spiMode   = 0 ;
-static uint8_t     spiBPW    = 8 ;
-static uint16_t    spiDelay  = 0;
+const static char       *spiDev0  = "/dev/spidev0.0" ;
+const static char       *spiDev1  = "/dev/spidev0.1" ;
+const static uint8_t     spiMode  = 0 ;
+const static uint8_t     spiBPW   = 8 ;
+const static uint16_t    spiDelay = 0 ;
 
 static uint32_t    spiSpeeds [2] ;
 static int         spiFds [2] ;
@@ -95,7 +99,7 @@ int wiringPiSPISetup (int channel, int speed)
   channel &= 1 ;
 
   if ((fd = open (channel == 0 ? spiDev0 : spiDev1, O_RDWR)) < 0)
-    return -1 ;
+    return wiringPiFailure (WPI_ALMOST, "Unable to open SPI device: %s\n", strerror (errno)) ;
 
   spiSpeeds [channel] = speed ;
   spiFds    [channel] = fd ;
@@ -104,14 +108,14 @@ int wiringPiSPISetup (int channel, int speed)
 //     Why are we reading it afterwriting it? I've no idea, but for now I'm blindly
 //     copying example code I've seen online...
 
-  if (ioctl (fd, SPI_IOC_WR_MODE, &spiMode)         < 0) return -1 ;
-  if (ioctl (fd, SPI_IOC_RD_MODE, &spiMode)         < 0) return -1 ;
-
-  if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0) return -1 ;
-  if (ioctl (fd, SPI_IOC_RD_BITS_PER_WORD, &spiBPW) < 0) return -1 ;
+  if (ioctl (fd, SPI_IOC_WR_MODE, &spiMode)         < 0)
+    return wiringPiFailure (WPI_ALMOST, "SPI Mode Change failure: %s\n", strerror (errno)) ;
+  
+  if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0)
+    return wiringPiFailure (WPI_ALMOST, "SPI BPW Change failure: %s\n", strerror (errno)) ;
 
   if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed)   < 0) return -1 ;
-  if (ioctl (fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed)   < 0) return -1 ;
+    return wiringPiFailure (WPI_ALMOST, "SPI Speed Change failure: %s\n", strerror (errno)) ;
 
   return fd ;
 }