Issue getting ESP32 SPI to work (Arduino SPI works but not ESP-IDF)

biggerfish
Posts: 3
Joined: Mon Jan 06, 2025 7:03 pm

Issue getting ESP32 SPI to work (Arduino SPI works but not ESP-IDF)

Postby biggerfish » Mon Jan 06, 2025 7:26 pm

Hello,

I am in the process of porting Arduino IDE code to ESP-IDF code and cannot get my SPI code to work. I am new to SPI, so I may be missing something. Any help would be very much appreciated as I have scoured the internet for the possible issue.

My code interfaces with a ADC that supports full duplex, SPI Mode 1 (TI AMC131M01).

I need to send the command 0x7880 followed by 0x0001. A dummy CRC is used to pad this to 24-bits.

I send the following:

SEND: 0x78 80 00 00 01 00 00 00 00 00 00 00 00 00 00

And normally (including with the Arduino SPI.h library) receive:

RECV: 0xFF 21 00 80 00 00 C3 10 00 58 80 00 80 00 00

However when I try to send the same thing using the ESP-IDF SPI library, I get garbage back:

SEND: 0x78 80 00 00 01 00 00 00 00 00 00 00 00 00 00

RECV: 0x02 a0 80 40 00 00 66 b9 00 02 a0 80 40 00 00

The response changes depending on what SPI\_HOST I use, and seems almost random.

I'm using the following code:

Code: Select all

	#include <stdio.h>
	#include <inttypes.h>
	#include "sdkconfig.h"
	#include "freertos/FreeRTOS.h"
	#include "freertos/task.h"
	#include "esp_chip_info.h"
	#include "esp_flash.h"
	#include "esp_system.h"

	#include "driver/gpio.h"
	#include "hal/gpio_types.h"
	#include <driver/spi_master.h>
	#include <string.h>
	#define ADC_CS_PIN GPIO_NUM_5
	#define ADC_MOSI_PIN 23
	#define ADC_MISO_PIN 19
	#define ADC_SCK_PIN 18
	spi_device_handle_t adc_spi_handle;

	void write_spi(uint8_t *data,int n,bool echo)
	{
	  if(echo)
	  {
		printf("SEND: 0x");
		for(int i = 0; i < n; i++)
		{
			printf("%02x",data[i]);
			printf(" ");
		}
		printf("\n");
	  }
	  uint8_t rxdata[15] = {0};
	  spi_transaction_t spi_transaction = {};
	  memset(&spi_transaction,0,sizeof(spi_transaction));
	  spi_transaction.addr = 0;
	  spi_transaction.cmd = 0;
	  spi_transaction.flags = 0;
	  spi_transaction.length = n*8;
	  spi_transaction.rxlength = n*8;
	  spi_transaction.tx_buffer = data;
	  spi_transaction.rx_buffer = rxdata;

	  gpio_set_level(ADC_CS_PIN, 0);
	  esp_err_t ret = spi_device_transmit(adc_spi_handle,&spi_transaction);
	  gpio_set_level(ADC_CS_PIN, 1);
	  ESP_ERROR_CHECK(ret);

	  if(echo)
	  {
		printf("RECV: 0x");
		for(int i = 0; i < n; i++)
		{
			printf("%02x",rxdata[i]);
			printf(" ");
		}
		printf("\n");
	  }


	}
	void app_main(void)
	{

		spi_bus_config_t spi_config = {};
		memset(&spi_config,0,sizeof(spi_config));
		spi_config.mosi_io_num = ADC_MOSI_PIN;
		spi_config.miso_io_num = ADC_MISO_PIN;
		spi_config.sclk_io_num = ADC_SCK_PIN;
		spi_config.quadwp_io_num = -1;
		spi_config.quadhd_io_num = -1;
		// spi_config.data4_io_num = -1;
		// spi_config.data5_io_num = -1;
		// spi_config.data6_io_num = -1;
		// spi_config.data7_io_num = -1;
		spi_config.max_transfer_sz = 256;
		// spi_configflags = SPICOMMON_BUSFLAG_MASTER;
			
		spi_device_interface_config_t devcfg = {};
		memset(&devcfg,0,sizeof(devcfg));
		devcfg.command_bits = 0;
		devcfg.address_bits = 0;
		devcfg.dummy_bits = 0;
		devcfg.mode = 1;
		// devcfg.duty_cycle_pos   = 0;
		// devcfg.cs_ena_posttrans = 0;
		// devcfg.cs_ena_pretrans  = 0;
		devcfg.clock_speed_hz = 25000000;
		devcfg.spics_io_num = ADC_CS_PIN;
		devcfg.flags            = 0;
		devcfg.queue_size       = 1;
		devcfg.pre_cb           = NULL;
		devcfg.post_cb          = NULL;

		esp_err_t ret = spi_bus_initialize(SPI3_HOST,&spi_config,SPI_DMA_CH_AUTO);
		ESP_ERROR_CHECK(ret);
		ret = spi_bus_add_device(SPI3_HOST,&devcfg,&adc_spi_handle);
		ESP_ERROR_CHECK(ret);


		uint8_t adc_data[] = {0x78, 0x80, 0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
		//uint8_t adc_data[] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x00,0x00,0x00,0x00,0x00,0x00};
		write_spi(adc_data,15,true);
		while(true)
		{
			vTaskDelay(1000 / portTICK_PERIOD_MS);
		}

	}
The equivalent Arduino SPI.h code works just fine however:

Code: Select all

	SPI.begin();
	SPISettings settingsA(25000000,MSBFIRST,SPI_MODE1);
	SPI.beginTransaction(settingsA);
	pinMode(ADC_CS_PIN,OUTPUT);
	uint8_t data[] = {0x78, 0x80, 0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
	digitalWrite(ADC_CS_PIN,LOW);
	SPI.transfer(data,n);
	digitalWrite(ADC_CS_PIN,HIGH);

username
Posts: 561
Joined: Thu May 03, 2018 1:18 pm

Re: Issue getting ESP32 SPI to work (Arduino SPI works but not ESP-IDF)

Postby username » Wed Jan 08, 2025 12:33 pm

if your going to manually control CS pin, you need to set devcfg.spics_io_num = -1;

biggerfish
Posts: 3
Joined: Mon Jan 06, 2025 7:03 pm

Re: Issue getting ESP32 SPI to work (Arduino SPI works but not ESP-IDF)

Postby biggerfish » Wed Jan 08, 2025 6:07 pm

I have tried the following:
* Setting the CS pin manually and assigning devcfg.spics_io_num = -1;
* Setting the CS pin manually and assigning devcfg.spics_io_num = ADC_CS_PIN;
* Not setting the CS pin manually and assigning devcfg.spics_io_num = ADC_CS_PIN;

None of them work.

Who is online

Users browsing this forum: No registered users and 159 guests