Skip to content

Commit 64fe5fc

Browse files
committed
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 <[email protected]>
1 parent 53f30f3 commit 64fe5fc

File tree

4 files changed

+348
-0
lines changed

4 files changed

+348
-0
lines changed
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
TARGET = rtthread_demo
2+
RTOS = RTThread
3+
4+
NUCLEI_SDK_ROOT = ../../..
5+
6+
COMMON_FLAGS = -O3 -DSMODE_RTOS
7+
8+
XLCFG_TEE := 1
9+
10+
SRCDIRS = .
11+
INCDIRS = .
12+
13+
include $(NUCLEI_SDK_ROOT)/Build/Makefile.base
+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright (c) 2006-2019, RT-Thread Development Team
3+
* Copyright (c) 2019-Present Nuclei Limited. All rights reserved.
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Change Logs:
8+
* Date Author Notes
9+
* 2020-03-26 Huaqi the first version
10+
*/
11+
12+
#include "nuclei_sdk_soc.h"
13+
#include <rtthread.h>
14+
#include <stdio.h>
15+
16+
#if !defined(__TEE_PRESENT) || (__TEE_PRESENT != 1)
17+
/* __TEE_PRESENT should be defined in <Device>.h */
18+
#error "This example require CPU TEE feature!"
19+
#endif
20+
21+
#if !defined(__PMP_PRESENT) || (__PMP_PRESENT != 1)
22+
/* __PMP_PRESENT should be defined in <Device>.h */
23+
#error "This example require CPU PMP feature!"
24+
#endif
25+
26+
#define THREAD_PRIORITY 2
27+
#define THREAD_STACK_SIZE 512
28+
#define THREAD_TIMESLICE 5
29+
#define THREAD_NUM 5
30+
31+
/* Align stack when using static thread */
32+
ALIGN(RT_ALIGN_SIZE)
33+
static rt_uint8_t thread_stack[THREAD_NUM][THREAD_STACK_SIZE];
34+
static struct rt_thread tid[THREAD_NUM];
35+
36+
/* Thread entry function */
37+
static void thread_entry(void* parameter)
38+
{
39+
rt_uint32_t count = 0;
40+
41+
while (1) {
42+
rt_kprintf("thread %d count: %d\n", (rt_uint32_t)parameter, count++);
43+
rt_thread_mdelay(250);
44+
}
45+
}
46+
47+
/* Thread demo */
48+
int create_thread_demo(void)
49+
{
50+
unsigned long i;
51+
for (i = 0; i < THREAD_NUM; i ++) {
52+
/* Create static threads */
53+
rt_thread_init(&tid[i], "thread", thread_entry, (void*)i, thread_stack[i],
54+
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
55+
}
56+
57+
/* Startup threads */
58+
for (i = 0; i < THREAD_NUM; i ++) {
59+
rt_thread_startup(&tid[i]);
60+
}
61+
62+
return 0;
63+
}
64+
65+
int main(void)
66+
{
67+
rt_uint32_t count = 0;
68+
69+
create_thread_demo();
70+
71+
while (1) {
72+
rt_kprintf("Main thread count: %d\n", count++);
73+
rt_thread_mdelay(500);
74+
#ifdef CFG_SIMULATION
75+
if (count > 2) {
76+
// directly exit if in nuclei internally simulation
77+
SIMULATION_EXIT(0);
78+
}
79+
#endif
80+
}
81+
}
82+
83+
extern void entry(void);
84+
85+
#define SMODE_STACK_SIZE 2048
86+
// Execute Hart ID
87+
#define EXECUTE_HARTID 0
88+
89+
/* Create a stack for supervisor mode execution */
90+
uint8_t smode_stack[SMODE_STACK_SIZE] __attribute__((aligned(16)));
91+
92+
uintptr_t smode_sp = (uintptr_t) (smode_stack + sizeof(smode_stack));
93+
94+
int main_entry(void)
95+
{
96+
CSR_MCFGINFO_Type mcfg;
97+
mcfg.d = __RV_CSR_READ(CSR_MCFG_INFO);
98+
99+
if ((mcfg.b.tee & mcfg.b.clic) == 0) {
100+
printf("ERROR: TEE and ECLIC feature are required to run this SMode RT-Thread Demo\n");
101+
return 1;
102+
}
103+
104+
// set pmp, S mode can access all address range
105+
pmp_config pmp_cfg = {
106+
/* M mode grants S and U mode with full permission of the whole address range */
107+
.protection = PMP_L | PMP_R | PMP_W | PMP_X,
108+
/* Memory region range 2^__RISCV_XLEN bytes */
109+
.order = __RISCV_XLEN,
110+
/* Initial base address is 0 */
111+
.base_addr = 0,
112+
};
113+
114+
__set_PMPENTRYx(0, &pmp_cfg);
115+
printf("Set ECLIC Timer Interrupt and Software Timer Interrupt to be executed in S-Mode\r\n");
116+
// before drop to S Mode, specifies in which privilege mode the interrupt should be taken
117+
ECLIC_SetModeIRQ(SysTimerSW_IRQn, PRV_S);
118+
ECLIC_SetModeIRQ(SysTimer_IRQn, PRV_S);
119+
120+
printf("Drop to S-Mode to prepare RT-Thread Environment\r\n");
121+
/* Drop to S mode */
122+
__switch_mode(PRV_S, smode_sp, entry);
123+
// Should never return here
124+
return 1;
125+
}
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Package Base Information
2+
name: app-nsdk_rtthread_smode_demo
3+
owner: nuclei
4+
version:
5+
description: S-Mode RTThread Task Demo
6+
type: app
7+
keywords:
8+
- rtthread
9+
- task demo
10+
category: rtthread application
11+
license:
12+
homepage:
13+
14+
## Package Dependency
15+
dependencies:
16+
- name: sdk-nuclei_sdk
17+
version:
18+
- name: osp-nsdk_rtthread
19+
version:
20+
21+
## Package Configurations
22+
configuration:
23+
app_commonflags:
24+
value: -O3
25+
type: text
26+
description: Application Compile Flags
27+
28+
## Set Configuration for other packages
29+
setconfig:
30+
- config: rtthread_msh
31+
value: 0
32+
33+
## Source Code Management
34+
codemanage:
35+
copyfiles:
36+
- path: ["*.c", "*.h"]
37+
incdirs:
38+
- path: ["./"]
39+
libdirs:
40+
ldlibs:
41+
- libs:
42+
43+
## Build Configuration
44+
buildconfig:
45+
- type: common
46+
common_flags: # flags need to be combined together across all packages
47+
# SMODE_RTOS is required, and CFG_HAS_TEE is defined to expect TEE feature present in hw
48+
- flags: -DSMODE_RTOS -DCFG_HAS_TEE ${app_commonflags}
+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/* RT-Thread config file */
2+
3+
#ifndef __RTTHREAD_CFG_H__
4+
#define __RTTHREAD_CFG_H__
5+
6+
#include <rtthread.h>
7+
8+
#if defined(__CC_ARM) || defined(__CLANG_ARM)
9+
#include "RTE_Components.h"
10+
11+
#if defined(RTE_USING_FINSH)
12+
#define RT_USING_FINSH
13+
#endif //RTE_USING_FINSH
14+
15+
#endif //(__CC_ARM) || (__CLANG_ARM)
16+
17+
// <<< Use Configuration Wizard in Context Menu >>>
18+
// <h>Basic Configuration
19+
// <o>Maximal level of thread priority <8-256>
20+
// <i>Default: 32
21+
#define RT_THREAD_PRIORITY_MAX 8
22+
// <o>OS tick per second
23+
// <i>Default: 1000 (1ms)
24+
#define RT_TICK_PER_SECOND 100
25+
// <o>Alignment size for CPU architecture data access
26+
// <i>Default: 4
27+
#define RT_ALIGN_SIZE 8
28+
// <o>the max length of object name<2-16>
29+
// <i>Default: 8
30+
#define RT_NAME_MAX 8
31+
// <c1>Using RT-Thread components initialization
32+
// <i>Using RT-Thread components initialization
33+
#define RT_USING_COMPONENTS_INIT
34+
// </c>
35+
36+
#define RT_USING_USER_MAIN
37+
38+
// <o>the stack size of main thread<1-4086>
39+
// <i>Default: 512
40+
#define RT_MAIN_THREAD_STACK_SIZE 1024
41+
42+
// <o>the stack size of main thread<1-4086>
43+
// <i>Default: 128
44+
#define IDLE_THREAD_STACK_SIZE 512
45+
46+
47+
48+
// </h>
49+
50+
// <h>Debug Configuration
51+
// <c1>enable kernel debug configuration
52+
// <i>Default: enable kernel debug configuration
53+
//#define RT_DEBUG
54+
// </c>
55+
// <o>enable components initialization debug configuration<0-1>
56+
// <i>Default: 0
57+
#define RT_DEBUG_INIT 0
58+
// <c1>thread stack over flow detect
59+
// <i> Diable Thread stack over flow detect
60+
//#define RT_USING_OVERFLOW_CHECK
61+
// </c>
62+
// </h>
63+
64+
// <h>Hook Configuration
65+
// <c1>using hook
66+
// <i>using hook
67+
//#define RT_USING_HOOK
68+
// </c>
69+
// <c1>using idle hook
70+
// <i>using idle hook
71+
//#define RT_USING_IDLE_HOOK
72+
// </c>
73+
// </h>
74+
75+
// <e>Software timers Configuration
76+
// <i> Enables user timers
77+
#define RT_USING_TIMER_SOFT 0
78+
#if RT_USING_TIMER_SOFT == 0
79+
#undef RT_USING_TIMER_SOFT
80+
#endif
81+
// <o>The priority level of timer thread <0-31>
82+
// <i>Default: 4
83+
#define RT_TIMER_THREAD_PRIO 4
84+
// <o>The stack size of timer thread <0-8192>
85+
// <i>Default: 512
86+
#define RT_TIMER_THREAD_STACK_SIZE 512
87+
// </e>
88+
89+
// <h>IPC(Inter-process communication) Configuration
90+
// <c1>Using Semaphore
91+
// <i>Using Semaphore
92+
#define RT_USING_SEMAPHORE
93+
// </c>
94+
// <c1>Using Mutex
95+
// <i>Using Mutex
96+
//#define RT_USING_MUTEX
97+
// </c>
98+
// <c1>Using Event
99+
// <i>Using Event
100+
//#define RT_USING_EVENT
101+
// </c>
102+
// <c1>Using MailBox
103+
// <i>Using MailBox
104+
#define RT_USING_MAILBOX
105+
// </c>
106+
// <c1>Using Message Queue
107+
// <i>Using Message Queue
108+
//#define RT_USING_MESSAGEQUEUE
109+
// </c>
110+
// </h>
111+
112+
// <h>Memory Management Configuration
113+
// <c1>Dynamic Heap Management
114+
// <i>Dynamic Heap Management
115+
//#define RT_USING_HEAP
116+
// </c>
117+
// <c1>using small memory
118+
// <i>using small memory
119+
#define RT_USING_SMALL_MEM
120+
// </c>
121+
// <c1>using tiny size of memory
122+
// <i>using tiny size of memory
123+
//#define RT_USING_TINY_SIZE
124+
// </c>
125+
// </h>
126+
127+
// <h>Console Configuration
128+
// <c1>Using console
129+
// <i>Using console
130+
#define RT_USING_CONSOLE
131+
// </c>
132+
// <o>the buffer size of console <1-1024>
133+
// <i>the buffer size of console
134+
// <i>Default: 128 (128Byte)
135+
#define RT_CONSOLEBUF_SIZE 128
136+
// </h>
137+
138+
#if defined(RT_USING_FINSH)
139+
#define FINSH_USING_MSH
140+
#define FINSH_USING_MSH_ONLY
141+
// <h>Finsh Configuration
142+
// <o>the priority of finsh thread <1-7>
143+
// <i>the priority of finsh thread
144+
// <i>Default: 6
145+
#define __FINSH_THREAD_PRIORITY 5
146+
#define FINSH_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 8 * __FINSH_THREAD_PRIORITY + 1)
147+
// <o>the stack of finsh thread <1-4096>
148+
// <i>the stack of finsh thread
149+
// <i>Default: 4096 (4096Byte)
150+
#define FINSH_THREAD_STACK_SIZE 512
151+
// <o>the history lines of finsh thread <1-32>
152+
// <i>the history lines of finsh thread
153+
// <i>Default: 5
154+
#define FINSH_HISTORY_LINES 1
155+
156+
#define FINSH_USING_SYMTAB
157+
// </h>
158+
#endif
159+
160+
// <<< end of configuration section >>>
161+
162+
#endif

0 commit comments

Comments
 (0)