[SPI Master] Data on MOSI is not the value we put in "spi_device_transmit()"

JonathanChen
Posts: 6
Joined: Thu Jan 19, 2017 6:36 am

[SPI Master] Data on MOSI is not the value we put in "spi_device_transmit()"

Postby JonathanChen » Thu Feb 04, 2021 4:20 am

Esp32 works in spi master mode and keeps sending data to a spi slaver.
After more than 30 minutes,we found that sometimes the data on MOSI is different from the data which we put in "spi_device_transmit()".

Once the mismatch occurs. the scope stops and we take the picture like this:

scope.PNG
scope.PNG (756.74 KiB) Viewed 5761 times
channel 1 is spi clk;
channel 2 is spi mosi;
channel 3 is gpio signal from slaver(just to trig scope)

and we can find 8 pulses with no data on MOSI.

The spi mode is :
13.333Mhz
mode 3
HSPI_HOST
DMA enable

Help!!!!

code:
init part:

Code: Select all

#define SPI_MISO_PIN 			13
#define SPI_MOSI_PIN 			12
#define SPI_CLK_PIN  			14
#define SPI_CS_PIN   			15
#define SPI_WAIT_PIN 			17
#define SPI_SPEED_HZ   			10000000  

static spi_device_handle_t Spi_Init()
{
	esp_err_t ret;

	//doesn't work
	gpio_iomux_in(SPI_MISO_PIN,HSPID_IN_IDX);
	gpio_iomux_out(SPI_MOSI_PIN,HSPIQ_OUT_IDX,false);
	gpio_iomux_out(SPI_CLK_PIN,HSPICLK_OUT_IDX,false);
	gpio_iomux_out(SPI_CS_PIN,HSPICS0_OUT_IDX,false);

	spi_device_handle_t spi;
	spi_bus_config_t buscfg={
			.miso_io_num=SPI_MISO_PIN,
			.mosi_io_num=SPI_MOSI_PIN,
			.sclk_io_num=SPI_CLK_PIN,
			.quadwp_io_num=-1,
			.quadhd_io_num=-1,
			.flags = SPICOMMON_BUSFLAG_MASTER,
			.intr_flags = ESP_INTR_FLAG_IRAM
	};
	spi_device_interface_config_t devcfg={
			.cs_ena_pretrans = 0,
			.cs_ena_posttrans = 10,
			.clock_speed_hz=SPI_SPEED_HZ,           
			.mode=3,                               
			.spics_io_num=SPI_CS_PIN,               
			.queue_size=1,                          
			.pre_cb=NULL,  							
			.flags = SPI_DEVICE_NO_DUMMY,

	};
	
	spi_host_device_t spiName = HSPI_HOST;

	//Initialize the SPI bus
	ret=spi_bus_initialize(spiName, &buscfg, 2); 
	assert(ret==ESP_OK);
	
	//Attach the DEVICE to the SPI bus
	ret=spi_bus_add_device(HSPI_HOST, &devcfg, &spi);
	assert(ret==ESP_OK);

	spiTxBuf = (uint8_t *)heap_caps_malloc(512, MALLOC_CAP_DMA);/
	spiRxBuf = (uint8_t *)heap_caps_malloc(512, MALLOC_CAP_DMA);

	return spi;
}
Trans part:

Code: Select all

	spi_transaction_t t;
	memset(&t, 0, sizeof(t));
	memset(spiTxBuf, 0, 512);
	memset(spiRxBuf, 0, 512);
	spiTxBuf[0] = 0xaa;
	t.length=8;                 	
	t.tx_buffer=spiTxBuf;           
	ret=spi_device_transmit(spi, &t);  
	assert(ret==ESP_OK);        
	
Last edited by JonathanChen on Thu Feb 04, 2021 4:38 am, edited 1 time in total.

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

Re: [SPI Master] Data on MOSI is not the value we put in "spi_device_transmit()"

Postby ESP_Sprite » Thu Feb 04, 2021 4:29 am

You did not manage to add a picture... Also, can you share the code that does the transmission?

JonathanChen
Posts: 6
Joined: Thu Jan 19, 2017 6:36 am

Re: [SPI Master] Data on MOSI is not the value we put in "spi_device_transmit()"

Postby JonathanChen » Thu Feb 04, 2021 4:40 am

Sorry :D
now i have update the img and code
please check.

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

Re: [SPI Master] Data on MOSI is not the value we put in "spi_device_transmit()"

Postby ESP_Sprite » Thu Feb 04, 2021 5:35 am

Strange... your code itself looks good. What ESP-IDF version are you using? What else is your code doing?

JonathanChen
Posts: 6
Joined: Thu Jan 19, 2017 6:36 am

Re: [SPI Master] Data on MOSI is not the value we put in "spi_device_transmit()"

Postby JonathanChen » Thu Feb 04, 2021 5:44 am

ESP_Sprite wrote:
Thu Feb 04, 2021 5:35 am
Strange... your code itself looks good. What ESP-IDF version are you using? What else is your code doing?
Release 3.3.4
hardware :esp32-wroom-32

My code has almost 10~15 tasks ,and WIFI, LWIP and BLE.
And I will check the value in " spiTxBuf[0]" before put it in "spi_device_transmit";

For now ,we try to make sure if there is some bug in our code.

Scott.Bonomi
Posts: 73
Joined: Mon Mar 09, 2020 7:36 pm

Re: [SPI Master] Data on MOSI is not the value we put in "spi_device_transmit()"

Postby Scott.Bonomi » Thu Feb 04, 2021 5:17 pm

JonathanChen -

For your anomaly tracing, it is often very worthwhile to use the Chip Select line down transition as a trigger for the scope.

- - Scott

JonathanChen
Posts: 6
Joined: Thu Jan 19, 2017 6:36 am

Re: [SPI Master] Data on MOSI is not the value we put in "spi_device_transmit()"

Postby JonathanChen » Fri Mar 12, 2021 2:14 am

update the code with idf-4.3-beta1.
And after a long test, it works well.
May be we solved it. :D :D :D :D :D

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 348 guests