perf(matmul): bypass printf — direct UART 16550 register writes

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
This commit is contained in:
2026-06-26 17:51:13 +08:00
parent f55e15af32
commit ae89363227

View File

@@ -10,11 +10,32 @@
* 4. XOR bank toggle → avoids g%2 recomputation per iteration * 4. XOR bank toggle → avoids g%2 recomputation per iteration
*/ */
#include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <dma.h> #include <dma.h>
/* ─── 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) ──────────────────────── */ /* ─── Configurable group count (default 5000) ──────────────────────── */
#ifndef MATMUL_GROUP_NUM #ifndef MATMUL_GROUP_NUM
#define MATMUL_GROUP_NUM 5000 #define MATMUL_GROUP_NUM 5000
@@ -80,7 +101,7 @@ int main(void)
uint32_t ctrl_start[2] = {CTRL_START_B0, CTRL_START_B1}; uint32_t ctrl_start[2] = {CTRL_START_B0, CTRL_START_B1};
/* ── Start marker (must be first output) ──────────────────── */ /* ── Start marker (must be first output) ──────────────────── */
printf("MATMUL_START\n"); uart_puts("MATMUL_START\r\n");
// Reset IP state // Reset IP state
*mat_status = STATUS_DONE | STATUS_ERROR; *mat_status = STATUS_DONE | STATUS_ERROR;
@@ -186,8 +207,9 @@ int main(void)
/* ── Read hardware CRC32 ──────────────────────────────────── */ /* ── Read hardware CRC32 ──────────────────────────────────── */
crc = *mat_crc; crc = *mat_crc;
printf("MATMUL_CRC32=%08lx\n", (unsigned long)crc); uart_puts("MATMUL_CRC32=");
printf("MATMUL_DONE\n"); uart_puthex(crc);
uart_puts("\r\nMATMUL_DONE\r\n");
return 0; return 0;
} }