osapi.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file osapi.c
  15. * @brief OS API module code.
  16. *
  17. * @addtogroup nasa_osapi
  18. * @{
  19. */
  20. #include <stdarg.h>
  21. #include <string.h>
  22. #include "ch.h"
  23. #include "common_types.h"
  24. #include "osapi.h"
  25. #if CH_CFG_ST_FREQUENCY > 1000000
  26. #error "CH_CFG_ST_FREQUENCY limit is 1000000"
  27. #endif
  28. #if (CH_CFG_ST_FREQUENCY % 1000) != 0
  29. #error "CH_CFG_ST_FREQUENCY is not a multiple of 1000"
  30. #endif
  31. #if CH_CFG_USE_REGISTRY == FALSE
  32. #error "NASA OSAL requires CH_CFG_USE_REGISTRY"
  33. #endif
  34. #if CH_CFG_USE_EVENTS == FALSE
  35. #error "NASA OSAL requires CH_CFG_USE_EVENTS"
  36. #endif
  37. #if CH_CFG_USE_MUTEXES == FALSE
  38. #error "NASA OSAL requires CH_CFG_USE_MUTEXES"
  39. #endif
  40. #if CH_CFG_USE_SEMAPHORES == FALSE
  41. #error "NASA OSAL requires CH_CFG_USE_SEMAPHORES"
  42. #endif
  43. #if CH_CFG_USE_MEMCORE == FALSE
  44. #error "NASA OSAL requires CH_CFG_USE_MEMCORE"
  45. #endif
  46. #if CH_CFG_USE_MEMPOOLS == FALSE
  47. #error "NASA OSAL requires CH_CFG_USE_MEMPOOLS"
  48. #endif
  49. #if CH_CFG_USE_HEAP == FALSE
  50. #error "NASA OSAL requires CH_CFG_USE_HEAP"
  51. #endif
  52. /*===========================================================================*/
  53. /* Module local definitions. */
  54. /*===========================================================================*/
  55. #define MIN_PRIORITY 1
  56. #define MAX_PRIORITY 255
  57. #define MIN_MESSAGE_SIZE 4
  58. #define MAX_MESSAGE_SIZE 16384
  59. #define MIN_QUEUE_DEPTH 1
  60. #define MAX_QUEUE_DEPTH 16384
  61. /*===========================================================================*/
  62. /* Module exported variables. */
  63. /*===========================================================================*/
  64. /*===========================================================================*/
  65. /* Module local types. */
  66. /*===========================================================================*/
  67. /**
  68. * @brief Generic function pointer type.
  69. */
  70. typedef void (*funcptr_t)(void);
  71. /**
  72. * @brief Type of OSAL timer.
  73. */
  74. typedef struct {
  75. uint32 is_free;
  76. char name[OS_MAX_API_NAME];
  77. OS_TimerCallback_t callback_ptr;
  78. uint32 start_time;
  79. uint32 interval_time;
  80. virtual_timer_t vt;
  81. } osal_timer_t;
  82. /**
  83. * @brief Type of an OSAL queue.
  84. */
  85. typedef struct {
  86. uint32 is_free;
  87. char name[OS_MAX_API_NAME];
  88. semaphore_t free_msgs;
  89. memory_pool_t messages;
  90. mailbox_t mb;
  91. msg_t *mb_buffer;
  92. void *q_buffer;
  93. uint32 depth;
  94. uint32 size;
  95. } osal_queue_t;
  96. /**
  97. * @brief Type of an osal message with minimum size.
  98. */
  99. typedef struct {
  100. size_t size;
  101. char buf[4];
  102. } osal_message_t;
  103. /**
  104. * @brief Type of OSAL main structure.
  105. */
  106. typedef struct {
  107. bool printf_enabled;
  108. int (*printf)(const char *fmt, ...);
  109. virtual_timer_t vt;
  110. OS_time_t localtime;
  111. memory_pool_t timers_pool;
  112. memory_pool_t queues_pool;
  113. memory_pool_t binary_semaphores_pool;
  114. memory_pool_t count_semaphores_pool;
  115. memory_pool_t mutexes_pool;
  116. osal_timer_t timers[OS_MAX_TIMERS];
  117. osal_queue_t queues[OS_MAX_QUEUES];
  118. binary_semaphore_t binary_semaphores[OS_MAX_BIN_SEMAPHORES];
  119. semaphore_t count_semaphores[OS_MAX_COUNT_SEMAPHORES];
  120. mutex_t mutexes[OS_MAX_MUTEXES];
  121. } osal_t;
  122. /*===========================================================================*/
  123. /* Module local variables. */
  124. /*===========================================================================*/
  125. static osal_t osal;
  126. /*===========================================================================*/
  127. /* Module local functions. */
  128. /*===========================================================================*/
  129. /**
  130. * @brief System time callback.
  131. */
  132. static void systime_update(void *p) {
  133. sysinterval_t delay = (sysinterval_t)p;
  134. chSysLockFromISR();
  135. osal.localtime.microsecs += 1000;
  136. if (osal.localtime.microsecs >= 1000000) {
  137. osal.localtime.microsecs = 0;
  138. osal.localtime.seconds++;
  139. }
  140. chVTDoSetI(&osal.vt, delay, systime_update, p);
  141. chSysUnlockFromISR();
  142. }
  143. /**
  144. * @brief Virtual timers callback.
  145. */
  146. static void timer_handler(void *p) {
  147. osal_timer_t *otp = (osal_timer_t *)p;
  148. /* Real callback.*/
  149. otp->callback_ptr((uint32)p);
  150. /* Timer restart if an interval is defined.*/
  151. if (otp->interval_time != 0) {
  152. chSysLockFromISR();
  153. chVTSetI(&otp->vt, TIME_US2I(otp->interval_time), timer_handler, p);
  154. chSysUnlockFromISR();
  155. }
  156. }
  157. /**
  158. * @brief Finds a queue by name.
  159. */
  160. uint32 queue_find(const char *queue_name) {
  161. osal_queue_t *oqp;
  162. /* Searching the queue in the table.*/
  163. for (oqp = &osal.queues[0]; oqp < &osal.queues[OS_MAX_QUEUES]; oqp++) {
  164. /* Entering a reentrant critical zone.*/
  165. syssts_t sts = chSysGetStatusAndLockX();
  166. if (!oqp->is_free &&
  167. (strncmp(oqp->name, queue_name, OS_MAX_API_NAME - 1) == 0)) {
  168. /* Leaving the critical zone.*/
  169. chSysRestoreStatusX(sts);
  170. return (uint32)oqp;
  171. }
  172. /* Leaving the critical zone.*/
  173. chSysRestoreStatusX(sts);
  174. }
  175. return 0;
  176. }
  177. /**
  178. * @brief Finds a timer by name.
  179. */
  180. uint32 timer_find(const char *timer_name) {
  181. osal_timer_t *otp;
  182. /* Searching the queue in the table.*/
  183. for (otp = &osal.timers[0]; otp < &osal.timers[OS_MAX_TIMERS]; otp++) {
  184. /* Entering a reentrant critical zone.*/
  185. syssts_t sts = chSysGetStatusAndLockX();
  186. if (!otp->is_free &&
  187. (strncmp(otp->name, timer_name, OS_MAX_API_NAME - 1) == 0)) {
  188. /* Leaving the critical zone.*/
  189. chSysRestoreStatusX(sts);
  190. return (uint32)otp;
  191. }
  192. /* Leaving the critical zone.*/
  193. chSysRestoreStatusX(sts);
  194. }
  195. return 0;
  196. }
  197. /*===========================================================================*/
  198. /* Module exported functions. */
  199. /*===========================================================================*/
  200. /*-- Initialization API -----------------------------------------------------*/
  201. /**
  202. * @brief OS initialization.
  203. * @details This function returns initializes the internal data structures
  204. * of the OS Abstraction Layer. It must be called in the application
  205. * startup code before calling any other OS routines.
  206. *
  207. * @return An error code.
  208. *
  209. * @api
  210. */
  211. int32 OS_API_Init(void) {
  212. chSysInit();
  213. /* OS_printf() initially disabled.*/
  214. osal.printf_enabled = false;
  215. osal.printf = NULL;
  216. /* System time handling.*/
  217. osal.localtime.microsecs = 0;
  218. osal.localtime.seconds = 0;
  219. chVTObjectInit(&osal.vt);
  220. chVTSet(&osal.vt, TIME_MS2I(1), systime_update, (void *)TIME_MS2I(1));
  221. /* Timers pool initialization.*/
  222. chPoolObjectInit(&osal.timers_pool,
  223. sizeof (osal_timer_t),
  224. NULL);
  225. chPoolLoadArray(&osal.timers_pool,
  226. &osal.timers[0],
  227. OS_MAX_TIMERS);
  228. /* Queues pool initialization.*/
  229. chPoolObjectInit(&osal.queues_pool,
  230. sizeof (osal_queue_t),
  231. NULL);
  232. chPoolLoadArray(&osal.queues_pool,
  233. &osal.queues[0],
  234. OS_MAX_QUEUES);
  235. /* Binary Semaphores pool initialization.*/
  236. chPoolObjectInit(&osal.binary_semaphores_pool,
  237. sizeof (binary_semaphore_t),
  238. NULL);
  239. chPoolLoadArray(&osal.binary_semaphores_pool,
  240. &osal.binary_semaphores[0],
  241. OS_MAX_BIN_SEMAPHORES);
  242. /* Counter Semaphores pool initialization.*/
  243. chPoolObjectInit(&osal.count_semaphores_pool,
  244. sizeof (semaphore_t),
  245. NULL);
  246. chPoolLoadArray(&osal.count_semaphores_pool,
  247. &osal.count_semaphores[0],
  248. OS_MAX_COUNT_SEMAPHORES);
  249. /* Mutexes pool initialization.*/
  250. chPoolObjectInit(&osal.mutexes_pool,
  251. sizeof (mutex_t),
  252. NULL);
  253. chPoolLoadArray(&osal.mutexes_pool,
  254. &osal.mutexes[0],
  255. OS_MAX_MUTEXES);
  256. return OS_SUCCESS;
  257. }
  258. /*-- Various API -----------------------------------------------------------*/
  259. /**
  260. * @brief OS printf-like function.
  261. * @note It is initially disabled.
  262. *
  263. * @param[in] string formatter string
  264. *
  265. * @api
  266. */
  267. void OS_printf(const char *string, ...) {
  268. va_list ap;
  269. if (osal.printf_enabled && (osal.printf != NULL)) {
  270. va_start(ap, string);
  271. (void) osal.printf(string);
  272. va_end(ap);
  273. }
  274. }
  275. /**
  276. * @brief Disables @p OS_printf().
  277. *
  278. * @api
  279. */
  280. void OS_printf_disable(void) {
  281. osal.printf_enabled = false;
  282. }
  283. /**
  284. * @brief Enables @p OS_printf().
  285. *
  286. * @api
  287. */
  288. void OS_printf_enable(void) {
  289. osal.printf_enabled = true;
  290. }
  291. /**
  292. * @brief Sets the system printf function.
  293. * @note By default the printf function is not defined.
  294. * @note This is a ChibiOS/RT extension.
  295. *
  296. * @param[in] printf pointer to a @p printf() like function
  297. *
  298. * @api
  299. */
  300. void OS_set_printf(int (*printf)(const char *fmt, ...)) {
  301. osal.printf = printf;
  302. }
  303. /**
  304. * @brief System tick period in microseconds.
  305. *
  306. * @return The system tick period.
  307. */
  308. int32 OS_Tick2Micros(void) {
  309. return 1000000 / CH_CFG_ST_FREQUENCY;
  310. }
  311. /**
  312. * @brief Returns the local time.
  313. *
  314. * @param[out] time_struct the system time
  315. * @return An error code.
  316. *
  317. * @api
  318. */
  319. int32 OS_GetLocalTime(OS_time_t *time_struct) {
  320. if (time_struct == NULL) {
  321. return OS_INVALID_POINTER;
  322. }
  323. chSysLock();
  324. *time_struct = osal.localtime;
  325. chSysUnlock();
  326. return OS_SUCCESS;
  327. }
  328. /**
  329. * @brief Changes the local time.
  330. *
  331. * @param[in] time_struct the system time
  332. * @return An error code.
  333. *
  334. * @api
  335. */
  336. int32 OS_SetLocalTime(OS_time_t *time_struct) {
  337. if (time_struct == NULL) {
  338. return OS_INVALID_POINTER;
  339. }
  340. chSysLock();
  341. osal.localtime = *time_struct;
  342. chSysUnlock();
  343. return OS_SUCCESS;
  344. }
  345. /**
  346. * @brief Conversion from milliseconds to ticks.
  347. *
  348. * @param[in] milli_seconds the time in milliseconds
  349. * @return The system ticks.
  350. *
  351. * @api
  352. */
  353. int32 OS_Milli2Ticks(uint32 milli_seconds) {
  354. return (int32)TIME_MS2I(milli_seconds);
  355. }
  356. /*-- timers API -------------------------------------------------------------*/
  357. /**
  358. * @brief Timer creation.
  359. *
  360. * @param[out] timer_id pointer to a timer id variable
  361. * @param[in] timer_name the timer name
  362. * @param[out] clock_accuracy timer accuracy in microseconds
  363. * @param[in] callback_ptr timer callback
  364. * @return An error code.
  365. *
  366. * @api
  367. */
  368. int32 OS_TimerCreate(uint32 *timer_id, const char *timer_name,
  369. uint32 *clock_accuracy, OS_TimerCallback_t callback_ptr) {
  370. osal_timer_t *otp;
  371. /* NULL pointer checks.*/
  372. if ((timer_id == NULL) || (timer_name == NULL) ||
  373. (clock_accuracy == NULL)) {
  374. return OS_INVALID_POINTER;
  375. }
  376. /* NULL callback check.*/
  377. if (callback_ptr == NULL) {
  378. *timer_id = 0;
  379. return OS_TIMER_ERR_INVALID_ARGS;
  380. }
  381. /* Checking timer name length.*/
  382. if (strlen(timer_name) >= OS_MAX_API_NAME) {
  383. *timer_id = 0;
  384. return OS_ERR_NAME_TOO_LONG;
  385. }
  386. /* Checking if the name is already taken.*/
  387. if (timer_find(timer_name) > 0) {
  388. *timer_id = 0;
  389. return OS_ERR_NAME_TAKEN;
  390. }
  391. /* Getting object.*/
  392. otp = chPoolAlloc(&osal.timers_pool);
  393. if (otp == NULL) {
  394. *timer_id = 0;
  395. return OS_ERR_NO_FREE_IDS;
  396. }
  397. strncpy(otp->name, timer_name, OS_MAX_API_NAME - 1);
  398. chVTObjectInit(&otp->vt);
  399. otp->start_time = 0;
  400. otp->interval_time = 0;
  401. otp->callback_ptr = callback_ptr;
  402. otp->is_free = 0; /* Note, last.*/
  403. *timer_id = (uint32)otp;
  404. *clock_accuracy = (uint32)(1000000 / CH_CFG_ST_FREQUENCY);
  405. return OS_SUCCESS;
  406. }
  407. /**
  408. * @brief Timer deletion.
  409. *
  410. * @param[in] timer_id timer id variable
  411. * @return An error code.
  412. *
  413. * @api
  414. */
  415. int32 OS_TimerDelete(uint32 timer_id) {
  416. osal_timer_t *otp = (osal_timer_t *)timer_id;
  417. /* Range check.*/
  418. if ((otp < &osal.timers[0]) ||
  419. (otp >= &osal.timers[OS_MAX_TIMERS]) ||
  420. (otp->is_free)) {
  421. return OS_ERR_INVALID_ID;
  422. }
  423. chSysLock();
  424. /* Marking as no more free, will be overwritten by the pool pointer.*/
  425. otp->is_free = 1;
  426. /* Resetting the timer.*/
  427. chVTResetI(&otp->vt);
  428. otp->start_time = 0;
  429. otp->interval_time = 0;
  430. /* Flagging it as unused and returning it to the pool.*/
  431. chPoolFreeI(&osal.timers_pool, (void *)otp);
  432. chSysUnlock();
  433. return OS_SUCCESS;
  434. }
  435. /**
  436. * @brief Timer deletion.
  437. * @note This function can be safely called from timer callbacks or ISRs.
  438. *
  439. * @param[in] timer_id timer id variable
  440. * @param[in] start_time start time in microseconds or zero
  441. * @param[in] interval_time interval time in microseconds or zero
  442. * @return An error code.
  443. *
  444. * @api
  445. */
  446. int32 OS_TimerSet(uint32 timer_id, uint32 start_time, uint32 interval_time) {
  447. syssts_t sts;
  448. osal_timer_t *otp = (osal_timer_t *)timer_id;
  449. /* Range check.*/
  450. if ((otp < &osal.timers[0]) ||
  451. (otp >= &osal.timers[OS_MAX_TIMERS]) ||
  452. (otp->is_free)) {
  453. return OS_ERR_INVALID_ID;
  454. }
  455. /* Entering a reentrant critical zone.*/
  456. sts = chSysGetStatusAndLockX();
  457. if (start_time == 0) {
  458. chVTResetI(&otp->vt);
  459. }
  460. else {
  461. otp->start_time = start_time;
  462. otp->interval_time = interval_time;
  463. chVTSetI(&otp->vt, TIME_US2I(start_time), timer_handler, (void *)timer_id);
  464. }
  465. /* Leaving the critical zone.*/
  466. chSysRestoreStatusX(sts);
  467. return OS_SUCCESS;
  468. }
  469. /**
  470. * @brief Retrieves a timer id by name.
  471. *
  472. * @param[out] timer_id pointer to a timer id variable
  473. * @param[in] sem_name the timer name
  474. * @return An error code.
  475. *
  476. * @api
  477. */
  478. int32 OS_TimerGetIdByName(uint32 *timer_id, const char *timer_name) {
  479. /* NULL pointer checks.*/
  480. if ((timer_id == NULL) || (timer_name == NULL)) {
  481. return OS_INVALID_POINTER;
  482. }
  483. /* Checking name length.*/
  484. if (strlen(timer_name) >= OS_MAX_API_NAME) {
  485. return OS_ERR_NAME_TOO_LONG;
  486. }
  487. /* Searching the queue.*/
  488. *timer_id = timer_find(timer_name);
  489. if (*timer_id > 0) {
  490. return OS_SUCCESS;
  491. }
  492. return OS_ERR_NAME_NOT_FOUND;
  493. }
  494. /**
  495. * @brief Returns timer information.
  496. * @note This function can be safely called from timer callbacks or ISRs.
  497. *
  498. * @param[in] timer_id timer id variable
  499. * @param[in] timer_prop timer properties
  500. * @return An error code.
  501. *
  502. * @api
  503. */
  504. int32 OS_TimerGetInfo(uint32 timer_id, OS_timer_prop_t *timer_prop) {
  505. syssts_t sts;
  506. osal_timer_t *otp = (osal_timer_t *)timer_id;
  507. /* NULL pointer checks.*/
  508. if (timer_prop == NULL) {
  509. return OS_INVALID_POINTER;
  510. }
  511. /* Range check.*/
  512. if ((otp < &osal.timers[0]) ||
  513. (otp >= &osal.timers[OS_MAX_TIMERS]) ||
  514. (otp->is_free)) {
  515. return OS_ERR_INVALID_ID;
  516. }
  517. /* Entering a reentrant critical zone.*/
  518. sts = chSysGetStatusAndLockX();
  519. /* If the timer is not in use then error.*/
  520. if (otp->is_free) {
  521. /* Leaving the critical zone.*/
  522. chSysRestoreStatusX(sts);
  523. return OS_ERR_INVALID_ID;
  524. }
  525. strncpy(timer_prop->name, otp->name, OS_MAX_API_NAME - 1);
  526. timer_prop->creator = (uint32)0;
  527. timer_prop->start_time = otp->start_time;
  528. timer_prop->interval_time = otp->interval_time;
  529. timer_prop->accuracy = (uint32)(1000000 / CH_CFG_ST_FREQUENCY);
  530. /* Leaving the critical zone.*/
  531. chSysRestoreStatusX(sts);
  532. return OS_SUCCESS;
  533. }
  534. /*-- Queues API -------------------------------------------------------------*/
  535. /**
  536. * @brief Queue creation.
  537. *
  538. * @param[out] queue_id pointer to a queue id variable
  539. * @param[in] queue_name the queue name
  540. * @param[in] queue_depth desired queue depth
  541. * @param[in] data_size maximum message size
  542. * @param[in] flags queue option flags
  543. * @return An error code.
  544. *
  545. * @api
  546. */
  547. int32 OS_QueueCreate(uint32 *queue_id, const char *queue_name,
  548. uint32 queue_depth, uint32 data_size, uint32 flags) {
  549. osal_queue_t *oqp;
  550. size_t msgsize;
  551. (void)flags;
  552. /* NULL pointer checks.*/
  553. if ((queue_id == NULL) || (queue_name == NULL)) {
  554. return OS_INVALID_POINTER;
  555. }
  556. /* Checking queue name length.*/
  557. if (strlen(queue_name) >= OS_MAX_API_NAME) {
  558. *queue_id = 0;
  559. return OS_ERR_NAME_TOO_LONG;
  560. }
  561. /* Checking if the name is already taken.*/
  562. if (queue_find(queue_name) > 0) {
  563. *queue_id = 0;
  564. return OS_ERR_NAME_TAKEN;
  565. }
  566. /* Checks on queue limits. There is no dedicated error code.*/
  567. if ((data_size < MIN_MESSAGE_SIZE) || (data_size > MAX_MESSAGE_SIZE) ||
  568. (queue_depth < MIN_QUEUE_DEPTH) || (queue_depth > MAX_QUEUE_DEPTH)) {
  569. *queue_id = 0;
  570. return OS_ERROR;
  571. }
  572. /* Getting object.*/
  573. oqp = chPoolAlloc(&osal.queues_pool);
  574. if (oqp == NULL) {
  575. *queue_id = 0;
  576. return OS_ERR_NO_FREE_IDS;
  577. }
  578. /* Attempting messages buffer allocation.*/
  579. msgsize = MEM_ALIGN_NEXT(data_size + sizeof (size_t), PORT_NATURAL_ALIGN);
  580. oqp->mb_buffer = chHeapAllocAligned(NULL,
  581. msgsize * (size_t)queue_depth,
  582. PORT_NATURAL_ALIGN);
  583. if (oqp->mb_buffer == NULL) {
  584. *queue_id = 0;
  585. return OS_ERROR;
  586. }
  587. /* Attempting queue buffer allocation.*/
  588. oqp->q_buffer = chHeapAllocAligned(NULL,
  589. sizeof (msg_t) * (size_t)queue_depth,
  590. PORT_NATURAL_ALIGN);
  591. if (oqp->q_buffer == NULL) {
  592. *queue_id = 0;
  593. chHeapFree(oqp->mb_buffer);
  594. return OS_ERROR;
  595. }
  596. /* Initializing object static parts.*/
  597. strncpy(oqp->name, queue_name, OS_MAX_API_NAME - 1);
  598. chMBObjectInit(&oqp->mb, oqp->q_buffer, (size_t)queue_depth);
  599. chSemObjectInit(&oqp->free_msgs, (cnt_t)queue_depth);
  600. chPoolObjectInit(&oqp->messages, msgsize, NULL);
  601. chPoolLoadArray(&oqp->messages, oqp->mb_buffer, (size_t)queue_depth);
  602. oqp->depth = queue_depth;
  603. oqp->size = data_size;
  604. oqp->is_free = 0; /* Note, last.*/
  605. *queue_id = (uint32)oqp;
  606. return OS_SUCCESS;
  607. }
  608. /**
  609. * @brief Queue deletion.
  610. *
  611. * @param[in] queue_id queue id variable
  612. * @return An error code.
  613. *
  614. * @api
  615. */
  616. int32 OS_QueueDelete(uint32 queue_id) {
  617. osal_queue_t *oqp = (osal_queue_t *)queue_id;
  618. void *q_buffer, *mb_buffer;
  619. /* Range check.*/
  620. if ((oqp < &osal.queues[0]) ||
  621. (oqp >= &osal.queues[OS_MAX_QUEUES]) ||
  622. (oqp->is_free)) {
  623. return OS_ERR_INVALID_ID;
  624. }
  625. /* Critical zone.*/
  626. chSysLock();
  627. /* Marking as no more free, will be overwritten by the pool pointer.*/
  628. oqp->is_free = 1;
  629. /* Pointers to areas to be freed.*/
  630. q_buffer = oqp->q_buffer;
  631. mb_buffer = oqp->mb_buffer;
  632. /* Resetting the queue.*/
  633. chMBResetI(&oqp->mb);
  634. chSemResetI(&oqp->free_msgs, 0);
  635. /* Flagging it as unused and returning it to the pool.*/
  636. chPoolFreeI(&osal.queues_pool, (void *)oqp);
  637. chSchRescheduleS();
  638. /* Leaving critical zone.*/
  639. chSysUnlock();
  640. /* Freeing buffers, outside critical zone, slow heap operation.*/
  641. chHeapFree(q_buffer);
  642. chHeapFree(mb_buffer);
  643. return OS_SUCCESS;
  644. }
  645. /**
  646. * @brief Retrieves a message from the queue.
  647. *
  648. * @param[in] queue_id queue id variable
  649. * @param[out] data message buffer pointer
  650. * @param[in] size size of the buffer
  651. * @param[out] size_copied size of the received message
  652. * @param[in] timeout timeout in ticks, the special values @p OS_PEND
  653. * and @p OS_CHECK can be specified
  654. * @return An error code.
  655. *
  656. * @api
  657. */
  658. int32 OS_QueueGet(uint32 queue_id, void *data, uint32 size,
  659. uint32 *size_copied, int32 timeout) {
  660. osal_queue_t *oqp = (osal_queue_t *)queue_id;
  661. msg_t msg, msgsts;
  662. void *body;
  663. /* NULL pointer checks.*/
  664. if ((data == NULL) || (size_copied == NULL)) {
  665. return OS_INVALID_POINTER;
  666. }
  667. /* Range check.*/
  668. if ((oqp < &osal.queues[0]) ||
  669. (oqp >= &osal.queues[OS_MAX_QUEUES]) ||
  670. (oqp->is_free)) {
  671. return OS_ERR_INVALID_ID;
  672. }
  673. /* Check on minimum size.*/
  674. if (size < oqp->size) {
  675. return OS_QUEUE_INVALID_SIZE;
  676. }
  677. /* Special time handling.*/
  678. if (timeout == OS_PEND) {
  679. msgsts = chMBFetchTimeout(&oqp->mb, &msg, TIME_INFINITE);
  680. if (msgsts < MSG_OK) {
  681. *size_copied = 0;
  682. return OS_ERROR;
  683. }
  684. }
  685. else if (timeout == OS_CHECK) {
  686. msgsts = chMBFetchTimeout(&oqp->mb, &msg, TIME_IMMEDIATE);
  687. if (msgsts < MSG_OK) {
  688. *size_copied = 0;
  689. return OS_QUEUE_EMPTY;
  690. }
  691. }
  692. else {
  693. msgsts = chMBFetchTimeout(&oqp->mb, &msg, (sysinterval_t)timeout);
  694. if (msgsts < MSG_OK) {
  695. *size_copied = 0;
  696. return OS_QUEUE_TIMEOUT;
  697. }
  698. }
  699. /* Message body and size.*/
  700. *size_copied = ((osal_message_t *)msg)->size;
  701. body = (void *)((osal_message_t *)msg)->buf;
  702. /* Copying the message body.*/
  703. memcpy(data, body, *size_copied);
  704. /* Freeing the message buffer.*/
  705. chPoolFree(&oqp->messages, (void *)msg);
  706. chSemSignal(&oqp->free_msgs);
  707. return OS_SUCCESS;
  708. }
  709. /**
  710. * @brief Puts a message in the queue.
  711. *
  712. * @param[in] queue_id queue id variable
  713. * @param[in] data message buffer pointer
  714. * @param[in] size size of the message
  715. * @param[in] flags operation flags
  716. * @return An error code.
  717. *
  718. * @api
  719. */
  720. int32 OS_QueuePut(uint32 queue_id, void *data, uint32 size, uint32 flags) {
  721. osal_queue_t *oqp = (osal_queue_t *)queue_id;
  722. msg_t msgsts;
  723. osal_message_t *omsg;
  724. (void)flags;
  725. /* NULL pointer checks.*/
  726. if (data == NULL) {
  727. return OS_INVALID_POINTER;
  728. }
  729. /* Range check.*/
  730. if ((oqp < &osal.queues[0]) ||
  731. (oqp >= &osal.queues[OS_MAX_QUEUES]) ||
  732. (oqp->is_free)) {
  733. return OS_ERR_INVALID_ID;
  734. }
  735. /* Check on maximum size.*/
  736. if (size > oqp->size) {
  737. return OS_QUEUE_INVALID_SIZE;
  738. }
  739. /* Getting a message buffer from the pool.*/
  740. msgsts = chSemWait(&oqp->free_msgs);
  741. if (msgsts < MSG_OK) {
  742. return OS_ERROR;
  743. }
  744. omsg = chPoolAlloc(&oqp->messages);
  745. /* Filling message size and data.*/
  746. omsg->size = (size_t)size;
  747. memcpy(omsg->buf, data, size);
  748. /* Posting the message.*/
  749. msgsts = chMBPostTimeout(&oqp->mb, (msg_t)omsg, TIME_INFINITE);
  750. if (msgsts < MSG_OK) {
  751. return OS_ERROR;
  752. }
  753. return OS_SUCCESS;
  754. }
  755. /**
  756. * @brief Retrieves a queue id by name.
  757. *
  758. * @param[out] queue_id pointer to a queue id variable
  759. * @param[in] sem_name the queue name
  760. * @return An error code.
  761. *
  762. * @api
  763. */
  764. int32 OS_QueueGetIdByName(uint32 *queue_id, const char *queue_name) {
  765. /* NULL pointer checks.*/
  766. if ((queue_id == NULL) || (queue_name == NULL)) {
  767. return OS_INVALID_POINTER;
  768. }
  769. /* Checking name length.*/
  770. if (strlen(queue_name) >= OS_MAX_API_NAME) {
  771. return OS_ERR_NAME_TOO_LONG;
  772. }
  773. /* Searching the queue.*/
  774. *queue_id = queue_find(queue_name);
  775. if (*queue_id > 0) {
  776. return OS_SUCCESS;
  777. }
  778. return OS_ERR_NAME_NOT_FOUND;
  779. }
  780. /**
  781. * @brief Returns queue information.
  782. * @note This function can be safely called from timer callbacks or ISRs.
  783. *
  784. * @param[in] queue_id queue id variable
  785. * @param[in] queue_prop queue properties
  786. * @return An error code.
  787. *
  788. * @api
  789. */
  790. int32 OS_QueueGetInfo (uint32 queue_id, OS_queue_prop_t *queue_prop) {
  791. osal_queue_t *oqp = (osal_queue_t *)queue_id;
  792. syssts_t sts;
  793. /* NULL pointer checks.*/
  794. if (queue_prop == NULL) {
  795. return OS_INVALID_POINTER;
  796. }
  797. /* Range check.*/
  798. if ((oqp < &osal.queues[0]) ||
  799. (oqp >= &osal.queues[OS_MAX_QUEUES]) ||
  800. (oqp->is_free)) {
  801. return OS_ERR_INVALID_ID;
  802. }
  803. /* Entering a reentrant critical zone.*/
  804. sts = chSysGetStatusAndLockX();
  805. /* If the queue is not in use then error.*/
  806. if (oqp->is_free) {
  807. /* Leaving the critical zone.*/
  808. chSysRestoreStatusX(sts);
  809. return OS_ERR_INVALID_ID;
  810. }
  811. strncpy(queue_prop->name, oqp->name, OS_MAX_API_NAME - 1);
  812. queue_prop->creator = (uint32)0;
  813. /* Leaving the critical zone.*/
  814. chSysRestoreStatusX(sts);
  815. return OS_SUCCESS;
  816. return OS_ERR_NOT_IMPLEMENTED;
  817. }
  818. /*-- Binary Semaphore API ---------------------------------------------------*/
  819. /**
  820. * @brief Binary semaphore creation.
  821. *
  822. * @param[out] sem_id pointer to a binary semaphore id variable
  823. * @param[in] sem_name the binary semaphore name
  824. * @param[in] sem_initial_value semaphore initial value
  825. * @param[in] options semaphore options
  826. * @return An error code.
  827. *
  828. * @api
  829. */
  830. int32 OS_BinSemCreate(uint32 *sem_id, const char *sem_name,
  831. uint32 sem_initial_value, uint32 options) {
  832. binary_semaphore_t *bsp;
  833. (void)options;
  834. /* NULL pointer checks.*/
  835. if ((sem_id == NULL) || (sem_name == NULL)) {
  836. return OS_INVALID_POINTER;
  837. }
  838. /* Checking semaphore name length.*/
  839. if (strlen(sem_name) >= OS_MAX_API_NAME) {
  840. return OS_ERR_NAME_TOO_LONG;
  841. }
  842. /* Semaphore counter check, it is binary so only 0 and 1.*/
  843. if (sem_initial_value > 1) {
  844. return OS_INVALID_INT_NUM;
  845. }
  846. /* Getting object.*/
  847. bsp = chPoolAlloc(&osal.binary_semaphores_pool);
  848. if (bsp == NULL) {
  849. return OS_ERR_NO_FREE_IDS;
  850. }
  851. /* Semaphore is initialized.*/
  852. chBSemObjectInit(bsp, sem_initial_value == 0 ? true : false);
  853. *sem_id = (uint32)bsp;
  854. return OS_SUCCESS;
  855. }
  856. /**
  857. * @brief Binary semaphore deletion.
  858. *
  859. * @param[in] sem_id binary semaphore id variable
  860. * @return An error code.
  861. *
  862. * @api
  863. */
  864. int32 OS_BinSemDelete(uint32 sem_id) {
  865. binary_semaphore_t *bsp = (binary_semaphore_t *)sem_id;
  866. /* Range check.*/
  867. if ((bsp < &osal.binary_semaphores[0]) ||
  868. (bsp >= &osal.binary_semaphores[OS_MAX_BIN_SEMAPHORES])) {
  869. return OS_ERR_INVALID_ID;
  870. }
  871. chSysLock();
  872. /* Resetting the semaphore, no threads in queue.*/
  873. chBSemResetI(bsp, true);
  874. /* Flagging it as unused and returning it to the pool.*/
  875. bsp->sem.queue.prev = NULL;
  876. chPoolFreeI(&osal.binary_semaphores_pool, (void *)bsp);
  877. /* Required because some thread could have been made ready.*/
  878. chSchRescheduleS();
  879. chSysUnlock();
  880. return OS_SUCCESS;
  881. }
  882. /**
  883. * @brief Binary semaphore flush.
  884. * @note The state of the binary semaphore is not changed.
  885. * @note This function can be safely called from timer callbacks or ISRs.
  886. *
  887. * @param[in] sem_id binary semaphore id variable
  888. * @return An error code.
  889. *
  890. * @api
  891. */
  892. int32 OS_BinSemFlush(uint32 sem_id) {
  893. syssts_t sts;
  894. binary_semaphore_t *bsp = (binary_semaphore_t *)sem_id;
  895. /* Range check.*/
  896. if ((bsp < &osal.binary_semaphores[0]) ||
  897. (bsp >= &osal.binary_semaphores[OS_MAX_BIN_SEMAPHORES])) {
  898. return OS_ERR_INVALID_ID;
  899. }
  900. /* Entering a reentrant critical zone.*/
  901. sts = chSysGetStatusAndLockX();
  902. /* If the semaphore is not in use then error.*/
  903. if (bsp->sem.queue.prev == NULL) {
  904. /* Leaving the critical zone.*/
  905. chSysRestoreStatusX(sts);
  906. return OS_SEM_FAILURE;
  907. }
  908. /* If the semaphore state is "not taken" then it is not touched.*/
  909. if (bsp->sem.cnt < 0) {
  910. chBSemResetI(bsp, true);
  911. }
  912. /* Leaving the critical zone.*/
  913. chSysRestoreStatusX(sts);
  914. return OS_SUCCESS;
  915. }
  916. /**
  917. * @brief Binary semaphore give.
  918. * @note This function can be safely called from timer callbacks or ISRs.
  919. *
  920. * @param[in] sem_id binary semaphore id variable
  921. * @return An error code.
  922. *
  923. * @api
  924. */
  925. int32 OS_BinSemGive(uint32 sem_id) {
  926. syssts_t sts;
  927. binary_semaphore_t *bsp = (binary_semaphore_t *)sem_id;
  928. /* Range check.*/
  929. if ((bsp < &osal.binary_semaphores[0]) ||
  930. (bsp >= &osal.binary_semaphores[OS_MAX_BIN_SEMAPHORES])) {
  931. return OS_ERR_INVALID_ID;
  932. }
  933. /* Entering a reentrant critical zone.*/
  934. sts = chSysGetStatusAndLockX();
  935. /* If the semaphore is not in use then error.*/
  936. if (bsp->sem.queue.prev == NULL) {
  937. /* Leaving the critical zone.*/
  938. chSysRestoreStatusX(sts);
  939. return OS_SEM_FAILURE;
  940. }
  941. chBSemSignalI(bsp);
  942. /* Leaving the critical zone.*/
  943. chSysRestoreStatusX(sts);
  944. return OS_SUCCESS;
  945. }
  946. /**
  947. * @brief Binary semaphore take.
  948. *
  949. * @param[in] sem_id binary semaphore id variable
  950. * @return An error code.
  951. *
  952. * @api
  953. */
  954. int32 OS_BinSemTake(uint32 sem_id) {
  955. binary_semaphore_t *bsp = (binary_semaphore_t *)sem_id;
  956. /* Range check.*/
  957. if ((bsp < &osal.binary_semaphores[0]) ||
  958. (bsp >= &osal.binary_semaphores[OS_MAX_BIN_SEMAPHORES])) {
  959. return OS_ERR_INVALID_ID;
  960. }
  961. chSysLock();
  962. /* If the semaphore is not in use then error.*/
  963. if (bsp->sem.queue.prev == NULL) {
  964. chSysUnlock();
  965. return OS_SEM_FAILURE;
  966. }
  967. (void) chBSemWaitS(bsp);
  968. chSysUnlock();
  969. return OS_SUCCESS;
  970. }
  971. /**
  972. * @brief Binary semaphore take with timeout.
  973. *
  974. * @param[in] sem_id binary semaphore id variable
  975. * @param[in] msecs timeout in milliseconds
  976. * @return An error code.
  977. *
  978. * @api
  979. */
  980. int32 OS_BinSemTimedWait(uint32 sem_id, uint32 msecs) {
  981. binary_semaphore_t *bsp = (binary_semaphore_t *)sem_id;
  982. msg_t msg;
  983. /* Range check.*/
  984. if ((bsp < &osal.binary_semaphores[0]) ||
  985. (bsp >= &osal.binary_semaphores[OS_MAX_BIN_SEMAPHORES])) {
  986. return OS_ERR_INVALID_ID;
  987. }
  988. /* Timeouts of zero not allowed.*/
  989. if (msecs == 0) {
  990. return OS_INVALID_INT_NUM;
  991. }
  992. chSysLock();
  993. /* If the semaphore is not in use then error.*/
  994. if (bsp->sem.queue.prev == NULL) {
  995. chSysUnlock();
  996. return OS_SEM_FAILURE;
  997. }
  998. msg = chBSemWaitTimeoutS(bsp, TIME_MS2I(msecs));
  999. chSysUnlock();
  1000. return msg == MSG_TIMEOUT ? OS_SEM_TIMEOUT : OS_SUCCESS;
  1001. }
  1002. /**
  1003. * @brief Retrieves a binary semaphore id by name.
  1004. * @note It is not currently implemented.
  1005. *
  1006. * @param[out] sem_id pointer to a binary semaphore id variable
  1007. * @param[in] sem_name the binary semaphore name
  1008. * @return An error code.
  1009. *
  1010. * @api
  1011. */
  1012. int32 OS_BinSemGetIdByName(uint32 *sem_id, const char *sem_name) {
  1013. /* NULL pointer checks.*/
  1014. if ((sem_id == NULL) || (sem_name == NULL)) {
  1015. return OS_INVALID_POINTER;
  1016. }
  1017. /* Checking name length.*/
  1018. if (strlen(sem_name) >= OS_MAX_API_NAME) {
  1019. return OS_ERR_NAME_TOO_LONG;
  1020. }
  1021. return OS_ERR_NOT_IMPLEMENTED;
  1022. }
  1023. /**
  1024. * @brief Returns binary semaphore information.
  1025. * @note This function can be safely called from timer callbacks or ISRs.
  1026. * @note It is not currently implemented.
  1027. *
  1028. * @param[in] sem_id binary semaphore id variable
  1029. * @param[in] bin_prop binary semaphore properties
  1030. * @return An error code.
  1031. *
  1032. * @api
  1033. */
  1034. int32 OS_BinSemGetInfo(uint32 sem_id, OS_bin_sem_prop_t *bin_prop) {
  1035. syssts_t sts;
  1036. binary_semaphore_t *bsp = (binary_semaphore_t *)sem_id;
  1037. /* NULL pointer checks.*/
  1038. if (bin_prop == NULL) {
  1039. return OS_INVALID_POINTER;
  1040. }
  1041. /* Range check.*/
  1042. if ((bsp < &osal.binary_semaphores[0]) ||
  1043. (bsp >= &osal.binary_semaphores[OS_MAX_BIN_SEMAPHORES])) {
  1044. return OS_ERR_INVALID_ID;
  1045. }
  1046. /* Entering a reentrant critical zone.*/
  1047. sts = chSysGetStatusAndLockX();
  1048. /* If the semaphore is not in use then error.*/
  1049. if (bsp->sem.queue.prev == NULL) {
  1050. /* Leaving the critical zone.*/
  1051. chSysRestoreStatusX(sts);
  1052. return OS_ERR_INVALID_ID;
  1053. }
  1054. /* Leaving the critical zone.*/
  1055. chSysRestoreStatusX(sts);
  1056. return OS_ERR_NOT_IMPLEMENTED;
  1057. }
  1058. /*-- Counter Semaphore API --------------------------------------------------*/
  1059. /**
  1060. * @brief Counter semaphore creation.
  1061. *
  1062. * @param[out] sem_id pointer to a counter semaphore id variable
  1063. * @param[in] sem_name the counter semaphore name
  1064. * @param[in] sem_initial_value semaphore initial value
  1065. * @param[in] options semaphore options
  1066. * @return An error code.
  1067. *
  1068. * @api
  1069. */
  1070. int32 OS_CountSemCreate(uint32 *sem_id, const char *sem_name,
  1071. uint32 sem_initial_value, uint32 options) {
  1072. semaphore_t *sp;
  1073. (void)options;
  1074. /* NULL pointer checks.*/
  1075. if ((sem_id == NULL) || (sem_name == NULL)) {
  1076. return OS_INVALID_POINTER;
  1077. }
  1078. /* Checking semaphore name length.*/
  1079. if (strlen(sem_name) >= OS_MAX_API_NAME) {
  1080. return OS_ERR_NAME_TOO_LONG;
  1081. }
  1082. /* Semaphore counter check, it must be non-negative.*/
  1083. if ((int32)sem_initial_value < 0) {
  1084. return OS_INVALID_INT_NUM;
  1085. }
  1086. /* Getting object.*/
  1087. sp = chPoolAlloc(&osal.count_semaphores_pool);
  1088. if (sp == NULL) {
  1089. return OS_ERR_NO_FREE_IDS;
  1090. }
  1091. /* Semaphore is initialized.*/
  1092. chSemObjectInit(sp, (cnt_t)sem_initial_value);
  1093. *sem_id = (uint32)sp;
  1094. return OS_SUCCESS;
  1095. }
  1096. /**
  1097. * @brief Counter semaphore deletion.
  1098. *
  1099. * @param[in] sem_id counter semaphore id variable
  1100. * @return An error code.
  1101. *
  1102. * @api
  1103. */
  1104. int32 OS_CountSemDelete(uint32 sem_id) {
  1105. semaphore_t *sp = (semaphore_t *)sem_id;
  1106. /* Range check.*/
  1107. if ((sp < &osal.count_semaphores[0]) ||
  1108. (sp >= &osal.count_semaphores[OS_MAX_COUNT_SEMAPHORES])) {
  1109. return OS_ERR_INVALID_ID;
  1110. }
  1111. chSysLock();
  1112. /* Resetting the semaphore, no threads in queue.*/
  1113. chSemResetI(sp, 0);
  1114. /* Flagging it as unused and returning it to the pool.*/
  1115. sp->queue.prev = NULL;
  1116. chPoolFreeI(&osal.count_semaphores_pool, (void *)sp);
  1117. /* Required because some thread could have been made ready.*/
  1118. chSchRescheduleS();
  1119. chSysUnlock();
  1120. return OS_SUCCESS;
  1121. }
  1122. /**
  1123. * @brief Counter semaphore give.
  1124. * @note This function can be safely called from timer callbacks or ISRs.
  1125. *
  1126. * @param[in] sem_id counter semaphore id variable
  1127. * @return An error code.
  1128. *
  1129. * @api
  1130. */
  1131. int32 OS_CountSemGive(uint32 sem_id) {
  1132. syssts_t sts;
  1133. semaphore_t *sp = (semaphore_t *)sem_id;
  1134. /* Range check.*/
  1135. if ((sp < &osal.count_semaphores[0]) ||
  1136. (sp >= &osal.count_semaphores[OS_MAX_COUNT_SEMAPHORES])) {
  1137. return OS_ERR_INVALID_ID;
  1138. }
  1139. /* Entering a reentrant critical zone.*/
  1140. sts = chSysGetStatusAndLockX();
  1141. /* If the semaphore is not in use then error.*/
  1142. if (sp->queue.prev == NULL) {
  1143. /* Leaving the critical zone.*/
  1144. chSysRestoreStatusX(sts);
  1145. return OS_SEM_FAILURE;
  1146. }
  1147. chSemSignalI(sp);
  1148. /* Leaving the critical zone.*/
  1149. chSysRestoreStatusX(sts);
  1150. return OS_SUCCESS;
  1151. }
  1152. /**
  1153. * @brief Counter semaphore take.
  1154. *
  1155. * @param[in] sem_id counter semaphore id variable
  1156. * @return An error code.
  1157. *
  1158. * @api
  1159. */
  1160. int32 OS_CountSemTake(uint32 sem_id) {
  1161. semaphore_t *sp = (semaphore_t *)sem_id;
  1162. /* Range check.*/
  1163. if ((sp < &osal.count_semaphores[0]) ||
  1164. (sp >= &osal.count_semaphores[OS_MAX_COUNT_SEMAPHORES])) {
  1165. return OS_ERR_INVALID_ID;
  1166. }
  1167. chSysLock();
  1168. /* If the semaphore is not in use then error.*/
  1169. if (sp->queue.prev == NULL) {
  1170. chSysUnlock();
  1171. return OS_SEM_FAILURE;
  1172. }
  1173. (void) chSemWaitS(sp);
  1174. chSysUnlock();
  1175. return OS_SUCCESS;
  1176. }
  1177. /**
  1178. * @brief Counter semaphore take with timeout.
  1179. *
  1180. * @param[in] sem_id counter semaphore id variable
  1181. * @param[in] msecs timeout in milliseconds
  1182. * @return An error code.
  1183. *
  1184. * @api
  1185. */
  1186. int32 OS_CountSemTimedWait(uint32 sem_id, uint32 msecs) {
  1187. semaphore_t *sp = (semaphore_t *)sem_id;
  1188. msg_t msg;
  1189. /* Range check.*/
  1190. if ((sp < &osal.count_semaphores[0]) ||
  1191. (sp >= &osal.count_semaphores[OS_MAX_COUNT_SEMAPHORES])) {
  1192. return OS_ERR_INVALID_ID;
  1193. }
  1194. /* Timeouts of zero not allowed.*/
  1195. if (msecs == 0) {
  1196. return OS_INVALID_INT_NUM;
  1197. }
  1198. chSysLock();
  1199. /* If the semaphore is not in use then error.*/
  1200. if (sp->queue.prev == NULL) {
  1201. chSysUnlock();
  1202. return OS_SEM_FAILURE;
  1203. }
  1204. msg = chSemWaitTimeoutS(sp, TIME_MS2I(msecs));
  1205. chSysUnlock();
  1206. return msg == MSG_TIMEOUT ? OS_SEM_TIMEOUT : OS_SUCCESS;
  1207. }
  1208. /**
  1209. * @brief Retrieves a counter semaphore id by name.
  1210. * @note It is not currently implemented.
  1211. *
  1212. * @param[out] sem_id pointer to a counter semaphore id variable
  1213. * @param[in] sem_name the counter semaphore name
  1214. * @return An error code.
  1215. *
  1216. * @api
  1217. */
  1218. int32 OS_CountSemGetIdByName(uint32 *sem_id, const char *sem_name) {
  1219. /* NULL pointer checks.*/
  1220. if ((sem_id == NULL) || (sem_name == NULL)) {
  1221. return OS_INVALID_POINTER;
  1222. }
  1223. /* Checking name length.*/
  1224. if (strlen(sem_name) >= OS_MAX_API_NAME) {
  1225. return OS_ERR_NAME_TOO_LONG;
  1226. }
  1227. return OS_ERR_NOT_IMPLEMENTED;
  1228. }
  1229. /**
  1230. * @brief Returns counter semaphore information.
  1231. * @note This function can be safely called from timer callbacks or ISRs.
  1232. * @note It is not currently implemented.
  1233. *
  1234. * @param[in] sem_id counter semaphore id variable
  1235. * @param[in] sem_prop counter semaphore properties
  1236. * @return An error code.
  1237. *
  1238. * @api
  1239. */
  1240. int32 OS_CountSemGetInfo(uint32 sem_id, OS_count_sem_prop_t *sem_prop) {
  1241. syssts_t sts;
  1242. semaphore_t *sp = (semaphore_t *)sem_id;
  1243. /* NULL pointer checks.*/
  1244. if (sem_prop == NULL) {
  1245. return OS_INVALID_POINTER;
  1246. }
  1247. /* Range check.*/
  1248. if ((sp < &osal.count_semaphores[0]) ||
  1249. (sp >= &osal.count_semaphores[OS_MAX_BIN_SEMAPHORES])) {
  1250. return OS_ERR_INVALID_ID;
  1251. }
  1252. /* Entering a reentrant critical zone.*/
  1253. sts = chSysGetStatusAndLockX();
  1254. /* If the semaphore is not in use then error.*/
  1255. if (sp->queue.prev == NULL) {
  1256. /* Leaving the critical zone.*/
  1257. chSysRestoreStatusX(sts);
  1258. return OS_ERR_INVALID_ID;
  1259. }
  1260. /* Leaving the critical zone.*/
  1261. chSysRestoreStatusX(sts);
  1262. return OS_ERR_NOT_IMPLEMENTED;
  1263. }
  1264. /*-- Mutex API --------------------------------------------------------------*/
  1265. /**
  1266. * @brief Mutex creation.
  1267. *
  1268. * @param[out] sem_id pointer to a mutex id variable
  1269. * @param[in] sem_name the mutex name
  1270. * @param[in] options mutex options
  1271. * @return An error code.
  1272. *
  1273. * @api
  1274. */
  1275. int32 OS_MutSemCreate(uint32 *sem_id, const char *sem_name, uint32 options) {
  1276. mutex_t *mp;
  1277. (void)options;
  1278. /* NULL pointer checks.*/
  1279. if ((sem_id == NULL) || (sem_name == NULL)) {
  1280. return OS_INVALID_POINTER;
  1281. }
  1282. /* Checking semaphore name length.*/
  1283. if (strlen(sem_name) >= OS_MAX_API_NAME) {
  1284. return OS_ERR_NAME_TOO_LONG;
  1285. }
  1286. /* Getting object.*/
  1287. mp = chPoolAlloc(&osal.mutexes_pool);
  1288. if (mp == NULL) {
  1289. return OS_ERR_NO_FREE_IDS;
  1290. }
  1291. /* Semaphore is initialized.*/
  1292. chMtxObjectInit(mp);
  1293. *sem_id = (uint32)mp;
  1294. return OS_SUCCESS;
  1295. }
  1296. /**
  1297. * @brief Mutex deletion.
  1298. *
  1299. * @param[in] sem_id mutex id variable
  1300. * @return An error code.
  1301. *
  1302. * @api
  1303. */
  1304. int32 OS_MutSemDelete(uint32 sem_id) {
  1305. mutex_t *mp = (mutex_t *)sem_id;
  1306. /* Range check.*/
  1307. if ((mp < &osal.mutexes[0]) ||
  1308. (mp >= &osal.mutexes[OS_MAX_MUTEXES])) {
  1309. return OS_ERR_INVALID_ID;
  1310. }
  1311. chSysLock();
  1312. /* Resetting the mutex, no threads in queue.*/
  1313. chMtxUnlockAllS();
  1314. /* Flagging it as unused and returning it to the pool.*/
  1315. mp->queue.prev = NULL;
  1316. chPoolFreeI(&osal.mutexes_pool, (void *)mp);
  1317. /* Required because some thread could have been made ready.*/
  1318. chSchRescheduleS();
  1319. chSysUnlock();
  1320. return OS_SUCCESS;
  1321. }
  1322. /**
  1323. * @brief Mutex give.
  1324. *
  1325. * @param[in] sem_id mutex id variable
  1326. * @return An error code.
  1327. *
  1328. * @api
  1329. */
  1330. int32 OS_MutSemGive(uint32 sem_id) {
  1331. mutex_t *mp = (mutex_t *)sem_id;
  1332. /* Range check.*/
  1333. if ((mp < &osal.mutexes[0]) ||
  1334. (mp >= &osal.mutexes[OS_MAX_COUNT_SEMAPHORES])) {
  1335. return OS_ERR_INVALID_ID;
  1336. }
  1337. chSysLock();
  1338. /* If the mutex is not in use then error.*/
  1339. if (mp->queue.prev == NULL) {
  1340. chSysUnlock();
  1341. return OS_SEM_FAILURE;
  1342. }
  1343. chMtxUnlockS(mp);
  1344. chSchRescheduleS();
  1345. chSysUnlock();
  1346. return OS_SUCCESS;
  1347. }
  1348. /**
  1349. * @brief Mutex take.
  1350. *
  1351. * @param[in] sem_id mutex id variable
  1352. * @return An error code.
  1353. *
  1354. * @api
  1355. */
  1356. int32 OS_MutSemTake(uint32 sem_id) {
  1357. mutex_t *mp = (mutex_t *)sem_id;
  1358. /* Range check.*/
  1359. if ((mp < &osal.mutexes[0]) ||
  1360. (mp >= &osal.mutexes[OS_MAX_COUNT_SEMAPHORES])) {
  1361. return OS_ERR_INVALID_ID;
  1362. }
  1363. chSysLock();
  1364. /* If the mutex is not in use then error.*/
  1365. if (mp->queue.prev == NULL) {
  1366. chSysUnlock();
  1367. return OS_SEM_FAILURE;
  1368. }
  1369. chMtxLockS(mp);
  1370. chSysUnlock();
  1371. return OS_SUCCESS;
  1372. }
  1373. /**
  1374. * @brief Retrieves a mutex id by name.
  1375. * @note It is not currently implemented.
  1376. *
  1377. * @param[out] sem_id pointer to a mutex id variable
  1378. * @param[in] sem_name the mutex name
  1379. * @return An error code.
  1380. *
  1381. * @api
  1382. */
  1383. int32 OS_MutSemGetIdByName(uint32 *sem_id, const char *sem_name) {
  1384. /* NULL pointer checks.*/
  1385. if ((sem_id == NULL) || (sem_name == NULL)) {
  1386. return OS_INVALID_POINTER;
  1387. }
  1388. /* Checking name length.*/
  1389. if (strlen(sem_name) >= OS_MAX_API_NAME) {
  1390. return OS_ERR_NAME_TOO_LONG;
  1391. }
  1392. return OS_ERR_NOT_IMPLEMENTED;
  1393. }
  1394. /**
  1395. * @brief Returns mutex information.
  1396. * @note This function can be safely called from timer callbacks or ISRs.
  1397. * @note It is not currently implemented.
  1398. *
  1399. * @param[in] sem_id mutex id variable
  1400. * @param[in] sem_prop mutex properties
  1401. * @return An error code.
  1402. *
  1403. * @api
  1404. */
  1405. int32 OS_MutSemGetInfo(uint32 sem_id, OS_mut_sem_prop_t *sem_prop) {
  1406. syssts_t sts;
  1407. mutex_t *mp = (mutex_t *)sem_id;
  1408. /* NULL pointer checks.*/
  1409. if (sem_prop == NULL) {
  1410. return OS_INVALID_POINTER;
  1411. }
  1412. /* Range check.*/
  1413. if ((mp < &osal.mutexes[0]) ||
  1414. (mp >= &osal.mutexes[OS_MAX_BIN_SEMAPHORES])) {
  1415. return OS_ERR_INVALID_ID;
  1416. }
  1417. /* Entering a reentrant critical zone.*/
  1418. sts = chSysGetStatusAndLockX();
  1419. /* If the mutex is not in use then error.*/
  1420. if (mp->queue.prev == NULL) {
  1421. /* Leaving the critical zone.*/
  1422. chSysRestoreStatusX(sts);
  1423. return OS_ERR_INVALID_ID;
  1424. }
  1425. /* Leaving the critical zone.*/
  1426. chSysRestoreStatusX(sts);
  1427. return OS_ERR_NOT_IMPLEMENTED;
  1428. }
  1429. /*-- Task Control API -------------------------------------------------------*/
  1430. /**
  1431. * @brief Task creation.
  1432. * @note The task name is not copied inside the task but kept by reference,
  1433. * the name is supposed to be persistent, better if defined as a
  1434. * sting constant.
  1435. *
  1436. * @param[out] task_id pointer to a task id variable
  1437. * @param[in] task_name the task name
  1438. * @param[in] function_pointer the task function
  1439. * @param[in] stack_pointer base of stack area
  1440. * @param[in] stack_size size of stack area
  1441. * @param[in] priority the task priority
  1442. * @param[in] flags task attributes
  1443. * @return An error code.
  1444. *
  1445. * @api
  1446. */
  1447. int32 OS_TaskCreate(uint32 *task_id,
  1448. const char *task_name,
  1449. osal_task_entry function_pointer,
  1450. const uint32 *stack_pointer,
  1451. uint32 stack_size,
  1452. uint32 priority,
  1453. uint32 flags) {
  1454. tprio_t rt_prio;
  1455. thread_t *tp;
  1456. (void)flags;
  1457. /* NULL pointer checks.*/
  1458. if ((task_id == NULL) || (task_name == NULL) ||
  1459. (function_pointer == NULL) || (stack_pointer == NULL)) {
  1460. return OS_INVALID_POINTER;
  1461. }
  1462. /* Checking alignment of stack base and size, it is application
  1463. responsibility to pass correct values.*/
  1464. if (!MEM_IS_ALIGNED(stack_pointer, PORT_WORKING_AREA_ALIGN) ||
  1465. !MEM_IS_ALIGNED(stack_size, sizeof (stkalign_t))) {
  1466. return OS_ERROR_ADDRESS_MISALIGNED;
  1467. }
  1468. /* Checking task name length.*/
  1469. if (strlen(task_name) >= OS_MAX_API_NAME) {
  1470. return OS_ERR_NAME_TOO_LONG;
  1471. }
  1472. /* Checking priority range.*/
  1473. if ((priority < MIN_PRIORITY) || (priority > MAX_PRIORITY)) {
  1474. return OS_ERR_INVALID_PRIORITY;
  1475. }
  1476. /* Checking if the specified stack size is below the bare minimum.*/
  1477. if (stack_size < (uint32)THD_WORKING_AREA_SIZE(0)) {
  1478. return OS_INVALID_INT_NUM;
  1479. }
  1480. /* Checking if this working area is already in use by some thread, the
  1481. error code is not very appropriate but this case seems to not be
  1482. coveded by the specification.*/
  1483. tp = chRegFindThreadByWorkingArea((stkalign_t *)stack_pointer);
  1484. if (tp != NULL) {
  1485. /* Releasing the thread reference.*/
  1486. chThdRelease(tp);
  1487. return OS_ERR_NO_FREE_IDS;
  1488. }
  1489. /* Checking if the name is already in use.*/
  1490. if ((tp = chRegFindThreadByName(task_name)) != NULL) {
  1491. /* Releasing the thread reference.*/
  1492. chThdRelease(tp);
  1493. return OS_ERR_NAME_TAKEN;
  1494. }
  1495. /* Converting priority to RT type.*/
  1496. rt_prio = (tprio_t)256 - (tprio_t)priority;
  1497. if (rt_prio == 1) {
  1498. rt_prio = 2;
  1499. }
  1500. thread_descriptor_t td = {
  1501. task_name,
  1502. (stkalign_t *)stack_pointer,
  1503. (stkalign_t *)((uint8_t *)stack_pointer + stack_size),
  1504. rt_prio,
  1505. (tfunc_t)(void *)function_pointer,
  1506. NULL
  1507. };
  1508. /* Creating the task and detaching it, other APIs will have to gain a
  1509. reference using the registry API.*/
  1510. tp = chThdCreate(&td);
  1511. chThdRelease(tp);
  1512. /* Storing the task id.*/
  1513. *task_id = (uint32)tp;
  1514. return OS_SUCCESS;
  1515. }
  1516. /**
  1517. * @brief Installs a deletion handler.
  1518. * @note It is implemented as hooks in chconf.h.
  1519. *
  1520. * @param[in] function_pointer the handler function
  1521. * @return An error code.
  1522. *
  1523. * @api
  1524. */
  1525. int32 OS_TaskInstallDeleteHandler(void *function_pointer) {
  1526. chThdGetSelfX()->osal_delete_handler = function_pointer;
  1527. return OS_SUCCESS;
  1528. }
  1529. /**
  1530. * @brief Check for task termination request.
  1531. * @note This is a ChibiOS/RT extension, direct task delete is not
  1532. * allowed in RT.
  1533. *
  1534. * @return The termination request flag.
  1535. * @retval false if termination has not been requested.
  1536. * @retval true if termination has been requested.
  1537. *
  1538. * @api
  1539. */
  1540. boolean OS_TaskDeleteCheck(void) {
  1541. return (boolean)chThdShouldTerminateX();
  1542. }
  1543. /**
  1544. * @brief Task delete.
  1545. * @note Limitation, it does not actually kill the thread, it just sets a
  1546. * flag in the thread that has then to terminate voluntarily. The
  1547. * flag can be checked using @p chThdShouldTerminateX().
  1548. *
  1549. * @param[in] task_id the task id
  1550. * @return An error code.
  1551. *
  1552. * @api
  1553. */
  1554. int32 OS_TaskDelete(uint32 task_id) {
  1555. thread_t *tp = (thread_t *)task_id;
  1556. funcptr_t fp;
  1557. /* Check for thread validity, getting a reference.*/
  1558. if (chRegFindThreadByPointer(tp) == NULL) {
  1559. return OS_ERR_INVALID_ID;
  1560. }
  1561. /* Asking for thread termination.*/
  1562. chThdTerminate(tp);
  1563. /* Getting the delete handler while the thread is still referenced.*/
  1564. fp = (funcptr_t)tp->osal_delete_handler;
  1565. /* Waiting for termination, releasing the reference.*/
  1566. chThdWait(tp);
  1567. /* Calling the delete handler, if defined.*/
  1568. if (fp != NULL) {
  1569. fp();
  1570. }
  1571. return OS_SUCCESS;
  1572. }
  1573. /**
  1574. * @brief Task exit.
  1575. *
  1576. * @api
  1577. */
  1578. void OS_TaskExit(void) {
  1579. chThdExit(MSG_OK);
  1580. }
  1581. /**
  1582. * @brief Wait for task termination.
  1583. * @note This is a ChibiOS/RT extension, added for improved testability.
  1584. *
  1585. * @param[in] task_id the task id
  1586. * @return An error code.
  1587. *
  1588. * @api
  1589. */
  1590. int32 OS_TaskWait(uint32 task_id) {
  1591. thread_t *tp = (thread_t *)task_id;
  1592. /* Check for thread validity, getting a reference.*/
  1593. if (chRegFindThreadByPointer(tp) == NULL) {
  1594. return OS_ERR_INVALID_ID;
  1595. }
  1596. (void) chThdWait(tp);
  1597. return OS_SUCCESS;
  1598. }
  1599. /**
  1600. * @brief Task delay.
  1601. *
  1602. * @param[in] milli_second the period in miliseconds
  1603. * @return An error code.
  1604. *
  1605. * @api
  1606. */
  1607. int32 OS_TaskDelay(uint32 milli_second) {
  1608. chThdSleepMilliseconds(milli_second);
  1609. return OS_SUCCESS;
  1610. }
  1611. /**
  1612. * @brief Change task priority.
  1613. * @note Priority 255 is not available and it is transformed internally in
  1614. * 254.
  1615. *
  1616. * @param[in] task_id the task id
  1617. * @param[in] new_priority the task new priority
  1618. * @return An error code.
  1619. *
  1620. * @api
  1621. */
  1622. int32 OS_TaskSetPriority(uint32 task_id, uint32 new_priority) {
  1623. tprio_t rt_newprio;
  1624. thread_t *tp = (thread_t *)task_id;
  1625. /* Checking priority range.*/
  1626. if ((new_priority < MIN_PRIORITY) || (new_priority > MAX_PRIORITY)) {
  1627. return OS_ERR_INVALID_PRIORITY;
  1628. }
  1629. /* Converting priority to RT type.*/
  1630. rt_newprio = (tprio_t)256 - (tprio_t)new_priority;
  1631. if (rt_newprio == 1) {
  1632. rt_newprio = 2;
  1633. }
  1634. if (chThdGetPriorityX() == rt_newprio) {
  1635. return OS_SUCCESS;
  1636. }
  1637. /* Check for thread validity.*/
  1638. if (chRegFindThreadByPointer(tp) == NULL) {
  1639. return OS_ERR_INVALID_ID;
  1640. }
  1641. chSysLock();
  1642. /* Changing priority.*/
  1643. if ((tp->prio == tp->realprio) || (rt_newprio > tp->prio)) {
  1644. tp->prio = rt_newprio;
  1645. }
  1646. tp->realprio = rt_newprio;
  1647. /* The following states need priority queues reordering.*/
  1648. switch (tp->state) {
  1649. case CH_STATE_WTMTX:
  1650. #if CH_CFG_USE_CONDVARS
  1651. case CH_STATE_WTCOND:
  1652. #endif
  1653. #if CH_CFG_USE_SEMAPHORES_PRIORITY
  1654. case CH_STATE_WTSEM:
  1655. #endif
  1656. #if CH_CFG_USE_MESSAGES && CH_CFG_USE_MESSAGES_PRIORITY
  1657. case CH_STATE_SNDMSGQ:
  1658. #endif
  1659. /* Re-enqueues tp with its new priority on the queue.*/
  1660. queue_prio_insert(queue_dequeue(tp),
  1661. (threads_queue_t *)tp->u.wtobjp);
  1662. break;
  1663. case CH_STATE_READY:
  1664. #if CH_DBG_ENABLE_ASSERTS
  1665. /* Prevents an assertion in chSchReadyI().*/
  1666. tp->state = CH_STATE_CURRENT;
  1667. #endif
  1668. /* Re-enqueues tp with its new priority on the ready list.*/
  1669. chSchReadyI(queue_dequeue(tp));
  1670. break;
  1671. }
  1672. /* Rescheduling.*/
  1673. chSchRescheduleS();
  1674. chSysUnlock();
  1675. /* Releasing the thread reference.*/
  1676. chThdRelease(tp);
  1677. return OS_SUCCESS;
  1678. }
  1679. /**
  1680. * @brief Task registration.
  1681. * @note In ChibiOS/RT it does nothing.
  1682. *
  1683. * @return An error code.
  1684. *
  1685. * @api
  1686. */
  1687. int32 OS_TaskRegister(void) {
  1688. return OS_SUCCESS;
  1689. }
  1690. /**
  1691. * @brief Current task id.
  1692. * @note This function can be safely called from timer callbacks or ISRs.
  1693. *
  1694. * @return The current task id.
  1695. *
  1696. * @api
  1697. */
  1698. uint32 OS_TaskGetId(void) {
  1699. return (uint32)chThdGetSelfX();
  1700. }
  1701. /**
  1702. * @brief Retrieves a task id by name.
  1703. *
  1704. * @param[out] task_id pointer to a task id variable
  1705. * @param[in] task_name the task name
  1706. * @return An error code.
  1707. *
  1708. * @api
  1709. */
  1710. int32 OS_TaskGetIdByName(uint32 *task_id, const char *task_name) {
  1711. thread_t *tp;
  1712. /* NULL pointer checks.*/
  1713. if ((task_id == NULL) || (task_name == NULL)) {
  1714. return OS_INVALID_POINTER;
  1715. }
  1716. /* Checking task name length.*/
  1717. if (strlen(task_name) >= OS_MAX_API_NAME) {
  1718. return OS_ERR_NAME_TOO_LONG;
  1719. }
  1720. /* Searching in the registry.*/
  1721. tp = chRegFindThreadByName(task_name);
  1722. if (tp == NULL) {
  1723. return OS_ERR_NAME_NOT_FOUND;
  1724. }
  1725. *task_id = (uint32)tp;
  1726. /* Releasing the thread reference.*/
  1727. chThdRelease(tp);
  1728. return OS_SUCCESS;
  1729. }
  1730. /**
  1731. * @brief Returns task information.
  1732. * @note This function can be safely called from timer callbacks or ISRs.
  1733. * @note Priority 255 is not available and it is transformed internally in
  1734. * 254.
  1735. *
  1736. * @param[in] task_id the task id
  1737. * @param[in] task_prop task properties
  1738. * @return An error code.
  1739. *
  1740. * @api
  1741. */
  1742. int32 OS_TaskGetInfo(uint32 task_id, OS_task_prop_t *task_prop) {
  1743. thread_t *tp = (thread_t *)task_id;
  1744. size_t wasize = (size_t)tp - (size_t)tp->wabase + sizeof (thread_t);
  1745. /* NULL pointer checks.*/
  1746. if (task_prop == NULL) {
  1747. return OS_INVALID_POINTER;
  1748. }
  1749. /* Check for thread validity.*/
  1750. if (chRegFindThreadByPointer(tp) == NULL) {
  1751. return OS_ERR_INVALID_ID;
  1752. }
  1753. strncpy(task_prop->name, tp->name, OS_MAX_API_NAME - 1);
  1754. task_prop->creator = (uint32)chSysGetIdleThreadX();
  1755. task_prop->stack_size = (uint32)MEM_ALIGN_NEXT(wasize, PORT_STACK_ALIGN);
  1756. task_prop->priority = (uint32)256U - (uint32)tp->realprio;
  1757. task_prop->OStask_id = task_id;
  1758. /* Releasing the thread reference.*/
  1759. chThdRelease(tp);
  1760. return OS_SUCCESS;
  1761. }
  1762. /*-- System Interrupt API ---------------------------------------------------*/
  1763. /* In ChibiOS interrupts are statically linked, the vectors table is in
  1764. flash.*/
  1765. int32 OS_IntAttachHandler (uint32 InterruptNumber,
  1766. osal_task_entry InterruptHandler,
  1767. int32 parameter) {
  1768. (void)InterruptNumber;
  1769. (void)parameter;
  1770. /* NULL pointer checks.*/
  1771. if (InterruptHandler == NULL) {
  1772. return OS_INVALID_POINTER;
  1773. }
  1774. return OS_ERR_NOT_IMPLEMENTED;
  1775. }
  1776. int32 OS_IntLock(void) {
  1777. return (int32)chSysGetStatusAndLockX();
  1778. }
  1779. int32 OS_IntUnlock(int32 IntLevel) {
  1780. chSysRestoreStatusX((syssts_t) IntLevel);
  1781. return OS_SUCCESS;
  1782. }
  1783. int32 OS_IntEnable(int32 Level) {
  1784. NVIC_EnableIRQ((IRQn_Type)Level);
  1785. return OS_SUCCESS;
  1786. }
  1787. int32 OS_IntDisable(int32 Level) {
  1788. NVIC_DisableIRQ((IRQn_Type)Level);
  1789. return OS_SUCCESS;
  1790. }
  1791. int32 OS_IntAck(int32 InterruptNumber) {
  1792. NVIC_ClearPendingIRQ((IRQn_Type)InterruptNumber);
  1793. return OS_SUCCESS;
  1794. }
  1795. /*-- System Exception API ---------------------------------------------------*/
  1796. /* In ChibiOS exceptions are statically linked, the vectors table is in
  1797. flash.*/
  1798. int32 OS_ExcAttachHandler(uint32 ExceptionNumber,
  1799. void (*ExceptionHandler)(uint32, uint32 *,uint32),
  1800. int32 parameter) {
  1801. (void)ExceptionNumber;
  1802. (void)parameter;
  1803. /* NULL pointer checks.*/
  1804. if (ExceptionHandler == NULL) {
  1805. return OS_INVALID_POINTER;
  1806. }
  1807. return OS_ERR_NOT_IMPLEMENTED;
  1808. }
  1809. /* No exceptions masking.*/
  1810. int32 OS_ExcEnable(int32 ExceptionNumber) {
  1811. (void)ExceptionNumber;
  1812. return OS_ERR_NOT_IMPLEMENTED;
  1813. }
  1814. /* No exceptions masking.*/
  1815. int32 OS_ExcDisable(int32 ExceptionNumber) {
  1816. (void)ExceptionNumber;
  1817. return OS_ERR_NOT_IMPLEMENTED;
  1818. }
  1819. /*-- Floating Point Unit API ------------------------------------------------*/
  1820. /* In ChibiOS exceptions are statically linked, the vectors table is in
  1821. flash.*/
  1822. int32 OS_FPUExcAttachHandler(uint32 ExceptionNumber,
  1823. void * ExceptionHandler ,
  1824. int32 parameter) {
  1825. (void)ExceptionNumber;
  1826. (void)parameter;
  1827. /* NULL pointer checks.*/
  1828. if (ExceptionHandler == NULL) {
  1829. return OS_INVALID_POINTER;
  1830. }
  1831. return OS_ERR_NOT_IMPLEMENTED;
  1832. }
  1833. int32 OS_FPUExcEnable(int32 ExceptionNumber) {
  1834. (void)ExceptionNumber;
  1835. return OS_ERR_NOT_IMPLEMENTED;
  1836. }
  1837. int32 OS_FPUExcDisable(int32 ExceptionNumber) {
  1838. (void)ExceptionNumber;
  1839. return OS_ERR_NOT_IMPLEMENTED;
  1840. }
  1841. int32 OS_FPUExcSetMask(uint32 mask) {
  1842. (void)mask;
  1843. return OS_ERR_NOT_IMPLEMENTED;
  1844. }
  1845. int32 OS_FPUExcGetMask(uint32 *mask) {
  1846. (void)mask;
  1847. return OS_ERR_NOT_IMPLEMENTED;
  1848. }
  1849. /** @} */