blob: 9a9a8878986f1c228530d1844d5eec263e27b696 [file] [log] [blame]
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +00001/*
Kevin Peng10a9fea2019-01-28 09:43:45 +08002 * Copyright (c) 2017-2019, Arm Limited. All rights reserved.
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include "test/suites/sst/non_secure/os_wrapper.h"
9
10#include <string.h>
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000011#include "cmsis_os2.h"
12
13/* This is an example OS abstraction layer rtx RTOS for non-secure test
14 * environment */
15
16uint32_t os_wrapper_new_thread(const char* name, uint32_t stack_size,
Jamie Fox96dfe7b2018-02-05 14:27:51 +000017 os_wrapper_thread_func func, void *arg,
18 uint32_t priority)
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000019{
20 osThreadAttr_t task_attribs = {.tz_module = 1};
21 osThreadId_t thread_id;
22
Jamie Fox440cd892018-02-26 15:43:23 +000023 task_attribs.attr_bits = osThreadJoinable;
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000024 task_attribs.stack_size = stack_size;
25 task_attribs.name = name;
TTornblomc640e072019-06-14 14:33:51 +020026 task_attribs.priority = (osPriority_t) priority;
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000027
Jamie Fox96dfe7b2018-02-05 14:27:51 +000028 thread_id = osThreadNew(func, arg, &task_attribs);
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000029 if (thread_id == NULL) {
30 return OS_WRAPPER_ERROR;
31 }
32
33 return (uint32_t)thread_id;
34}
35
36
37uint32_t os_wrapper_semaphore_create(uint32_t max_count, uint32_t initial_count,
38 const char* name)
39{
40 osSemaphoreAttr_t sema_attrib = {0};
41 osSemaphoreId_t semaphore;
42
43 sema_attrib.name = name;
44
45 semaphore = osSemaphoreNew(max_count, initial_count, &sema_attrib);
46 if (semaphore == NULL) {
47 return OS_WRAPPER_ERROR;
48 }
49
50 return (uint32_t)semaphore;
51}
52
53uint32_t os_wrapper_semaphore_acquire(uint32_t semaphore_id, uint32_t timeout)
54{
55 osStatus_t status;
56
57 status = osSemaphoreAcquire((osSemaphoreId_t)semaphore_id, timeout);
58 if (status != osOK) {
59 return OS_WRAPPER_ERROR;
60 }
61
62 return 0;
63}
64
65uint32_t os_wrapper_semaphore_release(uint32_t sema)
66{
67 osStatus_t status;
68
69 status = osSemaphoreRelease((osSemaphoreId_t)sema);
70 if (status != osOK) {
71 return OS_WRAPPER_ERROR;
72 }
73
74 return 0;
75}
76
77uint32_t os_wrapper_semaphore_delete(uint32_t sema)
78{
79 osStatus_t status;
80
81 status = osSemaphoreDelete((osSemaphoreId_t)sema);
82 if (status != osOK) {
83 return OS_WRAPPER_ERROR;
84 }
85
86 return 0;
87}
88
89uint32_t os_wrapper_get_thread_id(void)
90{
91 osThreadId_t thread_id;
92
93 thread_id = osThreadGetId();
94 if(thread_id == NULL) {
95 return OS_WRAPPER_ERROR;
96 }
97
98 return (uint32_t)thread_id;
99}
100
101uint32_t os_wrapper_get_thread_priority(uint32_t id)
102{
103 osPriority_t prio;
104
105 prio = osThreadGetPriority((osThreadId_t)id);
106 if (prio == osPriorityError) {
107 return OS_WRAPPER_ERROR;
108 }
109
110 return prio;
111}
112
Jamie Fox440cd892018-02-26 15:43:23 +0000113uint32_t os_wrapper_join_thread(uint32_t id)
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000114{
Jamie Fox440cd892018-02-26 15:43:23 +0000115 osStatus_t status;
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000116
Jamie Fox440cd892018-02-26 15:43:23 +0000117 /* Wait for the thread to terminate */
118 status = osThreadJoin((osThreadId_t)id);
119 if (status != osOK) {
120 return OS_WRAPPER_ERROR;
121 }
122
123 /* RTX handles thread deletion automatically. So, no action is required in
124 * this function to delete the thread.
125 */
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000126
127 return 0;
128}