0

I'm trying to program an Asus tinker board S as a SPI master, and the communication from the master to the slave microcontroller works as expected, and i am able to send data from the Asus tinker board S SPI master to the STM32f140 SPI slave. I want to be able to read the data both ways, and i've tried a few things, but none of them seem to work. I am using the WiringPi library (https://github.com/TinkerBoard/gpio_lib_c)

When i use an analog discovery to look at the connection between the two microcontrollers, the data is fine both ways - both the RX and TX data is correct, so the issue must be in reading the data on my master. My code on my tinkerboard looks like this:


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>

//#include <fcntl.h>
//#include <sys/ioctl.h>
//#include <linux/spi/spidev.h>

#include <wiringPi.h>
#include <wiringPiSPI.h>
#include <wiringTB.h> 

int main()
  {
 
wiringPiSetupGpio();

//asus_set_pin_mode(257,OUTPUT); //MOSI OUT
//asus_set_pin_mode(254,OUTPUT); //CLK OUT
asus_set_pin_mode(255, OUTPUT); //CS OUT
//asus_digitalWrite(257, LOW);
//asus_digitalWrite(254, LOW);
asus_digitalWrite(255,HIGH);
asus_set_pin_mode(256,INPUT);  
 
         uint8_t b=0;
        int spimode = 3;
        int spichannel = 1;
        int spispeed = 8000000;
uint8_t i=0;
int x;


 
        int fd = wiringPiSPISetupMode(spichannel,spispeed,spimode);
 

        if(fd==-1) {
            printf("Spi fail \n");
            return -1;
        }
 
        printf("\n SPI success \n");
 

        delayMicroseconds(1000);

while(1){

        unsigned char buf[4] = {1, 9, 0, 0};
        int8_t Req_header_MOSI[4] = {1, 9, 0, 0};
        unsigned char inbuf[4];


asus_digitalWrite(255,LOW);

for(i=0;i<4;i++){
     inbuf[i] = wiringPiSPIDataRW(spichannel, &buf[i], 1);

printf("%d",inbuf[i]);

}
delayMicroseconds(100);



 delayMicroseconds(1000);

asus_digitalWrite(255,HIGH);



printf("\n");
        delayMicroseconds(1000);

 
      }
}
 




On my slave i read 1 9 0 0 , as expected, and in return im trying to send 5 6 7 8 but recieve 1 1 1 1 on the master.

Thank you in advance

  • How do you know that the data is correct, what does MOSI and MISO look like on your scope? What about the usual SPI FAQ: where do you set clock polarity and phase and what does the system expect? How many hardware buffers have you got on this thing? What's the underlying guarantee that this lib can catch all SPI data without buffer overruns? – Lundin Feb 23 '21 at 11:35
  • Hi, I'm using the Waveforms SPI protocol do look on the connection, and it shows the data read in hexadecimal as 1 9 0 0 and 5 6 7 8 - My clock is set to active low, polarity 1 and phase 1. The slave expects the same. I'm not so sure i understand your question about the guarantee from the library - Do i need to do something other than just using the functions from the library? – Anton Bjerg Brodtkorb Feb 23 '21 at 11:57
  • 1
    You need an oscilloscope in order to use SPI in meaningful ways. For the same reason as you aren't writing the source code while wearing a blindfold. To begin with, these speeds are rather sensitive in terms of EMC so it's important to verify that the signals don't look like crap. As for buffers, receiving a stream of bytes at 8Mbps requires some manner of hardware buffering or there is just no way a PC can keep up. As for clock settings, maybe make a comment explaining that magic number 3 means CPOL=1 CPHA=1. – Lundin Feb 23 '21 at 12:15

0 Answers0