rtx_evflags.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. #ifdef __NO_EXCLUSIVE_ACCESS
  33. uint32_t primask = __get_PRIMASK();
  34. #endif
  35. int32_t event_flags;
  36. #ifdef __NO_EXCLUSIVE_ACCESS
  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. #ifdef __NO_EXCLUSIVE_ACCESS
  54. uint32_t primask = __get_PRIMASK();
  55. #endif
  56. int32_t event_flags;
  57. #ifdef __NO_EXCLUSIVE_ACCESS
  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. #ifdef __NO_EXCLUSIVE_ACCESS
  76. uint32_t primask;
  77. #endif
  78. int32_t event_flags;
  79. if ((options & osFlagsAutoClear) != 0U) {
  80. #ifdef __NO_EXCLUSIVE_ACCESS
  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_2(EventFlagsSet, int32_t, osEventFlagsId_t, int32_t)
  135. SVC0_2(EventFlagsClear, int32_t, osEventFlagsId_t, int32_t)
  136. SVC0_1(EventFlagsGet, int32_t, osEventFlagsId_t)
  137. SVC0_4(EventFlagsWait, int32_t, osEventFlagsId_t, int32_t, uint32_t, uint32_t)
  138. SVC0_1(EventFlagsDelete, osStatus_t, osEventFlagsId_t)
  139. /// Create and Initialize an Event Flags object.
  140. /// \note API identical to osEventFlagsNew
  141. osEventFlagsId_t os_svcEventFlagsNew (const osEventFlagsAttr_t *attr) {
  142. os_event_flags_t *ef;
  143. uint8_t flags;
  144. const char *name;
  145. // Process attributes
  146. if (attr != NULL) {
  147. name = attr->name;
  148. ef = attr->cb_mem;
  149. if (ef != NULL) {
  150. if (((uint32_t)ef & 3U) || (attr->cb_size < sizeof(os_event_flags_t))) {
  151. return (osEventFlagsId_t)NULL;
  152. }
  153. } else {
  154. if (attr->cb_size != 0U) {
  155. return (osEventFlagsId_t)NULL;
  156. }
  157. }
  158. } else {
  159. name = NULL;
  160. ef = NULL;
  161. }
  162. // Allocate object memory if not provided
  163. if (ef == NULL) {
  164. if (os_Info.mpi.event_flags != NULL) {
  165. ef = os_MemoryPoolAlloc(os_Info.mpi.event_flags);
  166. } else {
  167. ef = os_MemoryAlloc(os_Info.mem.cb, sizeof(os_event_flags_t));
  168. }
  169. if (ef == NULL) {
  170. return (osEventFlagsId_t)NULL;
  171. }
  172. flags = os_FlagSystemObject;
  173. } else {
  174. flags = 0U;
  175. }
  176. // Initialize control block
  177. ef->id = os_IdEventFlags;
  178. ef->state = os_ObjectActive;
  179. ef->flags = flags;
  180. ef->name = name;
  181. ef->thread_list = NULL;
  182. ef->event_flags = 0;
  183. // Register post ISR processing function
  184. os_Info.post_process.event_flags = os_EventFlagsPostProcess;
  185. return (osEventFlagsId_t)ef;
  186. }
  187. /// Set the specified Event Flags.
  188. /// \note API identical to osEventFlagsSet
  189. int32_t os_svcEventFlagsSet (osEventFlagsId_t ef_id, int32_t flags) {
  190. os_event_flags_t *ef = (os_event_flags_t *)ef_id;
  191. os_thread_t *thread;
  192. os_thread_t *thread_next;
  193. int32_t event_flags;
  194. int32_t event_flags0;
  195. // Check parameters
  196. if ((ef == NULL) ||
  197. (ef->id != os_IdEventFlags)) {
  198. return osErrorParameter;
  199. }
  200. if ((uint32_t)flags & ~((1U << os_EventFlagsLimit) - 1U)) {
  201. return osErrorParameter;
  202. }
  203. // Check object state
  204. if (ef->state == os_ObjectInactive) {
  205. return osErrorResource;
  206. }
  207. // Set Event Flags
  208. event_flags = os_EventFlagsSet(ef, flags);
  209. // Check if Threads are waiting for Event Flags
  210. thread = ef->thread_list;
  211. while (thread != NULL) {
  212. thread_next = thread->thread_next;
  213. event_flags0 = os_EventFlagsCheck(ef, thread->wait_flags, thread->flags_options);
  214. if (event_flags0 > 0) {
  215. if ((thread->flags_options & osFlagsAutoClear) != 0U) {
  216. event_flags = event_flags0 & ~thread->wait_flags;
  217. } else {
  218. event_flags = event_flags0;
  219. }
  220. os_ThreadListRemove(thread);
  221. os_ThreadWaitExit(thread, (uint32_t)event_flags0, false);
  222. }
  223. thread = thread_next;
  224. }
  225. os_ThreadDispatch(NULL);
  226. return event_flags;
  227. }
  228. /// Clear the specified Event Flags.
  229. /// \note API identical to osEventFlagsClear
  230. int32_t os_svcEventFlagsClear (osEventFlagsId_t ef_id, int32_t flags) {
  231. os_event_flags_t *ef = (os_event_flags_t *)ef_id;
  232. // Check parameters
  233. if ((ef == NULL) ||
  234. (ef->id != os_IdEventFlags)) {
  235. return osErrorParameter;
  236. }
  237. if ((uint32_t)flags & ~((1U << os_EventFlagsLimit) - 1U)) {
  238. return osErrorParameter;
  239. }
  240. // Check object state
  241. if (ef->state == os_ObjectInactive) {
  242. return osErrorResource;
  243. }
  244. // Clear Event Flags
  245. return os_EventFlagsClear(ef, flags);
  246. }
  247. /// Get the current Event Flags.
  248. /// \note API identical to osEventFlagsGet
  249. int32_t os_svcEventFlagsGet (osEventFlagsId_t ef_id) {
  250. os_event_flags_t *ef = (os_event_flags_t *)ef_id;
  251. // Check parameters
  252. if ((ef == NULL) ||
  253. (ef->id != os_IdEventFlags)) {
  254. return 0;
  255. }
  256. // Check object state
  257. if (ef->state == os_ObjectInactive) {
  258. return 0;
  259. }
  260. return ef->event_flags;
  261. }
  262. /// Wait for one or more Event Flags to become signaled.
  263. /// \note API identical to osEventFlagsWait
  264. int32_t os_svcEventFlagsWait (osEventFlagsId_t ef_id, int32_t flags, uint32_t options, uint32_t millisec) {
  265. os_event_flags_t *ef = (os_event_flags_t *)ef_id;
  266. os_thread_t *running_thread;
  267. int32_t event_flags;
  268. running_thread = os_ThreadGetRunning();
  269. if (running_thread == NULL) {
  270. return osError;
  271. }
  272. // Check parameters
  273. if ((ef == NULL) ||
  274. (ef->id != os_IdEventFlags)) {
  275. return osErrorParameter;
  276. }
  277. if ((uint32_t)flags & ~((1U << os_EventFlagsLimit) - 1U)) {
  278. return osErrorParameter;
  279. }
  280. // Check object state
  281. if (ef->state == os_ObjectInactive) {
  282. return osErrorResource;
  283. }
  284. // Check Event Flags
  285. event_flags = os_EventFlagsCheck(ef, flags, options);
  286. if (event_flags > 0) {
  287. return event_flags;
  288. }
  289. // Check if timeout is specified
  290. if (millisec != 0U) {
  291. // Store waiting flags and options
  292. running_thread->wait_flags = flags;
  293. running_thread->flags_options = (uint8_t)options;
  294. // Suspend current Thread
  295. os_ThreadListPut((os_object_t*)ef, running_thread);
  296. os_ThreadWaitEnter(os_ThreadWaitingEventFlags, millisec);
  297. return osErrorTimeout;
  298. }
  299. return osErrorResource;
  300. }
  301. /// Delete an Event Flags object.
  302. /// \note API identical to osEventFlagsDelete
  303. osStatus_t os_svcEventFlagsDelete (osEventFlagsId_t ef_id) {
  304. os_event_flags_t *ef = (os_event_flags_t *)ef_id;
  305. os_thread_t *thread;
  306. // Check parameters
  307. if ((ef == NULL) ||
  308. (ef->id != os_IdEventFlags)) {
  309. return osErrorParameter;
  310. }
  311. // Check object state
  312. if (ef->state == os_ObjectInactive) {
  313. return osErrorResource;
  314. }
  315. // Mark object as inactive
  316. ef->state = os_ObjectInactive;
  317. // Unblock waiting threads
  318. if (ef->thread_list != NULL) {
  319. do {
  320. thread = os_ThreadListGet((os_object_t*)ef);
  321. os_ThreadWaitExit(thread, (uint32_t)osErrorResource, false);
  322. } while (ef->thread_list != NULL);
  323. os_ThreadDispatch(NULL);
  324. }
  325. // Free object memory
  326. if (ef->flags & os_FlagSystemObject) {
  327. if (os_Info.mpi.event_flags != NULL) {
  328. os_MemoryPoolFree(os_Info.mpi.event_flags, ef);
  329. } else {
  330. os_MemoryFree(os_Info.mem.cb, ef);
  331. }
  332. }
  333. return osOK;
  334. }
  335. // ==== ISR Calls ====
  336. /// Set the specified Event Flags.
  337. /// \note API identical to osEventFlagsSet
  338. __STATIC_INLINE
  339. int32_t os_isrEventFlagsSet (osEventFlagsId_t ef_id, int32_t flags) {
  340. os_event_flags_t *ef = (os_event_flags_t *)ef_id;
  341. int32_t event_flags;
  342. // Check parameters
  343. if ((ef == NULL) ||
  344. (ef->id != os_IdEventFlags)) {
  345. return osErrorParameter;
  346. }
  347. if ((uint32_t)flags & ~((1U << os_EventFlagsLimit) - 1U)) {
  348. return osErrorParameter;
  349. }
  350. // Check object state
  351. if (ef->state == os_ObjectInactive) {
  352. return osErrorResource;
  353. }
  354. // Set Event Flags
  355. event_flags = os_EventFlagsSet(ef, flags);
  356. // Register post ISR processing
  357. os_PostProcess((os_object_t *)ef);
  358. return event_flags;
  359. }
  360. /// Wait for one or more Event Flags to become signaled.
  361. /// \note API identical to osEventFlagsWait
  362. __STATIC_INLINE
  363. int32_t os_isrEventFlagsWait (osEventFlagsId_t ef_id, int32_t flags, uint32_t options, uint32_t millisec) {
  364. os_event_flags_t *ef = (os_event_flags_t *)ef_id;
  365. int32_t event_flags;
  366. // Check parameters
  367. if ((ef == NULL) ||
  368. (ef->id != os_IdEventFlags)) {
  369. return osErrorParameter;
  370. }
  371. if ((uint32_t)flags & ~((1U << os_EventFlagsLimit) - 1U)) {
  372. return osErrorParameter;
  373. }
  374. if (millisec != 0U) {
  375. return osErrorParameter;
  376. }
  377. // Check object state
  378. if (ef->state == os_ObjectInactive) {
  379. return osErrorResource;
  380. }
  381. // Check Event Flags
  382. event_flags = os_EventFlagsCheck(ef, flags, options);
  383. if (event_flags > 0) {
  384. return event_flags;
  385. }
  386. return osErrorResource;
  387. }
  388. // ==== Public API ====
  389. /// Create and Initialize an Event Flags object.
  390. osEventFlagsId_t osEventFlagsNew (const osEventFlagsAttr_t *attr) {
  391. if (__get_IPSR() != 0U) {
  392. return (osEventFlagsId_t)NULL; // Not allowed in ISR
  393. }
  394. if ((os_KernelGetState() == os_KernelReady) && ((__get_CONTROL() & 1U) == 0U)) {
  395. // Kernel Ready (not running) and in Privileged mode
  396. return os_svcEventFlagsNew(attr);
  397. } else {
  398. return __svcEventFlagsNew(attr);
  399. }
  400. }
  401. /// Set the specified Event Flags.
  402. int32_t osEventFlagsSet (osEventFlagsId_t ef_id, int32_t flags) {
  403. if (__get_IPSR() != 0U) { // in ISR
  404. return os_isrEventFlagsSet(ef_id, flags);
  405. } else { // in Thread
  406. return __svcEventFlagsSet(ef_id, flags);
  407. }
  408. }
  409. /// Clear the specified Event Flags.
  410. int32_t osEventFlagsClear (osEventFlagsId_t ef_id, int32_t flags) {
  411. if (__get_IPSR() != 0U) { // in ISR
  412. return os_svcEventFlagsClear(ef_id, flags);
  413. } else { // in Thread
  414. return __svcEventFlagsClear(ef_id, flags);
  415. }
  416. }
  417. /// Get the current Event Flags.
  418. int32_t osEventFlagsGet (osEventFlagsId_t ef_id) {
  419. if (__get_IPSR() != 0U) { // in ISR
  420. return os_svcEventFlagsGet(ef_id);
  421. } else { // in Thread
  422. return __svcEventFlagsGet(ef_id);
  423. }
  424. }
  425. /// Wait for one or more Event Flags to become signaled.
  426. int32_t osEventFlagsWait (osEventFlagsId_t ef_id, int32_t flags, uint32_t options, uint32_t millisec) {
  427. if (__get_IPSR() != 0U) { // in ISR
  428. return os_isrEventFlagsWait(ef_id, flags, options, millisec);
  429. } else { // in Thread
  430. return __svcEventFlagsWait(ef_id, flags, options, millisec);
  431. }
  432. }
  433. /// Delete an Event Flags object.
  434. osStatus_t osEventFlagsDelete (osEventFlagsId_t ef_id) {
  435. if (__get_IPSR() != 0U) {
  436. return osErrorISR; // Not allowed in ISR
  437. }
  438. return __svcEventFlagsDelete(ef_id);
  439. }