SPI receive -> strange Byte shift
Posted: Sat Jul 20, 2013 9:33 pm
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
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();
}