Skip to content

Commit d857efa

Browse files
committed
Add support for processor affinity to CMSIS-RTOS2 wrapper
1 parent d5708b0 commit d857efa

4 files changed

Lines changed: 143 additions & 9 deletions

File tree

ARM.CMSIS-FreeRTOS.pdsc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<release version="0.0.0">
1212
FreeRTOS 11.0.1
1313
Active development...
14+
- Add support for processor affinity to CMSIS-RTOS2 wrapper
1415
- Add memory allocation configuration options to FreeRTOSConfig.h
1516
- CMSIS-RTOS2 requires CMSIS:OS Tick component
1617
- Drop support for Arm Compiler 5
@@ -20,7 +21,7 @@
2021

2122
<requirements>
2223
<packages>
23-
<package vendor="ARM" name="CMSIS" version="5.9.0"/>
24+
<package vendor="ARM" name="CMSIS" version="6.0.0-0"/>
2425
</packages>
2526
</requirements>
2627

@@ -870,7 +871,7 @@
870871

871872
<components>
872873
<!-- CMSIS-RTOS2 FreeRTOS component -->
873-
<component Cclass="CMSIS" Cgroup="RTOS2" Csub="FreeRTOS" Cvariant="Cortex-M" Cversion="11.0.1" Capiversion="2.2.0" condition="CMSIS RTOS2 FreeRTOS CortexM">
874+
<component Cclass="CMSIS" Cgroup="RTOS2" Csub="FreeRTOS" Cvariant="Cortex-M" Cversion="11.0.1" Capiversion="2.3.0" condition="CMSIS RTOS2 FreeRTOS CortexM">
874875
<description>CMSIS-RTOS2 implementation for Cortex-M based on FreeRTOS</description>
875876
<RTE_Components_h>
876877
#define RTE_CMSIS_RTOS2 /* CMSIS-RTOS2 */
@@ -884,7 +885,7 @@
884885
</files>
885886
</component>
886887

887-
<component Cclass="CMSIS" Cgroup="RTOS2" Csub="FreeRTOS" Cvariant="Cortex-A" Cversion="11.0.1" Capiversion="2.2.0" condition="CMSIS RTOS2 FreeRTOS CortexA">
888+
<component Cclass="CMSIS" Cgroup="RTOS2" Csub="FreeRTOS" Cvariant="Cortex-A" Cversion="11.0.1" Capiversion="2.3.0" condition="CMSIS RTOS2 FreeRTOS CortexA">
888889
<description>CMSIS-RTOS2 implementation for Cortex-A based on FreeRTOS</description>
889890
<RTE_Components_h>
890891
#define RTE_CMSIS_RTOS2 /* CMSIS-RTOS2 */

CMSIS/RTOS2/FreeRTOS/Config/ARMCM/FreeRTOSConfig.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,27 @@
271271
#define configMINIMAL_SECURE_STACK_SIZE ((uint32_t)128)
272272
// </h>
273273

274+
// <h> Symmetric Multiprocessing Configuration
275+
// <i> Enable and configure FreeRTOS for Symmetric Multiprocessing (SMP).
276+
277+
// <q>Number of processor cores
278+
// <i> Sets the number of available processor cores.
279+
// <i> Default: 1
280+
#define configNUMBER_OF_CORES 1
281+
282+
// <q>Use processor core affinity
283+
// <i> Enables the control for task to run on specific processor cores.
284+
// <i> Task that has no processor affinity set may run on any available core.
285+
// <i> Default: 0
286+
#define configUSE_CORE_AFFINITY 0
287+
288+
// <q>Use passive idle hook
289+
// <i> Enable callback function call on each idle task iteration.
290+
// <i> Callback function vApplicationPassiveIdleHook implementation is required when idle hook is enabled.
291+
// <i> Default: 0
292+
#define configUSE_PASSIVE_IDLE_HOOK 0
293+
// </h>
294+
274295
//------------- <<< end of configuration section >>> ---------------------------
275296

276297
/* Defines needed by FreeRTOS to implement CMSIS RTOS2 API. Do not change! */

CMSIS/RTOS2/FreeRTOS/Include/freertos_os2.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* --------------------------------------------------------------------------
2-
* Copyright (c) 2013-2023 Arm Limited. All rights reserved.
2+
* Copyright (c) 2013-2024 Arm Limited. All rights reserved.
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -110,6 +110,12 @@
110110
#define configUSE_OS2_MUTEX configUSE_MUTEXES
111111
#endif
112112

113+
/*
114+
Option to exclude CMSIS-RTOS2 Processor Affinity API functions from the application image.
115+
*/
116+
#ifndef configUSE_OS2_CPU_AFFINITY
117+
#define configUSE_OS2_CPU_AFFINITY configUSE_CORE_AFFINITY
118+
#endif
113119

