whd_rtos.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Sample code used for reference only
  3. * TBD : Public URL for CY RTOS repo
  4. */
  5. /******************************************************
  6. * Function definitions
  7. ******************************************************/
  8. cy_rslt_t cy_rtos_create_thread(cy_thread_t *thread, cy_thread_entry_fn_t entry_function,
  9. const char *name, void *stack, uint32_t stack_size, cy_thread_priority_t priority,
  10. cy_thread_arg_t arg)
  11. {
  12. osThreadAttr_t atr;
  13. atr.name = name;
  14. atr.attr_bits = osThreadDetached;
  15. atr.priority = (osPriority_t)priority;
  16. atr.stack_mem = stack;
  17. atr.stack_size = stack_size;
  18. atr.cb_mem = NULL;
  19. atr.cb_size = 0;
  20. atr.tz_module = 0;
  21. atr.reserved = 0;
  22. *thread = osThreadNew( (osThreadFunc_t)entry_function, (void *)arg, &atr );
  23. if (*thread == NULL)
  24. {
  25. }
  26. return (*thread == NULL) ? CY_RTOS_NO_MEMORY : CY_RSLT_SUCCESS;
  27. }
  28. /**
  29. * Exit the current thread
  30. *
  31. * @returns WHD_SUCCESS on success or otherwise
  32. */
  33. cy_rslt_t cy_rtos_exit_thread(void)
  34. {
  35. osThreadExit();
  36. return CY_RSLT_SUCCESS;
  37. }
  38. /**
  39. * Terminates the current thread
  40. *
  41. *
  42. * @param thread : handle of the thread to terminate
  43. *
  44. * @returns CY_RSLT_SUCCESS on success, error otherwise
  45. */
  46. cy_rslt_t cy_rtos_terminate_thread(cy_thread_t *thread)
  47. {
  48. osStatus_t status;
  49. if (*thread == NULL)
  50. return CY_RTOS_BAD_PARAM;
  51. status = osThreadTerminate(*thread); // stop the thread
  52. *thread = NULL;
  53. if (status == osOK)
  54. return CY_RSLT_SUCCESS;
  55. else
  56. return CY_RTOS_GENERAL_ERROR;
  57. }
  58. /**
  59. * Blocks the current thread until the indicated thread is complete
  60. *
  61. * @param thread : handle of the thread to terminate
  62. *
  63. * @returns CY_RSLT_SUCCESS on success, error otherwise
  64. */
  65. cy_rslt_t cy_rtos_join_thread(cy_thread_t *thread)
  66. {
  67. osStatus_t status;
  68. if (*thread == NULL)
  69. return CY_RTOS_BAD_PARAM;
  70. status = osThreadJoin(*thread);
  71. if (status == osOK)
  72. return CY_RSLT_SUCCESS;
  73. else
  74. return CY_RTOS_GENERAL_ERROR;
  75. }
  76. /**
  77. * Creates a semaphore
  78. *
  79. * @param semaphore : pointer to variable which will receive handle of created semaphore
  80. *
  81. * @returns CY_RSLT_SUCCESS on success, error otherwise
  82. */
  83. cy_rslt_t cy_rtos_init_semaphore(cy_semaphore_t *semaphore, uint32_t maxcount, uint32_t initcount)
  84. {
  85. *semaphore = osSemaphoreNew(1, 0, NULL);
  86. if (*semaphore == NULL)
  87. {
  88. return CY_RTOS_GENERAL_ERROR;
  89. }
  90. return CY_RSLT_SUCCESS;
  91. }
  92. /**
  93. * Gets a semaphore
  94. *
  95. * If value of semaphore is larger than zero, then the semaphore is decremented and function returns
  96. * Else If value of semaphore is zero, then current thread is suspended until semaphore is set.
  97. * Value of semaphore should never be below zero
  98. *
  99. * Must not be called from interrupt context, since it could block, and since an interrupt is not a
  100. * normal thread, so could cause RTOS problems if it tries to suspend it.
  101. * If called from interrupt context time out value should be 0.
  102. *
  103. * @param semaphore : Pointer to variable which will receive handle of created semaphore
  104. * @param timeout_ms : Maximum period to block for. Can be passed CY_RTOS_NEVER_TIMEOUT to request no timeout
  105. * @param will_set_in_isr : True if the semaphore will be set in an ISR. Currently only used for NoOS/NoNS
  106. *
  107. */
  108. cy_rslt_t cy_rtos_get_semaphore(cy_semaphore_t *semaphore, uint32_t timeout_ms,
  109. bool will_set_in_isr)
  110. {
  111. osStatus_t result;
  112. will_set_in_isr = will_set_in_isr;
  113. if (*semaphore == NULL)
  114. {
  115. return CY_RTOS_GENERAL_ERROR;
  116. }
  117. if (timeout_ms == CY_RTOS_NEVER_TIMEOUT)
  118. result = osSemaphoreAcquire(*semaphore, osWaitForever);
  119. else
  120. result = osSemaphoreAcquire(*semaphore, timeout_ms * 1000 / 1000);
  121. if (result == osOK)
  122. {
  123. return CY_RSLT_SUCCESS;
  124. }
  125. else if (result == osErrorTimeout)
  126. {
  127. return CY_RTOS_TIMEOUT;
  128. }
  129. return CY_RTOS_GENERAL_ERROR;
  130. }
  131. /**
  132. * Sets a semaphore
  133. *
  134. * If any threads are waiting on the semaphore, the first thread is resumed
  135. * Else increment semaphore.
  136. *
  137. * Can be called from interrupt context, so must be able to handle resuming other
  138. * threads from interrupt context.
  139. *
  140. * @param semaphore : Pointer to variable which will receive handle of created semaphore
  141. * @param called_from_ISR : Value of WHD_TRUE indicates calling from interrupt context
  142. * Value of WHD_FALSE indicates calling from normal thread context
  143. *
  144. * @return cy_rslt_t : CY_RSLT_SUCCESS if semaphore was successfully set
  145. * : error if an error occurred
  146. *
  147. */
  148. cy_rslt_t cy_rtos_set_semaphore(cy_semaphore_t *semaphore, bool called_from_ISR)
  149. {
  150. osStatus_t result;
  151. called_from_ISR = called_from_ISR;
  152. if (*semaphore == NULL)
  153. {
  154. return CY_RTOS_GENERAL_ERROR;
  155. }
  156. result = osSemaphoreRelease(*semaphore);
  157. if ( (result == osOK) || (result == osErrorResource) )
  158. {
  159. return CY_RSLT_SUCCESS;
  160. }
  161. else if (result == osErrorParameter)
  162. {
  163. return CY_RTOS_GENERAL_ERROR;
  164. }
  165. return CY_RTOS_GENERAL_ERROR;
  166. }
  167. /**
  168. * Deletes a semaphore
  169. *
  170. * WHD uses this function to delete a semaphore.
  171. *
  172. * @param semaphore : Pointer to the semaphore handle
  173. *
  174. * @return cy_rslt_t : CY_RSLT_SUCCESS if semaphore was successfully deleted
  175. * : error if an error occurred
  176. *
  177. */
  178. cy_rslt_t cy_rtos_deinit_semaphore(cy_semaphore_t *semaphore)
  179. {
  180. osStatus_t result;
  181. if (*semaphore == NULL)
  182. {
  183. return CY_RTOS_GENERAL_ERROR;
  184. }
  185. result = osSemaphoreDelete(*semaphore);
  186. if (result != osOK)
  187. return CY_RTOS_GENERAL_ERROR;
  188. return CY_RSLT_SUCCESS;
  189. }
  190. /**
  191. * Gets time in milliseconds since RTOS start
  192. *
  193. * @Note: since this is only 32 bits, it will roll over every 49 days, 17 hours.
  194. *
  195. * @returns Time in milliseconds since RTOS started.
  196. */
  197. cy_rslt_t cy_rtos_get_time(cy_time_t *tval)
  198. {
  199. /* Get Number of ticks per second */
  200. uint32_t tick_freq = osKernelGetTickFreq();
  201. /* Convert ticks count to time in milliseconds */
  202. *tval = (osKernelGetTickCount() * (1000 / tick_freq) );
  203. return CY_RSLT_SUCCESS;
  204. }
  205. /**
  206. * Delay for a number of milliseconds
  207. *
  208. * @return cy_rslt_t : CY_RSLT_SUCCESS if delay was successful
  209. * : error if an error occurred
  210. *
  211. */
  212. cy_rslt_t cy_rtos_delay_milliseconds(cy_time_t num_ms)
  213. {
  214. osStatus_t result;
  215. result = osDelay( (uint32_t )num_ms );
  216. if (result == osOK)
  217. {
  218. return CY_RSLT_SUCCESS;
  219. }
  220. else if (result == osErrorISR)
  221. {
  222. return CY_RTOS_GENERAL_ERROR;
  223. }
  224. else if (result == osErrorParameter)
  225. {
  226. return CY_RTOS_BAD_PARAM;
  227. }
  228. else if (result == osErrorNoMemory)
  229. {
  230. return CY_RTOS_GENERAL_ERROR;
  231. }
  232. return CY_RTOS_GENERAL_ERROR;
  233. }