I cann't SPI(VSPI) communication between external ADC and esp32-wrover-e with esp32-idf
Posted: 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
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