rt_CMSIS.c 64 KB

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