diff options
| author | hathach <[email protected]> | 2026-06-20 23:06:07 +0700 |
|---|---|---|
| committer | hathach <[email protected]> | 2026-06-20 23:06:07 +0700 |
| commit | ea5c6fa165649cfa36d704b1852babf19d5e6bb5 (patch) | |
| tree | 84aba062f7db24aa524abf7485471d0b4df383fd | |
| parent | cca6fe64e95175cbefcdae1d1306ea8765a075d9 (diff) | |
hw/bsp/ch58x: address review feedback and read the real chip unique id
Fold in the CH58x BSP review fixes:
- family.mk: drop stray trailing backslashes on the last LDFLAGS/SRC_C entries
(harmless -- GNU Make ends the list at the blank line -- but misleading).
- debug_uart.c: uart_write() spun on a full ring buffer with nothing to drain it
(only uart_sync() advances tx_consume), so a burst larger than the buffer
deadlocked. Drain the FIFO while waiting, like uart_sync() does.
- wch-riscv.cfg: move the OpenOCD work area from 0x80000000 (unmapped) to the
0x20000000 SRAM, sized to 32 KB, matching ch32v20x/wch-riscv.cfg.
- family.c: implement board_get_unique_id() from the factory MAC. CH58x is a BLE
part, so a unique 6-byte MAC lives in FlashROM at ROM_CFG_MAC_ADDR; GetMACAddress()
reads it via FLASH_EEPROM_CMD (in libISP583.a), so no extra source file is needed.
The read buffer is TU_ATTR_ALIGNED(4) and 8 bytes, per the SDK's documented
4-byte-aligned, word-granular buffer contract (CH58x_flash.c).
- test/hil/tinyusb.json: key ch582m_evt off this board's actual MAC (D443627B5450)
instead of the fixed placeholder, like every other board.
Verified on ci.lan HIL: ch582m_evt enumerates with serial D443627B5450 and all
device examples pass.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
| -rw-r--r-- | hw/bsp/ch58x/debug_uart.c | 10 | ||||
| -rw-r--r-- | hw/bsp/ch58x/family.c | 16 | ||||
| -rw-r--r-- | hw/bsp/ch58x/family.mk | 4 | ||||
| -rw-r--r-- | hw/bsp/ch58x/wch-riscv.cfg | 2 | ||||
| -rw-r--r-- | test/hil/tinyusb.json | 2 |
5 files changed, 25 insertions, 9 deletions
diff --git a/hw/bsp/ch58x/debug_uart.c b/hw/bsp/ch58x/debug_uart.c index 7dd133cb2..850e40718 100644 --- a/hw/bsp/ch58x/debug_uart.c +++ b/hw/bsp/ch58x/debug_uart.c @@ -41,8 +41,14 @@ static volatile uint32_t tx_consume; void uart_write(char c) { uint32_t tx_produce_next = (tx_produce + 1) & UART_RINGBUFFER_MASK_TX; - // If ring buffer is full, wait - while (tx_produce_next == tx_consume) {} + // If the ring buffer is full, drain it here as the FIFO frees up: nothing else advances + // tx_consume between uart_write() calls, so a plain spin would deadlock on a >buffer-size burst. + while (tx_produce_next == tx_consume) { + if (R8_UART1_LSR & RB_LSR_TX_FIFO_EMP) { + R8_UART1_THR = tx_buf[tx_consume]; + tx_consume = (tx_consume + 1) & UART_RINGBUFFER_MASK_TX; + } + } // If UART TX FIFO is empty and no pending data, send directly if ((tx_consume == tx_produce) && (R8_UART1_LSR & RB_LSR_TX_FIFO_EMP)) { diff --git a/hw/bsp/ch58x/family.c b/hw/bsp/ch58x/family.c index 8e5f2826b..64ae4a903 100644 --- a/hw/bsp/ch58x/family.c +++ b/hw/bsp/ch58x/family.c @@ -163,9 +163,19 @@ uint32_t board_button_read(void) { #endif } -// Note: CH58x exposes no memory-mapped unique-ID register (unlike ch32v10x/v20x at -// 0x1FFFF7E8), and the SDK's GET_UNIQUE_ID() is a BootROM stub not present in -// libISP583.a. So board_get_unique_id() falls back to the fixed default in board.c. +// CH58x has no memory-mapped unique-ID register (unlike ch32v10x/v20x at 0x1FFFF7E8), but the +// factory programs a unique 6-byte MAC address into FlashROM (it is a BLE part). GetMACAddress() +// reads it via the ISP ROM command FLASH_EEPROM_CMD, which is provided by libISP583.a. +size_t board_get_unique_id(uint8_t id[], size_t max_len) { + // FLASH_EEPROM_CMD writes word-granular and requires a 4-byte-aligned buffer (CH58x_flash.c); + // size 8 matches the SDK's GET_UNIQUE_ID buffer (6 MAC bytes + 2 it pads), so the read can't + // run past the end whether it returns 6 or a full 8. + TU_ATTR_ALIGNED(4) uint8_t mac[8]; + GetMACAddress(mac); + size_t len = TU_MIN(max_len, (size_t) 6); // the 6-byte MAC is the unique part + memcpy(id, mac, len); + return len; +} int board_uart_read(uint8_t* buf, int len) { (void) buf; diff --git a/hw/bsp/ch58x/family.mk b/hw/bsp/ch58x/family.mk index ccdaa7268..ac0443659 100644 --- a/hw/bsp/ch58x/family.mk +++ b/hw/bsp/ch58x/family.mk @@ -28,7 +28,7 @@ CFLAGS += \ LDFLAGS += \ -nostartfiles \ - --specs=nosys.specs --specs=nano.specs \ + --specs=nosys.specs --specs=nano.specs LIBS += $(TOP)/$(SDK_SRC_DIR)/StdPeriphDriver/libISP583.a @@ -40,7 +40,7 @@ SRC_C += \ $(SDK_SRC_DIR)/StdPeriphDriver/CH58x_sys.c \ $(FAMILY_PATH)/debug_uart.c \ $(FAMILY_PATH)/ch58x_it.c \ - $(FAMILY_PATH)/system_ch58x.c \ + $(FAMILY_PATH)/system_ch58x.c SRC_S += \ $(SDK_SRC_DIR)/Startup/startup_CH583.S diff --git a/hw/bsp/ch58x/wch-riscv.cfg b/hw/bsp/ch58x/wch-riscv.cfg index 5913a2465..64d595d8e 100644 --- a/hw/bsp/ch58x/wch-riscv.cfg +++ b/hw/bsp/ch58x/wch-riscv.cfg @@ -9,7 +9,7 @@ sdi newtap $_CHIPNAME cpu -irlen 5 -expected-id 0x00001 set _TARGETNAME $_CHIPNAME.cpu target create $_TARGETNAME.0 wch_riscv -chain-position $_TARGETNAME -$_TARGETNAME.0 configure -work-area-phys 0x80000000 -work-area-size 10000 -work-area-backup 1 +$_TARGETNAME.0 configure -work-area-phys 0x20000000 -work-area-size 0x8000 -work-area-backup 1 set _FLASHNAME $_CHIPNAME.flash flash bank $_FLASHNAME wch_riscv 0x00000000 0 0 0 $_TARGETNAME.0 diff --git a/test/hil/tinyusb.json b/test/hil/tinyusb.json index ccf81c582..71d92aae1 100644 --- a/test/hil/tinyusb.json +++ b/test/hil/tinyusb.json @@ -496,7 +496,7 @@ }, { "name": "ch582m_evt", - "uid": "0123456789ABCDEF", + "uid": "D443627B5450", "toolchain": "riscv-gcc", "tests": { "device": true, |
