cmsis_os.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  1. /* ----------------------------------------------------------------------
  2. * $Date: 5. February 2013
  3. * $Revision: V1.02
  4. *
  5. * Project: CMSIS-RTOS API
  6. * Title: cmsis_os.c
  7. *
  8. * Version 0.02
  9. * Initial Proposal Phase
  10. * Version 0.03
  11. * osKernelStart added, optional feature: main started as thread
  12. * osSemaphores have standard behavior
  13. * osTimerCreate does not start the timer, added osTimerStart
  14. * osThreadPass is renamed to osThreadYield
  15. * Version 1.01
  16. * Support for C++ interface
  17. * - const attribute removed from the osXxxxDef_t typedef's
  18. * - const attribute added to the osXxxxDef macros
  19. * Added: osTimerDelete, osMutexDelete, osSemaphoreDelete
  20. * Added: osKernelInitialize
  21. * Version 1.02
  22. * Control functions for short timeouts in microsecond resolution:
  23. * Added: osKernelSysTick, osKernelSysTickFrequency, osKernelSysTickMicroSec
  24. * Removed: osSignalGet
  25. *
  26. *
  27. *----------------------------------------------------------------------------
  28. *
  29. * Portions Copyright © 2016 STMicroelectronics International N.V. All rights reserved.
  30. * Portions Copyright (c) 2013 ARM LIMITED
  31. * All rights reserved.
  32. * Redistribution and use in source and binary forms, with or without
  33. * modification, are permitted provided that the following conditions are met:
  34. * - Redistributions of source code must retain the above copyright
  35. * notice, this list of conditions and the following disclaimer.
  36. * - Redistributions in binary form must reproduce the above copyright
  37. * notice, this list of conditions and the following disclaimer in the
  38. * documentation and/or other materials provided with the distribution.
  39. * - Neither the name of ARM nor the names of its contributors may be used
  40. * to endorse or promote products derived from this software without
  41. * specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  44. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  45. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  46. * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  47. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  48. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  49. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  50. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  51. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  52. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  53. * POSSIBILITY OF SUCH DAMAGE.
  54. *---------------------------------------------------------------------------*/
  55. #include <string.h>
  56. #include "cmsis_os.h"
  57. /*
  58. * ARM Compiler 4/5
  59. */
  60. #if defined ( __CC_ARM )
  61. #define __ASM __asm
  62. #define __INLINE __inline
  63. #define __STATIC_INLINE static __inline
  64. #include "cmsis_armcc.h"
  65. /*
  66. * GNU Compiler
  67. */
  68. #elif defined ( __GNUC__ )
  69. #define __ASM __asm /*!< asm keyword for GNU Compiler */
  70. #define __INLINE inline /*!< inline keyword for GNU Compiler */
  71. #define __STATIC_INLINE static inline
  72. #include "cmsis_gcc.h"
  73. /*
  74. * IAR Compiler
  75. */
  76. #elif defined ( __ICCARM__ )
  77. #ifndef __ASM
  78. #define __ASM __asm
  79. #endif
  80. #ifndef __INLINE
  81. #define __INLINE inline
  82. #endif
  83. #ifndef __STATIC_INLINE
  84. #define __STATIC_INLINE static inline
  85. #endif
  86. #include <cmsis_iar.h>
  87. #endif
  88. extern void xPortSysTickHandler(void);
  89. /* Convert from CMSIS type osPriority to FreeRTOS priority number */
  90. static unsigned portBASE_TYPE makeFreeRtosPriority (osPriority priority)
  91. {
  92. unsigned portBASE_TYPE fpriority = tskIDLE_PRIORITY;
  93. if (priority != osPriorityError) {
  94. fpriority += (priority - osPriorityIdle);
  95. }
  96. return fpriority;
  97. }
  98. #if (INCLUDE_uxTaskPriorityGet == 1)
  99. /* Convert from FreeRTOS priority number to CMSIS type osPriority */
  100. static osPriority makeCmsisPriority (unsigned portBASE_TYPE fpriority)
  101. {
  102. osPriority priority = osPriorityError;
  103. if ((fpriority - tskIDLE_PRIORITY) <= (osPriorityRealtime - osPriorityIdle)) {
  104. priority = (osPriority)((int)osPriorityIdle + (int)(fpriority - tskIDLE_PRIORITY));
  105. }
  106. return priority;
  107. }
  108. #endif
  109. /* Determine whether we are in thread mode or handler mode. */
  110. static int inHandlerMode (void)
  111. {
  112. return __get_IPSR() != 0;
  113. }
  114. /*********************** Kernel Control Functions *****************************/
  115. /**
  116. * @brief Initialize the RTOS Kernel for creating objects.
  117. * @retval status code that indicates the execution status of the function.
  118. * @note MUST REMAIN UNCHANGED: \b osKernelInitialize shall be consistent in every CMSIS-RTOS.
  119. */
  120. osStatus osKernelInitialize (void);
  121. /**
  122. * @brief Start the RTOS Kernel with executing the specified thread.
  123. * @param thread_def thread definition referenced with \ref osThread.
  124. * @param argument pointer that is passed to the thread function as start argument.
  125. * @retval status code that indicates the execution status of the function
  126. * @note MUST REMAIN UNCHANGED: \b osKernelStart shall be consistent in every CMSIS-RTOS.
  127. */
  128. osStatus osKernelStart (void)
  129. {
  130. vTaskStartScheduler();
  131. return osOK;
  132. }
  133. /**
  134. * @brief Check if the RTOS kernel is already started
  135. * @param None
  136. * @retval (0) RTOS is not started
  137. * (1) RTOS is started
  138. * (-1) if this feature is disabled in FreeRTOSConfig.h
  139. * @note MUST REMAIN UNCHANGED: \b osKernelRunning shall be consistent in every CMSIS-RTOS.
  140. */
  141. int32_t osKernelRunning(void)
  142. {
  143. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  144. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED)
  145. return 0;
  146. else
  147. return 1;
  148. #else
  149. return (-1);
  150. #endif
  151. }
  152. #if (defined (osFeature_SysTick) && (osFeature_SysTick != 0)) // System Timer available
  153. /**
  154. * @brief Get the value of the Kernel SysTick timer
  155. * @param None
  156. * @retval None
  157. * @note MUST REMAIN UNCHANGED: \b osKernelSysTick shall be consistent in every CMSIS-RTOS.
  158. */
  159. uint32_t osKernelSysTick(void)
  160. {
  161. if (inHandlerMode()) {
  162. return xTaskGetTickCountFromISR();
  163. }
  164. else {
  165. return xTaskGetTickCount();
  166. }
  167. }
  168. #endif // System Timer available
  169. /*********************** Thread Management *****************************/
  170. /**
  171. * @brief Create a thread and add it to Active Threads and set it to state READY.
  172. * @param thread_def thread definition referenced with \ref osThread.
  173. * @param argument pointer that is passed to the thread function as start argument.
  174. * @retval thread ID for reference by other functions or NULL in case of error.
  175. * @note MUST REMAIN UNCHANGED: \b osThreadCreate shall be consistent in every CMSIS-RTOS.
  176. */
  177. osThreadId osThreadCreate (const osThreadDef_t *thread_def, void *argument)
  178. {
  179. TaskHandle_t handle;
  180. #if( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  181. if((thread_def->buffer != NULL) && (thread_def->controlblock != NULL)) {
  182. handle = xTaskCreateStatic((TaskFunction_t)thread_def->pthread,(const portCHAR *)thread_def->name,
  183. thread_def->stacksize, argument, makeFreeRtosPriority(thread_def->tpriority),
  184. thread_def->buffer, thread_def->controlblock);
  185. }
  186. else {
  187. if (xTaskCreate((TaskFunction_t)thread_def->pthread,(const portCHAR *)thread_def->name,
  188. thread_def->stacksize, argument, makeFreeRtosPriority(thread_def->tpriority),
  189. &handle) != pdPASS) {
  190. return NULL;
  191. }
  192. }
  193. #elif( configSUPPORT_STATIC_ALLOCATION == 1 )
  194. handle = xTaskCreateStatic((TaskFunction_t)thread_def->pthread,(const portCHAR *)thread_def->name,
  195. thread_def->stacksize, argument, makeFreeRtosPriority(thread_def->tpriority),
  196. thread_def->buffer, thread_def->controlblock);
  197. #else
  198. if (xTaskCreate((TaskFunction_t)thread_def->pthread,(const portCHAR *)thread_def->name,
  199. thread_def->stacksize, argument, makeFreeRtosPriority(thread_def->tpriority),
  200. &handle) != pdPASS) {
  201. return NULL;
  202. }
  203. #endif
  204. return handle;
  205. }
  206. /**
  207. * @brief Return the thread ID of the current running thread.
  208. * @retval thread ID for reference by other functions or NULL in case of error.
  209. * @note MUST REMAIN UNCHANGED: \b osThreadGetId shall be consistent in every CMSIS-RTOS.
  210. */
  211. osThreadId osThreadGetId (void)
  212. {
  213. #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
  214. return xTaskGetCurrentTaskHandle();
  215. #else
  216. return NULL;
  217. #endif
  218. }
  219. /**
  220. * @brief Terminate execution of a thread and remove it from Active Threads.
  221. * @param thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  222. * @retval status code that indicates the execution status of the function.
  223. * @note MUST REMAIN UNCHANGED: \b osThreadTerminate shall be consistent in every CMSIS-RTOS.
  224. */
  225. osStatus osThreadTerminate (osThreadId thread_id)
  226. {
  227. #if (INCLUDE_vTaskDelete == 1)
  228. vTaskDelete(thread_id);
  229. return osOK;
  230. #else
  231. return osErrorOS;
  232. #endif
  233. }
  234. /**
  235. * @brief Pass control to next thread that is in state \b READY.
  236. * @retval status code that indicates the execution status of the function.
  237. * @note MUST REMAIN UNCHANGED: \b osThreadYield shall be consistent in every CMSIS-RTOS.
  238. */
  239. osStatus osThreadYield (void)
  240. {
  241. taskYIELD();
  242. return osOK;
  243. }
  244. /**
  245. * @brief Change priority of an active thread.
  246. * @param thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  247. * @param priority new priority value for the thread function.
  248. * @retval status code that indicates the execution status of the function.
  249. * @note MUST REMAIN UNCHANGED: \b osThreadSetPriority shall be consistent in every CMSIS-RTOS.
  250. */
  251. osStatus osThreadSetPriority (osThreadId thread_id, osPriority priority)
  252. {
  253. #if (INCLUDE_vTaskPrioritySet == 1)
  254. vTaskPrioritySet(thread_id, makeFreeRtosPriority(priority));
  255. return osOK;
  256. #else
  257. return osErrorOS;
  258. #endif
  259. }
  260. /**
  261. * @brief Get current priority of an active thread.
  262. * @param thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  263. * @retval current priority value of the thread function.
  264. * @note MUST REMAIN UNCHANGED: \b osThreadGetPriority shall be consistent in every CMSIS-RTOS.
  265. */
  266. osPriority osThreadGetPriority (osThreadId thread_id)
  267. {
  268. #if (INCLUDE_uxTaskPriorityGet == 1)
  269. if (inHandlerMode())
  270. {
  271. return makeCmsisPriority(uxTaskPriorityGetFromISR(thread_id));
  272. }
  273. else
  274. {
  275. return makeCmsisPriority(uxTaskPriorityGet(thread_id));
  276. }
  277. #else
  278. return osPriorityError;
  279. #endif
  280. }
  281. /*********************** Generic Wait Functions *******************************/
  282. /**
  283. * @brief Wait for Timeout (Time Delay)
  284. * @param millisec time delay value
  285. * @retval status code that indicates the execution status of the function.
  286. */
  287. osStatus osDelay (uint32_t millisec)
  288. {
  289. #if INCLUDE_vTaskDelay
  290. TickType_t ticks = millisec / portTICK_PERIOD_MS;
  291. vTaskDelay(ticks ? ticks : 1); /* Minimum delay = 1 tick */
  292. return osOK;
  293. #else
  294. (void) millisec;
  295. return osErrorResource;
  296. #endif
  297. }
  298. #if (defined (osFeature_Wait) && (osFeature_Wait != 0)) /* Generic Wait available */
  299. /**
  300. * @brief Wait for Signal, Message, Mail, or Timeout
  301. * @param millisec timeout value or 0 in case of no time-out
  302. * @retval event that contains signal, message, or mail information or error code.
  303. * @note MUST REMAIN UNCHANGED: \b osWait shall be consistent in every CMSIS-RTOS.
  304. */
  305. osEvent osWait (uint32_t millisec);
  306. #endif /* Generic Wait available */
  307. /*********************** Timer Management Functions ***************************/
  308. /**
  309. * @brief Create a timer.
  310. * @param timer_def timer object referenced with \ref osTimer.
  311. * @param type osTimerOnce for one-shot or osTimerPeriodic for periodic behavior.
  312. * @param argument argument to the timer call back function.
  313. * @retval timer ID for reference by other functions or NULL in case of error.
  314. * @note MUST REMAIN UNCHANGED: \b osTimerCreate shall be consistent in every CMSIS-RTOS.
  315. */
  316. osTimerId osTimerCreate (const osTimerDef_t *timer_def, os_timer_type type, void *argument)
  317. {
  318. #if (configUSE_TIMERS == 1)
  319. #if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  320. if(timer_def->controlblock != NULL) {
  321. return xTimerCreateStatic((const char *)"",
  322. 1, // period should be filled when starting the Timer using osTimerStart
  323. (type == osTimerPeriodic) ? pdTRUE : pdFALSE,
  324. (void *) argument,
  325. (TimerCallbackFunction_t)timer_def->ptimer,
  326. (StaticTimer_t *)timer_def->controlblock);
  327. }
  328. else {
  329. return xTimerCreate((const char *)"",
  330. 1, // period should be filled when starting the Timer using osTimerStart
  331. (type == osTimerPeriodic) ? pdTRUE : pdFALSE,
  332. (void *) argument,
  333. (TimerCallbackFunction_t)timer_def->ptimer);
  334. }
  335. #elif( configSUPPORT_STATIC_ALLOCATION == 1 )
  336. return xTimerCreateStatic((const char *)"",
  337. 1, // period should be filled when starting the Timer using osTimerStart
  338. (type == osTimerPeriodic) ? pdTRUE : pdFALSE,
  339. (void *) argument,
  340. (TimerCallbackFunction_t)timer_def->ptimer,
  341. (StaticTimer_t *)timer_def->controlblock);
  342. #else
  343. return xTimerCreate((const char *)"",
  344. 1, // period should be filled when starting the Timer using osTimerStart
  345. (type == osTimerPeriodic) ? pdTRUE : pdFALSE,
  346. (void *) argument,
  347. (TimerCallbackFunction_t)timer_def->ptimer);
  348. #endif
  349. #else
  350. return NULL;
  351. #endif
  352. }
  353. /**
  354. * @brief Start or restart a timer.
  355. * @param timer_id timer ID obtained by \ref osTimerCreate.
  356. * @param millisec time delay value of the timer.
  357. * @retval status code that indicates the execution status of the function
  358. * @note MUST REMAIN UNCHANGED: \b osTimerStart shall be consistent in every CMSIS-RTOS.
  359. */
  360. osStatus osTimerStart (osTimerId timer_id, uint32_t millisec)
  361. {
  362. osStatus result = osOK;
  363. #if (configUSE_TIMERS == 1)
  364. portBASE_TYPE taskWoken = pdFALSE;
  365. TickType_t ticks = millisec / portTICK_PERIOD_MS;
  366. if (ticks == 0)
  367. ticks = 1;
  368. if (inHandlerMode())
  369. {
  370. if (xTimerChangePeriodFromISR(timer_id, ticks, &taskWoken) != pdPASS)
  371. {
  372. result = osErrorOS;
  373. }
  374. else
  375. {
  376. portEND_SWITCHING_ISR(taskWoken);
  377. }
  378. }
  379. else
  380. {
  381. if (xTimerChangePeriod(timer_id, ticks, 0) != pdPASS)
  382. result = osErrorOS;
  383. }
  384. #else
  385. result = osErrorOS;
  386. #endif
  387. return result;
  388. }
  389. /**
  390. * @brief Stop a timer.
  391. * @param timer_id timer ID obtained by \ref osTimerCreate
  392. * @retval status code that indicates the execution status of the function.
  393. * @note MUST REMAIN UNCHANGED: \b osTimerStop shall be consistent in every CMSIS-RTOS.
  394. */
  395. osStatus osTimerStop (osTimerId timer_id)
  396. {
  397. osStatus result = osOK;
  398. #if (configUSE_TIMERS == 1)
  399. portBASE_TYPE taskWoken = pdFALSE;
  400. if (inHandlerMode()) {
  401. if (xTimerStopFromISR(timer_id, &taskWoken) != pdPASS) {
  402. return osErrorOS;
  403. }
  404. portEND_SWITCHING_ISR(taskWoken);
  405. }
  406. else {
  407. if (xTimerStop(timer_id, 0) != pdPASS) {
  408. result = osErrorOS;
  409. }
  410. }
  411. #else
  412. result = osErrorOS;
  413. #endif
  414. return result;
  415. }
  416. /**
  417. * @brief Delete a timer.
  418. * @param timer_id timer ID obtained by \ref osTimerCreate
  419. * @retval status code that indicates the execution status of the function.
  420. * @note MUST REMAIN UNCHANGED: \b osTimerDelete shall be consistent in every CMSIS-RTOS.
  421. */
  422. osStatus osTimerDelete (osTimerId timer_id)
  423. {
  424. osStatus result = osOK;
  425. #if (configUSE_TIMERS == 1)
  426. if (inHandlerMode()) {
  427. return osErrorISR;
  428. }
  429. else {
  430. if ((xTimerDelete(timer_id, osWaitForever )) != pdPASS) {
  431. result = osErrorOS;
  432. }
  433. }
  434. #else
  435. result = osErrorOS;
  436. #endif
  437. return result;
  438. }
  439. /*************************** Signal Management ********************************/
  440. /**
  441. * @brief Set the specified Signal Flags of an active thread.
  442. * @param thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  443. * @param signals specifies the signal flags of the thread that should be set.
  444. * @retval previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters.
  445. * @note MUST REMAIN UNCHANGED: \b osSignalSet shall be consistent in every CMSIS-RTOS.
  446. */
  447. int32_t osSignalSet (osThreadId thread_id, int32_t signal)
  448. {
  449. #if( configUSE_TASK_NOTIFICATIONS == 1 )
  450. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  451. uint32_t ulPreviousNotificationValue = 0;
  452. if (inHandlerMode())
  453. {
  454. if(xTaskGenericNotifyFromISR( thread_id , (uint32_t)signal, eSetBits, &ulPreviousNotificationValue, &xHigherPriorityTaskWoken ) != pdPASS )
  455. return 0x80000000;
  456. portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
  457. }
  458. else if(xTaskGenericNotify( thread_id , (uint32_t)signal, eSetBits, &ulPreviousNotificationValue) != pdPASS )
  459. return 0x80000000;
  460. return ulPreviousNotificationValue;
  461. #else
  462. (void) thread_id;
  463. (void) signal;
  464. return 0x80000000; /* Task Notification not supported */
  465. #endif
  466. }
  467. /**
  468. * @brief Clear the specified Signal Flags of an active thread.
  469. * @param thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  470. * @param signals specifies the signal flags of the thread that shall be cleared.
  471. * @retval previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters.
  472. * @note MUST REMAIN UNCHANGED: \b osSignalClear shall be consistent in every CMSIS-RTOS.
  473. */
  474. int32_t osSignalClear (osThreadId thread_id, int32_t signal);
  475. /**
  476. * @brief Wait for one or more Signal Flags to become signaled for the current \b RUNNING thread.
  477. * @param signals wait until all specified signal flags set or 0 for any single signal flag.
  478. * @param millisec timeout value or 0 in case of no time-out.
  479. * @retval event flag information or error code.
  480. * @note MUST REMAIN UNCHANGED: \b osSignalWait shall be consistent in every CMSIS-RTOS.
  481. */
  482. osEvent osSignalWait (int32_t signals, uint32_t millisec)
  483. {
  484. osEvent ret;
  485. #if( configUSE_TASK_NOTIFICATIONS == 1 )
  486. TickType_t ticks;
  487. ret.value.signals = 0;
  488. ticks = 0;
  489. if (millisec == osWaitForever) {
  490. ticks = portMAX_DELAY;
  491. }
  492. else if (millisec != 0) {
  493. ticks = millisec / portTICK_PERIOD_MS;
  494. if (ticks == 0) {
  495. ticks = 1;
  496. }
  497. }
  498. if (inHandlerMode())
  499. {
  500. ret.status = osErrorISR; /*Not allowed in ISR*/
  501. }
  502. else
  503. {
  504. if(xTaskNotifyWait( 0,(uint32_t) signals, (uint32_t *)&ret.value.signals, ticks) != pdTRUE)
  505. {
  506. if(ticks == 0) ret.status = osOK;
  507. else ret.status = osEventTimeout;
  508. }
  509. else if(ret.value.signals < 0)
  510. {
  511. ret.status = osErrorValue;
  512. }
  513. else ret.status = osEventSignal;
  514. }
  515. #else
  516. (void) signals;
  517. (void) millisec;
  518. ret.status = osErrorOS; /* Task Notification not supported */
  519. #endif
  520. return ret;
  521. }
  522. /**************************** Mutex Management ********************************/
  523. /**
  524. * @brief Create and Initialize a Mutex object
  525. * @param mutex_def mutex definition referenced with \ref osMutex.
  526. * @retval mutex ID for reference by other functions or NULL in case of error.
  527. * @note MUST REMAIN UNCHANGED: \b osMutexCreate shall be consistent in every CMSIS-RTOS.
  528. */
  529. osMutexId osMutexCreate (const osMutexDef_t *mutex_def)
  530. {
  531. #if ( configUSE_MUTEXES == 1)
  532. #if( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  533. if (mutex_def->controlblock != NULL) {
  534. return xSemaphoreCreateMutexStatic( mutex_def->controlblock );
  535. }
  536. else {
  537. return xSemaphoreCreateMutex();
  538. }
  539. #elif ( configSUPPORT_STATIC_ALLOCATION == 1 )
  540. return xSemaphoreCreateMutexStatic( mutex_def->controlblock );
  541. #else
  542. return xSemaphoreCreateMutex();
  543. #endif
  544. #else
  545. return NULL;
  546. #endif
  547. }
  548. /**
  549. * @brief Wait until a Mutex becomes available
  550. * @param mutex_id mutex ID obtained by \ref osMutexCreate.
  551. * @param millisec timeout value or 0 in case of no time-out.
  552. * @retval status code that indicates the execution status of the function.
  553. * @note MUST REMAIN UNCHANGED: \b osMutexWait shall be consistent in every CMSIS-RTOS.
  554. */
  555. osStatus osMutexWait (osMutexId mutex_id, uint32_t millisec)
  556. {
  557. TickType_t ticks;
  558. portBASE_TYPE taskWoken = pdFALSE;
  559. if (mutex_id == NULL) {
  560. return osErrorParameter;
  561. }
  562. ticks = 0;
  563. if (millisec == osWaitForever) {
  564. ticks = portMAX_DELAY;
  565. }
  566. else if (millisec != 0) {
  567. ticks = millisec / portTICK_PERIOD_MS;
  568. if (ticks == 0) {
  569. ticks = 1;
  570. }
  571. }
  572. if (inHandlerMode()) {
  573. if (xSemaphoreTakeFromISR(mutex_id, &taskWoken) != pdTRUE) {
  574. return osErrorOS;
  575. }
  576. portEND_SWITCHING_ISR(taskWoken);
  577. }
  578. else if (xSemaphoreTake(mutex_id, ticks) != pdTRUE) {
  579. return osErrorOS;
  580. }
  581. return osOK;
  582. }
  583. /**
  584. * @brief Release a Mutex that was obtained by \ref osMutexWait
  585. * @param mutex_id mutex ID obtained by \ref osMutexCreate.
  586. * @retval status code that indicates the execution status of the function.
  587. * @note MUST REMAIN UNCHANGED: \b osMutexRelease shall be consistent in every CMSIS-RTOS.
  588. */
  589. osStatus osMutexRelease (osMutexId mutex_id)
  590. {
  591. osStatus result = osOK;
  592. portBASE_TYPE taskWoken = pdFALSE;
  593. if (inHandlerMode()) {
  594. if (xSemaphoreGiveFromISR(mutex_id, &taskWoken) != pdTRUE) {
  595. return osErrorOS;
  596. }
  597. portEND_SWITCHING_ISR(taskWoken);
  598. }
  599. else if (xSemaphoreGive(mutex_id) != pdTRUE)
  600. {
  601. result = osErrorOS;
  602. }
  603. return result;
  604. }
  605. /**
  606. * @brief Delete a Mutex
  607. * @param mutex_id mutex ID obtained by \ref osMutexCreate.
  608. * @retval status code that indicates the execution status of the function.
  609. * @note MUST REMAIN UNCHANGED: \b osMutexDelete shall be consistent in every CMSIS-RTOS.
  610. */
  611. osStatus osMutexDelete (osMutexId mutex_id)
  612. {
  613. if (inHandlerMode()) {
  614. return osErrorISR;
  615. }
  616. vQueueDelete(mutex_id);
  617. return osOK;
  618. }
  619. /******************** Semaphore Management Functions **************************/
  620. #if (defined (osFeature_Semaphore) && (osFeature_Semaphore != 0))
  621. /**
  622. * @brief Create and Initialize a Semaphore object used for managing resources
  623. * @param semaphore_def semaphore definition referenced with \ref osSemaphore.
  624. * @param count number of available resources.
  625. * @retval semaphore ID for reference by other functions or NULL in case of error.
  626. * @note MUST REMAIN UNCHANGED: \b osSemaphoreCreate shall be consistent in every CMSIS-RTOS.
  627. */
  628. osSemaphoreId osSemaphoreCreate (const osSemaphoreDef_t *semaphore_def, int32_t count)
  629. {
  630. #if( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  631. osSemaphoreId sema;
  632. if (semaphore_def->controlblock != NULL){
  633. if (count == 1) {
  634. return xSemaphoreCreateBinaryStatic( semaphore_def->controlblock );
  635. }
  636. else {
  637. #if (configUSE_COUNTING_SEMAPHORES == 1 )
  638. return xSemaphoreCreateCountingStatic( count, count, semaphore_def->controlblock );
  639. #else
  640. return NULL;
  641. #endif
  642. }
  643. }
  644. else {
  645. if (count == 1) {
  646. vSemaphoreCreateBinary(sema);
  647. return sema;
  648. }
  649. else {
  650. #if (configUSE_COUNTING_SEMAPHORES == 1 )
  651. return xSemaphoreCreateCounting(count, count);
  652. #else
  653. return NULL;
  654. #endif
  655. }
  656. }
  657. #elif ( configSUPPORT_STATIC_ALLOCATION == 1 ) // configSUPPORT_DYNAMIC_ALLOCATION == 0
  658. if(count == 1) {
  659. return xSemaphoreCreateBinaryStatic( semaphore_def->controlblock );
  660. }
  661. else
  662. {
  663. #if (configUSE_COUNTING_SEMAPHORES == 1 )
  664. return xSemaphoreCreateCountingStatic( count, count, semaphore_def->controlblock );
  665. #else
  666. return NULL;
  667. #endif
  668. }
  669. #else // configSUPPORT_STATIC_ALLOCATION == 0 && configSUPPORT_DYNAMIC_ALLOCATION == 1
  670. osSemaphoreId sema;
  671. if (count == 1) {
  672. vSemaphoreCreateBinary(sema);
  673. return sema;
  674. }
  675. else {
  676. #if (configUSE_COUNTING_SEMAPHORES == 1 )
  677. return xSemaphoreCreateCounting(count, count);
  678. #else
  679. return NULL;
  680. #endif
  681. }
  682. #endif
  683. }
  684. /**
  685. * @brief Wait until a Semaphore token becomes available
  686. * @param semaphore_id semaphore object referenced with \ref osSemaphore.
  687. * @param millisec timeout value or 0 in case of no time-out.
  688. * @retval number of available tokens, or -1 in case of incorrect parameters.
  689. * @note MUST REMAIN UNCHANGED: \b osSemaphoreWait shall be consistent in every CMSIS-RTOS.
  690. */
  691. int32_t osSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec)
  692. {
  693. TickType_t ticks;
  694. portBASE_TYPE taskWoken = pdFALSE;
  695. if (semaphore_id == NULL) {
  696. return osErrorParameter;
  697. }
  698. ticks = 0;
  699. if (millisec == osWaitForever) {
  700. ticks = portMAX_DELAY;
  701. }
  702. else if (millisec != 0) {
  703. ticks = millisec / portTICK_PERIOD_MS;
  704. if (ticks == 0) {
  705. ticks = 1;
  706. }
  707. }
  708. if (inHandlerMode()) {
  709. if (xSemaphoreTakeFromISR(semaphore_id, &taskWoken) != pdTRUE) {
  710. return osErrorOS;
  711. }
  712. portEND_SWITCHING_ISR(taskWoken);
  713. }
  714. else if (xSemaphoreTake(semaphore_id, ticks) != pdTRUE) {
  715. return osErrorOS;
  716. }
  717. return osOK;
  718. }
  719. /**
  720. * @brief Release a Semaphore token
  721. * @param semaphore_id semaphore object referenced with \ref osSemaphore.
  722. * @retval status code that indicates the execution status of the function.
  723. * @note MUST REMAIN UNCHANGED: \b osSemaphoreRelease shall be consistent in every CMSIS-RTOS.
  724. */
  725. osStatus osSemaphoreRelease (osSemaphoreId semaphore_id)
  726. {
  727. osStatus result = osOK;
  728. portBASE_TYPE taskWoken = pdFALSE;
  729. if (inHandlerMode()) {
  730. if (xSemaphoreGiveFromISR(semaphore_id, &taskWoken) != pdTRUE) {
  731. return osErrorOS;
  732. }
  733. portEND_SWITCHING_ISR(taskWoken);
  734. }
  735. else {
  736. if (xSemaphoreGive(semaphore_id) != pdTRUE) {
  737. result = osErrorOS;
  738. }
  739. }
  740. return result;
  741. }
  742. /**
  743. * @brief Delete a Semaphore
  744. * @param semaphore_id semaphore object referenced with \ref osSemaphore.
  745. * @retval status code that indicates the execution status of the function.
  746. * @note MUST REMAIN UNCHANGED: \b osSemaphoreDelete shall be consistent in every CMSIS-RTOS.
  747. */
  748. osStatus osSemaphoreDelete (osSemaphoreId semaphore_id)
  749. {
  750. if (inHandlerMode()) {
  751. return osErrorISR;
  752. }
  753. vSemaphoreDelete(semaphore_id);
  754. return osOK;
  755. }
  756. #endif /* Use Semaphores */
  757. /******************* Memory Pool Management Functions ***********************/
  758. #if (defined (osFeature_Pool) && (osFeature_Pool != 0))
  759. //TODO
  760. //This is a primitive and inefficient wrapper around the existing FreeRTOS memory management.
  761. //A better implementation will have to modify heap_x.c!
  762. typedef struct os_pool_cb {
  763. void *pool;
  764. uint8_t *markers;
  765. uint32_t pool_sz;
  766. uint32_t item_sz;
  767. uint32_t currentIndex;
  768. } os_pool_cb_t;
  769. /**
  770. * @brief Create and Initialize a memory pool
  771. * @param pool_def memory pool definition referenced with \ref osPool.
  772. * @retval memory pool ID for reference by other functions or NULL in case of error.
  773. * @note MUST REMAIN UNCHANGED: \b osPoolCreate shall be consistent in every CMSIS-RTOS.
  774. */
  775. osPoolId osPoolCreate (const osPoolDef_t *pool_def)
  776. {
  777. #if (configSUPPORT_DYNAMIC_ALLOCATION == 1)
  778. osPoolId thePool;
  779. int itemSize = 4 * ((pool_def->item_sz + 3) / 4);
  780. uint32_t i;
  781. /* First have to allocate memory for the pool control block. */
  782. thePool = pvPortMalloc(sizeof(os_pool_cb_t));
  783. if (thePool) {
  784. thePool->pool_sz = pool_def->pool_sz;
  785. thePool->item_sz = itemSize;
  786. thePool->currentIndex = 0;
  787. /* Memory for markers */
  788. thePool->markers = pvPortMalloc(pool_def->pool_sz);
  789. if (thePool->markers) {
  790. /* Now allocate the pool itself. */
  791. thePool->pool = pvPortMalloc(pool_def->pool_sz * itemSize);
  792. if (thePool->pool) {
  793. for (i = 0; i < pool_def->pool_sz; i++) {
  794. thePool->markers[i] = 0;
  795. }
  796. }
  797. else {
  798. vPortFree(thePool->markers);
  799. vPortFree(thePool);
  800. thePool = NULL;
  801. }
  802. }
  803. else {
  804. vPortFree(thePool);
  805. thePool = NULL;
  806. }
  807. }
  808. return thePool;
  809. #else
  810. return NULL;
  811. #endif
  812. }
  813. /**
  814. * @brief Allocate a memory block from a memory pool
  815. * @param pool_id memory pool ID obtain referenced with \ref osPoolCreate.
  816. * @retval address of the allocated memory block or NULL in case of no memory available.
  817. * @note MUST REMAIN UNCHANGED: \b osPoolAlloc shall be consistent in every CMSIS-RTOS.
  818. */
  819. void *osPoolAlloc (osPoolId pool_id)
  820. {
  821. int dummy = 0;
  822. void *p = NULL;
  823. uint32_t i;
  824. uint32_t index;
  825. if (inHandlerMode()) {
  826. dummy = portSET_INTERRUPT_MASK_FROM_ISR();
  827. }
  828. else {
  829. vPortEnterCritical();
  830. }
  831. for (i = 0; i < pool_id->pool_sz; i++) {
  832. index = (pool_id->currentIndex + i) % pool_id->pool_sz;
  833. if (pool_id->markers[index] == 0) {
  834. pool_id->markers[index] = 1;
  835. p = (void *)((uint32_t)(pool_id->pool) + (index * pool_id->item_sz));
  836. pool_id->currentIndex = index;
  837. break;
  838. }
  839. }
  840. if (inHandlerMode()) {
  841. portCLEAR_INTERRUPT_MASK_FROM_ISR(dummy);
  842. }
  843. else {
  844. vPortExitCritical();
  845. }
  846. return p;
  847. }
  848. /**
  849. * @brief Allocate a memory block from a memory pool and set memory block to zero
  850. * @param pool_id memory pool ID obtain referenced with \ref osPoolCreate.
  851. * @retval address of the allocated memory block or NULL in case of no memory available.
  852. * @note MUST REMAIN UNCHANGED: \b osPoolCAlloc shall be consistent in every CMSIS-RTOS.
  853. */
  854. void *osPoolCAlloc (osPoolId pool_id)
  855. {
  856. void *p = osPoolAlloc(pool_id);
  857. if (p != NULL)
  858. {
  859. memset(p, 0, sizeof(pool_id->pool_sz));
  860. }
  861. return p;
  862. }
  863. /**
  864. * @brief Return an allocated memory block back to a specific memory pool
  865. * @param pool_id memory pool ID obtain referenced with \ref osPoolCreate.
  866. * @param block address of the allocated memory block that is returned to the memory pool.
  867. * @retval status code that indicates the execution status of the function.
  868. * @note MUST REMAIN UNCHANGED: \b osPoolFree shall be consistent in every CMSIS-RTOS.
  869. */
  870. osStatus osPoolFree (osPoolId pool_id, void *block)
  871. {
  872. uint32_t index;
  873. if (pool_id == NULL) {
  874. return osErrorParameter;
  875. }
  876. if (block == NULL) {
  877. return osErrorParameter;
  878. }
  879. if (block < pool_id->pool) {
  880. return osErrorParameter;
  881. }
  882. index = (uint32_t)block - (uint32_t)(pool_id->pool);
  883. if (index % pool_id->item_sz) {
  884. return osErrorParameter;
  885. }
  886. index = index / pool_id->item_sz;
  887. if (index >= pool_id->pool_sz) {
  888. return osErrorParameter;
  889. }
  890. pool_id->markers[index] = 0;
  891. return osOK;
  892. }
  893. #endif /* Use Memory Pool Management */
  894. /******************* Message Queue Management Functions *********************/
  895. #if (defined (osFeature_MessageQ) && (osFeature_MessageQ != 0)) /* Use Message Queues */
  896. /**
  897. * @brief Create and Initialize a Message Queue
  898. * @param queue_def queue definition referenced with \ref osMessageQ.
  899. * @param thread_id thread ID (obtained by \ref osThreadCreate or \ref osThreadGetId) or NULL.
  900. * @retval message queue ID for reference by other functions or NULL in case of error.
  901. * @note MUST REMAIN UNCHANGED: \b osMessageCreate shall be consistent in every CMSIS-RTOS.
  902. */
  903. osMessageQId osMessageCreate (const osMessageQDef_t *queue_def, osThreadId thread_id)
  904. {
  905. (void) thread_id;
  906. #if( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  907. if ((queue_def->buffer != NULL) && (queue_def->controlblock != NULL)) {
  908. return xQueueCreateStatic(queue_def->queue_sz, queue_def->item_sz, queue_def->buffer, queue_def->controlblock);
  909. }
  910. else {
  911. return xQueueCreate(queue_def->queue_sz, queue_def->item_sz);
  912. }
  913. #elif ( configSUPPORT_STATIC_ALLOCATION == 1 )
  914. return xQueueCreateStatic(queue_def->queue_sz, queue_def->item_sz, queue_def->buffer, queue_def->controlblock);
  915. #else
  916. return xQueueCreate(queue_def->queue_sz, queue_def->item_sz);
  917. #endif
  918. }
  919. /**
  920. * @brief Put a Message to a Queue.
  921. * @param queue_id message queue ID obtained with \ref osMessageCreate.
  922. * @param info message information.
  923. * @param millisec timeout value or 0 in case of no time-out.
  924. * @retval status code that indicates the execution status of the function.
  925. * @note MUST REMAIN UNCHANGED: \b osMessagePut shall be consistent in every CMSIS-RTOS.
  926. */
  927. osStatus osMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec)
  928. {
  929. portBASE_TYPE taskWoken = pdFALSE;
  930. TickType_t ticks;
  931. ticks = millisec / portTICK_PERIOD_MS;
  932. if (ticks == 0) {
  933. ticks = 1;
  934. }
  935. if (inHandlerMode()) {
  936. if (xQueueSendFromISR(queue_id, &info, &taskWoken) != pdTRUE) {
  937. return osErrorOS;
  938. }
  939. portEND_SWITCHING_ISR(taskWoken);
  940. }
  941. else {
  942. if (xQueueSend(queue_id, &info, ticks) != pdTRUE) {
  943. return osErrorOS;
  944. }
  945. }
  946. return osOK;
  947. }
  948. /**
  949. * @brief Get a Message or Wait for a Message from a Queue.
  950. * @param queue_id message queue ID obtained with \ref osMessageCreate.
  951. * @param millisec timeout value or 0 in case of no time-out.
  952. * @retval event information that includes status code.
  953. * @note MUST REMAIN UNCHANGED: \b osMessageGet shall be consistent in every CMSIS-RTOS.
  954. */
  955. osEvent osMessageGet (osMessageQId queue_id, uint32_t millisec)
  956. {
  957. portBASE_TYPE taskWoken;
  958. TickType_t ticks;
  959. osEvent event;
  960. event.def.message_id = queue_id;
  961. event.value.v = 0;
  962. if (queue_id == NULL) {
  963. event.status = osErrorParameter;
  964. return event;
  965. }
  966. taskWoken = pdFALSE;
  967. ticks = 0;
  968. if (millisec == osWaitForever) {
  969. ticks = portMAX_DELAY;
  970. }
  971. else if (millisec != 0) {
  972. ticks = millisec / portTICK_PERIOD_MS;
  973. if (ticks == 0) {
  974. ticks = 1;
  975. }
  976. }
  977. if (inHandlerMode()) {
  978. if (xQueueReceiveFromISR(queue_id, &event.value.v, &taskWoken) == pdTRUE) {
  979. /* We have mail */
  980. event.status = osEventMessage;
  981. }
  982. else {
  983. event.status = osOK;
  984. }
  985. portEND_SWITCHING_ISR(taskWoken);
  986. }
  987. else {
  988. if (xQueueReceive(queue_id, &event.value.v, ticks) == pdTRUE) {
  989. /* We have mail */
  990. event.status = osEventMessage;
  991. }
  992. else {
  993. event.status = (ticks == 0) ? osOK : osEventTimeout;
  994. }
  995. }
  996. return event;
  997. }
  998. #endif /* Use Message Queues */
  999. /******************** Mail Queue Management Functions ***********************/
  1000. #if (defined (osFeature_MailQ) && (osFeature_MailQ != 0)) /* Use Mail Queues */
  1001. typedef struct os_mailQ_cb {
  1002. const osMailQDef_t *queue_def;
  1003. QueueHandle_t handle;
  1004. osPoolId pool;
  1005. } os_mailQ_cb_t;
  1006. /**
  1007. * @brief Create and Initialize mail queue
  1008. * @param queue_def reference to the mail queue definition obtain with \ref osMailQ
  1009. * @param thread_id thread ID (obtained by \ref osThreadCreate or \ref osThreadGetId) or NULL.
  1010. * @retval mail queue ID for reference by other functions or NULL in case of error.
  1011. * @note MUST REMAIN UNCHANGED: \b osMailCreate shall be consistent in every CMSIS-RTOS.
  1012. */
  1013. osMailQId osMailCreate (const osMailQDef_t *queue_def, osThreadId thread_id)
  1014. {
  1015. #if (configSUPPORT_DYNAMIC_ALLOCATION == 1)
  1016. (void) thread_id;
  1017. osPoolDef_t pool_def = {queue_def->queue_sz, queue_def->item_sz, NULL};
  1018. /* Create a mail queue control block */
  1019. *(queue_def->cb) = pvPortMalloc(sizeof(struct os_mailQ_cb));
  1020. if (*(queue_def->cb) == NULL) {
  1021. return NULL;
  1022. }
  1023. (*(queue_def->cb))->queue_def = queue_def;
  1024. /* Create a queue in FreeRTOS */
  1025. (*(queue_def->cb))->handle = xQueueCreate(queue_def->queue_sz, sizeof(void *));
  1026. if ((*(queue_def->cb))->handle == NULL) {
  1027. vPortFree(*(queue_def->cb));
  1028. return NULL;
  1029. }
  1030. /* Create a mail pool */
  1031. (*(queue_def->cb))->pool = osPoolCreate(&pool_def);
  1032. if ((*(queue_def->cb))->pool == NULL) {
  1033. //TODO: Delete queue. How to do it in FreeRTOS?
  1034. vPortFree(*(queue_def->cb));
  1035. return NULL;
  1036. }
  1037. return *(queue_def->cb);
  1038. #else
  1039. return NULL;
  1040. #endif
  1041. }
  1042. /**
  1043. * @brief Allocate a memory block from a mail
  1044. * @param queue_id mail queue ID obtained with \ref osMailCreate.
  1045. * @param millisec timeout value or 0 in case of no time-out.
  1046. * @retval pointer to memory block that can be filled with mail or NULL in case error.
  1047. * @note MUST REMAIN UNCHANGED: \b osMailAlloc shall be consistent in every CMSIS-RTOS.
  1048. */
  1049. void *osMailAlloc (osMailQId queue_id, uint32_t millisec)
  1050. {
  1051. (void) millisec;
  1052. void *p;
  1053. if (queue_id == NULL) {
  1054. return NULL;
  1055. }
  1056. p = osPoolAlloc(queue_id->pool);
  1057. return p;
  1058. }
  1059. /**
  1060. * @brief Allocate a memory block from a mail and set memory block to zero
  1061. * @param queue_id mail queue ID obtained with \ref osMailCreate.
  1062. * @param millisec timeout value or 0 in case of no time-out.
  1063. * @retval pointer to memory block that can be filled with mail or NULL in case error.
  1064. * @note MUST REMAIN UNCHANGED: \b osMailCAlloc shall be consistent in every CMSIS-RTOS.
  1065. */
  1066. void *osMailCAlloc (osMailQId queue_id, uint32_t millisec)
  1067. {
  1068. uint32_t i;
  1069. void *p = osMailAlloc(queue_id, millisec);
  1070. if (p) {
  1071. for (i = 0; i < queue_id->queue_def->item_sz; i++) {
  1072. ((uint8_t *)p)[i] = 0;
  1073. }
  1074. }
  1075. return p;
  1076. }
  1077. /**
  1078. * @brief Put a mail to a queue
  1079. * @param queue_id mail queue ID obtained with \ref osMailCreate.
  1080. * @param mail memory block previously allocated with \ref osMailAlloc or \ref osMailCAlloc.
  1081. * @retval status code that indicates the execution status of the function.
  1082. * @note MUST REMAIN UNCHANGED: \b osMailPut shall be consistent in every CMSIS-RTOS.
  1083. */
  1084. osStatus osMailPut (osMailQId queue_id, void *mail)
  1085. {
  1086. portBASE_TYPE taskWoken;
  1087. if (queue_id == NULL) {
  1088. return osErrorParameter;
  1089. }
  1090. taskWoken = pdFALSE;
  1091. if (inHandlerMode()) {
  1092. if (xQueueSendFromISR(queue_id->handle, &mail, &taskWoken) != pdTRUE) {
  1093. return osErrorOS;
  1094. }
  1095. portEND_SWITCHING_ISR(taskWoken);
  1096. }
  1097. else {
  1098. if (xQueueSend(queue_id->handle, &mail, 0) != pdTRUE) {
  1099. return osErrorOS;
  1100. }
  1101. }
  1102. return osOK;
  1103. }
  1104. /**
  1105. * @brief Get a mail from a queue
  1106. * @param queue_id mail queue ID obtained with \ref osMailCreate.
  1107. * @param millisec timeout value or 0 in case of no time-out
  1108. * @retval event that contains mail information or error code.
  1109. * @note MUST REMAIN UNCHANGED: \b osMailGet shall be consistent in every CMSIS-RTOS.
  1110. */
  1111. osEvent osMailGet (osMailQId queue_id, uint32_t millisec)
  1112. {
  1113. portBASE_TYPE taskWoken;
  1114. TickType_t ticks;
  1115. osEvent event;
  1116. event.def.mail_id = queue_id;
  1117. if (queue_id == NULL) {
  1118. event.status = osErrorParameter;
  1119. return event;
  1120. }
  1121. taskWoken = pdFALSE;
  1122. ticks = 0;
  1123. if (millisec == osWaitForever) {
  1124. ticks = portMAX_DELAY;
  1125. }
  1126. else if (millisec != 0) {
  1127. ticks = millisec / portTICK_PERIOD_MS;
  1128. if (ticks == 0) {
  1129. ticks = 1;
  1130. }
  1131. }
  1132. if (inHandlerMode()) {
  1133. if (xQueueReceiveFromISR(queue_id->handle, &event.value.p, &taskWoken) == pdTRUE) {
  1134. /* We have mail */
  1135. event.status = osEventMail;
  1136. }
  1137. else {
  1138. event.status = osOK;
  1139. }
  1140. portEND_SWITCHING_ISR(taskWoken);
  1141. }
  1142. else {
  1143. if (xQueueReceive(queue_id->handle, &event.value.p, ticks) == pdTRUE) {
  1144. /* We have mail */
  1145. event.status = osEventMail;
  1146. }
  1147. else {
  1148. event.status = (ticks == 0) ? osOK : osEventTimeout;
  1149. }
  1150. }
  1151. return event;
  1152. }
  1153. /**
  1154. * @brief Free a memory block from a mail
  1155. * @param queue_id mail queue ID obtained with \ref osMailCreate.
  1156. * @param mail pointer to the memory block that was obtained with \ref osMailGet.
  1157. * @retval status code that indicates the execution status of the function.
  1158. * @note MUST REMAIN UNCHANGED: \b osMailFree shall be consistent in every CMSIS-RTOS.
  1159. */
  1160. osStatus osMailFree (osMailQId queue_id, void *mail)
  1161. {
  1162. if (queue_id == NULL) {
  1163. return osErrorParameter;
  1164. }
  1165. return osPoolFree(queue_id->pool, mail);
  1166. }
  1167. #endif /* Use Mail Queues */
  1168. /*************************** Additional specific APIs to Free RTOS ************/
  1169. /**
  1170. * @brief Handles the tick increment
  1171. * @param none.
  1172. * @retval none.
  1173. */
  1174. void osSystickHandler(void)
  1175. {
  1176. #if (INCLUDE_xTaskGetSchedulerState == 1 )
  1177. if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
  1178. {
  1179. #endif /* INCLUDE_xTaskGetSchedulerState */
  1180. xPortSysTickHandler();
  1181. #if (INCLUDE_xTaskGetSchedulerState == 1 )
  1182. }
  1183. #endif /* INCLUDE_xTaskGetSchedulerState */
  1184. }
  1185. #if ( INCLUDE_eTaskGetState == 1 )
  1186. /**
  1187. * @brief Obtain the state of any thread.
  1188. * @param thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  1189. * @retval the stae of the thread, states are encoded by the osThreadState enumerated type.
  1190. */
  1191. osThreadState osThreadGetState(osThreadId thread_id)
  1192. {
  1193. eTaskState ThreadState;
  1194. osThreadState result;
  1195. ThreadState = eTaskGetState(thread_id);
  1196. switch (ThreadState)
  1197. {
  1198. case eRunning :
  1199. result = osThreadRunning;
  1200. break;
  1201. case eReady :
  1202. result = osThreadReady;
  1203. break;
  1204. case eBlocked :
  1205. result = osThreadBlocked;
  1206. break;
  1207. case eSuspended :
  1208. result = osThreadSuspended;
  1209. break;
  1210. case eDeleted :
  1211. result = osThreadDeleted;
  1212. break;
  1213. default:
  1214. result = osThreadError;
  1215. }
  1216. return result;
  1217. }
  1218. #endif /* INCLUDE_eTaskGetState */
  1219. #if (INCLUDE_eTaskGetState == 1)
  1220. /**
  1221. * @brief Check if a thread is already suspended or not.
  1222. * @param thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  1223. * @retval status code that indicates the execution status of the function.
  1224. */
  1225. osStatus osThreadIsSuspended(osThreadId thread_id)
  1226. {
  1227. if (eTaskGetState(thread_id) == eSuspended)
  1228. return osOK;
  1229. else
  1230. return osErrorOS;
  1231. }
  1232. #endif /* INCLUDE_eTaskGetState */
  1233. /**
  1234. * @brief Suspend execution of a thread.
  1235. * @param thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  1236. * @retval status code that indicates the execution status of the function.
  1237. */
  1238. osStatus osThreadSuspend (osThreadId thread_id)
  1239. {
  1240. #if (INCLUDE_vTaskSuspend == 1)
  1241. vTaskSuspend(thread_id);
  1242. return osOK;
  1243. #else
  1244. return osErrorResource;
  1245. #endif
  1246. }
  1247. /**
  1248. * @brief Resume execution of a suspended thread.
  1249. * @param thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  1250. * @retval status code that indicates the execution status of the function.
  1251. */
  1252. osStatus osThreadResume (osThreadId thread_id)
  1253. {
  1254. #if (INCLUDE_vTaskSuspend == 1)
  1255. if(inHandlerMode())
  1256. {
  1257. if (xTaskResumeFromISR(thread_id) == pdTRUE)
  1258. {
  1259. portYIELD_FROM_ISR(pdTRUE);
  1260. }
  1261. }
  1262. else
  1263. {
  1264. vTaskResume(thread_id);
  1265. }
  1266. return osOK;
  1267. #else
  1268. return osErrorResource;
  1269. #endif
  1270. }
  1271. /**
  1272. * @brief Suspend execution of a all active threads.
  1273. * @retval status code that indicates the execution status of the function.
  1274. */
  1275. osStatus osThreadSuspendAll (void)
  1276. {
  1277. vTaskSuspendAll();
  1278. return osOK;
  1279. }
  1280. /**
  1281. * @brief Resume execution of a all suspended threads.
  1282. * @retval status code that indicates the execution status of the function.
  1283. */
  1284. osStatus osThreadResumeAll (void)
  1285. {
  1286. if (xTaskResumeAll() == pdTRUE)
  1287. return osOK;
  1288. else
  1289. return osErrorOS;
  1290. }
  1291. /**
  1292. * @brief Delay a task until a specified time
  1293. * @param PreviousWakeTime Pointer to a variable that holds the time at which the
  1294. * task was last unblocked. PreviousWakeTime must be initialised with the current time
  1295. * prior to its first use (PreviousWakeTime = osKernelSysTick() )
  1296. * @param millisec time delay value
  1297. * @retval status code that indicates the execution status of the function.
  1298. */
  1299. osStatus osDelayUntil (uint32_t *PreviousWakeTime, uint32_t millisec)
  1300. {
  1301. #if INCLUDE_vTaskDelayUntil
  1302. TickType_t ticks = (millisec / portTICK_PERIOD_MS);
  1303. vTaskDelayUntil((TickType_t *) PreviousWakeTime, ticks ? ticks : 1);
  1304. return osOK;
  1305. #else
  1306. (void) millisec;
  1307. (void) PreviousWakeTime;
  1308. return osErrorResource;
  1309. #endif
  1310. }
  1311. /**
  1312. * @brief Abort the delay for a specific thread
  1313. * @param thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId
  1314. * @retval status code that indicates the execution status of the function.
  1315. */
  1316. osStatus osAbortDelay(osThreadId thread_id)
  1317. {
  1318. #if INCLUDE_xTaskAbortDelay
  1319. xTaskAbortDelay(thread_id);
  1320. return osOK;
  1321. #else
  1322. (void) thread_id;
  1323. return osErrorResource;
  1324. #endif
  1325. }
  1326. /**
  1327. * @brief Lists all the current threads, along with their current state
  1328. * and stack usage high water mark.
  1329. * @param buffer A buffer into which the above mentioned details
  1330. * will be written
  1331. * @retval status code that indicates the execution status of the function.
  1332. */
  1333. osStatus osThreadList (uint8_t *buffer)
  1334. {
  1335. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) )
  1336. vTaskList((char *)buffer);
  1337. #endif
  1338. return osOK;
  1339. }
  1340. /**
  1341. * @brief Receive an item from a queue without removing the item from the queue.
  1342. * @param queue_id message queue ID obtained with \ref osMessageCreate.
  1343. * @param millisec timeout value or 0 in case of no time-out.
  1344. * @retval event information that includes status code.
  1345. */
  1346. osEvent osMessagePeek (osMessageQId queue_id, uint32_t millisec)
  1347. {
  1348. TickType_t ticks;
  1349. osEvent event;
  1350. event.def.message_id = queue_id;
  1351. if (queue_id == NULL) {
  1352. event.status = osErrorParameter;
  1353. return event;
  1354. }
  1355. ticks = 0;
  1356. if (millisec == osWaitForever) {
  1357. ticks = portMAX_DELAY;
  1358. }
  1359. else if (millisec != 0) {
  1360. ticks = millisec / portTICK_PERIOD_MS;
  1361. if (ticks == 0) {
  1362. ticks = 1;
  1363. }
  1364. }
  1365. if (xQueuePeek(queue_id, &event.value.v, ticks) == pdTRUE)
  1366. {
  1367. /* We have mail */
  1368. event.status = osEventMessage;
  1369. }
  1370. else
  1371. {
  1372. event.status = (ticks == 0) ? osOK : osEventTimeout;
  1373. }
  1374. return event;
  1375. }
  1376. /**
  1377. * @brief Get the number of messaged stored in a queue.
  1378. * @param queue_id message queue ID obtained with \ref osMessageCreate.
  1379. * @retval number of messages stored in a queue.
  1380. */
  1381. uint32_t osMessageWaiting(osMessageQId queue_id)
  1382. {
  1383. if (inHandlerMode()) {
  1384. return uxQueueMessagesWaitingFromISR(queue_id);
  1385. }
  1386. else
  1387. {
  1388. return uxQueueMessagesWaiting(queue_id);
  1389. }
  1390. }
  1391. /**
  1392. * @brief Get the available space in a message queue.
  1393. * @param queue_id message queue ID obtained with \ref osMessageCreate.
  1394. * @retval available space in a message queue.
  1395. */
  1396. uint32_t osMessageAvailableSpace(osMessageQId queue_id)
  1397. {
  1398. return uxQueueSpacesAvailable(queue_id);
  1399. }
  1400. /**
  1401. * @brief Delete a Message Queue
  1402. * @param queue_id message queue ID obtained with \ref osMessageCreate.
  1403. * @retval status code that indicates the execution status of the function.
  1404. */
  1405. osStatus osMessageDelete (osMessageQId queue_id)
  1406. {
  1407. if (inHandlerMode()) {
  1408. return osErrorISR;
  1409. }
  1410. vQueueDelete(queue_id);
  1411. return osOK;
  1412. }
  1413. /**
  1414. * @brief Create and Initialize a Recursive Mutex
  1415. * @param mutex_def mutex definition referenced with \ref osMutex.
  1416. * @retval mutex ID for reference by other functions or NULL in case of error..
  1417. */
  1418. osMutexId osRecursiveMutexCreate (const osMutexDef_t *mutex_def)
  1419. {
  1420. #if (configUSE_RECURSIVE_MUTEXES == 1)
  1421. #if( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  1422. if (mutex_def->controlblock != NULL){
  1423. return xSemaphoreCreateRecursiveMutexStatic( mutex_def->controlblock );
  1424. }
  1425. else {
  1426. return xSemaphoreCreateRecursiveMutex();
  1427. }
  1428. #elif ( configSUPPORT_STATIC_ALLOCATION == 1 )
  1429. return xSemaphoreCreateRecursiveMutexStatic( mutex_def->controlblock );
  1430. #else
  1431. return xSemaphoreCreateRecursiveMutex();
  1432. #endif
  1433. #else
  1434. return NULL;
  1435. #endif
  1436. }
  1437. /**
  1438. * @brief Release a Recursive Mutex
  1439. * @param mutex_id mutex ID obtained by \ref osRecursiveMutexCreate.
  1440. * @retval status code that indicates the execution status of the function.
  1441. */
  1442. osStatus osRecursiveMutexRelease (osMutexId mutex_id)
  1443. {
  1444. #if (configUSE_RECURSIVE_MUTEXES == 1)
  1445. osStatus result = osOK;
  1446. if (xSemaphoreGiveRecursive(mutex_id) != pdTRUE)
  1447. {
  1448. result = osErrorOS;
  1449. }
  1450. return result;
  1451. #else
  1452. return osErrorResource;
  1453. #endif
  1454. }
  1455. /**
  1456. * @brief Release a Recursive Mutex
  1457. * @param mutex_id mutex ID obtained by \ref osRecursiveMutexCreate.
  1458. * @param millisec timeout value or 0 in case of no time-out.
  1459. * @retval status code that indicates the execution status of the function.
  1460. */
  1461. osStatus osRecursiveMutexWait (osMutexId mutex_id, uint32_t millisec)
  1462. {
  1463. #if (configUSE_RECURSIVE_MUTEXES == 1)
  1464. TickType_t ticks;
  1465. if (mutex_id == NULL)
  1466. {
  1467. return osErrorParameter;
  1468. }
  1469. ticks = 0;
  1470. if (millisec == osWaitForever)
  1471. {
  1472. ticks = portMAX_DELAY;
  1473. }
  1474. else if (millisec != 0)
  1475. {
  1476. ticks = millisec / portTICK_PERIOD_MS;
  1477. if (ticks == 0)
  1478. {
  1479. ticks = 1;
  1480. }
  1481. }
  1482. if (xSemaphoreTakeRecursive(mutex_id, ticks) != pdTRUE)
  1483. {
  1484. return osErrorOS;
  1485. }
  1486. return osOK;
  1487. #else
  1488. return osErrorResource;
  1489. #endif
  1490. }
  1491. /**
  1492. * @brief Returns the current count value of a counting semaphore
  1493. * @param semaphore_id semaphore_id ID obtained by \ref osSemaphoreCreate.
  1494. * @retval count value
  1495. */
  1496. uint32_t osSemaphoreGetCount(osSemaphoreId semaphore_id)
  1497. {
  1498. return uxSemaphoreGetCount(semaphore_id);
  1499. }