From 64fe5fce1b99ea795f99daad12e4e41cc00b25f8 Mon Sep 17 00:00:00 2001 From: Huaqi Fang <578567190@qq.com> Date: Mon, 16 Dec 2024 17:06:39 +0800 Subject: [PATCH] application: add smode rtthread demo to demostrate how to run Take care this required TEE present and ECLIC present see Makefile Signed-off-by: Huaqi Fang <578567190@qq.com> --- application/rtthread/demo_smode/Makefile | 13 ++ application/rtthread/demo_smode/main.c | 125 ++++++++++++++++ application/rtthread/demo_smode/npk.yml | 48 ++++++ application/rtthread/demo_smode/rtconfig.h | 162 +++++++++++++++++++++ 4 files changed, 348 insertions(+) create mode 100644 application/rtthread/demo_smode/Makefile create mode 100644 application/rtthread/demo_smode/main.c create mode 100644 application/rtthread/demo_smode/npk.yml create mode 100644 application/rtthread/demo_smode/rtconfig.h diff --git a/application/rtthread/demo_smode/Makefile b/application/rtthread/demo_smode/Makefile new file mode 100644 index 00000000..15f54754 --- /dev/null +++ b/application/rtthread/demo_smode/Makefile @@ -0,0 +1,13 @@ +TARGET = rtthread_demo +RTOS = RTThread + +NUCLEI_SDK_ROOT = ../../.. + +COMMON_FLAGS = -O3 -DSMODE_RTOS + +XLCFG_TEE := 1 + +SRCDIRS = . +INCDIRS = . + +include $(NUCLEI_SDK_ROOT)/Build/Makefile.base diff --git a/application/rtthread/demo_smode/main.c b/application/rtthread/demo_smode/main.c new file mode 100644 index 00000000..4238dd76 --- /dev/null +++ b/application/rtthread/demo_smode/main.c @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * Copyright (c) 2019-Present Nuclei Limited. All rights reserved. + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2020-03-26 Huaqi the first version + */ + +#include "nuclei_sdk_soc.h" +#include +#include + +#if !defined(__TEE_PRESENT) || (__TEE_PRESENT != 1) +/* __TEE_PRESENT should be defined in .h */ +#error "This example require CPU TEE feature!" +#endif + +#if !defined(__PMP_PRESENT) || (__PMP_PRESENT != 1) +/* __PMP_PRESENT should be defined in .h */ +#error "This example require CPU PMP feature!" +#endif + +#define THREAD_PRIORITY 2 +#define THREAD_STACK_SIZE 512 +#define THREAD_TIMESLICE 5 +#define THREAD_NUM 5 + +/* Align stack when using static thread */ +ALIGN(RT_ALIGN_SIZE) +static rt_uint8_t thread_stack[THREAD_NUM][THREAD_STACK_SIZE]; +static struct rt_thread tid[THREAD_NUM]; + +/* Thread entry function */ +static void thread_entry(void* parameter) +{ + rt_uint32_t count = 0; + + while (1) { + rt_kprintf("thread %d count: %d\n", (rt_uint32_t)parameter, count++); + rt_thread_mdelay(250); + } +} + +/* Thread demo */ +int create_thread_demo(void) +{ + unsigned long i; + for (i = 0; i < THREAD_NUM; i ++) { + /* Create static threads */ + rt_thread_init(&tid[i], "thread", thread_entry, (void*)i, thread_stack[i], + THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); + } + + /* Startup threads */ + for (i = 0; i < THREAD_NUM; i ++) { + rt_thread_startup(&tid[i]); + } + + return 0; +} + +int main(void) +{ + rt_uint32_t count = 0; + + create_thread_demo(); + + while (1) { + rt_kprintf("Main thread count: %d\n", count++); + rt_thread_mdelay(500); +#ifdef CFG_SIMULATION + if (count > 2) { + // directly exit if in nuclei internally simulation + SIMULATION_EXIT(0); + } +#endif + } +} + +extern void entry(void); + +#define SMODE_STACK_SIZE 2048 +// Execute Hart ID +#define EXECUTE_HARTID 0 + +/* Create a stack for supervisor mode execution */ +uint8_t smode_stack[SMODE_STACK_SIZE] __attribute__((aligned(16))); + +uintptr_t smode_sp = (uintptr_t) (smode_stack + sizeof(smode_stack)); + +int main_entry(void) +{ + CSR_MCFGINFO_Type mcfg; + mcfg.d = __RV_CSR_READ(CSR_MCFG_INFO); + + if ((mcfg.b.tee & mcfg.b.clic) == 0) { + printf("ERROR: TEE and ECLIC feature are required to run this SMode RT-Thread Demo\n"); + return 1; + } + + // set pmp, S mode can access all address range + pmp_config pmp_cfg = { + /* M mode grants S and U mode with full permission of the whole address range */ + .protection = PMP_L | PMP_R | PMP_W | PMP_X, + /* Memory region range 2^__RISCV_XLEN bytes */ + .order = __RISCV_XLEN, + /* Initial base address is 0 */ + .base_addr = 0, + }; + + __set_PMPENTRYx(0, &pmp_cfg); + printf("Set ECLIC Timer Interrupt and Software Timer Interrupt to be executed in S-Mode\r\n"); + // before drop to S Mode, specifies in which privilege mode the interrupt should be taken + ECLIC_SetModeIRQ(SysTimerSW_IRQn, PRV_S); + ECLIC_SetModeIRQ(SysTimer_IRQn, PRV_S); + + printf("Drop to S-Mode to prepare RT-Thread Environment\r\n"); + /* Drop to S mode */ + __switch_mode(PRV_S, smode_sp, entry); + // Should never return here + return 1; +} diff --git a/application/rtthread/demo_smode/npk.yml b/application/rtthread/demo_smode/npk.yml new file mode 100644 index 00000000..4659212b --- /dev/null +++ b/application/rtthread/demo_smode/npk.yml @@ -0,0 +1,48 @@ +## Package Base Information +name: app-nsdk_rtthread_smode_demo +owner: nuclei +version: +description: S-Mode RTThread Task Demo +type: app +keywords: + - rtthread + - task demo +category: rtthread application +license: +homepage: + +## Package Dependency +dependencies: + - name: sdk-nuclei_sdk + version: + - name: osp-nsdk_rtthread + version: + +## Package Configurations +configuration: + app_commonflags: + value: -O3 + type: text + description: Application Compile Flags + +## Set Configuration for other packages +setconfig: + - config: rtthread_msh + value: 0 + +## Source Code Management +codemanage: + copyfiles: + - path: ["*.c", "*.h"] + incdirs: + - path: ["./"] + libdirs: + ldlibs: + - libs: + +## Build Configuration +buildconfig: + - type: common + common_flags: # flags need to be combined together across all packages + # SMODE_RTOS is required, and CFG_HAS_TEE is defined to expect TEE feature present in hw + - flags: -DSMODE_RTOS -DCFG_HAS_TEE ${app_commonflags} diff --git a/application/rtthread/demo_smode/rtconfig.h b/application/rtthread/demo_smode/rtconfig.h new file mode 100644 index 00000000..ad520e91 --- /dev/null +++ b/application/rtthread/demo_smode/rtconfig.h @@ -0,0 +1,162 @@ +/* RT-Thread config file */ + +#ifndef __RTTHREAD_CFG_H__ +#define __RTTHREAD_CFG_H__ + +#include + +#if defined(__CC_ARM) || defined(__CLANG_ARM) +#include "RTE_Components.h" + +#if defined(RTE_USING_FINSH) +#define RT_USING_FINSH +#endif //RTE_USING_FINSH + +#endif //(__CC_ARM) || (__CLANG_ARM) + +// <<< Use Configuration Wizard in Context Menu >>> +// Basic Configuration +// Maximal level of thread priority <8-256> +// Default: 32 +#define RT_THREAD_PRIORITY_MAX 8 +// OS tick per second +// Default: 1000 (1ms) +#define RT_TICK_PER_SECOND 100 +// Alignment size for CPU architecture data access +// Default: 4 +#define RT_ALIGN_SIZE 8 +// the max length of object name<2-16> +// Default: 8 +#define RT_NAME_MAX 8 +// Using RT-Thread components initialization +// Using RT-Thread components initialization +#define RT_USING_COMPONENTS_INIT +// + +#define RT_USING_USER_MAIN + +// the stack size of main thread<1-4086> +// Default: 512 +#define RT_MAIN_THREAD_STACK_SIZE 1024 + +// the stack size of main thread<1-4086> +// Default: 128 +#define IDLE_THREAD_STACK_SIZE 512 + + + +// + +// Debug Configuration +// enable kernel debug configuration +// Default: enable kernel debug configuration +//#define RT_DEBUG +// +// enable components initialization debug configuration<0-1> +// Default: 0 +#define RT_DEBUG_INIT 0 +// thread stack over flow detect +// Diable Thread stack over flow detect +//#define RT_USING_OVERFLOW_CHECK +// +// + +// Hook Configuration +// using hook +// using hook +//#define RT_USING_HOOK +// +// using idle hook +// using idle hook +//#define RT_USING_IDLE_HOOK +// +// + +// Software timers Configuration +// Enables user timers +#define RT_USING_TIMER_SOFT 0 +#if RT_USING_TIMER_SOFT == 0 +#undef RT_USING_TIMER_SOFT +#endif +// The priority level of timer thread <0-31> +// Default: 4 +#define RT_TIMER_THREAD_PRIO 4 +// The stack size of timer thread <0-8192> +// Default: 512 +#define RT_TIMER_THREAD_STACK_SIZE 512 +// + +// IPC(Inter-process communication) Configuration +// Using Semaphore +// Using Semaphore +#define RT_USING_SEMAPHORE +// +// Using Mutex +// Using Mutex +//#define RT_USING_MUTEX +// +// Using Event +// Using Event +//#define RT_USING_EVENT +// +// Using MailBox +// Using MailBox +#define RT_USING_MAILBOX +// +// Using Message Queue +// Using Message Queue +//#define RT_USING_MESSAGEQUEUE +// +// + +// Memory Management Configuration +// Dynamic Heap Management +// Dynamic Heap Management +//#define RT_USING_HEAP +// +// using small memory +// using small memory +#define RT_USING_SMALL_MEM +// +// using tiny size of memory +// using tiny size of memory +//#define RT_USING_TINY_SIZE +// +// + +// Console Configuration +// Using console +// Using console +#define RT_USING_CONSOLE +// +// the buffer size of console <1-1024> +// the buffer size of console +// Default: 128 (128Byte) +#define RT_CONSOLEBUF_SIZE 128 +// + +#if defined(RT_USING_FINSH) +#define FINSH_USING_MSH +#define FINSH_USING_MSH_ONLY +// Finsh Configuration +// the priority of finsh thread <1-7> +// the priority of finsh thread +// Default: 6 +#define __FINSH_THREAD_PRIORITY 5 +#define FINSH_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 8 * __FINSH_THREAD_PRIORITY + 1) +// the stack of finsh thread <1-4096> +// the stack of finsh thread +// Default: 4096 (4096Byte) +#define FINSH_THREAD_STACK_SIZE 512 +// the history lines of finsh thread <1-32> +// the history lines of finsh thread +// Default: 5 +#define FINSH_HISTORY_LINES 1 + +#define FINSH_USING_SYMTAB +// +#endif + +// <<< end of configuration section >>> + +#endif