wm_wl_mbox.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @file wm_wl_mbox.c
  3. *
  4. * @brief mailbox (mbox) APIs
  5. *
  6. * @author dave
  7. *
  8. * Copyright (c) 2015 Winner Microelectronics Co., Ltd.
  9. */
  10. #include "wm_mem.h"
  11. #include "wm_wl_mbox.h"
  12. #include "wm_wl_task.h"
  13. const void * const tls_null_pointer = (void *)0;
  14. /**
  15. * @brief create a malibox
  16. *
  17. * @param[out] *mbox pointer to the mailbox
  18. * @param[in] size size of mailbox
  19. *
  20. * @retval TLS_OS_SUCCESS success
  21. * @retval TLS_OS_ERROR failed
  22. *
  23. * @note None
  24. */
  25. s8 tls_mbox_new(tls_mbox_t *mbox, int size)
  26. {
  27. s8 err;
  28. tls_os_status_t status;
  29. if (size == 0)
  30. size = 10;
  31. status = tls_os_queue_create(mbox, size);
  32. if (status == TLS_OS_SUCCESS) {
  33. err = TLS_OS_SUCCESS;
  34. }
  35. else
  36. err = TLS_OS_ERROR;
  37. return err;
  38. }
  39. #ifndef tls_mbox_valid
  40. /**
  41. * @brief check if an mbox is valid/allocated
  42. *
  43. * @param[in] mbox pointer to the mailbox
  44. *
  45. * @retval 0 invalid
  46. * @retval 1 valid
  47. *
  48. * @note None
  49. */
  50. int tls_mbox_valid(tls_mbox_t mbox)
  51. {
  52. if (mbox == NULL)
  53. return 0;
  54. else
  55. return 1;
  56. }
  57. #endif
  58. /**
  59. * @brief sends a message to a mailbox
  60. *
  61. * @param[in] mbox pointer to the mailbox
  62. * @param[in] *msg pointer to the message to be post
  63. *
  64. * @return None
  65. *
  66. * @note None
  67. */
  68. void tls_mbox_post(tls_mbox_t mbox, void *msg)
  69. {
  70. u8 err;
  71. u8 i=0;
  72. if(msg == NULL )
  73. msg = (void*)tls_null_pointer;
  74. /* try 10 times */
  75. while (i < 10)
  76. {
  77. err = tls_os_queue_send(mbox, msg, 0);
  78. if(err == TLS_OS_SUCCESS)
  79. break;
  80. i++;
  81. tls_os_time_delay(5);
  82. }
  83. }
  84. /**
  85. * @brief posts the "msg" to the mailbox.
  86. *
  87. * @param[in] mbox pointer to the mailbox
  88. * @param[in] *msg pointer to the message to be post
  89. *
  90. * @retval TLS_OS_SUCCESS success
  91. * @retval TLS_OS_ERROR failed
  92. *
  93. * @note this function have to block until the "msg" is really posted.
  94. */
  95. s8 tls_mbox_trypost(tls_mbox_t mbox, void *msg)
  96. {
  97. u8 err;
  98. if( msg == NULL )
  99. {
  100. msg = (void*)tls_null_pointer;
  101. }
  102. err = tls_os_queue_send(mbox, msg, 0);
  103. if(err == TLS_OS_SUCCESS)
  104. return TLS_OS_SUCCESS;
  105. return TLS_OS_ERROR;
  106. }
  107. /**
  108. * @brief waits for a message within the specified time
  109. *
  110. * @param[in] mbox pointer to the mailbox
  111. * @param[out] **msg pointer to the message to be received
  112. * @param[in] timeout the specified time
  113. *
  114. * @retval SYS_ARCH_TIMEOUT time out
  115. * @retval other time of elapsed
  116. *
  117. * @note None
  118. */
  119. u32 tls_arch_mbox_fetch(tls_mbox_t mbox, void **msg, u32 timeout)
  120. {
  121. u8 err;
  122. u32 ucos_timeout = 0;
  123. u32 in_timeout = timeout * HZ / 1000;
  124. u32 tick_start, tick_stop, tick_elapsed;
  125. if(timeout && in_timeout == 0)
  126. in_timeout = 1;
  127. tick_start = tls_os_get_time();
  128. // *msg = OSQPend(mbox, in_timeout, &err);
  129. err = tls_os_queue_receive(mbox,msg,0,in_timeout);
  130. // if (err == OS_ERR_TIMEOUT ) {
  131. if (err != TLS_OS_SUCCESS ) {
  132. ucos_timeout = SYS_ARCH_TIMEOUT;
  133. return ucos_timeout;
  134. }
  135. tick_stop = tls_os_get_time();
  136. // Take care of wrap-around.
  137. if( tick_stop >= tick_start )
  138. tick_elapsed = tick_stop - tick_start;
  139. else
  140. tick_elapsed = 0xFFFFFFFF - tick_start + tick_stop ;
  141. return tick_elapsed * 1000/HZ;
  142. }