blob: b30d29bc7693e43ec99bba9aca67618fac4b1b33 [file] [log] [blame]
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +00001/*
Jamie Fox440cd892018-02-26 15:43:23 +00002 * Copyright (c) 2017-2018, 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>
11#include "cmsis.h"
12#include "cmsis_os2.h"
13
14/* This is an example OS abstraction layer rtx RTOS for non-secure test
15 * environment */
16
17uint32_t os_wrapper_new_thread(const char* name, uint32_t stack_size,
Jamie Fox96dfe7b2018-02-05 14:27:51 +000018 os_wrapper_thread_func func, void *arg,
19 uint32_t priority)
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000020{
21 osThreadAttr_t task_attribs = {.tz_module = 1};
22 osThreadId_t thread_id;
23
Jamie Fox440cd892018-02-26 15:43:23 +000024 task_attribs.attr_bits = osThreadJoinable;
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000025 task_attribs.stack_size = stack_size;
26 task_attribs.name = name;
27 task_attribs.priority = priority;
28
Jamie Fox96dfe7b2018-02-05 14:27:51 +000029 thread_id = osThreadNew(func, arg, &task_attribs);
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +000030 if (thread_id == NULL) {
31 return OS_WRAPPER_ERROR;
32 }
33
34 return (uint32_t)thread_id;
35}
36
37
38uint32_t os_wrapper_semaphore_create(uint32_t max_count, uint32_t initial_count,
39 const char* name)
40{
41 osSemaphoreAttr_t sema_attrib = {0};
42 osSemaphoreId_t semaphore;
43
44 sema_attrib.name = name;
45
46 semaphore = osSemaphoreNew(max_count, initial_count, &sema_attrib);
47 if (semaphore == NULL) {
48 return OS_WRAPPER_ERROR;
49 }
50
51 return (uint32_t)semaphore;
52}
53
54uint32_t os_wrapper_semaphore_acquire(uint32_t semaphore_id, uint32_t timeout)
55{
56 osStatus_t status;
57
58 status = osSemaphoreAcquire((osSemaphoreId_t)semaphore_id, timeout);
59 if (status != osOK) {
60 return OS_WRAPPER_ERROR;
61 }
62
63 return 0;
64}
65
66uint32_t os_wrapper_semaphore_release(uint32_t sema)
67{
68 osStatus_t status;
69
70 status = osSemaphoreRelease((osSemaphoreId_t)sema);
71 if (status != osOK) {
72 return OS_WRAPPER_ERROR;
73 }
74
75 return 0;
76}
77
78uint32_t os_wrapper_semaphore_delete(uint32_t sema)
79{
80 osStatus_t status;
81
82 status = osSemaphoreDelete((osSemaphoreId_t)sema);
83 if (status != osOK) {
84 return OS_WRAPPER_ERROR;
85 }
86
87 return 0;
88}
89
90uint32_t os_wrapper_get_thread_id(void)
91{
92 osThreadId_t thread_id;
93
94 thread_id = osThreadGetId();
95 if(thread_id == NULL) {
96 return OS_WRAPPER_ERROR;
97 }
98
99 return (uint32_t)thread_id;
100}
101
102uint32_t os_wrapper_get_thread_priority(uint32_t id)
103{
104 osPriority_t prio;
105
106 prio = osThreadGetPriority((osThreadId_t)id);
107 if (prio == osPriorityError) {
108 return OS_WRAPPER_ERROR;
109 }
110
111 return prio;
112}
113
Jamie Fox440cd892018-02-26 15:43:23 +0000114uint32_t os_wrapper_join_thread(uint32_t id)
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000115{
Jamie Fox440cd892018-02-26 15:43:23 +0000116 osStatus_t status;
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000117
Jamie Fox440cd892018-02-26 15:43:23 +0000118 /* Wait for the thread to terminate */
119 status = osThreadJoin((osThreadId_t)id);
120 if (status != osOK) {
121 return OS_WRAPPER_ERROR;
122 }
123
124 /* RTX handles thread deletion automatically. So, no action is required in
125 * this function to delete the thread.
126 */
Antonio de Angelisa54ed7e2017-11-29 13:37:58 +0000127
128 return 0;
129}