Browse Source

Merge pull request #11 from dolfandringa/support_multiple_spi_ports

Support multiple spi ports
pull/15/head
Orange Pi 5 years ago committed by GitHub
parent
commit
057e908d1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      wiringPi/wiringPiSPI.c
  2. 2
      wiringPi/wiringPiSPI.h

18
wiringPi/wiringPiSPI.c

@ -27,6 +27,7 @@
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <asm/ioctl.h>
#include <linux/spi/spidev.h>
@ -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,23 @@ 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 port, int speed, int mode)
{
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);
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)) ;
spiSpeeds [channel] = speed ;
@ -133,5 +141,5 @@ int wiringPiSPISetupMode (int channel, int speed, int mode)
int wiringPiSPISetup (int channel, int speed)
{
return wiringPiSPISetupMode (channel, speed, 0) ;
return wiringPiSPISetupMode (channel, 0, speed, 0) ;
}

2
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 port, int speed, int mode) ;
int wiringPiSPISetup (int channel, int speed) ;
#ifdef __cplusplus

Loading…
Cancel
Save