|
| 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