timer.c 3.4 KB

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