ets_timer_legacy.c 4.0 KB

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