I'm having trouble reading from an SPI bus, specifically an ILI9341 display adapter (i've had the same trouble with other devices as well though). When I attempt to read, all I ever get back are zeroes. I can write just fine.
I am using an ESP-WROVER-KIT v4.1 with an integrated ILI9341 display, so I know the wiring is correct. According to the documentation my configured MISO line is correct. Again, I write data just fine, so MOSI is good as well.
Basically, I set the address window, initiate a memory read (0x2E) and read a dummy parameter (which is always zero for me) and then read data. This is being done as separate transactions. When I write this way I have no trouble (except there is no dummy parameter on writes). Unfortunately, again I get zeroes when I read.
The complete code for reproducing this is as short as I can make it and yet too long for this forum, so I'm posting a link to the relevant .c file here that will reproduce the problem.
ili9341_read_rep/main.c at master · codewitch-honey-crisis/ili9341_read_rep · GitHub[
^]
That repo also contains a full platformio project if you need to run it. Just back up to the main repo link.
An excerpt of the code is below
Here is a link to the datasheet for reference
https://cdn-shop.adafruit.com/datasheets/ILI9341.pdf The relevant pages are 116 and 117. I do not think the datasheet will uncover the core issue but I'm providing it anyway. I think the issue is the way I'm calling the API, but I can't be sure. There are no good examples of reads in the ESP-IDF SDK except from an NVS thing which uses an entirely different SPI configuration (10 command bits, address bits, HALF_DUPLEX flag, etc) so it's not very useful to me.
I've also tried looking at Arduino code to do this but the SPI API is too different for me to make heads or tails of it enough to translate it to the ESP-IDF
Perhaps my code above needs some minor corrections? I'm hoping that's the case. Any help would be greatly appreciated.
I'm trying to ask this on stack-overflow as well, but it looks like my questions all get downvoted and I'll be blocked soon. I don't get that site.
What I have tried:
From main.c (see the full code at the link)
spi_device_handle_t handle;
ESP_ERROR_CHECK(spi_bus_add_device(HOST,&devcfg,&handle));
init_display(handle);
uint16_t fill[320];
memset(fill,0xFF,320*2);
set_window(handle,0,0,319,239);
uint8_t cmd = 0x2C; send(handle,&cmd,1,true);
for(int y=0;y<240;++y) {
send(handle,(uint8_t*)fill,320*2,false);
}
set_window(handle,0,0,319,239);
cmd = 0x2E; send(handle,&cmd,1,true);
retr(handle,&cmd,1,false);
printf("dummy value: 0x");
print_hex(&cmd,1);
printf("\r\n");
for(int y=0;y<240;++y) {
size_t sz = retr(handle,(uint8_t*)fill,320*2,false);
print_hex((uint8_t*)fill,sz);
printf("\r\n");
vTaskDelay(1);
}
Here's how I set up the read (the retr() function)
size_t retr(spi_device_handle_t handle, uint8_t* data,size_t size,bool cmd) {
if(0<size) {
spi_transaction_t t;
memset(&t,0,sizeof(t));
t.rx_buffer = data;
t.length = t.rxlength = 8*size;
t.user = (void*)!cmd;
ESP_ERROR_CHECK(spi_device_polling_transmit(handle,&t));
return (t.rxlength+7)/8;
}
return 0;
}