Skip to content

Commit 086337f

Browse files
Preliminary support for multithreading in IAR (#57)
1 parent a4224ed commit 086337f

File tree

3 files changed

+137
-1
lines changed

3 files changed

+137
-1
lines changed

ARM.CMSIS-Compiler.pdsc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@
147147
<require condition="CORE"/>
148148
<require Cclass="CMSIS" Cgroup="RTOS2"/>
149149
</condition>
150+
<condition id="IAR CORE CMSIS-RTOS2">
151+
<description>IAR and CORE component and CMSIS-RTOS2</description>
152+
<require Tcompiler="IAR"/>
153+
<require condition="CORE"/>
154+
<require Cclass="CMSIS" Cgroup="RTOS2"/>
155+
</condition>
150156
</conditions>
151157
<taxonomy>
152158
<description Cclass="CMSIS-Compiler" doc="documentation/html/index.html">Compiler Specific Interfaces</description>
@@ -450,6 +456,18 @@
450456
<file category="sourceC" name="source/gcc/retarget_lock_rtos2.c"/>
451457
</files>
452458
</component>
459+
<component Cclass="CMSIS-Compiler" Cgroup="OS Interface" Csub="CMSIS-RTOS2" Capiversion="1.0.0" Cversion="2.0.0" condition="IAR CORE CMSIS-RTOS2">
460+
<description>C library OS interface implementation using CMSIS-RTOS2</description>
461+
<RTE_Components_h>
462+
#define RTE_CMSIS_Compiler_OS_Interface /* Compiler OS Interface */
463+
#define RTE_CMSIS_Compiler_OS_Interface_RTOS2 /* Compiler OS Interface: CMSIS-RTOS2 */
464+
/* Features */
465+
#define RTE_CMSIS_Compiler_OS_Interface_RTOS2_LOCKS /* Implements locking routines */
466+
</RTE_Components_h>
467+
<files>
468+
<file category="sourceC" name="source/iar/retarget_os_rtos2.c"/>
469+
</files>
470+
</component>
453471
</components>
454472

455473
<examples>

example/build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def image_ext(self):
2525
ext = {
2626
CompilerAxis.AC6: 'axf',
2727
CompilerAxis.GCC: 'elf',
28-
CompilerAxis.IAR: 'elf',
28+
CompilerAxis.IAR: 'out',
2929
CompilerAxis.CLANG: 'elf',
3030
}
3131
return ext[self]

source/iar/retarget_os_rtos2.c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright (C) 2023 ARM Limited or its affiliates. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the License); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
14+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "cmsis_os2.h"
20+
#include "cmsis_compiler.h"
21+
22+
/* Check if the kernel has been initialized */
23+
static uint32_t os_kernel_is_initialized (void) {
24+
if (osKernelGetState() > osKernelInactive) {
25+
return 1U;
26+
} else {
27+
return 0U;
28+
}
29+
}
30+
31+
/* Check if the kernel is running */
32+
static uint32_t os_kernel_is_running (void) {
33+
if (osKernelGetState() == osKernelRunning) {
34+
return 1U;
35+
} else {
36+
return 0U;
37+
}
38+
}
39+
40+
/* Check if processor is in Thread or Handler mode */
41+
static uint32_t is_thread_mode (void) {
42+
if (__get_IPSR() == 0U) {
43+
return 1U; /* Thread mode */
44+
} else {
45+
return 0U; /* Handler mode */
46+
}
47+
}
48+
49+
/* Define retarget mutex structure to hold mutex identifier */
50+
struct rt_mutex_s {
51+
osMutexId_t id;
52+
};
53+
54+
#pragma language=save
55+
#pragma language=extended
56+
/* Initialize mutex */
57+
__USED void __iar_system_Mtxinit(struct rt_mutex_s *mutex)
58+
{
59+
if (os_kernel_is_initialized()) {
60+
mutex->id = osMutexNew(NULL);
61+
}
62+
}
63+
64+
/* Acquire mutex */
65+
__USED void __iar_system_Mtxlock(struct rt_mutex_s *mutex)
66+
{
67+
if (os_kernel_is_running() && is_thread_mode()) {
68+
(void)osMutexAcquire(mutex->id, osWaitForever);
69+
}
70+
}
71+
72+
/* Release mutex */
73+
__USED void __iar_system_Mtxunlock(struct rt_mutex_s *mutex) // Unlock a system lock
74+
{
75+
if (os_kernel_is_running() && is_thread_mode()) {
76+
(void)osMutexRelease(mutex->id);
77+
}
78+
}
79+
80+
/* Free mutex */
81+
__USED void __iar_system_Mtxdst(struct rt_mutex_s *mutex) // Destroy a system lock
82+
{
83+
(void)osMutexDelete(mutex->id);
84+
}
85+
86+
//#endif //defined(_DLIB_THREAD_SUPPORT) && _DLIB_THREAD_SUPPORT > 0
87+
88+
/* Initialize mutex */
89+
__USED void __iar_file_Mtxinit(struct rt_mutex_s *mutex)
90+
{
91+
if (os_kernel_is_initialized()) {
92+
mutex->id = osMutexNew(NULL);
93+
}
94+
}
95+
96+
/* Acquire mutex */
97+
__USED void __iar_file_Mtxlock(struct rt_mutex_s *mutex)
98+
{
99+
if (os_kernel_is_running() && is_thread_mode()) {
100+
(void)osMutexAcquire(mutex->id, osWaitForever);
101+
}
102+
}
103+
104+
/* Release mutex */
105+
__USED void __iar_file_Mtxunlock(struct rt_mutex_s *mutex) // Unlock a system lock
106+
{
107+
if (os_kernel_is_running() && is_thread_mode()) {
108+
(void)osMutexRelease(mutex->id);
109+
}
110+
}
111+
112+
/* Free mutex */
113+
__USED void __iar_file_Mtxdst(struct rt_mutex_s *mutex) // Destroy a system lock
114+
{
115+
(void)osMutexDelete(mutex->id);
116+
}
117+
118+
#pragma language=restore

0 commit comments

Comments
 (0)