Skip to content
This repository was archived by the owner on Dec 18, 2025. It is now read-only.

Commit 8fc0679

Browse files
RTX5: update library configuration setup
- reorganize Timer thread setup (prepare for MPU isolation)
1 parent 20d60b7 commit 8fc0679

7 files changed

Lines changed: 92 additions & 51 deletions

File tree

CMSIS/RTOS2/RTX/Include/rtx_os.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,8 @@ typedef struct {
460460
osThreadAttr_t *idle_thread_attr; ///< Idle Thread Attributes
461461
const
462462
osThreadAttr_t *timer_thread_attr; ///< Timer Thread Attributes
463+
void (*timer_thread)(void *); ///< Timer Thread Function
464+
int32_t (*timer_setup)(void); ///< Timer Setup Function
463465
const
464466
osMessageQueueAttr_t *timer_mq_attr; ///< Timer Message Queue Attributes
465467
uint32_t timer_mq_mcnt; ///< Timer Message Queue maximum Messages

CMSIS/RTOS2/RTX/RTX5.scvd

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@
414414
</typedef>
415415

416416
<!-- OS Configuration structure -->
417-
<typedef name="osRtxConfig_t" const="1" info="OS Configuration Structure" size="104">
417+
<typedef name="osRtxConfig_t" const="1" info="OS Configuration Structure" size="112">
418418
<member name="flags" type="uint32_t" offset="0" info="OS configuration flags"/>
419419
<member name="tick_freq" type="uint32_t" offset="4" info="Kernel tick frequency"/>
420420

@@ -444,8 +444,13 @@
444444
<member name="thread_stack_size" type="uint32_t" offset="84" info="Default thread stack size"/>
445445
<member name="idle_thread_attr" type="uint32_t" offset="88" info="Idle thread attributes (type is osThreadAttr_s *)"/>
446446
<member name="timer_thread_attr" type="uint32_t" offset="92" info="Timer thread attributes (type is osThreadAttr_s *)"/>
447-
<member name="timer_mq_attr" type="uint32_t" offset="96" info="Timer message queue attributes (type is osMessageQueueAttr_s *)"/>
448-
<member name="timer_mq_mcnt" type="uint32_t" offset="100" info="Timer message queue maximum messages"/>
447+
<member name="timer_thread" type="uint32_t" offset="96" info="Timer Thread Function (type is void(*func)(void *)"/>
448+
<member name="timer_setup" type="uint32_t" offset="100" info="Timer Setup Function (type is int32_t(*func)(void)"/>
449+
<member name="timer_mq_attr" type="uint32_t" offset="104" info="Timer message queue attributes (type is osMessageQueueAttr_s *)"/>
450+
<member name="timer_mq_mcnt" type="uint32_t" offset="108" info="Timer message queue maximum messages"/>
451+
452+
<var name="stack_check" type="uint8_t" info="Stack checking (0:disabled, 1:enabled)"/>
453+
<var name="stack_wmark" type="uint8_t" info="Stack watermark (0:disabled, 1:enabled)"/>
449454
</typedef>
450455

451456
<!-- Memory Pool Header -->
@@ -668,6 +673,11 @@
668673
<read name="os_Info" type="osRtxInfo_t" symbol="osRtxInfo"/>
669674
<read name="os_Config" type="osRtxConfig_t" symbol="osRtxConfig" const="1"/>
670675

676+
<calc>
677+
os_Config.stack_check = (os_Config.flags >> 1) &amp; 1;
678+
os_Config.stack_wmark = (os_Config.flags >> 2) &amp; 1;
679+
</calc>
680+
671681
<calc cond="((os_Info.version / 10000000) == 5) &amp;&amp; (os_Info.kernel_state &gt; 0) &amp;&amp; (os_Info.kernel_state &lt; 5)">
672682
RTX_En = 1;
673683
</calc>
@@ -682,9 +692,6 @@
682692
StaticMp_En = 1;
683693
</calc>
684694

685-
<var name="stack_check" type="uint8_t" value="(os_Config.flags >> 1) &amp; 1"/>
686-
<var name="stack_wmark" type="uint8_t" value="(os_Config.flags >> 2) &amp; 1"/>
687-
688695
<!-- Read ISR FIFO queue -->
689696
<read name="ISR_FIFO" cond="RTX_En" type="uint32_t" offset="os_Config.isr_queue_data" size="os_Config.isr_queue_max"/>
690697

@@ -866,7 +873,7 @@
866873
</calc>
867874

868875
<!-- Check also control values to determine maximum stack usage -->
869-
<calc cond="(stack_wmark != 0) &amp;&amp; (TCB[i].sp != 0) &amp;&amp; (TCB[i].stack_over == 0) &amp;&amp; (TCB[i].stack_size &lt; 65536)">
876+
<calc cond="(os_Config.stack_wmark != 0) &amp;&amp; (TCB[i].sp != 0) &amp;&amp; (TCB[i].stack_over == 0) &amp;&amp; (TCB[i].stack_size &lt; 65536)">
870877
TCB[i].stack_val = __CalcMemUsed (TCB[i].stack_mem, sp - TCB[i].stack_mem, 0xCCCCCCCC, 0xE25A2EA5);
871878
TCB[i].stack_over = TCB[i].stack_val >> 31;
872879
TCB[i].stack_maxb = (TCB[i].stack_mem + TCB[i].stack_size) - sp;
@@ -1058,10 +1065,10 @@
10581065
<calc>TCB_En += TCB[i].cb_valid; </calc>
10591066

10601067
<calc cond="TCB[i].cb_valid == 0"> TCB[i].out_type = 0;</calc>
1061-
<calc cond="TCB[i].cb_valid &amp;&amp; (TCB[i].name == 0) &amp;&amp; (stack_wmark == 0)"> TCB[i].out_type = 1; </calc>
1062-
<calc cond="TCB[i].cb_valid &amp;&amp; (TCB[i].name == 0) &amp;&amp; (stack_wmark != 0)"> TCB[i].out_type = 2; </calc>
1063-
<calc cond="TCB[i].cb_valid &amp;&amp; (TCB[i].name != 0) &amp;&amp; (stack_wmark == 0)"> TCB[i].out_type = 3; </calc>
1064-
<calc cond="TCB[i].cb_valid &amp;&amp; (TCB[i].name != 0) &amp;&amp; (stack_wmark != 0)"> TCB[i].out_type = 4; </calc>
1068+
<calc cond="TCB[i].cb_valid &amp;&amp; (TCB[i].name == 0) &amp;&amp; (os_Config.stack_wmark == 0)"> TCB[i].out_type = 1; </calc>
1069+
<calc cond="TCB[i].cb_valid &amp;&amp; (TCB[i].name == 0) &amp;&amp; (os_Config.stack_wmark != 0)"> TCB[i].out_type = 2; </calc>
1070+
<calc cond="TCB[i].cb_valid &amp;&amp; (TCB[i].name != 0) &amp;&amp; (os_Config.stack_wmark == 0)"> TCB[i].out_type = 3; </calc>
1071+
<calc cond="TCB[i].cb_valid &amp;&amp; (TCB[i].name != 0) &amp;&amp; (os_Config.stack_wmark != 0)"> TCB[i].out_type = 4; </calc>
10651072

10661073
<calc cond="TCB[i].sp_valid == 0"> TCB[i].out_type += 4; </calc>
10671074
</list>
@@ -1097,8 +1104,8 @@
10971104
<item property="Round Robin Timeout" value="%d[os_Config.robin_timeout]" cond="(os_Config.robin_timeout > 0) &amp;&amp; (RTX_En != 0)" />
10981105
<item property="Global Dynamic Memory" value="Not used" cond="(os_Config.mem_common_size == 0) &amp;&amp; (RTX_En != 0)"/>
10991106
<item property="Global Dynamic Memory" value="Base: %x[mem_head_com._addr], Size: %d[mem_head_com.size], Used: %d[mem_head_com.used], Max used: %d[mem_head_com.max_used]" cond="(os_Config.mem_common_size != 0) &amp;&amp; (RTX_En != 0)"/>
1100-
<item property="Stack Overrun Check" value="%t[stack_check ? &quot;Enabled&quot; : &quot;Disabled&quot;]" cond="RTX_En != 0"/>
1101-
<item property="Stack Usage Watermark" value="%t[stack_wmark ? &quot;Enabled&quot; : &quot;Disabled&quot;]" cond="RTX_En != 0"/>
1107+
<item property="Stack Overrun Check" value="%t[os_Config.stack_check ? &quot;Enabled&quot; : &quot;Disabled&quot;]" cond="RTX_En != 0"/>
1108+
<item property="Stack Usage Watermark" value="%t[os_Config.stack_wmark ? &quot;Enabled&quot; : &quot;Disabled&quot;]" cond="RTX_En != 0"/>
11021109
<item property="Default Thread Stack Size" value="%d[os_Config.thread_stack_size]" cond="RTX_En != 0"/>
11031110

11041111
<item property="ISR FIFO Queue" value="Size: %d[os_Info.isr_queue_max], Used: %d[os_Info.isr_queue_cnt]" cond="RTX_En">
@@ -1197,16 +1204,16 @@
11971204
</item>
11981205

11991206
<item>
1200-
<print cond="(TCB[i].sp_valid == 0) &amp;&amp; (stack_wmark == 0)" property="Stack" value="Used: unknown"/>
1201-
<print cond="(TCB[i].sp_valid == 0) &amp;&amp; (stack_wmark != 0)" property="Stack" value="Used: unknown, Max: %d[TCB[i].stack_maxp]%% [%d[TCB[i].stack_maxb]]"/>
1202-
<print cond="(TCB[i].sp_valid == 1) &amp;&amp; (stack_wmark == 0)" property="Stack" value="Used: %d[TCB[i].stack_curp]%% [%d[TCB[i].stack_curb]]"/>
1203-
<print cond="(TCB[i].sp_valid == 1) &amp;&amp; (stack_wmark != 0)" property="Stack" value="Used: %d[TCB[i].stack_curp]%% [%d[TCB[i].stack_curb]], Max: %d[TCB[i].stack_maxp]%% [%d[TCB[i].stack_maxb]]"/>
1207+
<print cond="(TCB[i].sp_valid == 0) &amp;&amp; (os_Config.stack_wmark == 0)" property="Stack" value="Used: unknown"/>
1208+
<print cond="(TCB[i].sp_valid == 0) &amp;&amp; (os_Config.stack_wmark != 0)" property="Stack" value="Used: unknown, Max: %d[TCB[i].stack_maxp]%% [%d[TCB[i].stack_maxb]]"/>
1209+
<print cond="(TCB[i].sp_valid == 1) &amp;&amp; (os_Config.stack_wmark == 0)" property="Stack" value="Used: %d[TCB[i].stack_curp]%% [%d[TCB[i].stack_curb]]"/>
1210+
<print cond="(TCB[i].sp_valid == 1) &amp;&amp; (os_Config.stack_wmark != 0)" property="Stack" value="Used: %d[TCB[i].stack_curp]%% [%d[TCB[i].stack_curb]], Max: %d[TCB[i].stack_maxp]%% [%d[TCB[i].stack_maxb]]"/>
12041211

12051212
<item>
12061213
<print cond="TCB[i].sp_valid == 0" property="Used" value="unknown"/>
12071214
<print cond="TCB[i].sp_valid == 1" property="Used" value="%d[TCB[i].stack_curb]"/>
12081215
</item>
1209-
<item property="Max" value="%d[TCB[i].stack_maxb]" cond="stack_wmark != 0"/>
1216+
<item property="Max" value="%d[TCB[i].stack_maxb]" cond="os_Config.stack_wmark != 0"/>
12101217
<item property="Top" value="%x[TCB[i].stack_mem + TCB[i].stack_size]"/>
12111218
<item>
12121219
<print cond="TCB[i].sp_valid == 0" property="Current" value="unknown"/>

CMSIS/RTOS2/RTX/Source/rtx_lib.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,6 @@ __attribute__((section(".bss.os.thread.stack")));
111111
#endif // (OS_THREAD_OBJ_MEM != 0)
112112

113113

114-
// Stack overrun checking
115-
#if (OS_STACK_CHECK == 0)
116-
// Override library function
117-
extern void osRtxThreadStackCheck (void);
118-
void osRtxThreadStackCheck (void) {}
119-
#endif
120-
121-
122114
// Idle Thread Control Block
123115
static osRtxThread_t os_idle_thread_cb \
124116
__attribute__((section(".bss.os.thread.cb")));
@@ -224,10 +216,8 @@ static const osMessageQueueAttr_t os_timer_mq_attr = {
224216
(uint32_t)sizeof(os_timer_mq_data)
225217
};
226218

227-
#else
228-
229-
extern void osRtxTimerThread (void *argument);
230-
void osRtxTimerThread (void *argument) { (void)argument; }
219+
extern int32_t osRtxTimerSetup (void);
220+
extern void osRtxTimerThread (void *argument);
231221

232222
#endif // ((OS_TIMER_THREAD_STACK_SIZE != 0) && (OS_TIMER_CB_QUEUE != 0))
233223

@@ -519,9 +509,13 @@ __attribute__((section(".rodata"))) =
519509
&os_idle_thread_attr,
520510
#if ((OS_TIMER_THREAD_STACK_SIZE != 0) && (OS_TIMER_CB_QUEUE != 0))
521511
&os_timer_thread_attr,
512+
osRtxTimerThread,
513+
osRtxTimerSetup,
522514
&os_timer_mq_attr,
523515
(uint32_t)OS_TIMER_CB_QUEUE
524516
#else
517+
NULL,
518+
NULL,
525519
NULL,
526520
NULL,
527521
0U
@@ -533,9 +527,9 @@ __attribute__((section(".rodata"))) =
533527
//lint -esym(526,irqRtxLib) "Defined by Exception handlers"
534528
//lint -esym(714,irqRtxLibRef) "Non weak reference"
535529
//lint -esym(765,irqRtxLibRef) "Global scope"
536-
extern uint8_t irqRtxLib;
537-
extern const uint8_t *irqRtxLibRef;
538-
const uint8_t *irqRtxLibRef = &irqRtxLib;
530+
extern const uint8_t irqRtxLib;
531+
extern const uint8_t * const irqRtxLibRef;
532+
const uint8_t * const irqRtxLibRef = &irqRtxLib;
539533

540534
// Default User SVC Table
541535
//lint -esym(714,osRtxUserSVC) "Referenced by Exception handlers"

CMSIS/RTOS2/RTX/Source/rtx_lib.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ extern bool_t osRtxThreadStackCheck (const os_thread_t *thread);
195195
extern bool_t osRtxThreadStartup (void);
196196

197197
// Timer Library functions
198-
extern void osRtxTimerThread (void *argument);
198+
extern int32_t osRtxTimerSetup (void);
199+
extern void osRtxTimerThread (void *argument);
199200

200201
// Mutex Library functions
201202
extern void osRtxMutexOwnerRelease (os_mutex_t *mutex_list);
@@ -211,6 +212,9 @@ extern uint32_t osRtxMemoryPoolInit (os_mp_info_t *mp_info, uint32_t block_co
211212
extern void *osRtxMemoryPoolAlloc (os_mp_info_t *mp_info);
212213
extern osStatus_t osRtxMemoryPoolFree (os_mp_info_t *mp_info, void *block);
213214

215+
// Message Queue Library functions
216+
extern int32_t osRtxMessageQueueTimerSetup (void);
217+
214218
// System Library functions
215219
extern void osRtxTick_Handler (void);
216220
extern void osRtxPendSV_Handler (void);

CMSIS/RTOS2/RTX/Source/rtx_msgqueue.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,23 @@ osStatus_t isrRtxMessageQueueGet (osMessageQueueId_t mq_id, void *msg_ptr, uint8
812812
}
813813

814814

815+
// ==== Library functions ====
816+
817+
/// Create a Message Queue for the Timer Thread.
818+
int32_t osRtxMessageQueueTimerSetup (void) {
819+
int32_t ret = -1;
820+
821+
osRtxInfo.timer.mq = osRtxMessageQueueId(
822+
svcRtxMessageQueueNew(osRtxConfig.timer_mq_mcnt, sizeof(os_timer_finfo_t), osRtxConfig.timer_mq_attr)
823+
);
824+
if (osRtxInfo.timer.mq != NULL) {
825+
ret = 0;
826+
}
827+
828+
return ret;
829+
}
830+
831+
815832
// ==== Public API ====
816833

817834
/// Create and Initialize a Message Queue object.

CMSIS/RTOS2/RTX/Source/rtx_thread.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,21 +1619,25 @@ uint32_t isrRtxThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
16191619
/// Thread startup (Idle and Timer Thread).
16201620
/// \return true - success, false - failure.
16211621
bool_t osRtxThreadStartup (void) {
1622-
bool_t ret = TRUE;
1622+
bool_t ret = FALSE;
16231623

16241624
// Create Idle Thread
16251625
osRtxInfo.thread.idle = osRtxThreadId(
16261626
svcRtxThreadNew(osRtxIdleThread, NULL, osRtxConfig.idle_thread_attr)
16271627
);
16281628

16291629
// Create Timer Thread
1630-
if (osRtxConfig.timer_mq_mcnt != 0U) {
1631-
osRtxInfo.timer.thread = osRtxThreadId(
1632-
svcRtxThreadNew(osRtxTimerThread, NULL, osRtxConfig.timer_thread_attr)
1633-
);
1634-
if (osRtxInfo.timer.thread == NULL) {
1635-
ret = FALSE;
1630+
if (osRtxConfig.timer_setup != NULL) {
1631+
if (osRtxConfig.timer_setup() == 0) {
1632+
osRtxInfo.timer.thread = osRtxThreadId(
1633+
svcRtxThreadNew(osRtxConfig.timer_thread, osRtxInfo.timer.mq, osRtxConfig.timer_thread_attr)
1634+
);
1635+
if (osRtxInfo.timer.thread != NULL) {
1636+
ret = TRUE;
1637+
}
16361638
}
1639+
} else {
1640+
ret = TRUE;
16371641
}
16381642

16391643
return ret;

CMSIS/RTOS2/RTX/Source/rtx_timer.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,33 @@ static void osRtxTimerTick (void) {
130130
osRtxThreadSetRunning(thread_running);
131131
}
132132

133-
/// Timer Thread
134-
__WEAK __NO_RETURN void osRtxTimerThread (void *argument) {
135-
os_timer_finfo_t finfo;
136-
osStatus_t status;
137-
(void) argument;
133+
/// Setup Timer Thread objects.
134+
//lint -esym(714,osRtxTimerSetup) "Referenced from library configuration"
135+
//lint -esym(759,osRtxTimerSetup) "Prototype in header"
136+
//lint -esym(765,osRtxTimerSetup) "Global scope"
137+
int32_t osRtxTimerSetup (void) {
138+
int32_t ret = -1;
139+
140+
if (osRtxMessageQueueTimerSetup() == 0) {
141+
osRtxInfo.timer.tick = osRtxTimerTick;
142+
ret = 0;
143+
}
138144

139-
osRtxInfo.timer.mq = osRtxMessageQueueId(
140-
osMessageQueueNew(osRtxConfig.timer_mq_mcnt, sizeof(os_timer_finfo_t), osRtxConfig.timer_mq_attr)
141-
);
142-
osRtxInfo.timer.tick = osRtxTimerTick;
145+
return ret;
146+
}
147+
148+
/// Timer Thread
149+
//lint -esym(714,osRtxTimerThread) "Referenced from library configuration"
150+
//lint -esym(759,osRtxTimerThread) "Prototype in header"
151+
//lint -esym(765,osRtxTimerThread) "Global scope"
152+
__NO_RETURN void osRtxTimerThread (void *argument) {
153+
os_timer_finfo_t finfo;
154+
osStatus_t status;
155+
osMessageQueueId_t mq = (osMessageQueueId_t)argument;
143156

144157
for (;;) {
145158
//lint -e{934} "Taking address of near auto variable"
146-
status = osMessageQueueGet(osRtxInfo.timer.mq, &finfo, NULL, osWaitForever);
159+
status = osMessageQueueGet(mq, &finfo, NULL, osWaitForever);
147160
if (status == osOK) {
148161
EvrRtxTimerCallback(finfo.func, finfo.arg);
149162
(finfo.func)(finfo.arg);

0 commit comments

Comments
 (0)