esp_phy_init.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #pragma once
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include "esp_err.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /**
  22. * @file PHY init parameters and API
  23. */
  24. /**
  25. * @brief Structure holding PHY init parameters
  26. */
  27. typedef struct {
  28. uint8_t param_ver_id; /*!< init_data structure version */
  29. uint8_t crystal_select; /*!< 0: 40MHz, 1: 26 MHz, 2: 24 MHz, 3: auto */
  30. uint8_t wifi_rx_gain_swp_step_1; /*!< do not change */
  31. uint8_t wifi_rx_gain_swp_step_2; /*!< do not change */
  32. uint8_t wifi_rx_gain_swp_step_3; /*!< do not change */
  33. uint8_t wifi_rx_gain_swp_step_4; /*!< do not change */
  34. uint8_t wifi_rx_gain_swp_step_5; /*!< do not change */
  35. uint8_t wifi_rx_gain_swp_step_6; /*!< do not change */
  36. uint8_t wifi_rx_gain_swp_step_7; /*!< do not change */
  37. uint8_t wifi_rx_gain_swp_step_8; /*!< do not change */
  38. uint8_t wifi_rx_gain_swp_step_9; /*!< do not change */
  39. uint8_t wifi_rx_gain_swp_step_10; /*!< do not change */
  40. uint8_t wifi_rx_gain_swp_step_11; /*!< do not change */
  41. uint8_t wifi_rx_gain_swp_step_12; /*!< do not change */
  42. uint8_t wifi_rx_gain_swp_step_13; /*!< do not change */
  43. uint8_t wifi_rx_gain_swp_step_14; /*!< do not change */
  44. uint8_t wifi_rx_gain_swp_step_15; /*!< do not change */
  45. uint8_t bt_rx_gain_swp_step_1; /*!< do not change */
  46. uint8_t bt_rx_gain_swp_step_2; /*!< do not change */
  47. uint8_t bt_rx_gain_swp_step_3; /*!< do not change */
  48. uint8_t bt_rx_gain_swp_step_4; /*!< do not change */
  49. uint8_t bt_rx_gain_swp_step_5; /*!< do not change */
  50. uint8_t bt_rx_gain_swp_step_6; /*!< do not change */
  51. uint8_t bt_rx_gain_swp_step_7; /*!< do not change */
  52. uint8_t bt_rx_gain_swp_step_8; /*!< do not change */
  53. uint8_t bt_rx_gain_swp_step_9; /*!< do not change */
  54. uint8_t bt_rx_gain_swp_step_10; /*!< do not change */
  55. uint8_t bt_rx_gain_swp_step_11; /*!< do not change */
  56. uint8_t bt_rx_gain_swp_step_12; /*!< do not change */
  57. uint8_t bt_rx_gain_swp_step_13; /*!< do not change */
  58. uint8_t bt_rx_gain_swp_step_14; /*!< do not change */
  59. uint8_t bt_rx_gain_swp_step_15; /*!< do not change */
  60. uint8_t gain_cmp_1; /*!< do not change */
  61. uint8_t gain_cmp_6; /*!< do not change */
  62. uint8_t gain_cmp_11; /*!< do not change */
  63. uint8_t gain_cmp_ext2_1; /*!< do not change */
  64. uint8_t gain_cmp_ext2_6; /*!< do not change */
  65. uint8_t gain_cmp_ext2_11; /*!< do not change */
  66. uint8_t gain_cmp_ext3_1; /*!< do not change */
  67. uint8_t gain_cmp_ext3_6; /*!< do not change */
  68. uint8_t gain_cmp_ext3_11; /*!< do not change */
  69. uint8_t gain_cmp_bt_ofs_1; /*!< do not change */
  70. uint8_t gain_cmp_bt_ofs_6; /*!< do not change */
  71. uint8_t gain_cmp_bt_ofs_11; /*!< do not change */
  72. uint8_t target_power_qdb_0; /*!< 78 means target power is 78/4=19.5dbm */
  73. uint8_t target_power_qdb_1; /*!< 76 means target power is 76/4=19dbm */
  74. uint8_t target_power_qdb_2; /*!< 74 means target power is 74/4=18.5dbm */
  75. uint8_t target_power_qdb_3; /*!< 68 means target power is 68/4=17dbm */
  76. uint8_t target_power_qdb_4; /*!< 64 means target power is 64/4=16dbm */
  77. uint8_t target_power_qdb_5; /*!< 52 means target power is 52/4=13dbm */
  78. uint8_t target_power_index_mcs0; /*!< target power index is 0, means target power is target_power_qdb_0 19.5dbm; (1m,2m,5.5m,11m,6m,9m) */
  79. uint8_t target_power_index_mcs1; /*!< target power index is 0, means target power is target_power_qdb_0 19.5dbm; (12m) */
  80. uint8_t target_power_index_mcs2; /*!< target power index is 1, means target power is target_power_qdb_1 19dbm; (18m) */
  81. uint8_t target_power_index_mcs3; /*!< target power index is 1, means target power is target_power_qdb_1 19dbm; (24m) */
  82. uint8_t target_power_index_mcs4; /*!< target power index is 2, means target power is target_power_qdb_2 18.5dbm; (36m) */
  83. uint8_t target_power_index_mcs5; /*!< target power index is 3, means target power is target_power_qdb_3 17dbm; (48m) */
  84. uint8_t target_power_index_mcs6; /*!< target power index is 4, means target power is target_power_qdb_4 16dbm; (54m) */
  85. uint8_t target_power_index_mcs7; /*!< target power index is 5, means target power is target_power_qdb_5 13dbm */
  86. uint8_t pwr_ind_11b_en; /*!< 0: 11b power is same as mcs0 and 6m, 1: 11b power different with OFDM */
  87. uint8_t pwr_ind_11b_0; /*!< 1m, 2m power index [0~5] */
  88. uint8_t pwr_ind_11b_1; /*!< 5.5m, 11m power index [0~5] */
  89. uint8_t chan_backoff_en; /*!< 0: channel backoff disable, 1:channel backoff enable */
  90. uint8_t chan1_power_backoff_qdb; /*!< 4 means backoff is 1db */
  91. uint8_t chan2_power_backoff_qdb; /*!< see chan1_power_backoff_qdb */
  92. uint8_t chan3_power_backoff_qdb; /*!< chan1_power_backoff_qdb */
  93. uint8_t chan4_power_backoff_qdb; /*!< chan1_power_backoff_qdb */
  94. uint8_t chan5_power_backoff_qdb; /*!< chan1_power_backoff_qdb */
  95. uint8_t chan6_power_backoff_qdb; /*!< chan1_power_backoff_qdb */
  96. uint8_t chan7_power_backoff_qdb; /*!< chan1_power_backoff_qdb */
  97. uint8_t chan8_power_backoff_qdb; /*!< chan1_power_backoff_qdb */
  98. uint8_t chan9_power_backoff_qdb; /*!< chan1_power_backoff_qdb */
  99. uint8_t chan10_power_backoff_qdb; /*!< chan1_power_backoff_qdb */
  100. uint8_t chan11_power_backoff_qdb; /*!< chan1_power_backoff_qdb */
  101. uint8_t chan12_power_backoff_qdb; /*!< chan1_power_backoff_qdb */
  102. uint8_t chan13_power_backoff_qdb; /*!< chan1_power_backoff_qdb */
  103. uint8_t chan14_power_backoff_qdb; /*!< chan1_power_backoff_qdb */
  104. uint8_t chan1_rate_backoff_index; /*!< if bit i is set, backoff data rate is target_power_qdb_i */
  105. uint8_t chan2_rate_backoff_index; /*!< see chan1_rate_backoff_index */
  106. uint8_t chan3_rate_backoff_index; /*!< see chan1_rate_backoff_index */
  107. uint8_t chan4_rate_backoff_index; /*!< see chan1_rate_backoff_index */
  108. uint8_t chan5_rate_backoff_index; /*!< see chan1_rate_backoff_index */
  109. uint8_t chan6_rate_backoff_index; /*!< see chan1_rate_backoff_index */
  110. uint8_t chan7_rate_backoff_index; /*!< see chan1_rate_backoff_index */
  111. uint8_t chan8_rate_backoff_index; /*!< see chan1_rate_backoff_index */
  112. uint8_t chan9_rate_backoff_index; /*!< see chan1_rate_backoff_index */
  113. uint8_t chan10_rate_backoff_index; /*!< see chan1_rate_backoff_index */
  114. uint8_t chan11_rate_backoff_index; /*!< see chan1_rate_backoff_index */
  115. uint8_t chan12_rate_backoff_index; /*!< see chan1_rate_backoff_index */
  116. uint8_t chan13_rate_backoff_index; /*!< see chan1_rate_backoff_index */
  117. uint8_t chan14_rate_backoff_index; /*!< see chan1_rate_backoff_index */
  118. uint8_t spur_freq_cfg_msb_1; /*!< first spur: */
  119. uint8_t spur_freq_cfg_1; /*!< spur_freq_cfg = (spur_freq_cfg_msb_1 <<8) | spur_freq_cfg_1 */
  120. uint8_t spur_freq_cfg_div_1; /*!< spur_freq=spur_freq_cfg/spur_freq_cfg_div_1 */
  121. uint8_t spur_freq_en_h_1; /*!< the seventh bit for total enable */
  122. uint8_t spur_freq_en_l_1; /*!< each bit for 1 channel, and use [spur_freq_en_h, spur_freq_en_l] to select the spur's channel priority */
  123. uint8_t spur_freq_cfg_msb_2; /*!< second spur: */
  124. uint8_t spur_freq_cfg_2; /*!< spur_freq_cfg = (spur_freq_cfg_msb_2 <<8) | spur_freq_cfg_2 */
  125. uint8_t spur_freq_cfg_div_2; /*!< spur_freq=spur_freq_cfg/spur_freq_cfg_div_2 */
  126. uint8_t spur_freq_en_h_2; /*!< the seventh bit for total enable */
  127. uint8_t spur_freq_en_l_2; /*!< each bit for 1 channel, and use [spur_freq_en_h, spur_freq_en_l] to select the spur's channel priority */
  128. uint8_t spur_freq_cfg_msb_3; /*!< third spur: */
  129. uint8_t spur_freq_cfg_3; /*!< spur_freq_cfg = (spur_freq_cfg_msb_3 <<8) | spur_freq_cfg_3 */
  130. uint8_t spur_freq_cfg_div_3; /*!< spur_freq=spur_freq_cfg/spur_freq_cfg_div_3 */
  131. uint8_t spur_freq_en_h_3; /*!< the seventh bit for total enable */
  132. uint8_t spur_freq_en_l_3; /*!< each bit for 1 channel, and use [spur_freq_en_h, spur_freq_en_l] to select the spur's channel priority, */
  133. uint8_t reserved[23]; /*!< reserved for future expansion */
  134. } esp_phy_init_data_t;
  135. /**
  136. * @brief Opaque PHY calibration data
  137. */
  138. typedef struct {
  139. uint8_t opaque[1904]; /*!< calibration data */
  140. } esp_phy_calibration_data_t;
  141. typedef enum {
  142. PHY_RF_CAL_PARTIAL = 0x00000000, /*!< Do part of RF calibration. This should be used after power-on reset. */
  143. PHY_RF_CAL_NONE = 0x00000001, /*!< Don't do any RF calibration. This mode is only suggested to be used after deep sleep reset. */
  144. PHY_RF_CAL_FULL = 0x00000002 /*!< Do full RF calibration. Produces best results, but also consumes a lot of time and current. Suggested to be used once. */
  145. } esp_phy_calibration_mode_t;
  146. /**
  147. * @brief Get PHY init data
  148. *
  149. * If "Use a partition to store PHY init data" option is set in menuconfig,
  150. * This function will load PHY init data from a partition. Otherwise,
  151. * PHY init data will be compiled into the application itself, and this function
  152. * will return a pointer to PHY init data located in read-only memory (DROM).
  153. *
  154. * If "Use a partition to store PHY init data" option is enabled, this function
  155. * may return NULL if the data loaded from flash is not valid.
  156. *
  157. * @note Call esp_phy_release_init_data to release the pointer obtained using
  158. * this function after the call to esp_wifi_init.
  159. *
  160. * @return pointer to PHY init data structure
  161. */
  162. const esp_phy_init_data_t* esp_phy_get_init_data();
  163. /**
  164. * @brief Release PHY init data
  165. * @param data pointer to PHY init data structure obtained from
  166. * esp_phy_get_init_data function
  167. */
  168. void esp_phy_release_init_data(const esp_phy_init_data_t* data);
  169. /**
  170. * @brief Function called by esp_phy_init to load PHY calibration data
  171. *
  172. * This is a convenience function which can be used to load PHY calibration
  173. * data from NVS. Data can be stored to NVS using esp_phy_store_cal_data_to_nvs
  174. * function.
  175. *
  176. * If calibration data is not present in the NVS, or
  177. * data is not valid (was obtained for a chip with a different MAC address,
  178. * or obtained for a different version of software), this function will
  179. * return an error.
  180. *
  181. * If "Initialize PHY in startup code" option is set in menuconfig, this
  182. * function will be used to load calibration data. To provide a different
  183. * mechanism for loading calibration data, disable
  184. * "Initialize PHY in startup code" option in menuconfig and call esp_phy_init
  185. * function from the application. For an example usage of esp_phy_init and
  186. * this function, see esp_phy_store_cal_data_to_nvs function in cpu_start.c
  187. *
  188. * @param out_cal_data pointer to calibration data structure to be filled with
  189. * loaded data.
  190. * @return ESP_OK on success
  191. */
  192. esp_err_t esp_phy_load_cal_data_from_nvs(esp_phy_calibration_data_t* out_cal_data);
  193. /**
  194. * @brief Function called by esp_phy_init to store PHY calibration data
  195. *
  196. * This is a convenience function which can be used to store PHY calibration
  197. * data to the NVS. Calibration data is returned by esp_phy_init function.
  198. * Data saved using this function to the NVS can later be loaded using
  199. * esp_phy_store_cal_data_to_nvs function.
  200. *
  201. * If "Initialize PHY in startup code" option is set in menuconfig, this
  202. * function will be used to store calibration data. To provide a different
  203. * mechanism for storing calibration data, disable
  204. * "Initialize PHY in startup code" option in menuconfig and call esp_phy_init
  205. * function from the application.
  206. *
  207. * @param cal_data pointer to calibration data which has to be saved.
  208. * @return ESP_OK on success
  209. */
  210. esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t* cal_data);
  211. /**
  212. * @brief Initialize PHY and RF module
  213. *
  214. * PHY and RF module should be initialized in order to use WiFi or BT.
  215. * Now PHY and RF initializing job is done automatically when start WiFi or BT. Users should not
  216. * call this API in their application.
  217. *
  218. * @param init_data PHY parameters. Default set of parameters can
  219. * be obtained by calling esp_phy_get_default_init_data
  220. * function.
  221. * @param mode Calibration mode (Full, partial, or no calibration)
  222. * @param[inout] calibration_data
  223. * @return ESP_OK on success.
  224. * @return ESP_FAIL on fail.
  225. */
  226. esp_err_t esp_phy_rf_init(const esp_phy_init_data_t* init_data,
  227. esp_phy_calibration_mode_t mode, esp_phy_calibration_data_t* calibration_data);
  228. /**
  229. * @brief De-initialize PHY and RF module
  230. *
  231. * PHY module should be de-initialized in order to shutdown WiFi or BT.
  232. * Now PHY and RF de-initializing job is done automatically when stop WiFi or BT. Users should not
  233. * call this API in their application.
  234. *
  235. * @return ESP_OK on success.
  236. */
  237. esp_err_t esp_phy_rf_deinit(void);
  238. /**
  239. * @brief Load calibration data from NVS and initialize PHY and RF module
  240. */
  241. void esp_phy_load_cal_and_init(void);
  242. #ifdef __cplusplus
  243. }
  244. #endif