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 65KB

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