From b1ec5bca905c1b7b4fee77fe591c7a3c96b5a1f2 Mon Sep 17 00:00:00 2001 From: Parthiban Nallathambi Date: Thu, 5 Nov 2026 14:35:08 +0530 Subject: [PATCH 1/5] Added RH850 G4KH GNU port For the Renesas RH850 G4KH core with v850-elf-gcc, hard and soft float. Issue #515. TX_DISABLE keeps a stricter PLMR of the caller when TX_PORT_MAX_IPL is above 0. TX_RH850_MAX_IPL sets TX_PORT_MAX_IPL and RH850_EXTRA_FLAGS adds compiler flags. The library builds hard and soft with RH850_EXTRA_FLAGS=-Werror and no warnings. Signed-off-by: Parthiban Nallathambi --- cmake/rh850_gnu.cmake | 24 ++ cmake/v850-elf.cmake | 28 ++ ports/rh850/gnu/CMakeLists.txt | 48 +++ ports/rh850/gnu/inc/rh850_asm.h | 25 ++ ports/rh850/gnu/inc/rh850_core.h | 79 +++++ ports/rh850/gnu/inc/rh850_sysreg.h | 49 +++ ports/rh850/gnu/inc/tx_port.h | 292 ++++++++++++++++++ ports/rh850/gnu/src/tx_initialize_low_level.S | 127 ++++++++ ports/rh850/gnu/src/tx_port_asm.h | 182 +++++++++++ ports/rh850/gnu/src/tx_port_offset_check.c | 33 ++ .../rh850/gnu/src/tx_thread_context_restore.S | 117 +++++++ ports/rh850/gnu/src/tx_thread_context_save.S | 74 +++++ .../gnu/src/tx_thread_interrupt_control.S | 61 ++++ ports/rh850/gnu/src/tx_thread_schedule.S | 100 ++++++ ports/rh850/gnu/src/tx_thread_stack_build.S | 84 +++++ ports/rh850/gnu/src/tx_thread_system_return.S | 117 +++++++ ports/rh850/gnu/src/tx_timer_interrupt.c | 94 ++++++ 17 files changed, 1534 insertions(+) create mode 100644 cmake/rh850_gnu.cmake create mode 100644 cmake/v850-elf.cmake create mode 100644 ports/rh850/gnu/CMakeLists.txt create mode 100644 ports/rh850/gnu/inc/rh850_asm.h create mode 100644 ports/rh850/gnu/inc/rh850_core.h create mode 100644 ports/rh850/gnu/inc/rh850_sysreg.h create mode 100644 ports/rh850/gnu/inc/tx_port.h create mode 100644 ports/rh850/gnu/src/tx_initialize_low_level.S create mode 100644 ports/rh850/gnu/src/tx_port_asm.h create mode 100644 ports/rh850/gnu/src/tx_port_offset_check.c create mode 100644 ports/rh850/gnu/src/tx_thread_context_restore.S create mode 100644 ports/rh850/gnu/src/tx_thread_context_save.S create mode 100644 ports/rh850/gnu/src/tx_thread_interrupt_control.S create mode 100644 ports/rh850/gnu/src/tx_thread_schedule.S create mode 100644 ports/rh850/gnu/src/tx_thread_stack_build.S create mode 100644 ports/rh850/gnu/src/tx_thread_system_return.S create mode 100644 ports/rh850/gnu/src/tx_timer_interrupt.c diff --git a/cmake/rh850_gnu.cmake b/cmake/rh850_gnu.cmake new file mode 100644 index 000000000..a2b52222f --- /dev/null +++ b/cmake/rh850_gnu.cmake @@ -0,0 +1,24 @@ +# Copyright (c) 2026 Eclipse ThreadX contributors +# SPDX-License-Identifier: MIT + +set(CMAKE_SYSTEM_NAME Generic) +set(CMAKE_SYSTEM_PROCESSOR rh850) + +set(THREADX_ARCH "rh850") +set(THREADX_TOOLCHAIN "gnu") + +set(RH850_FLOAT "hard" CACHE STRING "RH850 float ABI: hard | soft") +set_property(CACHE RH850_FLOAT PROPERTY STRINGS hard soft) +if(RH850_FLOAT STREQUAL "soft") + set(ARCH_FLAGS "-mv850e3v5 -mrh850-abi -msoft-float") +elseif(RH850_FLOAT STREQUAL "hard") + set(ARCH_FLAGS "-mv850e3v5 -mrh850-abi") +else() + message(FATAL_ERROR "RH850_FLOAT must be hard or soft") +endif() +set(RH850_EXTRA_FLAGS "" CACHE STRING "Extra C and assembler flags, for example -Werror") +set(CFLAGS "${ARCH_FLAGS} ${RH850_EXTRA_FLAGS}") +set(ASFLAGS "${ARCH_FLAGS} ${RH850_EXTRA_FLAGS}") +set(LDFLAGS "${ARCH_FLAGS}") + +include(${CMAKE_CURRENT_LIST_DIR}/v850-elf.cmake) diff --git a/cmake/v850-elf.cmake b/cmake/v850-elf.cmake new file mode 100644 index 000000000..3587a464a --- /dev/null +++ b/cmake/v850-elf.cmake @@ -0,0 +1,28 @@ +# Copyright (c) 2026 Eclipse ThreadX contributors +# SPDX-License-Identifier: MIT + +set(CMAKE_C_COMPILER v850-elf-gcc) +set(CMAKE_CXX_COMPILER v850-elf-g++) +set(CMAKE_ASM_COMPILER v850-elf-gcc) +set(AS v850-elf-as) +set(AR v850-elf-ar) +set(OBJCOPY v850-elf-objcopy) +set(OBJDUMP v850-elf-objdump) +set(SIZE v850-elf-size) + +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) + +set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) + +set(CMAKE_C_FLAGS "${CFLAGS}" CACHE INTERNAL "c compiler flags") +set(CMAKE_CXX_FLAGS "${CFLAGS}" CACHE INTERNAL "cxx compiler flags") +set(CMAKE_ASM_FLAGS "${ASFLAGS}" CACHE INTERNAL "asm compiler flags") +set(CMAKE_EXE_LINKER_FLAGS "${LDFLAGS}" CACHE INTERNAL "exe link flags") + +set(CMAKE_C_FLAGS_DEBUG "-Og -g" CACHE INTERNAL "c debug compiler flags") +set(CMAKE_ASM_FLAGS_DEBUG "-g" CACHE INTERNAL "asm debug compiler flags") +set(CMAKE_C_FLAGS_RELEASE "-O2" CACHE INTERNAL "c release compiler flags") +set(CMAKE_ASM_FLAGS_RELEASE "" CACHE INTERNAL "asm release compiler flags") diff --git a/ports/rh850/gnu/CMakeLists.txt b/ports/rh850/gnu/CMakeLists.txt new file mode 100644 index 000000000..2332ade2d --- /dev/null +++ b/ports/rh850/gnu/CMakeLists.txt @@ -0,0 +1,48 @@ +# Copyright (c) 2026 Eclipse ThreadX contributors +# SPDX-License-Identifier: MIT + +target_sources(${PROJECT_NAME} + PRIVATE + # {{BEGIN_TARGET_SOURCES}} + ${CMAKE_CURRENT_LIST_DIR}/src/tx_initialize_low_level.S + ${CMAKE_CURRENT_LIST_DIR}/src/tx_thread_context_restore.S + ${CMAKE_CURRENT_LIST_DIR}/src/tx_thread_context_save.S + ${CMAKE_CURRENT_LIST_DIR}/src/tx_thread_interrupt_control.S + ${CMAKE_CURRENT_LIST_DIR}/src/tx_thread_schedule.S + ${CMAKE_CURRENT_LIST_DIR}/src/tx_thread_stack_build.S + ${CMAKE_CURRENT_LIST_DIR}/src/tx_thread_system_return.S + ${CMAKE_CURRENT_LIST_DIR}/src/tx_timer_interrupt.c + ${CMAKE_CURRENT_LIST_DIR}/src/tx_port_offset_check.c + # {{END_TARGET_SOURCES}} +) + +target_include_directories(${PROJECT_NAME} + SYSTEM + PUBLIC + ${CMAKE_CURRENT_LIST_DIR}/inc +) + +if(RH850_FLOAT STREQUAL "soft") + target_compile_definitions(${PROJECT_NAME} PUBLIC RH850_FLOAT_HARD=0) +else() + target_compile_definitions(${PROJECT_NAME} PUBLIC RH850_FLOAT_HARD=1) +endif() + +option(TX_RH850_LOW_POWER "Build with tickless idle (TX_LOW_POWER)" OFF) +if(TX_RH850_LOW_POWER) + target_sources(${PROJECT_NAME} PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/../../../utility/low_power/tx_low_power.c) + target_include_directories(${PROJECT_NAME} PUBLIC + ${CMAKE_CURRENT_LIST_DIR}/../../../utility/low_power) + target_compile_definitions(${PROJECT_NAME} PUBLIC TX_LOW_POWER) +endif() + +set(TX_RH850_MAX_IPL "" CACHE STRING "TX_PORT_MAX_IPL of the build; empty keeps the tx_port.h default") +if(NOT TX_RH850_MAX_IPL STREQUAL "") + target_compile_definitions(${PROJECT_NAME} PUBLIC TX_PORT_MAX_IPL=${TX_RH850_MAX_IPL}) +endif() + +option(TX_RH850_BUILD_QEMU_EXAMPLE "Build the QEMU rh850-u2c example" OFF) +if(TX_RH850_BUILD_QEMU_EXAMPLE) + add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/example_build/qemu_rh850_u2c) +endif() diff --git a/ports/rh850/gnu/inc/rh850_asm.h b/ports/rh850/gnu/inc/rh850_asm.h new file mode 100644 index 000000000..f461d86a5 --- /dev/null +++ b/ports/rh850/gnu/inc/rh850_asm.h @@ -0,0 +1,25 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef RH850_ASM_H +#define RH850_ASM_H + +/* GCC v850 puts __USER_LABEL_PREFIX__ ("_") in front of C symbols */ +#define RH850_CAT2(a, b) a##b +#define RH850_CAT(a, b) RH850_CAT2(a, b) +#define C_SYM(s) RH850_CAT(__USER_LABEL_PREFIX__, s) + +#ifdef __ASSEMBLER__ + .macro rh850_stsr id, sel, reg + stsr \id, \reg, \sel + .endm +#endif + +#endif diff --git a/ports/rh850/gnu/inc/rh850_core.h b/ports/rh850/gnu/inc/rh850_core.h new file mode 100644 index 000000000..cf971cf6d --- /dev/null +++ b/ports/rh850/gnu/inc/rh850_core.h @@ -0,0 +1,79 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef RH850_CORE_G4KH_H +#define RH850_CORE_G4KH_H + +#include "rh850_sysreg.h" + +#define RH850_CORE_G4KH 1 + +#define RH850_CORE_HAS_FPU 1 +#define RH850_CORE_HAS_FPU_DP 1 +#define RH850_CORE_HAS_FXU 0 +#define RH850_CORE_HAS_SYNCE 1 + +#define RH850_FPSR_CC_SHIFT 24 +#define RH850_FPSR_IF 0x00400000 +#define RH850_FPSR_RM_SHIFT 18 +#define RH850_FPSR_RM_MASK 0x000c0000 +#define RH850_FPSR_RM_RN 0 +#define RH850_FPSR_RM_RZ 1 +#define RH850_FPSR_RM_RP 2 +#define RH850_FPSR_RM_RM 3 +#define RH850_FPSR_FS 0x00020000 +#define RH850_FPSR_XC_MASK 0x0000fc00 +#define RH850_FPSR_XP_MASK 0x0000001f + +#define RH850_CORE_HAS_FPEC 1 +#define RH850_SR_FPEC 11, 0 +#define RH850_FPEC_FPIVD 0x00000001 + +#define RH850_CORE_PRIO_LEVELS 16 + +/* PLM = n masks priority numbers n and above; 10H masks none */ +#define RH850_SR_PLMR 14, 2 +#define RH850_CORE_PLMR_ALL 0x10 +#define RH850_CORE_MASK_SR RH850_SR_PLMR +#define RH850_CORE_MASK_NONE RH850_CORE_PLMR_ALL +#define RH850_CORE_MASK(prio) (prio) + +#define RH850_SR_PEID 0, 2 + +#define RH850_SR_MEA 6, 2 +#define RH850_SR_MEI 8, 2 +#define RH850_SR_RBIP 18, 2 + +#define RH850_CORE_MPU_ENTRIES 24 +#define RH850_CORE_MPU_INDEXED 1 +#define RH850_SR_MCA 8, 5 +#define RH850_SR_MCS 9, 5 +#define RH850_SR_MCR 11, 5 +#define RH850_SR_MCI 12, 5 +#define RH850_SR_MPIDX 16, 5 +#define RH850_SR_MPLA 20, 5 +#define RH850_SR_MPUA 21, 5 +#define RH850_SR_MPAT 22, 5 +#define RH850_SR_MPID0 24, 5 +#define RH850_SR_MPID1 25, 5 +#define RH850_SR_MPID2 26, 5 +#define RH850_SR_MPID3 27, 5 +#define RH850_SR_MPID4 28, 5 +#define RH850_SR_MPID5 29, 5 +#define RH850_SR_MPID6 30, 5 +#define RH850_SR_MPID7 31, 5 + +#define RH850_SR_ICTAGL 16, 4 +#define RH850_SR_ICTAGH 17, 4 +#define RH850_SR_ICDATL 18, 4 +#define RH850_SR_ICDATH 19, 4 +#define RH850_SR_ICERR 28, 4 + +#endif diff --git a/ports/rh850/gnu/inc/rh850_sysreg.h b/ports/rh850/gnu/inc/rh850_sysreg.h new file mode 100644 index 000000000..ed2596e4e --- /dev/null +++ b/ports/rh850/gnu/inc/rh850_sysreg.h @@ -0,0 +1,49 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef RH850_SYSREG_H +#define RH850_SYSREG_H + +#define RH850_SR_EIPC 0, 0 +#define RH850_SR_EIPSW 1, 0 +#define RH850_SR_FEPC 2, 0 +#define RH850_SR_FEPSW 3, 0 +#define RH850_SR_PSW 5, 0 +#define RH850_SR_FPSR 6, 0 +#define RH850_SR_FPEPC 7, 0 +#define RH850_SR_FPST 8, 0 +#define RH850_SR_FPCC 9, 0 +#define RH850_SR_FPCFG 10, 0 +#define RH850_SR_EIIC 13, 0 +#define RH850_SR_FEIC 14, 0 +#define RH850_SR_CTPC 16, 0 +#define RH850_SR_CTPSW 17, 0 +#define RH850_SR_CTBP 20, 0 +#define RH850_SR_EIWR 28, 0 +#define RH850_SR_FEWR 29, 0 +#define RH850_SR_RBASE 2, 1 +#define RH850_SR_EBASE 3, 1 +#define RH850_SR_INTBP 4, 1 +#define RH850_SR_MCTL 5, 1 +#define RH850_SR_PID 6, 1 +#define RH850_SR_SCCFG 11, 1 +#define RH850_SR_SCBP 12, 1 +#define RH850_SR_ISPR 10, 2 +#define RH850_SR_ICSR 12, 2 +#define RH850_SR_INTCFG 13, 2 + +#define RH850_PSW_ID 0x00000020 +#define RH850_PSW_EP 0x00000040 +#define RH850_PSW_NP 0x00000080 +#define RH850_PSW_EBV 0x00008000 +#define RH850_PSW_CU0 0x00010000 +#define RH850_PSW_UM 0x40000000 + +#endif diff --git a/ports/rh850/gnu/inc/tx_port.h b/ports/rh850/gnu/inc/tx_port.h new file mode 100644 index 000000000..96afc2824 --- /dev/null +++ b/ports/rh850/gnu/inc/tx_port.h @@ -0,0 +1,292 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Port Specific */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +/**************************************************************************/ +/* */ +/* PORT SPECIFIC C INFORMATION RELEASE */ +/* */ +/* tx_port.h RH850/GNU */ +/* 6.5.2 */ +/* */ +/* AUTHOR */ +/* */ +/* Parthiban Nallathambi, Linumiz */ +/* */ +/* DESCRIPTION */ +/* */ +/* This file contains data type definitions that make the ThreadX */ +/* real-time kernel function identically on a variety of different */ +/* processor architectures. For example, the size or number of bits */ +/* in an "int" data type vary between microprocessor architectures */ +/* and therefore the ThreadX C data types are mapped to the RH850 */ +/* G4KH core with the GNU tools here. Interrupt masking uses the PLMR */ +/* core mask register. */ +/* */ +/**************************************************************************/ + +#ifndef TX_PORT_H +#define TX_PORT_H + +#include + +#ifdef TX_INCLUDE_USER_DEFINE_FILE +#include "tx_user.h" +#endif + +#include "rh850_core.h" + +#define VOID void +typedef char CHAR; +typedef unsigned char UCHAR; +typedef int INT; +typedef unsigned int UINT; +typedef long LONG; +typedef unsigned long ULONG; +typedef unsigned long long ULONG64; +typedef short SHORT; +typedef unsigned short USHORT; +#define ULONG64_DEFINED + +/* Priorities below TX_PORT_MAX_IPL stay unmasked and must not call ThreadX */ +#ifndef TX_PORT_MAX_IPL +#define TX_PORT_MAX_IPL 0 +#endif +#define TX_INT_DISABLE ((UINT)RH850_CORE_MASK(TX_PORT_MAX_IPL)) +#define TX_INT_ENABLE ((UINT)RH850_CORE_MASK_NONE) + +#ifndef TX_TIMER_TICKS_PER_SECOND +#define TX_TIMER_TICKS_PER_SECOND 1000UL +#endif + +#ifndef TX_MAX_PRIORITIES +#define TX_MAX_PRIORITIES 32 +#endif + +#ifndef TX_MINIMUM_STACK +#define TX_MINIMUM_STACK 512 +#endif + +#ifndef TX_TIMER_THREAD_STACK_SIZE +#define TX_TIMER_THREAD_STACK_SIZE 1024 +#endif + +#ifndef TX_TIMER_THREAD_PRIORITY +#define TX_TIMER_THREAD_PRIORITY 0 +#endif + +#ifndef TX_TRACE_TIME_SOURCE +#define TX_TRACE_TIME_SOURCE ++_tx_trace_simulated_time +#endif + +#ifndef TX_TRACE_TIME_MASK +#define TX_TRACE_TIME_MASK 0xFFFFFFFFUL +#endif + +#define TX_PORT_SPECIFIC_BUILD_OPTIONS 0 + +#define TX_INLINE_INITIALIZATION + +#ifdef TX_ENABLE_STACK_CHECKING +#undef TX_DISABLE_STACK_FILLING +#endif + +#define TX_THREAD_EXTENSION_0 +#define TX_THREAD_EXTENSION_1 +#define TX_THREAD_EXTENSION_2 +#define TX_THREAD_EXTENSION_3 +#define TX_BLOCK_POOL_EXTENSION +#define TX_BYTE_POOL_EXTENSION +#define TX_EVENT_FLAGS_GROUP_EXTENSION +#define TX_MUTEX_EXTENSION +#define TX_QUEUE_EXTENSION +#define TX_SEMAPHORE_EXTENSION +#define TX_TIMER_EXTENSION + +#ifndef TX_THREAD_USER_EXTENSION +#define TX_THREAD_USER_EXTENSION +#endif + +#define TX_THREAD_CREATE_EXTENSION(thread_ptr) +#define TX_THREAD_DELETE_EXTENSION(thread_ptr) +#define TX_THREAD_COMPLETED_EXTENSION(thread_ptr) +#define TX_THREAD_TERMINATED_EXTENSION(thread_ptr) +#define TX_BLOCK_POOL_CREATE_EXTENSION(pool_ptr) +#define TX_BYTE_POOL_CREATE_EXTENSION(pool_ptr) +#define TX_EVENT_FLAGS_GROUP_CREATE_EXTENSION(group_ptr) +#define TX_MUTEX_CREATE_EXTENSION(mutex_ptr) +#define TX_QUEUE_CREATE_EXTENSION(queue_ptr) +#define TX_SEMAPHORE_CREATE_EXTENSION(semaphore_ptr) +#define TX_TIMER_CREATE_EXTENSION(timer_ptr) +#define TX_BLOCK_POOL_DELETE_EXTENSION(pool_ptr) +#define TX_BYTE_POOL_DELETE_EXTENSION(pool_ptr) +#define TX_EVENT_FLAGS_GROUP_DELETE_EXTENSION(group_ptr) +#define TX_MUTEX_DELETE_EXTENSION(mutex_ptr) +#define TX_QUEUE_DELETE_EXTENSION(queue_ptr) +#define TX_SEMAPHORE_DELETE_EXTENSION(semaphore_ptr) +#define TX_TIMER_DELETE_EXTENSION(timer_ptr) + +#define TX_PORT_SR_(id, sel) "i"(id), "i"(sel) +#define TX_PORT_SR(sr) TX_PORT_SR_(sr) + +static inline UINT _tx_port_mask_get(VOID) +{ + UINT v; + + __asm__ volatile("stsr %1, %0, %2" : "=r"(v) : TX_PORT_SR(RH850_CORE_MASK_SR)); + return v; +} + +static inline VOID _tx_port_mask_set(UINT v) +{ + __asm__ volatile("ldsr %0, %1, %2" ::"r"(v), TX_PORT_SR(RH850_CORE_MASK_SR) : "memory"); +} + +#define TX_INTERRUPT_SAVE_AREA UINT interrupt_save; +#if TX_PORT_MAX_IPL == 0 +#define TX_DISABLE \ + { \ + interrupt_save = _tx_port_mask_get(); \ + _tx_port_mask_set(TX_INT_DISABLE); \ + } +#else +/* A stricter mask of the caller stays in place */ +#define TX_DISABLE \ + { \ + interrupt_save = _tx_port_mask_get(); \ + if (interrupt_save > TX_INT_DISABLE) { \ + _tx_port_mask_set(TX_INT_DISABLE); \ + } \ + } +#endif +#define TX_RESTORE { _tx_port_mask_set(interrupt_save); } + +UINT _tx_thread_interrupt_control(UINT new_posture); + +VOID tx_port_setup_timer_interrupt(VOID); +extern UCHAR tx_port_free_memory[]; +#ifndef TX_PORT_FREE_MEMORY_SIZE +#define TX_PORT_FREE_MEMORY_SIZE 8192U +#endif +VOID _tx_timer_interrupt(VOID); + +#ifdef TX_LOW_POWER +VOID tx_port_low_power_timer_setup(ULONG ticks); +ULONG tx_port_low_power_adjust(VOID); +#ifndef TX_LOW_POWER_TIMER_SETUP +#define TX_LOW_POWER_TIMER_SETUP(ticks) tx_port_low_power_timer_setup(ticks) +#endif +#ifndef TX_LOW_POWER_USER_TIMER_ADJUST +#define TX_LOW_POWER_USER_TIMER_ADJUST tx_port_low_power_adjust() +#endif +#endif + +#define TX_BLOCK_POOL_DISABLE TX_DISABLE +#define TX_BYTE_POOL_DISABLE TX_DISABLE +#define TX_EVENT_FLAGS_GROUP_DISABLE TX_DISABLE +#define TX_MUTEX_DISABLE TX_DISABLE +#define TX_QUEUE_DISABLE TX_DISABLE +#define TX_SEMAPHORE_DISABLE TX_DISABLE + +#ifdef TX_REGRESSION_TEST +typedef unsigned int TEST_FLAG; +extern TEST_FLAG threadx_byte_allocate_loop_test; +extern TEST_FLAG threadx_byte_release_loop_test; +extern TEST_FLAG threadx_mutex_suspension_put_test; +extern TEST_FLAG threadx_mutex_suspension_priority_test; +#ifndef TX_TIMER_PROCESS_IN_ISR +extern TEST_FLAG threadx_delete_timer_thread; +#endif +extern void abort_and_resume_byte_allocating_thread(void); +extern void abort_all_threads_suspended_on_mutex(void); +extern void suspend_lowest_priority(void); +#ifndef TX_TIMER_PROCESS_IN_ISR +extern void delete_timer_thread(void); +#endif +extern TEST_FLAG test_stack_analyze_flag; +extern TEST_FLAG test_initialize_flag; +extern TEST_FLAG test_forced_mutex_timeout; + +#define TX_BYTE_ALLOCATE_EXTENSION \ + if (threadx_byte_allocate_loop_test == ((TEST_FLAG)1)) { \ + pool_ptr->tx_byte_pool_owner = TX_NULL; \ + threadx_byte_allocate_loop_test = ((TEST_FLAG)0); \ + } + +#define TX_BYTE_RELEASE_EXTENSION \ + if (threadx_byte_release_loop_test == ((TEST_FLAG)1)) { \ + threadx_byte_release_loop_test = ((TEST_FLAG)0); \ + abort_and_resume_byte_allocating_thread(); \ + } + +#define TX_MUTEX_PUT_EXTENSION_1 \ + if (threadx_mutex_suspension_put_test == ((TEST_FLAG)1)) { \ + threadx_mutex_suspension_put_test = ((TEST_FLAG)0); \ + abort_all_threads_suspended_on_mutex(); \ + } + +#define TX_MUTEX_PUT_EXTENSION_2 \ + if (test_forced_mutex_timeout == ((TEST_FLAG)1)) { \ + test_forced_mutex_timeout = ((TEST_FLAG)0); \ + _tx_thread_wait_abort(mutex_ptr->tx_mutex_suspension_list); \ + } + +#define TX_MUTEX_PRIORITY_CHANGE_EXTENSION \ + if (threadx_mutex_suspension_priority_test == ((TEST_FLAG)1)) { \ + threadx_mutex_suspension_priority_test = ((TEST_FLAG)0); \ + suspend_lowest_priority(); \ + } + +#ifndef TX_TIMER_PROCESS_IN_ISR +#define TX_TIMER_INITIALIZE_EXTENSION(a) \ + if (threadx_delete_timer_thread == ((TEST_FLAG)1)) { \ + threadx_delete_timer_thread = ((TEST_FLAG)0); \ + delete_timer_thread(); \ + (a) = ((UINT)1); \ + } +#endif + +#define TX_THREAD_STACK_ANALYZE_EXTENSION \ + if (test_stack_analyze_flag == ((TEST_FLAG)1)) { \ + thread_ptr->tx_thread_id = ((TEST_FLAG)0); \ + test_stack_analyze_flag = ((TEST_FLAG)0); \ + } else if (test_stack_analyze_flag == ((TEST_FLAG)2)) { \ + stack_ptr = thread_ptr->tx_thread_stack_start; \ + test_stack_analyze_flag = ((TEST_FLAG)0); \ + } else if (test_stack_analyze_flag == ((TEST_FLAG)3)) { \ + *stack_ptr = TX_STACK_FILL; \ + test_stack_analyze_flag = ((TEST_FLAG)0); \ + } else { \ + test_stack_analyze_flag = ((TEST_FLAG)0); \ + } + +#define TX_INITIALIZE_KERNEL_ENTER_EXTENSION \ + if (test_initialize_flag == ((TEST_FLAG)1)) { \ + test_initialize_flag = ((TEST_FLAG)0); \ + return; \ + } +#endif + +#ifdef TX_THREAD_INIT +CHAR _tx_version_id[] = "(c) 2026-present Eclipse ThreadX contributors. * ThreadX RH850/GNU Version 6.5.2.202603b *"; +#else +extern CHAR _tx_version_id[]; +#endif + +#endif diff --git a/ports/rh850/gnu/src/tx_initialize_low_level.S b/ports/rh850/gnu/src/tx_initialize_low_level.S new file mode 100644 index 000000000..2a8739355 --- /dev/null +++ b/ports/rh850/gnu/src/tx_initialize_low_level.S @@ -0,0 +1,127 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Initialize */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#include "tx_port_asm.h" + + .section .text.tx_port, "ax" + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_initialize_low_level RH850/GNU */ +/* 6.5.2 */ +/* AUTHOR */ +/* */ +/* Parthiban Nallathambi, Linumiz */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function makes the reset stack the system stack and gives the */ +/* free memory block of the board, tx_port_free_memory, to */ +/* tx_application_define. */ +/* */ +/* INPUT */ +/* */ +/* None */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* None */ +/* */ +/* CALLED BY */ +/* */ +/* _tx_initialize_kernel_enter ThreadX entry function */ +/* */ +/**************************************************************************/ + .global C_SYM(_tx_initialize_low_level) +C_SYM(_tx_initialize_low_level): + mov hilo(__stack_top), r10 + mov hilo(C_SYM(_tx_thread_system_stack_ptr)), r11 + st.w r10, 0[r11] + mov hilo(C_SYM(tx_port_free_memory)), r10 + mov hilo(C_SYM(_tx_initialize_unused_memory)), r11 + st.w r10, 0[r11] + jr C_SYM(tx_port_setup_timer_interrupt) + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* rh850_eiint_entry RH850/GNU */ +/* 6.5.2 */ +/* AUTHOR */ +/* */ +/* Parthiban Nallathambi, Linumiz */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function is the entry of every EIINT. It saves the interrupt */ +/* context, reenables interrupts so that higher priorities nest, */ +/* calls the interrupt controller dispatch with the EIIC value and */ +/* returns through the context restore. */ +/* */ +/* INPUT */ +/* */ +/* None */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* _tx_thread_context_save Save interrupt context */ +/* rh850_intc_dispatch Board interrupt dispatch */ +/* _tx_thread_context_restore Restore interrupt context */ +/* */ +/* CALLED BY */ +/* */ +/* EIINT vectors Interrupt request */ +/* */ +/**************************************************************************/ + .global rh850_eiint_entry +rh850_eiint_entry: + addi -8, sp, sp + st.w r10, 0[sp] + st.w r11, 4[sp] + mov hilo(C_SYM(_tx_thread_system_state)), r10 + ld.w 0[r10], r11 + cmp r0, r11 + ld.w 0[sp], r10 + ld.w 4[sp], r11 + /* movea keeps the flags of cmp */ + movea 8, sp, sp + bne 1f + tx_frame_save + br 2f +1: + tx_isr_save +2: + jarl C_SYM(_tx_thread_context_save), lp + rh850_stsr RH850_SR_EIIC, r6 + ei + jarl C_SYM(rh850_intc_dispatch), lp + di + jr C_SYM(_tx_thread_context_restore) diff --git a/ports/rh850/gnu/src/tx_port_asm.h b/ports/rh850/gnu/src/tx_port_asm.h new file mode 100644 index 000000000..59449b57c --- /dev/null +++ b/ports/rh850/gnu/src/tx_port_asm.h @@ -0,0 +1,182 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#ifndef TX_PORT_ASM_H +#define TX_PORT_ASM_H + +#include "rh850_core.h" +#include "rh850_asm.h" + +#ifndef RH850_FLOAT_HARD +#error "RH850_FLOAT_HARD (0 or 1) must be set by the build" +#endif + +#if RH850_CORE_HAS_FPU && RH850_FLOAT_HARD +#define TX_PORT_USE_FPU 1 +#else +#define TX_PORT_USE_FPU 0 +#endif + +#define TX_THREAD_RUN_COUNT 4 +#define TX_THREAD_STACK_PTR 8 +#define TX_THREAD_STACK_END 16 +#define TX_THREAD_TIME_SLICE 24 + +#define TX_FRAME_PLMR 0 +#define TX_FRAME_FPEPC 4 +#define TX_FRAME_FPSR 8 +#define TX_FRAME_EIPSW 12 +#define TX_FRAME_EIPC 16 +#define TX_FRAME_R1 20 +#define TX_FRAME_R2 24 +#define TX_FRAME_R(n) (28 + ((n) - 4) * 4) +#define TX_FRAME_SIZE 144 + +/* Nested interrupt frame: the call-clobbered registers only */ +#define TX_ISR_EIPC 0 +#define TX_ISR_EIPSW 4 +#define TX_ISR_FPSR 8 +#define TX_ISR_FPEPC 12 +#define TX_ISR_R1 16 +#define TX_ISR_R2 20 +#define TX_ISR_R5 24 +#define TX_ISR_R(n) (28 + ((n) - 6) * 4) +#define TX_ISR_R30 84 +#define TX_ISR_R31 88 +#define TX_ISR_SIZE 96 + +#ifdef __ASSEMBLER__ + + .macro tx_frame_save +#if TX_PORT_USE_FPU && RH850_CORE_HAS_SYNCE + /* Complete FPU operations and FPI reports before FPSR is read */ + synce +#endif + addi -TX_FRAME_SIZE, sp, sp + st.w r1, TX_FRAME_R1[sp] + st.w r2, TX_FRAME_R2[sp] + .irp n, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, \ + 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 + st.w r\n, TX_FRAME_R(\n)[sp] + .endr + rh850_stsr RH850_SR_EIPC, r10 + st.w r10, TX_FRAME_EIPC[sp] + rh850_stsr RH850_SR_EIPSW, r10 + st.w r10, TX_FRAME_EIPSW[sp] +#if TX_PORT_USE_FPU + rh850_stsr RH850_SR_FPSR, r10 + st.w r10, TX_FRAME_FPSR[sp] + rh850_stsr RH850_SR_FPEPC, r10 + st.w r10, TX_FRAME_FPEPC[sp] +#endif + rh850_stsr RH850_CORE_MASK_SR, r10 + st.w r10, TX_FRAME_PLMR[sp] + .endm + + /* While an FPI is pending (FPEC.FPIVD), keep FPEPC and the FPSR cause bits */ + .macro tx_fpu_restore fpsr, fpepc + ld.w \fpsr[sp], r10 +#if RH850_CORE_HAS_FPEC + rh850_stsr RH850_SR_FPEC, r11 + andi RH850_FPEC_FPIVD, r11, r11 + bz 8f + rh850_stsr RH850_SR_FPSR, r11 + andi RH850_FPSR_XC_MASK, r11, r11 + andi RH850_FPSR_XC_MASK, r10, r12 + xor r12, r10 + or r11, r10 + ldsr r10, RH850_SR_FPSR + br 9f +8: +#endif + ldsr r10, RH850_SR_FPSR + ld.w \fpepc[sp], r10 + ldsr r10, RH850_SR_FPEPC +9: + .endm + + .macro tx_frame_restore + ld.w TX_FRAME_PLMR[sp], r10 + ldsr r10, RH850_CORE_MASK_SR +#if TX_PORT_USE_FPU + tx_fpu_restore TX_FRAME_FPSR, TX_FRAME_FPEPC +#endif + ld.w TX_FRAME_EIPSW[sp], r10 + ldsr r10, RH850_SR_EIPSW + ld.w TX_FRAME_EIPC[sp], r10 + ldsr r10, RH850_SR_EIPC + ld.w TX_FRAME_R1[sp], r1 + ld.w TX_FRAME_R2[sp], r2 + .irp n, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, \ + 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 + ld.w TX_FRAME_R(\n)[sp], r\n + .endr + addi TX_FRAME_SIZE, sp, sp + eiret + .endm + + .macro tx_isr_save +#if TX_PORT_USE_FPU && RH850_CORE_HAS_SYNCE + synce +#endif + addi -TX_ISR_SIZE, sp, sp + st.w r1, TX_ISR_R1[sp] + st.w r2, TX_ISR_R2[sp] + st.w r5, TX_ISR_R5[sp] + .irp n, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 + st.w r\n, TX_ISR_R(\n)[sp] + .endr + st.w r30, TX_ISR_R30[sp] + st.w r31, TX_ISR_R31[sp] + rh850_stsr RH850_SR_EIPC, r10 + st.w r10, TX_ISR_EIPC[sp] + rh850_stsr RH850_SR_EIPSW, r10 + st.w r10, TX_ISR_EIPSW[sp] +#if TX_PORT_USE_FPU + rh850_stsr RH850_SR_FPSR, r10 + st.w r10, TX_ISR_FPSR[sp] + rh850_stsr RH850_SR_FPEPC, r10 + st.w r10, TX_ISR_FPEPC[sp] +#endif + .endm + + .macro tx_isr_restore +#if TX_PORT_USE_FPU + tx_fpu_restore TX_ISR_FPSR, TX_ISR_FPEPC +#endif + ld.w TX_ISR_EIPSW[sp], r10 + ldsr r10, RH850_SR_EIPSW + ld.w TX_ISR_EIPC[sp], r10 + ldsr r10, RH850_SR_EIPC + ld.w TX_ISR_R1[sp], r1 + ld.w TX_ISR_R2[sp], r2 + ld.w TX_ISR_R5[sp], r5 + .irp n, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 + ld.w TX_ISR_R(\n)[sp], r\n + .endr + ld.w TX_ISR_R30[sp], r30 + ld.w TX_ISR_R31[sp], r31 + addi TX_ISR_SIZE, sp, sp + eiret + .endm + +#endif + +#endif diff --git a/ports/rh850/gnu/src/tx_port_offset_check.c b/ports/rh850/gnu/src/tx_port_offset_check.c new file mode 100644 index 000000000..0d426da9b --- /dev/null +++ b/ports/rh850/gnu/src/tx_port_offset_check.c @@ -0,0 +1,33 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#include +#include "tx_api.h" +#include "tx_port_asm.h" + +#define TX_PORT_CHECK(name, cond) typedef char tx_port_check_##name[(cond) ? 1 : -1] + +TX_PORT_CHECK(run_count, offsetof(TX_THREAD, tx_thread_run_count) == TX_THREAD_RUN_COUNT); +TX_PORT_CHECK(stack_ptr, offsetof(TX_THREAD, tx_thread_stack_ptr) == TX_THREAD_STACK_PTR); +TX_PORT_CHECK(stack_end, offsetof(TX_THREAD, tx_thread_stack_end) == TX_THREAD_STACK_END); +TX_PORT_CHECK(time_slice, offsetof(TX_THREAD, tx_thread_time_slice) == TX_THREAD_TIME_SLICE); +TX_PORT_CHECK(frame_size, TX_FRAME_R(31) + 4 <= TX_FRAME_SIZE); +TX_PORT_CHECK(frame_align, (TX_FRAME_SIZE % 8) == 0); +TX_PORT_CHECK(min_stack, TX_MINIMUM_STACK >= 2 * TX_FRAME_SIZE); diff --git a/ports/rh850/gnu/src/tx_thread_context_restore.S b/ports/rh850/gnu/src/tx_thread_context_restore.S new file mode 100644 index 000000000..e65beb924 --- /dev/null +++ b/ports/rh850/gnu/src/tx_thread_context_restore.S @@ -0,0 +1,117 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#include "tx_port_asm.h" + + .section .text.tx_port, "ax" + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_thread_context_restore RH850/GNU */ +/* 6.5.2 */ +/* AUTHOR */ +/* */ +/* Parthiban Nallathambi, Linumiz */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function restores the interrupt context if it is processing a */ +/* nested interrupt. If not, it returns to the interrupt thread if no */ +/* preemption is necessary. Otherwise, if preemption is necessary or */ +/* if no thread was running, the function returns to the scheduler. A */ +/* thread whose saved PLMR is masked is not preempted. */ +/* */ +/* INPUT */ +/* */ +/* None */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* _tx_thread_schedule Thread scheduling routine */ +/* */ +/* CALLED BY */ +/* */ +/* rh850_eiint_entry Interrupt entry */ +/* */ +/**************************************************************************/ + .global C_SYM(_tx_thread_context_restore) +C_SYM(_tx_thread_context_restore): + di + mov hilo(C_SYM(_tx_thread_system_state)), r10 + ld.w 0[r10], r11 + add -1, r11 + st.w r11, 0[r10] + cmp r0, r11 + bne 3f + + mov hilo(C_SYM(_tx_thread_current_ptr)), r12 + ld.w 0[r12], r13 + cmp r0, r13 + be 2f + mov hilo(C_SYM(_tx_thread_execute_ptr)), r14 + ld.w 0[r14], r15 + cmp r13, r15 + be 1f + mov hilo(C_SYM(_tx_thread_preempt_disable)), r14 + ld.w 0[r14], r15 + cmp r0, r15 + bne 1f + /* A thread with PLMR masked is never switched out */ + ld.w TX_THREAD_STACK_PTR[r13], r14 + ld.w TX_FRAME_PLMR[r14], r15 + mov RH850_CORE_MASK_NONE, r14 + cmp r14, r15 + bne 1f +#if TX_PORT_USE_FPU && RH850_CORE_HAS_FPEC + /* Keep a thread with a pending FPI; it is taken after EIRET */ + rh850_stsr RH850_SR_FPEC, r15 + andi RH850_FPEC_FPIVD, r15, r15 + bne 1f +#endif + + mov hilo(C_SYM(_tx_timer_time_slice)), r14 + ld.w 0[r14], r15 + cmp r0, r15 + be 4f + st.w r15, TX_THREAD_TIME_SLICE[r13] + st.w r0, 0[r14] +4: + st.w r0, 0[r12] + + /* EIRET with PSW.EP = 0 also ends the interrupt in the INTC */ +2: + mov hilo(C_SYM(_tx_thread_schedule)), r10 + ldsr r10, RH850_SR_EIPC + rh850_stsr RH850_SR_PSW, r10 + ldsr r10, RH850_SR_EIPSW + eiret + +1: + ld.w TX_THREAD_STACK_PTR[r13], sp + tx_frame_restore + +3: + tx_isr_restore diff --git a/ports/rh850/gnu/src/tx_thread_context_save.S b/ports/rh850/gnu/src/tx_thread_context_save.S new file mode 100644 index 000000000..b4f848cad --- /dev/null +++ b/ports/rh850/gnu/src/tx_thread_context_save.S @@ -0,0 +1,74 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#include "tx_port_asm.h" + + .section .text.tx_port, "ax" + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_thread_context_save RH850/GNU */ +/* 6.5.2 */ +/* AUTHOR */ +/* */ +/* Parthiban Nallathambi, Linumiz */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function saves the context of an executing thread in the */ +/* beginning of interrupt processing. The function also ensures that */ +/* the system stack is used upon return to the calling ISR. */ +/* */ +/* INPUT */ +/* */ +/* None */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* None */ +/* */ +/* CALLED BY */ +/* */ +/* rh850_eiint_entry Interrupt entry */ +/* */ +/**************************************************************************/ + .global C_SYM(_tx_thread_context_save) +C_SYM(_tx_thread_context_save): + mov hilo(C_SYM(_tx_thread_system_state)), r10 + ld.w 0[r10], r11 + add 1, r11 + st.w r11, 0[r10] + cmp 1, r11 + bne 1f + mov hilo(C_SYM(_tx_thread_current_ptr)), r10 + ld.w 0[r10], r11 + cmp r0, r11 + be 1f + st.w sp, TX_THREAD_STACK_PTR[r11] + mov hilo(C_SYM(_tx_thread_system_stack_ptr)), r10 + ld.w 0[r10], sp +1: + jmp [lp] diff --git a/ports/rh850/gnu/src/tx_thread_interrupt_control.S b/ports/rh850/gnu/src/tx_thread_interrupt_control.S new file mode 100644 index 000000000..bfe632191 --- /dev/null +++ b/ports/rh850/gnu/src/tx_thread_interrupt_control.S @@ -0,0 +1,61 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#include "tx_port_asm.h" + + .section .text.tx_port, "ax" + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_thread_interrupt_control RH850/GNU */ +/* 6.5.2 */ +/* AUTHOR */ +/* */ +/* Parthiban Nallathambi, Linumiz */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function is responsible for changing the interrupt lockout */ +/* posture of the system. The posture is the PLMR value. */ +/* */ +/* INPUT */ +/* */ +/* new_posture New interrupt lockout posture */ +/* */ +/* OUTPUT */ +/* */ +/* old_posture Old interrupt lockout posture */ +/* */ +/* CALLS */ +/* */ +/* None */ +/* */ +/* CALLED BY */ +/* */ +/* Application Code */ +/* */ +/**************************************************************************/ + .global C_SYM(_tx_thread_interrupt_control) +C_SYM(_tx_thread_interrupt_control): + rh850_stsr RH850_CORE_MASK_SR, r10 + ldsr r6, RH850_CORE_MASK_SR + jmp [lp] diff --git a/ports/rh850/gnu/src/tx_thread_schedule.S b/ports/rh850/gnu/src/tx_thread_schedule.S new file mode 100644 index 000000000..46fe98c60 --- /dev/null +++ b/ports/rh850/gnu/src/tx_thread_schedule.S @@ -0,0 +1,100 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#include "tx_port_asm.h" + + .section .text.tx_port, "ax" + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_thread_schedule RH850/GNU */ +/* 6.5.2 */ +/* AUTHOR */ +/* */ +/* Parthiban Nallathambi, Linumiz */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function waits for a thread control block pointer to appear */ +/* in the _tx_thread_execute_ptr variable. Once a thread pointer */ +/* appears in the variable, the corresponding thread is resumed. */ +/* */ +/* INPUT */ +/* */ +/* None */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* tx_low_power_enter Enter low power (TX_LOW_POWER) */ +/* tx_low_power_exit Exit low power (TX_LOW_POWER) */ +/* */ +/* CALLED BY */ +/* */ +/* _tx_initialize_kernel_enter ThreadX entry function */ +/* _tx_thread_system_return Return to system from thread */ +/* _tx_thread_context_restore Restore thread's context */ +/* */ +/**************************************************************************/ + .global C_SYM(_tx_thread_schedule) +C_SYM(_tx_thread_schedule): + di + mov hilo(C_SYM(_tx_thread_system_stack_ptr)), r10 + ld.w 0[r10], sp + rh850_stsr RH850_SR_PSW, r10 + /* EP = 1: the EIRET into a thread ends no interrupt in the INTC */ + ori RH850_PSW_EP, r10, r10 + ldsr r10, RH850_SR_PSW + + mov hilo(C_SYM(_tx_thread_execute_ptr)), r10 +1: + ld.w 0[r10], r11 + cmp r0, r11 + bne 2f + mov RH850_CORE_MASK_NONE, r12 + ldsr r12, RH850_CORE_MASK_SR +#ifdef TX_LOW_POWER + /* A request ends HALT even with ID = 1, so the time slept is counted first */ + jarl C_SYM(tx_low_power_enter), lp + halt + jarl C_SYM(tx_low_power_exit), lp + mov hilo(C_SYM(_tx_thread_execute_ptr)), r10 +#endif + ei + halt + di + br 1b + +2: + mov hilo(C_SYM(_tx_thread_current_ptr)), r12 + st.w r11, 0[r12] + ld.w TX_THREAD_RUN_COUNT[r11], r13 + add 1, r13 + st.w r13, TX_THREAD_RUN_COUNT[r11] + ld.w TX_THREAD_TIME_SLICE[r11], r13 + mov hilo(C_SYM(_tx_timer_time_slice)), r12 + st.w r13, 0[r12] + ld.w TX_THREAD_STACK_PTR[r11], sp + tx_frame_restore diff --git a/ports/rh850/gnu/src/tx_thread_stack_build.S b/ports/rh850/gnu/src/tx_thread_stack_build.S new file mode 100644 index 000000000..d71674660 --- /dev/null +++ b/ports/rh850/gnu/src/tx_thread_stack_build.S @@ -0,0 +1,84 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#include "tx_port_asm.h" + + .section .text.tx_port, "ax" + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_thread_stack_build RH850/GNU */ +/* 6.5.2 */ +/* AUTHOR */ +/* */ +/* Parthiban Nallathambi, Linumiz */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function builds a stack frame on the supplied thread's stack. */ +/* The stack frame results in a fake interrupt return to the supplied */ +/* function pointer. */ +/* */ +/* INPUT */ +/* */ +/* thread_ptr Pointer to thread control blk */ +/* function_ptr Pointer to return function */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* None */ +/* */ +/* CALLED BY */ +/* */ +/* _tx_thread_create Create thread service */ +/* */ +/**************************************************************************/ + .global C_SYM(_tx_thread_stack_build) +C_SYM(_tx_thread_stack_build): + ld.w TX_THREAD_STACK_END[r6], r10 + add 1, r10 + mov -8, r11 + and r11, r10 + addi -TX_FRAME_SIZE, r10, r10 + .set off, 0 + .rept TX_FRAME_SIZE / 4 + st.w r0, off[r10] + .set off, off + 4 + .endr + st.w r7, TX_FRAME_EIPC[r10] +#if TX_PORT_USE_FPU + mov RH850_PSW_EBV | RH850_PSW_CU0, r11 + st.w r11, TX_FRAME_EIPSW[r10] + mov RH850_FPSR_FS, r11 + st.w r11, TX_FRAME_FPSR[r10] +#else + mov RH850_PSW_EBV, r11 + st.w r11, TX_FRAME_EIPSW[r10] +#endif + mov RH850_CORE_MASK_NONE, r11 + st.w r11, TX_FRAME_PLMR[r10] + st.w r10, TX_THREAD_STACK_PTR[r6] + jmp [lp] diff --git a/ports/rh850/gnu/src/tx_thread_system_return.S b/ports/rh850/gnu/src/tx_thread_system_return.S new file mode 100644 index 000000000..4de3ed89f --- /dev/null +++ b/ports/rh850/gnu/src/tx_thread_system_return.S @@ -0,0 +1,117 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Thread */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#include "tx_port_asm.h" + + .section .text.tx_port, "ax" + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_thread_system_return RH850/GNU */ +/* 6.5.2 */ +/* AUTHOR */ +/* */ +/* Parthiban Nallathambi, Linumiz */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function is target processor specific. It is used to transfer */ +/* control from a thread back to the ThreadX system. Only a minimal */ +/* context is saved since the compiler assumes temp registers are */ +/* going to get slicked by a function call anyway. The switch is made */ +/* through TRAP0. */ +/* */ +/* INPUT */ +/* */ +/* None */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* _tx_thread_schedule Thread scheduling loop */ +/* */ +/* CALLED BY */ +/* */ +/* ThreadX components */ +/* */ +/**************************************************************************/ + .global C_SYM(_tx_thread_system_return) +C_SYM(_tx_thread_system_return): +/* SYNCE first, so a pending FPU exception is taken in this thread */ +#if TX_PORT_USE_FPU && RH850_CORE_HAS_SYNCE + synce +#endif + trap 0 + jmp [lp] + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* rh850_trap0_entry RH850/GNU */ +/* 6.5.2 */ +/* AUTHOR */ +/* */ +/* Parthiban Nallathambi, Linumiz */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function is the TRAP0 handler of _tx_thread_system_return. It */ +/* saves the thread frame, stores the stack pointer and the time */ +/* slice in the thread control block and enters the scheduler on the */ +/* system stack. */ +/* */ +/* INPUT */ +/* */ +/* None */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* _tx_thread_schedule Thread scheduling loop */ +/* */ +/* CALLED BY */ +/* */ +/* _tx_thread_system_return Return to system from thread */ +/* */ +/**************************************************************************/ + .global rh850_trap0_entry +rh850_trap0_entry: + tx_frame_save + mov hilo(C_SYM(_tx_thread_current_ptr)), r12 + ld.w 0[r12], r13 + st.w sp, TX_THREAD_STACK_PTR[r13] + mov hilo(C_SYM(_tx_timer_time_slice)), r14 + ld.w 0[r14], r15 + cmp r0, r15 + be 1f + st.w r15, TX_THREAD_TIME_SLICE[r13] + st.w r0, 0[r14] +1: + st.w r0, 0[r12] + jr C_SYM(_tx_thread_schedule) diff --git a/ports/rh850/gnu/src/tx_timer_interrupt.c b/ports/rh850/gnu/src/tx_timer_interrupt.c new file mode 100644 index 000000000..f3b417042 --- /dev/null +++ b/ports/rh850/gnu/src/tx_timer_interrupt.c @@ -0,0 +1,94 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** ThreadX Component */ +/** */ +/** Timer */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#define TX_SOURCE_CODE + +#include "tx_api.h" +#include "tx_timer.h" +#include "tx_thread.h" + +/**************************************************************************/ +/* */ +/* FUNCTION RELEASE */ +/* */ +/* _tx_timer_interrupt RH850/GNU */ +/* 6.5.2 */ +/* AUTHOR */ +/* */ +/* Parthiban Nallathambi, Linumiz */ +/* */ +/* DESCRIPTION */ +/* */ +/* This function processes the hardware timer interrupt. This */ +/* processing includes incrementing the system clock and checking for */ +/* time slice and/or timer expiration. If either is found, the */ +/* expiration functions are called. */ +/* */ +/* INPUT */ +/* */ +/* None */ +/* */ +/* OUTPUT */ +/* */ +/* None */ +/* */ +/* CALLS */ +/* */ +/* _tx_timer_expiration_process Timer expiration processing */ +/* _tx_thread_time_slice Time slice interrupted thread */ +/* */ +/* CALLED BY */ +/* */ +/* tick interrupt Board tick ISR */ +/* */ +/**************************************************************************/ +VOID _tx_timer_interrupt(VOID) +{ + TX_INTERRUPT_SAVE_AREA + UINT expired; + UINT slice; + + TX_DISABLE + _tx_timer_system_clock++; + if (_tx_timer_time_slice != 0U) { + _tx_timer_time_slice--; + if (_tx_timer_time_slice == 0U) { + _tx_timer_expired_time_slice = TX_TRUE; + } + } + if (*_tx_timer_current_ptr != TX_NULL) { + _tx_timer_expired = TX_TRUE; + } else { + _tx_timer_current_ptr++; + if (_tx_timer_current_ptr == _tx_timer_list_end) { + _tx_timer_current_ptr = _tx_timer_list_start; + } + } + expired = _tx_timer_expired; + slice = _tx_timer_expired_time_slice; + TX_RESTORE + + if (expired != TX_FALSE) { + _tx_timer_expiration_process(); + } + if (slice != TX_FALSE) { + _tx_thread_time_slice(); + } +} From c73853d9380fcef8ee48d0e405d222c563928566 Mon Sep 17 00:00:00 2001 From: Parthiban Nallathambi Date: Thu, 5 Nov 2026 14:35:08 +0530 Subject: [PATCH 2/5] Added RH850 QEMU example build Runs samples/demo_threadx.c on the rh850-u2c machine of qemu-system-rh850, with two port tests. mask_posture checks that ThreadX services keep a caller PLMR of 0, and fpu_switch checks FPSR and FPEPC of switched threads in the hard float build. Tested on QEMU rh850-u2c, the example passes hard 3/3, soft 2/2 and max-ipl 3/3. Signed-off-by: Parthiban Nallathambi --- .../qemu_rh850_u2c/CMakeLists.txt | 48 ++++++ .../gnu/example_build/qemu_rh850_u2c/board.c | 36 +++++ .../gnu/example_build/qemu_rh850_u2c/board.h | 32 ++++ .../qemu_rh850_u2c/board_sources.cmake | 27 ++++ .../qemu_rh850_u2c/board_threadx.c | 125 ++++++++++++++++ .../gnu/example_build/qemu_rh850_u2c/crt0.S | 86 +++++++++++ .../qemu_rh850_u2c/demo_monitor.c | 80 ++++++++++ .../gnu/example_build/qemu_rh850_u2c/fault.c | 21 +++ .../qemu_rh850_u2c/fault_entry.S | 32 ++++ .../example_build/qemu_rh850_u2c/fpu_switch.c | 135 +++++++++++++++++ .../example_build/qemu_rh850_u2c/irq_entry.S | 58 ++++++++ .../example_build/qemu_rh850_u2c/libc_glue.c | 31 ++++ .../example_build/qemu_rh850_u2c/libc_locks.c | 101 +++++++++++++ .../qemu_rh850_u2c/mask_posture.c | 108 ++++++++++++++ .../example_build/qemu_rh850_u2c/memory.ld | 18 +++ .../gnu/example_build/qemu_rh850_u2c/rh850.ld | 75 ++++++++++ .../qemu_rh850_u2c/rh850_board.h | 24 +++ .../qemu_rh850_u2c/rh850_core.inc | 60 ++++++++ .../example_build/qemu_rh850_u2c/rh850_cpu.h | 62 ++++++++ .../qemu_rh850_u2c/rh850_fault.h | 18 +++ .../example_build/qemu_rh850_u2c/rh850_intc.c | 138 ++++++++++++++++++ .../example_build/qemu_rh850_u2c/rh850_intc.h | 29 ++++ .../qemu_rh850_u2c/rh850_intc_regs.h | 24 +++ .../example_build/qemu_rh850_u2c/rh850_io.h | 46 ++++++ .../example_build/qemu_rh850_u2c/rh850_ostm.c | 50 +++++++ .../example_build/qemu_rh850_u2c/rh850_ostm.h | 23 +++ .../qemu_rh850_u2c/rh850_ostm_regs.h | 26 ++++ .../qemu_rh850_u2c/rh850_rlin3.c | 48 ++++++ .../qemu_rh850_u2c/rh850_rlin3.h | 19 +++ .../qemu_rh850_u2c/rh850_rlin3_regs.h | 34 +++++ .../gnu/example_build/qemu_rh850_u2c/soc.c | 37 +++++ .../gnu/example_build/qemu_rh850_u2c/soc.h | 63 ++++++++ .../qemu_rh850_u2c/test_common.c | 96 ++++++++++++ .../qemu_rh850_u2c/test_common.h | 26 ++++ .../example_build/qemu_rh850_u2c/test_exit.c | 36 +++++ .../example_build/qemu_rh850_u2c/test_exit.h | 20 +++ .../example_build/qemu_rh850_u2c/vectors.S | 38 +++++ 37 files changed, 1930 insertions(+) create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/CMakeLists.txt create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/board.c create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/board.h create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/board_sources.cmake create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/board_threadx.c create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/crt0.S create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/demo_monitor.c create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/fault.c create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/fault_entry.S create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/fpu_switch.c create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/irq_entry.S create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/libc_glue.c create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/libc_locks.c create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/mask_posture.c create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/memory.ld create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850.ld create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_board.h create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_core.inc create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_cpu.h create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_fault.h create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_intc.c create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_intc.h create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_intc_regs.h create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_io.h create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_ostm.c create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_ostm.h create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_ostm_regs.h create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_rlin3.c create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_rlin3.h create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_rlin3_regs.h create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/soc.c create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/soc.h create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/test_common.c create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/test_common.h create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/test_exit.c create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/test_exit.h create mode 100644 ports/rh850/gnu/example_build/qemu_rh850_u2c/vectors.S diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/CMakeLists.txt b/ports/rh850/gnu/example_build/qemu_rh850_u2c/CMakeLists.txt new file mode 100644 index 000000000..21f914bfb --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/CMakeLists.txt @@ -0,0 +1,48 @@ +# Copyright (c) 2026 Eclipse ThreadX contributors +# SPDX-License-Identifier: MIT + +include(${CMAKE_CURRENT_LIST_DIR}/board_sources.cmake) +set(U2C_THREADX_DIR ${CMAKE_CURRENT_LIST_DIR}/../../../../..) + +find_program(RH850_QEMU NAMES qemu-system-rh850) +if(NOT RH850_QEMU) + message(STATUS "qemu-system-rh850 not found; the rh850:: tests are not added") +endif() + +function(rh850_example name) + add_executable(${name}.elf + ${RH850_U2C_SOURCES} + ${RH850_U2C_DIR}/libc_locks.c + ${RH850_U2C_DIR}/test_common.c + ${ARGN} + ) + target_include_directories(${name}.elf PRIVATE ${RH850_U2C_DIR}) + target_compile_definitions(${name}.elf PRIVATE ${RH850_U2C_DEFINITIONS}) + target_compile_options(${name}.elf PRIVATE + -Wall -Wextra -Werror -ffunction-sections -fdata-sections -fno-common) + target_link_libraries(${name}.elf PRIVATE threadx) + target_link_options(${name}.elf PRIVATE ${RH850_U2C_LINK_OPTIONS} + -Wl,-Map=${name}.map) + if(RH850_QEMU) + add_test(NAME rh850::${name} + COMMAND ${RH850_QEMU} ${RH850_U2C_QEMU_ARGS} -kernel $) + set_tests_properties(rh850::${name} PROPERTIES + TIMEOUT 60 PASS_REGULAR_EXPRESSION "${name} pass" + FAIL_REGULAR_EXPRESSION "FAIL") + endif() +endfunction() + +rh850_example(demo_threadx + ${RH850_U2C_DIR}/demo_monitor.c + ${U2C_THREADX_DIR}/samples/demo_threadx.c +) +set_source_files_properties(${U2C_THREADX_DIR}/samples/demo_threadx.c PROPERTIES + COMPILE_DEFINITIONS "main=demo_main;tx_application_define=demo_application_define" + COMPILE_OPTIONS "-Wno-unused-parameter;-Wno-return-type") + +# Build with -DTX_RH850_MAX_IPL=4 to test a PLMR stricter than TX_DISABLE +rh850_example(mask_posture ${RH850_U2C_DIR}/mask_posture.c) + +if(NOT RH850_FLOAT STREQUAL "soft") + rh850_example(fpu_switch ${RH850_U2C_DIR}/fpu_switch.c) +endif() diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/board.c b/ports/rh850/gnu/example_build/qemu_rh850_u2c/board.c new file mode 100644 index 000000000..e60d5b650 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/board.c @@ -0,0 +1,36 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include "board.h" +#include "rh850_board.h" +#include "rh850_rlin3.h" +#include "test_exit.h" + +void board_init(void) +{ + soc_init(); + rh850_rlin3_init(BOARD_CONSOLE_BASE, BOARD_CONSOLE_CLK_HZ, BOARD_CONSOLE_BAUD); +} + +void board_putc(char c) +{ + if (c == '\n') { + rh850_rlin3_putc(BOARD_CONSOLE_BASE, '\r'); + } + rh850_rlin3_putc(BOARD_CONSOLE_BASE, c); +} + +void board_exit(int code) +{ + if (code == 0) { + test_exit_pass(BOARD_TEST_EXIT_BASE); + } + test_exit_fail(BOARD_TEST_EXIT_BASE, (uint16_t)code); +} diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/board.h b/ports/rh850/gnu/example_build/qemu_rh850_u2c/board.h new file mode 100644 index 000000000..df3b40fe4 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/board.h @@ -0,0 +1,32 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef BOARD_QEMU_RH850_U2C_H +#define BOARD_QEMU_RH850_U2C_H + +#include "soc.h" + +#define BOARD_CONSOLE_BASE SOC_RLIN311_BASE +#define BOARD_CONSOLE_CLK_HZ SOC_CLK_HSB_HZ +#define BOARD_CONSOLE_BAUD 115200 + +#define BOARD_TICK_OSTM_BASE SOC_OSTM0_BASE +#define BOARD_TICK_IRQ SOC_IRQ_OSTM0 +#define BOARD_TIMER2_OSTM_BASE SOC_OSTM1_BASE +#define BOARD_TIMER2_IRQ SOC_IRQ_OSTM1 +#define BOARD_TIMER_CLK_HZ SOC_CLK_HSB_HZ + +/* Edge channels with no device in this QEMU machine, raised by software */ +#define BOARD_SWIRQ0 SOC_IRQ_TAUJ0_I0 +#define BOARD_SWIRQ1 SOC_IRQ_TAUJ0_I1 + +#define BOARD_TEST_EXIT_BASE 0x40000000 + +#endif diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/board_sources.cmake b/ports/rh850/gnu/example_build/qemu_rh850_u2c/board_sources.cmake new file mode 100644 index 000000000..a4f97e987 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/board_sources.cmake @@ -0,0 +1,27 @@ +# Copyright (c) 2026 Eclipse ThreadX contributors +# SPDX-License-Identifier: MIT + +set(RH850_U2C_DIR ${CMAKE_CURRENT_LIST_DIR}) +set(RH850_U2C_SOURCES + ${RH850_U2C_DIR}/crt0.S + ${RH850_U2C_DIR}/vectors.S + ${RH850_U2C_DIR}/irq_entry.S + ${RH850_U2C_DIR}/fault_entry.S + ${RH850_U2C_DIR}/fault.c + ${RH850_U2C_DIR}/libc_glue.c + ${RH850_U2C_DIR}/soc.c + ${RH850_U2C_DIR}/board.c + ${RH850_U2C_DIR}/rh850_intc.c + ${RH850_U2C_DIR}/rh850_ostm.c + ${RH850_U2C_DIR}/rh850_rlin3.c + ${RH850_U2C_DIR}/test_exit.c + ${RH850_U2C_DIR}/board_threadx.c +) +set(RH850_U2C_DEFINITIONS RH850_INTC_HANDLERS=8) +set(RH850_U2C_LINK_OPTIONS + -T${RH850_U2C_DIR}/rh850.ld + -L${RH850_U2C_DIR} + -nostartfiles + -Wl,--gc-sections +) +set(RH850_U2C_QEMU_ARGS -M rh850-u2c,test-exit=on -icount shift=4,sleep=on -nographic) diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/board_threadx.c b/ports/rh850/gnu/example_build/qemu_rh850_u2c/board_threadx.c new file mode 100644 index 000000000..64f9731eb --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/board_threadx.c @@ -0,0 +1,125 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include +#include +#include "tx_api.h" +#include "board.h" +#include "rh850_intc.h" +#include "rh850_ostm.h" + +UCHAR tx_port_free_memory[TX_PORT_FREE_MEMORY_SIZE] __attribute__((aligned(8))); + +#ifdef TX_REGRESSION_TEST +void test_interrupt_dispatch(void) __attribute__((weak)); +#endif + +#ifdef TX_LOW_POWER +#define CYCLES_PER_TICK (BOARD_TIMER_CLK_HZ / TX_TIMER_TICKS_PER_SECOND) +/* Counter differences stay below 2^31 */ +#define MAX_SLEEP_TICKS (0x7fffffffUL / CYCLES_PER_TICK) + +static uint32_t last_tick; +static ULONG sleep_ticks; +ULONG tx_port_low_power_ticks; + +/* A compare the counter has passed waits a whole wrap, so raise the request */ +static void set_compare(uint32_t cmp) +{ + rh850_ostm_set_compare(BOARD_TICK_OSTM_BASE, cmp); + if ((int32_t)(rh850_ostm_count(BOARD_TICK_OSTM_BASE) - cmp) >= 0) { + rh850_intc_raise(BOARD_TICK_IRQ); + } +} + +static void tick_isr(void *arg) +{ + TX_INTERRUPT_SAVE_AREA + ULONG n; + + (void)arg; + TX_DISABLE + n = (rh850_ostm_count(BOARD_TICK_OSTM_BASE) - last_tick) / CYCLES_PER_TICK; + last_tick += n * CYCLES_PER_TICK; + set_compare(last_tick + CYCLES_PER_TICK); + TX_RESTORE +#ifdef TX_REGRESSION_TEST + tx_trace_isr_enter_insert(0); + if (test_interrupt_dispatch != NULL) { + test_interrupt_dispatch(); + } +#endif + while (n-- != 0U) { + _tx_timer_interrupt(); + } +#ifdef TX_REGRESSION_TEST + tx_trace_isr_exit_insert(0); +#endif +} + +VOID tx_port_setup_timer_interrupt(VOID) +{ + rh850_intc_connect(BOARD_TICK_IRQ, tick_isr, NULL); + rh850_intc_set_priority(BOARD_TICK_IRQ, RH850_INTC_PRIO_LOWEST); + rh850_intc_enable(BOARD_TICK_IRQ); + rh850_ostm_start_freerun(BOARD_TICK_OSTM_BASE, true); + last_tick = rh850_ostm_count(BOARD_TICK_OSTM_BASE); + set_compare(last_tick + CYCLES_PER_TICK); +} + +VOID tx_port_low_power_timer_setup(ULONG ticks) +{ + if (ticks > MAX_SLEEP_TICKS) { + ticks = MAX_SLEEP_TICKS; + } + sleep_ticks = ticks; + if (ticks != 0U) { + set_compare(last_tick + ticks * CYCLES_PER_TICK); + } +} + +ULONG tx_port_low_power_adjust(VOID) +{ + ULONG n = (rh850_ostm_count(BOARD_TICK_OSTM_BASE) - last_tick) / CYCLES_PER_TICK; + + if (n > sleep_ticks) { + n = sleep_ticks; + } + sleep_ticks = 0; + last_tick += n * CYCLES_PER_TICK; + tx_port_low_power_ticks += n; + set_compare(last_tick + CYCLES_PER_TICK); + return n; +} +#else +static void tick_isr(void *arg) +{ + (void)arg; +#ifdef TX_REGRESSION_TEST + tx_trace_isr_enter_insert(0); + if (test_interrupt_dispatch != NULL) { + test_interrupt_dispatch(); + } +#endif + _tx_timer_interrupt(); +#ifdef TX_REGRESSION_TEST + tx_trace_isr_exit_insert(0); +#endif +} + +VOID tx_port_setup_timer_interrupt(VOID) +{ + rh850_intc_connect(BOARD_TICK_IRQ, tick_isr, NULL); + rh850_intc_set_priority(BOARD_TICK_IRQ, RH850_INTC_PRIO_LOWEST); + rh850_intc_enable(BOARD_TICK_IRQ); + rh850_ostm_start_interval(BOARD_TICK_OSTM_BASE, + BOARD_TIMER_CLK_HZ / TX_TIMER_TICKS_PER_SECOND - 1U, true); +} +#endif diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/crt0.S b/ports/rh850/gnu/example_build/qemu_rh850_u2c/crt0.S new file mode 100644 index 000000000..b79006885 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/crt0.S @@ -0,0 +1,86 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include "rh850_asm.h" +#include "rh850_sysreg.h" +#include "rh850_core.inc" + + .section .text.crt0, "ax" + .global __start +__start: + .irp n, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, \ + 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 + mov r0, r\n + .endr + + ldsr r0, RH850_SR_EIPC + ldsr r0, RH850_SR_FEPC + ldsr r0, RH850_SR_CTPC + ldsr r0, RH850_SR_EIWR + ldsr r0, RH850_SR_FEWR + ldsr r0, RH850_SR_INTBP + rh850_core_clear_error_regs + + /* EBASE is written while PSW.EBV = 0 */ + mov hilo(rh850_vectors), r10 + ldsr r10, RH850_SR_EBASE +#if RH850_CORE_HAS_FPU + mov hilo(RH850_PSW_ID | RH850_PSW_EBV | RH850_PSW_CU0), r10 + ldsr r10, RH850_SR_PSW + /* CU0 is in effect before the FPU registers are written */ + synci + rh850_core_init_fpu r10 +#else + mov hilo(RH850_PSW_ID | RH850_PSW_EBV), r10 + ldsr r10, RH850_SR_PSW +#endif + + rh850_core_clear_mpu r10, r11 + rh850_core_clear_icache + synci + + mov hilo(__stack_top), sp + + mov hilo(__data_load), r6 + mov hilo(__data_start), r7 + mov hilo(__data_end), r8 +2: + cmp r8, r7 + bnl 3f + ld.w 0[r6], r10 + st.w r10, 0[r7] + add 4, r6 + add 4, r7 + br 2b + +3: + mov hilo(__bss_start), r7 + mov hilo(__bss_end), r8 +4: + cmp r8, r7 + bnl 5f + st.w r0, 0[r7] + add 4, r7 + br 4b + +5: + mov hilo(__bss_large_start), r7 + mov hilo(__bss_large_end), r8 +6: + cmp r8, r7 + bnl 7f + st.w r0, 0[r7] + add 4, r7 + br 6b + +7: + jarl C_SYM(main), lp + mov r10, r6 + jarl C_SYM(board_exit), lp diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/demo_monitor.c b/ports/rh850/gnu/example_build/qemu_rh850_u2c/demo_monitor.c new file mode 100644 index 000000000..ababbcdf0 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/demo_monitor.c @@ -0,0 +1,80 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include +#include "tx_api.h" +#include "rh850_board.h" +#include "test_common.h" + +#define SECONDS 10U +#define COUNTERS 10U +#define STACK 1024U + +int demo_main(void); +void demo_application_define(void *first_unused_memory); + +extern ULONG thread_0_counter, thread_1_counter, thread_1_messages_sent, thread_2_counter, + thread_2_messages_received, thread_3_counter, thread_4_counter, thread_5_counter, + thread_6_counter, thread_7_counter; + +static volatile ULONG *const counter[COUNTERS] = { + &thread_0_counter, &thread_1_counter, &thread_1_messages_sent, &thread_2_counter, + &thread_2_messages_received, &thread_3_counter, &thread_4_counter, &thread_5_counter, + &thread_6_counter, &thread_7_counter, +}; +static const char *const counter_name[COUNTERS] = { + "thread_0", "thread_1", "sent", "thread_2", "received", + "thread_3", "thread_4", "thread_5", "thread_6", "thread_7", +}; + +static TX_THREAD monitor; +static ULONG monitor_stack[STACK / sizeof(ULONG)]; + +static void monitor_thread(ULONG arg) +{ + ULONG last[COUNTERS] = { 0 }; + + (void)arg; + for (ULONG s = 0; s < SECONDS; s++) { + tx_thread_sleep(TX_TIMER_TICKS_PER_SECOND); + for (ULONG i = 0; i < COUNTERS; i++) { + ULONG c = *counter[i]; + + if (c == last[i]) { + printf("demo_threadx FAIL: %s stalled at %lu after %lu s\n", + counter_name[i], c, s + 1U); + board_exit(240 + (int)i); + } + last[i] = c; + } + } + printf("demo_threadx: after %u s:", SECONDS); + for (ULONG i = 0; i < COUNTERS; i++) { + printf(" %s %lu", counter_name[i], last[i]); + } + printf("\n"); + test_stack_report(); + printf("demo_threadx pass\n"); + board_exit(0); +} + +void tx_application_define(void *first_unused_memory) +{ + test_stack_init(); + demo_application_define(first_unused_memory); + tx_thread_create(&monitor, "monitor", monitor_thread, 0, monitor_stack, STACK, 0, 0, + TX_NO_TIME_SLICE, TX_AUTO_START); +} + +int main(void) +{ + board_init(); + return demo_main(); +} diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/fault.c b/ports/rh850/gnu/example_build/qemu_rh850_u2c/fault.c new file mode 100644 index 000000000..001b2b867 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/fault.c @@ -0,0 +1,21 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include +#include +#include "rh850_board.h" +#include "rh850_fault.h" + +void rh850_fault(uint32_t fe, uint32_t cause, uint32_t pc) +{ + printf("FAULT %s cause 0x%08lx pc 0x%08lx\n", fe != 0U ? "FE" : "EI", + (unsigned long)cause, (unsigned long)pc); + board_exit(RH850_FAULT_EXIT_CODE); +} diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/fault_entry.S b/ports/rh850/gnu/example_build/qemu_rh850_u2c/fault_entry.S new file mode 100644 index 000000000..a27d817db --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/fault_entry.S @@ -0,0 +1,32 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include "rh850_asm.h" +#include "rh850_sysreg.h" + + .section .text.rh850_fault, "ax" + + .global rh850_fe_fault +rh850_fe_fault: + mov 1, r6 + rh850_stsr RH850_SR_FEIC, r7 + rh850_stsr RH850_SR_FEPC, r8 + mov hilo(C_SYM(rh850_fault)), r10 + jmp [r10] + + .weak rh850_trap0_entry +rh850_trap0_entry: + .global rh850_ei_fault +rh850_ei_fault: + mov 0, r6 + rh850_stsr RH850_SR_EIIC, r7 + rh850_stsr RH850_SR_EIPC, r8 + mov hilo(C_SYM(rh850_fault)), r10 + jmp [r10] diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/fpu_switch.c b/ports/rh850/gnu/example_build/qemu_rh850_u2c/fpu_switch.c new file mode 100644 index 000000000..623b41b1d --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/fpu_switch.c @@ -0,0 +1,135 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +/* + * Two threads with their own FPSR rounding mode and FPEPC divide in turn, + * switched by time slice, relinquish and an FPU using ISR. Each checks its + * result, FPSR.RM and FPEPC after every resumption. + */ + +#include +#include "tx_api.h" +#include "board.h" +#include "rh850_board.h" +#include "rh850_cpu.h" +#include "rh850_intc.h" +#include "rh850_ostm.h" +#include "test_common.h" + +#define ROUNDS 100000U +#define STACK 1024U + +static TX_THREAD thread[2]; +static ULONG stack[2][STACK / sizeof(ULONG)]; +static const ULONG rm[2] = { RH850_FPSR_RM_RM, RH850_FPSR_RM_RP }; +static const ULONG fpepc[2] = { 0x00001000UL, 0x00002000UL }; +static volatile float num = 1.0f, den = 3.0f; +static volatile float isr_result; +static volatile ULONG isr_runs, switches, last_runner = 2U, finished; +static volatile ULONG result[2]; + +static void set_rm(ULONG v) +{ + ULONG fpsr = RH850_STSR(RH850_SR_FPSR); + + RH850_LDSR((fpsr & ~RH850_FPSR_RM_MASK) | (v << RH850_FPSR_RM_SHIFT), RH850_SR_FPSR); +} + +static ULONG get_rm(void) +{ + return (RH850_STSR(RH850_SR_FPSR) & RH850_FPSR_RM_MASK) >> RH850_FPSR_RM_SHIFT; +} + +static void isr8(void *arg) +{ + (void)arg; + set_rm(RH850_FPSR_RM_RZ); + RH850_LDSR(0x00003000UL, RH850_SR_FPEPC); + isr_result = num / den; + isr_runs++; +} + +static void finish(void) +{ + union { float f; ULONG u; } down = { .f = 0.0f }, up = { .f = 0.0f }; + + rh850_ostm_stop(BOARD_TIMER2_OSTM_BASE); + down.u = result[0]; + up.u = result[1]; + printf("fpu_switch: RM -inf 0x%08lx, RM +inf 0x%08lx, %lu switches, %lu ISRs\n", down.u, + up.u, switches, isr_runs); + if (up.u != down.u + 1U) { + board_exit(222); + } + if (switches < 1000U || isr_runs < 100U) { + board_exit(223); + } + test_stack_report(); + printf("fpu_switch pass\n"); + board_exit(0); +} + +static void fpu_thread(ULONG i) +{ + TX_INTERRUPT_SAVE_AREA + + set_rm(rm[i]); + RH850_LDSR(fpepc[i], RH850_SR_FPEPC); + const float want = num / den; + + for (ULONG n = 0; n < ROUNDS; n++) { + float r = num / den; + + if (r != want || get_rm() != rm[i] || RH850_STSR(RH850_SR_FPEPC) != fpepc[i]) { + printf("fpu_switch FAIL: thread %lu round %lu rm %lu fpepc 0x%08lx\n", i, n, + get_rm(), (ULONG)RH850_STSR(RH850_SR_FPEPC)); + board_exit(220 + (int)i); + } + if (last_runner != i) { + last_runner = i; + switches++; + } + if ((n & 63U) == 0U) { + tx_thread_relinquish(); + } + } + + union { float f; ULONG u; } w = { .f = want }; + + result[i] = w.u; + TX_DISABLE + finished++; + TX_RESTORE + if (finished == 2U) { + finish(); + } +} + +void tx_application_define(void *first_unused_memory) +{ + (void)first_unused_memory; + test_stack_init(); + rh850_intc_connect(BOARD_TIMER2_IRQ, isr8, NULL); + rh850_intc_set_priority(BOARD_TIMER2_IRQ, TEST_PRIO_MID); + rh850_intc_enable(BOARD_TIMER2_IRQ); + /* OSTM1 every 50 us */ + rh850_ostm_start_interval(BOARD_TIMER2_OSTM_BASE, BOARD_TIMER_CLK_HZ / 20000U - 1U, true); + for (ULONG i = 0; i < 2U; i++) { + tx_thread_create(&thread[i], "fpu", fpu_thread, i, stack[i], STACK, 10, 10, 1, + TX_AUTO_START); + } +} + +int main(void) +{ + board_init(); + tx_kernel_enter(); + return 1; +} diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/irq_entry.S b/ports/rh850/gnu/example_build/qemu_rh850_u2c/irq_entry.S new file mode 100644 index 000000000..723551e3d --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/irq_entry.S @@ -0,0 +1,58 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include "rh850_asm.h" +#include "rh850_sysreg.h" +#include "rh850_core.h" + + .section .text.rh850_eiint, "ax" + .weak rh850_eiint_entry +rh850_eiint_entry: + pushsp r1-r2 + pushsp r5-r19 + pushsp r30-r31 + add -12, sp + rh850_stsr RH850_SR_EIPC, r10 + st.w r10, 0[sp] + rh850_stsr RH850_SR_EIPSW, r10 + st.w r10, 4[sp] +#if RH850_CORE_HAS_FPU + rh850_stsr RH850_SR_FPSR, r10 + st.w r10, 8[sp] +#endif + + rh850_stsr RH850_SR_EIIC, r6 + jarl C_SYM(rh850_intc_dispatch), lp + +#if RH850_CORE_HAS_FPU + /* A pending FPI keeps its FPSR cause bits */ + ld.w 8[sp], r10 +#if RH850_CORE_HAS_FPEC + rh850_stsr RH850_SR_FPEC, r11 + andi RH850_FPEC_FPIVD, r11, r11 + bz 1f + rh850_stsr RH850_SR_FPSR, r11 + andi RH850_FPSR_XC_MASK, r11, r11 + andi RH850_FPSR_XC_MASK, r10, r12 + xor r12, r10 + or r11, r10 +1: +#endif + ldsr r10, RH850_SR_FPSR +#endif + ld.w 4[sp], r10 + ldsr r10, RH850_SR_EIPSW + ld.w 0[sp], r10 + ldsr r10, RH850_SR_EIPC + add 12, sp + popsp r30-r31 + popsp r5-r19 + popsp r1-r2 + eiret diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/libc_glue.c b/ports/rh850/gnu/example_build/qemu_rh850_u2c/libc_glue.c new file mode 100644 index 000000000..410fdffda --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/libc_glue.c @@ -0,0 +1,31 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include +#include +#include "rh850_board.h" + +static int rh850_stdio_putc(char c, FILE *file) +{ + (void)file; + board_putc(c); + return (unsigned char)c; +} + +static FILE rh850_stdio = FDEV_SETUP_STREAM(rh850_stdio_putc, NULL, NULL, + _FDEV_SETUP_WRITE); + +FILE *const stdout = &rh850_stdio; +__strong_reference(stdout, stderr); + +void _exit(int code) +{ + board_exit(code); +} diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/libc_locks.c b/ports/rh850/gnu/example_build/qemu_rh850_u2c/libc_locks.c new file mode 100644 index 000000000..883f4ae79 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/libc_locks.c @@ -0,0 +1,101 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include +#include "tx_api.h" +#include "tx_thread.h" +#include "rh850_board.h" +#include "rh850_cpu.h" + +#define LIBC_LOCKS 4 +#define LIBC_LOCKS_EXIT 97 + +struct __lock { + TX_MUTEX mutex; + volatile UINT ready; +}; + +struct __lock __lock___libc_recursive_mutex; + +static struct __lock lock_pool[LIBC_LOCKS]; +static UINT lock_used; + +static int lock_usable(void) +{ + return (_tx_thread_current_ptr != TX_NULL) && (_tx_thread_system_state == 0U) && + (RH850_STSR(RH850_CORE_MASK_SR) == RH850_CORE_MASK_NONE) && + ((RH850_STSR(RH850_SR_PSW) & RH850_PSW_ID) == 0U); +} + +static TX_MUTEX *lock_mutex(struct __lock *lock) +{ + if (!lock->ready) { + TX_INTERRUPT_SAVE_AREA + + TX_DISABLE + if (!lock->ready) { + (void)tx_mutex_create(&lock->mutex, "libc", TX_INHERIT); + lock->ready = 1; + } + TX_RESTORE + } + return &lock->mutex; +} + +void __retarget_lock_init_recursive(_LOCK_T *lock) +{ + TX_INTERRUPT_SAVE_AREA + + TX_DISABLE + if (lock_used == LIBC_LOCKS) { + board_exit(LIBC_LOCKS_EXIT); + } + *lock = &lock_pool[lock_used++]; + TX_RESTORE +} + +void __retarget_lock_init(_LOCK_T *lock) +{ + __retarget_lock_init_recursive(lock); +} + +void __retarget_lock_close_recursive(_LOCK_T lock) +{ + (void)lock; +} + +void __retarget_lock_close(_LOCK_T lock) +{ + (void)lock; +} + +void __retarget_lock_acquire_recursive(_LOCK_T lock) +{ + if (lock_usable()) { + (void)tx_mutex_get(lock_mutex(lock), TX_WAIT_FOREVER); + } +} + +void __retarget_lock_acquire(_LOCK_T lock) +{ + __retarget_lock_acquire_recursive(lock); +} + +void __retarget_lock_release_recursive(_LOCK_T lock) +{ + if (lock_usable() && lock->ready && lock->mutex.tx_mutex_owner == _tx_thread_current_ptr) { + (void)tx_mutex_put(&lock->mutex); + } +} + +void __retarget_lock_release(_LOCK_T lock) +{ + __retarget_lock_release_recursive(lock); +} diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/mask_posture.c b/ports/rh850/gnu/example_build/qemu_rh850_u2c/mask_posture.c new file mode 100644 index 000000000..8395c95fe --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/mask_posture.c @@ -0,0 +1,108 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +/* + * A thread masks every priority with PLMR 0 and calls ThreadX services. With + * TX_PORT_MAX_IPL 4, a pending priority 3 interrupt must stay held. + */ + +#include +#include "tx_api.h" +#include "board.h" +#include "rh850_board.h" +#include "rh850_cpu.h" +#include "rh850_intc.h" +#include "test_common.h" + +#define STACK 1024U + +static TX_THREAD main_thread; +static ULONG main_stack[STACK / sizeof(ULONG)]; +static TX_SEMAPHORE sem; +static volatile UINT fired; + +static UINT plmr(void) +{ + return (UINT)RH850_STSR(RH850_CORE_MASK_SR); +} + +static void check(int ok, int code, const char *what) +{ + if (!ok) { + printf("mask_posture FAIL %d: %s (PLMR 0x%x, fired %u)\n", code, what, plmr(), + fired); + board_exit(code); + } +} + +static void isr_high(void *arg) +{ + (void)arg; + fired++; +} + +static void services(void) +{ + ULONG count = 0; + + tx_semaphore_put(&sem); + tx_semaphore_get(&sem, TX_NO_WAIT); + (void)tx_time_get(); + tx_semaphore_info_get(&sem, TX_NULL, &count, TX_NULL, TX_NULL, TX_NULL); + check(count == 0U, 170, "semaphore count"); +} + +static void main_entry(ULONG arg) +{ + TX_INTERRUPT_SAVE_AREA + + (void)arg; + RH850_LDSR(RH850_CORE_MASK(0), RH850_CORE_MASK_SR); + rh850_intc_raise(BOARD_SWIRQ1); + test_spin(200); + check(fired == 0U, 171, "priority 3 held with PLMR 0"); + services(); + test_spin(200); + check(plmr() == RH850_CORE_MASK(0), 172, "PLMR 0 after the services"); + check(fired == 0U, 173, "priority 3 held through the services"); + RH850_LDSR(RH850_CORE_MASK_NONE, RH850_CORE_MASK_SR); + test_spin(200); + check(fired == 1U, 174, "priority 3 taken after PLMR is opened"); + + TX_DISABLE + check(plmr() == TX_INT_DISABLE, 175, "TX_DISABLE from an open PLMR"); + TX_RESTORE + check(plmr() == TX_INT_ENABLE, 176, "TX_RESTORE"); + + printf("mask_posture: TX_PORT_MAX_IPL %u, PLMR 0 kept through the services\n", + (UINT)TX_PORT_MAX_IPL); + test_stack_report(); + printf("mask_posture pass\n"); + board_exit(0); +} + +void tx_application_define(void *first_unused_memory) +{ + (void)first_unused_memory; + test_stack_init(); + rh850_intc_connect(BOARD_SWIRQ1, isr_high, NULL); + rh850_intc_set_priority(BOARD_SWIRQ1, TEST_PRIO_HIGH); + rh850_intc_enable(BOARD_SWIRQ1); + tx_semaphore_create(&sem, "sem", 0); + tx_thread_create(&main_thread, "main", main_entry, 0, main_stack, STACK, 10, 10, + TX_NO_TIME_SLICE, TX_AUTO_START); +} + +int main(void) +{ + board_init(); + tx_kernel_enter(); + return 1; +} diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/memory.ld b/ports/rh850/gnu/example_build/qemu_rh850_u2c/memory.ld new file mode 100644 index 000000000..36c431a5b --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/memory.ld @@ -0,0 +1,18 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +MEMORY +{ + FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 8M + RAM (rwx) : ORIGIN = 0xfdc00000, LENGTH = 128K + CRAM (rwx) : ORIGIN = 0xfe000000, LENGTH = 0x138000 +} + +REGION_ALIAS("RAM_LARGE", CRAM); diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850.ld b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850.ld new file mode 100644 index 000000000..bf9d5b569 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850.ld @@ -0,0 +1,75 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +OUTPUT_ARCH(v850:rh850) +ENTRY(__start) + +INCLUDE memory.ld + +__stack_size = DEFINED(__stack_size) ? __stack_size : 0x1000; +__heap_size = DEFINED(__heap_size) ? __heap_size : 0; + +SECTIONS +{ + .vectors ORIGIN(FLASH) : { + KEEP(*(.vectors)) + } > FLASH + + .text : { + *(.text.crt0) + *(.text .text.*) + } > FLASH + + .rodata : { + *(.rodata .rodata.*) + *(.rosdata .rosdata.* .rozdata .rozdata.*) + . = ALIGN(4); + } > FLASH + + .data : ALIGN(4) { + __data_start = .; + *(.data .data.*) + *(.sdata .sdata.* .tdata .tdata.* .zdata .zdata.*) + . = ALIGN(4); + __data_end = .; + } > RAM AT > FLASH + __data_load = LOADADDR(.data); + + /* Before .bss, so that .bss.rh850_large is not taken by .bss.* */ + .bss_large (NOLOAD) : ALIGN(8) { + __bss_large_start = .; + *(.bss.rh850_large .bss.rh850_large.*) + . = ALIGN(4); + __bss_large_end = .; + } > RAM_LARGE + + .bss (NOLOAD) : ALIGN(4) { + __bss_start = .; + *(.bss .bss.*) + *(.sbss .sbss.* .tbss .tbss.* .zbss .zbss.*) + *(COMMON) + . = ALIGN(4); + __bss_end = .; + } > RAM + + .stack (NOLOAD) : ALIGN(8) { + . += __stack_size; + __stack_top = .; + } > RAM + + /* The C names of these symbols carry the "_" prefix of GCC v850 */ + .heap (NOLOAD) : ALIGN(8) { + ___heap_start = .; + . += __heap_size; + ___heap_end = .; + } > RAM +} + +ASSERT(ADDR(.vectors) == 0, "vectors must be at the reset address 0") diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_board.h b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_board.h new file mode 100644 index 000000000..30d4b14a6 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_board.h @@ -0,0 +1,24 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef RH850_BOARD_H +#define RH850_BOARD_H + +#include + +void board_init(void); +void board_putc(char c); +void board_exit(int code) __attribute__((noreturn)); + +#define RH850_LARGE_BSS __attribute__((section(".bss.rh850_large"))) + +#define RH850_FAULT_EXIT_CODE 99 + +#endif diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_core.inc b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_core.inc new file mode 100644 index 000000000..a9284978f --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_core.inc @@ -0,0 +1,60 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include "rh850_core.h" + + .macro rh850_core_clear_error_regs + ldsr r0, RH850_SR_MEA + ldsr r0, RH850_SR_MEI + ldsr r0, RH850_SR_RBIP + .endm + + /* PSW.CU0 must be set and in effect */ + .macro rh850_core_init_fpu tmp + ldsr r0, RH850_SR_FPST + ldsr r0, RH850_SR_FPCC + mov hilo(RH850_FPSR_FS), \tmp + ldsr \tmp, RH850_SR_FPSR + ldsr r0, RH850_SR_FPEPC + .endm + + .macro rh850_core_clear_mpu idx, end + ldsr r0, RH850_SR_MCA + ldsr r0, RH850_SR_MCS + ldsr r0, RH850_SR_MCR + ldsr r0, RH850_SR_MCI + mov r0, \idx + movea RH850_CORE_MPU_ENTRIES, r0, \end +1: + ldsr \idx, RH850_SR_MPIDX + ldsr r0, RH850_SR_MPLA + ldsr r0, RH850_SR_MPUA + ldsr r0, RH850_SR_MPAT + add 1, \idx + cmp \end, \idx + bl 1b + ldsr r0, RH850_SR_MPIDX + ldsr r0, RH850_SR_MPID0 + ldsr r0, RH850_SR_MPID1 + ldsr r0, RH850_SR_MPID2 + ldsr r0, RH850_SR_MPID3 + ldsr r0, RH850_SR_MPID4 + ldsr r0, RH850_SR_MPID5 + ldsr r0, RH850_SR_MPID6 + ldsr r0, RH850_SR_MPID7 + .endm + + .macro rh850_core_clear_icache + ldsr r0, RH850_SR_ICTAGL + ldsr r0, RH850_SR_ICTAGH + ldsr r0, RH850_SR_ICDATL + ldsr r0, RH850_SR_ICDATH + ldsr r0, RH850_SR_ICERR + .endm diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_cpu.h b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_cpu.h new file mode 100644 index 000000000..7b588f36a --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_cpu.h @@ -0,0 +1,62 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef RH850_CPU_H +#define RH850_CPU_H + +#include +#include "rh850_sysreg.h" + +#define rh850_stsr_(id, sel) ({ \ + uint32_t v_; \ + __asm__ volatile("stsr %1, %0, %2" : "=r"(v_) : "i"(id), "i"(sel)); \ + v_; }) +#define rh850_ldsr_(v, id, sel) \ + __asm__ volatile("ldsr %0, %1, %2" :: "r"(v), "i"(id), "i"(sel) : "memory") + +#define RH850_STSR(sr) rh850_stsr_(sr) +#define RH850_LDSR(v, sr) rh850_ldsr_((uint32_t)(v), sr) + +static inline void rh850_di(void) +{ + __asm__ volatile("di" ::: "memory"); +} + +static inline void rh850_ei(void) +{ + __asm__ volatile("ei" ::: "memory"); +} + +static inline void rh850_syncp(void) +{ + __asm__ volatile("syncp" ::: "memory"); +} + +static inline void rh850_halt(void) +{ + __asm__ volatile("halt" ::: "memory"); +} + +static inline uint32_t rh850_irq_save(void) +{ + uint32_t psw = RH850_STSR(RH850_SR_PSW); + + rh850_di(); + return psw; +} + +static inline void rh850_irq_restore(uint32_t psw) +{ + if ((psw & RH850_PSW_ID) == 0U) { + rh850_ei(); + } +} + +#endif diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_fault.h b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_fault.h new file mode 100644 index 000000000..579a5bd72 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_fault.h @@ -0,0 +1,18 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef RH850_FAULT_H +#define RH850_FAULT_H + +#include + +void rh850_fault(uint32_t fe, uint32_t cause, uint32_t pc) __attribute__((noreturn)); + +#endif diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_intc.c b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_intc.c new file mode 100644 index 000000000..a60c319b8 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_intc.c @@ -0,0 +1,138 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include +#include +#include "rh850_cpu.h" +#include "rh850_fault.h" +#include "rh850_io.h" +#include "rh850_intc.h" +#include "rh850_intc_regs.h" +#include "soc.h" + +#ifndef RH850_INTC_HANDLERS +#define RH850_INTC_HANDLERS SOC_INTC_CHANNELS +#endif + +#if RH850_INTC_HANDLERS < 255 +typedef uint8_t rh850_intc_slot_t; +#else +typedef uint16_t rh850_intc_slot_t; +#endif + +struct rh850_intc_entry { + rh850_isr_t isr; + void *arg; +}; + +static rh850_intc_slot_t rh850_intc_slot[SOC_INTC_CHANNELS]; +static struct rh850_intc_entry rh850_intc_table[RH850_INTC_HANDLERS]; +static unsigned int rh850_intc_used; + +static uintptr_t eic_addr(unsigned int ch) +{ + uintptr_t base = (ch < SOC_INTC1_CHANNELS) ? SOC_INTC1_BASE : SOC_INTC2_BASE; + + return base + (uintptr_t)ch * RH850_EIC_STRIDE; +} + +/* Write the low byte of EICn only, so EIRF stays */ +static void eic_update(unsigned int ch, uint8_t clear, uint8_t set) +{ + uintptr_t addr = eic_addr(ch); + uint32_t key = rh850_irq_save(); + uint8_t v = rh850_read8(addr); + + /* EIOV is kept; bit 4 is reserved and written as 0 */ + v &= (uint8_t)~(clear | 0x10U); + v |= set; + rh850_write8(addr, v); + (void)rh850_read8(addr); + rh850_syncp(); + rh850_irq_restore(key); +} + +void rh850_intc_connect(unsigned int ch, rh850_isr_t isr, void *arg) +{ + if (ch >= SOC_INTC_CHANNELS) { + return; + } + + uint32_t key = rh850_irq_save(); + unsigned int slot = rh850_intc_slot[ch]; + + if (slot == 0U) { + if (rh850_intc_used == RH850_INTC_HANDLERS) { + rh850_fault(0, 0x10000U | ch, 0); + } + slot = ++rh850_intc_used; + rh850_intc_slot[ch] = (rh850_intc_slot_t)slot; + } + rh850_intc_table[slot - 1U].isr = isr; + rh850_intc_table[slot - 1U].arg = arg; + rh850_irq_restore(key); +} + +void rh850_intc_set_priority(unsigned int ch, unsigned int prio) +{ + if (ch < SOC_INTC_CHANNELS) { + eic_update(ch, RH850_EIC_EIP_MASK | RH850_EIC_EITB, + (uint8_t)(prio & RH850_EIC_EIP_MASK)); + } +} + +void rh850_intc_enable(unsigned int ch) +{ + if (ch < SOC_INTC_CHANNELS) { + eic_update(ch, RH850_EIC_EIMK, 0); + } +} + +void rh850_intc_disable(unsigned int ch) +{ + if (ch < SOC_INTC_CHANNELS) { + eic_update(ch, 0, RH850_EIC_EIMK); + } +} + +void rh850_intc_raise(unsigned int ch) +{ + if (ch < SOC_INTC_CHANNELS) { + uintptr_t addr = eic_addr(ch) + 1U; + + /* Upper byte: EIRF is bit 4, the other bits read-only or 0 */ + rh850_write8(addr, (uint8_t)(RH850_EIC_EIRF >> 8)); + (void)rh850_read8(addr); + rh850_syncp(); + } +} + +void rh850_intc_clear(unsigned int ch) +{ + if (ch < SOC_INTC_CHANNELS) { + uintptr_t addr = eic_addr(ch) + 1U; + + rh850_write8(addr, 0); + (void)rh850_read8(addr); + rh850_syncp(); + } +} + +void rh850_intc_dispatch(uint32_t eiic) +{ + uint32_t ch = (eiic & 0xffffU) - RH850_EIIC_EIINT; + unsigned int slot = (ch < SOC_INTC_CHANNELS) ? rh850_intc_slot[ch] : 0U; + + if ((slot == 0U) || (rh850_intc_table[slot - 1U].isr == NULL)) { + rh850_fault(0, eiic, RH850_STSR(RH850_SR_EIPC)); + } + + rh850_intc_table[slot - 1U].isr(rh850_intc_table[slot - 1U].arg); +} diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_intc.h b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_intc.h new file mode 100644 index 000000000..781a3e053 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_intc.h @@ -0,0 +1,29 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef RH850_INTC_H +#define RH850_INTC_H + +#include + +typedef void (*rh850_isr_t)(void *arg); + +#define RH850_INTC_PRIO_LOWEST 15 + +void rh850_intc_connect(unsigned int ch, rh850_isr_t isr, void *arg); +void rh850_intc_set_priority(unsigned int ch, unsigned int prio); +void rh850_intc_enable(unsigned int ch); +void rh850_intc_disable(unsigned int ch); +void rh850_intc_raise(unsigned int ch); +void rh850_intc_clear(unsigned int ch); + +void rh850_intc_dispatch(uint32_t eiic); + +#endif diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_intc_regs.h b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_intc_regs.h new file mode 100644 index 000000000..a61aa405e --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_intc_regs.h @@ -0,0 +1,24 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef RH850_INTC_REGS_H +#define RH850_INTC_REGS_H + +#define RH850_EIC_STRIDE 2 +#define RH850_EIC_EIP_MASK 0x0f +#define RH850_EIC_EIOV (1 << 5) +#define RH850_EIC_EITB (1 << 6) +#define RH850_EIC_EIMK (1 << 7) +/* EIRF is bit 12, bit 4 of the upper byte */ +#define RH850_EIC_EIRF (1 << 12) + +#define RH850_EIIC_EIINT 0x1000 + +#endif diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_io.h b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_io.h new file mode 100644 index 000000000..64317367f --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_io.h @@ -0,0 +1,46 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef RH850_IO_H +#define RH850_IO_H + +#include + +static inline uint8_t rh850_read8(uintptr_t addr) +{ + return *(volatile uint8_t *)addr; +} + +static inline uint16_t rh850_read16(uintptr_t addr) +{ + return *(volatile uint16_t *)addr; +} + +static inline uint32_t rh850_read32(uintptr_t addr) +{ + return *(volatile uint32_t *)addr; +} + +static inline void rh850_write8(uintptr_t addr, uint8_t v) +{ + *(volatile uint8_t *)addr = v; +} + +static inline void rh850_write16(uintptr_t addr, uint16_t v) +{ + *(volatile uint16_t *)addr = v; +} + +static inline void rh850_write32(uintptr_t addr, uint32_t v) +{ + *(volatile uint32_t *)addr = v; +} + +#endif diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_ostm.c b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_ostm.c new file mode 100644 index 000000000..789251218 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_ostm.c @@ -0,0 +1,50 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include "rh850_io.h" +#include "rh850_ostm.h" +#include "rh850_ostm_regs.h" + +void rh850_ostm_stop(uintptr_t base) +{ + rh850_write8(base + RH850_OSTM_TT, 1); + while ((rh850_read8(base + RH850_OSTM_TE) & 1U) != 0U) { + } +} + +/* CTL can only be written while the counter is stopped */ +void rh850_ostm_start_interval(uintptr_t base, uint32_t cmp, bool irq) +{ + rh850_ostm_stop(base); + rh850_write8(base + RH850_OSTM_CTL, irq ? RH850_OSTM_CTL_IE : 0U); + rh850_write32(base + RH850_OSTM_CMP, cmp); + rh850_write8(base + RH850_OSTM_TS, 1); +} + +uint32_t rh850_ostm_count(uintptr_t base) +{ + return rh850_read32(base + RH850_OSTM_CNT); +} + +void rh850_ostm_start_freerun(uintptr_t base, bool irq) +{ + rh850_ostm_stop(base); + rh850_write8(base + RH850_OSTM_CTL, + RH850_OSTM_CTL_MD1 | (irq ? RH850_OSTM_CTL_IE : 0U)); + rh850_write32(base + RH850_OSTM_CMP, 0xffffffffU); + rh850_write8(base + RH850_OSTM_TS, 1); +} + +/* Read back so the new CMP is in place before the caller checks CNT */ +void rh850_ostm_set_compare(uintptr_t base, uint32_t cmp) +{ + rh850_write32(base + RH850_OSTM_CMP, cmp); + (void)rh850_read32(base + RH850_OSTM_CMP); +} diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_ostm.h b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_ostm.h new file mode 100644 index 000000000..ed184e949 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_ostm.h @@ -0,0 +1,23 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef RH850_OSTM_H +#define RH850_OSTM_H + +#include +#include + +void rh850_ostm_start_interval(uintptr_t base, uint32_t cmp, bool irq); +void rh850_ostm_start_freerun(uintptr_t base, bool irq); +void rh850_ostm_set_compare(uintptr_t base, uint32_t cmp); +void rh850_ostm_stop(uintptr_t base); +uint32_t rh850_ostm_count(uintptr_t base); + +#endif diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_ostm_regs.h b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_ostm_regs.h new file mode 100644 index 000000000..ef0d1d85f --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_ostm_regs.h @@ -0,0 +1,26 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef RH850_OSTM_REGS_H +#define RH850_OSTM_REGS_H + +#define RH850_OSTM_CMP 0x00 +#define RH850_OSTM_CNT 0x04 +#define RH850_OSTM_TE 0x10 +#define RH850_OSTM_TS 0x14 +#define RH850_OSTM_TT 0x18 +#define RH850_OSTM_CTL 0x20 + +#define RH850_OSTM_CTL_MD0 (1 << 0) +#define RH850_OSTM_CTL_MD1 (1 << 1) +#define RH850_OSTM_CTL_MD2 (1 << 2) +#define RH850_OSTM_CTL_IE (1 << 7) + +#endif diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_rlin3.c b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_rlin3.c new file mode 100644 index 000000000..e7baa286f --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_rlin3.c @@ -0,0 +1,48 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include "rh850_io.h" +#include "rh850_rlin3.h" +#include "rh850_rlin3_regs.h" + +/* baud = clk / (LBRP01 + 1) / 16 with LWBR = 0; LBFC = 0 gives 8N1 */ +void rh850_rlin3_init(uintptr_t base, uint32_t clk_hz, uint32_t baud) +{ + uint32_t div = (clk_hz + (RH850_RLIN3_SAMPLES * baud) / 2U) / + (RH850_RLIN3_SAMPLES * baud); + + if (div == 0U) { + div = 1U; + } + + rh850_write8(base + RH850_RLIN3_LCUC, 0); + while ((rh850_read8(base + RH850_RLIN3_LMST) & RH850_RLIN3_LMST_OMM0) != 0U) { + } + + rh850_write8(base + RH850_RLIN3_LWBR, 0); + rh850_write16(base + RH850_RLIN3_LBRP01, (uint16_t)(div - 1U)); + rh850_write8(base + RH850_RLIN3_LMD, RH850_RLIN3_LMD_UART); + rh850_write8(base + RH850_RLIN3_LBFC, 0); + rh850_write8(base + RH850_RLIN3_LEDE, 0); + rh850_write8(base + RH850_RLIN3_LUOR1, 0); + + rh850_write8(base + RH850_RLIN3_LCUC, RH850_RLIN3_LCUC_OM0); + while ((rh850_read8(base + RH850_RLIN3_LMST) & RH850_RLIN3_LMST_OMM0) == 0U) { + } + rh850_write8(base + RH850_RLIN3_LUOER, + RH850_RLIN3_LUOER_UTOE | RH850_RLIN3_LUOER_UROE); +} + +void rh850_rlin3_putc(uintptr_t base, char c) +{ + rh850_write8(base + RH850_RLIN3_LUTDR, (uint8_t)c); + while ((rh850_read8(base + RH850_RLIN3_LST) & RH850_RLIN3_LST_UTS) != 0U) { + } +} diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_rlin3.h b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_rlin3.h new file mode 100644 index 000000000..8b830a197 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_rlin3.h @@ -0,0 +1,19 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef RH850_RLIN3_H +#define RH850_RLIN3_H + +#include + +void rh850_rlin3_init(uintptr_t base, uint32_t clk_hz, uint32_t baud); +void rh850_rlin3_putc(uintptr_t base, char c); + +#endif diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_rlin3_regs.h b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_rlin3_regs.h new file mode 100644 index 000000000..c18296a8b --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/rh850_rlin3_regs.h @@ -0,0 +1,34 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef RH850_RLIN3_REGS_H +#define RH850_RLIN3_REGS_H + +#define RH850_RLIN3_LWBR 0x01 +#define RH850_RLIN3_LBRP01 0x02 +#define RH850_RLIN3_LMD 0x08 +#define RH850_RLIN3_LBFC 0x09 +#define RH850_RLIN3_LEDE 0x0d +#define RH850_RLIN3_LCUC 0x0e +#define RH850_RLIN3_LMST 0x11 +#define RH850_RLIN3_LST 0x12 +#define RH850_RLIN3_LUOER 0x20 +#define RH850_RLIN3_LUOR1 0x21 +#define RH850_RLIN3_LUTDR 0x24 + +#define RH850_RLIN3_SAMPLES 16 +#define RH850_RLIN3_LMD_UART 0x01 +#define RH850_RLIN3_LCUC_OM0 (1 << 0) +#define RH850_RLIN3_LMST_OMM0 (1 << 0) +#define RH850_RLIN3_LST_UTS (1 << 4) +#define RH850_RLIN3_LUOER_UTOE (1 << 0) +#define RH850_RLIN3_LUOER_UROE (1 << 1) + +#endif diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/soc.c b/ports/rh850/gnu/example_build/qemu_rh850_u2c/soc.c new file mode 100644 index 000000000..d60806c9b --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/soc.c @@ -0,0 +1,37 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include +#include "rh850_io.h" +#include "soc.h" + +static void u2c_module_release(uintptr_t swmress, uintptr_t msr, uint32_t bit) +{ + while ((rh850_read32(swmress) & bit) != 0U) { + } + rh850_write32(SOC_MSRKCPROT, SOC_KCPROT_ENABLE); + rh850_write32(msr, rh850_read32(msr) & ~bit); + while ((rh850_read32(msr) & bit) != 0U) { + } + rh850_write32(SOC_MSRKCPROT, SOC_KCPROT_DISABLE); +} + +void soc_init(void) +{ + rh850_write32(SOC_CLKKCPROT1, SOC_KCPROT_ENABLE); + rh850_write32(SOC_CKS_RLINC, SOC_CKS_RLIN_HSB); + while ((rh850_read32(SOC_CKS_RLINS) & 0x7U) != SOC_CKS_RLIN_HSB) { + } + rh850_write32(SOC_CLKKCPROT1, SOC_KCPROT_DISABLE); + + u2c_module_release(SOC_SWMRESS_RLIN3, SOC_MSR_RLIN3, SOC_MSR_RLIN311_BIT); + u2c_module_release(SOC_SWMRESS_OSTM, SOC_MSR_OSTM, SOC_MSR_OSTM0_BIT); + u2c_module_release(SOC_SWMRESS_OSTM, SOC_MSR_OSTM, SOC_MSR_OSTM1_BIT); +} diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/soc.h b/ports/rh850/gnu/example_build/qemu_rh850_u2c/soc.h new file mode 100644 index 000000000..6cede76e8 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/soc.h @@ -0,0 +1,63 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef SOC_U2C_H +#define SOC_U2C_H + +#define SOC_FLASH_BASE 0x00000000 +#define SOC_FLASH_SIZE 0x00800000 +#define SOC_LRAM0_BASE 0xfdc00000 +#define SOC_LRAM0_SIZE 0x00020000 + +#define SOC_INTC1_BASE 0xfffc4000 +#define SOC_INTC2_BASE 0xfff80000 +#define SOC_INTC1_CHANNELS 32 +#define SOC_INTC_CHANNELS 630 + +#define SOC_OSTM0_BASE 0xffbf0000 +#define SOC_OSTM1_BASE 0xff89a100 +#define SOC_IRQ_OSTM0 63 +#define SOC_IRQ_OSTM1 484 + +#define SOC_IRQ_TAUJ0_I0 133 +#define SOC_IRQ_TAUJ0_I1 134 + +#define SOC_RLIN311_BASE 0xff89bb00 +#define SOC_IRQ_RLIN311 493 +#define SOC_IRQ_RLIN311_UR0 494 +#define SOC_IRQ_RLIN311_UR1 495 +#define SOC_IRQ_RLIN311_UR2 496 + +/* 20 MHz is the QEMU value; not checked on silicon */ +#define SOC_CLK_HSB_HZ 20000000 + +#define SOC_CKS_RLINC 0xff980140 +#define SOC_CKS_RLINS 0xff980148 +#define SOC_CLKKCPROT1 0xff980700 +#define SOC_CKS_RLIN_HSB 1 + +#define SOC_SWMRESS_RLIN3 0xff980964 +#define SOC_SWMRESS_OSTM 0xff980a84 + +#define SOC_MSR_RLIN3 0xff981060 +#define SOC_MSR_OSTM 0xff981180 +#define SOC_MSRKCPROT 0xff981710 +#define SOC_MSR_RLIN311_BIT (1 << 11) +#define SOC_MSR_OSTM0_BIT (1 << 0) +#define SOC_MSR_OSTM1_BIT (1 << 1) + +#define SOC_KCPROT_ENABLE 0xa5a5a501 +#define SOC_KCPROT_DISABLE 0xa5a5a500 + +#ifndef __ASSEMBLER__ +void soc_init(void); +#endif + +#endif diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/test_common.c b/ports/rh850/gnu/example_build/qemu_rh850_u2c/test_common.c new file mode 100644 index 000000000..01a754493 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/test_common.c @@ -0,0 +1,96 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include +#include "tx_api.h" +#include "tx_thread.h" +#include "rh850_board.h" +#include "test_common.h" + +extern char stack_top[] __asm__("__stack_top"); +extern char stack_size[] __asm__("__stack_size"); + +#define SYS_FILL 0x5aa5c33cUL +#define SYS_CANARY 0xc0deca11UL +#define CANARY_EXIT 131 + +uintptr_t test_sys_stack_bottom(void) +{ + return (uintptr_t)stack_top - (uintptr_t)stack_size; +} + +uintptr_t test_sys_stack_top(void) +{ + return (uintptr_t)stack_top; +} + +uintptr_t test_sp(void) +{ + uintptr_t sp; + + __asm__ volatile("mov sp, %0" : "=r"(sp)); + return sp; +} + +void test_spin(unsigned int n) +{ + for (volatile unsigned int i = 0; i < n; i++) { + } +} + +static void stack_error(TX_THREAD *thread_ptr) +{ + printf("stack FAIL: %s overflow\n", thread_ptr->tx_thread_name); + board_exit(130); +} + +void test_stack_init(void) +{ + ULONG *p = (ULONG *)test_sys_stack_bottom(); + ULONG *end = (ULONG *)(test_sp() - 32U); + + *p++ = SYS_CANARY; + while (p < end) { + *p++ = SYS_FILL; + } + tx_thread_stack_error_notify(stack_error); +} + +static ULONG free_bytes(const ULONG *p, const ULONG *end, ULONG fill) +{ + const ULONG *start = p; + + while (p < end && *p == fill) { + p++; + } + return (ULONG)(p - start) * sizeof(ULONG); +} + +void test_stack_report(void) +{ + TX_THREAD *thread_ptr = _tx_thread_created_ptr; + + if (*(const ULONG *)test_sys_stack_bottom() != SYS_CANARY) { + printf("stack FAIL: system stack canary overwritten\n"); + board_exit(CANARY_EXIT); + } + + for (ULONG i = 0; i < _tx_thread_created_count; i++) { + printf("stack: %-10s %5lu of %5lu bytes free\n", thread_ptr->tx_thread_name, + free_bytes(thread_ptr->tx_thread_stack_start, thread_ptr->tx_thread_stack_end, + TX_STACK_FILL), + thread_ptr->tx_thread_stack_size); + thread_ptr = thread_ptr->tx_thread_created_next; + } + printf("stack: %-10s %5lu of %5lu bytes free\n", "system", + free_bytes((const ULONG *)test_sys_stack_bottom() + 1, (const ULONG *)test_sys_stack_top(), + SYS_FILL), + (ULONG)(uintptr_t)stack_size); +} diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/test_common.h b/ports/rh850/gnu/example_build/qemu_rh850_u2c/test_common.h new file mode 100644 index 000000000..880185100 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/test_common.h @@ -0,0 +1,26 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef TEST_COMMON_H +#define TEST_COMMON_H + +#include + +#define TEST_PRIO_HIGH 3U +#define TEST_PRIO_MID 8U + +void test_stack_init(void); +void test_stack_report(void); +uintptr_t test_sys_stack_bottom(void); +uintptr_t test_sys_stack_top(void); +uintptr_t test_sp(void); +void test_spin(unsigned int n); + +#endif diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/test_exit.c b/ports/rh850/gnu/example_build/qemu_rh850_u2c/test_exit.c new file mode 100644 index 000000000..d5e7415fb --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/test_exit.c @@ -0,0 +1,36 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include "rh850_cpu.h" +#include "rh850_io.h" +#include "test_exit.h" + +/* Status in bits 15:0, exit code in bits 31:16 */ +#define TEST_EXIT_FAIL 0x3333 +#define TEST_EXIT_PASS 0x5555 + +static void __attribute__((noreturn)) test_exit_write(uintptr_t base, uint32_t v) +{ + rh850_write32(base, v); + for (;;) { + rh850_di(); + rh850_halt(); + } +} + +void test_exit_pass(uintptr_t base) +{ + test_exit_write(base, TEST_EXIT_PASS); +} + +void test_exit_fail(uintptr_t base, uint16_t code) +{ + test_exit_write(base, ((uint32_t)code << 16) | TEST_EXIT_FAIL); +} diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/test_exit.h b/ports/rh850/gnu/example_build/qemu_rh850_u2c/test_exit.h new file mode 100644 index 000000000..d362139a3 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/test_exit.h @@ -0,0 +1,20 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef RH850_TEST_EXIT_H +#define RH850_TEST_EXIT_H + +#include + +/* Test device of QEMU rh850-u2c (test-exit=on), not a U2C peripheral */ +void test_exit_pass(uintptr_t base) __attribute__((noreturn)); +void test_exit_fail(uintptr_t base, uint16_t code) __attribute__((noreturn)); + +#endif diff --git a/ports/rh850/gnu/example_build/qemu_rh850_u2c/vectors.S b/ports/rh850/gnu/example_build/qemu_rh850_u2c/vectors.S new file mode 100644 index 000000000..43ae07ae1 --- /dev/null +++ b/ports/rh850/gnu/example_build/qemu_rh850_u2c/vectors.S @@ -0,0 +1,38 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + + .macro vector target + .balign 16 + jr \target + .endm + + .section .vectors, "ax" + .balign 512 + .global rh850_vectors +rh850_vectors: + vector __start /* 000H RESET */ + vector rh850_fe_fault /* 010H SYSERR */ + vector rh850_fe_fault /* 020H reserved */ + vector rh850_fe_fault /* 030H FETRAP */ + vector rh850_trap0_entry /* 040H TRAP0 */ + vector rh850_ei_fault /* 050H TRAP1 */ + vector rh850_fe_fault /* 060H RIE */ + vector rh850_ei_fault /* 070H FPE, FPI */ + vector rh850_fe_fault /* 080H UCPOP */ + vector rh850_fe_fault /* 090H MIP, MDP */ + vector rh850_fe_fault /* 0A0H PIE */ + vector rh850_fe_fault /* 0B0H reserved */ + vector rh850_fe_fault /* 0C0H MAE */ + vector rh850_fe_fault /* 0D0H reserved */ + vector rh850_fe_fault /* 0E0H FENMI */ + vector rh850_fe_fault /* 0F0H FEINT */ + .rept 16 + vector rh850_eiint_entry /* 100H-1F0H EIINT priority 0-15 */ + .endr From eab4db70bdbd9a90bc3144864c678e88cbfe9600 Mon Sep 17 00:00:00 2001 From: Parthiban Nallathambi Date: Thu, 5 Nov 2026 14:35:08 +0530 Subject: [PATCH 3/5] Added RH850 regression test build Builds test/tx/regression with the configurations of test/tx/cmake/riscv. Each test runs once, a failure is not retried. Tested on QEMU rh850-u2c, regression 576/576 hard and soft without retries. Signed-off-by: Parthiban Nallathambi --- test/tx/cmake/rh850/CMakeLists.txt | 86 ++++++++++ test/tx/cmake/rh850/bsp/CMakeLists.txt | 18 ++ test/tx/cmake/rh850/bsp/rh850_memory.h | 20 +++ test/tx/cmake/rh850/bsp/rh850_regression.c | 55 ++++++ test/tx/cmake/rh850/bsp/tx_user.h | 17 ++ test/tx/cmake/rh850/regression/CMakeLists.txt | 158 ++++++++++++++++++ test/tx/cmake/rh850/run.sh | 135 +++++++++++++++ 7 files changed, 489 insertions(+) create mode 100644 test/tx/cmake/rh850/CMakeLists.txt create mode 100644 test/tx/cmake/rh850/bsp/CMakeLists.txt create mode 100644 test/tx/cmake/rh850/bsp/rh850_memory.h create mode 100644 test/tx/cmake/rh850/bsp/rh850_regression.c create mode 100644 test/tx/cmake/rh850/bsp/tx_user.h create mode 100644 test/tx/cmake/rh850/regression/CMakeLists.txt create mode 100755 test/tx/cmake/rh850/run.sh diff --git a/test/tx/cmake/rh850/CMakeLists.txt b/test/tx/cmake/rh850/CMakeLists.txt new file mode 100644 index 000000000..d9f467644 --- /dev/null +++ b/test/tx/cmake/rh850/CMakeLists.txt @@ -0,0 +1,86 @@ +# Copyright (c) 2026 Eclipse ThreadX contributors +# SPDX-License-Identifier: MIT + +cmake_minimum_required(VERSION 3.13 FATAL_ERROR) +cmake_policy(SET CMP0054 NEW) +cmake_policy(SET CMP0057 NEW) + +project(threadx_rh850_test LANGUAGES C ASM) + +set(BUILD_CONFIGURATIONS default_build optimized_build disable_notify_callbacks_build + stack_checking_build stack_checking_rand_fill_build trace_build) +set(CMAKE_CONFIGURATION_TYPES + ${BUILD_CONFIGURATIONS} + CACHE STRING "list of supported configuration types" FORCE) +set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS + ${CMAKE_CONFIGURATION_TYPES}) +list(GET CMAKE_CONFIGURATION_TYPES 0 BUILD_TYPE) +if((NOT CMAKE_BUILD_TYPE) OR (NOT ("${CMAKE_BUILD_TYPE}" IN_LIST + CMAKE_CONFIGURATION_TYPES))) + set(CMAKE_BUILD_TYPE + "${BUILD_TYPE}" + CACHE STRING "Build Type of the project" FORCE) +endif() + +message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") +message(STATUS "Using toolchain file: ${CMAKE_TOOLCHAIN_FILE}.") +message(STATUS "THREADX_ARCH: ${THREADX_ARCH}") +message(STATUS "RH850_FLOAT: ${RH850_FLOAT}") + +set(default_build -DTX_QUEUE_MESSAGE_MAX_SIZE=32) +set(optimized_build -DTX_QUEUE_MESSAGE_MAX_SIZE=32) +set(disable_notify_callbacks_build -DTX_QUEUE_MESSAGE_MAX_SIZE=32 -DTX_DISABLE_NOTIFY_CALLBACKS) +set(stack_checking_build -DTX_QUEUE_MESSAGE_MAX_SIZE=32 -DTX_ENABLE_STACK_CHECKING) +set(stack_checking_rand_fill_build -DTX_QUEUE_MESSAGE_MAX_SIZE=32 -DTX_ENABLE_STACK_CHECKING -DTX_ENABLE_RANDOM_NUMBER_STACK_FILLING) +set(trace_build -DTX_QUEUE_MESSAGE_MAX_SIZE=32 -DTX_ENABLE_EVENT_TRACE) + +if(CMAKE_BUILD_TYPE STREQUAL "optimized_build") + set(REGRESSION_OPTIMIZATION -O2) +else() + set(REGRESSION_OPTIMIZATION -O0) +endif() + +add_compile_options( + -std=c99 + ${REGRESSION_OPTIMIZATION} + -g3 + -fdiagnostics-color + -Werror + -ffunction-sections + -fdata-sections + -DTX_REGRESSION_TEST + -DTEST_STACK_SIZE_PRINTF=4096 + -DEXTERNAL_EXIT + -DTX_TIMER_TICKS_PER_SECOND=100UL + ${${CMAKE_BUILD_TYPE}} +) + +enable_testing() + +set(TX_USER_FILE ${CMAKE_CURRENT_LIST_DIR}/bsp/tx_user.h) +add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../../../.. threadx) + +include(${CMAKE_CURRENT_LIST_DIR}/../../../../ports/rh850/gnu/example_build/qemu_rh850_u2c/board_sources.cmake) +add_subdirectory(bsp) +add_subdirectory(regression) + +target_compile_options( + threadx + PRIVATE -Werror + -Wall + -Wextra + -pedantic + -fmessage-length=0 + -fsigned-char + -ffunction-sections + -fdata-sections + -Wunused + -Wuninitialized + -Wmissing-declarations + -Wconversion + -Wpointer-arith + -Wlogical-op + -Waggregate-return + -Wfloat-equal + $<$:-include$stdlib.h> +) diff --git a/test/tx/cmake/rh850/bsp/CMakeLists.txt b/test/tx/cmake/rh850/bsp/CMakeLists.txt new file mode 100644 index 000000000..4dc503752 --- /dev/null +++ b/test/tx/cmake/rh850/bsp/CMakeLists.txt @@ -0,0 +1,18 @@ +# Copyright (c) 2026 Eclipse ThreadX contributors +# SPDX-License-Identifier: MIT + +cmake_minimum_required(VERSION 3.13 FATAL_ERROR) + +project(rh850_bsp LANGUAGES C ASM) + +set(BSP_DIR ${CMAKE_CURRENT_LIST_DIR}) + +add_library(rh850_bsp + ${RH850_U2C_SOURCES} + ${BSP_DIR}/rh850_regression.c +) + +target_include_directories(rh850_bsp PUBLIC ${BSP_DIR} ${RH850_U2C_DIR}) +target_compile_definitions(rh850_bsp PRIVATE ${RH850_U2C_DEFINITIONS}) +target_link_libraries(rh850_bsp PUBLIC azrtos::threadx) +target_compile_definitions(rh850_bsp PUBLIC EXTERNAL_EXIT) diff --git a/test/tx/cmake/rh850/bsp/rh850_memory.h b/test/tx/cmake/rh850/bsp/rh850_memory.h new file mode 100644 index 000000000..fbe0e01e5 --- /dev/null +++ b/test/tx/cmake/rh850/bsp/rh850_memory.h @@ -0,0 +1,20 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +/* The two 384 KB blocks of testcontrol.c go to cluster RAM; local RAM is 128 KB */ +#ifndef RH850_MEMORY_H +#define RH850_MEMORY_H + +#include "tx_api.h" + +extern UCHAR test_control_memory[] __attribute__((section(".bss.rh850_large.test_control_memory"))); +extern UCHAR tests_memory[] __attribute__((section(".bss.rh850_large.tests_memory"))); + +#endif diff --git a/test/tx/cmake/rh850/bsp/rh850_regression.c b/test/tx/cmake/rh850/bsp/rh850_regression.c new file mode 100644 index 000000000..05a8929c0 --- /dev/null +++ b/test/tx/cmake/rh850/bsp/rh850_regression.c @@ -0,0 +1,55 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#include +#include +#include "board.h" +#include "rh850_board.h" +#include "rh850_cpu.h" +#include "rh850_ostm.h" + +void external_exit(unsigned int code); +/* ld applies --wrap to the C name; the "_" prefix of GCC v850 follows */ +void __real_main(void); +int __wrap_main(void); + +void external_exit(unsigned int code) +{ + board_exit((int)code); +} + +time_t time(time_t *timer) +{ + static uint32_t last, high; + uint32_t psw = rh850_irq_save(); + uint32_t now = rh850_ostm_count(BOARD_TIMER2_OSTM_BASE); + + if (now < last) { + high++; + } + last = now; + uint64_t clocks = ((uint64_t)high << 32) | now; + rh850_irq_restore(psw); + + time_t seconds = (time_t)(clocks / BOARD_TIMER_CLK_HZ); + + if (timer != NULL) { + *timer = seconds; + } + return seconds; +} + +int __wrap_main(void) +{ + board_init(); + rh850_ostm_start_freerun(BOARD_TIMER2_OSTM_BASE, false); + __real_main(); + return 1; +} diff --git a/test/tx/cmake/rh850/bsp/tx_user.h b/test/tx/cmake/rh850/bsp/tx_user.h new file mode 100644 index 000000000..f6e96a88a --- /dev/null +++ b/test/tx/cmake/rh850/bsp/tx_user.h @@ -0,0 +1,17 @@ +/*************************************************************************** + * Copyright (c) 2026 Eclipse ThreadX contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef TX_USER_H +#define TX_USER_H + +/* testcontrol.c uses its own test_control_memory */ +#define TX_PORT_FREE_MEMORY_SIZE 64U + +#endif diff --git a/test/tx/cmake/rh850/regression/CMakeLists.txt b/test/tx/cmake/rh850/regression/CMakeLists.txt new file mode 100644 index 000000000..097aa230f --- /dev/null +++ b/test/tx/cmake/rh850/regression/CMakeLists.txt @@ -0,0 +1,158 @@ +# Copyright (c) 2026 Eclipse ThreadX contributors +# SPDX-License-Identifier: MIT + +cmake_minimum_required(VERSION 3.13 FATAL_ERROR) +cmake_policy(SET CMP0057 NEW) + +project(rh850_regression_test LANGUAGES C ASM) + +set(SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/../../../regression) +set(BSP_DIR ${CMAKE_CURRENT_LIST_DIR}/../bsp) + +set(regression_test_cases + ${SOURCE_DIR}/threadx_block_memory_basic_test.c + ${SOURCE_DIR}/threadx_block_memory_error_detection_test.c + ${SOURCE_DIR}/threadx_block_memory_information_test.c + ${SOURCE_DIR}/threadx_block_memory_prioritize_test.c + ${SOURCE_DIR}/threadx_block_memory_suspension_test.c + ${SOURCE_DIR}/threadx_block_memory_suspension_timeout_test.c + ${SOURCE_DIR}/threadx_block_memory_thread_terminate_test.c + ${SOURCE_DIR}/threadx_byte_memory_basic_test.c + ${SOURCE_DIR}/threadx_byte_memory_information_test.c + ${SOURCE_DIR}/threadx_byte_memory_prioritize_test.c + ${SOURCE_DIR}/threadx_byte_memory_suspension_test.c + ${SOURCE_DIR}/threadx_byte_memory_suspension_timeout_test.c + ${SOURCE_DIR}/threadx_byte_memory_thread_contention_test.c + ${SOURCE_DIR}/threadx_byte_memory_thread_terminate_test.c + ${SOURCE_DIR}/threadx_event_flag_basic_test.c + ${SOURCE_DIR}/threadx_event_flag_information_test.c + ${SOURCE_DIR}/threadx_event_flag_isr_set_clear_test.c + ${SOURCE_DIR}/threadx_event_flag_isr_wait_abort_test.c + ${SOURCE_DIR}/threadx_event_flag_single_thread_terminate_test.c + ${SOURCE_DIR}/threadx_event_flag_suspension_consume_test.c + ${SOURCE_DIR}/threadx_event_flag_suspension_different_bits_consume_test.c + ${SOURCE_DIR}/threadx_event_flag_suspension_different_bits_test.c + ${SOURCE_DIR}/threadx_event_flag_suspension_test.c + ${SOURCE_DIR}/threadx_event_flag_suspension_timeout_test.c + ${SOURCE_DIR}/threadx_event_flag_thread_terminate_test.c + ${SOURCE_DIR}/threadx_interrupt_control_test.c + ${SOURCE_DIR}/threadx_mutex_basic_test.c + ${SOURCE_DIR}/threadx_mutex_delete_test.c + ${SOURCE_DIR}/threadx_mutex_information_test.c + ${SOURCE_DIR}/threadx_mutex_nested_priority_inheritance_test.c + ${SOURCE_DIR}/threadx_mutex_no_preemption_test.c + ${SOURCE_DIR}/threadx_mutex_preemption_test.c + ${SOURCE_DIR}/threadx_mutex_priority_inheritance_test.c + ${SOURCE_DIR}/threadx_mutex_proritize_test.c + ${SOURCE_DIR}/threadx_mutex_suspension_timeout_test.c + ${SOURCE_DIR}/threadx_mutex_thread_terminate_test.c + ${SOURCE_DIR}/threadx_queue_basic_eight_word_test.c + ${SOURCE_DIR}/threadx_queue_basic_four_word_test.c + ${SOURCE_DIR}/threadx_queue_basic_one_word_test.c + ${SOURCE_DIR}/threadx_queue_basic_sixteen_word_test.c + ${SOURCE_DIR}/threadx_queue_basic_two_word_test.c + ${SOURCE_DIR}/threadx_queue_basic_max_message_size_test.c + ${SOURCE_DIR}/threadx_queue_empty_suspension_test.c + ${SOURCE_DIR}/threadx_queue_flush_no_suspension_test.c + ${SOURCE_DIR}/threadx_queue_flush_test.c + ${SOURCE_DIR}/threadx_queue_front_send_test.c + ${SOURCE_DIR}/threadx_queue_full_suspension_test.c + ${SOURCE_DIR}/threadx_queue_information_test.c + ${SOURCE_DIR}/threadx_queue_prioritize.c + ${SOURCE_DIR}/threadx_queue_suspension_timeout_test.c + ${SOURCE_DIR}/threadx_queue_thread_terminate_test.c + ${SOURCE_DIR}/threadx_semaphore_basic_test.c + ${SOURCE_DIR}/threadx_semaphore_ceiling_put_test.c + ${SOURCE_DIR}/threadx_semaphore_delete_test.c + ${SOURCE_DIR}/threadx_semaphore_information_test.c + ${SOURCE_DIR}/threadx_semaphore_non_preemption_test.c + ${SOURCE_DIR}/threadx_semaphore_preemption_test.c + ${SOURCE_DIR}/threadx_semaphore_prioritize.c + ${SOURCE_DIR}/threadx_semaphore_thread_terminate_test.c + ${SOURCE_DIR}/threadx_semaphore_timeout_test.c + ${SOURCE_DIR}/threadx_thread_basic_execution_test.c + ${SOURCE_DIR}/threadx_thread_basic_time_slice_test.c + ${SOURCE_DIR}/threadx_thread_completed_test.c + ${SOURCE_DIR}/threadx_thread_create_preemption_threshold_test.c + ${SOURCE_DIR}/threadx_thread_delayed_suspension_test.c + ${SOURCE_DIR}/threadx_thread_information_test.c + ${SOURCE_DIR}/threadx_thread_multi_level_preemption_threshold_test.c + ${SOURCE_DIR}/threadx_thread_multiple_non_current_test.c + ${SOURCE_DIR}/threadx_thread_multiple_sleep_test.c + ${SOURCE_DIR}/threadx_thread_multiple_suspension_test.c + ${SOURCE_DIR}/threadx_thread_multiple_time_slice_test.c + ${SOURCE_DIR}/threadx_thread_preemptable_suspension_test.c + ${SOURCE_DIR}/threadx_thread_preemption_change_test.c + ${SOURCE_DIR}/threadx_thread_priority_change.c + ${SOURCE_DIR}/threadx_thread_relinquish_test.c + ${SOURCE_DIR}/threadx_thread_reset_test.c + ${SOURCE_DIR}/threadx_thread_simple_sleep_non_clear_test.c + ${SOURCE_DIR}/threadx_thread_simple_sleep_test.c + ${SOURCE_DIR}/threadx_thread_simple_suspend_test.c + ${SOURCE_DIR}/threadx_thread_sleep_for_100ticks_test.c + ${SOURCE_DIR}/threadx_thread_sleep_terminate_test.c + ${SOURCE_DIR}/threadx_thread_stack_checking_test.c + ${SOURCE_DIR}/threadx_thread_terminate_delete_test.c + ${SOURCE_DIR}/threadx_thread_time_slice_change_test.c + ${SOURCE_DIR}/threadx_thread_wait_abort_and_isr_test.c + ${SOURCE_DIR}/threadx_thread_wait_abort_test.c + ${SOURCE_DIR}/threadx_time_get_set_test.c + ${SOURCE_DIR}/threadx_timer_activate_deactivate_test.c + ${SOURCE_DIR}/threadx_timer_deactivate_accuracy_test.c + ${SOURCE_DIR}/threadx_timer_information_test.c + ${SOURCE_DIR}/threadx_timer_large_timer_accuracy_test.c + ${SOURCE_DIR}/threadx_timer_multiple_accuracy_test.c + ${SOURCE_DIR}/threadx_timer_multiple_test.c + ${SOURCE_DIR}/threadx_timer_simple_test.c + ${SOURCE_DIR}/threadx_trace_basic_test.c +) + +set(standalone_test_cases + ${SOURCE_DIR}/threadx_initialize_kernel_setup_test.c +) + +set(WEAK_DEFAULTS_SOURCE ${CMAKE_CURRENT_LIST_DIR}/../../../../shared/regression/testcontrol_weak_defaults.c) + +add_library(rh850_test_weak_defaults OBJECT ${WEAK_DEFAULTS_SOURCE}) +target_compile_definitions(rh850_test_weak_defaults PRIVATE + CTEST BATCH_TEST EXTERNAL_EXIT TEST_STACK_SIZE_PRINTF=4096) +target_compile_options(rh850_test_weak_defaults PRIVATE -include stdlib.h) +target_link_libraries(rh850_test_weak_defaults PRIVATE azrtos::threadx) + +add_library(rh850_test_utility ${SOURCE_DIR}/testcontrol.c) +target_link_libraries(rh850_test_utility PUBLIC rh850_bsp azrtos::threadx) +target_compile_definitions(rh850_test_utility PUBLIC + CTEST BATCH_TEST EXTERNAL_EXIT TEST_STACK_SIZE_PRINTF=4096) +target_compile_options(rh850_test_utility PUBLIC -include stdlib.h) +target_compile_options(rh850_test_utility PRIVATE "SHELL:-include ${BSP_DIR}/rh850_memory.h") + +find_program(RH850_QEMU NAMES qemu-system-rh850 REQUIRED) + +function(register_rh850_test test_name) + target_link_options(${test_name} PRIVATE ${RH850_U2C_LINK_OPTIONS} -Wl,--wrap=main) + add_test( + NAME ${CMAKE_BUILD_TYPE}::${RH850_FLOAT}::${test_name} + COMMAND ${RH850_QEMU} ${RH850_U2C_QEMU_ARGS} -kernel $ + ) + set_tests_properties(${CMAKE_BUILD_TYPE}::${RH850_FLOAT}::${test_name} + PROPERTIES TIMEOUT 120) +endfunction() + +foreach(test_case ${regression_test_cases}) + get_filename_component(test_name ${test_case} NAME_WE) + add_executable(${test_name} ${test_case} $) + target_link_libraries(${test_name} PRIVATE + -Wl,--whole-archive rh850_bsp -Wl,--no-whole-archive + rh850_test_utility) + register_rh850_test(${test_name}) +endforeach() + +foreach(test_case ${standalone_test_cases}) + get_filename_component(test_name ${test_case} NAME_WE) + add_executable(${test_name} ${test_case} $) + target_link_libraries(${test_name} PRIVATE + -Wl,--whole-archive rh850_bsp -Wl,--no-whole-archive + azrtos::threadx) + target_compile_options(${test_name} PRIVATE -include stdlib.h) + register_rh850_test(${test_name}) +endforeach() diff --git a/test/tx/cmake/rh850/run.sh b/test/tx/cmake/rh850/run.sh new file mode 100755 index 000000000..1a2a30723 --- /dev/null +++ b/test/tx/cmake/rh850/run.sh @@ -0,0 +1,135 @@ +#!/bin/bash +# Copyright (c) 2026 Eclipse ThreadX contributors +# SPDX-License-Identifier: MIT + +set -e + +function help() { + echo "Usage: $0 [all| ...]" + echo "" + echo " float: hard | soft" + echo " command: build | test" + echo "" + echo "Available build configurations:" + for build in ${build_configurations[*]}; do + echo " $build" + done + exit 1 +} + +function validate() { + for build in ${build_configurations[*]}; do + if [ "$1" == "$build" ]; then + return + fi + done + help +} + +function generate() { + local float=$1 + local build=$2 + + cmake -B"build/${float}_${build}" -GNinja \ + -DCMAKE_TOOLCHAIN_FILE="${REPO_ROOT}/cmake/rh850_gnu.cmake" \ + -DRH850_FLOAT="$float" \ + -DCMAKE_BUILD_TYPE="$build" \ + . +} + +function build() { + cmake --build "build/$1_$2" -- -k 0 +} + +function run_test() { + local float=$1 + local build=$2 + local parallel_jobs=$3 + + pushd "build/${float}_${build}" || return 1 + [ -z "${CTEST_PARALLEL_LEVEL}" ] && parallel="-j${parallel_jobs}" + local status=0 + ctest $parallel --timeout 1000 \ + -O "${float}_${build}.txt" \ + -T test --no-compress-output \ + --test-output-size-passed 4194304 \ + --test-output-size-failed 4194304 \ + --output-on-failure \ + --output-junit "${float}_${build}.xml" || status=$? + popd + return $status +} + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +REPO_ROOT="$(cd "${SCRIPT_DIR}/../../../.." && pwd)" +cd "$SCRIPT_DIR" + +result=$(sed -n "/(BUILD_CONFIGURATIONS/,/)/p" CMakeLists.txt | sed ':label;N;s/\n/ /;b label' | grep -Pzo "[a-zA-Z0-9_]*build[a-zA-Z0-9_]*\s*" | tr -d '\0') +IFS=' ' +read -ra build_configurations <<< "$result" + +if [ $# -lt 2 ]; then + help +fi + +float=$1 +shift +if [ "$float" != "hard" ] && [ "$float" != "soft" ]; then + echo "Error: float must be 'hard' or 'soft'" + help +fi + +command=$1 +shift + +if [ "$#" == "0" ]; then + builds=${build_configurations[0]} +elif [ "$*" == "all" ]; then + builds=${build_configurations[@]} +else + for item in $*; do + validate "$item" + done + builds=$* +fi + +if [ "$command" == "build" ]; then + for item in $builds; do + generate "$float" "$item" + echo "" + done + build_status=0 + for item in $builds; do + echo "Building ${float} ${item}" + build "$float" "$item" || build_status=$? + echo "" + done + [ $build_status -eq 0 ] || exit $build_status +elif [ "$command" == "test" ]; then + cores=$(nproc) + if [ -z "${CTEST_PARALLEL_LEVEL}" ]; then + build_counts=$(echo $builds | wc -w) + parallel_jobs=$(($cores / $build_counts)) + parallel_jobs=$(($parallel_jobs + 2)) + pids="" + for item in $builds; do + echo "Testing ${float} ${item}" + run_test "$float" "$item" "$parallel_jobs" & + pids+=" $!" + done + exit_code=0 + for p in $pids; do + wait $p || exit_code=$? + done + exit $exit_code + else + exit_code=0 + for item in $builds; do + echo "Testing ${float} ${item}" + run_test "$float" "$item" "$parallel_jobs" || exit_code=$? + done + exit $exit_code + fi +else + help +fi From 1b3c598985e4ca1ea35da31d5ac23f1974259891 Mon Sep 17 00:00:00 2001 From: Parthiban Nallathambi Date: Thu, 5 Nov 2026 14:35:08 +0530 Subject: [PATCH 4/5] Added the RH850 port readme Adds the build and QEMU run steps of the RH850 GNU port. Tested on QEMU rh850-u2c, the readme steps pass hard and soft. Signed-off-by: Parthiban Nallathambi --- ports/rh850/gnu/readme_threadx.txt | 170 +++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 ports/rh850/gnu/readme_threadx.txt diff --git a/ports/rh850/gnu/readme_threadx.txt b/ports/rh850/gnu/readme_threadx.txt new file mode 100644 index 000000000..658391b1e --- /dev/null +++ b/ports/rh850/gnu/readme_threadx.txt @@ -0,0 +1,170 @@ + Eclipse Foundation's RTOS, ThreadX for Renesas RH850 + + Using the GNU Tools + + +1. Building the ThreadX run-time Library + +The port is for the RH850 G4KH core (RH850/U2C), RH850 ABI, with or without +the FPU. It needs the GNU v850-elf toolchain with picolibc, release v1.2.0 +(GCC 14.3.0, binutils 2.43.1, picolibc 1.8.12): + + https://github.com/linumiz/rh850-gcc-toolchain/releases/tag/v1.2.0 + +with its bin directory on the PATH: + + v850-elf-gcc --version + +From the ThreadX top-level directory: + + cmake -Bbuild -GNinja -DCMAKE_TOOLCHAIN_FILE=cmake/rh850_gnu.cmake . + cmake --build ./build/ + +cmake/rh850_gnu.cmake sets the core flags (-mv850e3v5 -mrh850-abi) and +includes cmake/v850-elf.cmake for the tools. The result is build/libthreadx.a. + + +2. Build options + +RH850_FLOAT=hard (default) FPU code; FPSR and FPEPC are saved in every + thread frame and in every interrupt frame +RH850_FLOAT=soft -msoft-float; no FPU use, no FPU context +TX_RH850_LOW_POWER=ON tickless idle through utility/low_power + (compile definition TX_LOW_POWER) +TX_RH850_MAX_IPL=n TX_PORT_MAX_IPL for the library and the code + that links it +RH850_EXTRA_FLAGS="..." more C and assembler flags, for example -Werror +TX_RH850_BUILD_QEMU_EXAMPLE=ON + the QEMU example in example_build + +In tx_user.h (or with -D): + +TX_PORT_MAX_IPL default 0: TX_DISABLE masks every EIINT. With + n > 0 the priority numbers 0 to n-1 stay + unmasked inside ThreadX; their ISRs must not call + any ThreadX service. TX_DISABLE keeps a stricter + PLMR of the caller +TX_PORT_FREE_MEMORY_SIZE default 8192: size of tx_port_free_memory, the + block passed to tx_application_define +TX_TIMER_TICKS_PER_SECOND default 1000 + + +3. Example build and tests + +ports/rh850/gnu/example_build/qemu_rh850_u2c runs samples/demo_threadx.c, +unmodified, with a monitor thread on the rh850-u2c machine of +qemu-system-rh850, release rh850-v1.2.0: + + https://github.com/linumiz/qemu-rh850/releases/tag/rh850-v1.2.0 + +The monitor checks every sample counter each second for 10 s and ends the run +through the QEMU test device: + + cmake -Bbuild -GNinja -DCMAKE_TOOLCHAIN_FILE=cmake/rh850_gnu.cmake \ + -DCMAKE_BUILD_TYPE=Release -DTX_RH850_BUILD_QEMU_EXAMPLE=ON . + cmake --build ./build/ + ctest --test-dir build --output-on-failure + +or by hand: + + qemu-system-rh850 -M rh850-u2c,test-exit=on -icount shift=4,sleep=on \ + -nographic -kernel build/ports/rh850/gnu/example_build/qemu_rh850_u2c/demo_threadx.elf + +ctest also runs two port tests. In mask_posture a thread sets PLMR to 0 and +calls ThreadX services; with -DTX_RH850_MAX_IPL=4 a pending priority 3 +interrupt must stay held. In fpu_switch, hard float only, two threads with +their own FPSR rounding mode and FPEPC are switched by time slice, relinquish +and an ISR, and check both after every resumption. + +The board files (start-up, vectors, INTC, OSTM, RLIN3 console, test device, +linker script) are in the same directory; board_sources.cmake lists them for +the example and for the regression tests. + +test/tx/cmake/rh850 builds the generic regression suite (test/tx/regression) +on the same board, with the test list and the six build configurations of +test/tx/cmake/riscv: + + test/tx/cmake/rh850/run.sh hard build all + test/tx/cmake/rh850/run.sh hard test all + + +4. System Initialization + +The board's crt0.S starts at the reset vector (address 0), sets up the reset +stack, copies .data, clears .bss and calls main, which calls tx_kernel_enter. +_tx_initialize_low_level runs with PSW.ID = 1: the reset stack becomes the +system stack, and tx_application_define gets tx_port_free_memory, a block of +its own apart from the system stack. The board starts the tick in +tx_port_setup_timer_interrupt: OSTM0 at TX_TIMER_TICKS_PER_SECOND, at the +lowest interrupt priority. + + +5. Register Usage and Stack Frames + +A suspended thread always has one 144-byte frame on its stack, whether it was +switched by a ThreadX call or by an interrupt: + + Offset Register + 0x00 PLMR + 0x04 FPEPC (hard float) + 0x08 FPSR (hard float) + 0x0C EIPSW + 0x10 EIPC + 0x14 r1 + 0x18 r2 + 0x1C - 0x8C r4 - r31 + +_tx_thread_system_return saves the frame and switches through TRAP0 (with a +SYNCE first when the FPU is used). A new thread starts from a frame made by +_tx_thread_stack_build, with PSW.ID = 0, PLMR open and, with the FPU, PSW.CU0 +and FPSR.FS set. + + +6. Interrupt Handling + +Every EIINT goes through rh850_eiint_entry (direct vectors). EIPC, EIPSW and +EIIC are saved before interrupts are enabled again. The outermost level saves +the full frame on the interrupted stack and moves to the system stack; a +nested level saves a 96-byte call-used frame on the system stack. The board +dispatches by EIIC with interrupts enabled; the ISPR ceiling of the INTC holds +off equal and lower priorities until EIRET. Threads are switched only at the +outermost exit, by an EIRET into the scheduler. + +TX_DISABLE, TX_RESTORE and tx_interrupt_control use the PLMR core mask +register, not PSW.ID; the interrupt posture is the old PLMR value. A thread is +never switched out while its saved PLMR is masked. + + +7. Floating Point + +With RH850_FLOAT=hard, FPSR and FPEPC are part of every thread frame and of +the nested interrupt frame, and new threads start with the FPU enabled. A +thread with a pending imprecise FPU exception (FPEC.FPIVD) is not switched out +at the interrupt exit. While it is pending, both restore paths keep FPEPC and +the FPSR cause bits for that exception. RH850_FLOAT_HARD (0 or 1) is set from +RH850_FLOAT by ports/rh850/gnu/CMakeLists.txt; the library and the application +must use the same value. + + +8. Idle and Low Power + +The scheduler idles in HALT with PSW.ID = 0 and PLMR open. With TX_LOW_POWER +it calls tx_low_power_enter, halts with PSW.ID = 1 (an interrupt request still +ends HALT), calls tx_low_power_exit and then takes the request. The board +provides tx_port_low_power_timer_setup and tx_port_low_power_adjust; the +example board runs the tick on the OSTM0 free-running compare and keeps the +rest of a period in the counter. + + +9. Validation + +The regression suite in test/tx/cmake/rh850 passes in all six configurations, +with hard and soft float, under qemu-system-rh850 rh850-u2c. The library builds +with the strict warning flags of the regression build. + + +10. Revision History + +For generic code revision information, refer to readme_threadx_generic.txt. + +11-05-2026 Parthiban Nallathambi Initial RH850 G4KH GNU port From 04da0b014ae127b30dc425f9af11127c911bdab9 Mon Sep 17 00:00:00 2001 From: Parthiban Nallathambi Date: Wed, 18 Nov 2026 01:58:07 +0530 Subject: [PATCH 5/5] Added an RH850 QEMU workflow Builds the RH850 port hard and soft with -Werror and runs the example, the example with TX_RH850_MAX_IPL=4 and the regression tests on qemu-system-rh850, also for kernel changes. Tool archives are checked with SHA-256. Tested locally, the workflow steps pass. actionlint and shellcheck report nothing. Signed-off-by: Parthiban Nallathambi --- .github/workflows/rh850_qemu.yml | 110 +++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 .github/workflows/rh850_qemu.yml diff --git a/.github/workflows/rh850_qemu.yml b/.github/workflows/rh850_qemu.yml new file mode 100644 index 000000000..21ecffe1a --- /dev/null +++ b/.github/workflows/rh850_qemu.yml @@ -0,0 +1,110 @@ +name: rh850_qemu + +on: + push: + branches: [ master, dev ] + paths: + - ".github/workflows/rh850_qemu.yml" + - "cmake/rh850_gnu.cmake" + - "cmake/v850-elf.cmake" + - "ports/rh850/**" + - "test/tx/cmake/rh850/**" + - "common/**" + - "test/tx/regression/**" + - "scripts/tx_ci_common.sh" + pull_request: + branches: [ master, dev ] + paths: + - ".github/workflows/rh850_qemu.yml" + - "cmake/rh850_gnu.cmake" + - "cmake/v850-elf.cmake" + - "ports/rh850/**" + - "test/tx/cmake/rh850/**" + - "common/**" + - "test/tx/regression/**" + - "scripts/tx_ci_common.sh" + +permissions: + contents: read + +jobs: + rh850_qemu: + runs-on: ubuntu-24.04 + timeout-minutes: 60 + + env: + TOOLCHAIN_URL: https://github.com/linumiz/rh850-gcc-toolchain/releases/download/v1.2.0/v850-elf-toolchain-linux-x86_64.tar.gz + TOOLCHAIN_SHA256: a2194a060295008a2bd67db0e24c1cda8a795eba832b49b3f1d4cbb64cb242bd + QEMU_URL: https://github.com/linumiz/qemu-rh850/releases/download/rh850-v1.2.0/qemu-rh850-linux-x86_64.tar.gz + QEMU_SHA256: 1e1d3d5d3a365ec24047e9128b11df09fdfddb2b385aa4c77dc45253aa448f6b + + steps: + - name: Check out the repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Install the RH850 toolchain and QEMU + run: | + set -eu + . scripts/tx_ci_common.sh + packages="" + command -v ninja >/dev/null 2>&1 || packages="$packages ninja-build" + dpkg -s libglib2.0-0t64 >/dev/null 2>&1 || packages="$packages libglib2.0-0t64" + if [ -n "$packages" ]; then + retry sudo "${TIMEOUT[@]}" apt-get "${APT_OPTIONS[@]}" update || true + # shellcheck disable=SC2086 + retry sudo "${TIMEOUT[@]}" apt-get "${APT_OPTIONS[@]}" install -y --no-install-recommends $packages + fi + mkdir -p tools/v850-elf tools/qemu-rh850 + cd tools + curl -fsSL -o v850-elf.tar.gz "$TOOLCHAIN_URL" + echo "$TOOLCHAIN_SHA256 v850-elf.tar.gz" | sha256sum -c - + tar -xzf v850-elf.tar.gz -C v850-elf + rm -f v850-elf.tar.gz + curl -fsSL -o qemu-rh850.tar.gz "$QEMU_URL" + echo "$QEMU_SHA256 qemu-rh850.tar.gz" | sha256sum -c - + tar -xzf qemu-rh850.tar.gz -C qemu-rh850 + rm -f qemu-rh850.tar.gz + echo "$PWD/v850-elf/bin" >> "$GITHUB_PATH" + echo "$PWD/qemu-rh850/bin" >> "$GITHUB_PATH" + v850-elf/bin/v850-elf-gcc --version | head -1 + qemu-rh850/bin/qemu-system-rh850 --version | head -1 + + - name: Build the library + run: | + set -eu + for float in hard soft; do + cmake -S . -B "build/lib-$float" -G Ninja \ + -DCMAKE_TOOLCHAIN_FILE=cmake/rh850_gnu.cmake -DRH850_FLOAT="$float" \ + -DRH850_EXTRA_FLAGS=-Werror + cmake --build "build/lib-$float" + done + + - name: Run the example under QEMU + run: | + set -eu + for float in hard soft; do + cmake -S . -B "build/example-$float" -G Ninja \ + -DCMAKE_TOOLCHAIN_FILE=cmake/rh850_gnu.cmake -DRH850_FLOAT="$float" \ + -DCMAKE_BUILD_TYPE=Release -DTX_RH850_BUILD_QEMU_EXAMPLE=ON \ + -DRH850_EXTRA_FLAGS=-Werror + cmake --build "build/example-$float" + ctest --test-dir "build/example-$float" --output-on-failure --no-tests=error + done + cmake -S . -B build/example-max-ipl -G Ninja \ + -DCMAKE_TOOLCHAIN_FILE=cmake/rh850_gnu.cmake -DRH850_FLOAT=hard \ + -DCMAKE_BUILD_TYPE=Release -DTX_RH850_BUILD_QEMU_EXAMPLE=ON \ + -DRH850_EXTRA_FLAGS=-Werror -DTX_RH850_MAX_IPL=4 + cmake --build build/example-max-ipl + ctest --test-dir build/example-max-ipl --output-on-failure --no-tests=error + + - name: Build the regression tests + run: | + set -eu + test/tx/cmake/rh850/run.sh hard build all + test/tx/cmake/rh850/run.sh soft build all + + - name: Run the regression tests under QEMU + run: | + set -eu + test/tx/cmake/rh850/run.sh hard test all + test/tx/cmake/rh850/run.sh soft test all