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

Code: Untitled.c Select all

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

Code: Untitled.c Select all


//Pins definition
#define SPI_MISO 19 //SPI miso
#define SPI_MOSI 23 //SPI mosi
#define SPI_CLK 18 //SPI clock
#define SPI_CS 5 //SPI slave select

//SPI config
spi_bus_config_t bus_config;
memset(&bus_config, 0, sizeof(spi_bus_config_t));
bus_config.sclk_io_num = SPI_CLK;
bus_config.mosi_io_num = SPI_MOSI;
bus_config.miso_io_num =SPI_MISO;
bus_config.quadwp_io_num = -1; // Not used
bus_config.quadhd_io_num = -1; // Not used
bus_config.max_transfer_sz = 32;
ESP_LOGI(TAG, "... Initializing bus.");
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &bus_config, 1));

spi_device_handle_t handle;
spi_device_interface_config_t dev_config;
dev_config.address_bits = 0;
dev_config.command_bits = 0;
dev_config.dummy_bits = 0;
dev_config.mode = 3;
dev_config.duty_cycle_pos = 0;
dev_config.cs_ena_posttrans = 0;
dev_config.cs_ena_pretrans = 0;
dev_config.clock_speed_hz = 500000; //500kHz
dev_config.spics_io_num = SPI_CS;
dev_config.queue_size = 1;
dev_config.pre_cb = NULL;
dev_config.post_cb = NULL;
ESP_LOGI(TAG, "... Adding device bus.");
ESP_ERROR_CHECK(spi_bus_add_device(HSPI_HOST, &dev_config, &handle));

void LCD_CmdWrite(spi_device_handle_t spi, const uint8_t addr, uint8_t val)
{
esp_err_t ret;
spi_transaction_t t;
char data[3];
char rx_data[3];
data[0] = RA8875_Cmd_Write;
data[1] = addr;
data[2] = val;

memset(&t, 0, sizeof(t)); //Zero out the transaction
t.addr = 0;
t.cmd = 0;
t.flags = 0;
t.length = 8 * 3; // Total data length, in bits NOT number of bytes.
t.rxlength = 0; // (0 defaults this to the value of ``length``)
t.tx_buffer = &data;
t.rx_buffer = &rx_data;

ret = spi_device_transmit(spi, &t); //Transmit!
if (ret != ESP_OK)
ESP_LOGI(TAG, "LCD_CmdWrite %s", esp_err_to_name(ret));
}

I tried to modify these variables

Code: Untitled.c Select all

t.addr  = 0;
t.cmd = 0;
t.flags = 0;

dev_config.address_bits = 0;
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.