ets_timer_legacy.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*
  7. * ets_timer module implements a set of legacy timer APIs which are
  8. * used by the WiFi driver. This is done on top of the newer esp_timer APIs.
  9. * Applications should not use ets_timer functions, as they may change without
  10. * notice.
  11. */
  12. #include <string.h>
  13. #include "esp_types.h"
  14. #include "esp_log.h"
  15. #include "esp_attr.h"
  16. #include "esp_intr_alloc.h"
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/task.h"
  19. #include "freertos/semphr.h"
  20. #include "sdkconfig.h"
  21. #include "esp_timer.h"
  22. // for ETSTimer type
  23. #if CONFIG_IDF_TARGET_ESP32
  24. #include "esp32/rom/ets_sys.h"
  25. #elif CONFIG_IDF_TARGET_ESP32S2
  26. #include "esp32s2/rom/ets_sys.h"
  27. #elif CONFIG_IDF_TARGET_ESP32S3
  28. #include "esp32s3/rom/ets_sys.h"
  29. #elif CONFIG_IDF_TARGET_ESP32C3
  30. #include "esp32c3/rom/ets_sys.h"
  31. #elif CONFIG_IDF_TARGET_ESP32C2
  32. #include "esp32c2/rom/ets_sys.h"
  33. #elif CONFIG_IDF_TARGET_ESP32H2
  34. #include "esp32h2/rom/ets_sys.h"
  35. #endif
  36. /* We abuse 'timer_arg' field of ETSTimer structure to hold a pointer to esp_timer */
  37. #define ESP_TIMER(p_ets_timer) ((esp_timer_handle_t) (p_ets_timer)->timer_arg)
  38. /* We abuse 'timer_expire' field of ETSTimer structure to hold a magic value
  39. * signifying that the contents of the timer was zeroed out.
  40. */
  41. #define TIMER_INITIALIZED_FIELD(p_ets_timer) ((p_ets_timer)->timer_expire)
  42. #define TIMER_INITIALIZED_VAL 0x12121212
  43. static IRAM_ATTR bool timer_initialized(ETSTimer *ptimer)
  44. {
  45. return TIMER_INITIALIZED_FIELD(ptimer) == TIMER_INITIALIZED_VAL;
  46. }
  47. void ets_timer_setfn(ETSTimer *ptimer, ETSTimerFunc *pfunction, void *parg)
  48. {
  49. if (!timer_initialized(ptimer)) {
  50. memset(ptimer, 0, sizeof(*ptimer));
  51. TIMER_INITIALIZED_FIELD(ptimer) = TIMER_INITIALIZED_VAL;
  52. }
  53. if (ESP_TIMER(ptimer) == NULL) {
  54. const esp_timer_create_args_t create_args = {
  55. .callback = pfunction,
  56. .arg = parg,
  57. .name = "ETSTimer",
  58. .dispatch_method = ESP_TIMER_TASK
  59. };
  60. ESP_ERROR_CHECK( esp_timer_create(&create_args, (esp_timer_handle_t*)&(ptimer->timer_arg)) );
  61. }
  62. }
  63. void IRAM_ATTR ets_timer_arm_us(ETSTimer *ptimer, uint32_t time_us, bool repeat_flag)
  64. {
  65. assert(timer_initialized(ptimer));
  66. esp_timer_stop(ESP_TIMER(ptimer)); // no error check
  67. if (!repeat_flag) {
  68. ESP_ERROR_CHECK( esp_timer_start_once(ESP_TIMER(ptimer), time_us) );
  69. } else {
  70. ESP_ERROR_CHECK( esp_timer_start_periodic(ESP_TIMER(ptimer), time_us) );
  71. }
  72. }
  73. void IRAM_ATTR ets_timer_arm(ETSTimer *ptimer, uint32_t time_ms, bool repeat_flag)
  74. {
  75. uint64_t time_us = 1000LL * (uint64_t) time_ms;
  76. assert(timer_initialized(ptimer));
  77. esp_timer_stop(ESP_TIMER(ptimer)); // no error check
  78. if (!repeat_flag) {
  79. ESP_ERROR_CHECK( esp_timer_start_once(ESP_TIMER(ptimer), time_us) );
  80. } else {
  81. ESP_ERROR_CHECK( esp_timer_start_periodic(ESP_TIMER(ptimer), time_us) );
  82. }
  83. }
  84. void ets_timer_done(ETSTimer *ptimer)
  85. {
  86. if (timer_initialized(ptimer)) {
  87. esp_timer_delete(ESP_TIMER(ptimer));
  88. ptimer->timer_arg = NULL;
  89. TIMER_INITIALIZED_FIELD(ptimer) = 0;
  90. }
  91. }
  92. void IRAM_ATTR ets_timer_disarm(ETSTimer *ptimer)
  93. {
  94. if (timer_initialized(ptimer)) {
  95. esp_timer_stop(ESP_TIMER(ptimer));
  96. }
  97. }
  98. void ets_timer_init(void)
  99. {
  100. }
  101. void ets_timer_deinit(void)
  102. {
  103. }
  104. void os_timer_setfn(ETSTimer *ptimer, ETSTimerFunc *pfunction, void *parg) __attribute__((alias("ets_timer_setfn")));
  105. void os_timer_disarm(ETSTimer *ptimer) __attribute__((alias("ets_timer_disarm")));
  106. void os_timer_arm_us(ETSTimer *ptimer,uint32_t u_seconds,bool repeat_flag) __attribute__((alias("ets_timer_arm_us")));
  107. void os_timer_arm(ETSTimer *ptimer,uint32_t milliseconds,bool repeat_flag) __attribute__((alias("ets_timer_arm")));
  108. void os_timer_done(ETSTimer *ptimer) __attribute__((alias("ets_timer_done")));