cmsis_os2.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  1. /* --------------------------------------------------------------------------
  2. * Portions Copyright © 2017 STMicroelectronics International N.V. All rights reserved.
  3. * Portions Copyright (c) 2013-2017 ARM Limited. All rights reserved.
  4. * --------------------------------------------------------------------------
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the License); you may
  9. * not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Name: cmsis_os2.c
  21. * Purpose: CMSIS RTOS2 wrapper for FreeRTOS
  22. *
  23. *---------------------------------------------------------------------------*/
  24. #include <string.h>
  25. #include "cmsis_os2.h" // ::CMSIS:RTOS2
  26. #include "cmsis_compiler.h"
  27. #include "FreeRTOS.h" // ARM.FreeRTOS::RTOS:Core
  28. #include "task.h" // ARM.FreeRTOS::RTOS:Core
  29. #include "event_groups.h" // ARM.FreeRTOS::RTOS:Event Groups
  30. #include "semphr.h" // ARM.FreeRTOS::RTOS:Core
  31. /*---------------------------------------------------------------------------*/
  32. #ifndef __ARM_ARCH_6M__
  33. #define __ARM_ARCH_6M__ 0
  34. #endif
  35. #ifndef __ARM_ARCH_7M__
  36. #define __ARM_ARCH_7M__ 0
  37. #endif
  38. #ifndef __ARM_ARCH_7EM__
  39. #define __ARM_ARCH_7EM__ 0
  40. #endif
  41. #ifndef __ARM_ARCH_8M_MAIN__
  42. #define __ARM_ARCH_8M_MAIN__ 0
  43. #endif
  44. #ifndef __ARM_ARCH_7A__
  45. #define __ARM_ARCH_7A__ 0
  46. #endif
  47. #if ((__ARM_ARCH_7M__ == 1U) || \
  48. (__ARM_ARCH_7EM__ == 1U) || \
  49. (__ARM_ARCH_8M_MAIN__ == 1U))
  50. #define IS_IRQ_MASKED() ((__get_PRIMASK() != 0U) || ((KernelState == osKernelRunning) && (__get_BASEPRI() != 0U)))
  51. #elif (__ARM_ARCH_6M__ == 1U)
  52. #define IS_IRQ_MASKED() ((__get_PRIMASK() != 0U) && (KernelState == osKernelRunning))
  53. #elif (__ARM_ARCH_7A__ == 1)
  54. #define IS_IRQ_MASKED() (0U)
  55. #else
  56. #define IS_IRQ_MASKED() (__get_PRIMASK() != 0U)
  57. #endif
  58. #if (__ARM_ARCH_7A__ == 1U)
  59. /* CPSR mode bitmasks */
  60. #define CPSR_MODE_USER 0x10U
  61. #define CPSR_MODE_SYSTEM 0x1FU
  62. #define IS_IRQ_MODE() ((__get_mode() != CPSR_MODE_USER) && (__get_mode() != CPSR_MODE_SYSTEM))
  63. #else
  64. #define IS_IRQ_MODE() (__get_IPSR() != 0U)
  65. #endif
  66. #define IS_IRQ() (IS_IRQ_MODE() || IS_IRQ_MASKED())
  67. /* Limits */
  68. #define MAX_BITS_TASK_NOTIFY 31U
  69. #define MAX_BITS_EVENT_GROUPS 24U
  70. #define THREAD_FLAGS_INVALID_BITS (~((1UL << MAX_BITS_TASK_NOTIFY) - 1U))
  71. #define EVENT_FLAGS_INVALID_BITS (~((1UL << MAX_BITS_EVENT_GROUPS) - 1U))
  72. /* Kernel version and identification string definition */
  73. #define KERNEL_VERSION (((uint32_t)tskKERNEL_VERSION_MAJOR * 10000000UL) | \
  74. ((uint32_t)tskKERNEL_VERSION_MINOR * 10000UL) | \
  75. ((uint32_t)tskKERNEL_VERSION_BUILD * 1UL))
  76. #define KERNEL_ID "FreeRTOS V10.0.1"
  77. /* Timer callback information structure definition */
  78. typedef struct {
  79. osTimerFunc_t func;
  80. void *arg;
  81. } TimerCallback_t;
  82. /* Kernel initialization state */
  83. static osKernelState_t KernelState;
  84. /* Heap region definition used by heap_5 variant */
  85. #if defined(USE_FreeRTOS_HEAP_5)
  86. #if (configAPPLICATION_ALLOCATED_HEAP == 1)
  87. /*
  88. The application writer has already defined the array used for the RTOS
  89. heap - probably so it can be placed in a special segment or address.
  90. */
  91. extern uint8_t ucHeap[configTOTAL_HEAP_SIZE];
  92. #else
  93. static uint8_t ucHeap[configTOTAL_HEAP_SIZE];
  94. #endif /* configAPPLICATION_ALLOCATED_HEAP */
  95. static HeapRegion_t xHeapRegions[] = {
  96. { ucHeap, configTOTAL_HEAP_SIZE },
  97. { NULL, 0 }
  98. };
  99. #endif /* USE_FreeRTOS_HEAP_5 */
  100. #if defined(SysTick)
  101. /* FreeRTOS tick timer interrupt handler prototype */
  102. extern void xPortSysTickHandler (void);
  103. /*
  104. SysTick handler implementation that also clears overflow flag.
  105. */
  106. void SysTick_Handler (void) {
  107. /* Clear overflow flag */
  108. SysTick->CTRL;
  109. /* Call tick handler */
  110. xPortSysTickHandler();
  111. }
  112. #endif /* SysTick */
  113. /*---------------------------------------------------------------------------*/
  114. osStatus_t osKernelInitialize (void) {
  115. osStatus_t stat;
  116. if (IS_IRQ()) {
  117. stat = osErrorISR;
  118. }
  119. else {
  120. if (KernelState == osKernelInactive) {
  121. #if defined(USE_FreeRTOS_HEAP_5)
  122. vPortDefineHeapRegions (xHeapRegions);
  123. #endif
  124. KernelState = osKernelReady;
  125. stat = osOK;
  126. } else {
  127. stat = osError;
  128. }
  129. }
  130. return (stat);
  131. }
  132. osStatus_t osKernelGetInfo (osVersion_t *version, char *id_buf, uint32_t id_size) {
  133. if (version != NULL) {
  134. version->api = KERNEL_VERSION;
  135. version->kernel = KERNEL_VERSION;
  136. }
  137. if ((id_buf != NULL) && (id_size != 0U)) {
  138. if (id_size > sizeof(KERNEL_ID)) {
  139. id_size = sizeof(KERNEL_ID);
  140. }
  141. memcpy(id_buf, KERNEL_ID, id_size);
  142. }
  143. return (osOK);
  144. }
  145. osKernelState_t osKernelGetState (void) {
  146. osKernelState_t state;
  147. switch (xTaskGetSchedulerState()) {
  148. case taskSCHEDULER_RUNNING:
  149. state = osKernelRunning;
  150. break;
  151. case taskSCHEDULER_SUSPENDED:
  152. state = osKernelLocked;
  153. break;
  154. case taskSCHEDULER_NOT_STARTED:
  155. default:
  156. if (KernelState == osKernelReady) {
  157. state = osKernelReady;
  158. } else {
  159. state = osKernelInactive;
  160. }
  161. break;
  162. }
  163. return (state);
  164. }
  165. osStatus_t osKernelStart (void) {
  166. osStatus_t stat;
  167. if (IS_IRQ()) {
  168. stat = osErrorISR;
  169. }
  170. else {
  171. if (KernelState == osKernelReady) {
  172. KernelState = osKernelRunning;
  173. vTaskStartScheduler();
  174. stat = osOK;
  175. } else {
  176. stat = osError;
  177. }
  178. }
  179. return (stat);
  180. }
  181. int32_t osKernelLock (void) {
  182. int32_t lock;
  183. if (IS_IRQ()) {
  184. lock = (int32_t)osErrorISR;
  185. }
  186. else {
  187. switch (xTaskGetSchedulerState()) {
  188. case taskSCHEDULER_SUSPENDED:
  189. lock = 1;
  190. break;
  191. case taskSCHEDULER_RUNNING:
  192. vTaskSuspendAll();
  193. lock = 0;
  194. break;
  195. case taskSCHEDULER_NOT_STARTED:
  196. default:
  197. lock = (int32_t)osError;
  198. break;
  199. }
  200. }
  201. return (lock);
  202. }
  203. int32_t osKernelUnlock (void) {
  204. int32_t lock;
  205. if (IS_IRQ()) {
  206. lock = (int32_t)osErrorISR;
  207. }
  208. else {
  209. switch (xTaskGetSchedulerState()) {
  210. case taskSCHEDULER_SUSPENDED:
  211. lock = 1;
  212. if (xTaskResumeAll() != pdTRUE) {
  213. if (xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED) {
  214. lock = (int32_t)osError;
  215. }
  216. }
  217. break;
  218. case taskSCHEDULER_RUNNING:
  219. lock = 0;
  220. break;
  221. case taskSCHEDULER_NOT_STARTED:
  222. default:
  223. lock = (int32_t)osError;
  224. break;
  225. }
  226. }
  227. return (lock);
  228. }
  229. int32_t osKernelRestoreLock (int32_t lock) {
  230. if (IS_IRQ()) {
  231. lock = (int32_t)osErrorISR;
  232. }
  233. else {
  234. switch (xTaskGetSchedulerState()) {
  235. case taskSCHEDULER_SUSPENDED:
  236. case taskSCHEDULER_RUNNING:
  237. if (lock == 1) {
  238. vTaskSuspendAll();
  239. }
  240. else {
  241. if (lock != 0) {
  242. lock = (int32_t)osError;
  243. }
  244. else {
  245. if (xTaskResumeAll() != pdTRUE) {
  246. if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
  247. lock = (int32_t)osError;
  248. }
  249. }
  250. }
  251. }
  252. break;
  253. case taskSCHEDULER_NOT_STARTED:
  254. default:
  255. lock = (int32_t)osError;
  256. break;
  257. }
  258. }
  259. return (lock);
  260. }
  261. uint32_t osKernelGetTickCount (void) {
  262. TickType_t ticks;
  263. if (IS_IRQ()) {
  264. ticks = xTaskGetTickCountFromISR();
  265. } else {
  266. ticks = xTaskGetTickCount();
  267. }
  268. return (ticks);
  269. }
  270. uint32_t osKernelGetTickFreq (void) {
  271. return (configTICK_RATE_HZ);
  272. }
  273. uint32_t osKernelGetSysTimerCount (void) {
  274. TickType_t ticks;
  275. uint32_t val;
  276. portDISABLE_INTERRUPTS();
  277. ticks = xTaskGetTickCount();
  278. val = ticks * ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );
  279. portENABLE_INTERRUPTS();
  280. return (val);
  281. }
  282. uint32_t osKernelGetSysTimerFreq (void) {
  283. return (configCPU_CLOCK_HZ);
  284. }
  285. /*---------------------------------------------------------------------------*/
  286. osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr) {
  287. char empty;
  288. const char *name;
  289. uint32_t stack;
  290. TaskHandle_t hTask;
  291. UBaseType_t prio;
  292. int32_t mem;
  293. hTask = NULL;
  294. if (!IS_IRQ() && (func != NULL)) {
  295. stack = configMINIMAL_STACK_SIZE;
  296. prio = (UBaseType_t)osPriorityNormal;
  297. empty = '\0';
  298. name = &empty;
  299. mem = -1;
  300. if (attr != NULL) {
  301. if (attr->name != NULL) {
  302. name = attr->name;
  303. }
  304. if (attr->priority != osPriorityNone) {
  305. prio = (UBaseType_t)attr->priority;
  306. }
  307. if ((prio < osPriorityIdle) || (prio > osPriorityISR) || ((attr->attr_bits & osThreadJoinable) == osThreadJoinable)) {
  308. return (NULL);
  309. }
  310. if (attr->stack_size > 0U) {
  311. /* In FreeRTOS stack is not in bytes, but in sizeof(StackType_t) which is 4 on ARM ports. */
  312. /* Stack size should be therefore 4 byte aligned in order to avoid division caused side effects */
  313. stack = attr->stack_size / sizeof(StackType_t);
  314. }
  315. if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticTask_t)) &&
  316. (attr->stack_mem != NULL) && (attr->stack_size > 0U)) {
  317. mem = 1;
  318. }
  319. else {
  320. if ((attr->cb_mem == NULL) && (attr->cb_size == 0U) && (attr->stack_mem == NULL)) {
  321. mem = 0;
  322. }
  323. }
  324. }
  325. else {
  326. mem = 0;
  327. }
  328. if (mem == 1) {
  329. hTask = xTaskCreateStatic ((TaskFunction_t)func, name, stack, argument, prio, (StackType_t *)attr->stack_mem,
  330. (StaticTask_t *)attr->cb_mem);
  331. }
  332. else {
  333. if (mem == 0) {
  334. if (xTaskCreate ((TaskFunction_t)func, name, (uint16_t)stack, argument, prio, &hTask) != pdPASS) {
  335. hTask = NULL;
  336. }
  337. }
  338. }
  339. }
  340. return ((osThreadId_t)hTask);
  341. }
  342. const char *osThreadGetName (osThreadId_t thread_id) {
  343. TaskHandle_t hTask = (TaskHandle_t)thread_id;
  344. const char *name;
  345. if (IS_IRQ() || (hTask == NULL)) {
  346. name = NULL;
  347. } else {
  348. name = pcTaskGetName (hTask);
  349. }
  350. return (name);
  351. }
  352. osThreadId_t osThreadGetId (void) {
  353. osThreadId_t id;
  354. if (IS_IRQ()) {
  355. id = NULL;
  356. } else {
  357. id = (osThreadId_t)xTaskGetCurrentTaskHandle();
  358. }
  359. return (id);
  360. }
  361. osThreadState_t osThreadGetState (osThreadId_t thread_id) {
  362. TaskHandle_t hTask = (TaskHandle_t)thread_id;
  363. osThreadState_t state;
  364. if (IS_IRQ() || (hTask == NULL)) {
  365. state = osThreadError;
  366. }
  367. else {
  368. switch (eTaskGetState (hTask)) {
  369. case eRunning: state = osThreadRunning; break;
  370. case eReady: state = osThreadReady; break;
  371. case eBlocked:
  372. case eSuspended: state = osThreadBlocked; break;
  373. case eDeleted: state = osThreadTerminated; break;
  374. case eInvalid:
  375. default: state = osThreadError; break;
  376. }
  377. }
  378. return (state);
  379. }
  380. uint32_t osThreadGetStackSpace (osThreadId_t thread_id) {
  381. TaskHandle_t hTask = (TaskHandle_t)thread_id;
  382. uint32_t sz;
  383. if (IS_IRQ() || (hTask == NULL)) {
  384. sz = 0U;
  385. } else {
  386. sz = (uint32_t)uxTaskGetStackHighWaterMark (hTask);
  387. }
  388. return (sz);
  389. }
  390. uint32_t osThreadGetStackSize (osThreadId_t thread_id) {
  391. /*
  392. * this implmentation is not correct.
  393. * this function is implmented to avoid link errors (undefined reference)
  394. * Bug reported : https://github.com/ARM-software/CMSIS-FreeRTOS/issues/14
  395. */
  396. (void) thread_id;
  397. return 0;
  398. }
  399. osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority) {
  400. TaskHandle_t hTask = (TaskHandle_t)thread_id;
  401. osStatus_t stat;
  402. if (IS_IRQ()) {
  403. stat = osErrorISR;
  404. }
  405. else if ((hTask == NULL) || (priority < osPriorityIdle) || (priority > osPriorityISR)) {
  406. stat = osErrorParameter;
  407. }
  408. else {
  409. stat = osOK;
  410. vTaskPrioritySet (hTask, (UBaseType_t)priority);
  411. }
  412. return (stat);
  413. }
  414. osPriority_t osThreadGetPriority (osThreadId_t thread_id) {
  415. TaskHandle_t hTask = (TaskHandle_t)thread_id;
  416. osPriority_t prio;
  417. if (IS_IRQ() || (hTask == NULL)) {
  418. prio = osPriorityError;
  419. } else {
  420. prio = (osPriority_t)uxTaskPriorityGet (hTask);
  421. }
  422. return (prio);
  423. }
  424. osStatus_t osThreadYield (void) {
  425. osStatus_t stat;
  426. if (IS_IRQ()) {
  427. stat = osErrorISR;
  428. } else {
  429. stat = osOK;
  430. taskYIELD();
  431. }
  432. return (stat);
  433. }
  434. osStatus_t osThreadSuspend (osThreadId_t thread_id) {
  435. TaskHandle_t hTask = (TaskHandle_t)thread_id;
  436. osStatus_t stat;
  437. if (IS_IRQ()) {
  438. stat = osErrorISR;
  439. }
  440. else if (hTask == NULL) {
  441. stat = osErrorParameter;
  442. }
  443. else {
  444. stat = osOK;
  445. vTaskSuspend (hTask);
  446. }
  447. return (stat);
  448. }
  449. osStatus_t osThreadResume (osThreadId_t thread_id) {
  450. TaskHandle_t hTask = (TaskHandle_t)thread_id;
  451. osStatus_t stat;
  452. if (IS_IRQ()) {
  453. stat = osErrorISR;
  454. }
  455. else if (hTask == NULL) {
  456. stat = osErrorParameter;
  457. }
  458. else {
  459. stat = osOK;
  460. vTaskResume (hTask);
  461. }
  462. return (stat);
  463. }
  464. __NO_RETURN void osThreadExit (void) {
  465. #ifndef USE_FreeRTOS_HEAP_1
  466. vTaskDelete (NULL);
  467. #endif
  468. for (;;);
  469. }
  470. osStatus_t osThreadTerminate (osThreadId_t thread_id) {
  471. TaskHandle_t hTask = (TaskHandle_t)thread_id;
  472. osStatus_t stat;
  473. #ifndef USE_FreeRTOS_HEAP_1
  474. eTaskState tstate;
  475. if (IS_IRQ()) {
  476. stat = osErrorISR;
  477. }
  478. else if (hTask == NULL) {
  479. stat = osErrorParameter;
  480. }
  481. else {
  482. tstate = eTaskGetState (hTask);
  483. if (tstate != eDeleted) {
  484. stat = osOK;
  485. vTaskDelete (hTask);
  486. } else {
  487. stat = osErrorResource;
  488. }
  489. }
  490. #else
  491. stat = osError;
  492. #endif
  493. return (stat);
  494. }
  495. uint32_t osThreadGetCount (void) {
  496. uint32_t count;
  497. if (IS_IRQ()) {
  498. count = 0U;
  499. } else {
  500. count = uxTaskGetNumberOfTasks();
  501. }
  502. return (count);
  503. }
  504. uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) {
  505. uint32_t i, count;
  506. TaskStatus_t *task;
  507. if (IS_IRQ() || (thread_array == NULL) || (array_items == 0U)) {
  508. count = 0U;
  509. } else {
  510. vTaskSuspendAll();
  511. count = uxTaskGetNumberOfTasks();
  512. task = pvPortMalloc (count * sizeof(TaskStatus_t));
  513. if (task != NULL) {
  514. count = uxTaskGetSystemState (task, count, NULL);
  515. for (i = 0U; (i < count) && (i < array_items); i++) {
  516. thread_array[i] = (osThreadId_t)task[i].xHandle;
  517. }
  518. count = i;
  519. }
  520. (void)xTaskResumeAll();
  521. vPortFree (task);
  522. }
  523. return (count);
  524. }
  525. uint32_t osThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
  526. TaskHandle_t hTask = (TaskHandle_t)thread_id;
  527. uint32_t rflags;
  528. BaseType_t yield;
  529. if ((hTask == NULL) || ((flags & THREAD_FLAGS_INVALID_BITS) != 0U)) {
  530. rflags = (uint32_t)osErrorParameter;
  531. }
  532. else {
  533. rflags = (uint32_t)osError;
  534. if (IS_IRQ()) {
  535. yield = pdFALSE;
  536. (void)xTaskNotifyFromISR (hTask, flags, eSetBits, &yield);
  537. (void)xTaskNotifyAndQueryFromISR (hTask, 0, eNoAction, &rflags, NULL);
  538. portYIELD_FROM_ISR (yield);
  539. }
  540. else {
  541. (void)xTaskNotify (hTask, flags, eSetBits);
  542. (void)xTaskNotifyAndQuery (hTask, 0, eNoAction, &rflags);
  543. }
  544. }
  545. /* Return flags after setting */
  546. return (rflags);
  547. }
  548. uint32_t osThreadFlagsClear (uint32_t flags) {
  549. TaskHandle_t hTask;
  550. uint32_t rflags, cflags;
  551. if (IS_IRQ()) {
  552. rflags = (uint32_t)osErrorISR;
  553. }
  554. else if ((flags & THREAD_FLAGS_INVALID_BITS) != 0U) {
  555. rflags = (uint32_t)osErrorParameter;
  556. }
  557. else {
  558. hTask = xTaskGetCurrentTaskHandle();
  559. if (xTaskNotifyAndQuery (hTask, 0, eNoAction, &cflags) == pdPASS) {
  560. rflags = cflags;
  561. cflags &= ~flags;
  562. if (xTaskNotify (hTask, cflags, eSetValueWithOverwrite) != pdPASS) {
  563. rflags = (uint32_t)osError;
  564. }
  565. }
  566. else {
  567. rflags = (uint32_t)osError;
  568. }
  569. }
  570. /* Return flags before clearing */
  571. return (rflags);
  572. }
  573. uint32_t osThreadFlagsGet (void) {
  574. TaskHandle_t hTask;
  575. uint32_t rflags;
  576. if (IS_IRQ()) {
  577. rflags = (uint32_t)osErrorISR;
  578. }
  579. else {
  580. hTask = xTaskGetCurrentTaskHandle();
  581. if (xTaskNotifyAndQuery (hTask, 0, eNoAction, &rflags) != pdPASS) {
  582. rflags = (uint32_t)osError;
  583. }
  584. }
  585. return (rflags);
  586. }
  587. uint32_t osThreadFlagsWait (uint32_t flags, uint32_t options, uint32_t timeout) {
  588. uint32_t rflags, nval;
  589. uint32_t clear;
  590. TickType_t t0, td, tout;
  591. BaseType_t rval;
  592. if (IS_IRQ()) {
  593. rflags = (uint32_t)osErrorISR;
  594. }
  595. else if ((flags & THREAD_FLAGS_INVALID_BITS) != 0U) {
  596. rflags = (uint32_t)osErrorParameter;
  597. }
  598. else {
  599. if ((options & osFlagsNoClear) == osFlagsNoClear) {
  600. clear = 0U;
  601. } else {
  602. clear = flags;
  603. }
  604. rflags = 0U;
  605. tout = timeout;
  606. t0 = xTaskGetTickCount();
  607. do {
  608. rval = xTaskNotifyWait (0, clear, &nval, tout);
  609. if (rval == pdPASS) {
  610. rflags &= flags;
  611. rflags |= nval;
  612. if ((options & osFlagsWaitAll) == osFlagsWaitAll) {
  613. if ((flags & rflags) == flags) {
  614. break;
  615. } else {
  616. if (timeout == 0U) {
  617. rflags = (uint32_t)osErrorResource;
  618. break;
  619. }
  620. }
  621. }
  622. else {
  623. if ((flags & rflags) != 0) {
  624. break;
  625. } else {
  626. if (timeout == 0U) {
  627. rflags = (uint32_t)osErrorResource;
  628. break;
  629. }
  630. }
  631. }
  632. /* Update timeout */
  633. td = xTaskGetTickCount() - t0;
  634. if (td > tout) {
  635. tout = 0;
  636. } else {
  637. tout -= td;
  638. }
  639. }
  640. else {
  641. if (timeout == 0) {
  642. rflags = (uint32_t)osErrorResource;
  643. } else {
  644. rflags = (uint32_t)osErrorTimeout;
  645. }
  646. }
  647. }
  648. while (rval != pdFAIL);
  649. }
  650. /* Return flags before clearing */
  651. return (rflags);
  652. }
  653. osStatus_t osDelay (uint32_t ticks) {
  654. osStatus_t stat;
  655. if (IS_IRQ()) {
  656. stat = osErrorISR;
  657. }
  658. else {
  659. stat = osOK;
  660. if (ticks != 0U) {
  661. vTaskDelay(ticks);
  662. }
  663. }
  664. return (stat);
  665. }
  666. osStatus_t osDelayUntil (uint32_t ticks) {
  667. TickType_t tcnt;
  668. osStatus_t stat;
  669. if (IS_IRQ()) {
  670. stat = osErrorISR;
  671. }
  672. else {
  673. stat = osOK;
  674. tcnt = xTaskGetTickCount();
  675. vTaskDelayUntil (&tcnt, (TickType_t)(ticks - tcnt));
  676. }
  677. return (stat);
  678. }
  679. /*---------------------------------------------------------------------------*/
  680. static void TimerCallback (TimerHandle_t hTimer) {
  681. TimerCallback_t *callb;
  682. callb = (TimerCallback_t *)pvTimerGetTimerID (hTimer);
  683. if (callb != NULL) {
  684. callb->func (callb->arg);
  685. }
  686. }
  687. osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr) {
  688. const char *name;
  689. TimerHandle_t hTimer;
  690. TimerCallback_t *callb;
  691. UBaseType_t reload;
  692. int32_t mem;
  693. hTimer = NULL;
  694. if (!IS_IRQ() && (func != NULL)) {
  695. /* Allocate memory to store callback function and argument */
  696. callb = pvPortMalloc (sizeof(TimerCallback_t));
  697. if (callb != NULL) {
  698. callb->func = func;
  699. callb->arg = argument;
  700. if (type == osTimerOnce) {
  701. reload = pdFALSE;
  702. } else {
  703. reload = pdTRUE;
  704. }
  705. mem = -1;
  706. name = NULL;
  707. if (attr != NULL) {
  708. if (attr->name != NULL) {
  709. name = attr->name;
  710. }
  711. if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticTimer_t))) {
  712. mem = 1;
  713. }
  714. else {
  715. if ((attr->cb_mem == NULL) && (attr->cb_size == 0U)) {
  716. mem = 0;
  717. }
  718. }
  719. }
  720. else {
  721. mem = 0;
  722. }
  723. if (mem == 1) {
  724. hTimer = xTimerCreateStatic (name, 1, reload, callb, TimerCallback, (StaticTimer_t *)attr->cb_mem);
  725. }
  726. else {
  727. if (mem == 0) {
  728. hTimer = xTimerCreate (name, 1, reload, callb, TimerCallback);
  729. }
  730. }
  731. }
  732. }
  733. return ((osTimerId_t)hTimer);
  734. }
  735. const char *osTimerGetName (osTimerId_t timer_id) {
  736. TimerHandle_t hTimer = (TimerHandle_t)timer_id;
  737. const char *p;
  738. if (IS_IRQ() || (hTimer == NULL)) {
  739. p = NULL;
  740. } else {
  741. p = pcTimerGetName (hTimer);
  742. }
  743. return (p);
  744. }
  745. osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks) {
  746. TimerHandle_t hTimer = (TimerHandle_t)timer_id;
  747. osStatus_t stat;
  748. if (IS_IRQ()) {
  749. stat = osErrorISR;
  750. }
  751. else if (hTimer == NULL) {
  752. stat = osErrorParameter;
  753. }
  754. else {
  755. if (xTimerChangePeriod (hTimer, ticks, 0) == pdPASS) {
  756. stat = osOK;
  757. } else {
  758. stat = osErrorResource;
  759. }
  760. }
  761. return (stat);
  762. }
  763. osStatus_t osTimerStop (osTimerId_t timer_id) {
  764. TimerHandle_t hTimer = (TimerHandle_t)timer_id;
  765. osStatus_t stat;
  766. if (IS_IRQ()) {
  767. stat = osErrorISR;
  768. }
  769. else if (hTimer == NULL) {
  770. stat = osErrorParameter;
  771. }
  772. else {
  773. if (xTimerIsTimerActive (hTimer) == pdFALSE) {
  774. stat = osErrorResource;
  775. }
  776. else {
  777. if (xTimerStop (hTimer, 0) == pdPASS) {
  778. stat = osOK;
  779. } else {
  780. stat = osError;
  781. }
  782. }
  783. }
  784. return (stat);
  785. }
  786. uint32_t osTimerIsRunning (osTimerId_t timer_id) {
  787. TimerHandle_t hTimer = (TimerHandle_t)timer_id;
  788. uint32_t running;
  789. if (IS_IRQ() || (hTimer == NULL)) {
  790. running = 0U;
  791. } else {
  792. running = (uint32_t)xTimerIsTimerActive (hTimer);
  793. }
  794. return (running);
  795. }
  796. osStatus_t osTimerDelete (osTimerId_t timer_id) {
  797. TimerHandle_t hTimer = (TimerHandle_t)timer_id;
  798. osStatus_t stat;
  799. #ifndef USE_FreeRTOS_HEAP_1
  800. TimerCallback_t *callb;
  801. if (IS_IRQ()) {
  802. stat = osErrorISR;
  803. }
  804. else if (hTimer == NULL) {
  805. stat = osErrorParameter;
  806. }
  807. else {
  808. callb = (TimerCallback_t *)pvTimerGetTimerID (hTimer);
  809. if (xTimerDelete (hTimer, 0) == pdPASS) {
  810. vPortFree (callb);
  811. stat = osOK;
  812. } else {
  813. stat = osErrorResource;
  814. }
  815. }
  816. #else
  817. stat = osError;
  818. #endif
  819. return (stat);
  820. }
  821. /*---------------------------------------------------------------------------*/
  822. osEventFlagsId_t osEventFlagsNew (const osEventFlagsAttr_t *attr) {
  823. EventGroupHandle_t hEventGroup;
  824. int32_t mem;
  825. hEventGroup = NULL;
  826. if (!IS_IRQ()) {
  827. mem = -1;
  828. if (attr != NULL) {
  829. if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticEventGroup_t))) {
  830. mem = 1;
  831. }
  832. else {
  833. if ((attr->cb_mem == NULL) && (attr->cb_size == 0U)) {
  834. mem = 0;
  835. }
  836. }
  837. }
  838. else {
  839. mem = 0;
  840. }
  841. if (mem == 1) {
  842. hEventGroup = xEventGroupCreateStatic (attr->cb_mem);
  843. }
  844. else {
  845. if (mem == 0) {
  846. hEventGroup = xEventGroupCreate();
  847. }
  848. }
  849. }
  850. return ((osEventFlagsId_t)hEventGroup);
  851. }
  852. uint32_t osEventFlagsSet (osEventFlagsId_t ef_id, uint32_t flags) {
  853. EventGroupHandle_t hEventGroup = (EventGroupHandle_t)ef_id;
  854. uint32_t rflags;
  855. BaseType_t yield;
  856. if ((hEventGroup == NULL) || ((flags & EVENT_FLAGS_INVALID_BITS) != 0U)) {
  857. rflags = (uint32_t)osErrorParameter;
  858. }
  859. else if (IS_IRQ()) {
  860. yield = pdFALSE;
  861. if (xEventGroupSetBitsFromISR (hEventGroup, (EventBits_t)flags, &yield) != pdFAIL) {
  862. rflags = (uint32_t)osErrorResource;
  863. } else {
  864. rflags = flags;
  865. portYIELD_FROM_ISR (yield);
  866. }
  867. }
  868. else {
  869. rflags = xEventGroupSetBits (hEventGroup, (EventBits_t)flags);
  870. }
  871. return (rflags);
  872. }
  873. uint32_t osEventFlagsClear (osEventFlagsId_t ef_id, uint32_t flags) {
  874. EventGroupHandle_t hEventGroup = (EventGroupHandle_t)ef_id;
  875. uint32_t rflags;
  876. if ((hEventGroup == NULL) || ((flags & EVENT_FLAGS_INVALID_BITS) != 0U)) {
  877. rflags = (uint32_t)osErrorParameter;
  878. }
  879. else if (IS_IRQ()) {
  880. rflags = xEventGroupGetBitsFromISR (hEventGroup);
  881. if (xEventGroupClearBitsFromISR (hEventGroup, (EventBits_t)flags) == pdFAIL) {
  882. rflags = (uint32_t)osErrorResource;
  883. }
  884. }
  885. else {
  886. rflags = xEventGroupClearBits (hEventGroup, (EventBits_t)flags);
  887. }
  888. return (rflags);
  889. }
  890. uint32_t osEventFlagsGet (osEventFlagsId_t ef_id) {
  891. EventGroupHandle_t hEventGroup = (EventGroupHandle_t)ef_id;
  892. uint32_t rflags;
  893. if (ef_id == NULL) {
  894. rflags = 0U;
  895. }
  896. else if (IS_IRQ()) {
  897. rflags = xEventGroupGetBitsFromISR (hEventGroup);
  898. }
  899. else {
  900. rflags = xEventGroupGetBits (hEventGroup);
  901. }
  902. return (rflags);
  903. }
  904. uint32_t osEventFlagsWait (osEventFlagsId_t ef_id, uint32_t flags, uint32_t options, uint32_t timeout) {
  905. EventGroupHandle_t hEventGroup = (EventGroupHandle_t)ef_id;
  906. BaseType_t wait_all;
  907. BaseType_t exit_clr;
  908. uint32_t rflags;
  909. if ((hEventGroup == NULL) || ((flags & EVENT_FLAGS_INVALID_BITS) != 0U)) {
  910. rflags = (uint32_t)osErrorParameter;
  911. }
  912. else if (IS_IRQ()) {
  913. rflags = (uint32_t)osErrorISR;
  914. }
  915. else {
  916. if (options & osFlagsWaitAll) {
  917. wait_all = pdTRUE;
  918. } else {
  919. wait_all = pdFAIL;
  920. }
  921. if (options & osFlagsNoClear) {
  922. exit_clr = pdFAIL;
  923. } else {
  924. exit_clr = pdTRUE;
  925. }
  926. rflags = xEventGroupWaitBits (hEventGroup, (EventBits_t)flags, exit_clr, wait_all, (TickType_t)timeout);
  927. if (options & osFlagsWaitAll) {
  928. if (flags != rflags) {
  929. if (timeout > 0U) {
  930. rflags = (uint32_t)osErrorTimeout;
  931. } else {
  932. rflags = (uint32_t)osErrorResource;
  933. }
  934. }
  935. }
  936. else {
  937. if ((flags & rflags) == 0U) {
  938. if (timeout > 0U) {
  939. rflags = (uint32_t)osErrorTimeout;
  940. } else {
  941. rflags = (uint32_t)osErrorResource;
  942. }
  943. }
  944. }
  945. }
  946. return (rflags);
  947. }
  948. osStatus_t osEventFlagsDelete (osEventFlagsId_t ef_id) {
  949. EventGroupHandle_t hEventGroup = (EventGroupHandle_t)ef_id;
  950. osStatus_t stat;
  951. #ifndef USE_FreeRTOS_HEAP_1
  952. if (IS_IRQ()) {
  953. stat = osErrorISR;
  954. }
  955. else if (hEventGroup == NULL) {
  956. stat = osErrorParameter;
  957. }
  958. else {
  959. stat = osOK;
  960. vEventGroupDelete (hEventGroup);
  961. }
  962. #else
  963. stat = osError;
  964. #endif
  965. return (stat);
  966. }
  967. /*---------------------------------------------------------------------------*/
  968. osMutexId_t osMutexNew (const osMutexAttr_t *attr) {
  969. SemaphoreHandle_t hMutex;
  970. uint32_t type;
  971. uint32_t rmtx;
  972. int32_t mem;
  973. #if (configQUEUE_REGISTRY_SIZE > 0)
  974. const char *name;
  975. #endif
  976. hMutex = NULL;
  977. if (!IS_IRQ()) {
  978. if (attr != NULL) {
  979. type = attr->attr_bits;
  980. } else {
  981. type = 0U;
  982. }
  983. if ((type & osMutexRecursive) == osMutexRecursive) {
  984. rmtx = 1U;
  985. } else {
  986. rmtx = 0U;
  987. }
  988. if ((type & osMutexRobust) != osMutexRobust) {
  989. mem = -1;
  990. if (attr != NULL) {
  991. if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticSemaphore_t))) {
  992. mem = 1;
  993. }
  994. else {
  995. if ((attr->cb_mem == NULL) && (attr->cb_size == 0U)) {
  996. mem = 0;
  997. }
  998. }
  999. }
  1000. else {
  1001. mem = 0;
  1002. }
  1003. if (mem == 1) {
  1004. if (rmtx != 0U) {
  1005. hMutex = xSemaphoreCreateRecursiveMutexStatic (attr->cb_mem);
  1006. }
  1007. else {
  1008. hMutex = xSemaphoreCreateMutexStatic (attr->cb_mem);
  1009. }
  1010. }
  1011. else {
  1012. if (mem == 0) {
  1013. if (rmtx != 0U) {
  1014. hMutex = xSemaphoreCreateRecursiveMutex ();
  1015. } else {
  1016. hMutex = xSemaphoreCreateMutex ();
  1017. }
  1018. }
  1019. }
  1020. #if (configQUEUE_REGISTRY_SIZE > 0)
  1021. if (hMutex != NULL) {
  1022. if (attr != NULL) {
  1023. name = attr->name;
  1024. } else {
  1025. name = NULL;
  1026. }
  1027. vQueueAddToRegistry (hMutex, name);
  1028. }
  1029. #endif
  1030. if ((hMutex != NULL) && (rmtx != 0U)) {
  1031. hMutex = (SemaphoreHandle_t)((uint32_t)hMutex | 1U);
  1032. }
  1033. }
  1034. }
  1035. return ((osMutexId_t)hMutex);
  1036. }
  1037. osStatus_t osMutexAcquire (osMutexId_t mutex_id, uint32_t timeout) {
  1038. SemaphoreHandle_t hMutex;
  1039. osStatus_t stat;
  1040. uint32_t rmtx;
  1041. hMutex = (SemaphoreHandle_t)((uint32_t)mutex_id & ~1U);
  1042. rmtx = (uint32_t)mutex_id & 1U;
  1043. stat = osOK;
  1044. if (IS_IRQ()) {
  1045. stat = osErrorISR;
  1046. }
  1047. else if (hMutex == NULL) {
  1048. stat = osErrorParameter;
  1049. }
  1050. else {
  1051. if (rmtx != 0U) {
  1052. if (xSemaphoreTakeRecursive (hMutex, timeout) != pdPASS) {
  1053. if (timeout != 0U) {
  1054. stat = osErrorTimeout;
  1055. } else {
  1056. stat = osErrorResource;
  1057. }
  1058. }
  1059. }
  1060. else {
  1061. if (xSemaphoreTake (hMutex, timeout) != pdPASS) {
  1062. if (timeout != 0U) {
  1063. stat = osErrorTimeout;
  1064. } else {
  1065. stat = osErrorResource;
  1066. }
  1067. }
  1068. }
  1069. }
  1070. return (stat);
  1071. }
  1072. osStatus_t osMutexRelease (osMutexId_t mutex_id) {
  1073. SemaphoreHandle_t hMutex;
  1074. osStatus_t stat;
  1075. uint32_t rmtx;
  1076. hMutex = (SemaphoreHandle_t)((uint32_t)mutex_id & ~1U);
  1077. rmtx = (uint32_t)mutex_id & 1U;
  1078. stat = osOK;
  1079. if (IS_IRQ()) {
  1080. stat = osErrorISR;
  1081. }
  1082. else if (hMutex == NULL) {
  1083. stat = osErrorParameter;
  1084. }
  1085. else {
  1086. if (rmtx != 0U) {
  1087. if (xSemaphoreGiveRecursive (hMutex) != pdPASS) {
  1088. stat = osErrorResource;
  1089. }
  1090. }
  1091. else {
  1092. if (xSemaphoreGive (hMutex) != pdPASS) {
  1093. stat = osErrorResource;
  1094. }
  1095. }
  1096. }
  1097. return (stat);
  1098. }
  1099. osThreadId_t osMutexGetOwner (osMutexId_t mutex_id) {
  1100. SemaphoreHandle_t hMutex;
  1101. osThreadId_t owner;
  1102. hMutex = (SemaphoreHandle_t)((uint32_t)mutex_id & ~1U);
  1103. if (IS_IRQ() || (hMutex == NULL)) {
  1104. owner = NULL;
  1105. } else {
  1106. owner = (osThreadId_t)xSemaphoreGetMutexHolder (hMutex);
  1107. }
  1108. return (owner);
  1109. }
  1110. osStatus_t osMutexDelete (osMutexId_t mutex_id) {
  1111. osStatus_t stat;
  1112. #ifndef USE_FreeRTOS_HEAP_1
  1113. SemaphoreHandle_t hMutex;
  1114. hMutex = (SemaphoreHandle_t)((uint32_t)mutex_id & ~1U);
  1115. if (IS_IRQ()) {
  1116. stat = osErrorISR;
  1117. }
  1118. else if (hMutex == NULL) {
  1119. stat = osErrorParameter;
  1120. }
  1121. else {
  1122. #if (configQUEUE_REGISTRY_SIZE > 0)
  1123. vQueueUnregisterQueue (hMutex);
  1124. #endif
  1125. stat = osOK;
  1126. vSemaphoreDelete (hMutex);
  1127. }
  1128. #else
  1129. stat = osError;
  1130. #endif
  1131. return (stat);
  1132. }
  1133. /*---------------------------------------------------------------------------*/
  1134. osSemaphoreId_t osSemaphoreNew (uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr) {
  1135. SemaphoreHandle_t hSemaphore;
  1136. int32_t mem;
  1137. #if (configQUEUE_REGISTRY_SIZE > 0)
  1138. const char *name;
  1139. #endif
  1140. hSemaphore = NULL;
  1141. if (!IS_IRQ() && (max_count > 0U) && (initial_count <= max_count)) {
  1142. mem = -1;
  1143. if (attr != NULL) {
  1144. if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticSemaphore_t))) {
  1145. mem = 1;
  1146. }
  1147. else {
  1148. if ((attr->cb_mem == NULL) && (attr->cb_size == 0U)) {
  1149. mem = 0;
  1150. }
  1151. }
  1152. }
  1153. else {
  1154. mem = 0;
  1155. }
  1156. if (mem != -1) {
  1157. if (max_count == 1U) {
  1158. if (mem == 1) {
  1159. hSemaphore = xSemaphoreCreateBinaryStatic ((StaticSemaphore_t *)attr->cb_mem);
  1160. }
  1161. else {
  1162. hSemaphore = xSemaphoreCreateBinary();
  1163. }
  1164. if ((hSemaphore != NULL) && (initial_count != 0U)) {
  1165. if (xSemaphoreGive (hSemaphore) != pdPASS) {
  1166. vSemaphoreDelete (hSemaphore);
  1167. hSemaphore = NULL;
  1168. }
  1169. }
  1170. }
  1171. else {
  1172. if (mem == 1) {
  1173. hSemaphore = xSemaphoreCreateCountingStatic (max_count, initial_count, (StaticSemaphore_t *)attr->cb_mem);
  1174. }
  1175. else {
  1176. hSemaphore = xSemaphoreCreateCounting (max_count, initial_count);
  1177. }
  1178. }
  1179. #if (configQUEUE_REGISTRY_SIZE > 0)
  1180. if (hSemaphore != NULL) {
  1181. if (attr != NULL) {
  1182. name = attr->name;
  1183. } else {
  1184. name = NULL;
  1185. }
  1186. vQueueAddToRegistry (hSemaphore, name);
  1187. }
  1188. #endif
  1189. }
  1190. }
  1191. return ((osSemaphoreId_t)hSemaphore);
  1192. }
  1193. osStatus_t osSemaphoreAcquire (osSemaphoreId_t semaphore_id, uint32_t timeout) {
  1194. SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)semaphore_id;
  1195. osStatus_t stat;
  1196. BaseType_t yield;
  1197. stat = osOK;
  1198. if (hSemaphore == NULL) {
  1199. stat = osErrorParameter;
  1200. }
  1201. else if (IS_IRQ()) {
  1202. if (timeout != 0U) {
  1203. stat = osErrorParameter;
  1204. }
  1205. else {
  1206. yield = pdFALSE;
  1207. if (xSemaphoreTakeFromISR (hSemaphore, &yield) != pdPASS) {
  1208. stat = osErrorResource;
  1209. } else {
  1210. portYIELD_FROM_ISR (yield);
  1211. }
  1212. }
  1213. }
  1214. else {
  1215. if (xSemaphoreTake (hSemaphore, (TickType_t)timeout) != pdPASS) {
  1216. if (timeout != 0U) {
  1217. stat = osErrorTimeout;
  1218. } else {
  1219. stat = osErrorResource;
  1220. }
  1221. }
  1222. }
  1223. return (stat);
  1224. }
  1225. osStatus_t osSemaphoreRelease (osSemaphoreId_t semaphore_id) {
  1226. SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)semaphore_id;
  1227. osStatus_t stat;
  1228. BaseType_t yield;
  1229. stat = osOK;
  1230. if (hSemaphore == NULL) {
  1231. stat = osErrorParameter;
  1232. }
  1233. else if (IS_IRQ()) {
  1234. yield = pdFALSE;
  1235. if (xSemaphoreGiveFromISR (hSemaphore, &yield) != pdTRUE) {
  1236. stat = osErrorResource;
  1237. } else {
  1238. portYIELD_FROM_ISR (yield);
  1239. }
  1240. }
  1241. else {
  1242. if (xSemaphoreGive (hSemaphore) != pdPASS) {
  1243. stat = osErrorResource;
  1244. }
  1245. }
  1246. return (stat);
  1247. }
  1248. uint32_t osSemaphoreGetCount (osSemaphoreId_t semaphore_id) {
  1249. SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)semaphore_id;
  1250. uint32_t count;
  1251. if (hSemaphore == NULL) {
  1252. count = 0U;
  1253. }
  1254. else if (IS_IRQ()) {
  1255. count = uxQueueMessagesWaitingFromISR (hSemaphore);
  1256. } else {
  1257. count = (uint32_t)uxSemaphoreGetCount (hSemaphore);
  1258. }
  1259. return (count);
  1260. }
  1261. osStatus_t osSemaphoreDelete (osSemaphoreId_t semaphore_id) {
  1262. SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)semaphore_id;
  1263. osStatus_t stat;
  1264. #ifndef USE_FreeRTOS_HEAP_1
  1265. if (IS_IRQ()) {
  1266. stat = osErrorISR;
  1267. }
  1268. else if (hSemaphore == NULL) {
  1269. stat = osErrorParameter;
  1270. }
  1271. else {
  1272. #if (configQUEUE_REGISTRY_SIZE > 0)
  1273. vQueueUnregisterQueue (hSemaphore);
  1274. #endif
  1275. stat = osOK;
  1276. vSemaphoreDelete (hSemaphore);
  1277. }
  1278. #else
  1279. stat = osError;
  1280. #endif
  1281. return (stat);
  1282. }
  1283. /*---------------------------------------------------------------------------*/
  1284. osMessageQueueId_t osMessageQueueNew (uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t *attr) {
  1285. QueueHandle_t hQueue;
  1286. int32_t mem;
  1287. #if (configQUEUE_REGISTRY_SIZE > 0)
  1288. const char *name;
  1289. #endif
  1290. hQueue = NULL;
  1291. if (!IS_IRQ() && (msg_count > 0U) && (msg_size > 0U)) {
  1292. mem = -1;
  1293. if (attr != NULL) {
  1294. if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticQueue_t)) &&
  1295. (attr->mq_mem != NULL) && (attr->mq_size >= (msg_count * msg_size))) {
  1296. mem = 1;
  1297. }
  1298. else {
  1299. if ((attr->cb_mem == NULL) && (attr->cb_size == 0U) &&
  1300. (attr->mq_mem == NULL) && (attr->mq_size == 0U)) {
  1301. mem = 0;
  1302. }
  1303. }
  1304. }
  1305. else {
  1306. mem = 0;
  1307. }
  1308. if (mem == 1) {
  1309. hQueue = xQueueCreateStatic (msg_count, msg_size, attr->mq_mem, attr->cb_mem);
  1310. }
  1311. else {
  1312. if (mem == 0) {
  1313. hQueue = xQueueCreate (msg_count, msg_size);
  1314. }
  1315. }
  1316. #if (configQUEUE_REGISTRY_SIZE > 0)
  1317. if (hQueue != NULL) {
  1318. if (attr != NULL) {
  1319. name = attr->name;
  1320. } else {
  1321. name = NULL;
  1322. }
  1323. vQueueAddToRegistry (hQueue, name);
  1324. }
  1325. #endif
  1326. }
  1327. return ((osMessageQueueId_t)hQueue);
  1328. }
  1329. osStatus_t osMessageQueuePut (osMessageQueueId_t mq_id, const void *msg_ptr, uint8_t msg_prio, uint32_t timeout) {
  1330. QueueHandle_t hQueue = (QueueHandle_t)mq_id;
  1331. osStatus_t stat;
  1332. BaseType_t yield;
  1333. (void)msg_prio; /* Message priority is ignored */
  1334. stat = osOK;
  1335. if (IS_IRQ()) {
  1336. if ((hQueue == NULL) || (msg_ptr == NULL) || (timeout != 0U)) {
  1337. stat = osErrorParameter;
  1338. }
  1339. else {
  1340. yield = pdFALSE;
  1341. if (xQueueSendToBackFromISR (hQueue, msg_ptr, &yield) != pdTRUE) {
  1342. stat = osErrorResource;
  1343. } else {
  1344. portYIELD_FROM_ISR (yield);
  1345. }
  1346. }
  1347. }
  1348. else {
  1349. if ((hQueue == NULL) || (msg_ptr == NULL)) {
  1350. stat = osErrorParameter;
  1351. }
  1352. else {
  1353. if (xQueueSendToBack (hQueue, msg_ptr, (TickType_t)timeout) != pdPASS) {
  1354. if (timeout != 0U) {
  1355. stat = osErrorTimeout;
  1356. } else {
  1357. stat = osErrorResource;
  1358. }
  1359. }
  1360. }
  1361. }
  1362. return (stat);
  1363. }
  1364. osStatus_t osMessageQueueGet (osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout) {
  1365. QueueHandle_t hQueue = (QueueHandle_t)mq_id;
  1366. osStatus_t stat;
  1367. BaseType_t yield;
  1368. (void)msg_prio; /* Message priority is ignored */
  1369. stat = osOK;
  1370. if (IS_IRQ()) {
  1371. if ((hQueue == NULL) || (msg_ptr == NULL) || (timeout != 0U)) {
  1372. stat = osErrorParameter;
  1373. }
  1374. else {
  1375. yield = pdFALSE;
  1376. if (xQueueReceiveFromISR (hQueue, msg_ptr, &yield) != pdPASS) {
  1377. stat = osErrorResource;
  1378. } else {
  1379. portYIELD_FROM_ISR (yield);
  1380. }
  1381. }
  1382. }
  1383. else {
  1384. if ((hQueue == NULL) || (msg_ptr == NULL)) {
  1385. stat = osErrorParameter;
  1386. }
  1387. else {
  1388. if (xQueueReceive (hQueue, msg_ptr, (TickType_t)timeout) != pdPASS) {
  1389. if (timeout != 0U) {
  1390. stat = osErrorTimeout;
  1391. } else {
  1392. stat = osErrorResource;
  1393. }
  1394. }
  1395. }
  1396. }
  1397. return (stat);
  1398. }
  1399. uint32_t osMessageQueueGetCapacity (osMessageQueueId_t mq_id) {
  1400. StaticQueue_t *mq = (StaticQueue_t *)mq_id;
  1401. uint32_t capacity;
  1402. if (mq == NULL) {
  1403. capacity = 0U;
  1404. } else {
  1405. /* capacity = pxQueue->uxLength */
  1406. capacity = mq->uxDummy4[1];
  1407. }
  1408. return (capacity);
  1409. }
  1410. uint32_t osMessageQueueGetMsgSize (osMessageQueueId_t mq_id) {
  1411. StaticQueue_t *mq = (StaticQueue_t *)mq_id;
  1412. uint32_t size;
  1413. if (mq == NULL) {
  1414. size = 0U;
  1415. } else {
  1416. /* size = pxQueue->uxItemSize */
  1417. size = mq->uxDummy4[2];
  1418. }
  1419. return (size);
  1420. }
  1421. uint32_t osMessageQueueGetCount (osMessageQueueId_t mq_id) {
  1422. QueueHandle_t hQueue = (QueueHandle_t)mq_id;
  1423. UBaseType_t count;
  1424. if (hQueue == NULL) {
  1425. count = 0U;
  1426. }
  1427. else if (IS_IRQ()) {
  1428. count = uxQueueMessagesWaitingFromISR (hQueue);
  1429. }
  1430. else {
  1431. count = uxQueueMessagesWaiting (hQueue);
  1432. }
  1433. return ((uint32_t)count);
  1434. }
  1435. uint32_t osMessageQueueGetSpace (osMessageQueueId_t mq_id) {
  1436. StaticQueue_t *mq = (StaticQueue_t *)mq_id;
  1437. uint32_t space;
  1438. uint32_t isrm;
  1439. if (mq == NULL) {
  1440. space = 0U;
  1441. }
  1442. else if (IS_IRQ()) {
  1443. isrm = taskENTER_CRITICAL_FROM_ISR();
  1444. /* space = pxQueue->uxLength - pxQueue->uxMessagesWaiting; */
  1445. space = mq->uxDummy4[1] - mq->uxDummy4[0];
  1446. taskEXIT_CRITICAL_FROM_ISR(isrm);
  1447. }
  1448. else {
  1449. space = (uint32_t)uxQueueSpacesAvailable ((QueueHandle_t)mq);
  1450. }
  1451. return (space);
  1452. }
  1453. osStatus_t osMessageQueueReset (osMessageQueueId_t mq_id) {
  1454. QueueHandle_t hQueue = (QueueHandle_t)mq_id;
  1455. osStatus_t stat;
  1456. if (IS_IRQ()) {
  1457. stat = osErrorISR;
  1458. }
  1459. else if (hQueue == NULL) {
  1460. stat = osErrorParameter;
  1461. }
  1462. else {
  1463. stat = osOK;
  1464. (void)xQueueReset (hQueue);
  1465. }
  1466. return (stat);
  1467. }
  1468. osStatus_t osMessageQueueDelete (osMessageQueueId_t mq_id) {
  1469. QueueHandle_t hQueue = (QueueHandle_t)mq_id;
  1470. osStatus_t stat;
  1471. #ifndef USE_FreeRTOS_HEAP_1
  1472. if (IS_IRQ()) {
  1473. stat = osErrorISR;
  1474. }
  1475. else if (hQueue == NULL) {
  1476. stat = osErrorParameter;
  1477. }
  1478. else {
  1479. #if (configQUEUE_REGISTRY_SIZE > 0)
  1480. vQueueUnregisterQueue (hQueue);
  1481. #endif
  1482. stat = osOK;
  1483. vQueueDelete (hQueue);
  1484. }
  1485. #else
  1486. stat = osError;
  1487. #endif
  1488. return (stat);
  1489. }
  1490. /*---------------------------------------------------------------------------*/
  1491. /* Callback function prototypes */
  1492. extern void vApplicationIdleHook (void);
  1493. extern void vApplicationTickHook (void);
  1494. extern void vApplicationMallocFailedHook (void);
  1495. extern void vApplicationDaemonTaskStartupHook (void);
  1496. extern void vApplicationStackOverflowHook (TaskHandle_t xTask, signed char *pcTaskName);
  1497. /**
  1498. Dummy implementation of the callback function vApplicationIdleHook().
  1499. */
  1500. #if (configUSE_IDLE_HOOK == 1)
  1501. __WEAK void vApplicationIdleHook (void){}
  1502. #endif
  1503. /**
  1504. Dummy implementation of the callback function vApplicationTickHook().
  1505. */
  1506. #if (configUSE_TICK_HOOK == 1)
  1507. __WEAK void vApplicationTickHook (void){}
  1508. #endif
  1509. /**
  1510. Dummy implementation of the callback function vApplicationMallocFailedHook().
  1511. */
  1512. #if (configUSE_MALLOC_FAILED_HOOK == 1)
  1513. __WEAK void vApplicationMallocFailedHook (void){}
  1514. #endif
  1515. /**
  1516. Dummy implementation of the callback function vApplicationDaemonTaskStartupHook().
  1517. */
  1518. #if (configUSE_DAEMON_TASK_STARTUP_HOOK == 1)
  1519. __WEAK void vApplicationDaemonTaskStartupHook (void){}
  1520. #endif
  1521. /**
  1522. Dummy implementation of the callback function vApplicationStackOverflowHook().
  1523. */
  1524. #if (configCHECK_FOR_STACK_OVERFLOW > 0)
  1525. __WEAK void vApplicationStackOverflowHook (TaskHandle_t xTask, signed char *pcTaskName) {
  1526. (void)xTask;
  1527. (void)pcTaskName;
  1528. }
  1529. #endif
  1530. /*---------------------------------------------------------------------------*/
  1531. /* External Idle and Timer task static memory allocation functions */
  1532. extern void vApplicationGetIdleTaskMemory (StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize);
  1533. extern void vApplicationGetTimerTaskMemory (StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize);
  1534. /* Idle task control block and stack */
  1535. static StaticTask_t Idle_TCB;
  1536. static StackType_t Idle_Stack[configMINIMAL_STACK_SIZE];
  1537. /* Timer task control block and stack */
  1538. static StaticTask_t Timer_TCB;
  1539. static StackType_t Timer_Stack[configTIMER_TASK_STACK_DEPTH];
  1540. /*
  1541. vApplicationGetIdleTaskMemory gets called when configSUPPORT_STATIC_ALLOCATION
  1542. equals to 1 and is required for static memory allocation support.
  1543. */
  1544. void vApplicationGetIdleTaskMemory (StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize) {
  1545. *ppxIdleTaskTCBBuffer = &Idle_TCB;
  1546. *ppxIdleTaskStackBuffer = &Idle_Stack[0];
  1547. *pulIdleTaskStackSize = (uint32_t)configMINIMAL_STACK_SIZE;
  1548. }
  1549. /*
  1550. vApplicationGetTimerTaskMemory gets called when configSUPPORT_STATIC_ALLOCATION
  1551. equals to 1 and is required for static memory allocation support.
  1552. */
  1553. void vApplicationGetTimerTaskMemory (StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize) {
  1554. *ppxTimerTaskTCBBuffer = &Timer_TCB;
  1555. *ppxTimerTaskStackBuffer = &Timer_Stack[0];
  1556. *pulTimerTaskStackSize = (uint32_t)configTIMER_TASK_STACK_DEPTH;
  1557. }