ets_timer_legacy.c 4.0 KB

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