Hello,
I'm using the Kernel-3.2.0-psp04.06.00.11 and I have an application that uses the SPI to communicate with a microcontroller via a custom protocol. In this design, the AM335x processor I use is the master.
Before I get to the bug I need to explain the communication protocol I use. When I send a request to the microcontroller, there will be some time for it to process it and have an answer ready to send back. As the slave side can't enable the bus, the solution we use is to read a lot more than what we expect because we don't know when the answer will be ready.
For example, say that I'm expecting a package with 50 bytes. I would send the request message and then try to read 500 bytes, hoping that somewhere inside this big buffer I can find those 50 bytes I need.
The bug happens when the answer is truncated. Again, imagine that it took the microcontroller so much time to process the request that the answer started at byte number 470. When this happens, only 30 bytes would be received and I would need to read the bus again to get the remaining 20 bytes.
Now, the bug: there is ALWAYS one byte missing when the package is truncated. If my package was supposed to have 50 bytes, I get only 49.
Long story short, I found out the reason: the driver uses the file spi-omap2-mcspi.c to implement the transfer function. When I read LESS than 160 bytes, it calls a function that implements a programmed I/O read method, reading one byte at a time from the rx register. If I try to read more than 160 bytes, the transfer function the driver uses implements a direct memory access design to read things.
With an oscilloscope, I found out that FIO reads exactly the number of bytes I asks and copies the same exact number of bytes to the rx buffer. DMA, in the other hand, reads the number of bytes I ask PLUS ONE and copies the right number of bytes to the rx buffer. That means that if I try read(fd,rx_buff,500) I see 501 bytes going through the SPI bus, even though I always get the 500 bytes in rx_buff. This one extra byte is forever lost.
I made a simple program to further illustrate this issue. When I try to read anything from the bus, the microcontroller just sends back one byte and increment the value it sent. The example is shown below:
root@AM335x:~# ./spitest 200
0000 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
0010 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
0020 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
0030 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
0040 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F
0050 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F
0060 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F
0070 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F
0080 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F
0090 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F
0100 A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF
0110 B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF
0120 C0 C1 C2 C3 C4 C5 C6 C7
root@AM335x:~# ./spitest 200
0000 C9 CA CB CC CD CE CF D0 D1 D2 D3 D4 D5 D6 D7 D8
0010 D9 DA DB DC DD DE DF E0 E1 E2 E3 E4 E5 E6 E7 E8
0020 E9 EA EB EC ED EE EF F0 F1 F2 F3 F4 F5 F6 F7 F8
0030 F9 FA FB FC FD FE FF 00 01 02 03 04 05 06 07 08
0040 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18
0050 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28
0060 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38
0070 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48
0080 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58
0090 59 5A 5B 5C 5D 5E 5F 60 61 62 63 64 65 66 67 68
0100 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78
0110 79 7A 7B 7C 7D 7E 7F 80 81 82 83 84 85 86 87 88
0120 89 8A 8B 8C 8D 8E 8F 90
All the program does is read 200 bytes from the bus and print them somewhat like hexdump would. Two consecutive calls shows that I lost the C8 byte.
As I said, I sniffed the communication with an oscilloscope and the missing byte is being sent in the end of the first read call that uses the DMA.
Another test I did was to disable DMA, using PIO regardless of the size I would read. Works like a charm!
This is as far as my current knowledge goes. I don't fully understand how the DMA registers handles the SPI communication and why the heck would it read one extra byte every time...
Can someone help me?
Thanks for reading evething =)
DAVI