os_common_api.h 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  1. /*
  2. * Copyright (c) 2018 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file os common interface
  8. */
  9. #ifndef __OS_COMMON_API_H__
  10. #define __OS_COMMON_API_H__
  11. #include <zephyr.h>
  12. #include <mem_manager.h>
  13. #include <tracing/tracing.h>
  14. #define LOG_MODULE_NAME os_api
  15. #include <logging/log.h>
  16. #ifndef LOG_MODULE_CUSTOMER
  17. LOG_MODULE_DECLARE(os, LOG_LEVEL_INF);
  18. #endif
  19. #define LOG_PRINT_FUNC_NAME 0
  20. #ifdef CONFIG_ACTLOG
  21. #include <logging/act_log.h>
  22. #endif
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /**
  27. * @file
  28. *
  29. * @brief Public OS COMMON APIs.
  30. */
  31. /**
  32. * @brief os common APIs
  33. * @defgroup os_common_apis OS Common APIs
  34. * @{
  35. * @}
  36. */
  37. #define OS_FOREVER (-1)
  38. #define OS_ORI_FOREVER K_FOREVER
  39. #define OS_NO_WAIT (0)
  40. #define OS_MSEC(ms) K_MSEC(ms)
  41. #define OS_SECONDS(s) K_SECONDS(s)
  42. #define OS_MINUTES(m) K_MINUTES(m)
  43. #define OS_HOURS(h) K_HOURS(h)
  44. #define OS_ANY K_ANY
  45. #define OS_END K_END
  46. typedef struct k_mutex os_mutex;
  47. typedef struct k_sem os_sem;
  48. typedef struct k_timer os_timer;
  49. typedef struct k_delayed_work os_delayed_work;
  50. typedef struct k_work os_work;
  51. typedef struct k_work_q os_work_q;
  52. typedef struct k_msgq os_msgq;
  53. typedef struct k_mbox os_mbox;
  54. typedef struct k_pipe os_pipe;
  55. typedef struct k_fifo os_fifo;
  56. typedef struct k_lifo os_lifo;
  57. typedef struct k_mbox_msg os_mbox_msg;
  58. typedef struct k_thread os_thread;
  59. typedef k_thread_stack_t os_thread_stack_t;
  60. typedef k_tid_t os_tid_t;
  61. #define OS_MUTEX_DEFINE(name)\
  62. K_MUTEX_DEFINE(name)
  63. #define OS_FIFO_DEFINE(name)\
  64. K_FIFO_DEFINE(name)
  65. #define OS_SEM_DEFINE(name, initial_count, count_limit) \
  66. K_SEM_DEFINE(name, initial_count, count_limit)
  67. #define OS_WORK_DEFINE(work, work_handler) \
  68. K_WORK_DEFINE(work, work_handler)
  69. #define OS_DELAY_WORK_DEFINE(name, work_handler) \
  70. struct k_delayed_work name = { \
  71. .work = Z_WORK_DELAYABLE_INITIALIZER(work_handler) \
  72. }
  73. #define OS_THREAD_STACK_DEFINE(name, size) \
  74. K_THREAD_STACK_DEFINE(name, size)
  75. #define OS_TIMER_DEFINE(name, expiry_fn, stop_fn) \
  76. K_TIMER_DEFINE(name, expiry_fn, stop_fn)
  77. #define OS_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
  78. K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align)
  79. #ifdef CONFIG_USER_WORK_Q
  80. extern os_work_q user_work_q;
  81. #endif
  82. typedef enum
  83. {
  84. OS_POLL_TIMEOUT = 0,
  85. OS_POLL_MSG,
  86. OS_POLL_SEM,
  87. } os_poll_e;
  88. /**
  89. * @defgroup os_mutex_apis Os Mutex APIs
  90. * @ingroup os_common_apis
  91. * @{
  92. */
  93. /**
  94. * @brief Initialize a mutex.
  95. *
  96. * This routine initializes a mutex object, prior to its first use.
  97. *
  98. * Upon completion, the mutex is available and does not have an owner.
  99. *
  100. * @param mutex Address of the mutex.
  101. *
  102. * @return N/A
  103. */
  104. #define os_mutex_init(mutex) k_mutex_init(mutex)
  105. /**
  106. * @brief Lock a mutex.
  107. *
  108. * This routine locks @a mutex. If the mutex is locked by another thread,
  109. * the calling thread waits until the mutex becomes available or until
  110. * a timeout occurs.
  111. *
  112. * A thread is permitted to lock a mutex it has already locked. The operation
  113. * completes immediately and the lock count is increased by 1.
  114. *
  115. * @param mutex Address of the mutex.
  116. * @param timeout Waiting period to lock the mutex (in milliseconds),
  117. * or one of the special values K_NO_WAIT and K_FOREVER.
  118. *
  119. * @retval 0 Mutex locked.
  120. * @retval -EBUSY Returned without waiting.
  121. * @retval -EAGAIN Waiting period timed out.
  122. */
  123. //#define os_mutex_lock(mutex,timeout) k_mutex_lock(mutex,timeout)
  124. int os_mutex_lock(os_mutex * mutex, s32_t timeout);
  125. /**
  126. * @brief Unlock a mutex.
  127. *
  128. * This routine unlocks @a mutex. The mutex must already be locked by the
  129. * calling thread.
  130. *
  131. * The mutex cannot be claimed by another thread until it has been unlocked by
  132. * the calling thread as many times as it was previously locked by that
  133. * thread.
  134. *
  135. * @param mutex Address of the mutex.
  136. *
  137. * @return N/A
  138. */
  139. #define os_mutex_unlock(mutex) k_mutex_unlock(mutex)
  140. /**
  141. * @brief Initialize a semaphore.
  142. *
  143. * This routine initializes a semaphore object, prior to its first use.
  144. *
  145. * @param sem Address of the semaphore.
  146. * @param initial_count Initial semaphore count.
  147. * @param limit Maximum permitted semaphore count.
  148. *
  149. * @return N/A
  150. */
  151. /**
  152. * @} end defgroup os_mutex_apis
  153. */
  154. /**
  155. * @defgroup os_sem_apis Os Semaphore APIs
  156. * @ingroup os_common_apis
  157. * @{
  158. */
  159. #define os_sem_init(sem,initial_count,limit) k_sem_init(sem,initial_count,limit)
  160. /**
  161. * @brief Take a semaphore.
  162. *
  163. * This routine takes @a sem.
  164. *
  165. * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
  166. *
  167. * @param sem Address of the semaphore.
  168. * @param timeout Waiting period to take the semaphore (in milliseconds),
  169. * or one of the special values K_NO_WAIT and K_FOREVER.
  170. *
  171. * @note When porting code from the nanokernel legacy API to the new API, be
  172. * careful with the return value of this function. The return value is the
  173. * reverse of the one of nano_sem_take family of APIs: 0 means success, and
  174. * non-zero means failure, while the nano_sem_take family returns 1 for success
  175. * and 0 for failure.
  176. *
  177. * @retval 0 Semaphore taken.
  178. * @retval -EBUSY Returned without waiting.
  179. * @retval -EAGAIN Waiting period timed out.
  180. */
  181. //#define os_sem_take(sem, timeout) k_sem_take(sem, timeout)
  182. int os_sem_take(os_sem *sem, s32_t timeout);
  183. /**
  184. * @brief Give a semaphore.
  185. *
  186. * This routine gives @a sem, unless the semaphore is already at its maximum
  187. * permitted count.
  188. *
  189. * @note Can be called by ISRs.
  190. *
  191. * @param sem Address of the semaphore.
  192. *
  193. * @return N/A
  194. */
  195. #define os_sem_give(sem) k_sem_give(sem)
  196. /**
  197. * @brief Reset a semaphore's count to zero.
  198. *
  199. * This routine sets the count of @a sem to zero.
  200. *
  201. * @param sem Address of the semaphore.
  202. *
  203. * @return N/A
  204. */
  205. #define os_sem_reset(sem) k_sem_reset(sem)
  206. /**
  207. * @brief Get a semaphore's count.
  208. *
  209. * This routine returns the current count of @a sem.
  210. *
  211. * @param sem Address of the semaphore.
  212. *
  213. * @return Current semaphore count.
  214. */
  215. #define os_sem_count_get(sem) k_sem_count_get(sem)
  216. /**
  217. * @} end defgroup os_sem_apis
  218. */
  219. /**
  220. * @defgroup os_threads_apis Threads APIs
  221. * @ingroup os_common_apis
  222. * @{
  223. */
  224. /**
  225. * @brief Put the current thread to sleep.
  226. *
  227. * This routine puts the current thread to sleep for @a duration
  228. * milliseconds.
  229. *
  230. * @param duration Number of milliseconds to sleep.
  231. *
  232. * @return N/A
  233. */
  234. //#define os_sleep(duration) k_sleep(duration)
  235. s32_t os_sleep(int timeout);
  236. /**
  237. * @brief Cause the current thread to busy wait.
  238. *
  239. * This routine causes the current thread to execute a "do nothing" loop for
  240. * @a usec_to_wait microseconds.
  241. *
  242. * @return N/A
  243. */
  244. #define os_delay(usec_to_wait) k_busy_wait(usec_to_wait)
  245. /**
  246. * @brief Yield the current thread.
  247. *
  248. * This routine causes the current thread to yield execution to another
  249. * thread of the same or higher priority. If there are no other ready threads
  250. * of the same or higher priority, the routine returns immediately.
  251. *
  252. * @return N/A
  253. */
  254. #define os_yield() k_yield()
  255. /**
  256. * @brief Lock the scheduler.
  257. *
  258. * This routine prevents the current thread from being preempted by another
  259. * thread by instructing the scheduler to treat it as a cooperative thread.
  260. * If the thread subsequently performs an operation that makes it unready,
  261. * it will be context switched out in the normal manner. When the thread
  262. * again becomes the current thread, its non-preemptible status is maintained.
  263. *
  264. * This routine can be called recursively.
  265. *
  266. * @note os_sched_lock() and os_sched_unlock() should normally be used
  267. * when the operation being performed can be safely interrupted by ISRs.
  268. * However, if the amount of processing involved is very small, better
  269. * performance may be obtained by using irq_lock() and irq_unlock().
  270. *
  271. * @return N/A
  272. */
  273. #define os_sched_lock() k_sched_lock()
  274. /**
  275. * @brief Lock interrupts.
  276. * @def irq_lock()
  277. *
  278. * This routine disables all interrupts on the CPU. It returns an unsigned
  279. * integer "lock-out key", which is an architecture-dependent indicator of
  280. * whether interrupts were locked prior to the call. The lock-out key must be
  281. * passed to irq_unlock() to re-enable interrupts.
  282. *
  283. * @note
  284. * This routine must also serve as a memory barrier to ensure the uniprocessor
  285. * implementation of `k_spinlock_t` is correct.
  286. *
  287. * This routine can be called recursively, as long as the caller keeps track
  288. * of each lock-out key that is generated. Interrupts are re-enabled by
  289. * passing each of the keys to irq_unlock() in the reverse order they were
  290. * acquired. (That is, each call to irq_lock() must be balanced by
  291. * a corresponding call to irq_unlock().)
  292. *
  293. * This routine can only be invoked from supervisor mode. Some architectures
  294. * (for example, ARM) will fail silently if invoked from user mode instead
  295. * of generating an exception.
  296. *
  297. * @note
  298. * This routine can be called by ISRs or by threads. If it is called by a
  299. * thread, the interrupt lock is thread-specific; this means that interrupts
  300. * remain disabled only while the thread is running. If the thread performs an
  301. * operation that allows another thread to run (for example, giving a semaphore
  302. * or sleeping for N milliseconds), the interrupt lock no longer applies and
  303. * interrupts may be re-enabled while other processing occurs. When the thread
  304. * once again becomes the current thread, the kernel re-establishes its
  305. * interrupt lock; this ensures the thread won't be interrupted until it has
  306. * explicitly released the interrupt lock it established.
  307. *
  308. * @warning
  309. * The lock-out key should never be used to manually re-enable interrupts
  310. * or to inspect or manipulate the contents of the CPU's interrupt bits.
  311. *
  312. * @return An architecture-dependent lock-out key representing the
  313. * "interrupt disable state" prior to the call.
  314. */
  315. #define os_irq_lock() irq_lock()
  316. /**
  317. * @brief Unlock interrupts.
  318. * @def irq_unlock()
  319. *
  320. * This routine reverses the effect of a previous call to irq_lock() using
  321. * the associated lock-out key. The caller must call the routine once for
  322. * each time it called irq_lock(), supplying the keys in the reverse order
  323. * they were acquired, before interrupts are enabled.
  324. *
  325. * @note
  326. * This routine must also serve as a memory barrier to ensure the uniprocessor
  327. * implementation of `k_spinlock_t` is correct.
  328. *
  329. * This routine can only be invoked from supervisor mode. Some architectures
  330. * (for example, ARM) will fail silently if invoked from user mode instead
  331. * of generating an exception.
  332. *
  333. * @note Can be called by ISRs.
  334. *
  335. * @param key Lock-out key generated by irq_lock().
  336. *
  337. * @return N/A
  338. */
  339. #define os_irq_unlock(key) irq_unlock(key)
  340. /**
  341. * @brief Unlock the scheduler.
  342. *
  343. * This routine reverses the effect of a previous call to os_sched_lock().
  344. * A thread must call the routine once for each time it called os_sched_lock()
  345. * before the thread becomes preemptible.
  346. *
  347. * @return N/A
  348. */
  349. #define os_sched_unlock() k_sched_unlock()
  350. /**
  351. * @brief Initialize a work item.
  352. *
  353. * This routine initializes a workqueue work item, prior to its first use.
  354. *
  355. * @param work Address of work item.
  356. * @param handler Function to invoke each time work item is processed.
  357. *
  358. * @return N/A
  359. */
  360. #define os_work_init(work, handler) k_work_init(work, handler)
  361. /**
  362. * @brief Submit a work item.
  363. *
  364. * This routine submits work item @a work to be processed by workqueue
  365. * @a work_q. If the work item is already pending in the workqueue's queue
  366. * as a result of an earlier submission, this routine has no effect on the
  367. * work item. If the work item has already been processed, or is currently
  368. * being processed, its work is considered complete and the work item can be
  369. * resubmitted.
  370. *
  371. * @warning
  372. * A submitted work item must not be modified until it has been processed
  373. * by the workqueue.
  374. *
  375. * @note Can be called by ISRs.
  376. *
  377. * @param work_q Address of workqueue.
  378. * @param work Address of work item.
  379. *
  380. * @return N/A
  381. */
  382. #define os_work_submit_to_queue(work_q, work) k_work_submit_to_queue(work_q, work)
  383. /**
  384. * @brief Check if a work item is pending.
  385. *
  386. * This routine indicates if work item @a work is pending in a workqueue's
  387. * queue.
  388. *
  389. * @note Can be called by ISRs.
  390. *
  391. * @param work Address of work item.
  392. *
  393. * @return 1 if work item is pending, or 0 if it is not pending.
  394. */
  395. #define os_work_pending(work) k_work_pending(work)
  396. /**
  397. * @brief Start a workqueue.
  398. *
  399. * This routine starts workqueue @a work_q. The workqueue spawns its work
  400. * processing thread, which runs forever.
  401. *
  402. * @param work_q Address of workqueue.
  403. * @param stack Pointer to work queue thread's stack space.
  404. * @param stack_size Size of the work queue thread's stack (in bytes).
  405. * @param prio Priority of the work queue's thread.
  406. *
  407. * @return N/A
  408. */
  409. #define os_work_q_start(work_q, stack, stack_size, prio) \
  410. k_work_q_start(work_q, stack, stack_size, prio)
  411. /**
  412. * @brief Initialize a delayed work item.
  413. *
  414. * This routine initializes a workqueue delayed work item, prior to
  415. * its first use.
  416. *
  417. * @param work Address of delayed work item.
  418. * @param handler Function to invoke each time work item is processed.
  419. *
  420. * @return N/A
  421. */
  422. #define os_delayed_work_init(work, handler) \
  423. k_delayed_work_init(work, handler)
  424. /**
  425. * @brief Submit a delayed work item.
  426. *
  427. * This routine schedules work item @a work to be processed by workqueue
  428. * @a work_q after a delay of @a delay milliseconds. The routine initiates
  429. * an asychronous countdown for the work item and then returns to the caller.
  430. * Only when the countdown completes is the work item actually submitted to
  431. * the workqueue and becomes pending.
  432. *
  433. * Submitting a previously submitted delayed work item that is still
  434. * counting down cancels the existing submission and restarts the countdown
  435. * using the new delay. If the work item is currently pending on the
  436. * workqueue's queue because the countdown has completed it is too late to
  437. * resubmit the item, and resubmission fails without impacting the work item.
  438. * If the work item has already been processed, or is currently being processed,
  439. * its work is considered complete and the work item can be resubmitted.
  440. *
  441. * @warning
  442. * A delayed work item must not be modified until it has been processed
  443. * by the workqueue.
  444. *
  445. * @note Can be called by ISRs.
  446. *
  447. * @param work_q Address of workqueue.
  448. * @param work Address of delayed work item.
  449. * @param delay Delay before submitting the work item (in milliseconds).
  450. *
  451. * @retval 0 Work item countdown started.
  452. * @retval -EINPROGRESS Work item is already pending.
  453. * @retval -EINVAL Work item is being processed or has completed its work.
  454. * @retval -EADDRINUSE Work item is pending on a different workqueue.
  455. */
  456. int os_delayed_work_submit_to_queue(os_work_q *work_q, os_delayed_work *work, s32_t delay);
  457. /**
  458. * @brief Cancel a delayed work item.
  459. *
  460. * This routine cancels the submission of delayed work item @a work.
  461. * A delayed work item can only be cancelled while its countdown is still
  462. * underway.
  463. *
  464. * @note Can be called by ISRs.
  465. *
  466. * @param work Address of delayed work item.
  467. *
  468. * @retval 0 Work item countdown cancelled.
  469. * @retval -EINPROGRESS Work item is already pending.
  470. * @retval -EINVAL Work item is being processed or has completed its work.
  471. */
  472. #define os_delayed_work_cancel(work) k_delayed_work_cancel(work);
  473. /**
  474. * @brief Submit a work item to the system workqueue.
  475. *
  476. * This routine submits work item @a work to be processed by the system
  477. * workqueue. If the work item is already pending in the workqueue's queue
  478. * as a result of an earlier submission, this routine has no effect on the
  479. * work item. If the work item has already been processed, or is currently
  480. * being processed, its work is considered complete and the work item can be
  481. * resubmitted.
  482. *
  483. * @warning
  484. * Work items submitted to the system workqueue should avoid using handlers
  485. * that block or yield since this may prevent the system workqueue from
  486. * processing other work items in a timely manner.
  487. *
  488. * @note Can be called by ISRs.
  489. *
  490. * @param work Address of work item.
  491. *
  492. * @return N/A
  493. */
  494. #define os_work_submit(work) k_work_submit(work)
  495. /**
  496. * @brief Submit a delayed work item to the system workqueue.
  497. *
  498. * This routine schedules work item @a work to be processed by the system
  499. * workqueue after a delay of @a delay milliseconds. The routine initiates
  500. * an asychronous countdown for the work item and then returns to the caller.
  501. * Only when the countdown completes is the work item actually submitted to
  502. * the workqueue and becomes pending.
  503. *
  504. * Submitting a previously submitted delayed work item that is still
  505. * counting down cancels the existing submission and restarts the countdown
  506. * using the new delay. If the work item is currently pending on the
  507. * workqueue's queue because the countdown has completed it is too late to
  508. * resubmit the item, and resubmission fails without impacting the work item.
  509. * If the work item has already been processed, or is currently being processed,
  510. * its work is considered complete and the work item can be resubmitted.
  511. *
  512. * @warning
  513. * Work items submitted to the system workqueue should avoid using handlers
  514. * that block or yield since this may prevent the system workqueue from
  515. * processing other work items in a timely manner.
  516. *
  517. * @note Can be called by ISRs.
  518. *
  519. * @param work Address of delayed work item.
  520. * @param delay Delay before submitting the work item (in milliseconds).
  521. *
  522. * @retval 0 Work item countdown started.
  523. * @retval -EINPROGRESS Work item is already pending.
  524. * @retval -EINVAL Work item is being processed or has completed its work.
  525. * @retval -EADDRINUSE Work item is pending on a different workqueue.
  526. */
  527. int os_delayed_work_submit(os_delayed_work *work, s32_t delay);
  528. /**
  529. * @brief Get time remaining before a delayed work gets scheduled.
  530. *
  531. * This routine computes the (approximate) time remaining before a
  532. * delayed work gets executed. If the delayed work is not waiting to be
  533. * schedules, it returns zero.
  534. *
  535. * @param work Delayed work item.
  536. *
  537. * @return Remaining time (in milliseconds).
  538. */
  539. static inline int32_t os_delayed_work_remaining_get(os_delayed_work *work)
  540. {
  541. k_ticks_t rem = k_work_delayable_remaining_get(&work->work);
  542. /* Probably should be ceil32, but was floor32 */
  543. return k_ticks_to_ms_floor32(rem);
  544. }
  545. /** @brief Test whether a delayed work item is currently pending.
  546. *
  547. * Wrapper to determine whether a delayed work item is in a non-idle state.
  548. *
  549. * @note This is a live snapshot of state, which may change before the result
  550. * can be inspected. Use locks where appropriate.
  551. *
  552. * @funcprops \isr_ok
  553. *
  554. * @param dwork pointer to the delayable work item.
  555. *
  556. * @return true if and only if k_work_delayable_busy_get() returns a non-zero
  557. * value.
  558. */
  559. #define os_delayed_work_is_pending(work) k_work_delayable_is_pending(&work->work)
  560. /**
  561. * @brief Get a thread's priority.
  562. *
  563. * This routine gets the priority of @a thread.
  564. *
  565. * @param osthread ID of thread whose priority is needed.
  566. *
  567. * @return Priority of @a thread.
  568. */
  569. #define os_thread_priority_get(osthread) k_thread_priority_get(osthread)
  570. /**
  571. * @brief Set a thread's priority.
  572. *
  573. * This routine immediately changes the priority of @a thread.
  574. *
  575. * Rescheduling can occur immediately depending on the priority @a thread is
  576. * set to:
  577. *
  578. * - If its priority is raised above the priority of the caller of this
  579. * function, and the caller is preemptible, @a thread will be scheduled in.
  580. *
  581. * - If the caller operates on itself, it lowers its priority below that of
  582. * other threads in the system, and the caller is preemptible, the thread of
  583. * highest priority will be scheduled in.
  584. *
  585. * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
  586. * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
  587. * highest priority.
  588. *
  589. * @param osthread ID of thread whose priority is to be set.
  590. * @param prio New priority.
  591. *
  592. * @warning Changing the priority of a thread currently involved in mutex
  593. * priority inheritance may result in undefined behavior.
  594. *
  595. * @return N/A
  596. */
  597. #define os_thread_priority_set(osthread,prio) \
  598. k_thread_priority_set(osthread,prio)
  599. const char *os_thread_get_name_by_prio(int prio);
  600. /**
  601. * @brief Suspend a thread.
  602. *
  603. * This routine prevents the kernel scheduler from making @a thread the
  604. * current thread. All other internal operations on @a thread are still
  605. * performed; for example, any timeout it is waiting on keeps ticking,
  606. * kernel objects it is waiting on are still handed to it, etc.
  607. *
  608. * If @a thread is already suspended, the routine has no effect.
  609. *
  610. * @param osthread ID of thread to suspend.
  611. *
  612. * @return N/A
  613. */
  614. #define os_thread_suspend(osthread) k_thread_suspend(osthread)
  615. /**
  616. * @brief Resume a suspended thread.
  617. *
  618. * This routine allows the kernel scheduler to make @a thread the current
  619. * thread, when it is next eligible for that role.
  620. *
  621. * If @a osthread is not currently suspended, the routine has no effect.
  622. *
  623. * @param osthread ID of thread to resume.
  624. *
  625. * @return N/A
  626. */
  627. #define os_thread_resume(osthread) k_thread_resume(osthread)
  628. /**
  629. * @brief Get thread ID of the current thread.
  630. *
  631. * @return ID of current thread.
  632. */
  633. #define os_current_get() k_current_get()
  634. /**
  635. * @brief Cancel thread performing a delayed start.
  636. *
  637. * This routine prevents @a thread from executing if it has not yet started
  638. * execution. The thread must be re-spawned before it will execute.
  639. *
  640. * @param osthread ID of thread to cancel.
  641. *
  642. * @retval 0 osthread spawning cancelled.
  643. * @retval -EINVAL Thread has already started executing.
  644. */
  645. #define os_thread_cancel(osthread) k_thread_cancel(osthread)
  646. /**
  647. * @brief Abort a thread.
  648. *
  649. * This routine permanently stops execution of @a thread. The thread is taken
  650. * off all kernel queues it is part of (i.e. the ready queue, the timeout
  651. * queue, or a kernel object wait queue). However, any kernel resources the
  652. * thread might currently own (such as mutexes or memory blocks) are not
  653. * released. It is the responsibility of the caller of this routine to ensure
  654. * all necessary cleanup is performed.
  655. *
  656. * @param osthread ID of thread to abort.
  657. *
  658. * @return N/A
  659. */
  660. #define os_thread_abort(osthread) k_thread_abort(osthread)
  661. /**
  662. * @brief Set current thread name
  663. *
  664. * Set the name of the thread to be used when @option{CONFIG_THREAD_MONITOR}
  665. * is enabled for tracing and debugging.
  666. *
  667. * @param thread_id Thread to set name, or NULL to set the current thread
  668. * @param value Name string
  669. * @retval 0 on success
  670. * @retval -EFAULT Memory access error with supplied string
  671. * @retval -ENOSYS Thread name configuration option not enabled
  672. * @retval -EINVAL Thread name too long
  673. */
  674. #define os_thread_name_set(tid,name) k_thread_name_set(tid,name)
  675. /**
  676. * @brief Create a thread.
  677. *
  678. * This routine initializes a thread, then schedules it for execution.
  679. *
  680. * The new thread may be scheduled for immediate execution or a delayed start.
  681. * If the newly spawned thread does not have a delayed start the kernel
  682. * scheduler may preempt the current thread to allow the new thread to
  683. * execute.
  684. *
  685. * Thread options are architecture-specific, and can include K_ESSENTIAL,
  686. * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
  687. * them using "|" (the logical OR operator).
  688. *
  689. * Historically, users often would use the beginning of the stack memory region
  690. * to store the struct k_thread data, although corruption will occur if the
  691. * stack overflows this region and stack protection features may not detect this
  692. * situation.
  693. *
  694. * @param new_thread Pointer to uninitialized struct k_thread
  695. * @param stack Pointer to the stack space.
  696. * @param stack_size Stack size in bytes.
  697. * @param entry Thread entry function.
  698. * @param p1 1st entry point parameter.
  699. * @param p2 2nd entry point parameter.
  700. * @param p3 3rd entry point parameter.
  701. * @param prio Thread priority.
  702. * @param options Thread options.
  703. * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
  704. *
  705. * @return ID of new thread.
  706. */
  707. int os_thread_create(char *stack, size_t stack_size,
  708. void (*entry)(void *, void *, void*),
  709. void *p1, void *p2, void *p3,
  710. int prio, uint32_t options, int delay);
  711. /**
  712. * @brief prepare thread terminal.
  713. *
  714. * This routine prepare thread terminal.
  715. *
  716. * @param tid Pointer to terminal struct k_thread
  717. *
  718. * @return 0 ok others failed.
  719. */
  720. int os_thread_prepare_terminal(int tid);
  721. /**
  722. * @brief wait thread terminal.
  723. *
  724. * This routine wait thread terminal.
  725. *
  726. * @param tid Pointer to terminal struct k_thread
  727. *
  728. * @return 0 ok others failed.
  729. */
  730. int os_thread_wait_terminal(int tid);
  731. /**
  732. * @} end defgroup os_threads_apis
  733. */
  734. /**
  735. * @defgroup os_pipe_apis Fipe APIs
  736. * @ingroup os_common_apis
  737. * @{
  738. */
  739. /**
  740. * @brief Initialize a pipe.
  741. *
  742. * This routine initializes a pipe object, prior to its first use.
  743. *
  744. * @param pipe Address of the pipe.
  745. * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
  746. * is used.
  747. * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
  748. * buffer is used.
  749. *
  750. * @return N/A
  751. */
  752. #define os_pipe_init(pipe, buffer, size) k_pipe_init(pipe, buffer, size)
  753. /**
  754. * @brief Write data to a pipe.
  755. *
  756. * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
  757. *
  758. * @param pipe Address of the pipe.
  759. * @param data Address of data to write.
  760. * @param bytes_to_write Size of data (in bytes).
  761. * @param bytes_written Address of area to hold the number of bytes written.
  762. * @param min_xfer Minimum number of bytes to write.
  763. * @param timeout Waiting period to wait for the data to be written (in
  764. * milliseconds), or one of the special values K_NO_WAIT
  765. * and K_FOREVER.
  766. *
  767. * @retval 0 At least @a min_xfer bytes of data were written.
  768. * @retval -EIO Returned without waiting; zero data bytes were written.
  769. * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
  770. * minus one data bytes were written.
  771. */
  772. #define os_pipe_put(pipe, data, bytes_to_write, bytes_written, min_xfer, timeout) \
  773. k_pipe_put(pipe, data, bytes_to_write, bytes_written, min_xfer, timeout)
  774. /**
  775. * @brief Read data from a pipe.
  776. *
  777. * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
  778. *
  779. * @param pipe Address of the pipe.
  780. * @param data Address to place the data read from pipe.
  781. * @param bytes_to_read Maximum number of data bytes to read.
  782. * @param bytes_read Address of area to hold the number of bytes read.
  783. * @param min_xfer Minimum number of data bytes to read.
  784. * @param timeout Waiting period to wait for the data to be read (in
  785. * milliseconds), or one of the special values K_NO_WAIT
  786. * and K_FOREVER.
  787. *
  788. * @retval 0 At least @a min_xfer bytes of data were read.
  789. * @retval -EIO Returned without waiting; zero data bytes were read.
  790. * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
  791. * minus one data bytes were read.
  792. */
  793. #define os_pipe_get(pipe, data, bytes_to_read, bytes_read, min_xfer, timeout) \
  794. k_pipe_get(pipe, data, bytes_to_read, bytes_read, min_xfer, timeout)
  795. /**
  796. * @brief Write memory block to a pipe.
  797. *
  798. * This routine writes the data contained in a memory block to @a pipe.
  799. * Once all of the data in the block has been written to the pipe, it will
  800. * free the memory block @a block and give the semaphore @a sem (if specified).
  801. *
  802. * @param pipe Address of the pipe.
  803. * @param block Memory block containing data to send
  804. * @param size Number of data bytes in memory block to send
  805. * @param sem Semaphore to signal upon completion (else NULL)
  806. *
  807. * @return N/A
  808. */
  809. #define os_pipe_block_put(pipe, block, size, sem) \
  810. k_pipe_block_put(pipe, block, size, sem)
  811. /**
  812. * @} end defgroup os_pipe_apis
  813. */
  814. /**
  815. * @defgroup os_fifo_apis Fifo APIs
  816. * @ingroup os_common_apis
  817. * @{
  818. */
  819. /**
  820. * @brief Initialize a fifo.
  821. *
  822. * This routine initializes a fifo object, prior to its first use.
  823. *
  824. * @param fifo Address of the fifo.
  825. *
  826. * @return N/A
  827. */
  828. #define os_fifo_init(fifo) k_fifo_init(fifo)
  829. /**
  830. * @brief Add an element to a fifo.
  831. *
  832. * This routine adds a data item to @a fifo. A fifo data item must be
  833. * aligned on a 4-byte boundary, and the first 32 bits of the item are
  834. * reserved for the kernel's use.
  835. *
  836. * @note Can be called by ISRs.
  837. *
  838. * @param fifo Address of the fifo.
  839. * @param data Address of the data item.
  840. *
  841. * @return N/A
  842. */
  843. #define os_fifo_put(fifo, data) k_fifo_put(fifo, data)
  844. /**
  845. * @brief Calculate element in a fifo.
  846. */
  847. #define os_fifo_cnt_sum(fifo) k_fifo_cnt_sum(fifo)
  848. /**
  849. * @brief Get an element from a fifo.
  850. *
  851. * This routine removes a data item from @a fifo in a "first in, first out"
  852. * manner. The first 32 bits of the data item are reserved for the kernel's use.
  853. *
  854. * @note Can be called by ISRs, but @a timeout must be set to OS_NO_WAIT.
  855. *
  856. * @param fifo Address of the fifo.
  857. * @param timeout Waiting period to obtain a data item (in milliseconds),
  858. * or one of the special values OS_NO_WAIT and OS_FOREVER.
  859. *
  860. * @return Address of the data item if successful; NULL if returned
  861. * without waiting, or waiting period timed out.
  862. */
  863. //#define os_fifo_get(fifo, timeout) k_fifo_get(fifo, timeout)
  864. void * os_fifo_get(os_fifo *fifo, int32_t timeout);
  865. /**
  866. * @brief Query a fifo to see if it has data available.
  867. *
  868. * Note that the data might be already gone by the time this function returns
  869. * if other threads is also trying to read from the fifo.
  870. *
  871. * @note Can be called by ISRs.
  872. *
  873. * @param fifo Address of the fifo.
  874. *
  875. * @return Non-zero if the fifo is empty.
  876. * @return 0 if data is available.
  877. */
  878. #define os_fifo_is_empty(fifo) k_fifo_is_empty(fifo)
  879. /**
  880. * @} end defgroup os_fifo_apis
  881. */
  882. /**
  883. * @defgroup os_mailbox_apis Mailbox APIs
  884. * @ingroup os_common_apis
  885. * @{
  886. */
  887. /**
  888. * @brief Initialize a mailbox.
  889. *
  890. * This routine initializes a mailbox object, prior to its first use.
  891. *
  892. * @param mbox Address of the mailbox.
  893. *
  894. * @return N/A
  895. */
  896. #define os_mbox_init(mbox) k_mbox_init(mbox)
  897. /**
  898. * @brief Send a mailbox message in a synchronous manner.
  899. *
  900. * This routine sends a message to @a mbox and waits for a receiver to both
  901. * receive and process it. The message data may be in a buffer, in a memory
  902. * pool block, or non-existent (i.e. an empty message).
  903. *
  904. * @param mbox Address of the mailbox.
  905. * @param tx_msg Address of the transmit message descriptor.
  906. * @param timeout Waiting period for the message to be received (in
  907. * milliseconds), or one of the special values OS_NO_WAIT
  908. * and OS_FOREVER. Once the message has been received,
  909. * this routine waits as long as necessary for the message
  910. * to be completely processed.
  911. *
  912. * @retval 0 Message sent.
  913. * @retval -ENOMSG Returned without waiting.
  914. * @retval -EAGAIN Waiting period timed out.
  915. */
  916. #define os_mbox_put(mbox, tx_msg, timeout) k_mbox_put(mbox, tx_msg, timeout)
  917. /**
  918. * @brief Send a mailbox message in an asynchronous manner.
  919. *
  920. * This routine sends a message to @a mbox without waiting for a receiver
  921. * to process it. The message data may be in a buffer, in a memory pool block,
  922. * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
  923. * will be given when the message has been both received and completely
  924. * processed by the receiver.
  925. *
  926. * @param mbox Address of the mailbox.
  927. * @param tx_msg Address of the transmit message descriptor.
  928. * @param sem Address of a semaphore, or NULL if none is needed.
  929. *
  930. * @return N/A
  931. */
  932. #define os_mbox_async_put(mbox, tx_msg, sem) k_mbox_async_put(mbox, tx_msg, sem)
  933. /**
  934. * @brief Receive a mailbox message.
  935. *
  936. * This routine receives a message from @a mbox, then optionally retrieves
  937. * its data and disposes of the message.
  938. *
  939. * @param mbox Address of the mailbox.
  940. * @param rx_msg Address of the receive message descriptor.
  941. * @param buffer Address of the buffer to receive data, or NULL to defer data
  942. * retrieval and message disposal until later.
  943. * @param timeout Waiting period for a message to be received (in
  944. * milliseconds), or one of the special values OS_NO_WAIT
  945. * and OS_FOREVER.
  946. *
  947. * @retval 0 Message received.
  948. * @retval -ENOMSG Returned without waiting.
  949. * @retval -EAGAIN Waiting period timed out.
  950. */
  951. #define os_mbox_get(mbox, rx_msg, buffer, timeout) \
  952. k_mbox_get(mbox, rx_msg, buffer, timeout)
  953. /**
  954. * @brief clear all message in mbox.
  955. *
  956. * This routine clear all message in mbox , if async message is send to many than
  957. * max async message num ,drop all message send before.
  958. *
  959. * @param mbox Address of the mailbox.
  960. *
  961. * @retval void
  962. */
  963. #define os_mbox_clear_msg(mbox) \
  964. k_mbox_clear_msg(mbox)
  965. /**
  966. * @} end defgroup os_mailbox_apis
  967. */
  968. /**
  969. * @defgroup os_msgq_apis Msgq APIs
  970. * @ingroup os_common_apis
  971. * @{
  972. */
  973. /**
  974. * @brief Initialize a message queue.
  975. *
  976. * This routine initializes a message queue object, prior to its first use.
  977. *
  978. * The message queue's ring buffer must contain space for @a max_msgs messages,
  979. * each of which is @a msg_size bytes long. The buffer must be aligned to an
  980. * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
  981. * that each message is similarly aligned to this boundary, @a q_msg_size
  982. * must also be a multiple of N.
  983. *
  984. * @param q Address of the message queue.
  985. * @param buffer Pointer to ring buffer that holds queued messages.
  986. * @param msg_size Message size (in bytes).
  987. * @param max_msgs Maximum number of messages that can be queued.
  988. *
  989. * @return N/A
  990. */
  991. static inline void os_msgq_init(os_msgq *q, char *buffer, size_t msg_size,
  992. uint32_t max_msgs)
  993. {
  994. k_msgq_init(q, buffer, msg_size, max_msgs);
  995. }
  996. /**
  997. * @brief Release allocated buffer for a queue
  998. *
  999. * Releases memory allocated for the ring buffer.
  1000. *
  1001. * @param msgq message queue to cleanup
  1002. *
  1003. * @retval 0 on success
  1004. * @retval -EBUSY Queue not empty
  1005. */
  1006. static inline int os_msgq_cleanup(os_msgq *msgq)
  1007. {
  1008. return k_msgq_cleanup(msgq);
  1009. }
  1010. /**
  1011. * @brief Send a message to a message queue.
  1012. *
  1013. * This routine sends a message to message queue @a q.
  1014. *
  1015. * @note Can be called by ISRs.
  1016. * @note The message content is copied from @a data into @a msgq and the @a data
  1017. * pointer is not retained, so the message content will not be modified
  1018. * by this function.
  1019. *
  1020. * @param msgq Address of the message queue.
  1021. * @param data Pointer to the message.
  1022. * @param timeout Non-negative waiting period to add the message (in milliseconds),
  1023. * or one of the special values OS_NO_WAIT and
  1024. * OS_FOREVER.
  1025. *
  1026. * @retval 0 Message sent.
  1027. * @retval -ENOMSG Returned without waiting or queue purged.
  1028. * @retval -EAGAIN Waiting period timed out.
  1029. */
  1030. int os_msgq_put(os_msgq *msgq, const void *data, int32_t timeout);
  1031. /**
  1032. * @brief Receive a message from a message queue.
  1033. *
  1034. * This routine receives a message from message queue @a q in a "first in,
  1035. * first out" manner.
  1036. *
  1037. * @note Can be called by ISRs, but @a timeout must be set to OS_NO_WAIT.
  1038. *
  1039. * @param msgq Address of the message queue.
  1040. * @param data Address of area to hold the received message.
  1041. * @param timeout Waiting period to receive the message (in milliseconds),
  1042. * or one of the special values OS_NO_WAIT and
  1043. * OS_FOREVER.
  1044. *
  1045. * @retval 0 Message received.
  1046. * @retval -ENOMSG Returned without waiting.
  1047. * @retval -EAGAIN Waiting period timed out.
  1048. */
  1049. int os_msgq_get(os_msgq *msgq, void *data, int32_t timeout);
  1050. /**
  1051. * @brief Peek/read a message from a message queue.
  1052. *
  1053. * This routine reads a message from message queue @a q in a "first in,
  1054. * first out" manner and leaves the message in the queue.
  1055. *
  1056. * @note Can be called by ISRs.
  1057. *
  1058. * @param msgq Address of the message queue.
  1059. * @param data Address of area to hold the message read from the queue.
  1060. *
  1061. * @retval 0 Message read.
  1062. * @retval -ENOMSG Returned when the queue has no message.
  1063. */
  1064. static inline int os_msgq_peek(os_msgq *msgq, void *data)
  1065. {
  1066. return k_msgq_peek(msgq, data);
  1067. }
  1068. /**
  1069. * @brief Purge a message queue.
  1070. *
  1071. * This routine discards all unreceived messages in a message queue's ring
  1072. * buffer. Any threads that are blocked waiting to send a message to the
  1073. * message queue are unblocked and see an -ENOMSG error code.
  1074. *
  1075. * @param msgq Address of the message queue.
  1076. *
  1077. * @return N/A
  1078. */
  1079. static inline void os_msgq_purge(os_msgq *msgq)
  1080. {
  1081. k_msgq_purge(msgq);
  1082. }
  1083. /**
  1084. * @brief Get the amount of free space in a message queue.
  1085. *
  1086. * This routine returns the number of unused entries in a message queue's
  1087. * ring buffer.
  1088. *
  1089. * @param msgq Address of the message queue.
  1090. *
  1091. * @return Number of unused ring buffer entries.
  1092. */
  1093. static inline uint32_t os_msgq_num_free_get(os_msgq *msgq)
  1094. {
  1095. return k_msgq_num_free_get(msgq);
  1096. }
  1097. /**
  1098. * @brief Get the number of messages in a message queue.
  1099. *
  1100. * This routine returns the number of messages in a message queue's ring buffer.
  1101. *
  1102. * @param msgq Address of the message queue.
  1103. *
  1104. * @return Number of messages.
  1105. */
  1106. static inline uint32_t os_msgq_num_used_get(os_msgq *msgq)
  1107. {
  1108. return k_msgq_num_used_get(msgq);
  1109. }
  1110. /**
  1111. * @} end defgroup os_msgq_apis
  1112. */
  1113. /**
  1114. * @defgroup os_timer_apis Timer APIs
  1115. * @ingroup os_common_apis
  1116. * @{
  1117. */
  1118. /**
  1119. * @brief Initialize a timer.
  1120. *
  1121. * This routine initializes a timer, prior to its first use.
  1122. *
  1123. * @param timer Address of timer.
  1124. * @param expiry_fn Function to invoke each time the timer expires.
  1125. * @param stop_fn Function to invoke if the timer is stopped while running.
  1126. *
  1127. * @return N/A
  1128. */
  1129. #define os_timer_init(timer, expiry_fn, stop_fn) \
  1130. k_timer_init(timer, expiry_fn, stop_fn)
  1131. /**
  1132. * @brief Start a timer.
  1133. *
  1134. * This routine starts a timer, and resets its status to zero. The timer
  1135. * begins counting down using the specified duration and period values.
  1136. *
  1137. * Attempting to start a timer that is already running is permitted.
  1138. * The timer's status is reset to zero and the timer begins counting down
  1139. * using the new duration and period values.
  1140. *
  1141. * @param timer Address of timer.
  1142. * @param duration Initial timer duration (in milliseconds).
  1143. * @param period Timer period (in milliseconds).
  1144. *
  1145. * @return N/A
  1146. */
  1147. #define os_timer_start(timer, duration, period) \
  1148. k_timer_start(timer, duration, period)
  1149. /**
  1150. * @brief Stop a timer.
  1151. *
  1152. * This routine stops a running timer prematurely. The timer's stop function,
  1153. * if one exists, is invoked by the caller.
  1154. *
  1155. * Attempting to stop a timer that is not running is permitted, but has no
  1156. * effect on the timer.
  1157. *
  1158. * @note Can be called by ISRs. The stop handler has to be callable from ISRs
  1159. * if @a os_timer_stop is to be called from ISRs.
  1160. *
  1161. * @param timer Address of timer.
  1162. *
  1163. * @return N/A
  1164. */
  1165. #define os_timer_stop(timer) k_timer_stop(timer)
  1166. /**
  1167. * @brief Get system uptime.
  1168. *
  1169. * This routine returns the elapsed time since the system booted,
  1170. * in milliseconds.
  1171. *
  1172. * @return Current uptime.
  1173. */
  1174. #define os_uptime_get() k_uptime_get()
  1175. /**
  1176. * @brief Get system uptime (32-bit version).
  1177. *
  1178. * This routine returns the lower 32-bits of the elapsed time since the system
  1179. * booted, in milliseconds.
  1180. *
  1181. * This routine can be more efficient than k_uptime_get(), as it reduces the
  1182. * need for interrupt locking and 64-bit math. However, the 32-bit result
  1183. * cannot hold a system uptime time larger than approximately 50 days, so the
  1184. * caller must handle possible rollovers.
  1185. *
  1186. * @return Current uptime.
  1187. */
  1188. #define os_uptime_get_32() k_uptime_get_32()
  1189. /**
  1190. * @brief Get elapsed time.
  1191. *
  1192. * This routine computes the elapsed time between the current system uptime
  1193. * and an earlier reference time, in milliseconds.
  1194. *
  1195. * @param reftime Pointer to a reference time, which is updated to the current
  1196. * uptime upon return.
  1197. *
  1198. * @return Elapsed time.
  1199. */
  1200. #define os_uptime_delta(reftime) k_uptime_delta(reftime)
  1201. /**
  1202. * @brief Get elapsed time (32-bit version).
  1203. *
  1204. * This routine computes the elapsed time between the current system uptime
  1205. * and an earlier reference time, in milliseconds.
  1206. *
  1207. * This routine can be more efficient than k_uptime_delta(), as it reduces the
  1208. * need for interrupt locking and 64-bit math. However, the 32-bit result
  1209. * cannot hold an elapsed time larger than approximately 50 days, so the
  1210. * caller must handle possible rollovers.
  1211. *
  1212. * @param reftime Pointer to a reference time, which is updated to the current
  1213. * uptime upon return.
  1214. *
  1215. * @return Elapsed time.
  1216. */
  1217. #define os_uptime_delta_32(reftime) k_uptime_delta_32(reftime)
  1218. /**
  1219. * @brief Read the hardware clock.
  1220. *
  1221. * This routine returns the current time, as measured by the system's hardware
  1222. * clock.
  1223. *
  1224. * @return Current hardware clock up-counter (in cycles).
  1225. */
  1226. #define os_cycle_get_32() k_cycle_get_32()
  1227. /** @brief Convert hardware cycles to milliseconds
  1228. *
  1229. * Converts time values in hardware cycles to milliseconds.
  1230. * Computes result in 32 bit precision.
  1231. * Rounds to the nearest output unit.
  1232. *
  1233. * @return The converted time value
  1234. */
  1235. #define os_cyc_to_ms_near32(t) k_cyc_to_ms_near32(t)
  1236. #define os_clock_tick_get() sys_clock_tick_get()
  1237. #define os_clock_timeout_end_calc(timeout) sys_clock_timeout_end_calc(timeout)
  1238. /**
  1239. * @brief Writes an ERROR level message to the log.
  1240. *
  1241. * @details It's meant to report severe errors, such as those from which it's
  1242. * not possible to recover.
  1243. *
  1244. * @param ... A string optionally containing printk valid conversion specifier,
  1245. * followed by as many values as specifiers.
  1246. */
  1247. #ifndef CONFIG_ACTLOG
  1248. #ifdef CONFIG_LOG
  1249. #define SYS_LOG_ERR(...) \
  1250. do { \
  1251. if (LOG_PRINT_FUNC_NAME) \
  1252. printk("%s ", __func__); \
  1253. Z_LOG(LOG_LEVEL_ERR, __VA_ARGS__); \
  1254. printk("\n"); \
  1255. } while (0)
  1256. #else
  1257. #define SYS_LOG_ERR(...)
  1258. #endif
  1259. #else
  1260. #define SYS_LOG_ERR(...) ACT_LOG(LOG_LEVEL_ERR,__VA_ARGS__)
  1261. #endif
  1262. /**
  1263. * @brief Writes a WARNING level message to the log.
  1264. *
  1265. * @details It's meant to register messages related to unusual situations that
  1266. * are not necessarily errors.
  1267. *
  1268. * @param ... A string optionally containing printk valid conversion specifier,
  1269. * followed by as many values as specifiers.
  1270. */
  1271. #ifndef CONFIG_ACTLOG
  1272. #ifdef CONFIG_LOG
  1273. #define SYS_LOG_WRN(...) \
  1274. do { \
  1275. if (LOG_PRINT_FUNC_NAME) \
  1276. printk("%s ", __func__); \
  1277. Z_LOG(LOG_LEVEL_WRN, __VA_ARGS__); \
  1278. printk("\n"); \
  1279. } while (0)
  1280. #else
  1281. #define SYS_LOG_WRN(...)
  1282. #endif
  1283. #else
  1284. #define SYS_LOG_WRN(...) ACT_LOG(LOG_LEVEL_WRN,__VA_ARGS__)
  1285. #endif
  1286. /**
  1287. * @brief Writes an INFO level message to the log.
  1288. *
  1289. * @details It's meant to write generic user oriented messages.
  1290. *
  1291. * @param ... A string optionally containing printk valid conversion specifier,
  1292. * followed by as many values as specifiers.
  1293. */
  1294. #ifndef CONFIG_ACTLOG
  1295. #ifdef CONFIG_LOG
  1296. #define SYS_LOG_INF(...) \
  1297. do { \
  1298. if (LOG_PRINT_FUNC_NAME) \
  1299. printk("%s ", __func__); \
  1300. Z_LOG(LOG_LEVEL_INF, __VA_ARGS__); \
  1301. printk("\n"); \
  1302. } while (0)
  1303. #else
  1304. #define SYS_LOG_INF(...)
  1305. #endif
  1306. #else
  1307. #define SYS_LOG_INF(...) ACT_LOG(LOG_LEVEL_INF,__VA_ARGS__)
  1308. #endif
  1309. /**
  1310. * @brief Writes a DEBUG level message to the log.
  1311. *
  1312. * @details It's meant to write developer oriented information.
  1313. *
  1314. * @param ... A string optionally containing printk valid conversion specifier,
  1315. * followed by as many values as specifiers.
  1316. */
  1317. #ifndef CONFIG_ACTLOG
  1318. #ifdef CONFIG_LOG
  1319. #define SYS_LOG_DBG(...) \
  1320. do { \
  1321. if (LOG_PRINT_FUNC_NAME) \
  1322. Z_LOG(LOG_LEVEL_DBG, __VA_ARGS__); \
  1323. } while (0)
  1324. #else
  1325. #define SYS_LOG_DBG(...)
  1326. #endif
  1327. #else
  1328. #define SYS_LOG_DBG(...) ACT_LOG(LOG_LEVEL_DBG, __VA_ARGS__)
  1329. #endif
  1330. /**
  1331. * @brief Output a string
  1332. *
  1333. * Output a string on output installed by platform at init time. Some
  1334. * printf-like formatting is available.
  1335. *
  1336. * Available formatting:
  1337. * - %x/%X: outputs a number in hexadecimal format
  1338. * - %s: outputs a null-terminated string
  1339. * - %p: pointer, same as %x with a 0x prefix
  1340. * - %u: outputs a number in unsigned decimal format
  1341. * - %d/%i: outputs a number in signed decimal format
  1342. *
  1343. * Field width (with or without leading zeroes) is supported.
  1344. * Length attributes h, hh, l, ll and z are supported. However, integral
  1345. * values with %lld and %lli are only printed if they fit in a long
  1346. * otherwise 'ERR' is printed. Full 64-bit values may be printed with %llx.
  1347. *
  1348. * @param fmt formatted string to output
  1349. *
  1350. * @return N/A
  1351. */
  1352. void os_printk(const char *fmt, ...);
  1353. /**
  1354. * @} end defgroup os_timer_apis
  1355. */
  1356. /**
  1357. * @defgroup os_msg_apis message APIs
  1358. * @ingroup os_common_apis
  1359. * @{
  1360. */
  1361. int system_check_low_latencey_mode(void);
  1362. void system_set_low_latencey_mode(bool low_latencey);
  1363. /* equal to sizeof(struct app_msg) */
  1364. #define MSG_MEM_SIZE (5 * 4)
  1365. int msg_pool_get_free_msg_num(void);
  1366. void msg_pool_dump(void(*dump_fn)(os_tid_t sender, os_tid_t receiver,
  1367. const char *content, int max_size));
  1368. int os_send_sync_msg(void *receiver, void *msg, int msg_size);
  1369. int os_send_async_msg_discardable(void *receiver, void *msg, int msg_size);
  1370. int os_send_async_msg(void *receiver, void *msg, int msg_size);
  1371. int os_receive_msg(void *msg, int msg_size,int timeout);
  1372. int os_poll_msg(void *msg, int msg_size, os_sem *sem, int timeout);
  1373. void os_msg_clean(void);
  1374. void os_msg_init(void);
  1375. bool os_is_in_isr(void);
  1376. os_work_q *os_get_user_work_queue(void);
  1377. os_work_q *os_get_display_work_queue(void);
  1378. /**
  1379. * @} end defgroup os_msg_apis
  1380. */
  1381. /**
  1382. * @defgroup os_tracing_apis tracing APIs
  1383. * @ingroup os_common_apis
  1384. * @{
  1385. */
  1386. #define os_strace_void(id) \
  1387. sys_trace_void(id)
  1388. #define os_strace_end_call(id) \
  1389. sys_trace_end_call(id)
  1390. #define os_strace_end_call_u32(id, retv) \
  1391. sys_trace_end_call_u32(id, retv)
  1392. #define os_strace_u32(id, p1) \
  1393. sys_trace_u32(id, p1)
  1394. #define os_strace_u32x2(id, p1, p2) \
  1395. sys_trace_u32x2(id, p1, p2)
  1396. #define os_strace_u32x3(id, p1, p2, p3) \
  1397. sys_trace_u32x3(id, p1, p2, p3)
  1398. #define os_strace_u32x4(id, p1, p2, p3, p4) \
  1399. sys_trace_u32x4(id, p1, p2, p3, p4)
  1400. #define os_strace_u32x5(id, p1, p2, p3, p4, p5) \
  1401. sys_trace_u32x5(id, p1, p2, p3, p4, p5)
  1402. #define os_strace_u32x6(id, p1, p2, p3, p4, p5, p6) \
  1403. sys_trace_u32x6(id, p1, p2, p3, p4, p5, p6)
  1404. #define os_strace_u32x7(id, p1, p2, p3, p4, p5, p6, p7) \
  1405. sys_trace_u32x7(id, p1, p2, p3, p4, p5, p6, p7)
  1406. #define os_strace_u32x8(id, p1, p2, p3, p4, p5, p6, p7, p8) \
  1407. sys_trace_u32x8(id, p1, p2, p3, p4, p5, p6, p7, p8)
  1408. #define os_strace_u32x9(id, p1, p2, p3, p4, p5, p6, p7, p8, p9) \
  1409. sys_trace_u32x9(id, p1, p2, p3, p4, p5, p6, p7, p8, p9)
  1410. #define os_strace_u32x10(id, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10) \
  1411. sys_trace_u32x10(id, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)
  1412. #define os_strace_string(id, string) \
  1413. sys_trace_string(id, string)
  1414. #define os_strace_string_u32x5(id, string, p1, p2, p3, p4, p5) \
  1415. sys_trace_string_u32x5(id, string, p1, p2, p3, p4, p5)
  1416. #define min(a, b) ((a) < (b)) ? (a) : (b)
  1417. /**
  1418. * @} end defgroup os_tracing_apis
  1419. */
  1420. #ifdef __cplusplus
  1421. }
  1422. #endif
  1423. #endif /* __OS_COMMON_API_H__ */