Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | /* * Copyright (c) 2018 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <kernel_structs.h> #include "wrapper.h" #define DONT_CARE (0) #define NSEC_PER_MSEC (NSEC_PER_USEC * USEC_PER_MSEC) /** * @brief Set the specified Thread Flags of a thread. */ uint32_t osThreadFlagsSet(osThreadId_t thread_id, uint32_t flags) { int key; struct cv2_thread *tid = (struct cv2_thread *)thread_id; if ((thread_id == NULL) || (is_cmsis_rtos_v2_thread(thread_id) == NULL) || (flags & 0x80000000)) { return osFlagsErrorParameter; } key = irq_lock(); tid->signal_results |= flags; irq_unlock(key); k_poll_signal_raise(&tid->poll_signal, DONT_CARE); return tid->signal_results; } /** * @brief Get the current Thread Flags of current running thread. */ uint32_t osThreadFlagsGet(void) { struct cv2_thread *tid; if (k_is_in_isr()) { return 0; } tid = (struct cv2_thread *)osThreadGetId(); if (tid == NULL) { return 0; } else { return tid->signal_results; } } /** * @brief Clear the specified Thread Flags of current running thread. */ uint32_t osThreadFlagsClear(uint32_t flags) { struct cv2_thread *tid; int sig, key; if (k_is_in_isr()) { return osFlagsErrorUnknown; } if (flags & 0x80000000) { return osFlagsErrorParameter; } tid = (struct cv2_thread *)osThreadGetId(); if (tid == NULL) { return osFlagsErrorUnknown; } key = irq_lock(); sig = tid->signal_results; tid->signal_results &= ~(flags); irq_unlock(key); return sig; } /** * @brief Wait for one or more Thread Flags of the current running thread to * become signalled. */ uint32_t osThreadFlagsWait(uint32_t flags, uint32_t options, uint32_t timeout) { struct cv2_thread *tid; int retval, key; u32_t sig; u32_t time_delta_ms, timeout_ms = __ticks_to_ms(timeout); u64_t time_stamp_start, hwclk_cycles_delta, time_delta_ns; if (k_is_in_isr()) { return osFlagsErrorUnknown; } if (flags & 0x80000000) { return osFlagsErrorParameter; } tid = (struct cv2_thread *)osThreadGetId(); if (tid == NULL) { return osFlagsErrorUnknown; } for (;;) { time_stamp_start = (u64_t)k_cycle_get_32(); switch (timeout) { case 0: retval = k_poll(&tid->poll_event, 1, K_NO_WAIT); break; case osWaitForever: retval = k_poll(&tid->poll_event, 1, K_FOREVER); break; default: retval = k_poll(&tid->poll_event, 1, timeout_ms); break; } switch (retval) { case 0: break; case -EAGAIN: return osFlagsErrorTimeout; default: return osFlagsErrorUnknown; } __ASSERT(tid->poll_event.state == K_POLL_STATE_SIGNALED, "event state not signalled!"); __ASSERT(tid->poll_event.signal->signaled == 1U, "event signaled is not 1"); /* Reset the states to facilitate the next trigger */ tid->poll_event.signal->signaled = 0U; tid->poll_event.state = K_POLL_STATE_NOT_READY; if (options & osFlagsWaitAll) { /* Check if all events we are waiting on have * been signalled */ if ((tid->signal_results & flags) == flags) { break; } /* If we need to wait on more signals, we need to * adjust the timeout value accordingly based on * the time that has already elapsed. */ hwclk_cycles_delta = (u64_t)k_cycle_get_32() - time_stamp_start; time_delta_ns = (u32_t)SYS_CLOCK_HW_CYCLES_TO_NS(hwclk_cycles_delta); time_delta_ms = (u32_t)time_delta_ns / NSEC_PER_MSEC; if (timeout_ms > time_delta_ms) { timeout_ms -= time_delta_ms; } else { timeout_ms = 0U; } } else { break; } } sig = tid->signal_results; if (!(options & osFlagsNoClear)) { /* Clear signal flags as the thread is ready now */ key = irq_lock(); tid->signal_results &= ~(flags); irq_unlock(key); } return sig; } |