Skip to content

Commit c1f95c7

Browse files
committed
fix(logging): wait for SWO TPIU drain before jumping to app
After ZEPHYR_BOOT_LOG_STOP drains the log buffer to the ITM FIFO, the TPIU may still be clocking out bytes on the SWO pin. The app reinitializes ITM on startup, discarding any in-flight data. Wait for ITM BUSY to clear, then busy-wait 1ms for the TPIU output buffer to finish transmitting at the SWO baud rate. Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
1 parent d9c2ba1 commit c1f95c7

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

boot/zephyr/main.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,22 @@ int main(void)
675675
mcuboot_status_change(MCUBOOT_STATUS_BOOTABLE_IMAGE_FOUND);
676676

677677
ZEPHYR_BOOT_LOG_STOP();
678+
679+
#if defined(CONFIG_LOG_BACKEND_SWO) && defined(CONFIG_CPU_CORTEX_M)
680+
/* Wait for ITM to finish transmitting before jumping to app.
681+
* ZEPHYR_BOOT_LOG_STOP drains the log buffer to the ITM FIFO,
682+
* but the TPIU may still be clocking out bytes on the SWO pin.
683+
* Without this, the app reinitializes ITM and discards in-flight data.
684+
*/
685+
while (ITM->TCR & ITM_TCR_BUSY_Msk) {
686+
}
687+
/* ITM BUSY only means the formatter is done — the TPIU output buffer
688+
* still needs time to clock bytes out at the SWO baud rate.
689+
* At 4 MHz NRZ, ~200 bytes (a few log lines) takes ~500 µs.
690+
*/
691+
k_busy_wait(1000);
692+
#endif
693+
678694
do_boot(&rsp);
679695

680696
mcuboot_status_change(MCUBOOT_STATUS_BOOT_FAILED);

0 commit comments

Comments
 (0)