timer.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2018 Intel Corporation
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <kernel.h>
  7. #include <string.h>
  8. #include "wrapper.h"
  9. #define ACTIVE 1
  10. #define NOT_ACTIVE 0
  11. static void zephyr_timer_wrapper(struct k_timer *timer);
  12. K_MEM_SLAB_DEFINE(cv2_timer_slab, sizeof(struct cv2_timer),
  13. CONFIG_CMSIS_V2_TIMER_MAX_COUNT, 4);
  14. static const osTimerAttr_t init_timer_attrs = {
  15. .name = "ZephyrTimer",
  16. .attr_bits = 0,
  17. .cb_mem = NULL,
  18. .cb_size = 0,
  19. };
  20. static void zephyr_timer_wrapper(struct k_timer *timer)
  21. {
  22. struct cv2_timer *cm_timer;
  23. cm_timer = CONTAINER_OF(timer, struct cv2_timer, z_timer);
  24. (cm_timer->callback_function)(cm_timer->arg);
  25. }
  26. /**
  27. * @brief Create a Timer
  28. */
  29. osTimerId_t osTimerNew(osTimerFunc_t func, osTimerType_t type,
  30. void *argument, const osTimerAttr_t *attr)
  31. {
  32. struct cv2_timer *timer;
  33. if (type != osTimerOnce && type != osTimerPeriodic) {
  34. return NULL;
  35. }
  36. if (k_is_in_isr()) {
  37. return NULL;
  38. }
  39. if (attr == NULL) {
  40. attr = &init_timer_attrs;
  41. }
  42. if (k_mem_slab_alloc(&cv2_timer_slab, (void **)&timer, K_MSEC(100)) == 0) {
  43. (void)memset(timer, 0, sizeof(struct cv2_timer));
  44. } else {
  45. return NULL;
  46. }
  47. timer->callback_function = func;
  48. timer->arg = argument;
  49. timer->type = type;
  50. timer->status = NOT_ACTIVE;
  51. k_timer_init(&timer->z_timer, zephyr_timer_wrapper, NULL);
  52. if (attr->name == NULL) {
  53. strncpy(timer->name, init_timer_attrs.name,
  54. sizeof(timer->name) - 1);
  55. } else {
  56. strncpy(timer->name, attr->name, sizeof(timer->name) - 1);
  57. }
  58. return (osTimerId_t)timer;
  59. }
  60. /**
  61. * @brief Start or restart a Timer
  62. */
  63. osStatus_t osTimerStart(osTimerId_t timer_id, uint32_t ticks)
  64. {
  65. struct cv2_timer *timer = (struct cv2_timer *)timer_id;
  66. if (timer == NULL) {
  67. return osErrorParameter;
  68. }
  69. if (k_is_in_isr()) {
  70. return osErrorISR;
  71. }
  72. if (timer->type == osTimerOnce) {
  73. k_timer_start(&timer->z_timer, K_TICKS(ticks), K_NO_WAIT);
  74. } else if (timer->type == osTimerPeriodic) {
  75. k_timer_start(&timer->z_timer,
  76. K_TICKS(ticks), K_TICKS(ticks));
  77. }
  78. timer->status = ACTIVE;
  79. return osOK;
  80. }
  81. /**
  82. * @brief Stop the Timer
  83. */
  84. osStatus_t osTimerStop(osTimerId_t timer_id)
  85. {
  86. struct cv2_timer *timer = (struct cv2_timer *)timer_id;
  87. if (timer == NULL) {
  88. return osErrorParameter;
  89. }
  90. if (k_is_in_isr()) {
  91. return osErrorISR;
  92. }
  93. if (timer->status == NOT_ACTIVE) {
  94. return osErrorResource;
  95. }
  96. k_timer_stop(&timer->z_timer);
  97. timer->status = NOT_ACTIVE;
  98. return osOK;
  99. }
  100. /**
  101. * @brief Delete the timer that was created by osTimerCreate
  102. */
  103. osStatus_t osTimerDelete(osTimerId_t timer_id)
  104. {
  105. struct cv2_timer *timer = (struct cv2_timer *) timer_id;
  106. if (timer == NULL) {
  107. return osErrorParameter;
  108. }
  109. if (k_is_in_isr()) {
  110. return osErrorISR;
  111. }
  112. if (timer->status == ACTIVE) {
  113. k_timer_stop(&timer->z_timer);
  114. timer->status = NOT_ACTIVE;
  115. }
  116. k_mem_slab_free(&cv2_timer_slab, (void *) &timer);
  117. return osOK;
  118. }
  119. /**
  120. * @brief Get name of a timer.
  121. */
  122. const char *osTimerGetName(osTimerId_t timer_id)
  123. {
  124. struct cv2_timer *timer = (struct cv2_timer *)timer_id;
  125. if (k_is_in_isr() || (timer == NULL)) {
  126. return NULL;
  127. }
  128. return timer->name;
  129. }
  130. /**
  131. * @brief Check if a timer is running.
  132. */
  133. uint32_t osTimerIsRunning(osTimerId_t timer_id)
  134. {
  135. struct cv2_timer *timer = (struct cv2_timer *)timer_id;
  136. if (k_is_in_isr() || (timer == NULL)) {
  137. return 0;
  138. }
  139. return !(!(k_timer_remaining_get(&timer->z_timer)));
  140. }