I cann't SPI(VSPI) communication between external ADC and esp32-wrover-e with esp32-idf

dallim30
Posts: 22
Joined: Thu Apr 29, 2021 1:35 am

I cann't SPI(VSPI) communication between external ADC and esp32-wrover-e with esp32-idf

Postby dallim30 » Sun Oct 17, 2021 4:53 am

Hello
I am going to SPI (VSPI) communicate with external ADC using esp32-wrover-e and esp32-idf
It works well in arduino code as below, however, in esp32-idf, I cannot read or write the register value.
I tried to approach using some methods, but they did not communicate normally.
I want to know what's wrong with my code on esp32-idf.

thanks

------------------------------------------------
1. Arudino code (good works)
------------------------------------------------
int CS = 5;
unsigned char g_cData = 0x01;

void setup()
{
pinMode(CS, OUTPUT); // Chip Select pin is output
digitalWrite(CS, HIGH); // initial state is High
Serial.begin(115200);

SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.begin();
}

void loop()
{
int val1, val2;
char sbuf[100];

unsigned char cRegister = 0xE;

digitalWrite(CS, LOW);
/* write a data (g_cData) to a register (0xE) */
SPI.transfer(cRegister | 0x80);
SPI.transfer(g_cData);
digitalWrite(CS, HIGH);

sprintf(sbuf, "[Write] 0x%x to a Register (0x0E)\r\n", g_cData);
Serial.print(sbuf);

digitalWrite(CS, LOW);
/* read a data (val2) from a register (0xE) */
SPI.transfer(cRegister | 0x40);// default : full duplex
val1 = SPI.transfer(0x00);
digitalWrite(CS, HIGH);

digitalWrite(CS, LOW);
val2 = SPI.transfer((byte)0);
digitalWrite(CS, HIGH);

g_cData++;

sprintf(sbuf, "[Read] 0x%x from a Register (0x0E)\r\n", val1, val2);
Serial.print(sbuf);

delay(1000);
}
<Result>
[Write] 0x01 to a Register (0x0E)
[Read] 0xFF, 0x01 from a Register (0x0E)
[Write] 0x02 to a Register (0x0E)
[Read] 0xFF, 0x02 from a Register (0x0E)
[Write] 0x03 to a Register (0x0E)
[Read] 0xFF, 0x03 from a Register (0x0E)
[Write] 0x04 to a Register (0x0E)
[Read] 0xFF, 0x04 from a Register (0x0E)

------------------------------------------------
2. ESP32-IDF (didnt work)
------------------------------------------------
#define SPI_CS_PIN_O 5
#define SPI_SCLK_PIN_O 18
#define SPI_MOSI_PIN_O 23
#define SPI_MISO_PIN_I 19
#define DMA_CHAN 2

spi_device_handle_t g_SPI = 0;
unsigned char cRegister = 0xE;

void app_main(void)
{
................

bInit_spi_master();
}

bool bInit_spi_master()
{
esp_err_t ret;
spi_device_handle_t spi;
spi_host_device_t SPI_BUS = VSPI_HOST;

spi_bus_config_t buscfg = {
.miso_io_num = SPI_MISO_PIN_I,
.mosi_io_num = SPI_MOSI_PIN_O,
.sclk_io_num = SPI_SCLK_PIN_O,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = (4 * 8),
.flags = SPICOMMON_BUSFLAG_MASTER,
};
spi_device_interface_config_t devcfg = {
.mode = 0,
.clock_speed_hz = 10 * 1000 * 1000,
.spics_io_num = SPI_CS_PIN_O,
.queue_size = 1,
.pre_cb = NULL,
.post_cb = NULL,
};
//Initialize the SPI bus
ret = spi_bus_initialize(SPI_BUS, &buscfg, DMA_CHAN);
ESP_ERROR_CHECK(ret);

//Attach the SPI bus
ret = spi_bus_add_device(SPI_BUS, &devcfg, &spi);
ESP_ERROR_CHECK(ret);

g_SPI = spi;

/* Test */
writeData(cRegister, 0xab);
readData(cRegister);
}

void writeData(uint8_t addr, char data)
{
spi_transaction_t t;
uint8_t u8Tx[4] = {0};

u8Tx[0] = (addr | 0x80);
u8Tx[1] = data;

memset(&t, 0, sizeof(t));
t.flags = SPI_TRANS_USE_TXDATA;
t.length=2*8;
t.tx_buffer=u8Tx; //Data

esp_err_t ret = spi_device_transmit(g_SPI, &t);
assert(ret==ESP_OK);
}

uint8_t readData(uint8_t addr)
{
spi_transaction_t t;
uint8_t u8Tx[4] = {0};
uint8_t u8Rx[4] = {0};

u8Tx[0] = (addr | 0x40);

memset(&t, 0, sizeof(t));
t.length=4*8;
t.tx_buffer=u8Tx;
t.rx_buffer=u8Rx;

esp_err_t ret = spi_device_transmit(g_SPI, &t);
assert(ret==ESP_OK);

printk("READ(0xE)%d : 0x%x, 0x%x, 0x%x, 0x%x\r\n", t.rxlength, u8Rx[0], u8Rx[1], u8Rx[2], u8Rx[3]);

return 0;
}

<Result>
Read(0xE)24 : 0x0, 0x0, 0x0, 0x0

ESP_Sprite
Posts: 9749
Joined: Thu Nov 26, 2015 4:08 am

