timer.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "timer.h"
  2. #include "check.h"
  3. #include "core/common_defines.h"
  4. #include <FreeRTOS.h>
  5. #include <timers.h>
  6. typedef struct {
  7. FuriTimerCallback func;
  8. void* context;
  9. } TimerCallback_t;
  10. static void TimerCallback(TimerHandle_t hTimer) {
  11. TimerCallback_t* callb;
  12. /* Retrieve pointer to callback function and context */
  13. callb = (TimerCallback_t*)pvTimerGetTimerID(hTimer);
  14. /* Remove dynamic allocation flag */
  15. callb = (TimerCallback_t*)((uint32_t)callb & ~1U);
  16. if(callb != NULL) {
  17. callb->func(callb->context);
  18. }
  19. }
  20. FuriTimer* furi_timer_alloc(FuriTimerCallback func, FuriTimerType type, void* context) {
  21. furi_assert((furi_is_irq_context() == 0U) && (func != NULL));
  22. TimerHandle_t hTimer;
  23. TimerCallback_t* callb;
  24. UBaseType_t reload;
  25. uint32_t callb_dyn;
  26. hTimer = NULL;
  27. callb = NULL;
  28. callb_dyn = 0U;
  29. /* Dynamic memory allocation is available: if memory for callback and */
  30. /* its context is not provided, allocate it from dynamic memory pool */
  31. if(callb == NULL) {
  32. callb = (TimerCallback_t*)pvPortMalloc(sizeof(TimerCallback_t));
  33. if(callb != NULL) {
  34. /* Callback memory was allocated from dynamic pool, set flag */
  35. callb_dyn = 1U;
  36. }
  37. }
  38. if(callb != NULL) {
  39. callb->func = func;
  40. callb->context = context;
  41. if(type == FuriTimerTypeOnce) {
  42. reload = pdFALSE;
  43. } else {
  44. reload = pdTRUE;
  45. }
  46. /* Store callback memory dynamic allocation flag */
  47. callb = (TimerCallback_t*)((uint32_t)callb | callb_dyn);
  48. // TimerCallback function is always provided as a callback and is used to call application
  49. // specified function with its context both stored in structure callb.
  50. hTimer = xTimerCreate(NULL, 1, reload, callb, TimerCallback);
  51. if((hTimer == NULL) && (callb != NULL) && (callb_dyn == 1U)) {
  52. /* Failed to create a timer, release allocated resources */
  53. callb = (TimerCallback_t*)((uint32_t)callb & ~1U);
  54. vPortFree(callb);
  55. }
  56. }
  57. /* Return timer ID */
  58. return ((FuriTimer*)hTimer);
  59. }
  60. void furi_timer_free(FuriTimer* instance) {
  61. furi_assert(!furi_is_irq_context());
  62. furi_assert(instance);
  63. TimerHandle_t hTimer = (TimerHandle_t)instance;
  64. TimerCallback_t* callb;
  65. callb = (TimerCallback_t*)pvTimerGetTimerID(hTimer);
  66. if(xTimerDelete(hTimer, portMAX_DELAY) == pdPASS) {
  67. if((uint32_t)callb & 1U) {
  68. /* Callback memory was allocated from dynamic pool, clear flag */
  69. callb = (TimerCallback_t*)((uint32_t)callb & ~1U);
  70. /* Return allocated memory to dynamic pool */
  71. vPortFree(callb);
  72. }
  73. }
  74. }
  75. FuriStatus furi_timer_start(FuriTimer* instance, uint32_t ticks) {
  76. furi_assert(!furi_is_irq_context());
  77. furi_assert(instance);
  78. TimerHandle_t hTimer = (TimerHandle_t)instance;
  79. FuriStatus stat;
  80. if(xTimerChangePeriod(hTimer, ticks, portMAX_DELAY) == pdPASS) {
  81. stat = FuriStatusOk;
  82. } else {
  83. stat = FuriStatusErrorResource;
  84. }
  85. /* Return execution status */
  86. return (stat);
  87. }
  88. FuriStatus furi_timer_stop(FuriTimer* instance) {
  89. furi_assert(!furi_is_irq_context());
  90. furi_assert(instance);
  91. TimerHandle_t hTimer = (TimerHandle_t)instance;
  92. FuriStatus stat;
  93. if(xTimerIsTimerActive(hTimer) == pdFALSE) {
  94. stat = FuriStatusErrorResource;
  95. } else {
  96. if(xTimerStop(hTimer, portMAX_DELAY) == pdPASS) {
  97. stat = FuriStatusOk;
  98. } else {
  99. stat = FuriStatusError;
  100. }
  101. }
  102. /* Return execution status */
  103. return (stat);
  104. }
  105. uint32_t furi_timer_is_running(FuriTimer* instance) {
  106. furi_assert(!furi_is_irq_context());
  107. furi_assert(instance);
  108. TimerHandle_t hTimer = (TimerHandle_t)instance;
  109. /* Return 0: not running, 1: running */
  110. return (uint32_t)xTimerIsTimerActive(hTimer);
  111. }