Guru medidation error on spi_device_transmit()

kluverp
Posts: 26
Joined: Mon Jun 18, 2018 7:08 am

Guru medidation error on spi_device_transmit()

Postby kluverp » Tue Sep 25, 2018 1:04 pm

Hi all,

I have a weird issue.
I am using SPI to read out some temperature sensors. The code I have to do this runs fine on one ESP32, but crashes on another ESP32. Both hardware are the same.

To give some idea, here is my code to init SPI:

Code: Select all

spi_device_handle_t SPI_init(void)
{
	ESP_LOGD(tag, ">> test_spi_task");
	
	spi_bus_config_t bus_config {};
	bus_config.sclk_io_num		= PIN_NUM_CLK;		// CLK
	bus_config.mosi_io_num		= PIN_NUM_MOSI;		// MOSI
	bus_config.miso_io_num		= PIN_NUM_MISO;		// MISO
	bus_config.quadwp_io_num	= -1;				// Not used
	bus_config.quadhd_io_num	= -1;				// Not used
	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				= 0;
	dev_config.duty_cycle_pos	= 0;
	dev_config.cs_ena_posttrans	= 0;
	dev_config.cs_ena_pretrans	= 0;
	dev_config.clock_speed_hz	= 20000;
	dev_config.spics_io_num		= PIN_NUM_CS;
	dev_config.flags			= 0;
	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));

	return handle;
}
Then I have a function to retrieve the SPI data like so:

Code: Select all

uint32_t spi_tranfer_for_result(spi_device_handle_t spi, const uint8_t *data) 
{
	spi_transaction_t t;
	memset(&t, 0, sizeof(t));
	t.length = 8 * 3;
	t.flags = SPI_TRANS_USE_RXDATA;
	t.user = (void*)1;
	t.tx_buffer = data;

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

	// cast to uint32
	uint32_t rx_data = t.rx_data[0] | ( (uint32_t)t.rx_data[1] << 8 ) | ( (uint32_t)t.rx_data[2] << 16 ) | ( (uint32_t)t.rx_data[3] << 24 );

	return rx_data;
}
Now, the problem: on one ESP32 this all runs fine. Data is returned.
On another ESP32, I instantly get a Guru Meditation error as soon as I call the function "spi_device_transmit(spi, &t);" like so:

Code: Select all

Guru Meditation Error: Core  0 panic'ed (IllegalInstruction). Exception was unhandled.
Core 0 register dump:
PC      : 0x400d31bc  PS      : 0x00060430  A0      : 0x8008d20a  A1      : 0x3ffc7a60  
0x400d31bc: idle_hook_cb at /home/peter/esp/esp-idf-v3.1/components/esp32/task_wdt.c:76

A2      : 0x00000000  A3      : 0x00000000  A4      : 0x3ffb44f0  A5      : 0x3ffb1078  
A6      : 0x3ffb4814  A7      : 0x3ffb45bc  A8      : 0x800d3628  A9      : 0x3ffc7a40  
A10     : 0x400d31bc  A11     : 0x40082c10  A12     : 0x00060020  A13     : 0x00000001  
0x400d31bc: idle_hook_cb at /home/peter/esp/esp-idf-v3.1/components/esp32/task_wdt.c:76

0x40082c10: _free_r at /home/peter/esp/esp-idf-v3.1/components/newlib/syscalls.c:41

A14     : 0x0000cdcd  A15     : 0x00060a23  SAR     : 0x00000000  EXCCAUSE: 0x00000000  
EXCVADDR: 0x00000000  LBEG    : 0x00000000  LEND    : 0x00000000  LCOUNT  : 0x00000000  

Backtrace: 0x400d31bc:0x3ffc7a60 0x4008d207:0x3ffc7a80
0x400d31bc: idle_hook_cb at /home/peter/esp/esp-idf-v3.1/components/esp32/task_wdt.c:76

0x4008d207: prvIdleTask at /home/peter/esp/esp-idf-v3.1/components/freertos/tasks.c:5112


E (1178) spi_flash: Bad write at offset 0x114284 expected 0x00000000 readback 0x00000002
E (1178) spi_flash: Bad write at offset 0x3b305b4b expected 0x0000000c readback 0x00000001
E (1178) spi_flash: Bad write at offset 0x3b305b4f expected 0x00000001 readback 0x3ffc6330
E (1178) spi_flash: Bad write at offset 0x3b305b53 expected 0x00000000 readback 0x6e69616d
E (1178) spi_flash: Bad write at offset 0x3b305b57 expected 0x800835d1 readback 0xda796000
When I comment the line "esp_err_t ret = spi_device_transmit(spi, &t);" all runs fine. So I have narrowed it down to this function.

