ets_timer_legacy.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_ESP32C6
  34. #include "esp32c6/rom/ets_sys.h"
  35. #elif CONFIG_IDF_TARGET_ESP32H2
  36. #include "esp32h2/rom/ets_sys.h"
  37. #endif
  38. /* We abuse 'timer_arg' field of ETSTimer structure to hold a pointer to esp_timer */
  39. #define ESP_TIMER(p_ets_timer) ((esp_timer_handle_t) (p_ets_timer)->timer_arg)
  40. /* We abuse 'timer_expire' field of ETSTimer structure to hold a magic value
  41. * signifying that the contents of the timer was zeroed out.
  42. */
  43. #define TIMER_INITIALIZED_FIELD(p_ets_timer) ((p_ets_timer)->timer_expire)
  44. #define TIMER_INITIALIZED_VAL 0x12121212
  45. static IRAM_ATTR bool timer_initialized(ETSTimer *ptimer)
  46. {
  47. return TIMER_INITIALIZED_FIELD(ptimer) == TIMER_INITIALIZED_VAL;
  48. }
  49. void ets_timer_setfn(ETSTimer *ptimer, ETSTimerFunc *pfunction, void *parg)
  50. {
  51. if (!timer_initialized(ptimer)) {
  52. memset(ptimer, 0, sizeof(*ptimer));
  53. TIMER_INITIALIZED_FIELD(ptimer) = TIMER_INITIALIZED_VAL;
  54. }
  55. if (ESP_TIMER(ptimer) == NULL) {
  56. const esp_timer_create_args_t create_args = {
  57. .callback = pfunction,
  58. .arg = parg,
  59. .name = "ETSTimer",
  60. .dispatch_method = ESP_TIMER_TASK
  61. };
  62. ESP_ERROR_CHECK( esp_timer_create(&create_args, (esp_timer_handle_t*)&(ptimer->timer_arg)) );
  63. }
  64. }
  65. void IRAM_ATTR ets_timer_arm_us(ETSTimer *ptimer, uint32_t time_us, bool repeat_flag)
  66. {
  67. assert(timer_initialized(ptimer));
  68. esp_timer_stop(ESP_TIMER(ptimer)); // no error check
  69. if (!repeat_flag) {
  70. ESP_ERROR_CHECK( esp_timer_start_once(ESP_TIMER(ptimer), time_us) );
  71. } else {
  72. ESP_ERROR_CHECK( esp_timer_start_periodic(ESP_TIMER(ptimer), time_us) );
  73. }
  74. }
  75. void IRAM_ATTR ets_timer_arm(ETSTimer *ptimer, uint32_t time_ms, bool repeat_flag)
  76. {
  77. uint64_t time_us = 1000LL * (uint64_t) time_ms;
  78. assert(timer_initialized(ptimer));
  79. esp_timer_stop(ESP_TIMER(ptimer)); // no error check
  80. if (!repeat_flag) {
  81. ESP_ERROR_CHECK( esp_timer_start_once(ESP_TIMER(ptimer), time_us) );
  82. } else {
  83. ESP_ERROR_CHECK( esp_timer_start_periodic(ESP_TIMER(ptimer), time_us) );
  84. }
  85. }
  86. void ets_timer_done(ETSTimer *ptimer)
  87. {
  88. if (timer_initialized(ptimer)) {
  89. esp_timer_delete(ESP_TIMER(ptimer));
  90. ptimer->timer_arg = NULL;
  91. TIMER_INITIALIZED_FIELD(ptimer) = 0;
  92. }
  93. }
  94. void IRAM_ATTR ets_timer_disarm(ETSTimer *ptimer)
  95. {
  96. if (timer_initialized(ptimer)) {
  97. esp_timer_stop(ESP_TIMER(ptimer));
  98. }
  99. }
  100. void ets_timer_init(void)
  101. {
  102. }
  103. void ets_timer_deinit(void)
  104. {
  105. }
  106. void os_timer_setfn(ETSTimer *ptimer, ETSTimerFunc *pfunction, void *parg) __attribute__((alias("ets_timer_setfn")));
  107. void os_timer_disarm(ETSTimer *ptimer) __attribute__((alias("ets_timer_disarm")));
  108. void os_timer_arm_us(ETSTimer *ptimer,uint32_t u_seconds,bool repeat_flag) __attribute__((alias("ets_timer_arm_us")));
  109. void os_timer_arm(ETSTimer *ptimer,uint32_t milliseconds,bool repeat_flag) __attribute__((alias("ets_timer_arm")));
  110. void os_timer_done(ETSTimer *ptimer) __attribute__((alias("ets_timer_done")));