From ae893632278688175094ac1c4b1c84a69706b9ac Mon Sep 17 00:00:00 2001 From: FallenSigh Date: Fri, 26 Jun 2026 17:51:13 +0800 Subject: [PATCH] =?UTF-8?q?perf(matmul):=20bypass=20printf=20=E2=80=94=20d?= =?UTF-8?q?irect=20UART=2016550=20register=20writes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace all printf() calls with direct UART THR register access: - uart_tx(): poll LSR bit5, write byte to THR - uart_puts(): null-terminated string output - uart_puthex(): 8-digit lowercase hex for CRC32 Eliminates picolibc printf overhead: no format parsing, no FILE* I/O, no locale, no buffering. UART runs at 115200 baud (~8.7μs/byte), THR nearly always empty at 33MHz core → single poll iteration. Output: MATMUL_START, MATMUL_CRC32=XXXXXXXX, MATMUL_DONE --- sdk/software/examples/asm/user-sample.c | 30 +++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/sdk/software/examples/asm/user-sample.c b/sdk/software/examples/asm/user-sample.c index b9096e8..cfa8e97 100644 --- a/sdk/software/examples/asm/user-sample.c +++ b/sdk/software/examples/asm/user-sample.c @@ -10,11 +10,32 @@ * 4. XOR bank toggle → avoids g%2 recomputation per iteration */ -#include #include #include +/* ─── UART 16550 direct access (bypasses slow printf/picolibc) ──────── */ +#define UART_THR (*(volatile uint8_t *)0xbf000000) +#define UART_LSR (*(volatile uint8_t *)0xbf000005) +#define LSR_THRE 0x20 + +static inline void uart_tx(uint8_t c) { + while (!(UART_LSR & LSR_THRE)) {} + UART_THR = c; +} + +static void uart_puts(const char *s) { + while (*s) uart_tx(*s++); +} + +static void uart_puthex(uint32_t val) { + int i; + for (i = 28; i >= 0; i -= 4) { + uint8_t nibble = (val >> i) & 0xF; + uart_tx(nibble < 10 ? '0' + nibble : 'a' + nibble - 10); + } +} + /* ─── Configurable group count (default 5000) ──────────────────────── */ #ifndef MATMUL_GROUP_NUM #define MATMUL_GROUP_NUM 5000 @@ -80,7 +101,7 @@ int main(void) uint32_t ctrl_start[2] = {CTRL_START_B0, CTRL_START_B1}; /* ── Start marker (must be first output) ──────────────────── */ - printf("MATMUL_START\n"); + uart_puts("MATMUL_START\r\n"); // Reset IP state *mat_status = STATUS_DONE | STATUS_ERROR; @@ -186,8 +207,9 @@ int main(void) /* ── Read hardware CRC32 ──────────────────────────────────── */ crc = *mat_crc; - printf("MATMUL_CRC32=%08lx\n", (unsigned long)crc); - printf("MATMUL_DONE\n"); + uart_puts("MATMUL_CRC32="); + uart_puthex(crc); + uart_puts("\r\nMATMUL_DONE\r\n"); return 0; }