Now the weirdest part: The SPI does work on the ESP32 where it normally doesn't if I remove all code that has something to do with the UART or the Ethernet part.

And the location/reason of the Guru Meditation error is random.
Mostly it is one of:
- LoadProhibited
- StoreProhibited (0x401771ce: esp_vApplicationWaitiHook at /home/peter/esp/esp-idf-v3.1/components/esp32/freertos_hooks.c:66)
- IllegalInstruction (0x401771cb: esp_vApplicationWaitiHook at /home/peter/esp/esp-idf-v3.1/components/esp32/freertos_hooks.c:66)
- IllegalInstruction (0x401771df: LevelSwitch::initSetpDev(variable*, unsigned short, unsigned short, bool) at /home/peter/esp/WTW_controller/main/AOS/AOS_system.cpp:462)

Any thoughts of what this might be?

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

Re: Guru medidation error on spi_device_transmit()

Postby ESP_Sprite » Wed Sep 26, 2018 1:31 am

This smells like the SPI transaction somehow is interfering with the communication with the flash chip that contains the program the ESP32 runs, hence the weird errors and illegal instructions. Can you check if you perhaps have a short or low resistance between your SPI pins and these pins/pads?

kluverp
Posts: 26
Joined: Mon Jun 18, 2018 7:08 am

Re: Guru medidation error on spi_device_transmit()

Postby kluverp » Wed Sep 26, 2018 4:46 am

That sounds like reasonable at this point.

So I have the pins checked, and they seem OK.

Also, I have done another test. Just let the SPI code run in the app_main as a loop like so:

Code: Select all

void app_main(void)
{
	// Analog
	spi_device_handle_t spi_handle = SPI_init();

	delay(200);

	while(1)
	{
		SPI_read_temp(spi_handle);
		ESP_LOGI("test", "%d", AI[0].real);
	}
So there is no interfering code.

One ESP32 runs fine the whole night. Still running 13 hours later.

The other one, keeps crashing at exactly 50337 ms (Guru Medidation and stops in some "esp_timer.c" code. Every time I run this code.
Then let it rest one night. The next morning I start this ESP again, and it runs past the 50337 ms boundary for minutes.
Pressed reset again.... and crashed at 50347 ms. It seems totally random:

Code: Select all

uru Meditation Error: Core  0 panic'ed (IllegalInstruction). Exception was unhandled.
Core 0 register dump:
PC      : 0x400d198c  PS      : 0x00060933  A0      : 0x802167fe  A1      : 0x3ffafa00  
0x400d198c: timer_process_alarm at /home/peter/esp/esp-idf-v3.1/components/esp32/esp_timer.c:171
 (inlined by) timer_task at /home/peter/esp/esp-idf-v3.1/components/esp32/esp_timer.c:311

A2      : 0x3ffb2538  A3      : 0x00000000  A4      : 0x00000000  A5      : 0x00000000  
A6      : 0x00000000  A7      : 0x00000000  A8      : 0x800d1989  A9      : 0x3ffaf9e0  
A10     : 0x03000012  A11     : 0x00000000  A12     : 0x00000013  A13     : 0x00000000  
A14     : 0x3ff47024  A15     : 0x00000000  SAR     : 0x00000036  EXCCAUSE: 0x00000000  
EXCVADDR: 0x00000000  LBEG    : 0x00000000  LEND    : 0x00000000  LCOUNT  : 0x00000000  

Backtrace: 0x400d198c:0x3ffafa00 0x402167fb:0x3ffafa20
0x400d198c: timer_process_alarm at /home/peter/esp/esp-idf-v3.1/components/esp32/esp_timer.c:171
 (inlined by) timer_task at /home/peter/esp/esp-idf-v3.1/components/esp32/esp_timer.c:311
Interesting sidenote: running the code afterwards 3 times, and all crash at exactly 50347 ms!

It must be hardware related...right? But why the crash at the exact same time then?

Could the ESP32 be damaged? Corrupt memory or so?

Who is online

Users browsing this forum: Majestic-12 [Bot] and 76 guests