WiringOP-Zero icon indicating copy to clipboard operation
WiringOP-Zero copied to clipboard

spidev1.0 not working

Open batmaca opened this issue 7 years ago • 7 comments

Hi,

I've been trying on a project. To do so, I needed this library and my orange pi zero's SPI1 pins. Unfortunately I haven't been succeeded. I thought that something was wrong about opi but afterwards I wired my SCLK pin to oscilloscope and watch the signals.

The problem is not about OPi zero, must be about this library. Dont know what yet, but hope gonna find out. Anyone can help me or got any idea how to fix this?

batmaca avatar Sep 07 '17 12:09 batmaca

I have the same problem... I solve it temporary by this way: in wiringPiSPISetup function i changed string:

 if ((fd = open (channel == 0 ? spiDev0 : spiDev1, O_RDWR)) < 0)
   return wiringPiFailure (WPI_ALMOST, "Unable to open SPI device: %s\n", strerror (errno)) ;

for this:

if (channel == 0) {
   if ((fd = open (spiDev0, O_RDWR)) < 0)
     return wiringPiFailure (WPI_ALMOST, "Unable to open SPI device: %s\n", strerror (errno)) ;
 };

 if (channel == 1) {
   if ((fd = open (spiDev1, O_RDWR)) < 0)
     return wiringPiFailure (WPI_ALMOST, "Unable to open SPI device: %s\n", strerror (errno)) ;
 };

So /dev/spi1.0 is opened now. Looks like some problems with ?...:... consturction here... Of course you need to rebuild library...

ghost avatar Sep 22 '17 05:09 ghost

@vr-mth Could you try like this:

 char *spiDev = (channel == 0 ? spiDev0 : spiDev1);
 if ((fd = open (spiDev, O_RDWR)) < 0)
   return wiringPiFailure (WPI_ALMOST, "Unable to open SPI device: %s\n", strerror (errno)) ;

jackkum avatar Sep 22 '17 05:09 jackkum

[Compile] wiringPiSPI.c
wiringPiSPI.c: In function ‘wiringPiSPISetup’:
wiringPiSPI.c:101:17: warning: initialization discards ‘const’ qualifier from pointer target type
  char *spiDev = (channel == 0 ? spiDev0 : spiDev1);

But its compiled and working

ghost avatar Sep 22 '17 05:09 ghost

2jackkum - thanks for pretty solution. Helped this one:

  const char *spiDev = (channel == 0 ? spiDev0 : spiDev1);
  if ((fd = open (spiDev, O_RDWR)) < 0)
    return wiringPiFailure (WPI_ALMOST, "Unable to open SPI device: %s\n", strerror (errno)) ;

ghost avatar Sep 22 '17 06:09 ghost

I tried your solutions but, still got the same problem.

kanalka@orangepizero:~$ sudo gcc -o test test.c -lwiringPi -pthread kanalka@orangepizero:~$ sudo ./test Unable to open SPI device: No such file or directory

And here is the my test code. Where am i doing wrong?

` #include <stdio.h> #include <string.h> #include <wiringPi.h> #include <wiringPiSPI.h>

#define LED_PIN 7 #define BERKE_SPI_PORT_INDEX 1 #define BERKE_BUF_LEN 50

extern char *spiDev1; unsigned char bufferBerke[BERKE_BUF_LEN];

int main(void){

unsigned char iMain;
unsigned char pinValue = LOW;
wiringPiSetup();
wiringPiSPIGetFd(BERKE_SPI_PORT_INDEX);
wiringPiSPISetup(BERKE_SPI_PORT_INDEX,1000000);
pinMode(LED_PIN,OUTPUT);
for(iMain = 0; iMain < BERKE_BUF_LEN; iMain++){
	if(iMain%2)
		bufferBerke[iMain] = 0xFF;
	else
		bufferBerke[iMain] = 0x00;
}
for(;;){
	wiringPiSPIDataRW(BERKE_SPI_PORT_INDEX, bufferBerke, BERKE_BUF_LEN);
	if(pinValue == LOW){
		digitalWrite(LED_PIN, HIGH);
		pinValue = HIGH;
	}else {
		digitalWrite(LED_PIN, LOW);
		pinValue = LOW;
	}
	delay(500);
}
return 0;

} `

batmaca avatar Sep 22 '17 07:09 batmaca

Wow... you have to recompile library. Also you should to check script.bin file in /boot for spi1 is enabled.

Also, in program you need only 2 functions to use:

  1. wiringPiSPISetup
  2. wiringPiSPIDataRW

All another (extern char *spiDev1;, getFd ena etc) - is not needed...

ghost avatar Sep 22 '17 07:09 ghost

To enable the auxiliary SPI device (three slave selects) add the line dtoverlay=spi1-3cs to /boot/config.txt. You will need to reboot to get everything working well.

To view the list do ls /dev/spidev*

and you are bound to see something like this /dev/spidev0.0 /dev/spidev0.1 /dev/spidev1.0 /dev/spidev1.1 /dev/spidev1.2

rocksyne avatar Dec 09 '17 05:12 rocksyne