Antonio de Angelis | a54ed7e | 2017-11-29 13:37:58 +0000 | [diff] [blame] | 1 | /* |
Jamie Fox | 440cd89 | 2018-02-26 15:43:23 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018, Arm Limited. All rights reserved. |
Antonio de Angelis | a54ed7e | 2017-11-29 13:37:58 +0000 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | uint32_t os_wrapper_new_thread(const char* name, uint32_t stack_size, |
Jamie Fox | 96dfe7b | 2018-02-05 14:27:51 +0000 | [diff] [blame^] | 18 | os_wrapper_thread_func func, void *arg, |
| 19 | uint32_t priority) |
Antonio de Angelis | a54ed7e | 2017-11-29 13:37:58 +0000 | [diff] [blame] | 20 | { |
| 21 | osThreadAttr_t task_attribs = {.tz_module = 1}; |
| 22 | osThreadId_t thread_id; |
| 23 | |
Jamie Fox | 440cd89 | 2018-02-26 15:43:23 +0000 | [diff] [blame] | 24 | task_attribs.attr_bits = osThreadJoinable; |
Antonio de Angelis | a54ed7e | 2017-11-29 13:37:58 +0000 | [diff] [blame] | 25 | task_attribs.stack_size = stack_size; |
| 26 | task_attribs.name = name; |
| 27 | task_attribs.priority = priority; |
| 28 | |
Jamie Fox | 96dfe7b | 2018-02-05 14:27:51 +0000 | [diff] [blame^] | 29 | thread_id = osThreadNew(func, arg, &task_attribs); |
Antonio de Angelis | a54ed7e | 2017-11-29 13:37:58 +0000 | [diff] [blame] | 30 | if (thread_id == NULL) { |
| 31 | return OS_WRAPPER_ERROR; |
| 32 | } |
| 33 | |
| 34 | return (uint32_t)thread_id; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | uint32_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 | |
| 54 | uint32_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 | |
| 66 | uint32_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 | |
| 78 | uint32_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 | |
| 90 | uint32_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 | |
| 102 | uint32_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 Fox | 440cd89 | 2018-02-26 15:43:23 +0000 | [diff] [blame] | 114 | uint32_t os_wrapper_join_thread(uint32_t id) |
Antonio de Angelis | a54ed7e | 2017-11-29 13:37:58 +0000 | [diff] [blame] | 115 | { |
Jamie Fox | 440cd89 | 2018-02-26 15:43:23 +0000 | [diff] [blame] | 116 | osStatus_t status; |
Antonio de Angelis | a54ed7e | 2017-11-29 13:37:58 +0000 | [diff] [blame] | 117 | |
Jamie Fox | 440cd89 | 2018-02-26 15:43:23 +0000 | [diff] [blame] | 118 | /* 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 Angelis | a54ed7e | 2017-11-29 13:37:58 +0000 | [diff] [blame] | 127 | |
| 128 | return 0; |
| 129 | } |