Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rt_CMSIS.c 57KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885
  1. /*----------------------------------------------------------------------------
  2. * RL-ARM - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: rt_CMSIS.c
  5. * Purpose: CMSIS RTOS API
  6. * Rev.: V4.60
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
  10. * All rights reserved.
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * - Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * - Neither the name of ARM nor the names of its contributors may be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *---------------------------------------------------------------------------*/
  34. #define __CMSIS_GENERIC
  35. #if defined (__CORTEX_M4) || defined (__CORTEX_M4F)
  36. #include "core_cm4.h"
  37. #elif defined (__CORTEX_M3)
  38. #include "core_cm3.h"
  39. #elif defined (__CORTEX_M0)
  40. #include "core_cm0.h"
  41. #elif defined (__CORTEX_M0PLUS)
  42. #include "core_cm0plus.h"
  43. #else
  44. #error "Missing __CORTEX_Mx definition"
  45. #endif
  46. #include "rt_TypeDef.h"
  47. #include "RTX_Conf.h"
  48. #include "rt_System.h"
  49. #include "rt_Task.h"
  50. #include "rt_Event.h"
  51. #include "rt_List.h"
  52. #include "rt_Time.h"
  53. #include "rt_Mutex.h"
  54. #include "rt_Semaphore.h"
  55. #include "rt_Mailbox.h"
  56. #include "rt_MemBox.h"
  57. #include "rt_HAL_CM.h"
  58. #define os_thread_cb OS_TCB
  59. #include "cmsis_os.h"
  60. #if (osFeature_Signals != 16)
  61. #error Invalid "osFeature_Signals" value!
  62. #endif
  63. #if (osFeature_Semaphore > 65535)
  64. #error Invalid "osFeature_Semaphore" value!
  65. #endif
  66. #if (osFeature_Wait != 0)
  67. #error osWait not supported!
  68. #endif
  69. // ==== Enumeration, structures, defines ====
  70. // Service Calls defines
  71. #if defined (__CC_ARM) /* ARM Compiler */
  72. #define __NO_RETURN __declspec(noreturn)
  73. #define osEvent_type osEvent
  74. #define osEvent_ret_status ret
  75. #define osEvent_ret_value ret
  76. #define osEvent_ret_msg ret
  77. #define osEvent_ret_mail ret
  78. #define osCallback_type osCallback
  79. #define osCallback_ret ret
  80. #define SVC_0_1(f,t,...) \
  81. __svc_indirect(0) t _##f (t(*)()); \
  82. t f (void); \
  83. __attribute__((always_inline)) \
  84. static __inline t __##f (void) { \
  85. return _##f(f); \
  86. }
  87. #define SVC_1_1(f,t,t1,...) \
  88. __svc_indirect(0) t _##f (t(*)(t1),t1); \
  89. t f (t1 a1); \
  90. __attribute__((always_inline)) \
  91. static __inline t __##f (t1 a1) { \
  92. return _##f(f,a1); \
  93. }
  94. #define SVC_2_1(f,t,t1,t2,...) \
  95. __svc_indirect(0) t _##f (t(*)(t1,t2),t1,t2); \
  96. t f (t1 a1, t2 a2); \
  97. __attribute__((always_inline)) \
  98. static __inline t __##f (t1 a1, t2 a2) { \
  99. return _##f(f,a1,a2); \
  100. }
  101. #define SVC_3_1(f,t,t1,t2,t3,...) \
  102. __svc_indirect(0) t _##f (t(*)(t1,t2,t3),t1,t2,t3); \
  103. t f (t1 a1, t2 a2, t3 a3); \
  104. __attribute__((always_inline)) \
  105. static __inline t __##f (t1 a1, t2 a2, t3 a3) { \
  106. return _##f(f,a1,a2,a3); \
  107. }
  108. #define SVC_4_1(f,t,t1,t2,t3,t4,...) \
  109. __svc_indirect(0) t _##f (t(*)(t1,t2,t3,t4),t1,t2,t3,t4); \
  110. t f (t1 a1, t2 a2, t3 a3, t4 a4); \
  111. __attribute__((always_inline)) \
  112. static __inline t __##f (t1 a1, t2 a2, t3 a3, t4 a4) { \
  113. return _##f(f,a1,a2,a3,a4); \
  114. }
  115. #define SVC_1_2 SVC_1_1
  116. #define SVC_1_3 SVC_1_1
  117. #define SVC_2_3 SVC_2_1
  118. #elif defined (__GNUC__) /* GNU Compiler */
  119. #define __NO_RETURN __attribute__((noreturn))
  120. typedef uint32_t __attribute__((vector_size(8))) ret64;
  121. typedef uint32_t __attribute__((vector_size(16))) ret128;
  122. #define RET_pointer __r0
  123. #define RET_int32_t __r0
  124. #define RET_osStatus __r0
  125. #define RET_osPriority __r0
  126. #define RET_osEvent {(osStatus)__r0, {(uint32_t)__r1}, {(void *)__r2}}
  127. #define RET_osCallback {(void *)__r0, (void *)__r1}
  128. #define osEvent_type ret128
  129. #define osEvent_ret_status (ret128){ret.status}
  130. #define osEvent_ret_value (ret128){ret.status, ret.value.v}
  131. #define osEvent_ret_msg (ret128){ret.status, ret.value.v, (uint32_t)ret.def.message_id}
  132. #define osEvent_ret_mail (ret128){ret.status, ret.value.v, (uint32_t)ret.def.mail_id}
  133. #define osCallback_type ret64
  134. #define osCallback_ret (ret64) {(uint32_t)ret.fp, (uint32_t)ret.arg}
  135. #define SVC_ArgN(n) \
  136. register int __r##n __asm("r"#n);
  137. #define SVC_ArgR(n,t,a) \
  138. register t __r##n __asm("r"#n) = a;
  139. #define SVC_Arg0() \
  140. SVC_ArgN(0) \
  141. SVC_ArgN(1) \
  142. SVC_ArgN(2) \
  143. SVC_ArgN(3)
  144. #define SVC_Arg1(t1) \
  145. SVC_ArgR(0,t1,a1) \
  146. SVC_ArgN(1) \
  147. SVC_ArgN(2) \
  148. SVC_ArgN(3)
  149. #define SVC_Arg2(t1,t2) \
  150. SVC_ArgR(0,t1,a1) \
  151. SVC_ArgR(1,t2,a2) \
  152. SVC_ArgN(2) \
  153. SVC_ArgN(3)
  154. #define SVC_Arg3(t1,t2,t3) \
  155. SVC_ArgR(0,t1,a1) \
  156. SVC_ArgR(1,t2,a2) \
  157. SVC_ArgR(2,t3,a3) \
  158. SVC_ArgN(3)
  159. #define SVC_Arg4(t1,t2,t3,t4) \
  160. SVC_ArgR(0,t1,a1) \
  161. SVC_ArgR(1,t2,a2) \
  162. SVC_ArgR(2,t3,a3) \
  163. SVC_ArgR(3,t4,a4)
  164. #if (defined (__CORTEX_M0)) || defined (__CORTEX_M0PLUS)
  165. #define SVC_Call(f) \
  166. __asm volatile \
  167. ( \
  168. "ldr r7,="#f"\n\t" \
  169. "mov r12,r7\n\t" \
  170. "svc 0" \
  171. : "=r" (__r0), "=r" (__r1), "=r" (__r2), "=r" (__r3) \
  172. : "r" (__r0), "r" (__r1), "r" (__r2), "r" (__r3) \
  173. : "r7", "r12", "lr", "cc" \
  174. );
  175. #else
  176. #define SVC_Call(f) \
  177. __asm volatile \
  178. ( \
  179. "ldr r12,="#f"\n\t" \
  180. "svc 0" \
  181. : "=r" (__r0), "=r" (__r1), "=r" (__r2), "=r" (__r3) \
  182. : "r" (__r0), "r" (__r1), "r" (__r2), "r" (__r3) \
  183. : "r12", "lr", "cc" \
  184. );
  185. #endif
  186. #define SVC_0_1(f,t,rv) \
  187. __attribute__((always_inline)) \
  188. static inline t __##f (void) { \
  189. SVC_Arg0(); \
  190. SVC_Call(f); \
  191. return (t) rv; \
  192. }
  193. #define SVC_1_1(f,t,t1,rv) \
  194. __attribute__((always_inline)) \
  195. static inline t __##f (t1 a1) { \
  196. SVC_Arg1(t1); \
  197. SVC_Call(f); \
  198. return (t) rv; \
  199. }
  200. #define SVC_2_1(f,t,t1,t2,rv) \
  201. __attribute__((always_inline)) \
  202. static inline t __##f (t1 a1, t2 a2) { \
  203. SVC_Arg2(t1,t2); \
  204. SVC_Call(f); \
  205. return (t) rv; \
  206. }
  207. #define SVC_3_1(f,t,t1,t2,t3,rv) \
  208. __attribute__((always_inline)) \
  209. static inline t __##f (t1 a1, t2 a2, t3 a3) { \
  210. SVC_Arg3(t1,t2,t3); \
  211. SVC_Call(f); \
  212. return (t) rv; \
  213. }
  214. #define SVC_4_1(f,t,t1,t2,t3,t4,rv) \
  215. __attribute__((always_inline)) \
  216. static inline t __##f (t1 a1, t2 a2, t3 a3, t4 a4) { \
  217. SVC_Arg4(t1,t2,t3,t4); \
  218. SVC_Call(f); \
  219. return (t) rv; \
  220. }
  221. #define SVC_1_2 SVC_1_1
  222. #define SVC_1_3 SVC_1_1
  223. #define SVC_2_3 SVC_2_1
  224. #elif defined (__ICCARM__) /* IAR Compiler */
  225. #define __NO_RETURN __noreturn
  226. #define osEvent_type osEvent
  227. #define osEvent_ret_status ret
  228. #define osEvent_ret_value ret
  229. #define osEvent_ret_msg ret
  230. #define osEvent_ret_mail ret
  231. #define osCallback_type osCallback
  232. #define osCallback_ret ret
  233. #define RET_osEvent osEvent
  234. #define RET_osCallback osCallback
  235. #define SVC_Setup(f) \
  236. __asm( \
  237. "mov r12,%0\n" \
  238. :: "r"(&f): "r12" \
  239. );
  240. #define SVC_0_1(f,t,...) \
  241. t f (void); \
  242. _Pragma("swi_number=0") __swi t _##f (void); \
  243. static inline t __##f (void) { \
  244. SVC_Setup(f); \
  245. return _##f(); \
  246. }
  247. #define SVC_1_1(f,t,t1,...) \
  248. t f (t1 a1); \
  249. _Pragma("swi_number=0") __swi t _##f (t1 a1); \
  250. static inline t __##f (t1 a1) { \
  251. SVC_Setup(f); \
  252. return _##f(a1); \
  253. }
  254. #define SVC_2_1(f,t,t1,t2,...) \
  255. t f (t1 a1, t2 a2); \
  256. _Pragma("swi_number=0") __swi t _##f (t1 a1, t2 a2); \
  257. static inline t __##f (t1 a1, t2 a2) { \
  258. SVC_Setup(f); \
  259. return _##f(a1,a2); \
  260. }
  261. #define SVC_3_1(f,t,t1,t2,t3,...) \
  262. t f (t1 a1, t2 a2, t3 a3); \
  263. _Pragma("swi_number=0") __swi t _##f (t1 a1, t2 a2, t3 a3); \
  264. static inline t __##f (t1 a1, t2 a2, t3 a3) { \
  265. SVC_Setup(f); \
  266. return _##f(a1,a2,a3); \
  267. }
  268. #define SVC_4_1(f,t,t1,t2,t3,t4,...) \
  269. t f (t1 a1, t2 a2, t3 a3, t4 a4); \
  270. _Pragma("swi_number=0") __swi t _##f (t1 a1, t2 a2, t3 a3, t4 a4); \
  271. static inline t __##f (t1 a1, t2 a2, t3 a3, t4 a4) { \
  272. SVC_Setup(f); \
  273. return _##f(a1,a2,a3,a4); \
  274. }
  275. #define SVC_1_2 SVC_1_1
  276. #define SVC_1_3 SVC_1_1
  277. #define SVC_2_3 SVC_2_1
  278. #endif
  279. // Callback structure
  280. typedef struct {
  281. void *fp; // Function pointer
  282. void *arg; // Function argument
  283. } osCallback;
  284. // OS Section definitions
  285. #ifdef OS_SECTIONS_LINK_INFO
  286. extern const uint32_t os_section_id$$Base;
  287. extern const uint32_t os_section_id$$Limit;
  288. #endif
  289. // OS Timers external resources
  290. extern osThreadDef_t os_thread_def_osTimerThread;
  291. extern osThreadId osThreadId_osTimerThread;
  292. extern osMessageQDef_t os_messageQ_def_osTimerMessageQ;
  293. extern osMessageQId osMessageQId_osTimerMessageQ;
  294. // ==== Helper Functions ====
  295. /// Convert timeout in millisec to system ticks
  296. static uint32_t rt_ms2tick (uint32_t millisec) {
  297. uint32_t tick;
  298. if (millisec == osWaitForever) return 0xFFFF; // Indefinite timeout
  299. if (millisec > 4000000) return 0xFFFE; // Max ticks supported
  300. tick = ((1000 * millisec) + os_clockrate - 1) / os_clockrate;
  301. if (tick > 0xFFFE) return 0xFFFE;
  302. return tick;
  303. }
  304. /// Convert Thread ID to TCB pointer
  305. static P_TCB rt_tid2ptcb (osThreadId thread_id) {
  306. P_TCB ptcb;
  307. if (thread_id == NULL) return NULL;
  308. if ((uint32_t)thread_id & 3) return NULL;
  309. #ifdef OS_SECTIONS_LINK_INFO
  310. if ((os_section_id$$Base != 0) && (os_section_id$$Limit != 0)) {
  311. if (thread_id < (osThreadId)os_section_id$$Base) return NULL;
  312. if (thread_id >= (osThreadId)os_section_id$$Limit) return NULL;
  313. }
  314. #endif
  315. ptcb = thread_id;
  316. if (ptcb->cb_type != TCB) return NULL;
  317. return ptcb;
  318. }
  319. /// Convert ID pointer to Object pointer
  320. static void *rt_id2obj (void *id) {
  321. if ((uint32_t)id & 3) return NULL;
  322. #ifdef OS_SECTIONS_LINK_INFO
  323. if ((os_section_id$$Base != 0) && (os_section_id$$Limit != 0)) {
  324. if (id < (void *)os_section_id$$Base) return NULL;
  325. if (id >= (void *)os_section_id$$Limit) return NULL;
  326. }
  327. #endif
  328. return id;
  329. }
  330. // ==== Kernel Control ====
  331. uint8_t os_initialized; // Kernel Initialized flag
  332. uint8_t os_running; // Kernel Running flag
  333. // Kernel Control Service Calls declarations
  334. SVC_0_1(svcKernelInitialize, osStatus, RET_osStatus)
  335. SVC_0_1(svcKernelStart, osStatus, RET_osStatus)
  336. SVC_0_1(svcKernelRunning, int32_t, RET_int32_t)
  337. extern void sysThreadError (osStatus status);
  338. osThreadId svcThreadCreate (osThreadDef_t *thread_def, void *argument);
  339. osMessageQId svcMessageCreate (osMessageQDef_t *queue_def, osThreadId thread_id);
  340. // Kernel Control Service Calls
  341. /// Initialize the RTOS Kernel for creating objects
  342. osStatus svcKernelInitialize (void) {
  343. if (os_initialized) return osOK;
  344. rt_sys_init(); // RTX System Initialization
  345. os_tsk.run->prio = 255; // Highest priority
  346. sysThreadError(osOK);
  347. os_initialized = 1;
  348. return osOK;
  349. }
  350. /// Start the RTOS Kernel
  351. osStatus svcKernelStart (void) {
  352. if (os_running) return osOK;
  353. // Create OS Timers resources (Message Queue & Thread)
  354. osMessageQId_osTimerMessageQ = svcMessageCreate (&os_messageQ_def_osTimerMessageQ, NULL);
  355. osThreadId_osTimerThread = svcThreadCreate(&os_thread_def_osTimerThread, NULL);
  356. rt_tsk_prio(0, 0); // Lowest priority
  357. __set_PSP(os_tsk.run->tsk_stack + 8*4); // New context
  358. os_tsk.run = NULL; // Force context switch
  359. rt_sys_start();
  360. os_running = 1;
  361. return osOK;
  362. }
  363. /// Check if the RTOS kernel is already started
  364. int32_t svcKernelRunning(void) {
  365. return os_running;
  366. }
  367. // Kernel Control Public API
  368. /// Initialize the RTOS Kernel for creating objects
  369. osStatus osKernelInitialize (void) {
  370. if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
  371. if ((__get_CONTROL() & 1) == 0) { // Privileged mode
  372. return svcKernelInitialize();
  373. } else {
  374. return __svcKernelInitialize();
  375. }
  376. }
  377. /// Start the RTOS Kernel
  378. osStatus osKernelStart (void) {
  379. uint32_t stack[8];
  380. if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
  381. switch (__get_CONTROL() & 0x03) {
  382. case 0x00: // Privileged Thread mode & MSP
  383. __set_PSP((uint32_t)(stack + 8)); // Initial PSP
  384. if (os_flags & 1) {
  385. __set_CONTROL(0x02); // Set Privileged Thread mode & PSP
  386. } else {
  387. __set_CONTROL(0x03); // Set Unprivileged Thread mode & PSP
  388. }
  389. __DSB();
  390. __ISB();
  391. break;
  392. case 0x01: // Unprivileged Thread mode & MSP
  393. return osErrorOS;
  394. case 0x02: // Privileged Thread mode & PSP
  395. if ((os_flags & 1) == 0) { // Unprivileged Thread mode requested
  396. __set_CONTROL(0x03); // Set Unprivileged Thread mode & PSP
  397. __DSB();
  398. __ISB();
  399. }
  400. break;
  401. case 0x03: // Unprivileged Thread mode & PSP
  402. if (os_flags & 1) return osErrorOS; // Privileged Thread mode requested
  403. break;
  404. }
  405. return __svcKernelStart();
  406. }
  407. /// Check if the RTOS kernel is already started
  408. int32_t osKernelRunning(void) {
  409. if ((__get_IPSR() != 0) || ((__get_CONTROL() & 1) == 0)) {
  410. // in ISR or Privileged
  411. return os_running;
  412. } else {
  413. return __svcKernelRunning();
  414. }
  415. }
  416. // ==== Thread Management ====
  417. __NO_RETURN void osThreadExit (void);
  418. // Thread Service Calls declarations
  419. SVC_2_1(svcThreadCreate, osThreadId, osThreadDef_t *, void *, RET_pointer)
  420. SVC_0_1(svcThreadGetId, osThreadId, RET_pointer)
  421. SVC_1_1(svcThreadTerminate, osStatus, osThreadId, RET_osStatus)
  422. SVC_0_1(svcThreadYield, osStatus, RET_osStatus)
  423. SVC_2_1(svcThreadSetPriority, osStatus, osThreadId, osPriority, RET_osStatus)
  424. SVC_1_1(svcThreadGetPriority, osPriority, osThreadId, RET_osPriority)
  425. // Thread Service Calls
  426. extern OS_TID rt_get_TID (void);
  427. extern void rt_init_context (P_TCB p_TCB, U8 priority, FUNCP task_body);
  428. /// Create a thread and add it to Active Threads and set it to state READY
  429. osThreadId svcThreadCreate (osThreadDef_t *thread_def, void *argument) {
  430. P_TCB ptcb;
  431. if ((thread_def == NULL) ||
  432. (thread_def->pthread == NULL) ||
  433. (thread_def->tpriority < osPriorityIdle) ||
  434. (thread_def->tpriority > osPriorityRealtime) ||
  435. (thread_def->stacksize == 0) ||
  436. (thread_def->stack_pointer == NULL) ) {
  437. sysThreadError(osErrorParameter);
  438. return NULL;
  439. }
  440. U8 priority = thread_def->tpriority - osPriorityIdle + 1;
  441. P_TCB task_context = &thread_def->tcb;
  442. /* Utilize the user provided stack. */
  443. task_context->stack = (U32*)thread_def->stack_pointer;
  444. task_context->priv_stack = thread_def->stacksize;
  445. /* Find a free entry in 'os_active_TCB' table. */
  446. OS_TID tsk = rt_get_TID ();
  447. os_active_TCB[tsk-1] = task_context;
  448. task_context->task_id = tsk;
  449. /* Pass parameter 'argv' to 'rt_init_context' */
  450. task_context->msg = argument;
  451. /* Initialize thread context structure, including the thread's stack. */
  452. rt_init_context (task_context, priority, (FUNCP)thread_def->pthread);
  453. /* Dispatch this task to the scheduler for execution. */
  454. DBG_TASK_NOTIFY(task_context, __TRUE);
  455. rt_dispatch (task_context);
  456. ptcb = (P_TCB)os_active_TCB[tsk - 1]; // TCB pointer
  457. *((uint32_t *)ptcb->tsk_stack + 13) = (uint32_t)osThreadExit;
  458. return ptcb;
  459. }
  460. /// Return the thread ID of the current running thread
  461. osThreadId svcThreadGetId (void) {
  462. OS_TID tsk;
  463. tsk = rt_tsk_self();
  464. if (tsk == 0) return NULL;
  465. return (P_TCB)os_active_TCB[tsk - 1];
  466. }
  467. /// Terminate execution of a thread and remove it from ActiveThreads
  468. osStatus svcThreadTerminate (osThreadId thread_id) {
  469. OS_RESULT res;
  470. P_TCB ptcb;
  471. ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
  472. if (ptcb == NULL) return osErrorParameter;
  473. res = rt_tsk_delete(ptcb->task_id); // Delete task
  474. if (res == OS_R_NOK) return osErrorResource; // Delete task failed
  475. return osOK;
  476. }
  477. /// Pass control to next thread that is in state READY
  478. osStatus svcThreadYield (void) {
  479. rt_tsk_pass(); // Pass control to next task
  480. return osOK;
  481. }
  482. /// Change priority of an active thread
  483. osStatus svcThreadSetPriority (osThreadId thread_id, osPriority priority) {
  484. OS_RESULT res;
  485. P_TCB ptcb;
  486. ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
  487. if (ptcb == NULL) return osErrorParameter;
  488. if ((priority < osPriorityIdle) || (priority > osPriorityRealtime)) {
  489. return osErrorValue;
  490. }
  491. res = rt_tsk_prio( // Change task priority
  492. ptcb->task_id, // Task ID
  493. priority - osPriorityIdle + 1 // New task priority
  494. );
  495. if (res == OS_R_NOK) return osErrorResource; // Change task priority failed
  496. return osOK;
  497. }
  498. /// Get current priority of an active thread
  499. osPriority svcThreadGetPriority (osThreadId thread_id) {
  500. P_TCB ptcb;
  501. ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
  502. if (ptcb == NULL) return osPriorityError;
  503. return (osPriority)(ptcb->prio - 1 + osPriorityIdle);
  504. }
  505. // Thread Public API
  506. /// Create a thread and add it to Active Threads and set it to state READY
  507. osThreadId osThreadCreate (osThreadDef_t *thread_def, void *argument) {
  508. if (__get_IPSR() != 0) return NULL; // Not allowed in ISR
  509. if (((__get_CONTROL() & 1) == 0) && (os_running == 0)) {
  510. // Privileged and not running
  511. return svcThreadCreate(thread_def, argument);
  512. } else {
  513. return __svcThreadCreate(thread_def, argument);
  514. }
  515. }
  516. /// Return the thread ID of the current running thread
  517. osThreadId osThreadGetId (void) {
  518. if (__get_IPSR() != 0) return NULL; // Not allowed in ISR
  519. return __svcThreadGetId();
  520. }
  521. /// Terminate execution of a thread and remove it from ActiveThreads
  522. osStatus osThreadTerminate (osThreadId thread_id) {
  523. if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
  524. return __svcThreadTerminate(thread_id);
  525. }
  526. /// Pass control to next thread that is in state READY
  527. osStatus osThreadYield (void) {
  528. if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
  529. return __svcThreadYield();
  530. }
  531. /// Change priority of an active thread
  532. osStatus osThreadSetPriority (osThreadId thread_id, osPriority priority) {
  533. if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
  534. return __svcThreadSetPriority(thread_id, priority);
  535. }
  536. /// Get current priority of an active thread
  537. osPriority osThreadGetPriority (osThreadId thread_id) {
  538. if (__get_IPSR() != 0) return osPriorityError;// Not allowed in ISR
  539. return __svcThreadGetPriority(thread_id);
  540. }
  541. /// INTERNAL - Not Public
  542. /// Auto Terminate Thread on exit (used implicitly when thread exists)
  543. __NO_RETURN void osThreadExit (void) {
  544. __svcThreadTerminate(__svcThreadGetId());
  545. for (;;); // Should never come here
  546. }
  547. // ==== Generic Wait Functions ====
  548. // Generic Wait Service Calls declarations
  549. SVC_1_1(svcDelay, osStatus, uint32_t, RET_osStatus)
  550. #if osFeature_Wait != 0
  551. SVC_1_3(svcWait, os_InRegs osEvent, uint32_t, RET_osEvent)
  552. #endif
  553. // Generic Wait Service Calls
  554. /// Wait for Timeout (Time Delay)
  555. osStatus svcDelay (uint32_t millisec) {
  556. if (millisec == 0) return osOK;
  557. rt_dly_wait(rt_ms2tick(millisec));
  558. return osEventTimeout;
  559. }
  560. /// Wait for Signal, Message, Mail, or Timeout
  561. #if osFeature_Wait != 0
  562. os_InRegs osEvent_type svcWait (uint32_t millisec) {
  563. osEvent ret;
  564. if (millisec == 0) {
  565. ret.status = osOK;
  566. return osEvent_ret_status;
  567. }
  568. /* To Do: osEventSignal, osEventMessage, osEventMail */
  569. rt_dly_wait(rt_ms2tick(millisec));
  570. ret.status = osEventTimeout;
  571. return osEvent_ret_status;
  572. }
  573. #endif
  574. // Generic Wait API
  575. /// Wait for Timeout (Time Delay)
  576. osStatus osDelay (uint32_t millisec) {
  577. if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
  578. return __svcDelay(millisec);
  579. }
  580. /// Wait for Signal, Message, Mail, or Timeout
  581. os_InRegs osEvent osWait (uint32_t millisec) {
  582. osEvent ret;
  583. #if osFeature_Wait == 0
  584. ret.status = osErrorOS;
  585. return ret;
  586. #else
  587. if (__get_IPSR() != 0) { // Not allowed in ISR
  588. ret.status = osErrorISR;
  589. return ret;
  590. }
  591. return __svcWait(millisec);
  592. #endif
  593. }
  594. // ==== Timer Management ====
  595. // Timer definitions
  596. #define osTimerInvalid 0
  597. #define osTimerStopped 1
  598. #define osTimerRunning 2
  599. // Timer structures
  600. typedef struct os_timer_cb_ { // Timer Control Block
  601. struct os_timer_cb_ *next; // Pointer to next active Timer
  602. uint8_t state; // Timer State
  603. uint8_t type; // Timer Type (Periodic/One-shot)
  604. uint16_t reserved; // Reserved
  605. uint16_t tcnt; // Timer Delay Count
  606. uint16_t icnt; // Timer Initial Count
  607. void *arg; // Timer Function Argument
  608. osTimerDef_t *timer; // Pointer to Timer definition
  609. } os_timer_cb;
  610. // Timer variables
  611. os_timer_cb *os_timer_head; // Pointer to first active Timer
  612. // Timer Helper Functions
  613. // Insert Timer into the list sorted by time
  614. static void rt_timer_insert (os_timer_cb *pt, uint32_t tcnt) {
  615. os_timer_cb *p, *prev;
  616. prev = NULL;
  617. p = os_timer_head;
  618. while (p != NULL) {
  619. if (tcnt < p->tcnt) break;
  620. tcnt -= p->tcnt;
  621. prev = p;
  622. p = p->next;
  623. }
  624. pt->next = p;
  625. pt->tcnt = (uint16_t)tcnt;
  626. if (p != NULL) {
  627. p->tcnt -= pt->tcnt;
  628. }
  629. if (prev != NULL) {
  630. prev->next = pt;
  631. } else {
  632. os_timer_head = pt;
  633. }
  634. }
  635. // Remove Timer from the list
  636. static int rt_timer_remove (os_timer_cb *pt) {
  637. os_timer_cb *p, *prev;
  638. prev = NULL;
  639. p = os_timer_head;
  640. while (p != NULL) {
  641. if (p == pt) break;
  642. prev = p;
  643. p = p->next;
  644. }
  645. if (p == NULL) return -1;
  646. if (prev != NULL) {
  647. prev->next = pt->next;
  648. } else {
  649. os_timer_head = pt->next;
  650. }
  651. if (pt->next != NULL) {
  652. pt->next->tcnt += pt->tcnt;
  653. }
  654. return 0;
  655. }
  656. // Timer Service Calls declarations
  657. SVC_3_1(svcTimerCreate, osTimerId, osTimerDef_t *, os_timer_type, void *, RET_pointer)
  658. SVC_2_1(svcTimerStart, osStatus, osTimerId, uint32_t, RET_osStatus)
  659. SVC_1_1(svcTimerStop, osStatus, osTimerId, RET_osStatus)
  660. SVC_1_1(svcTimerDelete, osStatus, osTimerId, RET_osStatus)
  661. SVC_1_2(svcTimerCall, os_InRegs osCallback, osTimerId, RET_osCallback)
  662. // Timer Management Service Calls
  663. /// Create timer
  664. osTimerId svcTimerCreate (osTimerDef_t *timer_def, os_timer_type type, void *argument) {
  665. os_timer_cb *pt;
  666. if ((timer_def == NULL) || (timer_def->ptimer == NULL)) {
  667. sysThreadError(osErrorParameter);
  668. return NULL;
  669. }
  670. pt = timer_def->timer;
  671. if (pt == NULL) {
  672. sysThreadError(osErrorParameter);
  673. return NULL;
  674. }
  675. if ((type != osTimerOnce) && (type != osTimerPeriodic)) {
  676. sysThreadError(osErrorValue);
  677. return NULL;
  678. }
  679. if (osThreadId_osTimerThread == NULL) {
  680. sysThreadError(osErrorResource);
  681. return NULL;
  682. }
  683. if (pt->state != osTimerInvalid){
  684. sysThreadError(osErrorResource);
  685. return NULL;
  686. }
  687. pt->state = osTimerStopped;
  688. pt->type = (uint8_t)type;
  689. pt->arg = argument;
  690. pt->timer = timer_def;
  691. return (osTimerId)pt;
  692. }
  693. /// Start or restart timer
  694. osStatus svcTimerStart (osTimerId timer_id, uint32_t millisec) {
  695. os_timer_cb *pt;
  696. uint32_t tcnt;
  697. pt = rt_id2obj(timer_id);
  698. if (pt == NULL) return osErrorParameter;
  699. tcnt = rt_ms2tick(millisec);
  700. if (tcnt == 0) return osErrorValue;
  701. switch (pt->state) {
  702. case osTimerRunning:
  703. if (rt_timer_remove(pt) != 0) {
  704. return osErrorResource;
  705. }
  706. break;
  707. case osTimerStopped:
  708. pt->state = osTimerRunning;
  709. pt->icnt = (uint16_t)tcnt;
  710. break;
  711. default:
  712. return osErrorResource;
  713. }
  714. rt_timer_insert(pt, tcnt);
  715. return osOK;
  716. }
  717. /// Stop timer
  718. osStatus svcTimerStop (osTimerId timer_id) {
  719. os_timer_cb *pt;
  720. pt = rt_id2obj(timer_id);
  721. if (pt == NULL) return osErrorParameter;
  722. if (pt->state != osTimerRunning) return osErrorResource;
  723. pt->state = osTimerStopped;
  724. if (rt_timer_remove(pt) != 0) {
  725. return osErrorResource;
  726. }
  727. return osOK;
  728. }
  729. /// Delete timer
  730. osStatus svcTimerDelete (osTimerId timer_id) {
  731. os_timer_cb *pt;
  732. pt = rt_id2obj(timer_id);
  733. if (pt == NULL) return osErrorParameter;
  734. switch (pt->state) {
  735. case osTimerRunning:
  736. rt_timer_remove(pt);
  737. break;
  738. case osTimerStopped:
  739. break;
  740. default:
  741. return osErrorResource;
  742. }
  743. pt->state = osTimerInvalid;
  744. return osOK;
  745. }
  746. /// Get timer callback parameters
  747. os_InRegs osCallback_type svcTimerCall (osTimerId timer_id) {
  748. os_timer_cb *pt;
  749. osCallback ret;
  750. pt = rt_id2obj(timer_id);
  751. if (pt == NULL) {
  752. ret.fp = NULL;
  753. ret.arg = NULL;
  754. return osCallback_ret;
  755. }
  756. ret.fp = (void *)pt->timer->ptimer;
  757. ret.arg = pt->arg;
  758. return osCallback_ret;
  759. }
  760. static __INLINE osStatus isrMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec);
  761. /// Timer Tick (called each SysTick)
  762. void sysTimerTick (void) {
  763. os_timer_cb *pt, *p;
  764. p = os_timer_head;
  765. if (p == NULL) return;
  766. p->tcnt--;
  767. while ((p != NULL) && (p->tcnt == 0)) {
  768. pt = p;
  769. p = p->next;
  770. os_timer_head = p;
  771. isrMessagePut(osMessageQId_osTimerMessageQ, (uint32_t)pt, 0);
  772. if (pt->type == osTimerPeriodic) {
  773. rt_timer_insert(pt, pt->icnt);
  774. } else {
  775. pt->state = osTimerStopped;
  776. }
  777. }
  778. }
  779. // Timer Management Public API
  780. /// Create timer
  781. osTimerId osTimerCreate (osTimerDef_t *timer_def, os_timer_type type, void *argument) {
  782. if (__get_IPSR() != 0) return NULL; // Not allowed in ISR
  783. if (((__get_CONTROL() & 1) == 0) && (os_running == 0)) {
  784. // Privileged and not running
  785. return svcTimerCreate(timer_def, type, argument);
  786. } else {
  787. return __svcTimerCreate(timer_def, type, argument);
  788. }
  789. }
  790. /// Start or restart timer
  791. osStatus osTimerStart (osTimerId timer_id, uint32_t millisec) {
  792. if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
  793. return __svcTimerStart(timer_id, millisec);
  794. }
  795. /// Stop timer
  796. osStatus osTimerStop (osTimerId timer_id) {
  797. if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
  798. return __svcTimerStop(timer_id);
  799. }
  800. /// Delete timer
  801. osStatus osTimerDelete (osTimerId timer_id) {
  802. if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
  803. return __svcTimerDelete(timer_id);
  804. }
  805. /// INTERNAL - Not Public
  806. /// Get timer callback parameters (used by OS Timer Thread)
  807. os_InRegs osCallback osTimerCall (osTimerId timer_id) {
  808. return __svcTimerCall(timer_id);
  809. }
  810. // Timer Thread
  811. __NO_RETURN void osTimerThread (void const *argument) {
  812. osCallback cb;
  813. osEvent evt;
  814. for (;;) {
  815. evt = osMessageGet(osMessageQId_osTimerMessageQ, osWaitForever);
  816. if (evt.status == osEventMessage) {
  817. cb = osTimerCall(evt.value.p);
  818. if (cb.fp != NULL) {
  819. (*(os_ptimer)cb.fp)(cb.arg);
  820. }
  821. }
  822. }
  823. }
  824. // ==== Signal Management ====
  825. // Signal Service Calls declarations
  826. SVC_2_1(svcSignalSet, int32_t, osThreadId, int32_t, RET_int32_t)
  827. SVC_2_1(svcSignalClear, int32_t, osThreadId, int32_t, RET_int32_t)
  828. SVC_1_1(svcSignalGet, int32_t, osThreadId, RET_int32_t)
  829. SVC_2_3(svcSignalWait, os_InRegs osEvent, int32_t, uint32_t, RET_osEvent)
  830. // Signal Service Calls
  831. /// Set the specified Signal Flags of an active thread
  832. int32_t svcSignalSet (osThreadId thread_id, int32_t signals) {
  833. P_TCB ptcb;
  834. int32_t sig;
  835. ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
  836. if (ptcb == NULL) return 0x80000000;
  837. if (signals & (0xFFFFFFFF << osFeature_Signals)) return 0x80000000;
  838. sig = ptcb->events; // Previous signal flags
  839. rt_evt_set(signals, ptcb->task_id); // Set event flags
  840. return sig;
  841. }
  842. /// Clear the specified Signal Flags of an active thread
  843. int32_t svcSignalClear (osThreadId thread_id, int32_t signals) {
  844. P_TCB ptcb;
  845. int32_t sig;
  846. ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
  847. if (ptcb == NULL) return 0x80000000;
  848. if (signals & (0xFFFFFFFF << osFeature_Signals)) return 0x80000000;
  849. sig = ptcb->events; // Previous signal flags
  850. rt_evt_clr(signals, ptcb->task_id); // Clear event flags
  851. return sig;
  852. }
  853. /// Get Signal Flags status of an active thread
  854. int32_t svcSignalGet (osThreadId thread_id) {
  855. P_TCB ptcb;
  856. ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
  857. if (ptcb == NULL) return 0x80000000;
  858. return ptcb->events; // Return event flags
  859. }
  860. /// Wait for one or more Signal Flags to become signaled for the current RUNNING thread
  861. os_InRegs osEvent_type svcSignalWait (int32_t signals, uint32_t millisec) {
  862. OS_RESULT res;
  863. osEvent ret;
  864. if (signals & (0xFFFFFFFF << osFeature_Signals)) {
  865. ret.status = osErrorValue;
  866. return osEvent_ret_status;
  867. }
  868. if (signals != 0) { // Wait for all specified signals
  869. res = rt_evt_wait(signals, rt_ms2tick(millisec), __TRUE);
  870. } else { // Wait for any signal
  871. res = rt_evt_wait(0xFFFF, rt_ms2tick(millisec), __FALSE);
  872. }
  873. if (res == OS_R_EVT) {
  874. ret.status = osEventSignal;
  875. ret.value.signals = signals ? signals : os_tsk.run->waits;
  876. } else {
  877. ret.status = millisec ? osEventTimeout : osOK;
  878. ret.value.signals = 0;
  879. }
  880. return osEvent_ret_value;
  881. }
  882. // Signal ISR Calls
  883. /// Set the specified Signal Flags of an active thread
  884. static __INLINE int32_t isrSignalSet (osThreadId thread_id, int32_t signals) {
  885. P_TCB ptcb;
  886. int32_t sig;
  887. ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer
  888. if (ptcb == NULL) return 0x80000000;
  889. if (signals & (0xFFFFFFFF << osFeature_Signals)) return 0x80000000;
  890. sig = ptcb->events; // Previous signal flags
  891. isr_evt_set(signals, ptcb->task_id); // Set event flags
  892. return sig;
  893. }
  894. // Signal Public API
  895. /// Set the specified Signal Flags of an active thread
  896. int32_t osSignalSet (osThreadId thread_id, int32_t signals) {
  897. if (__get_IPSR() != 0) { // in ISR
  898. return isrSignalSet(thread_id, signals);
  899. } else { // in Thread
  900. return __svcSignalSet(thread_id, signals);
  901. }
  902. }
  903. /// Clear the specified Signal Flags of an active thread
  904. int32_t osSignalClear (osThreadId thread_id, int32_t signals) {
  905. if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
  906. return __svcSignalClear(thread_id, signals);
  907. }
  908. /// Get Signal Flags status of an active thread
  909. int32_t osSignalGet (osThreadId thread_id) {
  910. if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
  911. return __svcSignalGet(thread_id);
  912. }
  913. /// Wait for one or more Signal Flags to become signaled for the current RUNNING thread
  914. os_InRegs osEvent osSignalWait (int32_t signals, uint32_t millisec) {
  915. osEvent ret;
  916. if (__get_IPSR() != 0) { // Not allowed in ISR
  917. ret.status = osErrorISR;
  918. return ret;
  919. }
  920. return __svcSignalWait(signals, millisec);
  921. }
  922. // ==== Mutex Management ====
  923. // Mutex Service Calls declarations
  924. SVC_1_1(svcMutexCreate, osMutexId, osMutexDef_t *, RET_pointer)
  925. SVC_2_1(svcMutexWait, osStatus, osMutexId, uint32_t, RET_osStatus)
  926. SVC_1_1(svcMutexRelease, osStatus, osMutexId, RET_osStatus)
  927. SVC_1_1(svcMutexDelete, osStatus, osMutexId, RET_osStatus)
  928. // Mutex Service Calls
  929. /// Create and Initialize a Mutex object
  930. osMutexId svcMutexCreate (osMutexDef_t *mutex_def) {
  931. OS_ID mut;
  932. if (mutex_def == NULL) {
  933. sysThreadError(osErrorParameter);
  934. return NULL;
  935. }
  936. mut = mutex_def->mutex;
  937. if (mut == NULL) {
  938. sysThreadError(osErrorParameter);
  939. return NULL;
  940. }
  941. if (((P_MUCB)mut)->cb_type != 0) {
  942. sysThreadError(osErrorParameter);
  943. return NULL;
  944. }
  945. rt_mut_init(mut); // Initialize Mutex
  946. return mut;
  947. }
  948. /// Wait until a Mutex becomes available
  949. osStatus svcMutexWait (osMutexId mutex_id, uint32_t millisec) {
  950. OS_ID mut;
  951. OS_RESULT res;
  952. mut = rt_id2obj(mutex_id);
  953. if (mut == NULL) return osErrorParameter;
  954. if (((P_MUCB)mut)->cb_type != MUCB) return osErrorParameter;
  955. res = rt_mut_wait(mut, rt_ms2tick(millisec)); // Wait for Mutex
  956. if (res == OS_R_TMO) {
  957. return (millisec ? osErrorTimeoutResource : osErrorResource);
  958. }
  959. return osOK;
  960. }
  961. /// Release a Mutex that was obtained with osMutexWait
  962. osStatus svcMutexRelease (osMutexId mutex_id) {
  963. OS_ID mut;
  964. OS_RESULT res;
  965. mut = rt_id2obj(mutex_id);
  966. if (mut == NULL) return osErrorParameter;
  967. if (((P_MUCB)mut)->cb_type != MUCB) return osErrorParameter;
  968. res = rt_mut_release(mut); // Release Mutex
  969. if (res == OS_R_NOK) return osErrorResource; // Thread not owner or Zero Counter
  970. return osOK;
  971. }
  972. /// Delete a Mutex that was created by osMutexCreate
  973. osStatus svcMutexDelete (osMutexId mutex_id) {
  974. OS_ID mut;
  975. mut = rt_id2obj(mutex_id);
  976. if (mut == NULL) return osErrorParameter;
  977. if (((P_MUCB)mut)->cb_type != MUCB) return osErrorParameter;
  978. rt_mut_delete(mut); // Release Mutex
  979. return osOK;
  980. }
  981. // Mutex Public API
  982. /// Create and Initialize a Mutex object
  983. osMutexId osMutexCreate (osMutexDef_t *mutex_def) {
  984. if (__get_IPSR() != 0) return NULL; // Not allowed in ISR
  985. if (((__get_CONTROL() & 1) == 0) && (os_running == 0)) {
  986. // Privileged and not running
  987. return svcMutexCreate(mutex_def);
  988. } else {
  989. return __svcMutexCreate(mutex_def);
  990. }
  991. }
  992. /// Wait until a Mutex becomes available
  993. osStatus osMutexWait (osMutexId mutex_id, uint32_t millisec) {
  994. if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
  995. return __svcMutexWait(mutex_id, millisec);
  996. }
  997. /// Release a Mutex that was obtained with osMutexWait
  998. osStatus osMutexRelease (osMutexId mutex_id) {
  999. if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
  1000. return __svcMutexRelease(mutex_id);
  1001. }
  1002. /// Delete a Mutex that was created by osMutexCreate
  1003. osStatus osMutexDelete (osMutexId mutex_id) {
  1004. if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
  1005. return __svcMutexDelete(mutex_id);
  1006. }
  1007. // ==== Semaphore Management ====
  1008. // Semaphore Service Calls declarations
  1009. SVC_2_1(svcSemaphoreCreate, osSemaphoreId, const osSemaphoreDef_t *, int32_t, RET_pointer)
  1010. SVC_2_1(svcSemaphoreWait, int32_t, osSemaphoreId, uint32_t, RET_int32_t)
  1011. SVC_1_1(svcSemaphoreRelease, osStatus, osSemaphoreId, RET_osStatus)
  1012. SVC_1_1(svcSemaphoreDelete, osStatus, osSemaphoreId, RET_osStatus)
  1013. // Semaphore Service Calls
  1014. /// Create and Initialize a Semaphore object
  1015. osSemaphoreId svcSemaphoreCreate (const osSemaphoreDef_t *semaphore_def, int32_t count) {
  1016. OS_ID sem;
  1017. if (semaphore_def == NULL) {
  1018. sysThreadError(osErrorParameter);
  1019. return NULL;
  1020. }
  1021. sem = semaphore_def->semaphore;
  1022. if (sem == NULL) {
  1023. sysThreadError(osErrorParameter);
  1024. return NULL;
  1025. }
  1026. if (((P_SCB)sem)->cb_type != 0) {
  1027. sysThreadError(osErrorParameter);
  1028. return NULL;
  1029. }
  1030. if (count > osFeature_Semaphore) {
  1031. sysThreadError(osErrorValue);
  1032. return NULL;
  1033. }
  1034. rt_sem_init(sem, count); // Initialize Semaphore
  1035. return sem;
  1036. }
  1037. /// Wait until a Semaphore becomes available
  1038. int32_t svcSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec) {
  1039. OS_ID sem;
  1040. OS_RESULT res;
  1041. sem = rt_id2obj(semaphore_id);
  1042. if (sem == NULL) return -1;
  1043. if (((P_SCB)sem)->cb_type != SCB) return -1;
  1044. res = rt_sem_wait(sem, rt_ms2tick(millisec)); // Wait for Semaphore
  1045. if (res == OS_R_TMO) return 0; // Timeout
  1046. return (((P_SCB)sem)->tokens + 1);
  1047. }
  1048. /// Release a Semaphore
  1049. osStatus svcSemaphoreRelease (osSemaphoreId semaphore_id) {
  1050. OS_ID sem;
  1051. sem = rt_id2obj(semaphore_id);
  1052. if (sem == NULL) return osErrorParameter;
  1053. if (((P_SCB)sem)->cb_type != SCB) return osErrorParameter;
  1054. if (((P_SCB)sem)->tokens == osFeature_Semaphore) return osErrorResource;
  1055. rt_sem_send(sem); // Release Semaphore
  1056. return osOK;
  1057. }
  1058. /// Delete a Semaphore that was created by osSemaphoreCreate
  1059. osStatus svcSemaphoreDelete (osSemaphoreId semaphore_id) {
  1060. OS_ID sem;
  1061. sem = rt_id2obj(semaphore_id);
  1062. if (sem == NULL) return osErrorParameter;
  1063. if (((P_SCB)sem)->cb_type != SCB) return osErrorParameter;
  1064. rt_sem_delete(sem); // Delete Semaphore
  1065. return osOK;
  1066. }
  1067. // Semaphore ISR Calls
  1068. /// Release a Semaphore
  1069. static __INLINE osStatus isrSemaphoreRelease (osSemaphoreId semaphore_id) {
  1070. OS_ID sem;
  1071. sem = rt_id2obj(semaphore_id);
  1072. if (sem == NULL) return osErrorParameter;
  1073. if (((P_SCB)sem)->cb_type != SCB) return osErrorParameter;
  1074. if (((P_SCB)sem)->tokens == osFeature_Semaphore) return osErrorResource;
  1075. isr_sem_send(sem); // Release Semaphore
  1076. return osOK;
  1077. }
  1078. // Semaphore Public API
  1079. /// Create and Initialize a Semaphore object
  1080. osSemaphoreId osSemaphoreCreate (osSemaphoreDef_t *semaphore_def, int32_t count) {
  1081. if (__get_IPSR() != 0) return NULL; // Not allowed in ISR
  1082. if (((__get_CONTROL() & 1) == 0) && (os_running == 0)) {
  1083. // Privileged and not running
  1084. return svcSemaphoreCreate(semaphore_def, count);
  1085. } else {
  1086. return __svcSemaphoreCreate(semaphore_def, count);
  1087. }
  1088. }
  1089. /// Wait until a Semaphore becomes available
  1090. int32_t osSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec) {
  1091. if (__get_IPSR() != 0) return -1; // Not allowed in ISR
  1092. return __svcSemaphoreWait(semaphore_id, millisec);
  1093. }
  1094. /// Release a Semaphore
  1095. osStatus osSemaphoreRelease (osSemaphoreId semaphore_id) {
  1096. if (__get_IPSR() != 0) { // in ISR
  1097. return isrSemaphoreRelease(semaphore_id);
  1098. } else { // in Thread
  1099. return __svcSemaphoreRelease(semaphore_id);
  1100. }
  1101. }
  1102. /// Delete a Semaphore that was created by osSemaphoreCreate
  1103. osStatus osSemaphoreDelete (osSemaphoreId semaphore_id) {
  1104. if (__get_IPSR() != 0) return osErrorISR; // Not allowed in ISR
  1105. return __svcSemaphoreDelete(semaphore_id);
  1106. }
  1107. // ==== Memory Management Functions ====
  1108. // Memory Management Helper Functions
  1109. // Clear Memory Box (Zero init)
  1110. static void rt_clr_box (void *box_mem, void *box) {
  1111. uint32_t *p, n;
  1112. if (box) {
  1113. p = box;
  1114. for (n = ((P_BM)box_mem)->blk_size; n; n -= 4) {
  1115. *p++ = 0;
  1116. }
  1117. }
  1118. }
  1119. // Memory Management Service Calls declarations
  1120. SVC_1_1(svcPoolCreate, osPoolId, const osPoolDef_t *, RET_pointer)
  1121. SVC_2_1(sysPoolAlloc, void *, osPoolId, uint32_t, RET_pointer)
  1122. SVC_2_1(sysPoolFree, osStatus, osPoolId, void *, RET_osStatus)
  1123. // Memory Management Service & ISR Calls
  1124. /// Create and Initialize memory pool
  1125. osPoolId svcPoolCreate (const osPoolDef_t *pool_def) {
  1126. uint32_t blk_sz;
  1127. if ((pool_def == NULL) ||
  1128. (pool_def->pool_sz == 0) ||
  1129. (pool_def->item_sz == 0) ||
  1130. (pool_def->pool == NULL)) {
  1131. sysThreadError(osErrorParameter);
  1132. return NULL;
  1133. }
  1134. blk_sz = (pool_def->item_sz + 3) & ~3;
  1135. _init_box(pool_def->pool, sizeof(struct OS_BM) + pool_def->pool_sz * blk_sz, blk_sz);
  1136. return pool_def->pool;
  1137. }
  1138. /// Allocate a memory block from a memory pool
  1139. void *sysPoolAlloc (osPoolId pool_id, uint32_t clr) {
  1140. void *ptr;
  1141. if (pool_id == NULL) return NULL;
  1142. ptr = rt_alloc_box(pool_id);
  1143. if (clr) {
  1144. rt_clr_box(pool_id, ptr);
  1145. }
  1146. return ptr;
  1147. }
  1148. /// Return an allocated memory block back to a specific memory pool
  1149. osStatus sysPoolFree (osPoolId pool_id, void *block) {
  1150. int32_t res;
  1151. if (pool_id == NULL) return osErrorParameter;
  1152. res = rt_free_box(pool_id, block);
  1153. if (res != 0) return osErrorValue;
  1154. return osOK;
  1155. }
  1156. // Memory Management Public API
  1157. /// Create and Initialize memory pool
  1158. osPoolId osPoolCreate (osPoolDef_t *pool_def) {
  1159. if (__get_IPSR() != 0) return NULL; // Not allowed in ISR
  1160. if (((__get_CONTROL() & 1) == 0) && (os_running == 0)) {
  1161. // Privileged and not running
  1162. return svcPoolCreate(pool_def);
  1163. } else {
  1164. return __svcPoolCreate(pool_def);
  1165. }
  1166. }
  1167. /// Allocate a memory block from a memory pool
  1168. void *osPoolAlloc (osPoolId pool_id) {
  1169. if ((__get_IPSR() != 0) || ((__get_CONTROL() & 1) == 0)) { // in ISR or Privileged
  1170. return sysPoolAlloc(pool_id, 0);
  1171. } else { // in Thread
  1172. return __sysPoolAlloc(pool_id, 0);
  1173. }
  1174. }
  1175. /// Allocate a memory block from a memory pool and set memory block to zero
  1176. void *osPoolCAlloc (osPoolId pool_id) {
  1177. if ((__get_IPSR() != 0) || ((__get_CONTROL() & 1) == 0)) { // in ISR or Privileged
  1178. return sysPoolAlloc(pool_id, 1);
  1179. } else { // in Thread
  1180. return __sysPoolAlloc(pool_id, 1);
  1181. }
  1182. }
  1183. /// Return an allocated memory block back to a specific memory pool
  1184. osStatus osPoolFree (osPoolId pool_id, void *block) {
  1185. if ((__get_IPSR() != 0) || ((__get_CONTROL() & 1) == 0)) { // in ISR or Privileged
  1186. return sysPoolFree(pool_id, block);
  1187. } else { // in Thread
  1188. return __sysPoolFree(pool_id, block);
  1189. }
  1190. }
  1191. // ==== Message Queue Management Functions ====
  1192. // Message Queue Management Service Calls declarations
  1193. SVC_2_1(svcMessageCreate, osMessageQId, osMessageQDef_t *, osThreadId, RET_pointer)
  1194. SVC_3_1(svcMessagePut, osStatus, osMessageQId, uint32_t, uint32_t, RET_osStatus)
  1195. SVC_2_3(svcMessageGet, os_InRegs osEvent, osMessageQId, uint32_t, RET_osEvent)
  1196. // Message Queue Service Calls
  1197. /// Create and Initialize Message Queue
  1198. osMessageQId svcMessageCreate (osMessageQDef_t *queue_def, osThreadId thread_id) {
  1199. if ((queue_def == NULL) ||
  1200. (queue_def->queue_sz == 0) ||
  1201. (queue_def->pool == NULL)) {
  1202. sysThreadError(osErrorParameter);
  1203. return NULL;
  1204. }
  1205. if (((P_MCB)queue_def->pool)->cb_type != 0) {
  1206. sysThreadError(osErrorParameter);
  1207. return NULL;
  1208. }
  1209. rt_mbx_init(queue_def->pool, 4*(queue_def->queue_sz + 4));
  1210. return queue_def->pool;
  1211. }
  1212. /// Put a Message to a Queue
  1213. osStatus svcMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec) {
  1214. OS_RESULT res;
  1215. if (queue_id == NULL) return osErrorParameter;
  1216. if (((P_MCB)queue_id)->cb_type != MCB) return osErrorParameter;
  1217. res = rt_mbx_send(queue_id, (void *)info, rt_ms2tick(millisec));
  1218. if (res == OS_R_TMO) {
  1219. return (millisec ? osErrorTimeoutResource : osErrorResource);
  1220. }
  1221. return osOK;
  1222. }
  1223. /// Get a Message or Wait for a Message from a Queue
  1224. os_InRegs osEvent_type svcMessageGet (osMessageQId queue_id, uint32_t millisec) {
  1225. OS_RESULT res;
  1226. osEvent ret;
  1227. if (queue_id == NULL) {
  1228. ret.status = osErrorParameter;
  1229. return osEvent_ret_status;
  1230. }
  1231. if (((P_MCB)queue_id)->cb_type != MCB) {
  1232. ret.status = osErrorParameter;
  1233. return osEvent_ret_status;
  1234. }
  1235. res = rt_mbx_wait(queue_id, &ret.value.p, rt_ms2tick(millisec));
  1236. if (res == OS_R_TMO) {
  1237. ret.status = millisec ? osEventTimeout : osOK;
  1238. return osEvent_ret_value;
  1239. }
  1240. ret.status = osEventMessage;
  1241. return osEvent_ret_value;
  1242. }
  1243. // Message Queue ISR Calls
  1244. /// Put a Message to a Queue
  1245. static __INLINE osStatus isrMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec) {
  1246. if ((queue_id == NULL) || (millisec != 0)) {
  1247. return osErrorParameter;
  1248. }
  1249. if (((P_MCB)queue_id)->cb_type != MCB) return osErrorParameter;
  1250. if (rt_mbx_check(queue_id) == 0) { // Check if Queue is full
  1251. return osErrorResource;
  1252. }
  1253. isr_mbx_send(queue_id, (void *)info);
  1254. return osOK;
  1255. }
  1256. /// Get a Message or Wait for a Message from a Queue
  1257. static __INLINE os_InRegs osEvent isrMessageGet (osMessageQId queue_id, uint32_t millisec) {
  1258. OS_RESULT res;
  1259. osEvent ret;
  1260. if ((queue_id == NULL) || (millisec != 0)) {
  1261. ret.status = osErrorParameter;
  1262. return ret;
  1263. }
  1264. if (((P_MCB)queue_id)->cb_type != MCB) {
  1265. ret.status = osErrorParameter;
  1266. return ret;
  1267. }
  1268. res = isr_mbx_receive(queue_id, &ret.value.p);
  1269. if (res != OS_R_MBX) {
  1270. ret.status = osOK;
  1271. return ret;
  1272. }
  1273. ret.status = osEventMessage;
  1274. return ret;
  1275. }
  1276. // Message Queue Management Public API
  1277. /// Create and Initialize Message Queue
  1278. osMessageQId osMessageCreate (osMessageQDef_t *queue_def, osThreadId thread_id) {
  1279. if (__get_IPSR() != 0) return NULL; // Not allowed in ISR
  1280. if (((__get_CONTROL() & 1) == 0) && (os_running == 0)) {
  1281. // Privileged and not running
  1282. return svcMessageCreate(queue_def, thread_id);
  1283. } else {
  1284. return __svcMessageCreate(queue_def, thread_id);
  1285. }
  1286. }
  1287. /// Put a Message to a Queue
  1288. osStatus osMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec) {
  1289. if (__get_IPSR() != 0) { // in ISR
  1290. return isrMessagePut(queue_id, info, millisec);
  1291. } else { // in Thread
  1292. return __svcMessagePut(queue_id, info, millisec);
  1293. }
  1294. }
  1295. /// Get a Message or Wait for a Message from a Queue
  1296. os_InRegs osEvent osMessageGet (osMessageQId queue_id, uint32_t millisec) {
  1297. if (__get_IPSR() != 0) { // in ISR
  1298. return isrMessageGet(queue_id, millisec);
  1299. } else { // in Thread
  1300. return __svcMessageGet(queue_id, millisec);
  1301. }
  1302. }
  1303. // ==== Mail Queue Management Functions ====
  1304. // Mail Queue Management Service Calls declarations
  1305. SVC_2_1(svcMailCreate, osMailQId, osMailQDef_t *, osThreadId, RET_pointer)
  1306. SVC_4_1(sysMailAlloc, void *, osMailQId, uint32_t, uint32_t, uint32_t, RET_pointer)
  1307. SVC_3_1(sysMailFree, osStatus, osMailQId, void *, uint32_t, RET_osStatus)
  1308. // Mail Queue Management Service & ISR Calls
  1309. /// Create and Initialize mail queue
  1310. osMailQId svcMailCreate (osMailQDef_t *queue_def, osThreadId thread_id) {
  1311. uint32_t blk_sz;
  1312. P_MCB pmcb;
  1313. void *pool;
  1314. if ((queue_def == NULL) ||
  1315. (queue_def->queue_sz == 0) ||
  1316. (queue_def->item_sz == 0) ||
  1317. (queue_def->pool == NULL)) {
  1318. sysThreadError(osErrorParameter);
  1319. return NULL;
  1320. }
  1321. pmcb = *(((void **)queue_def->pool) + 0);
  1322. pool = *(((void **)queue_def->pool) + 1);
  1323. if ((pool == NULL) || (pmcb == NULL) || (pmcb->cb_type != 0)) {
  1324. sysThreadError(osErrorParameter);
  1325. return NULL;
  1326. }
  1327. blk_sz = (queue_def->item_sz + 3) & ~3;
  1328. _init_box(pool, sizeof(struct OS_BM) + queue_def->queue_sz * blk_sz, blk_sz);
  1329. rt_mbx_init(pmcb, 4*(queue_def->queue_sz + 4));
  1330. return queue_def->pool;
  1331. }
  1332. /// Allocate a memory block from a mail
  1333. void *sysMailAlloc (osMailQId queue_id, uint32_t millisec, uint32_t isr, uint32_t clr) {
  1334. P_MCB pmcb;
  1335. void *pool;
  1336. void *mem;
  1337. if (queue_id == NULL) return NULL;
  1338. pmcb = *(((void **)queue_id) + 0);
  1339. pool = *(((void **)queue_id) + 1);
  1340. if ((pool == NULL) || (pmcb == NULL)) return NULL;
  1341. if (isr && (millisec != 0)) return NULL;
  1342. mem = rt_alloc_box(pool);
  1343. if (clr) {
  1344. rt_clr_box(pool, mem);
  1345. }
  1346. if ((mem == NULL) && (millisec != 0)) {
  1347. // Put Task to sleep when Memory not available
  1348. if (pmcb->p_lnk != NULL) {
  1349. rt_put_prio((P_XCB)pmcb, os_tsk.run);
  1350. } else {
  1351. pmcb->p_lnk = os_tsk.run;
  1352. os_tsk.run->p_lnk = NULL;
  1353. os_tsk.run->p_rlnk = (P_TCB)pmcb;
  1354. // Task is waiting to allocate a message
  1355. pmcb->state = 3;
  1356. }
  1357. rt_block(rt_ms2tick(millisec), WAIT_MBX);
  1358. }
  1359. return mem;
  1360. }
  1361. /// Free a memory block from a mail
  1362. osStatus sysMailFree (osMailQId queue_id, void *mail, uint32_t isr) {
  1363. P_MCB pmcb;
  1364. P_TCB ptcb;
  1365. void *pool;
  1366. void *mem;
  1367. int32_t res;
  1368. if (queue_id == NULL) return osErrorParameter;
  1369. pmcb = *(((void **)queue_id) + 0);
  1370. pool = *(((void **)queue_id) + 1);
  1371. if ((pmcb == NULL) || (pool == NULL)) return osErrorParameter;
  1372. res = rt_free_box(pool, mail);
  1373. if (res != 0) return osErrorValue;
  1374. if (pmcb->state == 3) {
  1375. // Task is waiting to allocate a message
  1376. if (isr) {
  1377. rt_psq_enq (pmcb, (U32)pool);
  1378. rt_psh_req ();
  1379. } else {
  1380. mem = rt_alloc_box(pool);
  1381. if (mem != NULL) {
  1382. ptcb = rt_get_first((P_XCB)pmcb);
  1383. if (pmcb->p_lnk == NULL) {
  1384. pmcb->state = 0;
  1385. }
  1386. rt_ret_val(ptcb, (U32)mem);
  1387. rt_rmv_dly(ptcb);
  1388. rt_dispatch(ptcb);
  1389. }
  1390. }
  1391. }
  1392. return osOK;
  1393. }
  1394. // Mail Queue Management Public API
  1395. /// Create and Initialize mail queue
  1396. osMailQId osMailCreate (osMailQDef_t *queue_def, osThreadId thread_id) {
  1397. if (__get_IPSR() != 0) return NULL; // Not allowed in ISR
  1398. if (((__get_CONTROL() & 1) == 0) && (os_running == 0)) {
  1399. // Privileged and not running
  1400. return svcMailCreate(queue_def, thread_id);
  1401. } else {
  1402. return __svcMailCreate(queue_def, thread_id);
  1403. }
  1404. }
  1405. /// Allocate a memory block from a mail
  1406. void *osMailAlloc (osMailQId queue_id, uint32_t millisec) {
  1407. if (__get_IPSR() != 0) { // in ISR
  1408. return sysMailAlloc(queue_id, millisec, 1, 0);
  1409. } else { // in Thread
  1410. return __sysMailAlloc(queue_id, millisec, 0, 0);
  1411. }
  1412. }
  1413. /// Allocate a memory block from a mail and set memory block to zero
  1414. void *osMailCAlloc (osMailQId queue_id, uint32_t millisec) {
  1415. if (__get_IPSR() != 0) { // in ISR
  1416. return sysMailAlloc(queue_id, millisec, 1, 1);
  1417. } else { // in Thread
  1418. return __sysMailAlloc(queue_id, millisec, 0, 1);
  1419. }
  1420. }
  1421. /// Free a memory block from a mail
  1422. osStatus osMailFree (osMailQId queue_id, void *mail) {
  1423. if (__get_IPSR() != 0) { // in ISR
  1424. return sysMailFree(queue_id, mail, 1);
  1425. } else { // in Thread
  1426. return __sysMailFree(queue_id, mail, 0);
  1427. }
  1428. }
  1429. /// Put a mail to a queue
  1430. osStatus osMailPut (osMailQId queue_id, void *mail) {
  1431. if (queue_id == NULL) return osErrorParameter;
  1432. if (mail == NULL) return osErrorValue;
  1433. return osMessagePut(*((void **)queue_id), (uint32_t)mail, 0);
  1434. }
  1435. #ifdef __CC_ARM
  1436. #pragma push
  1437. #pragma Ospace
  1438. #endif // __arm__
  1439. /// Get a mail from a queue
  1440. os_InRegs osEvent osMailGet (osMailQId queue_id, uint32_t millisec) {
  1441. osEvent ret;
  1442. if (queue_id == NULL) {
  1443. ret.status = osErrorParameter;
  1444. return ret;
  1445. }
  1446. ret = osMessageGet(*((void **)queue_id), millisec);
  1447. if (ret.status == osEventMessage) ret.status = osEventMail;
  1448. return ret;
  1449. }
  1450. #ifdef __CC_ARM
  1451. #pragma pop
  1452. #endif // __arm__