os_common_api.h 46 KB

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