Page 1 of 1

SPI Master

Posted: Fri Aug 20, 2021 7:58 pm
by caetanowagner
Hi all.
I think there is something I'm missing.
I'm using
macOS High Sierra version 10.13.6
Visual Studio Code version 1.59.1
ESP-IDF Extension version 1.1.1
ESP-IDF version 4.2.2
Python version 3.9.1
ESP32 chip revision: 1

I'm want esp32 to communicate to RA8875 (TFT display controller) via SPI 4 wires.
RA8875 datasheet shows SPI CPOL = high and CPHA = 1

I can initialise and add device to bus, here's the log
I (355) DisplayTFT: ... Initializing bus.
I (355) DisplayTFT: ... Adding device bus.

When I try to use
  1. ret = spi_device_transmit(spi, &t);
got this
E (455) spi_master: check_trans_valid(661): invalid dev handle
I (455) DisplayTFT: LCD_CmdWrite ESP_ERR_INVALID_ARG

Here's the code
  1. //Pins definition
  2. #define SPI_MISO 19     //SPI miso
  3. #define SPI_MOSI 23     //SPI mosi
  4. #define SPI_CLK 18      //SPI clock
  5. #define SPI_CS 5        //SPI slave select
  6.  
  7. //SPI config
  8. spi_bus_config_t bus_config;
  9. memset(&bus_config, 0, sizeof(spi_bus_config_t));
  10. bus_config.sclk_io_num = SPI_CLK;
  11. bus_config.mosi_io_num = SPI_MOSI;
  12. bus_config.miso_io_num =SPI_MISO;
  13. bus_config.quadwp_io_num = -1; // Not used
  14. bus_config.quadhd_io_num = -1; // Not used
  15. bus_config.max_transfer_sz = 32;
  16. ESP_LOGI(TAG, "... Initializing bus.");
  17. ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &bus_config, 1));
  18.  
  19. spi_device_handle_t handle;
  20. spi_device_interface_config_t dev_config;
  21. dev_config.address_bits = 0;
  22. dev_config.command_bits = 0;
  23. dev_config.dummy_bits = 0;
  24. dev_config.mode = 3;
  25. dev_config.duty_cycle_pos = 0;
  26. dev_config.cs_ena_posttrans = 0;
  27. dev_config.cs_ena_pretrans = 0;
  28. dev_config.clock_speed_hz = 500000;    //500kHz
  29. dev_config.spics_io_num = SPI_CS;
  30. dev_config.queue_size = 1;
  31. dev_config.pre_cb = NULL;
  32. dev_config.post_cb = NULL;
  33. ESP_LOGI(TAG, "... Adding device bus.");
  34. ESP_ERROR_CHECK(spi_bus_add_device(HSPI_HOST, &dev_config, &handle));
  35.  
  36. void LCD_CmdWrite(spi_device_handle_t spi, const uint8_t addr, uint8_t val)
  37. {
  38.     esp_err_t ret;
  39.     spi_transaction_t t;
  40.     char data[3];
  41.     char rx_data[3];
  42.     data[0] = RA8875_Cmd_Write;
  43.     data[1] = addr;
  44.     data[2] = val;
  45.  
  46.     memset(&t, 0, sizeof(t));                   //Zero out the transaction
  47.     t.addr = 0;
  48.     t.cmd   = 0;
  49.     t.flags     = 0;
  50.     t.length    = 8 * 3; // Total data length, in bits NOT number of bytes.
  51.     t.rxlength  = 0; // (0 defaults this to the value of ``length``)
  52.     t.tx_buffer = &data;
  53.     t.rx_buffer = &rx_data;
  54.  
  55.     ret = spi_device_transmit(spi, &t); //Transmit!
  56.     if (ret != ESP_OK)
  57.         ESP_LOGI(TAG, "LCD_CmdWrite %s", esp_err_to_name(ret));
  58. }

I tried to modify these variables
  1. t.addr  = 0;
  2. t.cmd  = 0;
  3. t.flags = 0;
  4.  
  5. dev_config.address_bits = 0;
  6. dev_config.command_bits = 0;
always unsuccessful.

I used this https://docs.espressif.com/projects/esp ... aster.html
and some other examples I found on the net.

Thanks.

Re: SPI Master

Posted: Sat Aug 21, 2021 9:34 am
by Victoria Nope
If I were you and wanted to control such display in some reasonable way, I would, rather than crafting my own, focus on using some ready made graphics library such as LVGL. It is ported for ESP32 and supports your RA8875 controller.

Re: SPI Master

Posted: Sun Aug 22, 2021 8:06 pm
by fasani
I second Victoria recommendation here. I would also like to mention, that many SPI peripherals, need a reset before starting communication.
I checked the ra8875 in LVGL and has one, a part of a memory clear, maybe that's something you need to do additionally before sending your SPI commands.