114120
/*
115121
CMSIS-RTOS2 FreeRTOS configuration check (FreeRTOSConfig.h).
@@ -267,6 +273,26 @@
267273
#endif
268274
#endif
269275

276+
#if (configUSE_CORE_AFFINITY == 0)
277+
/*
278+
CMSIS-RTOS2 Processor Affinity API functions require FreeRTOS kernel support for
279+
Symmetric Multiprocessing (SMP). In case if this functionality is not available
280+
and the functions are not used in the application image, compiler will optimize
281+
them away.
282+
Set #define configUSE_CORE_AFFINITY 1 to fix this error.
283+
Note: SMP is only available when #define configNUMBER_OF_CORES > 1
284+
285+
Alternatively, if the application does not use processor affinity functions they
286+
can be excluded from the image code by setting:
287+
#define configUSE_OS2_CPU_AFFINITY 0 (in FreeRTOSConfig.h)
288+
*/
289+
290+
#if (configUSE_OS2_CPU_AFFINITY == 1)
291+
#error "Definitions configNUMBER_OF_CORES and configUSE_CORE_AFFINITY must equal 1 to implement Processor Affinity API."
292+
#endif
293+
#endif
294+
295+
270296
#if (configUSE_COUNTING_SEMAPHORES == 0)
271297
/*
272298
CMSIS-RTOS2 Memory Pool functions use FreeRTOS function xSemaphoreCreateCounting

CMSIS/RTOS2/FreeRTOS/Source/cmsis_os2.c

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,9 @@ osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAtt
521521
TaskHandle_t hTask;
522522
UBaseType_t prio;
523523
int32_t mem;
524+
#if (configUSE_OS2_CPU_AFFINITY == 1)
525+
UBaseType_t core_aff = tskNO_AFFINITY;
526+
#endif
524527

525528
hTask = NULL;
526529

@@ -561,23 +564,62 @@ osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAtt
561564
mem = 0;
562565
}
563566
}
567+
568+
#if (configUSE_OS2_CPU_AFFINITY == 1)
569+
if (attr->affinity_mask != 0U) {
570+
core_aff = attr->affinity_mask;
571+
}
572+
#endif
564573
}
565574
else {
566575
mem = 0;
567576
}
568577

569578
if (mem == 1) {
570579
#if (configSUPPORT_STATIC_ALLOCATION == 1)
571-
hTask = xTaskCreateStatic ((TaskFunction_t)func, name, stack, argument, prio, (StackType_t *)attr->stack_mem,
572-
(StaticTask_t *)attr->cb_mem);
580+
#if (configUSE_OS2_CPU_AFFINITY == 0)
581+
hTask = xTaskCreateStatic ((TaskFunction_t)func,
582+
name,
583+
stack,
584+
argument,
585+
prio,
586+
(StackType_t *)attr->stack_mem,
587+
(StaticTask_t *)attr->cb_mem);
588+
#else
589+
hTask = xTaskCreateStaticAffinitySet ((TaskFunction_t)func,
590+
name,
591+
stack,
592+
argument,
593+
prio,
594+
(StackType_t *)attr->stack_mem,
595+
(StaticTask_t *)attr->cb_mem,
596+
core_aff);
597+
#endif
573598
#endif
574599
}
575600
else {
576601
if (mem == 0) {
577602
#if (configSUPPORT_DYNAMIC_ALLOCATION == 1)
578-
if (xTaskCreate ((TaskFunction_t)func, name, (configSTACK_DEPTH_TYPE)stack, argument, prio, &hTask) != pdPASS) {
579-
hTask = NULL;
580-
}
603+
#if (configUSE_OS2_CPU_AFFINITY == 0)
604+
if (xTaskCreate ((TaskFunction_t )func,
605+
name,
606+
(configSTACK_DEPTH_TYPE)stack,
607+
argument,
608+
prio,
609+
&hTask) != pdPASS) {
610+
hTask = NULL;
611+
}
612+
#else
613+
if (xTaskCreateAffinitySet ((TaskFunction_t )func,
614+
name,
615+
(configSTACK_DEPTH_TYPE)stack,
616+
argument,
617+
prio,
618+
core_aff,
619+
&hTask) != pdPASS) {
620+
hTask = NULL;
621+
}
622+
#endif
581623
#endif
582624
}
583625
}
@@ -878,6 +920,50 @@ uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) {
878920
}
879921
#endif /* (configUSE_OS2_THREAD_ENUMERATE == 1) */
880922

923+
#if (configUSE_OS2_CPU_AFFINITY == 1)
924+
/*
925+
Set processor affinity mask of a thread.
926+
*/
927+
osStatus_t osThreadSetAffinityMask (osThreadId_t thread_id, uint32_t affinity_mask) {
928+
TaskHandle_t hTask = (TaskHandle_t)thread_id;
929+
osStatus_t stat;
930+
931+
if (IRQ_Context() != 0U) {
932+
stat = osErrorISR;
933+
}
934+
else if (hTask == NULL) {
935+
stat = osErrorParameter;
936+
}
937+
else {
938+
stat = osOK;
939+
vTaskCoreAffinitySet (hTask, (UBaseType_t)affinity_mask);
940+
}
941+
942+
/* Return execution status */
943+
return (stat);
944+
}
945+
946+
/*
947+
Get current processor affinity mask of a thread.
948+
*/
949+
uint32_t osThreadGetAffinityMask (osThreadId_t thread_id) {
950+
TaskHandle_t hTask = (TaskHandle_t)thread_id;
951+
UBaseType_t affinity_mask;
952+
953+
if (IRQ_Context() != 0U) {
954+
affinity_mask = 0U;
955+
}
956+
else if (hTask == NULL) {
957+
affinity_mask = 0U;
958+
}
959+
else {
960+
affinity_mask = vTaskCoreAffinityGet (hTask);
961+
}
962+
963+
/* Return current processor affinity mask */
964+
return ((uint32_t)affinity_mask);
965+
}
966+
#endif /* (configUSE_OS2_CPU_AFFINITY == 1) */
881967

882968
/* ==== Thread Flags Functions ==== */
883969

0 commit comments

Comments
 (0)