Re: I cann't SPI(VSPI) communication between external ADC and esp32-wrover-e with esp32-idf

Postby ESP_Sprite » Mon Oct 18, 2021 1:18 am

Hm, I can't see any fault in your code... only difference is that Arduino defaults to 1MHz, while your ESP-IDF code sets the SPI speed to 10MHz. Perhaps 10MHz is too fast for your hardware?

dallim30
Posts: 22
Joined: Thu Apr 29, 2021 1:35 am

Re: I cann't SPI(VSPI) communication between external ADC and esp32-wrover-e with esp32-idf

Postby dallim30 » Mon Oct 18, 2021 4:33 am

Thanks your response
I did the same test with an SPI speed of 1 MHz.
But it still didn't work.

ESP_Sprite
Posts: 9749
Joined: Thu Nov 26, 2015 4:08 am

Re: I cann't SPI(VSPI) communication between external ADC and esp32-wrover-e with esp32-idf

Postby ESP_Sprite » Mon Oct 18, 2021 7:45 am

Can you connect a logic analyzer or oscilloscope to the wires, see what it does? Alternatively, if you remove your ADC and connect MOSI to MISO on the ESP, what result do you get?

dallim30
Posts: 22
Joined: Thu Apr 29, 2021 1:35 am

Re: I cann't SPI(VSPI) communication between external ADC and esp32-wrover-e with esp32-idf

Postby dallim30 » Tue Oct 19, 2021 2:50 pm

It works well in my arduino code, so I don't think it's a hardware issue. Is there anything I need to configure regarding the esp32-idf API or menuconfig?

thanks

ESP_Sprite
Posts: 9749
Joined: Thu Nov 26, 2015 4:08 am

Re: I cann't SPI(VSPI) communication between external ADC and esp32-wrover-e with esp32-idf

Postby ESP_Sprite » Wed Oct 20, 2021 1:04 am

No, reasonably simple transfers like this should just work.

dallim30
Posts: 22
Joined: Thu Apr 29, 2021 1:35 am

Re: I cann't SPI(VSPI) communication between external ADC and esp32-wrover-e with esp32-idf

Postby dallim30 » Thu Oct 21, 2021 8:41 am

Could you give me a clue on how to solve it?
Thanks.

ESP_Sprite
Posts: 9749
Joined: Thu Nov 26, 2015 4:08 am

Re: I cann't SPI(VSPI) communication between external ADC and esp32-wrover-e with esp32-idf

Postby ESP_Sprite » Fri Oct 22, 2021 8:08 am

As I said, connecting a LA or scope to the signals would help tremendously in seeing what part of the SPI stuff actually goes wrong.

dallim30
Posts: 22
Joined: Thu Apr 29, 2021 1:35 am

Re: I cann't SPI(VSPI) communication between external ADC and esp32-wrover-e with esp32-idf

Postby dallim30 » Mon Nov 01, 2021 2:21 am

Thanks for the replies and advice.

I changed some codes on ESP32-IDF.
And then, I checked output wave from the ESP32.
SDI output no signal when I used ESP32-IDF code
but, there are them on arduino code
but, In the Arduino codes, the SDI output waveform is observed.
I used 24bit ADS (TI, ADS127L11)

1. The output data frame : Read Register Data on ADS127L11

[img]d:\Work\NewPSS\ADS.PNG[/img]

2. esp32-idf codes
uint8_t readData(uint8_t addr)
{
spi_transaction_t t;
uint8_t u8Tx[4] = {0};
uint8_t u8Rx[4] = {0};

u8Tx[0] = (addr | 0x40);

t.flags = SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA;
t.rxlength = 8 * 1;
t.length = 8 * 1; .
t.tx_data[0] = u8Tx[0];

cs_low();
ret=spi_device_transmit(g_SPI, &t);
assert(ret == ESP_OK);

memset(&t, 0, sizeof(t));
t.flags = SPI_TRANS_USE_RXDATA;
t.rxlength = 8 * 1; //Len is in bytes, transaction length is in bits.
t.length = 8 * 1;

ret=spi_device_transmit(g_SPI, &t);
assert(ret == ESP_OK);
cs_high();

cs_low();
ret=spi_device_transmit(g_SPI, &t);
cs_high();
assert(ret == ESP_OK);

for (int i = 0; i < 4; i++) {
u8Rx = t.rx_data;
}
printk("READ(0xE)%d : 0x%x, 0x%x, 0x%x, 0x%x\r\n", t.rxlength, u8Rx[0], u8Rx[1], u8Rx[2], u8Rx[3]);

3. Screen shots
[img]d:\Work\NewPSS\arduino_01.png[/img]

Thanks

sjkim
Attachments
ads127l11.pdf
(2.78 MiB) Downloaded 423 times
arduino_01.png
arduino_01.png (29.29 KiB) Viewed 5300 times
ADS.PNG
ADS.PNG (18.71 KiB) Viewed 5300 times

ESP_Sprite
Posts: 9749
Joined: Thu Nov 26, 2015 4:08 am

Re: I cann't SPI(VSPI) communication between external ADC and esp32-wrover-e with esp32-idf

Postby ESP_Sprite » Mon Nov 01, 2021 9:16 am

You have an image of the LA trace of the Arduino code? What does the equivalent ESP-IDF code trace look like?

If anything, you don't memset() your transaction to 0 before the first byte you send. As t is allocated on the stack, this may lead to uninialized members of t to contain garbage.

Who is online

Users browsing this forum: No registered users and 86 guests