How to reset the TWDT if the SPI slave is waiting with "portMAX_DELAY" in a separat task

HEckardt
Posts: 31
Joined: Wed Aug 16, 2023 3:13 pm

How to reset the TWDT if the SPI slave is waiting with "portMAX_DELAY" in a separat task

Postby HEckardt » Wed May 20, 2026 9:01 am

Dear Community,

I want to have a SPI traffic between an ESP32 slave and a PIC-MCU master. The SPI interface is working as well in case I start the transfer in the main.c. But the traffic procedure is asynchron, so the ESP-SPI must wait until the transfer ist finalized by the master. For this I have to start the ESP slave spi transfer with "portMAX_DELAY". This is blocking the further process. So I decide to create separate task for the SPI slave transfer.

Code: Select all

.........
    TaskHandle_t Se_Re_SPI_Task = NULL;
	ReadyForSPItrans = false;
	WaitForSPIresp = false;
    xTaskCreate(Send_Rec_SPI, "Send_Rec_SPI", 1024, &SPItransmit, 1,
					&Se_Re_SPI_Task);
    configASSERT(Se_Re_SPI_Task);
	
.........
This task creating is working. But I hve to add this task also to the TaskWatchdogTimer:

Code: Select all

void Send_Rec_SPI(void *AdrToSPIdef)			//own Task
{
	

	ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
	printf("\nTask added to TWDT\n");
	while(1)
	{
		
		
		vTaskDelay(3000 / portTICK_PERIOD_MS);	//just for test
		if (ReadyForSPItrans)				//spi slave is ready for transfer, trigger-GPIO for PIC is active, wait for
											master-pickup
		{
			spi_slave_transmit(RCV_HOST, AdrToSPIdef, portMAX_DELAY);
			ReadyForSPItrans = false;				// handshake
			WaitForSPIresp = true;				// handshake
			printf("\nSPI transmit started\n");
		}
		ESP_ERROR_CHECK(esp_task_wdt_reset());	//without effect because "portMAX_DELAY" is blocking the task, in case
												"spi_slave_transmit" is started
		printf("\nTWDT Resetted\n");
		if (WaitForSPIresp == true) printf("\nWait for resp is true\n");;
	}
	
}
The Watchdog reset is also working. But only if the spi_slave_transmit is not active. The reason for it is most likely that I wait with "portMAX_DELAY" for the master responds and the "esp_task_wdt_reset()" is not called. The ESP is printig this message in the monitor window:

E (30538) task_wdt: Task watchdog got triggered. The following tasks/users did not reset the watchdog in time:
E (30538) task_wdt: - Send_Rec_SPI (CPU 0/1)
E (30538) task_wdt: Tasks currently running:
E (30538) task_wdt: CPU 0: IDLE0
E (30538) task_wdt: CPU 1: IDLE1
E (30538) task_wdt: Print CPU 0 (current core) backtrace


Backtrace: 0x400DB6C6:0x3FFB2280 0x400DBA8C:0x3FFB22A0 0x40082F79:0x3FFB22D0 0x40087983:0x3FFBBB70 0x401687FA:0x3FFBBB90 0x4008ADA5:0x3FFBBBB0 0x40089D29:0x3FFBBBD0
--- 0x400db6c6: task_wdt_timeout_handling at C:/Espressif/frameworks/esp-idf-v5.5.2/components/esp_system/task_wdt/task_wdt.c:436
--- 0x400dba8c: task_wdt_isr at C:/Espressif/frameworks/esp-idf-v5.5.2/components/esp_system/task_wdt/task_wdt.c:509
--- 0x40082f79: _xt_lowint1 at C:/Espressif/frameworks/esp-idf-v5.5.2/components/xtensa/xtensa_vectors.S:1240
--- 0x40087983: xt_utils_wait_for_intr at C:/Espressif/frameworks/esp-idf-v5.5.2/components/xtensa/include/xt_utils.h:82
--- (inlined by) esp_cpu_wait_for_intr at C:/Espressif/frameworks/esp-idf-v5.5.2/components/esp_hw_support/cpu.c:55
--- 0x401687fa: esp_vApplicationIdleHook at C:/Espressif/frameworks/esp-idf-v5.5.2/components/esp_system/freertos_hooks.c:58
--- 0x4008ada5: prvIdleTask at C:/Espressif/frameworks/esp-idf-v5.5.2/components/freertos/FreeRTOS-Kernel/tasks.c:4350
--- 0x40089d29: vPortTaskWrapper at C:/Espressif/frameworks/esp-idf-v5.5.2/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:139

E (30538) task_wdt: Print CPU 1 backtrace


