From 8f5ee9302d40d9b9ae71ca07ce9d7ff2e2e36935 Mon Sep 17 00:00:00 2001 From: Dolf Andringa Date: Mon, 2 Mar 2020 12:11:26 +0800 Subject: [PATCH 1/3] first stab --- wiringPi/wiringPiSPI.c | 15 +++++++++++---- wiringPi/wiringPiSPI.h | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/wiringPi/wiringPiSPI.c b/wiringPi/wiringPiSPI.c index f806071..3cdbe3d 100644 --- a/wiringPi/wiringPiSPI.c +++ b/wiringPi/wiringPiSPI.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -39,8 +40,6 @@ // 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/spidev1.0" ; static const uint8_t spiBPW = 8 ; static const uint16_t spiDelay = 0 ; @@ -97,14 +96,22 @@ int wiringPiSPIDataRW (int channel, unsigned char *data, int len) ********************************************************************************* */ -int wiringPiSPISetupMode (int channel, int speed, int mode) +void getDevice(char* spidev, int channel, int port) { + sprintf(spidev, "/dev/spidev/%i.%i", channel, port); +} + +int wiringPiSPISetupMode (int channel, int speed, int mode, int port) { int fd ; 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) + static char spidev[14]; + + getDevice(spidev, channel, port); + + if ((fd = open (spidev, O_RDWR)) < 0) return wiringPiFailure (WPI_ALMOST, "Unable to open SPI device: %s\n", strerror (errno)) ; spiSpeeds [channel] = speed ; diff --git a/wiringPi/wiringPiSPI.h b/wiringPi/wiringPiSPI.h index 3980321..962f025 100644 --- a/wiringPi/wiringPiSPI.h +++ b/wiringPi/wiringPiSPI.h @@ -28,7 +28,7 @@ extern "C" { int wiringPiSPIGetFd (int channel) ; int wiringPiSPIDataRW (int channel, unsigned char *data, int len) ; -int wiringPiSPISetupMode (int channel, int speed, int mode) ; +int wiringPiSPISetupMode (int channel, int speed, int mode, int port=0) ; int wiringPiSPISetup (int channel, int speed) ; #ifdef __cplusplus From 5e85a44f9327c4aeb9db939b8dd1d1ed17d05b4e Mon Sep 17 00:00:00 2001 From: Dolf Andringa Date: Mon, 2 Mar 2020 04:46:27 +0000 Subject: [PATCH 2/3] fix argument error --- wiringPi/wiringPiSPI.c | 4 ++-- wiringPi/wiringPiSPI.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/wiringPi/wiringPiSPI.c b/wiringPi/wiringPiSPI.c index 3cdbe3d..ff1ca66 100644 --- a/wiringPi/wiringPiSPI.c +++ b/wiringPi/wiringPiSPI.c @@ -100,7 +100,7 @@ void getDevice(char* spidev, int channel, int port) { sprintf(spidev, "/dev/spidev/%i.%i", channel, port); } -int wiringPiSPISetupMode (int channel, int speed, int mode, int port) +int wiringPiSPISetupMode (int channel, int port, int speed, int mode) { int fd ; @@ -140,5 +140,5 @@ int wiringPiSPISetupMode (int channel, int speed, int mode, int port) int wiringPiSPISetup (int channel, int speed) { - return wiringPiSPISetupMode (channel, speed, 0) ; + return wiringPiSPISetupMode (channel, 0, speed, 0) ; } diff --git a/wiringPi/wiringPiSPI.h b/wiringPi/wiringPiSPI.h index 962f025..62c7aed 100644 --- a/wiringPi/wiringPiSPI.h +++ b/wiringPi/wiringPiSPI.h @@ -28,7 +28,7 @@ extern "C" { int wiringPiSPIGetFd (int channel) ; int wiringPiSPIDataRW (int channel, unsigned char *data, int len) ; -int wiringPiSPISetupMode (int channel, int speed, int mode, int port=0) ; +int wiringPiSPISetupMode (int channel, int port, int speed, int mode) ; int wiringPiSPISetup (int channel, int speed) ; #ifdef __cplusplus From 5525b6250f9eccb5b7f705df3249d9a4bee24060 Mon Sep 17 00:00:00 2001 From: Dolf Andringa Date: Mon, 2 Mar 2020 06:05:04 +0000 Subject: [PATCH 3/3] Correct port number --- wiringPi/wiringPiSPI.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/wiringPi/wiringPiSPI.c b/wiringPi/wiringPiSPI.c index ff1ca66..e65da1e 100644 --- a/wiringPi/wiringPiSPI.c +++ b/wiringPi/wiringPiSPI.c @@ -97,7 +97,7 @@ int wiringPiSPIDataRW (int channel, unsigned char *data, int len) */ void getDevice(char* spidev, int channel, int port) { - sprintf(spidev, "/dev/spidev/%i.%i", channel, port); + sprintf(spidev, "/dev/spidev%i.%i", channel, port); } int wiringPiSPISetupMode (int channel, int port, int speed, int mode) @@ -110,6 +110,7 @@ int wiringPiSPISetupMode (int channel, int port, int speed, int mode) static char spidev[14]; getDevice(spidev, channel, port); + printf("Opening device %s\n", spidev); if ((fd = open (spidev, O_RDWR)) < 0) return wiringPiFailure (WPI_ALMOST, "Unable to open SPI device: %s\n", strerror (errno)) ;