HAL_OS_rtthread.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) 2006-2018 RT-Thread Development Team. All rights reserved.
  3. * License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. * Again edit by rt-thread group
  18. * Change Logs:
  19. * Date Author Notes
  20. * 2019-07-21 MurphyZhao first edit
  21. */
  22. #include <rtthread.h>
  23. #include <stdlib.h>
  24. #include "infra_types.h"
  25. #include "infra_defs.h"
  26. #include "wrappers_defs.h"
  27. #define DBG_TAG "ali.os"
  28. #define DBG_LVL DBG_INFO
  29. #include <rtdbg.h>
  30. static char log_buf[RT_CONSOLEBUF_SIZE];
  31. void *HAL_MutexCreate(void)
  32. {
  33. rt_mutex_t mutex = rt_mutex_create("ali_ld_mutex", RT_IPC_FLAG_FIFO);
  34. return mutex;
  35. }
  36. void HAL_MutexDestroy(void *mutex)
  37. {
  38. int err_num;
  39. if (0 != (err_num = rt_mutex_delete((rt_mutex_t)mutex)))
  40. {
  41. LOG_E("destroy mutex failed, err num: %d", err_num);
  42. }
  43. }
  44. void HAL_MutexLock(void *mutex)
  45. {
  46. int err_num;
  47. if (0 != (err_num = rt_mutex_take((rt_mutex_t)mutex, RT_WAITING_FOREVER)))
  48. {
  49. LOG_E("lock mutex failed, err num: %d", err_num);
  50. }
  51. }
  52. void HAL_MutexUnlock(void *mutex)
  53. {
  54. int err_num;
  55. if (0 != (err_num = rt_mutex_release((rt_mutex_t)mutex)))
  56. {
  57. LOG_E("unlock mutex failed, err num: %d", err_num);
  58. }
  59. }
  60. /**
  61. * @brief create a semaphore
  62. *
  63. * @return semaphore handle.
  64. * @see None.
  65. * @note The recommended value of maximum count of the semaphore is 255.
  66. */
  67. void *HAL_SemaphoreCreate(void)
  68. {
  69. char name[10] = {0};
  70. static uint8_t sem_num = 0;
  71. rt_snprintf(name, sizeof(name), "sem%02d", ((++sem_num)%100));
  72. rt_sem_t sem = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
  73. if (!sem)
  74. {
  75. LOG_E("Semaphore create failed!");
  76. }
  77. return (void*)sem;
  78. }
  79. /**
  80. * @brief destory a semaphore
  81. *
  82. * @param[in] sem @n the specified sem.
  83. * @return None.
  84. * @see None.
  85. * @note None.
  86. */
  87. void HAL_SemaphoreDestroy(void *sem)
  88. {
  89. rt_err_t err = RT_EOK;
  90. rt_object_t obj = sem;
  91. if (!obj)
  92. {
  93. LOG_E("In param (sem) is NULL!");
  94. return;
  95. }
  96. if (obj->type == RT_Object_Class_Semaphore)
  97. {
  98. err = rt_sem_delete((rt_sem_t)obj);
  99. if (err != RT_EOK)
  100. {
  101. LOG_E("sem delete failed! errno:%d", err);
  102. }
  103. }
  104. else
  105. {
  106. LOG_E("Error sem handler!");
  107. }
  108. return;
  109. }
  110. /**
  111. * @brief signal thread wait on a semaphore
  112. *
  113. * @param[in] sem @n the specified semaphore.
  114. * @return None.
  115. * @see None.
  116. * @note None.
  117. */
  118. void HAL_SemaphorePost(void *sem)
  119. {
  120. rt_err_t err = RT_EOK;
  121. rt_object_t obj = sem;
  122. if (!obj)
  123. {
  124. LOG_E("In param (sem) is NULL!");
  125. return;
  126. }
  127. if (obj->type == RT_Object_Class_Semaphore)
  128. {
  129. err = rt_sem_release((rt_sem_t)obj);
  130. if (err != RT_EOK)
  131. {
  132. LOG_E("sem release failed! errno:%d", err);
  133. }
  134. }
  135. else
  136. {
  137. LOG_E("Error sem handler!");
  138. }
  139. return;
  140. }
  141. /**
  142. * @brief wait on a semaphore
  143. *
  144. * @param[in] sem @n the specified semaphore.
  145. * @param[in] timeout_ms @n timeout interval in millisecond.
  146. If timeout_ms is PLATFORM_WAIT_INFINITE, the function will return only when the semaphore is signaled.
  147. * @return
  148. @verbatim
  149. = 0: The state of the specified object is signaled.
  150. = -1: The time-out interval elapsed, and the object's state is nonsignaled.
  151. @endverbatim
  152. * @see None.
  153. * @note None.
  154. */
  155. int HAL_SemaphoreWait(void *sem, uint32_t timeout_ms)
  156. {
  157. rt_err_t err = RT_EOK;
  158. rt_object_t obj = sem;
  159. if (!obj)
  160. {
  161. LOG_E("In param (sem) is NULL!");
  162. return -1;
  163. }
  164. if (obj->type == RT_Object_Class_Semaphore)
  165. {
  166. err = rt_sem_take((rt_sem_t)obj, timeout_ms);
  167. if (err != RT_EOK)
  168. {
  169. LOG_E("sem take failed! errno:%d", err);
  170. }
  171. }
  172. else
  173. {
  174. LOG_E("Error sem handler!");
  175. }
  176. return (err == RT_EOK ? 0 : -1);
  177. }
  178. void *HAL_Malloc(uint32_t size)
  179. {
  180. return rt_malloc(size);
  181. }
  182. void HAL_Free(void *ptr)
  183. {
  184. rt_free(ptr);
  185. }
  186. uint64_t HAL_UptimeMs(void)
  187. {
  188. #if (RT_TICK_PER_SECOND == 1000)
  189. return (uint64_t)rt_tick_get();
  190. #else
  191. uint64_t tick;
  192. tick = rt_tick_get();
  193. tick = tick * 1000;
  194. return (tick + RT_TICK_PER_SECOND - 1)/RT_TICK_PER_SECOND;
  195. #endif
  196. }
  197. void HAL_SleepMs(uint32_t ms)
  198. {
  199. rt_thread_mdelay(ms);
  200. }
  201. void HAL_Srandom(uint32_t seed)
  202. {
  203. srand(seed);
  204. }
  205. uint32_t HAL_Random(uint32_t region)
  206. {
  207. return (region > 0) ? (rand() % region) : 0;
  208. }
  209. int HAL_Snprintf(char *str, const int len, const char *fmt, ...)
  210. {
  211. va_list args;
  212. int rc;
  213. va_start(args, fmt);
  214. rc = rt_vsnprintf(str, len, fmt, args);
  215. va_end(args);
  216. return rc;
  217. }
  218. int HAL_Vsnprintf(char *str, const int len, const char *format, va_list ap)
  219. {
  220. return rt_vsnprintf(str, len, format, ap);
  221. }
  222. void HAL_Printf(const char *fmt, ...)
  223. {
  224. va_list args;
  225. va_start(args, fmt);
  226. rt_vsnprintf(log_buf, RT_CONSOLEBUF_SIZE, fmt, args);
  227. va_end(args);
  228. rt_kprintf("%s", log_buf);
  229. }