ets_timer_legacy.c 4.1 KB

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