From 2aeaec3737708331cfa0eb02c477e4b7cec2fbc2 Mon Sep 17 00:00:00 2001 From: boa-z Date: Sat, 14 Nov 2026 00:22:22 -0800 Subject: [PATCH 1/3] [bsp][artinchip] Add basic D213ECV-DEMO-V4-0 support --- bsp/artinchip/README.md | 30 ++ bsp/artinchip/README_zh.md | 30 ++ bsp/artinchip/d213ecv-demo-v4-0/.config | 403 ++++++++++++++++++ bsp/artinchip/d213ecv-demo-v4-0/.gitignore | 10 + bsp/artinchip/d213ecv-demo-v4-0/Kconfig | 11 + bsp/artinchip/d213ecv-demo-v4-0/README.md | 147 +++++++ bsp/artinchip/d213ecv-demo-v4-0/README_zh.md | 147 +++++++ bsp/artinchip/d213ecv-demo-v4-0/SConscript | 5 + bsp/artinchip/d213ecv-demo-v4-0/SConstruct | 28 ++ .../d213ecv-demo-v4-0/applications/SConscript | 4 + .../d213ecv-demo-v4-0/applications/main.c | 13 + bsp/artinchip/d213ecv-demo-v4-0/board/Kconfig | 38 ++ .../d213ecv-demo-v4-0/board/SConscript | 5 + bsp/artinchip/d213ecv-demo-v4-0/board/board.c | 55 +++ bsp/artinchip/d213ecv-demo-v4-0/board/board.h | 19 + bsp/artinchip/d213ecv-demo-v4-0/link.lds | 129 ++++++ bsp/artinchip/d213ecv-demo-v4-0/rtconfig.h | 222 ++++++++++ bsp/artinchip/d213ecv-demo-v4-0/rtconfig.py | 49 +++ bsp/artinchip/libraries/Kconfig | 6 + bsp/artinchip/libraries/SConscript | 4 + .../libraries/include/aic_uart_reg.h | 54 +++ bsp/artinchip/libraries/soc/d21x/SConscript | 10 + .../libraries/soc/d21x/drivers/drv_uart.c | 242 +++++++++++ .../libraries/soc/d21x/include/aic_soc.h | 45 ++ bsp/artinchip/libraries/soc/d21x/port/clock.c | 32 ++ .../libraries/soc/d21x/port/interrupt.c | 33 ++ .../libraries/soc/d21x/port/system.c | 25 ++ bsp/artinchip/libraries/soc/d21x/port/timer.c | 35 ++ 28 files changed, 1831 insertions(+) create mode 100644 bsp/artinchip/README.md create mode 100644 bsp/artinchip/README_zh.md create mode 100644 bsp/artinchip/d213ecv-demo-v4-0/.config create mode 100644 bsp/artinchip/d213ecv-demo-v4-0/.gitignore create mode 100644 bsp/artinchip/d213ecv-demo-v4-0/Kconfig create mode 100644 bsp/artinchip/d213ecv-demo-v4-0/README.md create mode 100644 bsp/artinchip/d213ecv-demo-v4-0/README_zh.md create mode 100644 bsp/artinchip/d213ecv-demo-v4-0/SConscript create mode 100644 bsp/artinchip/d213ecv-demo-v4-0/SConstruct create mode 100644 bsp/artinchip/d213ecv-demo-v4-0/applications/SConscript create mode 100644 bsp/artinchip/d213ecv-demo-v4-0/applications/main.c create mode 100644 bsp/artinchip/d213ecv-demo-v4-0/board/Kconfig create mode 100644 bsp/artinchip/d213ecv-demo-v4-0/board/SConscript create mode 100644 bsp/artinchip/d213ecv-demo-v4-0/board/board.c create mode 100644 bsp/artinchip/d213ecv-demo-v4-0/board/board.h create mode 100644 bsp/artinchip/d213ecv-demo-v4-0/link.lds create mode 100644 bsp/artinchip/d213ecv-demo-v4-0/rtconfig.h create mode 100644 bsp/artinchip/d213ecv-demo-v4-0/rtconfig.py create mode 100644 bsp/artinchip/libraries/Kconfig create mode 100644 bsp/artinchip/libraries/SConscript create mode 100644 bsp/artinchip/libraries/include/aic_uart_reg.h create mode 100644 bsp/artinchip/libraries/soc/d21x/SConscript create mode 100644 bsp/artinchip/libraries/soc/d21x/drivers/drv_uart.c create mode 100644 bsp/artinchip/libraries/soc/d21x/include/aic_soc.h create mode 100644 bsp/artinchip/libraries/soc/d21x/port/clock.c create mode 100644 bsp/artinchip/libraries/soc/d21x/port/interrupt.c create mode 100644 bsp/artinchip/libraries/soc/d21x/port/system.c create mode 100644 bsp/artinchip/libraries/soc/d21x/port/timer.c diff --git a/bsp/artinchip/README.md b/bsp/artinchip/README.md new file mode 100644 index 00000000000..ebfe7fb5135 --- /dev/null +++ b/bsp/artinchip/README.md @@ -0,0 +1,30 @@ +# ArtInChip BSPs + +[English](README.md) | [中文](README_zh.md) + +This directory provides RT-Thread board support for ArtInChip SoCs. + +| Board | SoC / CPU | Support | +|-------|-----------|-----------------| +| [D213ECV-DEMO-V4-0](d213ecv-demo-v4-0/README.md) | D213ECV / T-Head C906, RV64GC | M-mode base kernel, UART0 console | + +## Source organization + +```text +artinchip/ +├── d213ecv-demo-v4-0/ Board configuration and build entry +│ ├── applications/ Startup application +│ └── board/ Board wiring and initialization sequence +└── libraries/ + ├── include/ UART register definitions + └── soc/d21x/ + ├── include/ SoC addresses and MMIO accessors + ├── drivers/ D213 UART0 serial adapter + └── port/ CPU, clock, PLIC and timer platform hooks +``` + +Board wiring and memory layout are configured in the board directory. Shared D21x register definitions, drivers and platform initialization are in `libraries/soc/d21x`. + +The BSP uses the RT-Thread RISC-V common port for startup, context switching, FPU state, interrupts and system ticks. It provides UART0 with the required clock, reset and pin configuration. The bootloader initializes DDR, PLLs and memory/cache attributes before starting RT-Thread. + +See the [board README](d213ecv-demo-v4-0/README.md) for independent builds, configuration, loader requirements, validation status and source attribution. diff --git a/bsp/artinchip/README_zh.md b/bsp/artinchip/README_zh.md new file mode 100644 index 00000000000..e1faf322a6b --- /dev/null +++ b/bsp/artinchip/README_zh.md @@ -0,0 +1,30 @@ +# ArtInChip 板级支持包 + +[English](README.md) | [中文](README_zh.md) + +本目录提供 ArtInChip SoC 的 RT-Thread 板级支持。 + +| 开发板 | SoC / CPU | 支持功能 | +|--------|-----------|----------| +| [D213ECV-DEMO-V4-0](d213ecv-demo-v4-0/README_zh.md) | D213ECV / T-Head C906,RV64GC | M-mode 基础内核、UART0 控制台 | + +## 源码结构 + +```text +artinchip/ +├── d213ecv-demo-v4-0/ 板级配置与构建入口 +│ ├── applications/ 启动应用 +│ └── board/ 板级接线与初始化编排 +└── libraries/ + ├── include/ UART 寄存器定义 + └── soc/d21x/ + ├── include/ SoC 地址与 MMIO 访问接口 + ├── drivers/ D213 UART0 串口适配 + └── port/ CPU、时钟、PLIC 和定时器平台接口 +``` + +板级目录用于配置开发板接线和内存布局,`libraries/soc/d21x` 存放 D21x 共用的寄存器定义、驱动和平台初始化代码。 + +BSP 使用 RT-Thread RISC-V common port 完成启动、上下文切换、FPU 状态保存、中断处理和系统定时,提供 UART0 及所需的时钟、复位和引脚配置。Bootloader 在启动 RT-Thread 前完成 DDR、PLL 和内存/Cache 属性初始化。 + +独立构建、配置、loader 要求、验证状态和源码归属见 [开发板说明](d213ecv-demo-v4-0/README_zh.md)。 diff --git a/bsp/artinchip/d213ecv-demo-v4-0/.config b/bsp/artinchip/d213ecv-demo-v4-0/.config new file mode 100644 index 00000000000..4a3c21aa22f --- /dev/null +++ b/bsp/artinchip/d213ecv-demo-v4-0/.config @@ -0,0 +1,403 @@ + +# +# RT-Thread Kernel +# + +# +# klibc options +# + +# +# rt_vsnprintf options +# +# CONFIG_RT_KLIBC_USING_LIBC_VSNPRINTF is not set +CONFIG_RT_KLIBC_USING_VSNPRINTF_LONGLONG=y +CONFIG_RT_KLIBC_USING_VSNPRINTF_STANDARD=y +CONFIG_RT_KLIBC_USING_VSNPRINTF_DECIMAL_SPECIFIERS=y +CONFIG_RT_KLIBC_USING_VSNPRINTF_EXPONENTIAL_SPECIFIERS=y +CONFIG_RT_KLIBC_USING_VSNPRINTF_WRITEBACK_SPECIFIER=y +CONFIG_RT_KLIBC_USING_VSNPRINTF_CHECK_NUL_IN_FORMAT_SPECIFIER=y +# CONFIG_RT_KLIBC_USING_VSNPRINTF_MSVC_STYLE_INTEGER_SPECIFIERS is not set +CONFIG_RT_KLIBC_USING_VSNPRINTF_INTEGER_BUFFER_SIZE=32 +CONFIG_RT_KLIBC_USING_VSNPRINTF_DECIMAL_BUFFER_SIZE=32 +CONFIG_RT_KLIBC_USING_VSNPRINTF_FLOAT_PRECISION=6 +CONFIG_RT_KLIBC_USING_VSNPRINTF_MAX_INTEGRAL_DIGITS_FOR_DECIMAL=9 +CONFIG_RT_KLIBC_USING_VSNPRINTF_LOG10_TAYLOR_TERMS=4 +# end of rt_vsnprintf options + +# +# rt_vsscanf options +# +# CONFIG_RT_KLIBC_USING_LIBC_VSSCANF is not set +# end of rt_vsscanf options + +# +# rt_memset options +# +# CONFIG_RT_KLIBC_USING_USER_MEMSET is not set +# CONFIG_RT_KLIBC_USING_LIBC_MEMSET is not set +# CONFIG_RT_KLIBC_USING_TINY_MEMSET is not set +# end of rt_memset options + +# +# rt_memcpy options +# +# CONFIG_RT_KLIBC_USING_USER_MEMCPY is not set +# CONFIG_RT_KLIBC_USING_LIBC_MEMCPY is not set +# CONFIG_RT_KLIBC_USING_TINY_MEMCPY is not set +# end of rt_memcpy options + +# +# rt_memmove options +# +# CONFIG_RT_KLIBC_USING_USER_MEMMOVE is not set +# CONFIG_RT_KLIBC_USING_LIBC_MEMMOVE is not set +# end of rt_memmove options + +# +# rt_memcmp options +# +# CONFIG_RT_KLIBC_USING_USER_MEMCMP is not set +# CONFIG_RT_KLIBC_USING_LIBC_MEMCMP is not set +# end of rt_memcmp options + +# +# rt_strstr options +# +# CONFIG_RT_KLIBC_USING_USER_STRSTR is not set +# CONFIG_RT_KLIBC_USING_LIBC_STRSTR is not set +# end of rt_strstr options + +# +# rt_strcasecmp options +# +# CONFIG_RT_KLIBC_USING_USER_STRCASECMP is not set +# end of rt_strcasecmp options + +# +# rt_strncpy options +# +# CONFIG_RT_KLIBC_USING_USER_STRNCPY is not set +# CONFIG_RT_KLIBC_USING_LIBC_STRNCPY is not set +# end of rt_strncpy options + +# +# rt_strcpy options +# +# CONFIG_RT_KLIBC_USING_USER_STRCPY is not set +# CONFIG_RT_KLIBC_USING_LIBC_STRCPY is not set +# end of rt_strcpy options + +# +# rt_strncmp options +# +# CONFIG_RT_KLIBC_USING_USER_STRNCMP is not set +# CONFIG_RT_KLIBC_USING_LIBC_STRNCMP is not set +# end of rt_strncmp options + +# +# rt_strcmp options +# +# CONFIG_RT_KLIBC_USING_USER_STRCMP is not set +# CONFIG_RT_KLIBC_USING_LIBC_STRCMP is not set +# end of rt_strcmp options + +# +# rt_strlen options +# +# CONFIG_RT_KLIBC_USING_USER_STRLEN is not set +# CONFIG_RT_KLIBC_USING_LIBC_STRLEN is not set +# end of rt_strlen options + +# +# rt_strnlen options +# +# CONFIG_RT_KLIBC_USING_USER_STRNLEN is not set +# end of rt_strnlen options +# end of klibc options + +CONFIG_RT_NAME_MAX=16 +# CONFIG_RT_USING_ARCH_DATA_TYPE is not set +# CONFIG_RT_USING_NANO is not set +# CONFIG_RT_USING_SMART is not set +# CONFIG_RT_USING_AMP is not set +# CONFIG_RT_USING_SMP is not set +CONFIG_RT_CPUS_NR=1 +CONFIG_RT_ALIGN_SIZE=8 +# CONFIG_RT_THREAD_PRIORITY_8 is not set +CONFIG_RT_THREAD_PRIORITY_32=y +# CONFIG_RT_THREAD_PRIORITY_256 is not set +CONFIG_RT_THREAD_PRIORITY_MAX=32 +CONFIG_RT_TICK_PER_SECOND=1000 +CONFIG_RT_USING_OVERFLOW_CHECK=y +# CONFIG_RT_USING_HOOK is not set +# CONFIG_RT_USING_HOOKLIST is not set +# CONFIG_RT_USING_IDLE_HOOK is not set +CONFIG_IDLE_THREAD_STACK_SIZE=2048 +# CONFIG_RT_USING_TIMER_SOFT is not set +# CONFIG_RT_USING_CPU_USAGE_TRACER is not set + +# +# kservice options +# +# CONFIG_RT_USING_TINY_FFS is not set +# end of kservice options + +CONFIG_RT_USING_DEBUG=y +CONFIG_RT_DEBUGING_ASSERT=y +# CONFIG_RT_DEBUGING_COLOR is not set +# CONFIG_RT_DEBUGING_CONTEXT is not set +# CONFIG_RT_DEBUGING_AUTO_INIT is not set +# CONFIG_RT_USING_CI_ACTION is not set + +# +# Inter-Thread communication +# +CONFIG_RT_USING_SEMAPHORE=y +CONFIG_RT_USING_MUTEX=y +CONFIG_RT_USING_EVENT=y +CONFIG_RT_USING_MAILBOX=y +CONFIG_RT_USING_MESSAGEQUEUE=y +# CONFIG_RT_USING_MESSAGEQUEUE_PRIORITY is not set +# CONFIG_RT_USING_SIGNALS is not set +# end of Inter-Thread communication + +# +# Memory Management +# +# CONFIG_RT_USING_MEMPOOL is not set +CONFIG_RT_USING_SMALL_MEM=y +# CONFIG_RT_USING_SLAB is not set +# CONFIG_RT_USING_MEMHEAP is not set +CONFIG_RT_USING_SMALL_MEM_AS_HEAP=y +# CONFIG_RT_USING_MEMHEAP_AS_HEAP is not set +# CONFIG_RT_USING_SLAB_AS_HEAP is not set +# CONFIG_RT_USING_USERHEAP is not set +# CONFIG_RT_USING_NOHEAP is not set +# CONFIG_RT_USING_MEMTRACE is not set +# CONFIG_RT_USING_HEAP_ISR is not set +CONFIG_RT_USING_HEAP=y +# end of Memory Management + +CONFIG_RT_USING_DEVICE=y +CONFIG_RT_USING_DEVICE_OPS=y +# CONFIG_RT_USING_INTERRUPT_INFO is not set +# CONFIG_RT_USING_THREADSAFE_PRINTF is not set +CONFIG_RT_USING_CONSOLE=y +CONFIG_RT_CONSOLEBUF_SIZE=128 +CONFIG_RT_CONSOLE_DEVICE_NAME="uart0" +# CONFIG_RT_USING_CONSOLE_OUTPUT_CTL is not set +CONFIG_RT_VER_NUM=0x50400 +# CONFIG_RT_USING_STDC_ATOMIC is not set +CONFIG_RT_BACKTRACE_LEVEL_MAX_NR=32 +# end of RT-Thread Kernel + +CONFIG_ARCH_TEXT_OFFSET=0x200000 +CONFIG_ARCH_RAM_OFFSET=0 +CONFIG_ARCH_CPU_64BIT=y +CONFIG_RT_USING_CPU_FFS=y +CONFIG_ARCH_RISCV=y +CONFIG_ARCH_RISCV_FPU=y +CONFIG_ARCH_RISCV_FPU_D=y +CONFIG_ARCH_RISCV64=y + +# +# RISC-V Architecture Configuration +# +CONFIG_RT_USING_RISCV_NEW_COMMON=y +CONFIG_ARCH_RISCV_M_MODE=y +CONFIG_ARCH_RISCV_USING_IRQ_STACK=y +CONFIG_ARCH_RISCV_IRQ_STACK_SIZE=4096 +# CONFIG_RT_USING_USERSPACE_32BIT_LIMIT is not set +# end of RISC-V Architecture Configuration + +CONFIG_ARCH_USING_NEW_CTX_SWITCH=y + +# +# RT-Thread Components +# +CONFIG_RT_USING_COMPONENTS_INIT=y +CONFIG_RT_USING_USER_MAIN=y +CONFIG_RT_MAIN_THREAD_STACK_SIZE=4096 +CONFIG_RT_MAIN_THREAD_PRIORITY=10 +# CONFIG_RT_USING_LEGACY is not set +CONFIG_RT_USING_MSH=y +CONFIG_RT_USING_FINSH=y +CONFIG_FINSH_USING_MSH=y +CONFIG_FINSH_THREAD_NAME="tshell" +CONFIG_FINSH_THREAD_PRIORITY=20 +CONFIG_FINSH_THREAD_STACK_SIZE=4096 +CONFIG_FINSH_USING_HISTORY=y +CONFIG_FINSH_HISTORY_LINES=5 +# CONFIG_FINSH_USING_WORD_OPERATION is not set +# CONFIG_FINSH_USING_FUNC_EXT is not set +CONFIG_FINSH_USING_SYMTAB=y +CONFIG_FINSH_CMD_SIZE=80 +CONFIG_MSH_USING_BUILT_IN_COMMANDS=y +CONFIG_FINSH_USING_DESCRIPTION=y +# CONFIG_FINSH_ECHO_DISABLE_DEFAULT is not set +# CONFIG_FINSH_USING_AUTH is not set +CONFIG_FINSH_ARG_MAX=10 +CONFIG_FINSH_USING_OPTION_COMPLETION=y + +# +# DFS: device virtual file system +# +# CONFIG_RT_USING_DFS is not set +# end of DFS: device virtual file system + +# CONFIG_RT_USING_FAL is not set + +# +# Device Drivers +# +# CONFIG_RT_USING_DM is not set +# CONFIG_RT_USING_DEV_BUS is not set +CONFIG_RT_USING_DEVICE_IPC=y +CONFIG_RT_UNAMED_PIPE_NUMBER=64 +# CONFIG_RT_USING_SYSTEM_WORKQUEUE is not set +CONFIG_RT_USING_SERIAL=y +CONFIG_RT_USING_SERIAL_V1=y +# CONFIG_RT_USING_SERIAL_V2 is not set +# CONFIG_RT_SERIAL_USING_DMA is not set +CONFIG_RT_SERIAL_RB_BUFSZ=512 +# CONFIG_RT_USING_SERIAL_BYPASS is not set +# CONFIG_RT_USING_CAN is not set +# CONFIG_RT_USING_ADC is not set +# CONFIG_RT_USING_CLOCK_TIME is not set +# CONFIG_RT_USING_I2C is not set +# CONFIG_RT_USING_PHY is not set +# CONFIG_RT_USING_PHY_V2 is not set +# CONFIG_RT_USING_DAC is not set +# CONFIG_RT_USING_NULL is not set +# CONFIG_RT_USING_ZERO is not set +# CONFIG_RT_USING_RANDOM is not set +# CONFIG_RT_USING_PWM is not set +# CONFIG_RT_USING_PULSE_ENCODER is not set +# CONFIG_RT_USING_INPUT_CAPTURE is not set +# CONFIG_RT_USING_MTD_NOR is not set +# CONFIG_RT_USING_MTD_NAND is not set +# CONFIG_RT_USING_PM is not set +# CONFIG_RT_USING_RTC is not set +# CONFIG_RT_USING_SDIO is not set +# CONFIG_RT_USING_SPI is not set +# CONFIG_RT_USING_WDT is not set +# CONFIG_RT_USING_AUDIO is not set +# CONFIG_RT_USING_SENSOR is not set +# CONFIG_RT_USING_TOUCH is not set +# CONFIG_RT_USING_LCD is not set +# CONFIG_RT_USING_HWCRYPTO is not set +# CONFIG_RT_USING_WIFI is not set +# CONFIG_RT_USING_RPMSG is not set +# CONFIG_RT_USING_BLK is not set +# CONFIG_RT_USING_REGULATOR is not set +# CONFIG_RT_USING_POWER_SUPPLY is not set +# CONFIG_RT_USING_PIN is not set +# CONFIG_RT_USING_CHERRYUSB is not set +# end of Device Drivers + +# +# C/C++ and POSIX layer +# + +# +# ISO-ANSI C layer +# + +# +# Timezone and Daylight Saving Time +# +# CONFIG_RT_LIBC_USING_FULL_TZ_DST is not set +CONFIG_RT_LIBC_USING_LIGHT_TZ_DST=y +CONFIG_RT_LIBC_TZ_DEFAULT_HOUR=8 +CONFIG_RT_LIBC_TZ_DEFAULT_MIN=0 +CONFIG_RT_LIBC_TZ_DEFAULT_SEC=0 +# end of Timezone and Daylight Saving Time +# end of ISO-ANSI C layer + +# +# POSIX (Portable Operating System Interface) layer +# +# CONFIG_RT_USING_POSIX_FS is not set +# CONFIG_RT_USING_POSIX_DELAY is not set +# CONFIG_RT_USING_POSIX_CLOCK is not set +# CONFIG_RT_USING_POSIX_TIMER is not set +# CONFIG_RT_USING_PTHREADS is not set +# CONFIG_RT_USING_MODULE is not set + +# +# Interprocess Communication (IPC) +# +# CONFIG_RT_USING_POSIX_PIPE is not set +# CONFIG_RT_USING_POSIX_MESSAGE_QUEUE is not set +# CONFIG_RT_USING_POSIX_MESSAGE_SEMAPHORE is not set + +# +# Socket is in the 'Network' category +# +# end of Interprocess Communication (IPC) +# end of POSIX (Portable Operating System Interface) layer + +# CONFIG_RT_USING_CPLUSPLUS is not set +# end of C/C++ and POSIX layer + +# +# Network +# +# CONFIG_RT_USING_SAL is not set +# CONFIG_RT_USING_NETDEV is not set +# CONFIG_RT_USING_LWIP is not set +# CONFIG_RT_USING_AT is not set +# end of Network + +# +# Memory protection +# +# CONFIG_RT_USING_MEM_PROTECTION is not set +# CONFIG_RT_USING_HW_STACK_GUARD is not set +# end of Memory protection + +# +# Utilities +# +# CONFIG_RT_USING_RYM is not set +# CONFIG_RT_USING_ULOG is not set +# CONFIG_RT_USING_UTEST is not set +# CONFIG_RT_USING_VAR_EXPORT is not set +# CONFIG_RT_USING_RESOURCE_ID is not set +CONFIG_RT_USING_ADT=y +# CONFIG_RT_USING_ADT_AVL is not set +CONFIG_RT_USING_ADT_BITMAP=y +# CONFIG_RT_USING_ADT_HASHMAP is not set +# CONFIG_RT_USING_ADT_REF is not set +# CONFIG_RT_USING_RT_LINK is not set +# end of Utilities + +# +# Using USB legacy version +# +# CONFIG_RT_USING_USB_HOST is not set +# CONFIG_RT_USING_USB_DEVICE is not set +# end of Using USB legacy version + +# CONFIG_RT_USING_FDT is not set +# CONFIG_RT_USING_LEGACY_VIRTIO is not set +# CONFIG_RT_USING_RUST is not set +# end of RT-Thread Components + +# +# RT-Thread Utestcases +# +# CONFIG_RT_USING_UTESTCASES is not set +# end of RT-Thread Utestcases + +CONFIG_AIC_SOC_D21X=y +CONFIG_SOC_D213=y + +# +# Board options +# +CONFIG_BSP_USING_UART0=y +CONFIG_BSP_UART_CLK_FREQ=48000000 +CONFIG_BSP_CLINT_FREQ=4000000 +# end of Board options diff --git a/bsp/artinchip/d213ecv-demo-v4-0/.gitignore b/bsp/artinchip/d213ecv-demo-v4-0/.gitignore new file mode 100644 index 00000000000..376664dab29 --- /dev/null +++ b/bsp/artinchip/d213ecv-demo-v4-0/.gitignore @@ -0,0 +1,10 @@ +build/ +dist/ +.sconsign.dblite +__pycache__/ +rtthread.elf +rtthread.bin +rtthread.map +rtthread.asm +.cconfig.h +.config.old diff --git a/bsp/artinchip/d213ecv-demo-v4-0/Kconfig b/bsp/artinchip/d213ecv-demo-v4-0/Kconfig new file mode 100644 index 00000000000..92b9778ae4b --- /dev/null +++ b/bsp/artinchip/d213ecv-demo-v4-0/Kconfig @@ -0,0 +1,11 @@ +mainmenu "RT-Thread Project Configuration" + +BSP_DIR := . +RTT_DIR := ../../.. +PKGS_DIR := packages + +source "$(RTT_DIR)/Kconfig" +osource "$(PKGS_DIR)/Kconfig" +orsource "libraries/Kconfig" +orsource "../libraries/Kconfig" +rsource "board/Kconfig" diff --git a/bsp/artinchip/d213ecv-demo-v4-0/README.md b/bsp/artinchip/d213ecv-demo-v4-0/README.md new file mode 100644 index 00000000000..0ecbd1dc619 --- /dev/null +++ b/bsp/artinchip/d213ecv-demo-v4-0/README.md @@ -0,0 +1,147 @@ +# D213ECV-DEMO-V4-0 + +[English](README.md) | [中文](README_zh.md) + +## Hardware + +- Board: ArtInChip D213ECV-DEMO-V4-0. +- SoC: D213ECV, T-Head C906, RV64GC (IMAFDC), single hart, M-mode. +- Memory: 128 MiB DDR3 at `0x40000000`. +- Console: UART0, PA.0 TX / PA.1 RX, 115200 baud, 8N1, no flow control. +- [Official board information](https://aicdoc.artinchip.com/topics/product/d213ecV4.html). + +| Function | Support | +|----------|-------------| +| Startup, scheduler, double-precision FPU context | RT-Thread RISC-V common port | +| System tick | D213 CLINT comparator and time CSR, common timer core | +| Interrupt controller | Common PLIC driver, D213 priority-enable setup | +| Clock/reset | GPIO and UART0 gates only; loader retains PLL/DDR ownership | +| UART0 | Serial-v1, polling TX and a thread feeding the standard RX FIFO | +| FinSH/MSH, heap, device framework | Enabled by default | + +The UART adapter keeps the UART0 PLIC line masked and polls RX at a nominal 1 ms interval. The RX thread runs one priority level above FinSH so console output cannot starve polling; the default serial receive buffer is 512 bytes. It is intended for an interactive console; sustained transfers or long scheduling delays can lose received data. UART hardware interrupts and DMA are not supported. Selecting serial-v2 disables the UART0 option because this adapter uses the serial-v1 API. + +## UART configuration + +UART0 is enabled by default with `BSP_USING_UART0`. It uses PA.0 TX / PA.1 RX (function 5) and retains the loader-configured input clock, 48 MHz by default. + +Use `RT_DEVICE_CTRL_CONFIG` with `struct serial_configure` before opening the port. The adapter accepts 5–8 data bits, no/odd/even parity, and one or two stop bits. Five data bits with two stop bits is rejected: the hardware would produce 1.5 stop bits, which serial-v1 cannot express. Unsupported formats, flow control, inverted/MSB-first operation and divisors outside 1–65535 return `-RT_EINVAL` before changing registers. Configuration clears FIFOs, waits up to 100 ms for the controller to become idle and checks LCR readback before accessing the divisor registers. A busy timeout returns `-RT_ETIMEOUT`; rejected LCR writes return `-RT_EIO`. Baud rates use the nearest integer divisor; the resulting baud error depends on the input clock and requested rate. + +Open with `RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX` to use the framework's receive FIFO and callback, or use polling reads without `INT_RX`. Here `INT_RX` selects the serial-v1 buffered interface; hardware IRQ reception remains disabled. Closing the device disables buffered reception. Reconfiguration resets the hardware FIFOs, so configure only with traffic stopped. DMA and RS-485 direction control are not provided. + +## Build independently + +Install Python 3, SCons, kconfiglib and a GNU bare-metal RISC-V toolchain providing `riscv64-unknown-elf-*`, RV64GC and the `lp64d` ABI: + +```sh +python -m pip install scons kconfiglib +cd bsp/artinchip/d213ecv-demo-v4-0 +export RTT_EXEC_PATH=/path/to/riscv-toolchain/bin +scons --pyconfig-silent +scons -j8 +``` + +On PowerShell, set the toolchain path with: + +```powershell +$env:RTT_EXEC_PATH = 'C:/path/to/riscv-toolchain/bin' +scons --pyconfig-silent +scons -j8 +``` + +The compiler can also be added to PATH. Set `RTT_CC_PREFIX` when using a different toolchain prefix. The build produces `rtthread.elf`, `rtthread.bin` and `rtthread.map`. + +Local builds use Xuantie V2.6.1 (GCC 10.2.0); CI uses RT-Thread's public GCC 10.2.0 RISC-V toolchain. For GCC 12 and later, `rtconfig.py` adds explicit `zicsr_zifencei` to the ISA string. Those newer toolchains have not been validated for this BSP. + +## Configuration and distribution + +The default `.config` enables UART0/Serial-v1, MSH, heap and the base kernel, with a 1000 Hz system tick. Configure the project and generate `rtconfig.h` with: + +```sh +scons --menuconfig +scons --pyconfig-silent +scons -j8 +scons --dist +``` + +Board options include UART0 selection, the UART input clock (default 48 MHz) and the CLINT timebase (default 4 MHz). Set the frequency values to match the bootloader configuration; these options do not change hardware dividers. Generate `rtconfig.h` with the configuration tools above. + +Extract `dist/project.zip` into a separate directory and unset `RTT_ROOT`. Run the same configuration and build commands in the extracted `project/` directory. The archive contains the BSP, its libraries, RT-Thread sources and build tools. + +## Memory and startup requirements + +| Region | Address / purpose | +|--------|-------------------| +| DDR3 | `0x40000000..0x47ffffff`, 128 MiB | +| RT-Thread | `0x40000000..0x477fffff`, 120 MiB | +| Loader reservation | `0x47800000..0x47ffffff`, upper 8 MiB | +| Binary load address / ELF entry | `0x40000000` | + +The heap starts after static sections and the early stack, and ends at `0x47800000`. The early common-port stack is 16-byte aligned; the interrupt stack resides in BSS. Linker assertions check section ordering, stack alignment and heap bounds. + +An ArtInChip-compatible external loader must: + +1. Initialize DDR, PLLs, bus clocks and CPU memory/cache attributes. +2. Configure UART0 for a 48 MHz input clock and provide a 4 MHz timebase, or use matching BSP frequency settings. +3. Load the image at `0x40000000`, clean loaded data and synchronize the instruction cache if caches are enabled. +4. Enter the first instruction on hart 0 in M-mode, with interrupts disabled, MMIO non-cacheable, no active DMA and no outstanding claimed PLIC IRQs. + +The BSP retains the bootloader's DDR, PLL, UART0 clock-divider and cache configuration. The RISC-V common port handles startup and PLIC claim/complete; board initialization sets up the C906, peripheral gates and timer access. + +## Load and use the console + +Package `rtthread.bin` in the FIT or firmware format required by your ArtInChip bootloader, then load it at `0x40000000`. Use a bootloader with DDR initialization and a matching partition layout. + +For a `demo128-nand` full image on 2 KiB-page NAND, keep the assembled `bootloader.aic` within 411,648 bytes, including PBP, headers and resources. This is the SPL page-table limit. Use the existing bootloader and partition layout for an OS-only update. + +After loading, connect UART0 at 115200 8N1. Expected output includes: + +```text +RT-Thread Operating System +D213ECV-DEMO-V4-0 +msh > +``` + +| Command | Purpose | +|---------|---------| +| `help` | List shell commands | +| `version` | Show RT-Thread version | +| `ps` or `list thread` | List threads | +| `list device` | List registered devices; the default exposes `uart0` | +| `list timer` | List timers and the current tick | +| `free` | Show heap usage | + +The default thread list includes `tshell`, `uart0rx` and `tidle0` after the startup application exits. + +## Validation status + +The UART0 image built on November 14, 2026 at 02:39:03 reached MSH on the board. Serial logs confirm repeated `help`, `list device`, `list timer` and `ps` commands, with only `uart0` registered. The receive thread runs at priority 19, above the shell at priority 20. Board testing also confirmed stable MSH startup after a full power cycle and repeated command entry while `help` and `ps` output was in progress. + +| Check | Result | +|-------|--------| +| Default UART0 configuration and independent SCons build | Passed with GCC 10.2.0 | +| No-UART and serial-v2 exclusion configurations | Passed with GCC 10.2.0 | +| Dist export and independent rebuild outside the repository | Passed with GCC 10.2.0 | +| UART0 formats, parameter validation, TX timeout, RX close/reopen and DLAB protection | Host MMIO simulation passed; 21 valid formats | +| SPI-NAND loader handoff, FIT CRC32 and RT-Thread startup | Passed on the previous UART0 revision | +| UART0 TX/RX, repeated MSH `help` and `ps` | Confirmed by current image board logs | +| MSH `version` and `free` | Passed on the previous UART0 revision | +| Device/timer enumeration | Current image reports only `uart0`, receive-thread timer and current tick | +| Full power-cycle startup and command input during `help` / `ps` output | Passed on the current UART0 image | +| Tick accuracy, preemption and repeated wakeup | Pending | +| Heap stress/alignment and FPU/FCSR preservation under preemption/IRQs | Pending | +| Controlled external PLIC delivery/complete, reset cycles and soak | Pending | + +## Source and license + +The BSP is licensed under Apache-2.0. + +SoC/register definitions, hardware sequences and board pinmux derive from the public [ArtInChip Luban-Lite SDK](https://gitee.com/artinchip/luban-lite). The following table lists the vendor source paths: + +| BSP content | SDK source | License | +|-------------|------------|---------| +| D213 addresses and CMU gate/reset bits | `bsp/artinchip/sys/d21x/include/aic_soc.h`, `aic_clk_id.h` | Apache-2.0 | +| C906 and PLIC setup | `bsp/artinchip/sys/d21x/system.c` | Apache-2.0 | +| UART registers/programming | `bsp/artinchip/hal/uart/aic_hal_uart.c` | Apache-2.0 | +| GPIO pin configuration | `bsp/artinchip/hal/gpio/aic_hal_gpio.c`, `target/d21x/demo128-nand/pinmux.c` | Apache-2.0 | +| Gate/reset sequencing | `bsp/artinchip/hal/cmu/aic_hal_clk.c` | Apache-2.0 | diff --git a/bsp/artinchip/d213ecv-demo-v4-0/README_zh.md b/bsp/artinchip/d213ecv-demo-v4-0/README_zh.md new file mode 100644 index 00000000000..b41fce922c6 --- /dev/null +++ b/bsp/artinchip/d213ecv-demo-v4-0/README_zh.md @@ -0,0 +1,147 @@ +# D213ECV-DEMO-V4-0 + +[English](README.md) | [中文](README_zh.md) + +## 硬件信息 + +- 开发板:ArtInChip D213ECV-DEMO-V4-0。 +- SoC:D213ECV,T-Head C906,RV64GC(IMAFDC),单 hart,M-mode。 +- 内存:128 MiB DDR3,起始地址 `0x40000000`。 +- 控制台:UART0,PA.0 TX / PA.1 RX,115200 波特率、8N1、无流控。 +- [官方开发板资料](https://aicdoc.artinchip.com/topics/product/d213ecV4.html)。 + +| 功能 | 支持情况 | +|------|----------| +| 启动、调度器、双精度 FPU 上下文 | RT-Thread RISC-V common port | +| 系统 Tick | D213 CLINT 比较器与 time CSR,复用 common 定时器核心 | +| 中断控制器 | common PLIC 驱动,补充 D213 priority-enable 初始化 | +| 时钟/复位 | GPIO 与 UART0 的门控;PLL/DDR 由 loader 管理 | +| UART0 | Serial-v1,轮询发送,接收线程填充标准接收 FIFO | +| FinSH/MSH、堆、设备框架 | 默认开启 | + +UART 适配器保持UART0 的 PLIC 中断屏蔽,以标称 1 ms 间隔轮询接收。接收线程比 FinSH 高一个优先级,避免控制台持续输出时接收轮询无法运行;默认串口接收缓冲区为 512 字节。该方案面向交互式控制台,持续大流量传输或调度长时间延迟可能丢字节。不支持 UART 硬件中断和 DMA;由于使用 Serial-v1 API,选择 Serial-v2 时会关闭 UART0 选项。 + +## UART 配置 + +通过 `BSP_USING_UART0` 默认启用 UART0,使用 PA.0 TX / PA.1 RX(功能 5),沿用 Bootloader 配置的输入时钟,默认为 48 MHz。 + +打开设备前,通过 `RT_DEVICE_CTRL_CONFIG` 和 `struct serial_configure` 配置。适配器接受 5–8 位数据、无/奇/偶校验,以及 1 或 2 位停止位。5 位数据加 2 位停止位会被拒绝:硬件实际产生 1.5 位停止位,Serial-v1 无法表达。不支持的格式、流控、反相/MSB 优先,以及超出 1–65535 的分频值均返回 `-RT_EINVAL`,且不修改寄存器。配置时先清空 FIFO,最多等待 100 ms 让控制器进入空闲状态,并回读 LCR 确认切换成功后才访问分频寄存器。忙等待超时返回 `-RT_ETIMEOUT`,LCR 写入未生效返回 `-RT_EIO`。波特率采用最接近的整数分频,实际误差取决于输入时钟和请求值。 + +使用 `RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX` 打开可使用框架接收 FIFO 和回调;不带 `INT_RX` 时可轮询读取。这里的 `INT_RX` 选择 Serial-v1 缓冲接口,硬件中断接收仍关闭。关闭设备后停止缓冲接收。重新配置会清空硬件 FIFO,应停止收发后再配置。不提供 DMA 和 RS-485 方向控制。 + +## 独立构建 + +安装 Python 3、SCons、kconfiglib,以及支持 RV64GC、`lp64d` ABI 并提供 `riscv64-unknown-elf-*` 的 GNU 裸机 RISC-V 工具链: + +```sh +python -m pip install scons kconfiglib +cd bsp/artinchip/d213ecv-demo-v4-0 +export RTT_EXEC_PATH=/path/to/riscv-toolchain/bin +scons --pyconfig-silent +scons -j8 +``` + +PowerShell 使用以下方式设置工具链路径: + +```powershell +$env:RTT_EXEC_PATH = 'C:/path/to/riscv-toolchain/bin' +scons --pyconfig-silent +scons -j8 +``` + +也可将编译器加入 PATH,通过 `RTT_CC_PREFIX` 指定其他工具链前缀。构建产物为 `rtthread.elf`、`rtthread.bin` 和 `rtthread.map`。 + +本地构建使用 Xuantie V2.6.1(GCC 10.2.0),CI 使用 RT-Thread 公开的 GCC 10.2.0 RISC-V 工具链。针对 GCC 12 及以上版本,`rtconfig.py` 会在 ISA 字符串中显式加入 `zicsr_zifencei`;本 BSP 尚未实际验证这些新版工具链。 + +## 配置与独立工程导出 + +默认 `.config` 启用 UART0/Serial-v1、MSH、堆和基础内核,系统 Tick 为 1000 Hz。使用以下命令配置工程并生成 `rtconfig.h`: + +```sh +scons --menuconfig +scons --pyconfig-silent +scons -j8 +scons --dist +``` + +板级选项包括 UART0 开关、UART 输入时钟(默认 48 MHz)及 CLINT 时基(默认 4 MHz)。频率设置应与 Bootloader 配置一致,这些选项不会改变硬件分频器。`rtconfig.h` 由上述配置工具生成。 + +将 `dist/project.zip` 解压到独立目录,清除 `RTT_ROOT`,在解压后的 `project/` 目录中运行相同的配置及构建命令。导出包包含 BSP、所需库文件、RT-Thread 源码及构建工具。 + +## 内存与启动要求 + +| 区域 | 地址 / 用途 | +|------|-------------| +| DDR3 | `0x40000000..0x47ffffff`,128 MiB | +| RT-Thread | `0x40000000..0x477fffff`,120 MiB | +| loader 保留区 | `0x47800000..0x47ffffff`,顶部 8 MiB | +| BIN 加载地址 / ELF 入口 | `0x40000000` | + +堆起点位于静态段和早期栈之后,终点为 `0x47800000`。common port 早期栈按 16 字节对齐,中断栈位于 BSS。链接脚本检查段顺序、栈对齐和堆边界。 + +外部 ArtInChip 兼容 loader 必须: + +1. 初始化 DDR、PLL、总线时钟及 CPU 内存/Cache 属性。 +2. 将 UART0 输入时钟配置为 48 MHz,并提供 4 MHz 时基,或使 BSP 频率设置与实际值一致。 +3. 将镜像加载到 `0x40000000`;Cache 开启时清理已加载数据并同步指令 Cache。 +4. 在 hart 0、M-mode 下进入第一条指令,进入时中断关闭、MMIO 不可缓存,无活动 DMA,也无尚未完成的已 claim PLIC 中断。 + +BSP 沿用 Bootloader 的 DDR、PLL、UART0 时钟分频和 Cache 配置。RISC-V common port 负责启动及 PLIC claim/complete,板级代码完成 C906、外设门控和定时器访问初始化。 + +## 加载与控制台使用 + +根据所用 ArtInChip Bootloader 的要求,将 `rtthread.bin` 打包为 FIT 或烧录镜像,并加载到 `0x40000000`。Bootloader 需包含 DDR 初始化程序,并使用匹配的分区布局。 + +使用 2 KiB 页 NAND 的 `demo128-nand` 整包时,拼接后的 `bootloader.aic` 应控制在 411,648 字节以内,包含 PBP、头部和资源。这是 SPL 页表的容量限制。仅更新 OS 时,沿用现有 Bootloader 和分区布局。 + +加载后,以 115200 8N1 连接 UART0,应出现以下输出: + +```text +RT-Thread Operating System +D213ECV-DEMO-V4-0 +msh > +``` + +| 命令 | 用途 | +|------|------| +| `help` | 列出 shell 命令 | +| `version` | 查看 RT-Thread 版本 | +| `ps` 或 `list thread` | 查看线程 | +| `list device` | 查看注册设备;默认包含 `uart0` | +| `list timer` | 查看定时器及当前 Tick | +| `free` | 查看堆使用情况 | + +启动应用退出后,默认线程列表包含 `tshell`、`uart0rx` 和 `tidle0`。 + +## 验证状态 + +2026 年 11 月 14 日 02:39:03 构建的 UART0 镜像已在实机进入 MSH。串口日志确认重复执行 `help`,以及 `list device`、`list timer` 和 `ps` 正常,仅注册 `uart0`。接收线程优先级为 19,高于 shell 的 20。实机验证还确认,完全断电后重新上电可稳定进入 MSH,在 `help`、`ps` 输出过程中重复输入下一条命令均正常。 + +| 检查项 | 结果 | +|--------|------| +| 默认 UART0 配置及独立 SCons 构建 | GCC 10.2.0 下通过 | +| 无 UART 和 Serial-v2 排除配置 | GCC 10.2.0 下通过 | +| dist 导出及仓库外独立重建 | GCC 10.2.0 下通过 | +| UART0 格式、参数校验、发送超时、接收关闭/重开及 DLAB 保护 | 主机 MMIO 模拟通过,覆盖 21 种有效格式 | +| SPI NAND loader 交接、FIT CRC32、RT-Thread 启动 | 上一 UART0 版本实机通过 | +| UART0 收发、重复执行 MSH `help` 及 `ps` | 本次镜像实机日志确认 | +| MSH `version` 和 `free` | 上一 UART0 版本实机通过 | +| 设备/定时器枚举 | 本次镜像仅注册 `uart0`,已输出接收线程定时器及当前 Tick | +| 完全断电后启动及 `help` / `ps` 输出期间输入命令 | 本次 UART0 镜像实机通过 | +| Tick 精度、抢占及反复唤醒 | 待验证 | +| 堆压力/对齐及抢占、中断下的 FPU/FCSR 保存 | 待验证 | +| 受控外部 PLIC 投递/完成、反复复位和长时间运行 | 待验证 | + +## 源码与许可证 + +BSP 使用 Apache-2.0 许可证。 + +SoC/寄存器定义、硬件时序及板级 pinmux 来自公开的 [ArtInChip Luban-Lite SDK](https://gitee.com/artinchip/luban-lite)。原厂源码路径如下: + +| BSP 内容 | SDK 来源 | 许可证 | +|----------|----------|--------| +| D213 地址、CMU 门控/复位位 | `bsp/artinchip/sys/d21x/include/aic_soc.h`、`aic_clk_id.h` | Apache-2.0 | +| C906 和 PLIC 初始化 | `bsp/artinchip/sys/d21x/system.c` | Apache-2.0 | +| UART 寄存器/编程 | `bsp/artinchip/hal/uart/aic_hal_uart.c` | Apache-2.0 | +| GPIO 引脚配置 | `bsp/artinchip/hal/gpio/aic_hal_gpio.c`、`target/d21x/demo128-nand/pinmux.c` | Apache-2.0 | +| 门控/复位时序 | `bsp/artinchip/hal/cmu/aic_hal_clk.c` | Apache-2.0 | diff --git a/bsp/artinchip/d213ecv-demo-v4-0/SConscript b/bsp/artinchip/d213ecv-demo-v4-0/SConscript new file mode 100644 index 00000000000..07fb08dced5 --- /dev/null +++ b/bsp/artinchip/d213ecv-demo-v4-0/SConscript @@ -0,0 +1,5 @@ +from building import * + +objs = SConscript('board/SConscript') +objs += SConscript('applications/SConscript') +Return('objs') diff --git a/bsp/artinchip/d213ecv-demo-v4-0/SConstruct b/bsp/artinchip/d213ecv-demo-v4-0/SConstruct new file mode 100644 index 00000000000..3e09818a7b4 --- /dev/null +++ b/bsp/artinchip/d213ecv-demo-v4-0/SConstruct @@ -0,0 +1,28 @@ +import os +import sys +import rtconfig + +RTT_ROOT = os.getenv('RTT_ROOT') or os.path.join(os.getcwd(), '..', '..', '..') +sys.path.append(os.path.join(RTT_ROOT, 'tools')) +from building import * + +TARGET = 'rtthread.' + rtconfig.TARGET_EXT + +DefaultEnvironment(tools=[]) +env = Environment(tools=['mingw'], + AS=rtconfig.AS, ASFLAGS=rtconfig.AFLAGS, + CC=rtconfig.CC, CCFLAGS=rtconfig.CFLAGS, + CXX=rtconfig.CXX, CXXFLAGS=rtconfig.CXXFLAGS, + AR=rtconfig.AR, ARFLAGS='-rc', + LINK=rtconfig.LINK, LINKFLAGS=rtconfig.LFLAGS) +env.PrependENVPath('PATH', rtconfig.EXEC_PATH) +env['ASCOM'] = env['ASPPCOM'] + +Export('RTT_ROOT', 'rtconfig') +objs = PrepareBuilding(env, RTT_ROOT, has_libcpu=False) + +# The dist project carries the shared SoC sources inside its own root. +libraries = 'libraries' if os.path.isdir('libraries') else '../libraries' +objs += SConscript(os.path.join(libraries, 'SConscript'), + variant_dir='build/libraries', duplicate=0) +DoBuilding(TARGET, objs) diff --git a/bsp/artinchip/d213ecv-demo-v4-0/applications/SConscript b/bsp/artinchip/d213ecv-demo-v4-0/applications/SConscript new file mode 100644 index 00000000000..bf8f3af7cae --- /dev/null +++ b/bsp/artinchip/d213ecv-demo-v4-0/applications/SConscript @@ -0,0 +1,4 @@ +from building import * + +group = DefineGroup('Applications', ['main.c'], depend=['']) +Return('group') diff --git a/bsp/artinchip/d213ecv-demo-v4-0/applications/main.c b/bsp/artinchip/d213ecv-demo-v4-0/applications/main.c new file mode 100644 index 00000000000..0e2616c928f --- /dev/null +++ b/bsp/artinchip/d213ecv-demo-v4-0/applications/main.c @@ -0,0 +1,13 @@ +/* + * Copyright (c) 2026, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +int main(void) +{ + rt_kprintf("D213ECV-DEMO-V4-0\n"); + return 0; +} diff --git a/bsp/artinchip/d213ecv-demo-v4-0/board/Kconfig b/bsp/artinchip/d213ecv-demo-v4-0/board/Kconfig new file mode 100644 index 00000000000..b1a36044c83 --- /dev/null +++ b/bsp/artinchip/d213ecv-demo-v4-0/board/Kconfig @@ -0,0 +1,38 @@ +config SOC_D213 + bool + default y + select AIC_SOC_D21X + select ARCH_CPU_64BIT + select ARCH_RISCV + select ARCH_RISCV64 + select ARCH_RISCV_FPU + select ARCH_RISCV_FPU_D + select ARCH_RISCV_M_MODE + select RT_USING_RISCV_NEW_COMMON + select RT_USING_ADT + select RT_USING_ADT_BITMAP + select RT_USING_COMPONENTS_INIT + select RT_USING_USER_MAIN + +menu "Board options" + +config BSP_USING_UART0 + bool "Enable UART0 console" + depends on RT_USING_SERIAL_V1 + default y + help + UART0 on PA.0/PA.1. Transmit is polled; a polling thread supplies + the serial-v1 receive FIFO. UART hardware interrupts and DMA are + not supported in this initial BSP. + +config BSP_UART_CLK_FREQ + int "Bootloader-configured UART0 input clock (Hz)" + range 1843200 200000000 + default 48000000 + +config BSP_CLINT_FREQ + int "Bootloader-configured CLINT timebase (Hz)" + range 1000000 100000000 + default 4000000 + +endmenu diff --git a/bsp/artinchip/d213ecv-demo-v4-0/board/SConscript b/bsp/artinchip/d213ecv-demo-v4-0/board/SConscript new file mode 100644 index 00000000000..9bb16647ddf --- /dev/null +++ b/bsp/artinchip/d213ecv-demo-v4-0/board/SConscript @@ -0,0 +1,5 @@ +from building import * + +group = DefineGroup('Board', ['board.c'], depend=[''], + CPPPATH=[GetCurrentDir()]) +Return('group') diff --git a/bsp/artinchip/d213ecv-demo-v4-0/board/board.c b/bsp/artinchip/d213ecv-demo-v4-0/board/board.c new file mode 100644 index 00000000000..532d9d22dff --- /dev/null +++ b/bsp/artinchip/d213ecv-demo-v4-0/board/board.c @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2022-2026, ArtInChip Technology Co., Ltd + * Copyright (c) 2026, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include "board.h" + +extern char __heap_start[], __heap_end[]; +extern int rt_hw_tick_init(void); + +void rt_hw_board_init(void) +{ + riscv_isa_ext_set(I); + riscv_isa_ext_set(M); + riscv_isa_ext_set(A); + riscv_isa_ext_set(F); + riscv_isa_ext_set(D); + riscv_isa_ext_set(C); + + riscv_timer_set_frequency(BSP_CLINT_FREQ); + aic_soc_init(); + aic_interrupt_init(); + aic_clock_init(); + +#ifdef BSP_USING_UART0 + /* D213ECV-DEMO-V4-0: PA.0 TX / PA.1 RX, function 5, drive 3. */ + aic_write32(AIC_GPIO_BASE + 0x80, 0x35); + aic_write32(AIC_GPIO_BASE + 0x84, 0x335); +#endif + +#ifdef RT_USING_HEAP + rt_system_heap_init(__heap_start, __heap_end); +#endif + + rt_hw_interrupt_init(); + rt_hw_tick_init(); + +#ifdef BSP_USING_UART0 + rt_hw_serial_init(); +#endif + +#ifdef RT_USING_COMPONENTS_INIT + rt_components_board_init(); +#endif + +#if defined(RT_USING_CONSOLE) && defined(BSP_USING_UART0) + rt_console_set_device(RT_CONSOLE_DEVICE_NAME); +#endif +} diff --git a/bsp/artinchip/d213ecv-demo-v4-0/board/board.h b/bsp/artinchip/d213ecv-demo-v4-0/board/board.h new file mode 100644 index 00000000000..0918d6b64b3 --- /dev/null +++ b/bsp/artinchip/d213ecv-demo-v4-0/board/board.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2026, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef __BOARD_H__ +#define __BOARD_H__ + +#include +#include +#include "aic_soc.h" + +#define MAX_HANDLERS 128 +#define INTC_TYPE INTC_TYPE_RISCV + +void rt_hw_serial_init(void); + +#endif /* __BOARD_H__ */ diff --git a/bsp/artinchip/d213ecv-demo-v4-0/link.lds b/bsp/artinchip/d213ecv-demo-v4-0/link.lds new file mode 100644 index 00000000000..3a8a6958892 --- /dev/null +++ b/bsp/artinchip/d213ecv-demo-v4-0/link.lds @@ -0,0 +1,129 @@ +/* D213ECV-DEMO-V4-0: 128 MiB DDR, loader-owned upper 8 MiB. + * SPDX-License-Identifier: Apache-2.0 + */ +OUTPUT_ARCH("riscv") +ENTRY(_start) + +MEMORY +{ + DRAM (rwx) : ORIGIN = 0x40000000, LENGTH = 0x07800000 /* top 8 MiB reserved for the loader */ +} + +SECTIONS +{ + . = ORIGIN(DRAM); + + .text : { + PROVIDE(__text_start = .); + KEEP(*(.start)) + KEEP(*(.text.start)) + KEEP(*(.text.entry)) + *(.text) + *(.text.*) + *(.gnu.linkonce.t*) + . = ALIGN(8); + + . = ALIGN(8); + __fsymtab_start = .; + KEEP(*(FSymTab)) + __fsymtab_end = .; + . = ALIGN(8); + __vsymtab_start = .; + KEEP(*(VSymTab)) + __vsymtab_end = .; + . = ALIGN(8); + __rtmsymtab_start = .; + KEEP(*(RTMSymTab)) + __rtmsymtab_end = .; + . = ALIGN(8); + __rt_init_start = .; + KEEP(*(SORT(.rti_fn*))) + __rt_init_end = .; + . = ALIGN(8); + + . = ALIGN(8); + + . = ALIGN(8); + __etext = .; + } > DRAM + + .rodata : { + . = ALIGN(8); + __srodata = .; + *(.rodata) + *(.rodata.*) + *(.srodata) + *(.srodata.*) + *(.rdata) + *(.rdata.*) + . = ALIGN(8); + __ctors_start__ = .; + KEEP(*(SORT(.init_array.*))) + KEEP(*(.init_array)) + __ctors_end__ = .; + __dtors_start__ = .; + KEEP(*(SORT(.fini_array.*))) + KEEP(*(.fini_array)) + __dtors_end__ = .; + . = ALIGN(8); + __rodata_end__ = .; + } > DRAM + + .data : { + . = ALIGN(8); + __data_start__ = .; + __global_pointer$ = . + 0x800; + *(.data) + *(.data.*) + *(.sdata) + *(.sdata.*) + *(.gnu.linkonce.d*) + . = ALIGN(8); + __data_end__ = .; + } > DRAM + + .bss (NOLOAD) : { + . = ALIGN(8); + __bss_noclean_start = .; + *(.bss.noclean.*) + __bss_noclean_end = .; + . = ALIGN(8); + __bss_start = .; + *(.bss) + *(.bss.*) + *(.sbss) + *(.sbss.*) + *(COMMON) + . = ALIGN(8); + __bss_end = .; + } > DRAM + + .percpu (NOLOAD) : { + . = ALIGN(16); + __percpu_start = .; + *(.percpu) + __percpu_end = .; + __percpu_real_end = .; + } > DRAM + + _end = .; + __bss_start__ = __bss_start; + __bss_end__ = __bss_end; + + . = ALIGN(16); + __heap_start = .; + + __heap_end = ORIGIN(DRAM) + LENGTH(DRAM); + + /DISCARD/ : { + *(.eh_frame) + *(.eh_frame_hdr) + *(.comment) + } +} + +ASSERT(__data_end__ <= __bss_start__, "data overlaps bss") +ASSERT(__bss_end__ <= __heap_start, "bss overlaps heap") +ASSERT(__percpu_start <= __percpu_end, "percpu section is invalid") +ASSERT(__heap_start < __heap_end, "heap is empty") +ASSERT((__percpu_end & 15) == 0, "early stack is not ABI aligned") diff --git a/bsp/artinchip/d213ecv-demo-v4-0/rtconfig.h b/bsp/artinchip/d213ecv-demo-v4-0/rtconfig.h new file mode 100644 index 00000000000..ab49bfcb5f2 --- /dev/null +++ b/bsp/artinchip/d213ecv-demo-v4-0/rtconfig.h @@ -0,0 +1,222 @@ +#ifndef RT_CONFIG_H__ +#define RT_CONFIG_H__ + +/* RT-Thread Kernel */ + +/* klibc options */ + +/* rt_vsnprintf options */ + +#define RT_KLIBC_USING_VSNPRINTF_LONGLONG +#define RT_KLIBC_USING_VSNPRINTF_STANDARD +#define RT_KLIBC_USING_VSNPRINTF_DECIMAL_SPECIFIERS +#define RT_KLIBC_USING_VSNPRINTF_EXPONENTIAL_SPECIFIERS +#define RT_KLIBC_USING_VSNPRINTF_WRITEBACK_SPECIFIER +#define RT_KLIBC_USING_VSNPRINTF_CHECK_NUL_IN_FORMAT_SPECIFIER +#define RT_KLIBC_USING_VSNPRINTF_INTEGER_BUFFER_SIZE 32 +#define RT_KLIBC_USING_VSNPRINTF_DECIMAL_BUFFER_SIZE 32 +#define RT_KLIBC_USING_VSNPRINTF_FLOAT_PRECISION 6 +#define RT_KLIBC_USING_VSNPRINTF_MAX_INTEGRAL_DIGITS_FOR_DECIMAL 9 +#define RT_KLIBC_USING_VSNPRINTF_LOG10_TAYLOR_TERMS 4 +/* end of rt_vsnprintf options */ + +/* rt_vsscanf options */ + +/* end of rt_vsscanf options */ + +/* rt_memset options */ + +/* end of rt_memset options */ + +/* rt_memcpy options */ + +/* end of rt_memcpy options */ + +/* rt_memmove options */ + +/* end of rt_memmove options */ + +/* rt_memcmp options */ + +/* end of rt_memcmp options */ + +/* rt_strstr options */ + +/* end of rt_strstr options */ + +/* rt_strcasecmp options */ + +/* end of rt_strcasecmp options */ + +/* rt_strncpy options */ + +/* end of rt_strncpy options */ + +/* rt_strcpy options */ + +/* end of rt_strcpy options */ + +/* rt_strncmp options */ + +/* end of rt_strncmp options */ + +/* rt_strcmp options */ + +/* end of rt_strcmp options */ + +/* rt_strlen options */ + +/* end of rt_strlen options */ + +/* rt_strnlen options */ + +/* end of rt_strnlen options */ +/* end of klibc options */ +#define RT_NAME_MAX 16 +#define RT_CPUS_NR 1 +#define RT_ALIGN_SIZE 8 +#define RT_THREAD_PRIORITY_32 +#define RT_THREAD_PRIORITY_MAX 32 +#define RT_TICK_PER_SECOND 1000 +#define RT_USING_OVERFLOW_CHECK +#define IDLE_THREAD_STACK_SIZE 2048 + +/* kservice options */ + +/* end of kservice options */ +#define RT_USING_DEBUG +#define RT_DEBUGING_ASSERT + +/* Inter-Thread communication */ + +#define RT_USING_SEMAPHORE +#define RT_USING_MUTEX +#define RT_USING_EVENT +#define RT_USING_MAILBOX +#define RT_USING_MESSAGEQUEUE +/* end of Inter-Thread communication */ + +/* Memory Management */ + +#define RT_USING_SMALL_MEM +#define RT_USING_SMALL_MEM_AS_HEAP +#define RT_USING_HEAP +/* end of Memory Management */ +#define RT_USING_DEVICE +#define RT_USING_DEVICE_OPS +#define RT_USING_CONSOLE +#define RT_CONSOLEBUF_SIZE 128 +#define RT_CONSOLE_DEVICE_NAME "uart0" +#define RT_VER_NUM 0x50400 +#define RT_BACKTRACE_LEVEL_MAX_NR 32 +/* end of RT-Thread Kernel */ +#define ARCH_TEXT_OFFSET 0x200000 +#define ARCH_RAM_OFFSET 0 +#define ARCH_CPU_64BIT +#define RT_USING_CPU_FFS +#define ARCH_RISCV +#define ARCH_RISCV_FPU +#define ARCH_RISCV_FPU_D +#define ARCH_RISCV64 + +/* RISC-V Architecture Configuration */ + +#define RT_USING_RISCV_NEW_COMMON +#define ARCH_RISCV_M_MODE +#define ARCH_RISCV_USING_IRQ_STACK +#define ARCH_RISCV_IRQ_STACK_SIZE 4096 +/* end of RISC-V Architecture Configuration */ +#define ARCH_USING_NEW_CTX_SWITCH + +/* RT-Thread Components */ + +#define RT_USING_COMPONENTS_INIT +#define RT_USING_USER_MAIN +#define RT_MAIN_THREAD_STACK_SIZE 4096 +#define RT_MAIN_THREAD_PRIORITY 10 +#define RT_USING_MSH +#define RT_USING_FINSH +#define FINSH_USING_MSH +#define FINSH_THREAD_NAME "tshell" +#define FINSH_THREAD_PRIORITY 20 +#define FINSH_THREAD_STACK_SIZE 4096 +#define FINSH_USING_HISTORY +#define FINSH_HISTORY_LINES 5 +#define FINSH_USING_SYMTAB +#define FINSH_CMD_SIZE 80 +#define MSH_USING_BUILT_IN_COMMANDS +#define FINSH_USING_DESCRIPTION +#define FINSH_ARG_MAX 10 +#define FINSH_USING_OPTION_COMPLETION + +/* DFS: device virtual file system */ + +/* end of DFS: device virtual file system */ + +/* Device Drivers */ + +#define RT_USING_DEVICE_IPC +#define RT_UNAMED_PIPE_NUMBER 64 +#define RT_USING_SERIAL +#define RT_USING_SERIAL_V1 +#define RT_SERIAL_RB_BUFSZ 512 +/* end of Device Drivers */ + +/* C/C++ and POSIX layer */ + +/* ISO-ANSI C layer */ + +/* Timezone and Daylight Saving Time */ + +#define RT_LIBC_USING_LIGHT_TZ_DST +#define RT_LIBC_TZ_DEFAULT_HOUR 8 +#define RT_LIBC_TZ_DEFAULT_MIN 0 +#define RT_LIBC_TZ_DEFAULT_SEC 0 +/* end of Timezone and Daylight Saving Time */ +/* end of ISO-ANSI C layer */ + +/* POSIX (Portable Operating System Interface) layer */ + + +/* Interprocess Communication (IPC) */ + + +/* Socket is in the 'Network' category */ + +/* end of Interprocess Communication (IPC) */ +/* end of POSIX (Portable Operating System Interface) layer */ +/* end of C/C++ and POSIX layer */ + +/* Network */ + +/* end of Network */ + +/* Memory protection */ + +/* end of Memory protection */ + +/* Utilities */ + +#define RT_USING_ADT +#define RT_USING_ADT_BITMAP +/* end of Utilities */ + +/* Using USB legacy version */ + +/* end of Using USB legacy version */ +/* end of RT-Thread Components */ + +/* RT-Thread Utestcases */ + +/* end of RT-Thread Utestcases */ +#define AIC_SOC_D21X +#define SOC_D213 + +/* Board options */ + +#define BSP_USING_UART0 +#define BSP_UART_CLK_FREQ 48000000 +#define BSP_CLINT_FREQ 4000000 +/* end of Board options */ + +#endif diff --git a/bsp/artinchip/d213ecv-demo-v4-0/rtconfig.py b/bsp/artinchip/d213ecv-demo-v4-0/rtconfig.py new file mode 100644 index 00000000000..7986697b7c0 --- /dev/null +++ b/bsp/artinchip/d213ecv-demo-v4-0/rtconfig.py @@ -0,0 +1,49 @@ +import os +import shutil +import subprocess + +ARCH = 'risc-v' +CPU = 'c906' +CROSS_TOOL = 'gcc' +PLATFORM = 'gcc' +EXEC_PATH = os.getenv('RTT_EXEC_PATH') or '' +PREFIX = os.getenv('RTT_CC_PREFIX') or 'riscv64-unknown-elf-' + +CC = PREFIX + 'gcc' +CXX = PREFIX + 'g++' +AS = CC +AR = PREFIX + 'ar' +LINK = CC +SIZE = PREFIX + 'size' +OBJCPY = PREFIX + 'objcopy' +OBJDUMP = PREFIX + 'objdump' +TARGET_EXT = 'elf' + +# Accept both the CI GCC 10 ISA spelling and newer GNU assemblers. +compiler = os.path.join(EXEC_PATH, CC) if EXEC_PATH else shutil.which(CC) +if compiler and os.name == 'nt' and not compiler.lower().endswith('.exe'): + compiler += '.exe' +isa = 'rv64imafdc' +if compiler and os.path.isfile(compiler): + version = subprocess.check_output([compiler, '-dumpversion']).decode() + if int(version.split('.')[0]) >= 12: + isa += '_zicsr_zifencei' +if not EXEC_PATH and compiler: + EXEC_PATH = os.path.dirname(compiler) + +DEVICE = ' -march=' + isa + ' -mabi=lp64d -mcmodel=medany' +CFLAGS = DEVICE + ' -O2 -g -Wall -ffunction-sections -fdata-sections -fno-common' +CXXFLAGS = CFLAGS +AFLAGS = DEVICE + ' -c -g -x assembler-with-cpp -D__ASSEMBLY__' +LFLAGS = DEVICE + ' -nostartfiles -Wl,--gc-sections -Wl,-Map=rtthread.map -T link.lds' +CPATH = '' +LPATH = '' +POST_ACTION = OBJCPY + ' -O binary $TARGET rtthread.bin\n' + SIZE + ' $TARGET\n' + + +def dist_handle(BSP_ROOT, dist_dir): + from mkdist import do_copy_folder + libraries = os.path.join(BSP_ROOT, 'libraries') + if not os.path.isdir(libraries): + libraries = os.path.join(BSP_ROOT, '..', 'libraries') + do_copy_folder(libraries, os.path.join(dist_dir, 'libraries')) diff --git a/bsp/artinchip/libraries/Kconfig b/bsp/artinchip/libraries/Kconfig new file mode 100644 index 00000000000..1dd9be6fd07 --- /dev/null +++ b/bsp/artinchip/libraries/Kconfig @@ -0,0 +1,6 @@ +config AIC_SOC_D21X + bool + default n + help + Selects the ArtInChip D21x SoC layer. This initial port supports + D213 only; other family members require separate validation. diff --git a/bsp/artinchip/libraries/SConscript b/bsp/artinchip/libraries/SConscript new file mode 100644 index 00000000000..7615e47f633 --- /dev/null +++ b/bsp/artinchip/libraries/SConscript @@ -0,0 +1,4 @@ +from building import * + +group = SConscript('soc/d21x/SConscript') +Return('group') diff --git a/bsp/artinchip/libraries/include/aic_uart_reg.h b/bsp/artinchip/libraries/include/aic_uart_reg.h new file mode 100644 index 00000000000..7d4383ccee0 --- /dev/null +++ b/bsp/artinchip/libraries/include/aic_uart_reg.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2021-2026, ArtInChip Technology Co., Ltd + * Copyright (c) 2026, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * ArtInChip UART register layout. + * + * Register offsets and fields used by the D213 UART controllers. + * + * Derived from the ArtInChip Luban-Lite HAL + * (bsp/artinchip/hal/uart/aic_hal_uart.c, Apache-2.0). + */ + +#ifndef __AIC_UART_REG_H__ +#define __AIC_UART_REG_H__ + +/* Register offsets */ +#define UART_RBR 0x00 +#define UART_THR 0x00 +#define UART_DLL 0x00 +#define UART_DLH 0x04 +#define UART_IER 0x04 +#define UART_FCR 0x08 +#define UART_LCR 0x0C +#define UART_MCR 0x10 +#define UART_LSR 0x14 +#define UART_USR 0x7C + +/* Line status register */ +#define UART_LSR_DR 0x01 +#define UART_LSR_THRE 0x20 + +/* UART status register */ +#define UART_USR_BUSY 0x01 +#define UART_USR_TFNF 0x02 + +/* Line control register */ +#define UART_LCR_DLAB 0x80 +#define UART_LCR_8N1 0x03 +#define UART_LCR_STOP 0x04 +#define UART_LCR_PARITY 0x08 +#define UART_LCR_EVEN 0x10 + +/* FIFO control register */ +#define UART_FCR_ENABLE 0x01 +#define UART_FCR_RX_RESET 0x02 +#define UART_FCR_TX_RESET 0x04 + +/* Vendor non-DMA FIFO thresholds: TX level 3, RX level 2. */ +#define UART_FCR_TX_TRIGGER (3U << 4) +#define UART_FCR_RX_TRIGGER (2U << 6) + +#endif /* __AIC_UART_REG_H__ */ diff --git a/bsp/artinchip/libraries/soc/d21x/SConscript b/bsp/artinchip/libraries/soc/d21x/SConscript new file mode 100644 index 00000000000..70124d6aeb1 --- /dev/null +++ b/bsp/artinchip/libraries/soc/d21x/SConscript @@ -0,0 +1,10 @@ +from building import * + +cwd = GetCurrentDir() +src = ['port/system.c', 'port/clock.c', 'port/interrupt.c', 'port/timer.c'] +if GetDepend('BSP_USING_UART0'): + src += ['drivers/drv_uart.c'] + +group = DefineGroup('D21x', src, depend=['AIC_SOC_D21X'], + CPPPATH=[cwd + '/include', cwd + '/../../include']) +Return('group') diff --git a/bsp/artinchip/libraries/soc/d21x/drivers/drv_uart.c b/bsp/artinchip/libraries/soc/d21x/drivers/drv_uart.c new file mode 100644 index 00000000000..9dba218f25f --- /dev/null +++ b/bsp/artinchip/libraries/soc/d21x/drivers/drv_uart.c @@ -0,0 +1,242 @@ +/* + * Copyright (c) 2021-2026, ArtInChip Technology Co., Ltd + * Copyright (c) 2026, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * D213 serial-v1 adapter. Register sequence adapted from Luban-Lite + * bsp/artinchip/hal/uart/aic_hal_uart.c. No vendor OSAL or DMA dependency. + */ + +#include +#include +#include +#include +#include "aic_soc.h" +#include "aic_uart_reg.h" + +struct aic_uart +{ + struct rt_serial_device serial; + rt_ubase_t base; + rt_ubase_t clock_reg; + int irq; + const char *name; + rt_bool_t rx_enabled; +}; + +static struct aic_uart uart0 = { + .base = AIC_UART0_BASE, + .clock_reg = AIC_CMU_UART0, + .irq = AIC_UART0_IRQ, + .name = "uart0", +}; +static struct rt_thread rx_thread; +rt_align(RT_ALIGN_SIZE) static rt_uint8_t rx_stack[1024]; + +static rt_err_t aic_uart_configure(struct rt_serial_device *serial, + struct serial_configure *cfg) +{ + struct aic_uart *uart = serial->parent.user_data; + rt_uint32_t divisor, lcr, old_lcr; + rt_uint64_t deadline; + rt_err_t ret = -RT_EIO; + rt_base_t level; + + if (!cfg->baud_rate || cfg->data_bits < DATA_BITS_5 || + cfg->data_bits > DATA_BITS_8 || + (cfg->stop_bits != STOP_BITS_1 && cfg->stop_bits != STOP_BITS_2) || + (cfg->data_bits == DATA_BITS_5 && cfg->stop_bits == STOP_BITS_2) || + (cfg->parity != PARITY_NONE && cfg->parity != PARITY_ODD && + cfg->parity != PARITY_EVEN) || + cfg->bit_order != BIT_ORDER_LSB || cfg->invert != NRZ_NORMAL || + cfg->flowcontrol != RT_SERIAL_FLOWCONTROL_NONE) + { + /* With five data bits the hardware STOP bit means 1.5 stop bits, + * which serial-v1 cannot represent. Reject a request for two. */ + return -RT_EINVAL; + } + lcr = cfg->data_bits - DATA_BITS_5; + if (cfg->stop_bits == STOP_BITS_2) + { + lcr |= UART_LCR_STOP; + } + if (cfg->parity != PARITY_NONE) + { + lcr |= UART_LCR_PARITY; + if (cfg->parity == PARITY_EVEN) + { + lcr |= UART_LCR_EVEN; + } + } + divisor = ((rt_uint64_t)BSP_UART_CLK_FREQ + cfg->baud_rate * 8ULL) / + (cfg->baud_rate * 16ULL); + if (!divisor || divisor > 0xffff) + { + return -RT_EINVAL; + } + + deadline = riscv_timer_rdtime() + BSP_CLINT_FREQ / 10; + for (;;) + { + level = rt_hw_interrupt_disable(); + /* Like the vendor HAL, reset FIFOs before waiting for idle. + * A busy controller can ignore LCR writes; DLL then aliases THR. */ + aic_write32(uart->base + UART_FCR, + UART_FCR_ENABLE | UART_FCR_RX_RESET | UART_FCR_TX_RESET | + UART_FCR_TX_TRIGGER | UART_FCR_RX_TRIGGER); + if (!(aic_read32(uart->base + UART_USR) & UART_USR_BUSY)) + { + break; + } + rt_hw_interrupt_enable(level); + if (riscv_timer_rdtime() >= deadline) + { + return -RT_ETIMEOUT; + } + } + /* IER aliases DLH while DLAB is set, including on loader handoff. */ + old_lcr = aic_read32(uart->base + UART_LCR) & ~UART_LCR_DLAB; + aic_write32(uart->base + UART_LCR, old_lcr); + if (aic_read32(uart->base + UART_LCR) & UART_LCR_DLAB) + { + goto restore_lcr; + } + aic_write32(uart->base + UART_IER, 0); + aic_write32(uart->base + UART_LCR, lcr | UART_LCR_DLAB); + if ((aic_read32(uart->base + UART_LCR) & 0xff) != (lcr | UART_LCR_DLAB)) + { + goto restore_lcr; + } + aic_write32(uart->base + UART_DLL, divisor & 0xff); + aic_write32(uart->base + UART_DLH, divisor >> 8); + aic_write32(uart->base + UART_LCR, lcr); + if ((aic_read32(uart->base + UART_LCR) & 0xff) != lcr) + { + goto restore_lcr; + } + aic_write32(uart->base + UART_MCR, 0); + ret = RT_EOK; + goto out; + +restore_lcr: + aic_write32(uart->base + UART_LCR, old_lcr); +out: + rt_hw_interrupt_enable(level); + return ret; +} + +static rt_err_t aic_uart_control(struct rt_serial_device *serial, int cmd, void *arg) +{ + struct aic_uart *uart = serial->parent.user_data; + rt_base_t level; + + switch (cmd) + { + case RT_DEVICE_CTRL_SET_INT: + case RT_DEVICE_CTRL_CLR_INT: + level = rt_hw_interrupt_disable(); + uart->rx_enabled = (cmd == RT_DEVICE_CTRL_SET_INT); + rt_hw_interrupt_enable(level); + return RT_EOK; + default: + return -RT_ENOSYS; + } +} + +static int aic_uart_putc(struct rt_serial_device *serial, char c) +{ + struct aic_uart *uart = serial->parent.user_data; + + /* Bound a stalled transmitter without depending on scheduler state. */ + rt_uint64_t deadline = riscv_timer_rdtime() + BSP_CLINT_FREQ / 10; + + /* Like the vendor hal_usart_putchar, wait for room in the TX FIFO. + * LSR.THRE reports the TX threshold, not whether a byte can be queued. */ + while (!(aic_read32(uart->base + UART_USR) & UART_USR_TFNF)) + { + if (riscv_timer_rdtime() >= deadline) + { + return -1; + } + } + aic_write32(uart->base + UART_THR, (rt_uint8_t)c); + return 1; +} + +static int aic_uart_getc(struct rt_serial_device *serial) +{ + struct aic_uart *uart = serial->parent.user_data; + + if (aic_read32(uart->base + UART_LSR) & UART_LSR_DR) + { + return aic_read32(uart->base + UART_RBR) & 0xff; + } + return -1; +} + +static const struct rt_uart_ops aic_uart_ops = { + .configure = aic_uart_configure, + .control = aic_uart_control, + .putc = aic_uart_putc, + .getc = aic_uart_getc, +}; + +static void aic_uart_rx_poll(void *parameter) +{ + for (;;) + { + rt_base_t level = rt_hw_interrupt_disable(); + + /* Serial-v1 owns the FIFO. Serialize with close/reconfigure + * so the single-core poller cannot access a freed FIFO. */ + if (uart0.rx_enabled && uart0.serial.serial_rx != RT_NULL && + (aic_read32(uart0.base + UART_LSR) & UART_LSR_DR)) + { + rt_hw_serial_isr(&uart0.serial, RT_SERIAL_EVENT_RX_IND); + } + rt_hw_interrupt_enable(level); + rt_thread_mdelay(1); + } +} + +static int aic_uart_poll_init(void) +{ + rt_err_t ret; + int priority = RT_THREAD_PRIORITY_MAX / 2; + +#ifdef RT_USING_FINSH + /* Poll RX even while the shell is busy transmitting console output. */ + priority = FINSH_THREAD_PRIORITY > 0 ? FINSH_THREAD_PRIORITY - 1 : 0; +#endif + + ret = rt_thread_init(&rx_thread, "uart0rx", aic_uart_rx_poll, RT_NULL, + rx_stack, sizeof(rx_stack), priority, 10); + if (ret != RT_EOK) + { + return ret; + } + return rt_thread_startup(&rx_thread); +} +INIT_DEVICE_EXPORT(aic_uart_poll_init); + +void rt_hw_serial_init(void) +{ + struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; + rt_err_t ret; + + rt_uint32_t value = aic_read32(uart0.clock_reg); + + /* Drop loader state while retaining the divider and gate bits. */ + aic_write32(uart0.clock_reg, value & ~(1U << 13)); + rt_hw_us_delay(30); + aic_write32(uart0.clock_reg, value | (1U << 13)); + rt_hw_us_delay(30); + + uart0.serial.ops = &aic_uart_ops; + uart0.serial.config = config; + rt_hw_interrupt_mask(IRQ_PLIC(uart0.irq)); + ret = rt_hw_serial_register(&uart0.serial, uart0.name, + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, &uart0); + RT_ASSERT(ret == RT_EOK); +} diff --git a/bsp/artinchip/libraries/soc/d21x/include/aic_soc.h b/bsp/artinchip/libraries/soc/d21x/include/aic_soc.h new file mode 100644 index 00000000000..0832b6fddd0 --- /dev/null +++ b/bsp/artinchip/libraries/soc/d21x/include/aic_soc.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022, ArtInChip Technology Co., Ltd + * Copyright (c) 2026, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * D213 register addresses from Luban-Lite bsp/artinchip/sys/d21x. + */ + +#ifndef __AIC_SOC_H__ +#define __AIC_SOC_H__ + +#include + +#define AIC_CMU_BASE 0x18020000UL +#define AIC_GPIO_BASE 0x18700000UL +#define AIC_UART0_BASE 0x18710000UL +#define AIC_PLIC_BASE 0x20000000UL +#define AIC_CLINT_BASE 0x24000000UL +#define AIC_UART0_IRQ 76 +#define AIC_CMU_UART0 (AIC_CMU_BASE + 0x840) + +/* Order device accesses. The loader must leave MMIO non-cacheable. */ +rt_inline rt_uint32_t aic_read32(rt_ubase_t addr) +{ + rt_uint32_t value; + + __asm__ volatile("fence iorw, iorw" ::: "memory"); + value = *(volatile rt_uint32_t *)addr; + __asm__ volatile("fence iorw, iorw" ::: "memory"); + return value; +} + +rt_inline void aic_write32(rt_ubase_t addr, rt_uint32_t value) +{ + __asm__ volatile("fence iorw, iorw" ::: "memory"); + *(volatile rt_uint32_t *)addr = value; + __asm__ volatile("fence iorw, iorw" ::: "memory"); +} + +void aic_soc_init(void); +void aic_clock_init(void); +void aic_interrupt_init(void); + +#endif /* __AIC_SOC_H__ */ diff --git a/bsp/artinchip/libraries/soc/d21x/port/clock.c b/bsp/artinchip/libraries/soc/d21x/port/clock.c new file mode 100644 index 00000000000..cf3da03be7b --- /dev/null +++ b/bsp/artinchip/libraries/soc/d21x/port/clock.c @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2022-2026, ArtInChip Technology Co., Ltd + * Copyright (c) 2026, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Gate/reset sequencing adapted from Luban-Lite bsp/artinchip/hal/cmu. + * DDR, PLLs and the bus clock tree belong to the loader. + */ + +#include +#include "aic_soc.h" + +#define CMU_BUS_GATE (1U << 12) +#define CMU_RESET_N (1U << 13) +#define CMU_MODULE_GATE (1U << 8) + +static void aic_clock_enable(rt_ubase_t reg, rt_uint32_t gates) +{ + aic_write32(reg, aic_read32(reg) | gates); + rt_hw_us_delay(30); + aic_write32(reg, aic_read32(reg) | CMU_RESET_N); + rt_hw_us_delay(30); +} + +void aic_clock_init(void) +{ +#ifdef BSP_USING_UART0 + aic_clock_enable(AIC_CMU_UART0, CMU_BUS_GATE | CMU_MODULE_GATE); +#endif + aic_clock_enable(AIC_CMU_BASE + 0x83c, CMU_BUS_GATE); /* GPIO */ +} diff --git a/bsp/artinchip/libraries/soc/d21x/port/interrupt.c b/bsp/artinchip/libraries/soc/d21x/port/interrupt.c new file mode 100644 index 00000000000..f3b9dfaf0c2 --- /dev/null +++ b/bsp/artinchip/libraries/soc/d21x/port/interrupt.c @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022-2026, ArtInChip Technology Co., Ltd + * Copyright (c) 2026, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * PLIC setup adapted from Luban-Lite bsp/artinchip/sys/d21x/system.c. + */ + +#include "aic_soc.h" + +void aic_interrupt_init(void) +{ + /* Mask loader-owned sources before the common PLIC driver starts. + * Pending bits are read-only. Never complete an unclaimed source. */ + for (int i = 0; i < 32; i++) + { + aic_write32(AIC_PLIC_BASE + 0x2000 + i * 4, 0); + aic_write32(AIC_PLIC_BASE + 0x2080 + i * 4, 0); + } + /* C906 PLIC priority enable, not part of the standard PLIC block. */ + aic_write32(AIC_PLIC_BASE + 0x1ffffc, 1); +} + +rt_ubase_t platform_get_plic_base(void) +{ + return AIC_PLIC_BASE; +} + +rt_size_t platform_get_plic_size(void) +{ + return 0x210000; +} diff --git a/bsp/artinchip/libraries/soc/d21x/port/system.c b/bsp/artinchip/libraries/soc/d21x/port/system.c new file mode 100644 index 00000000000..a136a9d43c0 --- /dev/null +++ b/bsp/artinchip/libraries/soc/d21x/port/system.c @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2022-2026, ArtInChip Technology Co., Ltd + * Copyright (c) 2026, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * C906 setup adapted from Luban-Lite bsp/artinchip/sys/d21x/system.c. + */ + +#include +#include "aic_soc.h" + +void aic_soc_init(void) +{ + rt_ubase_t value; + + /* C906 THEADISAEE and misaligned access, as in vendor SystemInit. */ + __asm__ volatile("csrr %0, 0x7c0" : "=r"(value)); + value |= (1UL << 22) | (1UL << 15); + __asm__ volatile("csrw 0x7c0, %0" ::"r"(value) : "memory"); + + __asm__ volatile("csrr %0, mstatus" : "=r"(value)); + value = (value & ~(3UL << 13)) | (1UL << 13); + __asm__ volatile("csrw mstatus, %0" ::"r"(value) : "memory"); +} diff --git a/bsp/artinchip/libraries/soc/d21x/port/timer.c b/bsp/artinchip/libraries/soc/d21x/port/timer.c new file mode 100644 index 00000000000..c25953fe8b3 --- /dev/null +++ b/bsp/artinchip/libraries/soc/d21x/port/timer.c @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022-2026, ArtInChip Technology Co., Ltd + * Copyright (c) 2026, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * D213 timer access adapted from the ArtInChip D21x RT-Thread port. + */ + +#include +#include "aic_soc.h" + +/* D213 keeps mtime in the time CSR; mtimecmp uses two 32-bit stores. */ +rt_uint64_t riscv_timer_rdtime(void) +{ + rt_uint64_t value; + + __asm__ volatile("rdtime %0" : "=r"(value)); + return value; +} + +void riscv_timer_set_compare(rt_uint64_t value) +{ + rt_ubase_t cmp = AIC_CLINT_BASE + 0x4000; + + /* Prevent a transient expired value while updating the low word. */ + aic_write32(cmp + 4, RT_UINT32_MAX); + aic_write32(cmp, (rt_uint32_t)value); + aic_write32(cmp + 4, (rt_uint32_t)(value >> 32)); +} + +rt_uint64_t rt_hw_get_clock_timer_freq(void) +{ + return BSP_CLINT_FREQ; +} From 4b464f10f1c989572240c183c823a66e9d3664ee Mon Sep 17 00:00:00 2001 From: boa-z Date: Sat, 14 Nov 2026 00:22:23 -0800 Subject: [PATCH 2/3] [bsp][artinchip] Register D213 BSP in board index and CI --- .github/ALL_BSP_COMPILE.json | 1 + bsp/README.md | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/.github/ALL_BSP_COMPILE.json b/.github/ALL_BSP_COMPILE.json index c7ab65a00bd..4e2bcaf30b2 100644 --- a/.github/ALL_BSP_COMPILE.json +++ b/.github/ALL_BSP_COMPILE.json @@ -438,6 +438,7 @@ "RTT_BSP": "riscv64-unknown", "RTT_TOOL_CHAIN": "sourcery-riscv64-unknown-elf", "SUB_RTT_BSP": [ + "artinchip/d213ecv-demo-v4-0", "bluetrum/ab32vg1-ab-prougen", "qemu-virt64-riscv", "ultrarisc/ur_dp1000_evb" diff --git a/bsp/README.md b/bsp/README.md index 221ac204685..0d20c2c0ff6 100644 --- a/bsp/README.md +++ b/bsp/README.md @@ -886,6 +886,12 @@ This document is based on the RT-Thread mainline repository and categorizes the | [ri5cy](rv32m1_vega/ri5cy) | ✅ | ✅ | - | - | - | - | | [rv32m1_sdk_riscv](rv32m1_vega/rv32m1_sdk_riscv) | ✅ | ✅ | - | - | - | - | +#### ⚪ ArtInChip + +| BSP Name | GPIO | UART | ADC | I2C | SPI | WDT | +|----------|------|------|-----|-----|-----|-----| +| [d213ecv-demo-v4-0](artinchip/d213ecv-demo-v4-0) | - | ✅ | - | - | - | - | + --- ### AVR Architecture Series From 2ee13d1cc8896ff155d2550c9229169a2532125d Mon Sep 17 00:00:00 2001 From: boa-z Date: Sun, 15 Nov 2026 21:45:27 -0800 Subject: [PATCH 3/3] [bsp][artinchip] Fix UART TX timeout and UTest registration --- bsp/artinchip/README.md | 2 +- bsp/artinchip/README_zh.md | 2 +- bsp/artinchip/d213ecv-demo-v4-0/README.md | 162 +++++++----------- bsp/artinchip/d213ecv-demo-v4-0/README_zh.md | 162 +++++++----------- bsp/artinchip/d213ecv-demo-v4-0/link.lds | 4 +- .../libraries/soc/d21x/drivers/drv_uart.c | 23 ++- 6 files changed, 150 insertions(+), 205 deletions(-) diff --git a/bsp/artinchip/README.md b/bsp/artinchip/README.md index ebfe7fb5135..820b0bf174a 100644 --- a/bsp/artinchip/README.md +++ b/bsp/artinchip/README.md @@ -27,4 +27,4 @@ Board wiring and memory layout are configured in the board directory. Shared D21 The BSP uses the RT-Thread RISC-V common port for startup, context switching, FPU state, interrupts and system ticks. It provides UART0 with the required clock, reset and pin configuration. The bootloader initializes DDR, PLLs and memory/cache attributes before starting RT-Thread. -See the [board README](d213ecv-demo-v4-0/README.md) for independent builds, configuration, loader requirements, validation status and source attribution. +See the [board README](d213ecv-demo-v4-0/README.md) for build instructions, configuration, bootloader requirements and UART usage. diff --git a/bsp/artinchip/README_zh.md b/bsp/artinchip/README_zh.md index e1faf322a6b..fa318610063 100644 --- a/bsp/artinchip/README_zh.md +++ b/bsp/artinchip/README_zh.md @@ -27,4 +27,4 @@ artinchip/ BSP 使用 RT-Thread RISC-V common port 完成启动、上下文切换、FPU 状态保存、中断处理和系统定时,提供 UART0 及所需的时钟、复位和引脚配置。Bootloader 在启动 RT-Thread 前完成 DDR、PLL 和内存/Cache 属性初始化。 -独立构建、配置、loader 要求、验证状态和源码归属见 [开发板说明](d213ecv-demo-v4-0/README_zh.md)。 +构建方法、配置、Bootloader 要求和 UART 使用方法见 [开发板说明](d213ecv-demo-v4-0/README_zh.md)。 diff --git a/bsp/artinchip/d213ecv-demo-v4-0/README.md b/bsp/artinchip/d213ecv-demo-v4-0/README.md index 0ecbd1dc619..c92ea2aca39 100644 --- a/bsp/artinchip/d213ecv-demo-v4-0/README.md +++ b/bsp/artinchip/d213ecv-demo-v4-0/README.md @@ -2,36 +2,37 @@ [English](README.md) | [中文](README_zh.md) -## Hardware +## Overview -- Board: ArtInChip D213ECV-DEMO-V4-0. -- SoC: D213ECV, T-Head C906, RV64GC (IMAFDC), single hart, M-mode. -- Memory: 128 MiB DDR3 at `0x40000000`. -- Console: UART0, PA.0 TX / PA.1 RX, 115200 baud, 8N1, no flow control. -- [Official board information](https://aicdoc.artinchip.com/topics/product/d213ecV4.html). +This BSP supports the ArtInChip D213ECV-DEMO-V4-0 development board with RT-Thread running in M-mode on a single T-Head C906 core. -| Function | Support | -|----------|-------------| -| Startup, scheduler, double-precision FPU context | RT-Thread RISC-V common port | -| System tick | D213 CLINT comparator and time CSR, common timer core | -| Interrupt controller | Common PLIC driver, D213 priority-enable setup | -| Clock/reset | GPIO and UART0 gates only; loader retains PLL/DDR ownership | -| UART0 | Serial-v1, polling TX and a thread feeding the standard RX FIFO | -| FinSH/MSH, heap, device framework | Enabled by default | +| Item | Description | +|------|-------------| +| SoC | ArtInChip D213ECV | +| CPU | T-Head C906, RV64GC | +| Memory | 128 MiB DDR3 at `0x40000000` | +| Console | UART0, PA.0 (TX) / PA.1 (RX), 115200 baud, 8N1, no flow control | -The UART adapter keeps the UART0 PLIC line masked and polls RX at a nominal 1 ms interval. The RX thread runs one priority level above FinSH so console output cannot starve polling; the default serial receive buffer is 512 bytes. It is intended for an interactive console; sustained transfers or long scheduling delays can lose received data. UART hardware interrupts and DMA are not supported. Selecting serial-v2 disables the UART0 option because this adapter uses the serial-v1 API. +See the [official board documentation](https://aicdoc.artinchip.com/topics/product/d213ecV4.html) for hardware details. -## UART configuration +## Supported features -UART0 is enabled by default with `BSP_USING_UART0`. It uses PA.0 TX / PA.1 RX (function 5) and retains the loader-configured input clock, 48 MHz by default. +| Feature | Description | +|---------|-------------| +| Kernel port | RT-Thread RISC-V common port, including double-precision FPU context switching | +| System tick | CLINT timer, 1000 Hz tick by default | +| Interrupt controller | PLIC | +| UART0 | Serial-v1, polling TX and thread-based polling RX | +| FinSH/MSH | Enabled by default | +| Heap and device framework | Enabled by default | -Use `RT_DEVICE_CTRL_CONFIG` with `struct serial_configure` before opening the port. The adapter accepts 5–8 data bits, no/odd/even parity, and one or two stop bits. Five data bits with two stop bits is rejected: the hardware would produce 1.5 stop bits, which serial-v1 cannot express. Unsupported formats, flow control, inverted/MSB-first operation and divisors outside 1–65535 return `-RT_EINVAL` before changing registers. Configuration clears FIFOs, waits up to 100 ms for the controller to become idle and checks LCR readback before accessing the divisor registers. A busy timeout returns `-RT_ETIMEOUT`; rejected LCR writes return `-RT_EIO`. Baud rates use the nearest integer divisor; the resulting baud error depends on the input clock and requested rate. +UART0 is intended for an interactive console. Sustained input or long scheduling delays may cause data loss. UART hardware interrupts, DMA and flow control are not supported. The BSP provides the clock, reset and pin configuration required by UART0; DDR and PLL initialization are handled by the bootloader. -Open with `RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX` to use the framework's receive FIFO and callback, or use polling reads without `INT_RX`. Here `INT_RX` selects the serial-v1 buffered interface; hardware IRQ reception remains disabled. Closing the device disables buffered reception. Reconfiguration resets the hardware FIFOs, so configure only with traffic stopped. DMA and RS-485 direction control are not provided. +## Build and configuration -## Build independently +Install Python 3, SCons, kconfiglib and a GNU bare-metal RISC-V toolchain supporting RV64GC and the `lp64d` ABI. GCC 10.2.0 is recommended. -Install Python 3, SCons, kconfiglib and a GNU bare-metal RISC-V toolchain providing `riscv64-unknown-elf-*`, RV64GC and the `lp64d` ABI: +From the repository root: ```sh python -m pip install scons kconfiglib @@ -41,60 +42,50 @@ scons --pyconfig-silent scons -j8 ``` -On PowerShell, set the toolchain path with: +On PowerShell, use `$env:RTT_EXEC_PATH = 'C:/path/to/riscv-toolchain/bin'` to set the toolchain path. The default compiler prefix is `riscv64-unknown-elf-`; set `RTT_CC_PREFIX` if your toolchain uses a different prefix. The compiler can also be located through PATH. -```powershell -$env:RTT_EXEC_PATH = 'C:/path/to/riscv-toolchain/bin' -scons --pyconfig-silent -scons -j8 -``` - -The compiler can also be added to PATH. Set `RTT_CC_PREFIX` when using a different toolchain prefix. The build produces `rtthread.elf`, `rtthread.bin` and `rtthread.map`. +The build produces `rtthread.elf`, `rtthread.bin` and `rtthread.map`. -Local builds use Xuantie V2.6.1 (GCC 10.2.0); CI uses RT-Thread's public GCC 10.2.0 RISC-V toolchain. For GCC 12 and later, `rtconfig.py` adds explicit `zicsr_zifencei` to the ISA string. Those newer toolchains have not been validated for this BSP. - -## Configuration and distribution - -The default `.config` enables UART0/Serial-v1, MSH, heap and the base kernel, with a 1000 Hz system tick. Configure the project and generate `rtconfig.h` with: +To change the configuration, run: ```sh scons --menuconfig scons --pyconfig-silent scons -j8 -scons --dist ``` -Board options include UART0 selection, the UART input clock (default 48 MHz) and the CLINT timebase (default 4 MHz). Set the frequency values to match the bootloader configuration; these options do not change hardware dividers. Generate `rtconfig.h` with the configuration tools above. - -Extract `dist/project.zip` into a separate directory and unset `RTT_ROOT`. Run the same configuration and build commands in the extracted `project/` directory. The archive contains the BSP, its libraries, RT-Thread sources and build tools. +Generate `rtconfig.h` with the configuration tools rather than editing it directly. -## Memory and startup requirements +| Board option | Default | Description | +|--------------|---------|-------------| +| `BSP_USING_UART0` | Enabled | UART0 console; requires Serial-v1 | +| `BSP_UART_CLK_FREQ` | 48000000 | UART0 input clock in Hz | +| `BSP_CLINT_FREQ` | 4000000 | CLINT timebase in Hz | -| Region | Address / purpose | -|--------|-------------------| -| DDR3 | `0x40000000..0x47ffffff`, 128 MiB | -| RT-Thread | `0x40000000..0x477fffff`, 120 MiB | -| Loader reservation | `0x47800000..0x47ffffff`, upper 8 MiB | -| Binary load address / ELF entry | `0x40000000` | +The clock settings must match the bootloader configuration; they do not change hardware clock dividers. Serial-v2 is not supported by the UART0 driver. -The heap starts after static sections and the early stack, and ends at `0x47800000`. The early common-port stack is 16-byte aligned; the interrupt stack resides in BSS. Linker assertions check section ordering, stack alignment and heap bounds. +UTest is disabled by default. To use it, enable `RT_USING_UTESTCASES` and select the required test cases in menuconfig, then regenerate the configuration and rebuild. -An ArtInChip-compatible external loader must: +To export a standalone project, run `scons --dist`. Extract `dist/project.zip`, unset `RTT_ROOT` if it is set, and run the same configuration and build commands in the extracted `project/` directory. -1. Initialize DDR, PLLs, bus clocks and CPU memory/cache attributes. -2. Configure UART0 for a 48 MHz input clock and provide a 4 MHz timebase, or use matching BSP frequency settings. -3. Load the image at `0x40000000`, clean loaded data and synchronize the instruction cache if caches are enabled. -4. Enter the first instruction on hart 0 in M-mode, with interrupts disabled, MMIO non-cacheable, no active DMA and no outstanding claimed PLIC IRQs. +## Download and run -The BSP retains the bootloader's DDR, PLL, UART0 clock-divider and cache configuration. The RISC-V common port handles startup and PLIC claim/complete; board initialization sets up the C906, peripheral gates and timer access. +This BSP requires an external ArtInChip-compatible bootloader. Package `rtthread.bin` in the FIT or firmware format expected by that bootloader, using a matching partition layout. -## Load and use the console +| Region | Address range | +|--------|---------------| +| RT-Thread, including heap | `0x40000000`–`0x477fffff` (120 MiB) | +| Bootloader reservation | `0x47800000`–`0x47ffffff` (8 MiB) | +| Binary load address and ELF entry | `0x40000000` | -Package `rtthread.bin` in the FIT or firmware format required by your ArtInChip bootloader, then load it at `0x40000000`. Use a bootloader with DDR initialization and a matching partition layout. +Before entering RT-Thread, the bootloader must: -For a `demo128-nand` full image on 2 KiB-page NAND, keep the assembled `bootloader.aic` within 411,648 bytes, including PBP, headers and resources. This is the SPL page-table limit. Use the existing bootloader and partition layout for an OS-only update. +1. Initialize DDR, PLLs, bus clocks and CPU memory/cache attributes, with MMIO mapped as non-cacheable. +2. Provide the UART0 input clock and CLINT timebase specified in the BSP configuration. +3. Load the image at `0x40000000`, clean the loaded data from the data cache and synchronize the instruction cache if caches are enabled. +4. Enter the image on hart 0 in M-mode with interrupts disabled, no active DMA and no outstanding claimed PLIC interrupts. -After loading, connect UART0 at 115200 8N1. Expected output includes: +Connect a serial terminal to UART0 using 115200 baud, 8 data bits, no parity, 1 stop bit and no flow control. After boot, the console displays: ```text RT-Thread Operating System @@ -102,46 +93,19 @@ D213ECV-DEMO-V4-0 msh > ``` -| Command | Purpose | -|---------|---------| -| `help` | List shell commands | -| `version` | Show RT-Thread version | -| `ps` or `list thread` | List threads | -| `list device` | List registered devices; the default exposes `uart0` | -| `list timer` | List timers and the current tick | -| `free` | Show heap usage | - -The default thread list includes `tshell`, `uart0rx` and `tidle0` after the startup application exits. - -## Validation status - -The UART0 image built on November 14, 2026 at 02:39:03 reached MSH on the board. Serial logs confirm repeated `help`, `list device`, `list timer` and `ps` commands, with only `uart0` registered. The receive thread runs at priority 19, above the shell at priority 20. Board testing also confirmed stable MSH startup after a full power cycle and repeated command entry while `help` and `ps` output was in progress. - -| Check | Result | -|-------|--------| -| Default UART0 configuration and independent SCons build | Passed with GCC 10.2.0 | -| No-UART and serial-v2 exclusion configurations | Passed with GCC 10.2.0 | -| Dist export and independent rebuild outside the repository | Passed with GCC 10.2.0 | -| UART0 formats, parameter validation, TX timeout, RX close/reopen and DLAB protection | Host MMIO simulation passed; 21 valid formats | -| SPI-NAND loader handoff, FIT CRC32 and RT-Thread startup | Passed on the previous UART0 revision | -| UART0 TX/RX, repeated MSH `help` and `ps` | Confirmed by current image board logs | -| MSH `version` and `free` | Passed on the previous UART0 revision | -| Device/timer enumeration | Current image reports only `uart0`, receive-thread timer and current tick | -| Full power-cycle startup and command input during `help` / `ps` output | Passed on the current UART0 image | -| Tick accuracy, preemption and repeated wakeup | Pending | -| Heap stress/alignment and FPU/FCSR preservation under preemption/IRQs | Pending | -| Controlled external PLIC delivery/complete, reset cycles and soak | Pending | - -## Source and license - -The BSP is licensed under Apache-2.0. - -SoC/register definitions, hardware sequences and board pinmux derive from the public [ArtInChip Luban-Lite SDK](https://gitee.com/artinchip/luban-lite). The following table lists the vendor source paths: - -| BSP content | SDK source | License | -|-------------|------------|---------| -| D213 addresses and CMU gate/reset bits | `bsp/artinchip/sys/d21x/include/aic_soc.h`, `aic_clk_id.h` | Apache-2.0 | -| C906 and PLIC setup | `bsp/artinchip/sys/d21x/system.c` | Apache-2.0 | -| UART registers/programming | `bsp/artinchip/hal/uart/aic_hal_uart.c` | Apache-2.0 | -| GPIO pin configuration | `bsp/artinchip/hal/gpio/aic_hal_gpio.c`, `target/d21x/demo128-nand/pinmux.c` | Apache-2.0 | -| Gate/reset sequencing | `bsp/artinchip/hal/cmu/aic_hal_clk.c` | Apache-2.0 | +Use `help` to list shell commands, `ps` to view threads, `free` to view heap usage, and `list device` to view registered devices. The default configuration registers `uart0`. + +## UART usage + +UART0 is enabled by default. To change its format, use `RT_DEVICE_CTRL_CONFIG` with `struct serial_configure` before opening the device. Supported formats are 5–8 data bits, no/odd/even parity, and one or two stop bits, except for 5 data bits with two stop bits. The available baud rates depend on the configured input clock and the hardware divisor range of 1–65535. + +Open with `RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX` for buffered reception and receive callbacks. In this driver, `INT_RX` uses a polling thread rather than hardware interrupts. Polling reads are available when opened without `INT_RX`. + +Stop traffic before reconfiguring the device because configuration clears the hardware FIFOs. Transmit waits have a timeout; check the returned byte count for short writes. RS-485 direction control is not supported. + +## References and license + +- [ArtInChip board documentation](https://aicdoc.artinchip.com/topics/product/d213ecV4.html) +- [ArtInChip Luban-Lite SDK](https://gitee.com/artinchip/luban-lite), the source of the SoC definitions and hardware initialization sequences used by this BSP. + +The BSP is licensed under Apache-2.0. Copyright and license notices are retained in the source files. diff --git a/bsp/artinchip/d213ecv-demo-v4-0/README_zh.md b/bsp/artinchip/d213ecv-demo-v4-0/README_zh.md index b41fce922c6..b71ef5491ae 100644 --- a/bsp/artinchip/d213ecv-demo-v4-0/README_zh.md +++ b/bsp/artinchip/d213ecv-demo-v4-0/README_zh.md @@ -2,36 +2,37 @@ [English](README.md) | [中文](README_zh.md) -## 硬件信息 +## 简介 -- 开发板:ArtInChip D213ECV-DEMO-V4-0。 -- SoC:D213ECV,T-Head C906,RV64GC(IMAFDC),单 hart,M-mode。 -- 内存:128 MiB DDR3,起始地址 `0x40000000`。 -- 控制台:UART0,PA.0 TX / PA.1 RX,115200 波特率、8N1、无流控。 -- [官方开发板资料](https://aicdoc.artinchip.com/topics/product/d213ecV4.html)。 +本 BSP 支持 ArtInChip D213ECV-DEMO-V4-0 开发板,RT-Thread 在单核 T-Head C906 上以 M-mode 运行。 -| 功能 | 支持情况 | -|------|----------| -| 启动、调度器、双精度 FPU 上下文 | RT-Thread RISC-V common port | -| 系统 Tick | D213 CLINT 比较器与 time CSR,复用 common 定时器核心 | -| 中断控制器 | common PLIC 驱动,补充 D213 priority-enable 初始化 | -| 时钟/复位 | GPIO 与 UART0 的门控;PLL/DDR 由 loader 管理 | -| UART0 | Serial-v1,轮询发送,接收线程填充标准接收 FIFO | -| FinSH/MSH、堆、设备框架 | 默认开启 | +| 项目 | 说明 | +|------|------| +| SoC | ArtInChip D213ECV | +| CPU | T-Head C906,RV64GC | +| 内存 | 128 MiB DDR3,起始地址 `0x40000000` | +| 控制台 | UART0,PA.0(TX)/ PA.1(RX),115200 波特率,8N1,无流控 | -UART 适配器保持UART0 的 PLIC 中断屏蔽,以标称 1 ms 间隔轮询接收。接收线程比 FinSH 高一个优先级,避免控制台持续输出时接收轮询无法运行;默认串口接收缓冲区为 512 字节。该方案面向交互式控制台,持续大流量传输或调度长时间延迟可能丢字节。不支持 UART 硬件中断和 DMA;由于使用 Serial-v1 API,选择 Serial-v2 时会关闭 UART0 选项。 +硬件信息请参阅[官方开发板文档](https://aicdoc.artinchip.com/topics/product/d213ecV4.html)。 -## UART 配置 +## 支持功能 -通过 `BSP_USING_UART0` 默认启用 UART0,使用 PA.0 TX / PA.1 RX(功能 5),沿用 Bootloader 配置的输入时钟,默认为 48 MHz。 +| 功能 | 说明 | +|------|------| +| 内核移植 | RT-Thread RISC-V common port,包含双精度 FPU 上下文切换 | +| 系统时钟 | CLINT 定时器,默认系统 Tick 为 1000 Hz | +| 中断控制器 | PLIC | +| UART0 | Serial-v1,轮询发送,线程轮询接收 | +| FinSH/MSH | 默认启用 | +| 堆与设备框架 | 默认启用 | -打开设备前,通过 `RT_DEVICE_CTRL_CONFIG` 和 `struct serial_configure` 配置。适配器接受 5–8 位数据、无/奇/偶校验,以及 1 或 2 位停止位。5 位数据加 2 位停止位会被拒绝:硬件实际产生 1.5 位停止位,Serial-v1 无法表达。不支持的格式、流控、反相/MSB 优先,以及超出 1–65535 的分频值均返回 `-RT_EINVAL`,且不修改寄存器。配置时先清空 FIFO,最多等待 100 ms 让控制器进入空闲状态,并回读 LCR 确认切换成功后才访问分频寄存器。忙等待超时返回 `-RT_ETIMEOUT`,LCR 写入未生效返回 `-RT_EIO`。波特率采用最接近的整数分频,实际误差取决于输入时钟和请求值。 +UART0 面向交互式控制台,持续输入或调度长时间延迟可能导致数据丢失。不支持 UART 硬件中断、DMA 和流控。BSP 提供 UART0 所需的时钟、复位和引脚配置,DDR 与 PLL 由 Bootloader 初始化。 -使用 `RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX` 打开可使用框架接收 FIFO 和回调;不带 `INT_RX` 时可轮询读取。这里的 `INT_RX` 选择 Serial-v1 缓冲接口,硬件中断接收仍关闭。关闭设备后停止缓冲接收。重新配置会清空硬件 FIFO,应停止收发后再配置。不提供 DMA 和 RS-485 方向控制。 +## 构建与配置 -## 独立构建 +安装 Python 3、SCons、kconfiglib,以及支持 RV64GC 和 `lp64d` ABI 的 GNU 裸机 RISC-V 工具链。推荐使用 GCC 10.2.0。 -安装 Python 3、SCons、kconfiglib,以及支持 RV64GC、`lp64d` ABI 并提供 `riscv64-unknown-elf-*` 的 GNU 裸机 RISC-V 工具链: +在仓库根目录执行: ```sh python -m pip install scons kconfiglib @@ -41,60 +42,50 @@ scons --pyconfig-silent scons -j8 ``` -PowerShell 使用以下方式设置工具链路径: - -```powershell -$env:RTT_EXEC_PATH = 'C:/path/to/riscv-toolchain/bin' -scons --pyconfig-silent -scons -j8 -``` - -也可将编译器加入 PATH,通过 `RTT_CC_PREFIX` 指定其他工具链前缀。构建产物为 `rtthread.elf`、`rtthread.bin` 和 `rtthread.map`。 +PowerShell 中使用 `$env:RTT_EXEC_PATH = 'C:/path/to/riscv-toolchain/bin'` 设置工具链路径。默认编译器前缀为 `riscv64-unknown-elf-`,使用其他前缀时设置 `RTT_CC_PREFIX`。也可通过 PATH 查找编译器。 -本地构建使用 Xuantie V2.6.1(GCC 10.2.0),CI 使用 RT-Thread 公开的 GCC 10.2.0 RISC-V 工具链。针对 GCC 12 及以上版本,`rtconfig.py` 会在 ISA 字符串中显式加入 `zicsr_zifencei`;本 BSP 尚未实际验证这些新版工具链。 +构建产物为 `rtthread.elf`、`rtthread.bin` 和 `rtthread.map`。 -## 配置与独立工程导出 - -默认 `.config` 启用 UART0/Serial-v1、MSH、堆和基础内核,系统 Tick 为 1000 Hz。使用以下命令配置工程并生成 `rtconfig.h`: +修改配置时执行: ```sh scons --menuconfig scons --pyconfig-silent scons -j8 -scons --dist ``` -板级选项包括 UART0 开关、UART 输入时钟(默认 48 MHz)及 CLINT 时基(默认 4 MHz)。频率设置应与 Bootloader 配置一致,这些选项不会改变硬件分频器。`rtconfig.h` 由上述配置工具生成。 - -将 `dist/project.zip` 解压到独立目录,清除 `RTT_ROOT`,在解压后的 `project/` 目录中运行相同的配置及构建命令。导出包包含 BSP、所需库文件、RT-Thread 源码及构建工具。 +请通过配置工具生成 `rtconfig.h`,不要直接编辑该文件。 -## 内存与启动要求 +| 板级选项 | 默认值 | 说明 | +|----------|--------|------| +| `BSP_USING_UART0` | 启用 | UART0 控制台,依赖 Serial-v1 | +| `BSP_UART_CLK_FREQ` | 48000000 | UART0 输入时钟,单位 Hz | +| `BSP_CLINT_FREQ` | 4000000 | CLINT 时基,单位 Hz | -| 区域 | 地址 / 用途 | -|------|-------------| -| DDR3 | `0x40000000..0x47ffffff`,128 MiB | -| RT-Thread | `0x40000000..0x477fffff`,120 MiB | -| loader 保留区 | `0x47800000..0x47ffffff`,顶部 8 MiB | -| BIN 加载地址 / ELF 入口 | `0x40000000` | +时钟设置必须与 Bootloader 配置一致,这些选项不会改变硬件时钟分频器。UART0 驱动不支持 Serial-v2。 -堆起点位于静态段和早期栈之后,终点为 `0x47800000`。common port 早期栈按 16 字节对齐,中断栈位于 BSS。链接脚本检查段顺序、栈对齐和堆边界。 +UTest 默认关闭。如需使用,在 menuconfig 中启用 `RT_USING_UTESTCASES` 并选择所需测试用例,然后重新生成配置并构建。 -外部 ArtInChip 兼容 loader 必须: +使用 `scons --dist` 导出独立工程。解压 `dist/project.zip`,清除已设置的 `RTT_ROOT` 环境变量,在解压后的 `project/` 目录运行相同的配置与构建命令。 -1. 初始化 DDR、PLL、总线时钟及 CPU 内存/Cache 属性。 -2. 将 UART0 输入时钟配置为 48 MHz,并提供 4 MHz 时基,或使 BSP 频率设置与实际值一致。 -3. 将镜像加载到 `0x40000000`;Cache 开启时清理已加载数据并同步指令 Cache。 -4. 在 hart 0、M-mode 下进入第一条指令,进入时中断关闭、MMIO 不可缓存,无活动 DMA,也无尚未完成的已 claim PLIC 中断。 +## 下载与运行 -BSP 沿用 Bootloader 的 DDR、PLL、UART0 时钟分频和 Cache 配置。RISC-V common port 负责启动及 PLIC claim/complete,板级代码完成 C906、外设门控和定时器访问初始化。 +本 BSP 需要外部 ArtInChip 兼容 Bootloader。请按 Bootloader 要求将 `rtthread.bin` 打包为 FIT 或烧录镜像,并使用匹配的分区布局。 -## 加载与控制台使用 +| 区域 | 地址范围 | +|------|----------| +| RT-Thread,包含堆 | `0x40000000`–`0x477fffff`(120 MiB)| +| Bootloader 保留区 | `0x47800000`–`0x47ffffff`(8 MiB)| +| BIN 加载地址与 ELF 入口 | `0x40000000` | -根据所用 ArtInChip Bootloader 的要求,将 `rtthread.bin` 打包为 FIT 或烧录镜像,并加载到 `0x40000000`。Bootloader 需包含 DDR 初始化程序,并使用匹配的分区布局。 +进入 RT-Thread 前,Bootloader 必须: -使用 2 KiB 页 NAND 的 `demo128-nand` 整包时,拼接后的 `bootloader.aic` 应控制在 411,648 字节以内,包含 PBP、头部和资源。这是 SPL 页表的容量限制。仅更新 OS 时,沿用现有 Bootloader 和分区布局。 +1. 初始化 DDR、PLL、总线时钟及 CPU 内存/Cache 属性,将 MMIO 区域设为不可缓存。 +2. 提供与 BSP 配置一致的 UART0 输入时钟和 CLINT 时基。 +3. 将镜像加载到 `0x40000000`;Cache 开启时,将已加载数据从数据 Cache 写回,并同步指令 Cache。 +4. 在 hart 0、M-mode 下进入镜像,进入时关闭中断,无活动 DMA,也无尚未完成的已 claim PLIC 中断。 -加载后,以 115200 8N1 连接 UART0,应出现以下输出: +将串口终端连接至 UART0,设置为 115200 波特率、8 位数据、无校验、1 位停止位、无流控。启动后控制台显示: ```text RT-Thread Operating System @@ -102,46 +93,19 @@ D213ECV-DEMO-V4-0 msh > ``` -| 命令 | 用途 | -|------|------| -| `help` | 列出 shell 命令 | -| `version` | 查看 RT-Thread 版本 | -| `ps` 或 `list thread` | 查看线程 | -| `list device` | 查看注册设备;默认包含 `uart0` | -| `list timer` | 查看定时器及当前 Tick | -| `free` | 查看堆使用情况 | - -启动应用退出后,默认线程列表包含 `tshell`、`uart0rx` 和 `tidle0`。 - -## 验证状态 - -2026 年 11 月 14 日 02:39:03 构建的 UART0 镜像已在实机进入 MSH。串口日志确认重复执行 `help`,以及 `list device`、`list timer` 和 `ps` 正常,仅注册 `uart0`。接收线程优先级为 19,高于 shell 的 20。实机验证还确认,完全断电后重新上电可稳定进入 MSH,在 `help`、`ps` 输出过程中重复输入下一条命令均正常。 - -| 检查项 | 结果 | -|--------|------| -| 默认 UART0 配置及独立 SCons 构建 | GCC 10.2.0 下通过 | -| 无 UART 和 Serial-v2 排除配置 | GCC 10.2.0 下通过 | -| dist 导出及仓库外独立重建 | GCC 10.2.0 下通过 | -| UART0 格式、参数校验、发送超时、接收关闭/重开及 DLAB 保护 | 主机 MMIO 模拟通过,覆盖 21 种有效格式 | -| SPI NAND loader 交接、FIT CRC32、RT-Thread 启动 | 上一 UART0 版本实机通过 | -| UART0 收发、重复执行 MSH `help` 及 `ps` | 本次镜像实机日志确认 | -| MSH `version` 和 `free` | 上一 UART0 版本实机通过 | -| 设备/定时器枚举 | 本次镜像仅注册 `uart0`,已输出接收线程定时器及当前 Tick | -| 完全断电后启动及 `help` / `ps` 输出期间输入命令 | 本次 UART0 镜像实机通过 | -| Tick 精度、抢占及反复唤醒 | 待验证 | -| 堆压力/对齐及抢占、中断下的 FPU/FCSR 保存 | 待验证 | -| 受控外部 PLIC 投递/完成、反复复位和长时间运行 | 待验证 | - -## 源码与许可证 - -BSP 使用 Apache-2.0 许可证。 - -SoC/寄存器定义、硬件时序及板级 pinmux 来自公开的 [ArtInChip Luban-Lite SDK](https://gitee.com/artinchip/luban-lite)。原厂源码路径如下: - -| BSP 内容 | SDK 来源 | 许可证 | -|----------|----------|--------| -| D213 地址、CMU 门控/复位位 | `bsp/artinchip/sys/d21x/include/aic_soc.h`、`aic_clk_id.h` | Apache-2.0 | -| C906 和 PLIC 初始化 | `bsp/artinchip/sys/d21x/system.c` | Apache-2.0 | -| UART 寄存器/编程 | `bsp/artinchip/hal/uart/aic_hal_uart.c` | Apache-2.0 | -| GPIO 引脚配置 | `bsp/artinchip/hal/gpio/aic_hal_gpio.c`、`target/d21x/demo128-nand/pinmux.c` | Apache-2.0 | -| 门控/复位时序 | `bsp/artinchip/hal/cmu/aic_hal_clk.c` | Apache-2.0 | +使用 `help` 查看 shell 命令,`ps` 查看线程,`free` 查看堆使用情况,`list device` 查看注册设备。默认配置注册 `uart0`。 + +## UART 使用 + +UART0 默认启用。修改串口格式时,在打开设备前通过 `RT_DEVICE_CTRL_CONFIG` 和 `struct serial_configure` 配置。支持 5–8 位数据、无/奇/偶校验、1 或 2 位停止位,但不支持 5 位数据加 2 位停止位。可用波特率取决于配置的输入时钟和 1–65535 的硬件分频范围。 + +使用 `RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX` 打开设备可启用缓冲接收与接收回调。本驱动的 `INT_RX` 通过轮询线程实现,不使用硬件中断。不带 `INT_RX` 打开时可轮询读取。 + +重新配置会清空硬件 FIFO,请先停止收发。发送等待有超时限制,应检查返回的实际写入字节数。不支持 RS-485 方向控制。 + +## 参考资料与许可证 + +- [ArtInChip 开发板文档](https://aicdoc.artinchip.com/topics/product/d213ecV4.html) +- [ArtInChip Luban-Lite SDK](https://gitee.com/artinchip/luban-lite),本 BSP 使用的 SoC 定义与硬件初始化时序来源。 + +BSP 使用 Apache-2.0 许可证,源码文件保留相应版权与许可证声明。 diff --git a/bsp/artinchip/d213ecv-demo-v4-0/link.lds b/bsp/artinchip/d213ecv-demo-v4-0/link.lds index 3a8a6958892..40cdfda2f96 100644 --- a/bsp/artinchip/d213ecv-demo-v4-0/link.lds +++ b/bsp/artinchip/d213ecv-demo-v4-0/link.lds @@ -39,9 +39,11 @@ SECTIONS __rt_init_start = .; KEEP(*(SORT(.rti_fn*))) __rt_init_end = .; - . = ALIGN(8); . = ALIGN(8); + __rt_utest_tc_tab_start = .; + KEEP(*(UtestTcTab)) + __rt_utest_tc_tab_end = .; . = ALIGN(8); __etext = .; diff --git a/bsp/artinchip/libraries/soc/d21x/drivers/drv_uart.c b/bsp/artinchip/libraries/soc/d21x/drivers/drv_uart.c index 9dba218f25f..c04a91bac41 100644 --- a/bsp/artinchip/libraries/soc/d21x/drivers/drv_uart.c +++ b/bsp/artinchip/libraries/soc/d21x/drivers/drv_uart.c @@ -23,6 +23,7 @@ struct aic_uart int irq; const char *name; rt_bool_t rx_enabled; + rt_uint64_t tx_timeout_ticks; }; static struct aic_uart uart0 = { @@ -30,6 +31,7 @@ static struct aic_uart uart0 = { .clock_reg = AIC_CMU_UART0, .irq = AIC_UART0_IRQ, .name = "uart0", + .tx_timeout_ticks = BSP_CLINT_FREQ / 10, }; static struct rt_thread rx_thread; rt_align(RT_ALIGN_SIZE) static rt_uint8_t rx_stack[1024]; @@ -38,8 +40,8 @@ static rt_err_t aic_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg) { struct aic_uart *uart = serial->parent.user_data; - rt_uint32_t divisor, lcr, old_lcr; - rt_uint64_t deadline; + rt_uint32_t divisor, lcr, old_lcr, frame_bits; + rt_uint64_t deadline, tx_timeout; rt_err_t ret = -RT_EIO; rt_base_t level; @@ -76,6 +78,18 @@ static rt_err_t aic_uart_configure(struct rt_serial_device *serial, return -RT_EINVAL; } + /* TFNF needs one FIFO slot, not an empty FIFO. Allow two actual frame + * times, rounded up, with the existing 100 ms minimum for fast rates. */ + frame_bits = 1 + cfg->data_bits + (cfg->parity != PARITY_NONE) + + (cfg->stop_bits == STOP_BITS_2 ? 2 : 1); + tx_timeout = (16ULL * divisor * frame_bits * BSP_CLINT_FREQ + BSP_UART_CLK_FREQ - 1) / + BSP_UART_CLK_FREQ; + tx_timeout *= 2; + if (tx_timeout < BSP_CLINT_FREQ / 10) + { + tx_timeout = BSP_CLINT_FREQ / 10; + } + deadline = riscv_timer_rdtime() + BSP_CLINT_FREQ / 10; for (;;) { @@ -116,6 +130,7 @@ static rt_err_t aic_uart_configure(struct rt_serial_device *serial, goto restore_lcr; } aic_write32(uart->base + UART_MCR, 0); + uart->tx_timeout_ticks = tx_timeout; ret = RT_EOK; goto out; @@ -149,13 +164,13 @@ static int aic_uart_putc(struct rt_serial_device *serial, char c) struct aic_uart *uart = serial->parent.user_data; /* Bound a stalled transmitter without depending on scheduler state. */ - rt_uint64_t deadline = riscv_timer_rdtime() + BSP_CLINT_FREQ / 10; + rt_uint64_t start = riscv_timer_rdtime(); /* Like the vendor hal_usart_putchar, wait for room in the TX FIFO. * LSR.THRE reports the TX threshold, not whether a byte can be queued. */ while (!(aic_read32(uart->base + UART_USR) & UART_USR_TFNF)) { - if (riscv_timer_rdtime() >= deadline) + if (riscv_timer_rdtime() - start >= uart->tx_timeout_ticks) { return -1; }