Backtrace: 0x40084076:0x3FFB2AE0 0x40082F79:0x3FFB2B00 0x40087983:0x3FFBC180 0x401687FA:0x3FFBC1A0 0x4008ADA5:0x3FFBC1C0 0x40089D29:0x3FFBC1E0
--- 0x40084076: esp_crosscore_isr at C:/Espressif/frameworks/esp-idf-v5.5.2/components/esp_system/crosscore_int.c:74
--- 0x40082f79: _xt_lowint1 at C:/Espressif/frameworks/esp-idf-v5.5.2/components/xtensa/xtensa_vectors.S:1240
--- 0x40087983: xt_utils_wait_for_intr at C:/Espressif/frameworks/esp-idf-v5.5.2/components/xtensa/include/xt_utils.h:82
--- (inlined by) esp_cpu_wait_for_intr at C:/Espressif/frameworks/esp-idf-v5.5.2/components/esp_hw_support/cpu.c:55
--- 0x401687fa: esp_vApplicationIdleHook
at C:/Espressif/frameworks/esp-idf-v5.5.2/components/esp_system/freertos_hooks.c:58
--- 0x4008ada5: prvIdleTask at C:/Espressif/frameworks/esp-idf-v5.5.2/components/freertos/FreeRTOS-Kernel/tasks.c:4350
--- 0x40089d29: vPortTaskWrapper at C:/Espressif/frameworks/esp-idf-v5.5.2/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:139
My question is:

- why it is working in the main.c without the error message
- what I have to do, that the behavior of "spi_slave_transmit" in the task "Send_Rec_SPI" is the same as in the "main".


Thank you for the support.
Henry

MicroController
Posts: 2663
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: How to reset the TWDT if the SPI slave is waiting with "portMAX_DELAY" in a separat task

Postby MicroController » Wed May 20, 2026 5:47 pm

Code: Select all

    xTaskCreate(Send_Rec_SPI, "Send_Rec_SPI", 1024, &SPItransmit, 1,
					&Se_Re_SPI_Task);
1KB of stack is very little. I suggest starting out with 3-4KB.
The Watchdog reset is also working. But only if the spi_slave_transmit is not active. The reason for it is most likely that I wait with "portMAX_DELAY" for the master responds and the "esp_task_wdt_reset()" is not called.
More likely because of

Code: Select all

vTaskDelay(3000 / portTICK_PERIOD_MS);	//just for test

Generally you'll have to decide if you want to
a) allow operations in a task (like spi_slave_transmit()) to block indefinitely (portMAX_DELAY),
or
b) have the WDT enforce regular activity of the task.
You cannot have both at the same time.

MicroController
Posts: 2663
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: How to reset the TWDT if the SPI slave is waiting with "portMAX_DELAY" in a separat task

Postby MicroController » Wed May 20, 2026 6:11 pm

Doesn't actually solve the conceptual problem, but you could do something like this:

Code: Select all

esp_err_t r;

do {
  esp_task_wdt_reset();
  r = spi_slave_queue_trans(RCV_HOST, AdrToSPIdef, 100 / portTICK_PERIOD_MS);
} while ( r == ESP_ERR_TIMEOUT );

if(r == ESP_OK) {
  spi_slave_transaction_t* finishedTransaction;
  do {
    esp_task_wdt_reset();
    r = spi_slave_get_trans_result(RCV_HOST, &finishedTransaction, 100 / portTICK_PERIOD_MS);
  } while ( r == ESP_ERR_TIMEOUT );
  ...
}

HEckardt
Posts: 31
Joined: Wed Aug 16, 2023 3:13 pm

Re: How to reset the TWDT if the SPI slave is waiting with "portMAX_DELAY" in a separat task

Postby HEckardt » Thu May 21, 2026 10:53 am

Dear MicroController,

thanks for your responds. I adapted your suggestion into my code and it is working. I don't know wether we shuold discuss the still open question: why is the TWDT in the main.c-loop not triggered if "spi_slave_transmit()" with "portMAX_DELAY" is started? Because the code in the separated task could be easyer in case this task have the same behavior as the main.c.

Greetings
Henry

MicroController
Posts: 2663
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: How to reset the TWDT if the SPI slave is waiting with "portMAX_DELAY" in a separat task

Postby MicroController » Fri May 22, 2026 1:48 pm

why is the TWDT in the main.c-loop not triggered ... ?
Well, obviously because something is different in or about your main(?) task. We on the internet cannot know what that difference is because only you have the code...

HEckardt
Posts: 31
Joined: Wed Aug 16, 2023 3:13 pm

Re: How to reset the TWDT if the SPI slave is waiting with "portMAX_DELAY" in a separat task

Postby HEckardt » Fri May 22, 2026 2:53 pm

Hello MC,

OK, I will skip this topic. Basecally I have a solution (more or less a workaround).

Thank you for your support.
Henry

Who is online

Users browsing this forum: Baidu [Spider], Semrush [Bot] and 4 guests