SPI always sends and receives at the same time. The 'transfer' where you use an array and a length replaces each byte being sent with the corresponding received byte.
When you send 'request' you should be aware that whatever was in the array will be replaced with whatever the other end sends when it has no data to send (typically zeroes). If you try to send that array again you will probably be sending zeroes. Re-fill the 'request' array before each send.
When you receive 'result', whatever values were in the array will be sent out. You should probably set the array contents to zeroes before you do the receive. Note: The datasheet for the device might say what dummy values to send when receiving data. If it does specify, use that value instead of zero.
My experience with SPI at the lower level is from using the ESP32 SPI API.
Still SPI is SPI.
SPI sends and receives in bytes, uint_8t or int8_t.
If one bit or 12 bits are sent, the data is going to be sent as a byte(s). When I worked with writing a ESP32 SPI library for the LSM9DS1, one of its data sends was 18 bits in size. Boy what a hair-pull that was, till I figured that I will still be getting bytes from the device but one of the bytes will not be completely full of data. And then to recreate the number, on a 32 bit MCU, not all of the 32 bits hold valid data.
Yeah actually I was wrong on understanding the use of SPI.transfer(buff,size). I believed that the buffer is an array of multiples uint16_t but it's actually a chain of bits. So I changed my idea to apply the following technique that I learnt from another topic:
- I send firstly 3 datas (uint16_t) in the request by SPI.transfer16(data0-1-2) and no need to receive anything.
- Then I send the 4th data by : uint16_t recv0 = SPI.transfer16(data3), in which recv0 is the first result I expect to receive.
- Then I will receive next 11 results by sending 0 to DSP (in a for loop):
uint16_t recv(1->11) = SPI.transfer16(0)
Could you please confirm this technique ? Somehow, I have a problem that (as explained in others replies) : I have only 3 connectors in DSP card to connect to Arduino UNO, so I used SCK, MISO, MOSI and no place for SS. Is it possible to transfer data without using SS as I have only one slave ?
Thank you again !