ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ns_main.c
Go to the documentation of this file.
1
18
19#include <stdint.h>
20#include <string.h>
21
22#include "ra8_err.h"
23#include "ra8_nsc.h"
24#include "tx_api.h"
25
26/* Linker symbols for BSS, Stack, and Run memory */
27extern uint32_t g_ra8_ls_ns_bss_start;
28extern uint32_t g_ra8_ls_ns_bss_end;
29extern uint32_t g_ra8_ls_ns_stack_top;
30extern uint32_t g_ra8_ls_ns_run_start;
31
33typedef enum : uintptr_t {
34 k_ns_scb_vtor_addr = 0xE000ED08U,
36
42typedef enum : uint32_t {
46
49
52
62
63/* External declarations for thread tick hooks */
64extern void PendSV_Handler(void);
65extern void _tx_timer_interrupt(void);
66extern volatile uint32_t g_ra8_threadx_systick_ready;
67
71static void ns_systick_handler(void)
72{
75 }
76}
77
81[[noreturn]] static void ns_panic_halt(void)
82{
83 while (1) {
84 __asm__ volatile("wfi");
85 }
86}
87
97typedef enum : uint16_t {
101
105static void ui_thread_entry(ULONG thread_input)
106{
107 (void)thread_input;
108 (void)ra8_nsc_log_emit("UI", "UI thread started -- Simulating screen/event loop");
109
110 uint32_t count = 0U;
111 for (;;) {
112 count++;
113 if ((count % (uint32_t)k_ns_ui_heartbeat_iters) == 0U) {
114 (void)ra8_nsc_log_emit("UI", "UI Loop: Refreshing screen layout...");
115 }
117 }
118}
119
123static void work_thread_entry(ULONG thread_input)
124{
125 (void)thread_input;
126 (void)ra8_nsc_log_emit("WORK", "Worker thread started -- Processing background tasks");
127
128 uint32_t count = 0U;
129 for (;;) {
130 count++;
131 if ((count % (uint32_t)k_ns_work_heartbeat_iters) == 0U) {
132 (void)ra8_nsc_log_emit("WORK", "Worker Loop: Reading battery & sensor state via NSC veneers");
133 }
135 }
136}
137
141void tx_application_define(void* first_unused_memory)
142{
143 (void)first_unused_memory;
144
145 /* Create the UI thread. (CHAR*)(uintptr_t): the vendored ThreadX API takes
146 * a non-const CHAR* for the thread name; the uintptr_t hop launders the
147 * string-literal const without tripping -Wcast-qual. */
149 (CHAR*)(uintptr_t)"UI Thread",
151 0UL,
154 10U, /* Priority */
155 10U, /* Preemption threshold */
158
159 /* Create the Worker thread */
161 (CHAR*)(uintptr_t)"Worker Thread",
163 0UL,
166 15U, /* Priority */
167 15U, /* Preemption threshold */
170}
171
189[[noreturn]] void ns_reset_handler(void);
190
191[[noreturn]] void ns_reset_handler(void)
192{
193 /* Zero the NS BSS section */
194 const uintptr_t bss_start = (uintptr_t)&g_ra8_ls_ns_bss_start;
195 const uintptr_t bss_end = (uintptr_t)&g_ra8_ls_ns_bss_end;
196 for (uintptr_t addr = bss_start; addr < bss_end; addr += sizeof(uint32_t)) {
197 *(volatile uint32_t*)addr = 0U;
198 }
199
200 /* Set the NS VTOR so exceptions vector correctly to NS handlers */
201 *(volatile uint32_t*)k_ns_scb_vtor_addr = (uint32_t)(uintptr_t)&g_ra8_ls_ns_run_start;
202
203 /* Call Secure-side substrate initialization via NSC veneer gateway */
204 if (ra8_nsc_periph_init() != k_ra8_ok) {
206 }
207
208 /* Announce that Non-Secure is online! */
209 (void)ra8_nsc_log_emit("BOOT", "tz_threadx_demo: Non-Secure world online!");
210
211 /* Enter ThreadX RTOS kernel (never returns) */
212 tx_kernel_enter();
213
215}
216
217/* =============================================================================
218 * Non-Secure Vector Table
219 * =============================================================================
220 */
221typedef void (*ns_exc_handler_t)(void);
222
223[[noreturn]] static void ns_nmi_halt(void)
224{
225 while (1) {
226 __asm__ volatile("wfi");
227 }
228}
229
230[[gnu::section(".ns_vectors"), gnu::used]] const ns_exc_handler_t g_ra8_ns_vector_table[16] = {
231 (ns_exc_handler_t)&g_ra8_ls_ns_stack_top, /* 0 Initial MSP_NS */
232 ns_reset_handler, /* 1 Reset */
233 ns_nmi_halt, /* 2 NMI */
234 ns_nmi_halt, /* 3 HardFault */
235 ns_nmi_halt, /* 4 MemManage */
236 ns_nmi_halt, /* 5 BusFault */
237 ns_nmi_halt, /* 6 UsageFault */
238 ns_nmi_halt, /* 7 SecureFault */
239 0, /* 8 Reserved */
240 0, /* 9 Reserved */
241 0, /* 10 Reserved */
242 ns_nmi_halt, /* 11 SVCall */
243 ns_nmi_halt, /* 12 DebugMonitor */
244 0, /* 13 Reserved */
245 PendSV_Handler, /* 14 PendSV */
246 ns_systick_handler, /* 15 SysTick */
247};
void PendSV_Handler(void)
Perform the ThreadX Non-Secure context switch requested by PendSV.
void _tx_timer_interrupt(void)
Advance the ThreadX kernel timer by one hardware tick.
void ns_reset_handler(void)
Non-Secure Reset handler: entered via Secure-to-NS transition.
Definition ns_main.c:431
void tx_application_define(void *first_unused_memory)
Setup ThreadX threads and resources.
Definition ns_main.c:370
const uint32_t g_ra8_ns_vector_table[8]
NS-world ARMv8-M vector table.
Definition ns_main.c:455
uint32_t g_ra8_ls_ns_stack_top
uint32_t g_ra8_ls_ns_bss_start
NS world reset handler – entry point of the BLXNS branch.
void ns_reset_handler(void)
Definition ns_main.c:384
uint32_t g_ra8_ls_ns_bss_end
ns_scb_addr_t
NS-state VTOR (0xE000ED08 is the current-domain alias in NS).
Definition ns_main.c:158
@ k_ns_scb_vtor_addr
Ns scb vtor address.
Definition ns_main.c:159
static void ns_nmi_halt(void)
NMI / fault halt vector for the NS table.
Definition ns_main.c:352
void(* ns_exc_handler_t)(void)
Function-pointer type for entries in the NS vector table.
Definition ns_main.c:337
static void ns_systick_handler(void)
NS SysTick handler – drives the ThreadX 1 ms time base.
Definition ns_main.c:316
uint32_t g_ra8_ls_ns_run_start
Linker symbol: NS vector table base.
ns_log_cadence_t
Heartbeat-log cadence per thread (iterations between log lines).
Definition ns_main.c:58
@ k_ns_ui_heartbeat_iters
UI heartbeat log cadence.
Definition ns_main.c:59
@ k_ns_work_heartbeat_iters
Worker heartbeat log cadence.
Definition ns_main.c:60
static TX_THREAD s_ui_thread
Definition ns_main.c:47
static TX_THREAD s_work_thread
Definition ns_main.c:48
static void ns_panic_halt(void)
Park the CPU in an idle loop if a fatal startup error occurs.
Definition ns_main.c:81
ns_thread_cadence_t
Per-thread sleep cadence, in ThreadX ticks.
Definition ns_main.c:97
@ k_ns_work_sleep_ticks
Worker thread sleep between batches.
Definition ns_main.c:99
@ k_ns_ui_sleep_ticks
UI thread sleep between refreshes.
Definition ns_main.c:98
ns_thread_stack_t
Stack sizes for Non-Secure ThreadX threads.
Definition ns_main.c:42
@ k_ns_ui_thread_stack_size
Stack size for UI thread.
Definition ns_main.c:43
@ k_ns_work_thread_stack_size
Stack size for Worker thread.
Definition ns_main.c:44
static uint8_t s_ui_thread_stack[k_ns_ui_thread_stack_size]
Definition ns_main.c:50
static void ui_thread_entry(ULONG thread_input)
UI thread entry: logs periodically to simulate UI rendering loops.
Definition ns_main.c:105
static uint8_t s_work_thread_stack[k_ns_work_thread_stack_size]
Definition ns_main.c:51
static void work_thread_entry(ULONG thread_input)
Worker thread entry: simulates background sensors/storage processing.
Definition ns_main.c:123
void PendSV_Handler(void)
Error Code Definitions for ra8-firmware.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
Non-Secure Callable veneers – the only NS->S gateway.
ra8_err_t ra8_nsc_log_emit(const char *tag, const char *message)
NSC veneer: emit a log line via the secure ITM channel.
Definition ra8_nsc_log.c:94
ra8_err_t ra8_nsc_periph_init(void)
NSC veneer: bring up the secure-side peripheral substrate.
volatile uint32_t g_ra8_threadx_systick_ready
0 until _tx_initialize_low_level has armed ThreadX's SysTick + timer state; 1 afterwards.
#define tx_thread_create
#define tx_thread_sleep
#define TX_NO_TIME_SLICE
char CHAR
ThreadX-compatible CHAR (host stub).
#define TX_AUTO_START
unsigned long ULONG
ThreadX-compatible unsigned long (host stub).
Opaque thread stand-in for the host build.