gptimer.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. // #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG // uncomment this line to enable debug logs
  7. #include <stdlib.h>
  8. #include <sys/lock.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "esp_attr.h"
  11. #include "esp_err.h"
  12. #include "esp_heap_caps.h"
  13. #include "esp_intr_alloc.h"
  14. #include "esp_log.h"
  15. #include "esp_check.h"
  16. #include "esp_pm.h"
  17. #include "driver/gptimer.h"
  18. #include "hal/timer_types.h"
  19. #include "hal/timer_hal.h"
  20. #include "hal/timer_ll.h"
  21. #include "soc/timer_periph.h"
  22. #include "soc/soc_memory_types.h"
  23. #include "esp_private/periph_ctrl.h"
  24. #include "esp_private/esp_clk.h"
  25. // If ISR handler is allowed to run whilst cache is disabled,
  26. // Make sure all the code and related variables used by the handler are in the SRAM
  27. #if CONFIG_GPTIMER_ISR_IRAM_SAFE
  28. #define GPTIMER_INTR_ALLOC_FLAGS (ESP_INTR_FLAG_IRAM | ESP_INTR_FLAG_INTRDISABLED)
  29. #define GPTIMER_MEM_ALLOC_CAPS (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)
  30. #else
  31. #define GPTIMER_INTR_ALLOC_FLAGS ESP_INTR_FLAG_INTRDISABLED
  32. #define GPTIMER_MEM_ALLOC_CAPS MALLOC_CAP_DEFAULT
  33. #endif //CONFIG_GPTIMER_ISR_IRAM_SAFE
  34. #define GPTIMER_PM_LOCK_NAME_LEN_MAX 16
  35. static const char *TAG = "gptimer";
  36. typedef struct gptimer_platform_t gptimer_platform_t;
  37. typedef struct gptimer_group_t gptimer_group_t;
  38. typedef struct gptimer_t gptimer_t;
  39. struct gptimer_platform_t {
  40. _lock_t mutex; // platform level mutex lock
  41. gptimer_group_t *groups[SOC_TIMER_GROUPS]; // timer group pool
  42. int group_ref_counts[SOC_TIMER_GROUPS]; // reference count used to protect group install/uninstall
  43. };
  44. struct gptimer_group_t {
  45. int group_id;
  46. portMUX_TYPE spinlock; // to protect per-group register level concurrent access
  47. gptimer_t *timers[SOC_TIMER_GROUP_TIMERS_PER_GROUP];
  48. };
  49. typedef enum {
  50. GPTIMER_FSM_STOP,
  51. GPTIMER_FSM_START,
  52. } gptimer_lifecycle_fsm_t;
  53. struct gptimer_t {
  54. gptimer_group_t *group;
  55. int timer_id;
  56. unsigned int resolution_hz;
  57. unsigned long long reload_count;
  58. unsigned long long alarm_count;
  59. gptimer_count_direction_t direction;
  60. timer_hal_context_t hal;
  61. gptimer_lifecycle_fsm_t fsm; // access to fsm should be protect by spinlock, as fsm is also accessed from ISR handler
  62. intr_handle_t intr;
  63. portMUX_TYPE spinlock; // to protect per-timer resources concurent accessed by task and ISR handler
  64. gptimer_alarm_cb_t on_alarm;
  65. void *user_ctx;
  66. esp_pm_lock_handle_t pm_lock; // power management lock
  67. #if CONFIG_PM_ENABLE
  68. char pm_lock_name[GPTIMER_PM_LOCK_NAME_LEN_MAX]; // pm lock name
  69. #endif
  70. struct {
  71. uint32_t intr_shared: 1;
  72. uint32_t auto_reload_on_alarm: 1;
  73. uint32_t alarm_en: 1;
  74. } flags;
  75. };
  76. // gptimer driver platform, it's always a singleton
  77. static gptimer_platform_t s_platform;
  78. static gptimer_group_t *gptimer_acquire_group_handle(int group_id);
  79. static void gptimer_release_group_handle(gptimer_group_t *group);
  80. static esp_err_t gptimer_select_periph_clock(gptimer_t *timer, gptimer_clock_source_t src_clk, uint32_t resolution_hz);
  81. IRAM_ATTR static void gptimer_default_isr(void *args);
  82. esp_err_t gptimer_new_timer(const gptimer_config_t *config, gptimer_handle_t *ret_timer)
  83. {
  84. esp_err_t ret = ESP_OK;
  85. gptimer_group_t *group = NULL;
  86. gptimer_t *timer = NULL;
  87. int group_id = -1;
  88. int timer_id = -1;
  89. ESP_GOTO_ON_FALSE(config && ret_timer, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
  90. ESP_GOTO_ON_FALSE(config->resolution_hz, ESP_ERR_INVALID_ARG, err, TAG, "invalid timer resolution:%d", config->resolution_hz);
  91. timer = heap_caps_calloc(1, sizeof(gptimer_t), GPTIMER_MEM_ALLOC_CAPS);
  92. ESP_GOTO_ON_FALSE(timer, ESP_ERR_NO_MEM, err, TAG, "no mem for gptimer");
  93. for (int i = 0; (i < SOC_TIMER_GROUPS) && (timer_id < 0); i++) {
  94. group = gptimer_acquire_group_handle(i);
  95. ESP_GOTO_ON_FALSE(group, ESP_ERR_NO_MEM, err, TAG, "no mem for group (%d)", i);
  96. // loop to search free timer in the group
  97. portENTER_CRITICAL(&group->spinlock);
  98. for (int j = 0; j < SOC_TIMER_GROUP_TIMERS_PER_GROUP; j++) {
  99. if (!group->timers[j]) {
  100. group_id = i;
  101. timer_id = j;
  102. group->timers[j] = timer;
  103. break;
  104. }
  105. }
  106. portEXIT_CRITICAL(&group->spinlock);
  107. if (timer_id < 0) {
  108. gptimer_release_group_handle(group);
  109. group = NULL;
  110. }
  111. }
  112. ESP_GOTO_ON_FALSE(timer_id != -1, ESP_ERR_NOT_FOUND, err, TAG, "no free timer");
  113. timer->timer_id = timer_id;
  114. timer->group = group;
  115. // initialize HAL layer
  116. timer_hal_init(&timer->hal, group_id, timer_id);
  117. // stop counter, alarm, auto-reload
  118. timer_ll_enable_counter(timer->hal.dev, timer_id, false);
  119. timer_ll_enable_auto_reload(timer->hal.dev, timer_id, false);
  120. timer_ll_enable_alarm(timer->hal.dev, timer_id, false);
  121. // select clock source, set clock resolution
  122. ESP_GOTO_ON_ERROR(gptimer_select_periph_clock(timer, config->clk_src, config->resolution_hz), err, TAG, "set periph clock failed");
  123. // initialize counter value to zero
  124. timer_hal_set_counter_value(&timer->hal, 0);
  125. // set counting direction
  126. timer_ll_set_count_direction(timer->hal.dev, timer_id, config->direction);
  127. // interrupt register is shared by all timers in the same group
  128. portENTER_CRITICAL(&group->spinlock);
  129. timer_ll_enable_intr(timer->hal.dev, TIMER_LL_EVENT_ALARM(timer_id), false); // disable interrupt
  130. timer_ll_clear_intr_status(timer->hal.dev, TIMER_LL_EVENT_ALARM(timer_id)); // clear pending interrupt event
  131. portEXIT_CRITICAL(&group->spinlock);
  132. // initialize other members of timer
  133. timer->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
  134. timer->fsm = GPTIMER_FSM_STOP;
  135. timer->direction = config->direction;
  136. timer->flags.intr_shared = config->flags.intr_shared;
  137. ESP_LOGD(TAG, "new gptimer (%d,%d) at %p, resolution=%uHz", group_id, timer_id, timer, timer->resolution_hz);
  138. *ret_timer = timer;
  139. return ESP_OK;
  140. err:
  141. if (timer) {
  142. if (timer->pm_lock) {
  143. esp_pm_lock_delete(timer->pm_lock);
  144. }
  145. free(timer);
  146. }
  147. if (group) {
  148. gptimer_release_group_handle(group);
  149. }
  150. return ret;
  151. }
  152. esp_err_t gptimer_del_timer(gptimer_handle_t timer)
  153. {
  154. gptimer_group_t *group = NULL;
  155. bool valid_state = true;
  156. ESP_RETURN_ON_FALSE(timer, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  157. portENTER_CRITICAL(&timer->spinlock);
  158. if (timer->fsm != GPTIMER_FSM_STOP) {
  159. valid_state = false;
  160. }
  161. portEXIT_CRITICAL(&timer->spinlock);
  162. ESP_RETURN_ON_FALSE(valid_state, ESP_ERR_INVALID_STATE, TAG, "can't delete timer as it's not in stop state");
  163. group = timer->group;
  164. int group_id = group->group_id;
  165. int timer_id = timer->timer_id;
  166. if (timer->intr) {
  167. esp_intr_free(timer->intr);
  168. ESP_LOGD(TAG, "uninstall interrupt service for timer (%d,%d)", group_id, timer_id);
  169. }
  170. if (timer->pm_lock) {
  171. esp_pm_lock_delete(timer->pm_lock);
  172. ESP_LOGD(TAG, "uninstall APB_FREQ_MAX lock for timer (%d,%d)", group_id, timer_id);
  173. }
  174. free(timer);
  175. ESP_LOGD(TAG, "del timer (%d,%d)", group_id, timer_id);
  176. portENTER_CRITICAL(&group->spinlock);
  177. group->timers[timer_id] = NULL;
  178. portEXIT_CRITICAL(&group->spinlock);
  179. // timer has a reference on group, release it now
  180. gptimer_release_group_handle(group);
  181. return ESP_OK;
  182. }
  183. esp_err_t gptimer_set_raw_count(gptimer_handle_t timer, unsigned long long value)
  184. {
  185. ESP_RETURN_ON_FALSE(timer, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  186. portENTER_CRITICAL_SAFE(&timer->spinlock);
  187. timer_hal_set_counter_value(&timer->hal, value);
  188. portEXIT_CRITICAL_SAFE(&timer->spinlock);
  189. return ESP_OK;
  190. }
  191. esp_err_t gptimer_get_raw_count(gptimer_handle_t timer, unsigned long long *value)
  192. {
  193. ESP_RETURN_ON_FALSE(timer && value, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  194. portENTER_CRITICAL_SAFE(&timer->spinlock);
  195. *value = timer_ll_get_counter_value(timer->hal.dev, timer->timer_id);
  196. portEXIT_CRITICAL_SAFE(&timer->spinlock);
  197. return ESP_OK;
  198. }
  199. esp_err_t gptimer_register_event_callbacks(gptimer_handle_t timer, const gptimer_event_callbacks_t *cbs, void *user_data)
  200. {
  201. gptimer_group_t *group = NULL;
  202. ESP_RETURN_ON_FALSE(timer && cbs, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  203. group = timer->group;
  204. int group_id = group->group_id;
  205. int timer_id = timer->timer_id;
  206. #if CONFIG_GPTIMER_ISR_IRAM_SAFE
  207. if (cbs->on_alarm) {
  208. ESP_RETURN_ON_FALSE(esp_ptr_in_iram(cbs->on_alarm), ESP_ERR_INVALID_ARG, TAG, "on_alarm callback not in IRAM");
  209. }
  210. if (user_data) {
  211. ESP_RETURN_ON_FALSE(esp_ptr_in_dram(user_data) ||
  212. esp_ptr_in_diram_dram(user_data) ||
  213. esp_ptr_in_rtc_dram_fast(user_data), ESP_ERR_INVALID_ARG, TAG, "user context not in DRAM");
  214. }
  215. #endif
  216. // lazy install interrupt service
  217. if (!timer->intr) {
  218. // if user wants to control the interrupt allocation more precisely, we can expose more flags in `gptimer_config_t`
  219. int isr_flags = timer->flags.intr_shared ? ESP_INTR_FLAG_SHARED | GPTIMER_INTR_ALLOC_FLAGS : GPTIMER_INTR_ALLOC_FLAGS;
  220. ESP_RETURN_ON_ERROR(esp_intr_alloc_intrstatus(timer_group_periph_signals.groups[group_id].timer_irq_id[timer_id], isr_flags,
  221. (uint32_t)timer_ll_get_intr_status_reg(timer->hal.dev), TIMER_LL_EVENT_ALARM(timer_id),
  222. gptimer_default_isr, timer, &timer->intr), TAG, "install interrupt service failed");
  223. }
  224. // enable/disable GPTimer interrupt events
  225. portENTER_CRITICAL_SAFE(&group->spinlock);
  226. timer_ll_enable_intr(timer->hal.dev, TIMER_LL_EVENT_ALARM(timer->timer_id), cbs->on_alarm != NULL); // enable timer interrupt
  227. portEXIT_CRITICAL_SAFE(&group->spinlock);
  228. timer->on_alarm = cbs->on_alarm;
  229. timer->user_ctx = user_data;
  230. return ESP_OK;
  231. }
  232. esp_err_t gptimer_set_alarm_action(gptimer_handle_t timer, const gptimer_alarm_config_t *config)
  233. {
  234. ESP_RETURN_ON_FALSE(timer, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  235. if (config) {
  236. // When auto_reload is enabled, alarm_count should not be equal to reload_count
  237. bool valid_auto_reload = !config->flags.auto_reload_on_alarm || config->alarm_count != config->reload_count;
  238. ESP_RETURN_ON_FALSE(valid_auto_reload, ESP_ERR_INVALID_ARG, TAG, "reload count can't equal to alarm count");
  239. timer->reload_count = config->reload_count;
  240. timer->alarm_count = config->alarm_count;
  241. timer->flags.auto_reload_on_alarm = config->flags.auto_reload_on_alarm;
  242. timer->flags.alarm_en = true;
  243. portENTER_CRITICAL_SAFE(&timer->spinlock);
  244. timer_ll_set_reload_value(timer->hal.dev, timer->timer_id, config->reload_count);
  245. timer_ll_set_alarm_value(timer->hal.dev, timer->timer_id, config->alarm_count);
  246. portEXIT_CRITICAL_SAFE(&timer->spinlock);
  247. } else {
  248. timer->flags.auto_reload_on_alarm = false;
  249. timer->flags.alarm_en = false;
  250. }
  251. portENTER_CRITICAL_SAFE(&timer->spinlock);
  252. timer_ll_enable_auto_reload(timer->hal.dev, timer->timer_id, timer->flags.auto_reload_on_alarm);
  253. timer_ll_enable_alarm(timer->hal.dev, timer->timer_id, timer->flags.alarm_en);
  254. portEXIT_CRITICAL_SAFE(&timer->spinlock);
  255. return ESP_OK;
  256. }
  257. esp_err_t gptimer_start(gptimer_handle_t timer)
  258. {
  259. ESP_RETURN_ON_FALSE(timer, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  260. // acquire power manager lock
  261. if (timer->pm_lock) {
  262. ESP_RETURN_ON_ERROR(esp_pm_lock_acquire(timer->pm_lock), TAG, "acquire APB_FREQ_MAX lock failed");
  263. }
  264. // interrupt interupt service
  265. if (timer->intr) {
  266. ESP_RETURN_ON_ERROR(esp_intr_enable(timer->intr), TAG, "enable interrupt service failed");
  267. }
  268. portENTER_CRITICAL_SAFE(&timer->spinlock);
  269. timer_ll_enable_counter(timer->hal.dev, timer->timer_id, true);
  270. timer_ll_enable_alarm(timer->hal.dev, timer->timer_id, timer->flags.alarm_en);
  271. timer->fsm = GPTIMER_FSM_START;
  272. portEXIT_CRITICAL_SAFE(&timer->spinlock);
  273. return ESP_OK;
  274. }
  275. esp_err_t gptimer_stop(gptimer_handle_t timer)
  276. {
  277. ESP_RETURN_ON_FALSE(timer, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  278. // disable counter, alarm, autoreload
  279. portENTER_CRITICAL_SAFE(&timer->spinlock);
  280. timer_ll_enable_counter(timer->hal.dev, timer->timer_id, false);
  281. timer_ll_enable_alarm(timer->hal.dev, timer->timer_id, false);
  282. timer->fsm = GPTIMER_FSM_STOP;
  283. portEXIT_CRITICAL_SAFE(&timer->spinlock);
  284. // disable interrupt service
  285. if (timer->intr) {
  286. ESP_RETURN_ON_ERROR(esp_intr_disable(timer->intr), TAG, "disable interrupt service failed");
  287. }
  288. // release power manager lock
  289. if (timer->pm_lock) {
  290. ESP_RETURN_ON_ERROR(esp_pm_lock_release(timer->pm_lock), TAG, "release APB_FREQ_MAX lock failed");
  291. }
  292. return ESP_OK;
  293. }
  294. static gptimer_group_t *gptimer_acquire_group_handle(int group_id)
  295. {
  296. // esp_log_level_set(TAG, ESP_LOG_DEBUG);
  297. bool new_group = false;
  298. gptimer_group_t *group = NULL;
  299. // prevent install timer group concurrently
  300. _lock_acquire(&s_platform.mutex);
  301. if (!s_platform.groups[group_id]) {
  302. group = heap_caps_calloc(1, sizeof(gptimer_group_t), GPTIMER_MEM_ALLOC_CAPS);
  303. if (group) {
  304. new_group = true;
  305. s_platform.groups[group_id] = group;
  306. // initialize timer group members
  307. group->group_id = group_id;
  308. group->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
  309. // enable APB access timer registers
  310. periph_module_enable(timer_group_periph_signals.groups[group_id].module);
  311. }
  312. } else {
  313. group = s_platform.groups[group_id];
  314. }
  315. if (group) {
  316. // someone acquired the group handle means we have a new object that refer to this group
  317. s_platform.group_ref_counts[group_id]++;
  318. }
  319. _lock_release(&s_platform.mutex);
  320. if (new_group) {
  321. ESP_LOGD(TAG, "new group (%d) @%p", group_id, group);
  322. }
  323. return group;
  324. }
  325. static void gptimer_release_group_handle(gptimer_group_t *group)
  326. {
  327. int group_id = group->group_id;
  328. bool do_deinitialize = false;
  329. _lock_acquire(&s_platform.mutex);
  330. s_platform.group_ref_counts[group_id]--;
  331. if (s_platform.group_ref_counts[group_id] == 0) {
  332. assert(s_platform.groups[group_id]);
  333. do_deinitialize = true;
  334. s_platform.groups[group_id] = NULL;
  335. // Theoretically we need to disable the peripheral clock for the timer group
  336. // However, next time when we enable the peripheral again, the registers will be reset to default value, including the watchdog registers inside the group
  337. // Then the watchdog will go into reset state, e.g. the flash boot watchdog is enabled again and reset the system very soon
  338. // periph_module_disable(timer_group_periph_signals.groups[group_id].module);
  339. }
  340. _lock_release(&s_platform.mutex);
  341. if (do_deinitialize) {
  342. free(group);
  343. ESP_LOGD(TAG, "del group (%d)", group_id);
  344. }
  345. }
  346. static esp_err_t gptimer_select_periph_clock(gptimer_t *timer, gptimer_clock_source_t src_clk, uint32_t resolution_hz)
  347. {
  348. unsigned int counter_src_hz = 0;
  349. esp_err_t ret = ESP_OK;
  350. int timer_id = timer->timer_id;
  351. switch (src_clk) {
  352. case GPTIMER_CLK_SRC_APB:
  353. counter_src_hz = esp_clk_apb_freq();
  354. #if CONFIG_PM_ENABLE
  355. sprintf(timer->pm_lock_name, "gptimer_%d_%d", timer->group->group_id, timer_id); // e.g. gptimer_0_0
  356. ret = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, timer->pm_lock_name, &timer->pm_lock);
  357. ESP_RETURN_ON_ERROR(ret, TAG, "create APB_FREQ_MAX lock failed");
  358. ESP_LOGD(TAG, "install APB_FREQ_MAX lock for timer (%d,%d)", timer->group->group_id, timer_id);
  359. #endif
  360. break;
  361. #if SOC_TIMER_GROUP_SUPPORT_XTAL
  362. case GPTIMER_CLK_SRC_XTAL:
  363. counter_src_hz = esp_clk_xtal_freq();
  364. break;
  365. #endif
  366. default:
  367. ESP_RETURN_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, TAG, "clock source %d is not support", src_clk);
  368. break;
  369. }
  370. timer_ll_set_clock_source(timer->hal.dev, timer_id, src_clk);
  371. unsigned int prescale = counter_src_hz / resolution_hz; // potential resolution loss here
  372. timer_ll_set_clock_prescale(timer->hal.dev, timer_id, prescale);
  373. timer->resolution_hz = counter_src_hz / prescale; // this is the real resolution
  374. if (timer->resolution_hz != resolution_hz) {
  375. ESP_LOGW(TAG, "resolution lost, expect %ul, real %ul", resolution_hz, timer->resolution_hz);
  376. }
  377. return ret;
  378. }
  379. // Put the default ISR handler in the IRAM for better performance
  380. IRAM_ATTR static void gptimer_default_isr(void *args)
  381. {
  382. bool need_yield = false;
  383. gptimer_t *timer = (gptimer_t *)args;
  384. gptimer_group_t *group = timer->group;
  385. gptimer_alarm_cb_t on_alarm_cb = timer->on_alarm;
  386. uint32_t intr_status = timer_ll_get_intr_status(timer->hal.dev);
  387. if (intr_status & TIMER_LL_EVENT_ALARM(timer->timer_id)) {
  388. // Note: when alarm event happends, the alarm will be disabled automatically by hardware
  389. gptimer_alarm_event_data_t edata = {
  390. .count_value = timer_ll_get_counter_value(timer->hal.dev, timer->timer_id),
  391. .alarm_value = timer->alarm_count,
  392. };
  393. portENTER_CRITICAL_ISR(&group->spinlock);
  394. timer_ll_clear_intr_status(timer->hal.dev, TIMER_LL_EVENT_ALARM(timer->timer_id));
  395. // for auto-reload, we need to re-enable the alarm manually
  396. if (timer->flags.auto_reload_on_alarm) {
  397. timer_ll_enable_alarm(timer->hal.dev, timer->timer_id, true);
  398. }
  399. portEXIT_CRITICAL_ISR(&group->spinlock);
  400. if (on_alarm_cb) {
  401. if (on_alarm_cb(timer, &edata, timer->user_ctx)) {
  402. need_yield = true;
  403. }
  404. }
  405. }
  406. if (need_yield) {
  407. portYIELD_FROM_ISR();
  408. }
  409. }
  410. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  411. ///// The Following APIs are for internal use only (e.g. unit test) /////////////////////////////////////////////////
  412. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  413. esp_err_t gptimer_get_intr_handle(gptimer_handle_t timer, intr_handle_t *ret_intr_handle)
  414. {
  415. ESP_RETURN_ON_FALSE(timer && ret_intr_handle, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  416. *ret_intr_handle = timer->intr;
  417. return ESP_OK;
  418. }
  419. esp_err_t gptimer_get_pm_lock(gptimer_handle_t timer, esp_pm_lock_handle_t *ret_pm_lock)
  420. {
  421. ESP_RETURN_ON_FALSE(timer && ret_pm_lock, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  422. *ret_pm_lock = timer->pm_lock;
  423. return ESP_OK;
  424. }
  425. /**
  426. * @brief This function will be called during start up, to check that gptimer driver is not running along with the legacy timer group driver
  427. */
  428. __attribute__((constructor))
  429. static void check_gptimer_driver_conflict(void)
  430. {
  431. extern int timer_group_driver_init_count;
  432. timer_group_driver_init_count++;
  433. if (timer_group_driver_init_count > 1) {
  434. ESP_EARLY_LOGE(TAG, "CONFLICT! The gptimer driver can't work along with the legacy timer group driver");
  435. abort();
  436. }
  437. }