Does multi-byte SPI transfer() work?
Posted: 30 Jan 2021 05:36
It could just be me but I'm struggling to get SPI.transfer(buffer,count) to work.
I have SPI working in general and can successfully execute commands on the slave device (a display). I can set the screen color to whatever I like using either SPI.transfer(byte) or SPI.transfer(word) (16 bit color values). But when I try to use the multibyte version of transfer, it gives me the wrong result (white screen instead of black).
After telling the display I am doing a write to screen memory, I am allowed to send as many 16-bit color values (one per pixel) as I want so a bulk transfer will make things much faster. But for some reason even though I initialize a buffer to all zeros (black), when I send it to the display somehow the values are 0xFFFF (white) when they started as black (0x0000).
For example, if I transfer these ways, it works:
uint8_t color = 0;
SPI.transfer(color);
SPI.transfer(color);
or
uint16_t color = 0;
SPI.transfer16(color);
BUT if I try the following I get WHITE instead of black in each pixel written.
uint8_t buffer = {0,0};
SPI.transfer(buffer,2);
I don't really understand it as the SPI header/cpp seems to suggest that the single byte and 16-bit versions of transfer just use the multi-byte version under the covers so it makes no sense to me that the multi-byte one is broken.
Any ideas? Maybe something really stupid I've missed? I've tried lots of different combinations such as this (which also WORKS):
uint8_t buffer = {0,0};
uint16_t color = (buffer[0] << 8) + (buffer[1] & 0xFF);
SPI.transfer16(color);
So it seems like buffer[] has the right contents in it but somehow using it directly isn't working with multibyte transfer(). I did see in some code online for a Zuno project that it said the multibyte transfer() had issues but no idea how old that is or if still correct (or if they are even the issues I'm having).
Any ideas would be appreciated.
XM
I have SPI working in general and can successfully execute commands on the slave device (a display). I can set the screen color to whatever I like using either SPI.transfer(byte) or SPI.transfer(word) (16 bit color values). But when I try to use the multibyte version of transfer, it gives me the wrong result (white screen instead of black).
After telling the display I am doing a write to screen memory, I am allowed to send as many 16-bit color values (one per pixel) as I want so a bulk transfer will make things much faster. But for some reason even though I initialize a buffer to all zeros (black), when I send it to the display somehow the values are 0xFFFF (white) when they started as black (0x0000).
For example, if I transfer these ways, it works:
uint8_t color = 0;
SPI.transfer(color);
SPI.transfer(color);
or
uint16_t color = 0;
SPI.transfer16(color);
BUT if I try the following I get WHITE instead of black in each pixel written.
uint8_t buffer = {0,0};
SPI.transfer(buffer,2);
I don't really understand it as the SPI header/cpp seems to suggest that the single byte and 16-bit versions of transfer just use the multi-byte version under the covers so it makes no sense to me that the multi-byte one is broken.
Any ideas? Maybe something really stupid I've missed? I've tried lots of different combinations such as this (which also WORKS):
uint8_t buffer = {0,0};
uint16_t color = (buffer[0] << 8) + (buffer[1] & 0xFF);
SPI.transfer16(color);
So it seems like buffer[] has the right contents in it but somehow using it directly isn't working with multibyte transfer(). I did see in some code online for a Zuno project that it said the multibyte transfer() had issues but no idea how old that is or if still correct (or if they are even the issues I'm having).
Any ideas would be appreciated.
XM