Page 1 of 1

Should I use no_sleep power managment lock when using uart_write_bytes

Posted: Sun Dec 28, 2025 6:51 am
by Ildar Belkin
Hello

I use heavy no_sleep power managment lock. So should I use this for uart_write_bytes?
I never read about internal using locks in uart_write_bytes function. Viewed the source of uart_write_bytes and nothing see about power managment.

what official docs say:
(https://docs.espressif.com/projects/esp ... modes.html)
UART Output Handling
Before entering sleep mode, esp_deep_sleep_start() will flush the contents of UART FIFOs.

When entering Light-sleep mode using esp_light_sleep_start(), UART FIFOs will not be flushed. Instead, UART output will be suspended, and remaining characters in the FIFO will be sent out after wakeup from Light-sleep.
I think power managment may goes to light sleep during uart_write_bytes execution when sending bytes from buffer to TX line. So it seems to me that tx data may be broken during transmition.

my code snippet where I use lock:

Code: Select all


	...
	esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, GNSS_PM_lock_name, &GNSS_PM_lock_handle);
	...

	xSemaphoreTake( xMutexGNSS, portMAX_DELAY );
	{
		sprintf(message,"$PMTK%s",data);
		crc = get_crc(message+1);
		sprintf(msg_end, "*%lx\r\n", crc);
		strcat(message, msg_end);
		
		esp_pm_lock_acquire(GNSS_PM_lock_handle);
		uart_write_bytes(GNSS_UART_PORT, message, strlen(message));
		uart_wait_tx_done(GNSS_UART_PORT,portMAX_DELAY);
		esp_pm_lock_release(GNSS_PM_lock_handle);

		//xEventGroupSetBits(xGNSS_EventGroup_handle, FLAG_GNSS_FIX_EVT);


	}
	xSemaphoreGive( xMutexGNSS );
Could you tell me should I use locks or not

Re: Should I use no_sleep power managment lock when using uart_write_bytes

Posted: Wed Dec 31, 2025 5:12 pm
by lichurbagan
You should use the PM lock. uart_write_bytes() does not acquire power-management locks. The UART driver allows light sleep during transmission. Light sleep pauses UART output mid-frame. Paused output resumes later, creating timing gaps. Data is not lost, but timing is broken. GNSS devices often require continuous UART timing. Timing gaps may break protocol parsing. Your lock prevents light sleep during transmission. uart_wait_tx_done() ensures physical transmission completion. Your current implementation is correct and recommended. For more info about ESP32 low power modes, you can see here. https://www.theengineeringprojects.com/ ... modes.html