Skip to content

Commit

Permalink
application: add smode rtthread demo to demostrate how to run
Browse files Browse the repository at this point in the history
Take care this required TEE present and ECLIC present

see Makefile

Signed-off-by: Huaqi Fang <[email protected]>
  • Loading branch information
fanghuaqi committed Dec 16, 2024
1 parent 53f30f3 commit 64fe5fc
Show file tree
Hide file tree
Showing 4 changed files with 348 additions and 0 deletions.
13 changes: 13 additions & 0 deletions application/rtthread/demo_smode/Makefile
Original file line number Diff line number Diff line change
@@ -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
125 changes: 125 additions & 0 deletions application/rtthread/demo_smode/main.c
Original file line number Diff line number Diff line change
@@ -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 <rtthread.h>
#include <stdio.h>

#if !defined(__TEE_PRESENT) || (__TEE_PRESENT != 1)
/* __TEE_PRESENT should be defined in <Device>.h */
#error "This example require CPU TEE feature!"
#endif

#if !defined(__PMP_PRESENT) || (__PMP_PRESENT != 1)
/* __PMP_PRESENT should be defined in <Device>.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;
}
48 changes: 48 additions & 0 deletions application/rtthread/demo_smode/npk.yml
Original file line number Diff line number Diff line change
@@ -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}
162 changes: 162 additions & 0 deletions application/rtthread/demo_smode/rtconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/* RT-Thread config file */

#ifndef __RTTHREAD_CFG_H__
#define __RTTHREAD_CFG_H__

#include <rtthread.h>

#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 >>>
// <h>Basic Configuration
// <o>Maximal level of thread priority <8-256>
// <i>Default: 32
#define RT_THREAD_PRIORITY_MAX 8
// <o>OS tick per second
// <i>Default: 1000 (1ms)
#define RT_TICK_PER_SECOND 100
// <o>Alignment size for CPU architecture data access
// <i>Default: 4
#define RT_ALIGN_SIZE 8
// <o>the max length of object name<2-16>
// <i>Default: 8
#define RT_NAME_MAX 8
// <c1>Using RT-Thread components initialization
// <i>Using RT-Thread components initialization
#define RT_USING_COMPONENTS_INIT
// </c>

#define RT_USING_USER_MAIN

// <o>the stack size of main thread<1-4086>
// <i>Default: 512
#define RT_MAIN_THREAD_STACK_SIZE 1024

// <o>the stack size of main thread<1-4086>
// <i>Default: 128
#define IDLE_THREAD_STACK_SIZE 512



// </h>

// <h>Debug Configuration
// <c1>enable kernel debug configuration
// <i>Default: enable kernel debug configuration
//#define RT_DEBUG
// </c>
// <o>enable components initialization debug configuration<0-1>
// <i>Default: 0
#define RT_DEBUG_INIT 0
// <c1>thread stack over flow detect
// <i> Diable Thread stack over flow detect
//#define RT_USING_OVERFLOW_CHECK
// </c>
// </h>

// <h>Hook Configuration
// <c1>using hook
// <i>using hook
//#define RT_USING_HOOK
// </c>
// <c1>using idle hook
// <i>using idle hook
//#define RT_USING_IDLE_HOOK
// </c>
// </h>

// <e>Software timers Configuration
// <i> Enables user timers
#define RT_USING_TIMER_SOFT 0
#if RT_USING_TIMER_SOFT == 0
#undef RT_USING_TIMER_SOFT
#endif
// <o>The priority level of timer thread <0-31>
// <i>Default: 4
#define RT_TIMER_THREAD_PRIO 4
// <o>The stack size of timer thread <0-8192>
// <i>Default: 512
#define RT_TIMER_THREAD_STACK_SIZE 512
// </e>

// <h>IPC(Inter-process communication) Configuration
// <c1>Using Semaphore
// <i>Using Semaphore
#define RT_USING_SEMAPHORE
// </c>
// <c1>Using Mutex
// <i>Using Mutex
//#define RT_USING_MUTEX
// </c>
// <c1>Using Event
// <i>Using Event
//#define RT_USING_EVENT
// </c>
// <c1>Using MailBox
// <i>Using MailBox
#define RT_USING_MAILBOX
// </c>
// <c1>Using Message Queue
// <i>Using Message Queue
//#define RT_USING_MESSAGEQUEUE
// </c>
// </h>

// <h>Memory Management Configuration
// <c1>Dynamic Heap Management
// <i>Dynamic Heap Management
//#define RT_USING_HEAP
// </c>
// <c1>using small memory
// <i>using small memory
#define RT_USING_SMALL_MEM
// </c>
// <c1>using tiny size of memory
// <i>using tiny size of memory
//#define RT_USING_TINY_SIZE
// </c>
// </h>

// <h>Console Configuration
// <c1>Using console
// <i>Using console
#define RT_USING_CONSOLE
// </c>
// <o>the buffer size of console <1-1024>
// <i>the buffer size of console
// <i>Default: 128 (128Byte)
#define RT_CONSOLEBUF_SIZE 128
// </h>

#if defined(RT_USING_FINSH)
#define FINSH_USING_MSH
#define FINSH_USING_MSH_ONLY
// <h>Finsh Configuration
// <o>the priority of finsh thread <1-7>
// <i>the priority of finsh thread
// <i>Default: 6
#define __FINSH_THREAD_PRIORITY 5
#define FINSH_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 8 * __FINSH_THREAD_PRIORITY + 1)
// <o>the stack of finsh thread <1-4096>
// <i>the stack of finsh thread
// <i>Default: 4096 (4096Byte)
#define FINSH_THREAD_STACK_SIZE 512
// <o>the history lines of finsh thread <1-32>
// <i>the history lines of finsh thread
// <i>Default: 5
#define FINSH_HISTORY_LINES 1

#define FINSH_USING_SYMTAB
// </h>
#endif

// <<< end of configuration section >>>

#endif

0 comments on commit 64fe5fc

Please sign in to comment.