rpmsg_env_rt-thread.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /*
  2. * Copyright (c) 2014, Mentor Graphics Corporation
  3. * Copyright (c) 2015 Xilinx, Inc.
  4. * Copyright (c) 2016 Freescale Semiconductor, Inc.
  5. * Copyright 2016-2023 NXP
  6. * Copyright 2021 ACRIOS Systems s.r.o.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holder nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. /**************************************************************************
  34. * FILE NAME
  35. *
  36. * rpmsg_env_freertos.c
  37. *
  38. *
  39. * DESCRIPTION
  40. *
  41. * This file is FreeRTOS Implementation of env layer for OpenAMP.
  42. *
  43. *
  44. **************************************************************************/
  45. #include "rtthread.h"
  46. #include "rthw.h"
  47. #define DBG_TAG "rpmsg.env"
  48. #define DBG_LVL DBG_WARNING
  49. #include <rtdbg.h>
  50. #include "rpmsg_compiler.h"
  51. #include "rpmsg_env.h"
  52. #include "rpmsg_platform.h"
  53. #include "virtqueue.h"
  54. #include "rpmsg_lite.h"
  55. #include <stdlib.h>
  56. #include <string.h>
  57. static int32_t env_init_counter = 0;
  58. static rt_sem_t env_sema = ((void *)0);
  59. static rt_event_t event_group = ((void *)0);
  60. #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
  61. struct rt_semaphore env_sem_static_context;
  62. struct rt_event event_group_static_context;
  63. #endif
  64. /* RL_ENV_MAX_MUTEX_COUNT is an arbitrary count greater than 'count'
  65. if the inital count is 1, this function behaves as a mutex
  66. if it is greater than 1, it acts as a "resource allocator" with
  67. the maximum of 'count' resources available.
  68. Currently, only the first use-case is applicable/applied in RPMsg-Lite.
  69. */
  70. #define RL_ENV_MAX_MUTEX_COUNT (1)
  71. /* Max supported ISR counts */
  72. #define ISR_COUNT (32U)
  73. /*!
  74. * Structure to keep track of registered ISR's.
  75. */
  76. struct isr_info
  77. {
  78. void *data;
  79. };
  80. static struct isr_info isr_table[ISR_COUNT];
  81. #if defined(RL_USE_ENVIRONMENT_CONTEXT) && (RL_USE_ENVIRONMENT_CONTEXT == 1)
  82. #error "This RPMsg-Lite port requires RL_USE_ENVIRONMENT_CONTEXT set to 0"
  83. #endif
  84. #if defined(AARCH64)
  85. static int32_t os_in_isr(void)
  86. {
  87. return 0;
  88. }
  89. #endif
  90. /*!
  91. * env_in_isr
  92. *
  93. * @returns - true, if currently in ISR
  94. *
  95. */
  96. static int32_t env_in_isr(void)
  97. {
  98. #if defined(AARCH64)
  99. return os_in_isr();
  100. #else
  101. return platform_in_isr();
  102. #endif
  103. return 0;
  104. }
  105. /*!
  106. * env_wait_for_link_up
  107. *
  108. * Wait until the link_state parameter of the rpmsg_lite_instance is set.
  109. * Utilize events to avoid busy loop implementation.
  110. *
  111. */
  112. uint32_t env_wait_for_link_up(volatile uint32_t *link_state, uint32_t link_id, uint32_t timeout_ms)
  113. {
  114. rt_uint32_t recved;
  115. rt_int32_t timeout;
  116. if (*link_state != 1U)
  117. {
  118. if (timeout_ms == RL_BLOCK)
  119. timeout = RT_WAITING_FOREVER;
  120. else
  121. timeout = rt_tick_from_millisecond(timeout_ms);
  122. if (rt_event_recv(event_group, 1 << link_id, RT_EVENT_FLAG_CLEAR | RT_EVENT_FLAG_OR, timeout, &recved) == RT_EOK)
  123. {
  124. return 1U;
  125. }
  126. else
  127. {
  128. LOG_E("rt_event_recv failed...");
  129. return 0U;
  130. }
  131. }
  132. else
  133. {
  134. return 1U;
  135. }
  136. }
  137. /*!
  138. * env_tx_callback
  139. *
  140. * Set event to notify task waiting in env_wait_for_link_up().
  141. *
  142. */
  143. void env_tx_callback(uint32_t link_id)
  144. {
  145. rt_event_send(event_group, 1UL << link_id);
  146. }
  147. /*!
  148. * env_init
  149. *
  150. * Initializes OS/BM environment.
  151. *
  152. */
  153. int32_t env_init(void)
  154. {
  155. int32_t retval;
  156. rt_base_t level;
  157. level = rt_hw_interrupt_disable(); /* stop scheduler */
  158. /* verify 'env_init_counter' */
  159. RL_ASSERT(env_init_counter >= 0);
  160. if (env_init_counter < 0)
  161. {
  162. /* coco begin validated: (env_init_counter < 0) condition will never met unless RAM is corrupted */
  163. rt_hw_interrupt_enable(level); /* re-enable scheduler */
  164. return -1;
  165. /* coco end */
  166. }
  167. env_init_counter++;
  168. /* multiple call of 'env_init' - return ok */
  169. if (env_init_counter == 1)
  170. {
  171. /* first call */
  172. #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
  173. if (rt_sem_init(&env_sem_static_context, "rl_sem", 0, RT_IPC_FLAG_FIFO) != RT_EOK)
  174. {
  175. LOG_E("rt_sem_init failed ...");
  176. rt_hw_interrupt_enable(level);
  177. return -1;
  178. }
  179. env_sema = &env_sem_static_context;
  180. if (rt_event_init(&event_group_static_context, "rl_event", RT_IPC_FLAG_FIFO) != EOK)
  181. {
  182. LOG_E("rt_event_init failed ...");
  183. rt_sem_detach(&env_sem_static_context);
  184. rt_hw_interrupt_enable(level);
  185. return -1;
  186. }
  187. event_group = &event_group_static_context;
  188. #else
  189. env_sema = rt_sem_create("rl_sem", 0, RT_IPC_FLAG_FIFO);
  190. if (env_sema == RT_NULL)
  191. {
  192. rt_hw_interrupt_enable(level);
  193. return -1;
  194. }
  195. event_group = rt_event_create("rl_event", RT_IPC_FLAG_FIFO);
  196. if (event_group == RT_NULL)
  197. {
  198. rt_sem_delete(env_sema);
  199. rt_hw_interrupt_enable(level);
  200. return -1;
  201. }
  202. #endif
  203. (void)memset(isr_table, 0, sizeof(isr_table));
  204. rt_hw_interrupt_enable(level);
  205. retval = platform_init();
  206. return retval;
  207. }
  208. else
  209. {
  210. rt_hw_interrupt_enable(level);
  211. /* Get the semaphore and then return it,
  212. * this allows for platform_init() to block
  213. * if needed and other tasks to wait for the
  214. * blocking to be done.
  215. * This is in ENV layer as this is ENV specific.*/
  216. if (rt_sem_take(env_sema, RT_WAITING_FOREVER) == RT_EOK)
  217. {
  218. rt_sem_release(env_sema);
  219. }
  220. return 0;
  221. }
  222. }
  223. /*!
  224. * env_deinit
  225. *
  226. * Uninitializes OS/BM environment.
  227. *
  228. * @returns - execution status
  229. */
  230. int32_t env_deinit(void)
  231. {
  232. int32_t retval;
  233. rt_base_t level;
  234. level = rt_hw_interrupt_disable(); /* stop scheduler */
  235. /* verify 'env_init_counter' */
  236. RL_ASSERT(env_init_counter > 0);
  237. if (env_init_counter <= 0)
  238. {
  239. rt_hw_interrupt_enable(level); /* re-enable scheduler */
  240. return -1;
  241. }
  242. /* counter on zero - call platform deinit */
  243. env_init_counter--;
  244. /* multiple call of 'env_deinit' - return ok */
  245. if (env_init_counter <= 0)
  246. {
  247. /* last call */
  248. (void)memset(isr_table, 0, sizeof(isr_table));
  249. retval = platform_deinit();
  250. rt_event_delete(event_group);
  251. event_group = ((void *)0);
  252. rt_sem_delete(env_sema);
  253. env_sema = ((void *)0);
  254. rt_hw_interrupt_enable(level);
  255. return retval;
  256. }
  257. else
  258. {
  259. rt_hw_interrupt_enable(level);
  260. return 0;
  261. }
  262. }
  263. #if !(defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1))
  264. /*!
  265. * env_allocate_memory - implementation
  266. *
  267. * @param size
  268. */
  269. void *env_allocate_memory(uint32_t size)
  270. {
  271. return (rt_malloc(size));
  272. }
  273. /*!
  274. * env_free_memory - implementation
  275. *
  276. * @param ptr
  277. */
  278. void env_free_memory(void *ptr)
  279. {
  280. if (ptr != ((void *)0))
  281. {
  282. rt_free(ptr);
  283. }
  284. }
  285. #endif
  286. /*!
  287. *
  288. * env_memset - implementation
  289. *
  290. * @param ptr
  291. * @param value
  292. * @param size
  293. */
  294. void env_memset(void *ptr, int32_t value, uint32_t size)
  295. {
  296. (void)memset(ptr, value, size);
  297. }
  298. /*!
  299. *
  300. * env_memcpy - implementation
  301. *
  302. * @param dst
  303. * @param src
  304. * @param len
  305. */
  306. void env_memcpy(void *dst, void const *src, uint32_t len)
  307. {
  308. (void)memcpy(dst, src, len);
  309. }
  310. /*!
  311. *
  312. * env_strcmp - implementation
  313. *
  314. * @param dst
  315. * @param src
  316. */
  317. int32_t env_strcmp(const char *dst, const char *src)
  318. {
  319. return (strcmp(dst, src));
  320. }
  321. /*!
  322. *
  323. * env_strncpy - implementation
  324. *
  325. * @param dest
  326. * @param src
  327. * @param len
  328. */
  329. void env_strncpy(char *dest, const char *src, uint32_t len)
  330. {
  331. (void)strncpy(dest, src, len);
  332. }
  333. /*!
  334. *
  335. * env_strncmp - implementation
  336. *
  337. * @param dest
  338. * @param src
  339. * @param len
  340. */
  341. int32_t env_strncmp(char *dest, const char *src, uint32_t len)
  342. {
  343. return (strncmp(dest, src, len));
  344. }
  345. /*!
  346. *
  347. * env_mb - implementation
  348. *
  349. */
  350. void env_mb(void)
  351. {
  352. MEM_BARRIER();
  353. }
  354. /*!
  355. * env_rmb - implementation
  356. */
  357. void env_rmb(void)
  358. {
  359. MEM_BARRIER();
  360. }
  361. /*!
  362. * env_wmb - implementation
  363. */
  364. void env_wmb(void)
  365. {
  366. MEM_BARRIER();
  367. }
  368. /*!
  369. * env_map_vatopa - implementation
  370. *
  371. * @param address
  372. */
  373. uint32_t env_map_vatopa(void *address)
  374. {
  375. return platform_vatopa(address);
  376. }
  377. /*!
  378. * env_map_patova - implementation
  379. *
  380. * @param address
  381. */
  382. void *env_map_patova(uint32_t address)
  383. {
  384. return platform_patova(address);
  385. }
  386. /*!
  387. * env_create_mutex
  388. *
  389. * Creates a mutex with the given initial count.
  390. *
  391. */
  392. #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
  393. int32_t env_create_mutex(void **lock, int32_t count, void *context)
  394. #else
  395. int32_t env_create_mutex(void **lock, int32_t count)
  396. #endif
  397. {
  398. if (count > RL_ENV_MAX_MUTEX_COUNT)
  399. {
  400. return -1;
  401. }
  402. #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
  403. if (rt_mutex_init(context, "rl_mutex", RT_IPC_FLAG_PRIO) != RT_EOK)
  404. {
  405. LOG_E("rt_mutex_init failed ...");
  406. return -1;
  407. }
  408. *lock = context;
  409. #else
  410. *lock = rt_mutex_create("rl_mutex", RT_IPC_FLAG_PRIO);
  411. #endif
  412. if (*lock != ((void *)0))
  413. {
  414. return 0;
  415. }
  416. else
  417. {
  418. return -1;
  419. }
  420. }
  421. /*!
  422. * env_delete_mutex
  423. *
  424. * Deletes the given lock
  425. *
  426. */
  427. void env_delete_mutex(void *lock)
  428. {
  429. rt_mutex_delete(lock);
  430. }
  431. /*!
  432. * env_lock_mutex
  433. *
  434. * Tries to acquire the lock, if lock is not available then call to
  435. * this function will suspend.
  436. */
  437. void env_lock_mutex(void *lock)
  438. {
  439. rt_mutex_take(lock, RT_WAITING_FOREVER);
  440. }
  441. /*!
  442. * env_unlock_mutex
  443. *
  444. * Releases the given lock.
  445. */
  446. void env_unlock_mutex(void *lock)
  447. {
  448. rt_mutex_release(lock);
  449. }
  450. /*!
  451. * env_create_sync_lock
  452. *
  453. * Creates a synchronization lock primitive. It is used
  454. * when signal has to be sent from the interrupt context to main
  455. * thread context.
  456. */
  457. #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
  458. int32_t env_create_sync_lock(void **lock, int32_t state, void *context)
  459. {
  460. return env_create_mutex(lock, state, context); /* state=1 .. initially free */
  461. }
  462. #else
  463. int32_t env_create_sync_lock(void **lock, int32_t state)
  464. {
  465. return env_create_mutex(lock, state); /* state=1 .. initially free */
  466. }
  467. #endif
  468. /*!
  469. * env_delete_sync_lock
  470. *
  471. * Deletes the given lock
  472. *
  473. */
  474. void env_delete_sync_lock(void *lock)
  475. {
  476. if (lock != ((void *)0))
  477. {
  478. env_delete_mutex(lock);
  479. }
  480. }
  481. /*!
  482. * env_acquire_sync_lock
  483. *
  484. * Tries to acquire the lock, if lock is not available then call to
  485. * this function waits for lock to become available.
  486. */
  487. void env_acquire_sync_lock(void *lock)
  488. {
  489. rt_mutex_take((rt_mutex_t)lock, RT_WAITING_FOREVER);
  490. }
  491. /*!
  492. * env_release_sync_lock
  493. *
  494. * Releases the given lock.
  495. */
  496. void env_release_sync_lock(void *lock)
  497. {
  498. rt_mutex_release((rt_mutex_t)lock);
  499. }
  500. /*!
  501. * env_sleep_msec
  502. *
  503. * Suspends the calling thread for given time , in msecs.
  504. */
  505. void env_sleep_msec(uint32_t num_msec)
  506. {
  507. rt_thread_mdelay(num_msec);
  508. }
  509. /*!
  510. * env_register_isr
  511. *
  512. * Registers interrupt handler data for the given interrupt vector.
  513. *
  514. * @param vector_id - virtual interrupt vector number
  515. * @param data - interrupt handler data (virtqueue)
  516. */
  517. void env_register_isr(uint32_t vector_id, void *data)
  518. {
  519. RL_ASSERT(vector_id < ISR_COUNT);
  520. if (vector_id < ISR_COUNT)
  521. {
  522. isr_table[vector_id].data = data;
  523. }
  524. }
  525. /*!
  526. * env_unregister_isr
  527. *
  528. * Unregisters interrupt handler data for the given interrupt vector.
  529. *
  530. * @param vector_id - virtual interrupt vector number
  531. */
  532. void env_unregister_isr(uint32_t vector_id)
  533. {
  534. RL_ASSERT(vector_id < ISR_COUNT);
  535. if (vector_id < ISR_COUNT)
  536. {
  537. isr_table[vector_id].data = ((void *)0);
  538. }
  539. }
  540. /*!
  541. * env_enable_interrupt
  542. *
  543. * Enables the given interrupt
  544. *
  545. * @param vector_id - virtual interrupt vector number
  546. */
  547. void env_enable_interrupt(uint32_t vector_id)
  548. {
  549. (void)platform_interrupt_enable(vector_id);
  550. }
  551. /*!
  552. * env_disable_interrupt
  553. *
  554. * Disables the given interrupt
  555. *
  556. * @param vector_id - virtual interrupt vector number
  557. */
  558. void env_disable_interrupt(uint32_t vector_id)
  559. {
  560. (void)platform_interrupt_disable(vector_id);
  561. }
  562. /*!
  563. * env_map_memory
  564. *
  565. * Enables memory mapping for given memory region.
  566. *
  567. * @param pa - physical address of memory
  568. * @param va - logical address of memory
  569. * @param size - memory size
  570. * param flags - flags for cache/uncached and access type
  571. */
  572. void env_map_memory(uint32_t pa, uint32_t va, uint32_t size, uint32_t flags)
  573. {
  574. platform_map_mem_region(va, pa, size, flags);
  575. }
  576. /*!
  577. * env_disable_cache
  578. *
  579. * Disables system caches.
  580. *
  581. */
  582. void env_disable_cache(void)
  583. {
  584. platform_cache_all_flush_invalidate();
  585. platform_cache_disable();
  586. }
  587. /*!
  588. *
  589. * env_get_timestamp
  590. *
  591. * Returns a 64 bit time stamp.
  592. *
  593. *
  594. */
  595. uint64_t env_get_timestamp(void)
  596. {
  597. return (uint64_t)rt_tick_get();
  598. }
  599. /*========================================================= */
  600. /* Util data / functions */
  601. void env_isr(uint32_t vector)
  602. {
  603. struct isr_info *info;
  604. RL_ASSERT(vector < ISR_COUNT);
  605. if (vector < ISR_COUNT)
  606. {
  607. info = &isr_table[vector];
  608. virtqueue_notification((struct virtqueue *)info->data);
  609. }
  610. }
  611. /*
  612. * env_create_queue
  613. *
  614. * Creates a message queue.
  615. *
  616. * @param queue - pointer to created queue
  617. * @param length - maximum number of elements in the queue
  618. * @param element_size - queue element size in bytes
  619. * @param queue_static_storage - pointer to queue static storage buffer
  620. * @param queue_static_context - pointer to queue static context
  621. *
  622. * @return - status of function execution
  623. */
  624. #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
  625. int32_t env_create_queue(void **queue,
  626. int32_t length,
  627. int32_t element_size,
  628. uint8_t *queue_static_storage,
  629. rpmsg_static_queue_ctxt *queue_static_context)
  630. {
  631. if (rt_mq_init(queue_static_context, "rl_mq", queue_static_storage, element_size, length, RT_IPC_FLAG_PRIO) != RT_EOK)
  632. {
  633. LOG_E("rt_mq_init failed ...");
  634. return -1;
  635. }
  636. *queue = queue_static_context;
  637. #else
  638. int32_t env_create_queue(void **queue, int32_t length, int32_t element_size)
  639. {
  640. *queue = rt_mq_create("rl_mq", element_size, length, RT_IPC_FLAG_PRIO);
  641. #endif
  642. if (*queue != ((void *)0))
  643. {
  644. return 0;
  645. }
  646. else
  647. {
  648. LOG_E("rt_mq_create failed...");
  649. }
  650. return -1;
  651. }
  652. /*!
  653. * env_delete_queue
  654. *
  655. * Deletes the message queue.
  656. *
  657. * @param queue - queue to delete
  658. */
  659. void env_delete_queue(void *queue)
  660. {
  661. rt_mq_delete(queue);
  662. }
  663. /*!
  664. * env_put_queue
  665. *
  666. * Put an element in a queue.
  667. *
  668. * @param queue - queue to put element in
  669. * @param msg - pointer to the message to be put into the queue
  670. * @param timeout_ms - timeout in ms
  671. *
  672. * @return - status of function execution
  673. */
  674. int32_t env_put_queue(void *queue, void *msg, uintptr_t timeout_ms)
  675. {
  676. rt_int32_t timeout;
  677. if (timeout_ms == RL_BLOCK)
  678. timeout = RT_WAITING_FOREVER;
  679. else
  680. timeout = rt_tick_from_millisecond(timeout_ms);
  681. rt_mq_t mq = (rt_mq_t)queue;
  682. rt_err_t ret = rt_mq_send_wait(mq, msg, mq->msg_size, timeout);
  683. if (ret < 0)
  684. {
  685. LOG_E("rt_mq_send_wait failed ret:%d", ret);
  686. return 0;
  687. }
  688. else
  689. LOG_D("rt_mq_send_wait success...");
  690. return 1;
  691. }
  692. /*!
  693. * env_get_queue
  694. *
  695. * Get an element out of a queue.
  696. *
  697. * @param queue - queue to get element from
  698. * @param msg - pointer to a memory to save the message
  699. * @param timeout_ms - timeout in ms
  700. *
  701. * @return - status of function execution
  702. */
  703. int32_t env_get_queue(void *queue, void *msg, uintptr_t timeout_ms)
  704. {
  705. rt_int32_t timeout;
  706. if (timeout_ms == RL_BLOCK)
  707. timeout = RT_WAITING_FOREVER;
  708. else
  709. timeout = rt_tick_from_millisecond(timeout_ms);
  710. rt_mq_t mq = (rt_mq_t)queue;
  711. rt_err_t ret = rt_mq_recv(mq, msg, mq->msg_size, timeout);
  712. if (ret < 0)
  713. {
  714. LOG_E("rt_mq_recv failed ret:%d", ret);
  715. return 0;
  716. }
  717. else
  718. LOG_D("rt_mq_recv success...");
  719. return 1;
  720. }
  721. /*!
  722. * env_get_current_queue_size
  723. *
  724. * Get current queue size.
  725. *
  726. * @param queue - queue pointer
  727. *
  728. * @return - Number of queued items in the queue
  729. */
  730. int32_t env_get_current_queue_size(void *queue)
  731. {
  732. rt_mq_t mq = (rt_mq_t)queue;
  733. return mq->entry;
  734. }