Page 1 of 1

SPI receive -> strange Byte shift

Posted: Sat Jul 20, 2013 9:33 pm
by jgnoss
I've a problem receiving data on a RasPi using wiringPi.
I think it's not wiringPi specific, because wiringPi just use ioctl as
another SPI examples do.
I tried a lot and now I'm out of ideas, you guys may have another Idea what's
happening there.

Background:
A STM32 and a RasPi need data exchange. Raspi is Master STM32 is slave.
On occasions the STM need to send data, so he moves a pin on the raspi,
and the raspi sends a dummy frame and receives data in return.

for test purpose, the STM sends always same data:
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

and the raspi receives it as:

Slave request
48 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
Slave request
83 48 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
Slave request
82 83 48 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
Slave request
48 82 83 48 48 49 50 51 52 53 54 55 56 57 58 59 60 61
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
Slave request
48 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
Slave request
83 48 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
Slave request
82 83 48 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
Slave request
48 82 83 48 48 49 50 51 52 53 54 55 56 57 58 59 60 61
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
Slave request
48 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
Slave request
83 48 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

above are sequential sends from the STM
There is a strange 4 times byte shift and then it starts again

Making same communication on 2 STM's, works fine.
Sending from RasPi to STM also works fine.
Do you have any idea?

here the RasPi code

Code: Select all

#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "spi_Comm_Handler.h"
#include <wiringPi.h>
#include <wiringPiSPI.h>


volatile uint8_t dbg_SPIKnockCounter = 0;
volatile uint8_t transferArray[SPI_TRANSFERDATASIZE] = {0, };


/* ISR for slave knock Pin */
void slaveKnockHandler (void) {
  printf("Slave request\n");
  dbg_SPIKnockCounter++ ;
  flush_Transfer_Buffer();
  send_SPI_Frame();
}

int init_SPI_Comm_Handler(void) {
  int spidev;
  printf ( "sizeof(transferArray): %d\n", sizeof(transferArray)) ;
  if (wiringPiSetup () < 0) {
    fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ;
    return 0 ;
  }  
      
  spidev = wiringPiSPISetup (0, 500000) ;
  if (spidev == -1) {
    fprintf (stderr, "Unable to setup SPI: %s\n", strerror (errno)) ;
    return 0 ;  
  }
  
  pinMode (SLAVE_KNOCK_PIN,  INPUT) ;
    
  if (wiringPiISR (SLAVE_KNOCK_PIN, INT_EDGE_FALLING, &slaveKnockHandler) < 0) {
    fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ;
    return 0 ;
  }  
  
  spi_writeLCD(0, 0, "Hello Raspi here");
  
  return 1 ;
}

void flush_Transfer_Buffer(void){
  uint8_t c = 0;
  
  for (c = 0; c < SPI_TRANSFERDATASIZE; c++) {
    transferArray[c] = 0;
  }
}

void send_SPI_Frame(void){

  if (wiringPiSPIDataRW (0, (unsigned char *)transferArray, SPI_TRANSFERDATASIZE)) {
    handle_incoming_SPI_Data();
  } else {
    fprintf (stderr, "Error in Transfer\n") ;
  }
}

void showRData(void) {
  
  uint8_t c = 0;
  
  for (c = 0; c < SPI_TRANSFERDATASIZE; c++) {
    printf(" %d ", transferArray[c]);
  }
  printf("\n");
  return;
  
}

/* 
 * to prevent recursive traffic 
 * here we cannot call to transfer another data
 */
void handle_incoming_SPI_Data(void) {
  showRData();
}

Re: SPI receive -> strange Byte shift

Posted: Mon Jul 22, 2013 12:56 pm
by PiHeich
i have the same problem, look http://www.raspberrypi.org/phpBB3/viewt ... 33&t=49663
.....

Re: SPI receive -> strange Byte shift

Posted: Mon Jul 29, 2013 8:49 am
by superp
Hello, can you post stm32 spi slave com code please? i have a similar problem. it seems that if i make a communication byte by byte it works, but when i try to send the whole frame array stm32 is not replying correctly.
thanks

Re: SPI receive -> strange Byte shift

Posted: Fri Nov 17, 2017 11:39 am
by April2727
hello have you solved the problem? My project is just like yours. STM32 communicate with Pi through SPI. I also add an extra pin to send request to Pi to trigger the Interrupt in PI and then start to send data. My code is as followed. As I test , the interrupt can be triggered, but wiringPiSPIDataRW(spiChannel,(unsigned char*)data,length) doesn't work. I cannot see the data sent by STM32.

#define ISR_PIN_0 0 /*Interrupt pin*/
#define PIN_CE0 10 /*slave select pin*/
#define test_PIN_3 3

unsigned char data[2]={20,20};
int spiChannel = 0;
int clock=500000;
int length=2;


/*slave send request*/
void myInterrupt(void)
{

/*transfer data*/
wiringPiSPIDataRW(spiChannel,(unsigned char*)data,length); /*deactivate slave select after the transport process is ended*/
delayMicroseconds (550);

}
int main(void)
{

int spi_0;
/*initialization library wiringPi*/
if (wiringPiSetup () < 0)
{
fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ;
return 0 ;
}
spi_0 = wiringPiSPISetup(spiChannel,clock); //initialize a channel
if (spi_0==-1)
{
fprintf (stderr, "Unable to setup SPI: %s\n", strerror (errno)) ;
return 0 ;
}

pinMode(ISR_PIN_0,INPUT);//set PIN 0 as Input to receive slave request

/*Interrupt*/
if (wiringPiISR(ISR_PIN_0,INT_EDGE_BOTH, &myInterrupt)<0)
{
fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ;
return 0 ;
}

while(1)
{delay(100);}
}

Re: SPI receive -> strange Byte shift

Posted: Fri Nov 17, 2017 2:59 pm
by jgnoss
The really problem was the Kernel module on the Pi.
At the time of my post, nobody could answer and I switched to
I2C, but there was a similar problem in the kernel module of the Pi.
After some hot discussions on the kernel mailing lists, we found a
workaround and included it in the kernel module.
The actual debian and raspbia Kernel should have the fixes included.

If you already use the actual Kernel, put attention to the speed of your
communication between the STM and the RasPi. Not all speeds the
STM can do, is understandable for the RasPi. It has to be a match with
the speed, your RasPi runs on.

BTW, we don't use wiringPi any more but original C API.



Ju

Re: SPI receive -> strange Byte shift

Posted: Sun Jan 07, 2018 11:14 pm
by April2727
jgnoss wrote:
Fri Nov 17, 2017 2:59 pm
The really problem was the Kernel module on the Pi.
At the time of my post, nobody could answer and I switched to
I2C, but there was a similar problem in the kernel module of the Pi.
After some hot discussions on the kernel mailing lists, we found a
workaround and included it in the kernel module.
The actual debian and raspbia Kernel should have the fixes included.

If you already use the actual Kernel, put attention to the speed of your
communication between the STM and the RasPi. Not all speeds the
STM can do, is understandable for the RasPi. It has to be a match with
the speed, your RasPi runs on.

BTW, we don't use wiringPi any more but original C API.



Ju
Thanks so much for the reply. The weird thing is that my Pi can receive the data from STM32 totally right. But when PI send data to STM, STM can only receive half of the data. Even though I abandoned wiringPi and instead I used original C.

for example, the sending data is an array[10]={1,2,3,4,5,6,7,8,9,10};
the STM can only receive 1 3 5 7 9

I don't quite understand the kernel thing. could explain it more?


Lin