cmsis_os2.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871
  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. }