rtx_evflags.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. * Copyright (c) 2013-2016 ARM Limited. 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. * http://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. *
  20. * Project: CMSIS-RTOS RTX
  21. * Title: Event Flags functions
  22. *
  23. * -----------------------------------------------------------------------------
  24. */
  25. #include "rtx_lib.h"
  26. // ==== Helper functions ====
  27. /// Set Event Flags.
  28. /// \param[in] ef event flags object.
  29. /// \param[in] flags specifies the flags to set.
  30. /// \return event flags after setting.
  31. static int32_t os_EventFlagsSet (os_event_flags_t *ef, int32_t flags) {
  32. #if (__EXCLUSIVE_ACCESS == 0U)
  33. uint32_t primask = __get_PRIMASK();
  34. #endif
  35. int32_t event_flags;
  36. #if (__EXCLUSIVE_ACCESS == 0U)
  37. __disable_irq();
  38. ef->event_flags |= flags;
  39. event_flags = ef->event_flags;
  40. if (primask == 0U) {
  41. __enable_irq();
  42. }
  43. #else
  44. event_flags = (int32_t)os_exc_set32((uint32_t *)&ef->event_flags, (uint32_t)flags);
  45. #endif
  46. return event_flags;
  47. }
  48. /// Clear Event Flags.
  49. /// \param[in] ef event flags object.
  50. /// \param[in] flags specifies the flags to clear.
  51. /// \return event flags before clearing.
  52. static int32_t os_EventFlagsClear (os_event_flags_t *ef, int32_t flags) {
  53. #if (__EXCLUSIVE_ACCESS == 0U)
  54. uint32_t primask = __get_PRIMASK();
  55. #endif
  56. int32_t event_flags;
  57. #if (__EXCLUSIVE_ACCESS == 0U)
  58. __disable_irq();
  59. event_flags = ef->event_flags;
  60. ef->event_flags &= ~flags;
  61. if (primask == 0U) {
  62. __enable_irq();
  63. }
  64. #else
  65. event_flags = (int32_t)os_exc_clr32((uint32_t *)&ef->event_flags, (uint32_t)flags);
  66. #endif
  67. return event_flags;
  68. }
  69. /// Check Event Flags.
  70. /// \param[in] ef event flags object.
  71. /// \param[in] flags specifies the flags to check.
  72. /// \param[in] options specifies flags options (osFlagsXxxx).
  73. /// \return event flags before clearing or 0 if specified flags have not been set.
  74. static int32_t os_EventFlagsCheck (os_event_flags_t *ef, int32_t flags, uint32_t options) {
  75. #if (__EXCLUSIVE_ACCESS == 0U)
  76. uint32_t primask;
  77. #endif
  78. int32_t event_flags;
  79. if ((options & osFlagsNoClear) == 0U) {
  80. #if (__EXCLUSIVE_ACCESS == 0U)
  81. primask = __get_PRIMASK();
  82. __disable_irq();
  83. event_flags = ef->event_flags;
  84. if ((((options & osFlagsWaitAll) != 0U) && ((event_flags & flags) != flags)) ||
  85. (((options & osFlagsWaitAll) == 0U) && ((event_flags & flags) == 0))) {
  86. event_flags = 0;
  87. } else {
  88. ef->event_flags &= ~flags;
  89. }
  90. if (primask == 0U) {
  91. __enable_irq();
  92. }
  93. #else
  94. if ((options & osFlagsWaitAll) != 0U) {
  95. event_flags = (int32_t)os_exc_chk32_all((uint32_t *)&ef->event_flags, (uint32_t)flags);
  96. } else {
  97. event_flags = (int32_t)os_exc_chk32_any((uint32_t *)&ef->event_flags, (uint32_t)flags);
  98. }
  99. #endif
  100. } else {
  101. event_flags = ef->event_flags;
  102. if ((((options & osFlagsWaitAll) != 0U) && ((event_flags & flags) != flags)) ||
  103. (((options & osFlagsWaitAll) == 0U) && ((event_flags & flags) == 0))) {
  104. event_flags = 0;
  105. }
  106. }
  107. return event_flags;
  108. }
  109. // ==== Library functions ====
  110. /// Event Flags post ISR processing.
  111. /// \param[in] ef event flags object.
  112. void os_EventFlagsPostProcess (os_event_flags_t *ef) {
  113. os_thread_t *thread;
  114. os_thread_t *thread_next;
  115. int32_t event_flags;
  116. if (ef->state == os_ObjectInactive) {
  117. return;
  118. }
  119. // Check if Threads are waiting for Event Flags
  120. thread = ef->thread_list;
  121. while (thread != NULL) {
  122. thread_next = thread->thread_next;
  123. event_flags = os_EventFlagsCheck(ef, thread->wait_flags, thread->flags_options);
  124. if (event_flags > 0) {
  125. os_ThreadListRemove(thread);
  126. os_ThreadWaitExit(thread, (uint32_t)event_flags, false);
  127. }
  128. thread = thread_next;
  129. }
  130. }
  131. // ==== Service Calls ====
  132. // Service Calls definitions
  133. SVC0_1(EventFlagsNew, osEventFlagsId_t, const osEventFlagsAttr_t *)
  134. SVC0_1(EventFlagsGetName, const char *, osEventFlagsId_t)
  135. SVC0_2(EventFlagsSet, int32_t, osEventFlagsId_t, int32_t)
  136. SVC0_2(EventFlagsClear, int32_t, osEventFlagsId_t, int32_t)
  137. SVC0_1(EventFlagsGet, int32_t, osEventFlagsId_t)
  138. SVC0_4(EventFlagsWait, int32_t, osEventFlagsId_t, int32_t, uint32_t, uint32_t)
  139. SVC0_1(EventFlagsDelete, osStatus_t, osEventFlagsId_t)
  140. /// Create and Initialize an Event Flags object.
  141. /// \note API identical to osEventFlagsNew
  142. osEventFlagsId_t os_svcEventFlagsNew (const osEventFlagsAttr_t *attr) {
  143. os_event_flags_t *ef;
  144. uint8_t flags;
  145. const char *name;
  146. // Process attributes
  147. if (attr != NULL) {
  148. name = attr->name;
  149. ef = attr->cb_mem;
  150. if (ef != NULL) {
  151. if (((uint32_t)ef & 3U) || (attr->cb_size < sizeof(os_event_flags_t))) {
  152. return NULL;
  153. }
  154. } else {
  155. if (attr->cb_size != 0U) {
  156. return NULL;
  157. }
  158. }
  159. } else {
  160. name = NULL;
  161. ef = NULL;
  162. }
  163. // Allocate object memory if not provided
  164. if (ef == NULL) {
  165. if (os_Info.mpi.event_flags != NULL) {
  166. ef = os_MemoryPoolAlloc(os_Info.mpi.event_flags);
  167. } else {
  168. ef = os_MemoryAlloc(os_Info.mem.common, sizeof(os_event_flags_t), 1U);
  169. }
  170. if (ef == NULL) {
  171. return NULL;
  172. }
  173. flags = os_FlagSystemObject;
  174. } else {
  175. flags = 0U;
  176. }
  177. // Initialize control block
  178. ef->id = os_IdEventFlags;
  179. ef->state = os_ObjectActive;
  180. ef->flags = flags;
  181. ef->name = name;
  182. ef->thread_list = NULL;
  183. ef->event_flags = 0;
  184. // Register post ISR processing function
  185. os_Info.post_process.event_flags = os_EventFlagsPostProcess;
  186. return ef;
  187. }
  188. /// Get name of an Event Flags object.
  189. /// \note API identical to osEventFlagsGetName
  190. const char *os_svcEventFlagsGetName (osEventFlagsId_t ef_id) {
  191. os_event_flags_t *ef = (os_event_flags_t *)ef_id;
  192. // Check parameters
  193. if ((ef == NULL) ||
  194. (ef->id != os_IdEventFlags)) {
  195. return NULL;
  196. }
  197. // Check object state
  198. if (ef->state == os_ObjectInactive) {
  199. return NULL;
  200. }
  201. return ef->name;
  202. }
  203. /// Set the specified Event Flags.
  204. /// \note API identical to osEventFlagsSet
  205. int32_t os_svcEventFlagsSet (osEventFlagsId_t ef_id, int32_t flags) {
  206. os_event_flags_t *ef = (os_event_flags_t *)ef_id;
  207. os_thread_t *thread;
  208. os_thread_t *thread_next;
  209. int32_t event_flags;
  210. int32_t event_flags0;
  211. // Check parameters
  212. if ((ef == NULL) ||
  213. (ef->id != os_IdEventFlags)) {
  214. return osErrorParameter;
  215. }
  216. if ((uint32_t)flags & ~((1U << os_EventFlagsLimit) - 1U)) {
  217. return osErrorParameter;
  218. }
  219. // Check object state
  220. if (ef->state == os_ObjectInactive) {
  221. return osErrorResource;
  222. }
  223. // Set Event Flags
  224. event_flags = os_EventFlagsSet(ef, flags);
  225. // Check if Threads are waiting for Event Flags
  226. thread = ef->thread_list;
  227. while (thread != NULL) {
  228. thread_next = thread->thread_next;
  229. event_flags0 = os_EventFlagsCheck(ef, thread->wait_flags, thread->flags_options);
  230. if (event_flags0 > 0) {
  231. if ((thread->flags_options & osFlagsNoClear) == 0U) {
  232. event_flags = event_flags0 & ~thread->wait_flags;
  233. } else {
  234. event_flags = event_flags0;
  235. }
  236. os_ThreadListRemove(thread);
  237. os_ThreadWaitExit(thread, (uint32_t)event_flags0, false);
  238. }
  239. thread = thread_next;
  240. }
  241. os_ThreadDispatch(NULL);
  242. return event_flags;
  243. }
  244. /// Clear the specified Event Flags.
  245. /// \note API identical to osEventFlagsClear
  246. int32_t os_svcEventFlagsClear (osEventFlagsId_t ef_id, int32_t flags) {
  247. os_event_flags_t *ef = (os_event_flags_t *)ef_id;
  248. // Check parameters
  249. if ((ef == NULL) ||
  250. (ef->id != os_IdEventFlags)) {
  251. return osErrorParameter;
  252. }
  253. if ((uint32_t)flags & ~((1U << os_EventFlagsLimit) - 1U)) {
  254. return osErrorParameter;
  255. }
  256. // Check object state
  257. if (ef->state == os_ObjectInactive) {
  258. return osErrorResource;
  259. }
  260. // Clear Event Flags
  261. return os_EventFlagsClear(ef, flags);
  262. }
  263. /// Get the current Event Flags.
  264. /// \note API identical to osEventFlagsGet
  265. int32_t os_svcEventFlagsGet (osEventFlagsId_t ef_id) {
  266. os_event_flags_t *ef = (os_event_flags_t *)ef_id;
  267. // Check parameters
  268. if ((ef == NULL) ||
  269. (ef->id != os_IdEventFlags)) {
  270. return 0;
  271. }
  272. // Check object state
  273. if (ef->state == os_ObjectInactive) {
  274. return 0;
  275. }
  276. return ef->event_flags;
  277. }
  278. /// Wait for one or more Event Flags to become signaled.
  279. /// \note API identical to osEventFlagsWait
  280. int32_t os_svcEventFlagsWait (osEventFlagsId_t ef_id, int32_t flags, uint32_t options, uint32_t timeout) {
  281. os_event_flags_t *ef = (os_event_flags_t *)ef_id;
  282. os_thread_t *running_thread;
  283. int32_t event_flags;
  284. running_thread = os_ThreadGetRunning();
  285. if (running_thread == NULL) {
  286. return osError;
  287. }
  288. // Check parameters
  289. if ((ef == NULL) ||
  290. (ef->id != os_IdEventFlags)) {
  291. return osErrorParameter;
  292. }
  293. if ((uint32_t)flags & ~((1U << os_EventFlagsLimit) - 1U)) {
  294. return osErrorParameter;
  295. }
  296. // Check object state
  297. if (ef->state == os_ObjectInactive) {
  298. return osErrorResource;
  299. }
  300. // Check Event Flags
  301. event_flags = os_EventFlagsCheck(ef, flags, options);
  302. if (event_flags > 0) {
  303. return event_flags;
  304. }
  305. // Check if timeout is specified
  306. if (timeout != 0U) {
  307. // Store waiting flags and options
  308. running_thread->wait_flags = flags;
  309. running_thread->flags_options = (uint8_t)options;
  310. // Suspend current Thread
  311. os_ThreadListPut((os_object_t*)ef, running_thread);
  312. os_ThreadWaitEnter(os_ThreadWaitingEventFlags, timeout);
  313. return osErrorTimeout;
  314. }
  315. return osErrorResource;
  316. }
  317. /// Delete an Event Flags object.
  318. /// \note API identical to osEventFlagsDelete
  319. osStatus_t os_svcEventFlagsDelete (osEventFlagsId_t ef_id) {
  320. os_event_flags_t *ef = (os_event_flags_t *)ef_id;
  321. os_thread_t *thread;
  322. // Check parameters
  323. if ((ef == NULL) ||
  324. (ef->id != os_IdEventFlags)) {
  325. return osErrorParameter;
  326. }
  327. // Check object state
  328. if (ef->state == os_ObjectInactive) {
  329. return osErrorResource;
  330. }
  331. // Mark object as inactive
  332. ef->state = os_ObjectInactive;
  333. // Unblock waiting threads
  334. if (ef->thread_list != NULL) {
  335. do {
  336. thread = os_ThreadListGet((os_object_t*)ef);
  337. os_ThreadWaitExit(thread, (uint32_t)osErrorResource, false);
  338. } while (ef->thread_list != NULL);
  339. os_ThreadDispatch(NULL);
  340. }
  341. // Free object memory
  342. if (ef->flags & os_FlagSystemObject) {
  343. if (os_Info.mpi.event_flags != NULL) {
  344. os_MemoryPoolFree(os_Info.mpi.event_flags, ef);
  345. } else {
  346. os_MemoryFree(os_Info.mem.common, ef);
  347. }
  348. }
  349. return osOK;
  350. }
  351. // ==== ISR Calls ====
  352. /// Set the specified Event Flags.
  353. /// \note API identical to osEventFlagsSet
  354. __STATIC_INLINE
  355. int32_t os_isrEventFlagsSet (osEventFlagsId_t ef_id, int32_t flags) {
  356. os_event_flags_t *ef = (os_event_flags_t *)ef_id;
  357. int32_t event_flags;
  358. // Check parameters
  359. if ((ef == NULL) ||
  360. (ef->id != os_IdEventFlags)) {
  361. return osErrorParameter;
  362. }
  363. if ((uint32_t)flags & ~((1U << os_EventFlagsLimit) - 1U)) {
  364. return osErrorParameter;
  365. }
  366. // Check object state
  367. if (ef->state == os_ObjectInactive) {
  368. return osErrorResource;
  369. }
  370. // Set Event Flags
  371. event_flags = os_EventFlagsSet(ef, flags);
  372. // Register post ISR processing
  373. os_PostProcess((os_object_t *)ef);
  374. return event_flags;
  375. }
  376. /// Wait for one or more Event Flags to become signaled.
  377. /// \note API identical to osEventFlagsWait
  378. __STATIC_INLINE
  379. int32_t os_isrEventFlagsWait (osEventFlagsId_t ef_id, int32_t flags, uint32_t options, uint32_t timeout) {
  380. os_event_flags_t *ef = (os_event_flags_t *)ef_id;
  381. int32_t event_flags;
  382. // Check parameters
  383. if ((ef == NULL) ||
  384. (ef->id != os_IdEventFlags)) {
  385. return osErrorParameter;
  386. }
  387. if ((uint32_t)flags & ~((1U << os_EventFlagsLimit) - 1U)) {
  388. return osErrorParameter;
  389. }
  390. if (timeout != 0U) {
  391. return osErrorParameter;
  392. }
  393. // Check object state
  394. if (ef->state == os_ObjectInactive) {
  395. return osErrorResource;
  396. }
  397. // Check Event Flags
  398. event_flags = os_EventFlagsCheck(ef, flags, options);
  399. if (event_flags > 0) {
  400. return event_flags;
  401. }
  402. return osErrorResource;
  403. }
  404. // ==== Public API ====
  405. /// Create and Initialize an Event Flags object.
  406. osEventFlagsId_t osEventFlagsNew (const osEventFlagsAttr_t *attr) {
  407. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  408. return NULL;
  409. }
  410. if ((os_KernelGetState() == os_KernelReady) && IS_PRIVILEGED()) {
  411. // Kernel Ready (not running) and in Privileged mode
  412. return os_svcEventFlagsNew(attr);
  413. } else {
  414. return __svcEventFlagsNew(attr);
  415. }
  416. }
  417. /// Get name of an Event Flags object.
  418. const char *osEventFlagsGetName (osEventFlagsId_t ef_id) {
  419. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  420. return NULL;
  421. }
  422. return __svcEventFlagsGetName(ef_id);
  423. }
  424. /// Set the specified Event Flags.
  425. int32_t osEventFlagsSet (osEventFlagsId_t ef_id, int32_t flags) {
  426. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  427. return os_isrEventFlagsSet(ef_id, flags);
  428. } else {
  429. return __svcEventFlagsSet(ef_id, flags);
  430. }
  431. }
  432. /// Clear the specified Event Flags.
  433. int32_t osEventFlagsClear (osEventFlagsId_t ef_id, int32_t flags) {
  434. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  435. return os_svcEventFlagsClear(ef_id, flags);
  436. } else {
  437. return __svcEventFlagsClear(ef_id, flags);
  438. }
  439. }
  440. /// Get the current Event Flags.
  441. int32_t osEventFlagsGet (osEventFlagsId_t ef_id) {
  442. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  443. return os_svcEventFlagsGet(ef_id);
  444. } else {
  445. return __svcEventFlagsGet(ef_id);
  446. }
  447. }
  448. /// Wait for one or more Event Flags to become signaled.
  449. int32_t osEventFlagsWait (osEventFlagsId_t ef_id, int32_t flags, uint32_t options, uint32_t timeout) {
  450. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  451. return os_isrEventFlagsWait(ef_id, flags, options, timeout);
  452. } else {
  453. return __svcEventFlagsWait(ef_id, flags, options, timeout);
  454. }
  455. }
  456. /// Delete an Event Flags object.
  457. osStatus_t osEventFlagsDelete (osEventFlagsId_t ef_id) {
  458. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  459. return osErrorISR;
  460. }
  461. return __svcEventFlagsDelete(ef_id);
  462. }