From 8f5ee9302d40d9b9ae71ca07ce9d7ff2e2e36935 Mon Sep 17 00:00:00 2001 From: Dolf Andringa Date: Mon, 2 Mar 2020 12:11:26 +0800 Subject: [PATCH] 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