11/* --------------------------------------------------------------------------
2- * Copyright (c) 2013-2017 Arm Limited. All rights reserved.
2+ * Copyright (c) 2013-2019 Arm Limited. All rights reserved.
33 *
44 * SPDX-License-Identifier: Apache-2.0
55 *
2323#include <stdint.h>
2424#include <stddef.h>
2525
26- #include "cmsis_os2.h"
27- #include "cmsis_compiler.h"
26+ #include "FreeRTOS.h" // ARM.FreeRTOS::RTOS:Core
27+ #include "task.h" // ARM.FreeRTOS::RTOS:Core
28+ #include "semphr.h" // ARM.FreeRTOS::RTOS:Core
2829
2930/* Define the number of Threads which use standard C/C++ library libspace */
3031#ifndef OS_THREAD_LIBSPACE_NUM
3132 #define OS_THREAD_LIBSPACE_NUM 4
3233#endif
3334
35+ /* Define the number of Mutexes used by standard C/C++ library for stream protection */
36+ #ifndef OS_MUTEX_CLIB_NUM
37+ #define OS_MUTEX_CLIB_NUM 5
38+ #endif
39+
3440/*----------------------------------------------------------------------------*/
3541
36- /* Initialize OS */
42+ /* Initialization after stack and heap setup */
3743#if defined(__CC_ARM ) || (defined(__ARMCC_VERSION ) && (__ARMCC_VERSION >= 6010050 ))
3844
3945#ifndef __MICROLIB
4046__WEAK
4147void _platform_post_stackheap_init (void );
4248void _platform_post_stackheap_init (void ) {
43- osKernelInitialize ();
49+ /* Initialize OS, memory, etc. */
50+ EvrFreeRTOSSetup (0 );
4451}
4552#endif /* __MICROLIB */
4653
4754#elif defined(__GNUC__ )
4855__WEAK
4956void software_init_hook (void );
5057void software_init_hook (void ) {
51- osKernelInitialize ();
58+ /* Initialize OS, memory, etc. */
59+ EvrFreeRTOSSetup (0 );
5260}
5361
5462#endif
@@ -64,14 +72,14 @@ void software_init_hook (void) {
6472static uint32_t os_libspace [OS_THREAD_LIBSPACE_NUM + 1 ][LIBSPACE_SIZE /sizeof (uint32_t )];
6573
6674/* Array of Threads (IDs) using libspace */
67- static osThreadId_t os_libspace_id [OS_THREAD_LIBSPACE_NUM ];
75+ static TaskHandle_t os_libspace_id [OS_THREAD_LIBSPACE_NUM ];
6876
6977/* OS Kernel state checking */
7078static uint32_t os_kernel_is_active (void ) {
7179 static uint8_t os_kernel_active = 0U ;
7280
7381 if (os_kernel_active == 0U ) {
74- if (osKernelGetState () > osKernelReady ) {
82+ if (xTaskGetSchedulerState () != taskSCHEDULER_NOT_STARTED ) {
7583 os_kernel_active = 1U ;
7684 return 1U ;
7785 }
@@ -82,18 +90,22 @@ static uint32_t os_kernel_is_active (void) {
8290}
8391
8492/* Provide libspace for current thread */
93+ void * __user_perthread_libspace (void );
8594void * __user_perthread_libspace (void ) {
86- osThreadId_t id ;
95+ TaskHandle_t id ;
8796 uint32_t n ;
8897
8998 if (!os_kernel_is_active ()) {
9099 return (void * )& os_libspace [OS_THREAD_LIBSPACE_NUM ][0 ];
91100 }
92101
93- id = osThreadGetId ();
102+ id = xTaskGetCurrentTaskHandle ();
103+
94104 for (n = 0U ; n < OS_THREAD_LIBSPACE_NUM ; n ++ ) {
95105 if (os_libspace_id [n ] == NULL ) {
106+
96107 os_libspace_id [n ] = id ;
108+
97109 return (void * )& os_libspace [n ][0 ];
98110 }
99111 if (os_libspace_id [n ] == id ) {
@@ -106,6 +118,11 @@ void *__user_perthread_libspace (void) {
106118
107119/*----------------------------------------------------------------------------*/
108120
121+ #if (OS_MUTEX_CLIB_NUM > 0 )
122+ static StaticSemaphore_t clib_mutex_cb [OS_MUTEX_CLIB_NUM ];
123+ static SemaphoreHandle_t clib_mutex_id [OS_MUTEX_CLIB_NUM ];
124+ #endif
125+
109126/* Define mutex object and function prototypes */
110127typedef void * mutex ;
111128
@@ -117,7 +134,30 @@ __USED void _mutex_free (mutex *m);
117134
118135/* Initialize mutex */
119136int _mutex_initialize (mutex * m ) {
120- * m = osMutexNew (NULL );
137+ #if (OS_MUTEX_CLIB_NUM > 0 )
138+ uint32_t i ;
139+ #endif
140+
141+ * m = NULL ;
142+
143+ #if (OS_MUTEX_CLIB_NUM > 0 )
144+ for (i = 0U ; i < OS_MUTEX_CLIB_NUM ; i ++ ) {
145+ if (clib_mutex_id [i ] == NULL ) {
146+ /* Create mutex using static memory */
147+ clib_mutex_id [i ] = xSemaphoreCreateMutexStatic (& clib_mutex_cb [i ]);
148+
149+ /* Return mutex id */
150+ * m = clib_mutex_id [i ];
151+
152+ return 1 ;
153+ }
154+ }
155+ #endif
156+ if (os_kernel_is_active ()) {
157+ /* Create mutex using dynamic memory */
158+ * m = xSemaphoreCreateMutex ();
159+ }
160+
121161 if (* m == NULL ) {
122162 return 0 ;
123163 }
@@ -126,21 +166,37 @@ int _mutex_initialize(mutex *m) {
126166
127167/* Acquire mutex */
128168void _mutex_acquire (mutex * m ) {
169+
129170 if (os_kernel_is_active ()) {
130- osMutexAcquire (* m , osWaitForever );
171+ xSemaphoreTake (* m , portMAX_DELAY );
131172 }
132173}
133174
134175/* Release mutex */
135176void _mutex_release (mutex * m ) {
177+
136178 if (os_kernel_is_active ()) {
137- osMutexRelease (* m );
179+ xSemaphoreGive (* m );
138180 }
139181}
140182
141- /* // Free mutex */
183+ /* Free mutex */
142184void _mutex_free (mutex * m ) {
143- osMutexDelete (* m );
185+ #if (OS_MUTEX_CLIB_NUM > 0 )
186+ uint32_t i ;
187+ #endif
188+
189+ vSemaphoreDelete (* m );
190+
191+ #if (OS_MUTEX_CLIB_NUM > 0 )
192+ /* Check if mutex was using static memory */
193+ for (i = 0U ; i < OS_MUTEX_CLIB_NUM ; i ++ ) {
194+ if (* m == clib_mutex_id [i ]) {
195+ clib_mutex_id [i ] = NULL ;
196+ break ;
197+ }
198+ }
199+ #endif
144200}
145201
146202#endif
0 commit comments