Ich hab hier ein komisches verhalten des SPI auf dem Raspi und keine Ahnung wo ich noch suchen soll. Vermutlich ist es irgendeine Dummheit in meinem Code, ein cast Fehler oder irgend so etwas, aber ich weiss nicht wo. Kann sich das mal jemand ansehen. Kurze Beschreibung. Es soll eine Kommunikation zwischen einem RasPi und einem STM32 werden. Da das RasPi nur SPI Master kann und immer den Takt geben will muss der Slave (STM32) an einem Pin des RasPi wackeln, dann sendet das RasPi einen dummy Frame mit lauter Nullen und empfangt die Daten des STM32. Problem: Die Daten die ich empfange sind immer um 1 bis 4 Bytes "geshiftet" Der STM sendet immer: 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 auf dem RasPi kommt das dann folgendermassen an. 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 Auf dem RasPi benutze ich die wiringPi lib, habe es aber auch schon direkt mit ioctl versucht, mit dem selben Ergebniss. Auf dem STM laeuft eigener Code. Wenn ich die Uebertragung zwischen zwei STM32 mache, kommt das raus was ich erwarte. hier der RasPi code
1 | #include <stdio.h> |
2 | #include <inttypes.h> |
3 | #include <string.h> |
4 | #include <errno.h> |
5 | #include <stdlib.h> |
6 | #include "spi_Comm_Handler.h" |
7 | #include <wiringPi.h> |
8 | #include <wiringPiSPI.h> |
9 | |
10 | |
11 | volatile uint8_t dbg_SPIKnockCounter = 0; |
12 | volatile uint8_t transferArray[SPI_TRANSFERDATASIZE] = {0, }; |
13 | |
14 | |
15 | /* ISR for slave knock Pin */
|
16 | void slaveKnockHandler (void) { |
17 | printf("Slave request\n"); |
18 | dbg_SPIKnockCounter++ ; |
19 | flush_Transfer_Buffer(); |
20 | send_SPI_Frame(); |
21 | }
|
22 | |
23 | int init_SPI_Comm_Handler(void) { |
24 | int spidev; |
25 | printf ( "sizeof(transferArray): %d\n", sizeof(transferArray)) ; |
26 | if (wiringPiSetup () < 0) { |
27 | fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ; |
28 | return 0 ; |
29 | }
|
30 | |
31 | spidev = wiringPiSPISetup (0, 500000) ; |
32 | if (spidev == -1) { |
33 | fprintf (stderr, "Unable to setup SPI: %s\n", strerror (errno)) ; |
34 | return 0 ; |
35 | }
|
36 | |
37 | pinMode (SLAVE_KNOCK_PIN, INPUT) ; |
38 | |
39 | if (wiringPiISR (SLAVE_KNOCK_PIN, INT_EDGE_FALLING, &slaveKnockHandler) < 0) { |
40 | fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ; |
41 | return 0 ; |
42 | }
|
43 | |
44 | spi_writeLCD(0, 0, "Hello Raspi here"); |
45 | |
46 | return 1 ; |
47 | }
|
48 | |
49 | void flush_Transfer_Buffer(void){ |
50 | uint8_t c = 0; |
51 | |
52 | for (c = 0; c < SPI_TRANSFERDATASIZE; c++) { |
53 | transferArray[c] = 0; |
54 | }
|
55 | }
|
56 | |
57 | void send_SPI_Frame(void){ |
58 | |
59 | if (wiringPiSPIDataRW (0, (unsigned char *)transferArray, SPI_TRANSFERDATASIZE)) { |
60 | handle_incoming_SPI_Data(); |
61 | } else { |
62 | fprintf (stderr, "Error in Transfer\n") ; |
63 | }
|
64 | }
|
65 | |
66 | void showRData(void) { |
67 | |
68 | uint8_t c = 0; |
69 | |
70 | for (c = 0; c < SPI_TRANSFERDATASIZE; c++) { |
71 | printf(" %d ", transferArray[c]); |
72 | }
|
73 | printf("\n"); |
74 | return; |
75 | |
76 | }
|
77 | |
78 | /*
|
79 | * to prevent recursive traffic
|
80 | * here we cannot call to transfer another data
|
81 | */
|
82 | void handle_incoming_SPI_Data(void) { |
83 | showRData(); |
84 | }
|