rtc.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2015-2016 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. * @file rtc.h
  16. * @brief Declarations of APIs provided by librtc.a
  17. *
  18. * This file is not in the include directory of esp32 component, so it is not
  19. * part of the public API. As the source code of librtc.a is gradually moved
  20. * into the ESP-IDF, some of these APIs will be exposed to applications.
  21. *
  22. * For now, only esp_deep_sleep function declared in esp_deepsleep.h
  23. * is part of public API.
  24. */
  25. #pragma once
  26. #include <stdint.h>
  27. #include <stddef.h>
  28. #include "soc/soc.h"
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. typedef enum{
  33. XTAL_40M = 40,
  34. XTAL_26M = 26,
  35. XTAL_24M = 24,
  36. XTAL_AUTO = 0
  37. } xtal_freq_t;
  38. typedef enum{
  39. CPU_XTAL = 0,
  40. CPU_80M = 1,
  41. CPU_160M = 2,
  42. CPU_240M = 3,
  43. CPU_2M = 4
  44. } cpu_freq_t;
  45. typedef enum {
  46. CALI_RTC_MUX = 0,
  47. CALI_8MD256 = 1,
  48. CALI_32K_XTAL = 2
  49. } cali_clk_t;
  50. /**
  51. * This function must be called to initialize RTC library
  52. * @param xtal_freq Frequency of main crystal
  53. */
  54. void rtc_init_lite(xtal_freq_t xtal_freq);
  55. /**
  56. * Switch CPU frequency
  57. * @param cpu_freq new CPU frequency
  58. */
  59. void rtc_set_cpu_freq(cpu_freq_t cpu_freq);
  60. /**
  61. * @brief Return RTC slow clock's period
  62. * @param cali_clk clock to calibrate
  63. * @param slow_clk_cycles number of slow clock cycles to average
  64. * @param xtal_freq chip's main XTAL freq
  65. * @return average slow clock period in microseconds, Q13.19 fixed point format
  66. */
  67. uint32_t rtc_slowck_cali(cali_clk_t cali_clk, uint32_t slow_clk_cycles);
  68. /**
  69. * @brief Convert from microseconds to slow clock cycles
  70. * @param time_in_us_h Time in microseconds, higher 32 bit part
  71. * @param time_in_us_l Time in microseconds, lower 32 bit part
  72. * @param slow_clk_period Period of slow clock in microseconds, Q13.19 fixed point format (as returned by rtc_slowck_cali).
  73. * @param[out] cylces_h output, higher 32 bit part of number of slow clock cycles
  74. * @param[out] cycles_l output, lower 32 bit part of number of slow clock cycles
  75. */
  76. void rtc_usec2rtc(uint32_t time_in_us_h, uint32_t time_in_us_l, uint32_t slow_clk_period, uint32_t *cylces_h, uint32_t *cycles_l);
  77. #define DEEP_SLEEP_PD_NORMAL BIT(0) /*!< Base deep sleep mode */
  78. #define DEEP_SLEEP_PD_RTC_PERIPH BIT(1) /*!< Power down RTC peripherals */
  79. #define DEEP_SLEEP_PD_RTC_SLOW_MEM BIT(2) /*!< Power down RTC SLOW memory */
  80. #define DEEP_SLEEP_PD_RTC_FAST_MEM BIT(3) /*!< Power down RTC FAST memory */
  81. /**
  82. * @brief Prepare for entering sleep mode
  83. * @param deep_slp DEEP_SLEEP_PD_ flags combined with OR (DEEP_SLEEP_PD_NORMAL must be included)
  84. * @param cpu_lp_mode for deep sleep, should be 0
  85. */
  86. void rtc_slp_prep_lite(uint32_t deep_slp, uint32_t cpu_lp_mode);
  87. #define RTC_EXT_EVENT0_TRIG BIT(0)
  88. #define RTC_EXT_EVENT1_TRIG BIT(1)
  89. #define RTC_GPIO_TRIG BIT(2)
  90. #define RTC_TIMER_EXPIRE BIT(3)
  91. #define RTC_SDIO_TRIG BIT(4)
  92. #define RTC_MAC_TRIG BIT(5)
  93. #define RTC_UART0_TRIG BIT(6)
  94. #define RTC_UART1_TRIG BIT(7)
  95. #define RTC_TOUCH_TRIG BIT(8)
  96. #define RTC_SAR_TRIG BIT(9)
  97. #define RTC_BT_TRIG BIT(10)
  98. #define RTC_EXT_EVENT0_TRIG_EN RTC_EXT_EVENT0_TRIG
  99. #define RTC_EXT_EVENT1_TRIG_EN RTC_EXT_EVENT1_TRIG
  100. #define RTC_GPIO_TRIG_EN RTC_GPIO_TRIG
  101. #define RTC_TIMER_EXPIRE_EN RTC_TIMER_EXPIRE
  102. #define RTC_SDIO_TRIG_EN RTC_SDIO_TRIG
  103. #define RTC_MAC_TRIG_EN RTC_MAC_TRIG
  104. #define RTC_UART0_TRIG_EN RTC_UART0_TRIG
  105. #define RTC_UART1_TRIG_EN RTC_UART1_TRIG
  106. #define RTC_TOUCH_TRIG_EN RTC_TOUCH_TRIG
  107. #define RTC_SAR_TRIG_EN RTC_SAR_TRIG
  108. #define RTC_BT_TRIG_EN RTC_BT_TRIG
  109. /**
  110. * @brief Enter sleep mode for given number of cycles
  111. * @param cycles_h higher 32 bit part of number of slow clock cycles
  112. * @param cycles_l lower 32 bit part of number of slow clock cycles
  113. * @param wakeup_opt wake up reason to enable (RTC_xxx_EN flags combined with OR)
  114. * @param reject_opt reserved, should be 0
  115. * @return TBD
  116. */
  117. uint32_t rtc_sleep(uint32_t cycles_h, uint32_t cycles_l, uint32_t wakeup_opt, uint32_t reject_opt);
  118. #ifdef __cplusplus
  119. }
  120. #endif