1 /*
2  * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 #include <stdint.h>
9 #include <stdbool.h>
10 #include "esp_err.h"
11 #include "zephyr_compat.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 /**
18  * @file
19  *          init parameters and API
20  */
21 
22 
23 /**
24  * @brief Structure holding PHY init parameters
25  */
26 typedef struct {
27 	uint8_t params[128];                    /*!< opaque PHY initialization parameters */
28 } esp_phy_init_data_t;
29 
30 /**
31  * @brief Opaque PHY calibration data
32  */
33 typedef struct {
34     uint8_t version[4];                     /*!< PHY version */
35     uint8_t mac[6];                         /*!< The MAC address of the station */
36     uint8_t opaque[1894];                   /*!< calibration data */
37 } esp_phy_calibration_data_t;
38 
39 /**
40  * @brief PHY calibration mode
41  *
42  */
43 typedef enum {
44     PHY_RF_CAL_PARTIAL = 0x00000000,        /*!< Do part of RF calibration. This should be used after power-on reset. */
45     PHY_RF_CAL_NONE    = 0x00000001,        /*!< Don't do any RF calibration. This mode is only suggested to be used after deep sleep reset. */
46     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. */
47 } esp_phy_calibration_mode_t;
48 
49 #if CONFIG_ESP_PHY_MULTIPLE_INIT_DATA_BIN
50 /**
51  * @brief PHY init data type
52  */
53 typedef enum {
54     ESP_PHY_INIT_DATA_TYPE_DEFAULT = 0,
55     ESP_PHY_INIT_DATA_TYPE_SRRC,
56     ESP_PHY_INIT_DATA_TYPE_FCC,
57     ESP_PHY_INIT_DATA_TYPE_CE,
58     ESP_PHY_INIT_DATA_TYPE_NCC,
59     ESP_PHY_INIT_DATA_TYPE_KCC,
60     ESP_PHY_INIT_DATA_TYPE_MIC,
61     ESP_PHY_INIT_DATA_TYPE_IC,
62     ESP_PHY_INIT_DATA_TYPE_ACMA,
63     ESP_PHY_INIT_DATA_TYPE_ANATEL,
64     ESP_PHY_INIT_DATA_TYPE_ISED,
65     ESP_PHY_INIT_DATA_TYPE_WPC,
66     ESP_PHY_INIT_DATA_TYPE_OFCA,
67     ESP_PHY_INIT_DATA_TYPE_IFETEL,
68     ESP_PHY_INIT_DATA_TYPE_RCM,
69     ESP_PHY_INIT_DATA_TYPE_NUMBER,
70 } phy_init_data_type_t;
71 #endif
72 
73 /**
74  * @brief Get PHY init data
75  *
76  * If "Use a partition to store PHY init data" option is set in menuconfig,
77  * This function will load PHY init data from a partition. Otherwise,
78  * PHY init data will be compiled into the application itself, and this function
79  * will return a pointer to PHY init data located in read-only memory (DROM).
80  *
81  * If "Use a partition to store PHY init data" option is enabled, this function
82  * may return NULL if the data loaded from flash is not valid.
83  *
84  * @note Call esp_phy_release_init_data to release the pointer obtained using
85  * this function after the call to esp_wifi_init.
86  *
87  * @return pointer to PHY init data structure
88  */
89 const esp_phy_init_data_t* esp_phy_get_init_data(void);
90 
91 /**
92  * @brief Release PHY init data
93  * @param data  pointer to PHY init data structure obtained from
94  *              esp_phy_get_init_data function
95  */
96 void esp_phy_release_init_data(const esp_phy_init_data_t* data);
97 
98 /**
99  * @brief Function called by esp_phy_load_cal_and_init to load PHY calibration data
100  *
101  * This is a convenience function which can be used to load PHY calibration
102  * data from NVS. Data can be stored to NVS using esp_phy_store_cal_data_to_nvs
103  * function.
104  *
105  * If calibration data is not present in the NVS, or
106  * data is not valid (was obtained for a chip with a different MAC address,
107  * or obtained for a different version of software), this function will
108  * return an error.
109  *
110  * @param out_cal_data pointer to calibration data structure to be filled with
111  *                     loaded data.
112  * @return ESP_OK on success
113  */
114 esp_err_t esp_phy_load_cal_data_from_nvs(esp_phy_calibration_data_t* out_cal_data);
115 
116 /**
117  * @brief Function called by esp_phy_load_cal_and_init to store PHY calibration data
118  *
119  * This is a convenience function which can be used to store PHY calibration
120  * data to the NVS. Calibration data is returned by esp_phy_load_cal_and_init function.
121  * Data saved using this function to the NVS can later be loaded using
122  * esp_phy_store_cal_data_to_nvs function.
123  *
124  * @param cal_data pointer to calibration data which has to be saved.
125  * @return ESP_OK on success
126  */
127 esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t* cal_data);
128 
129 /**
130  * @brief Erase PHY calibration data which is stored in the NVS
131  *
132  * This is a function which can be used to trigger full calibration as a last-resort remedy
133  * if partial calibration is used. It can be called in the application based on some conditions
134  * (e.g. an option provided in some diagnostic mode).
135  *
136  * @return ESP_OK on success
137  * @return others on fail. Please refer to NVS API return value error number.
138  */
139 esp_err_t esp_phy_erase_cal_data_in_nvs(void);
140 
141 /**
142  * @brief Enable PHY and RF module
143  *
144  * PHY and RF module should be enabled in order to use WiFi or BT.
145  * Now PHY and RF enabling job is done automatically when start WiFi or BT. Users should not
146  * call this API in their application.
147  *
148  */
149 void esp_phy_enable(void);
150 
151 /**
152  * @brief Disable PHY and RF module
153  *
154  * PHY module should be disabled in order to shutdown WiFi or BT.
155  * Now PHY and RF disabling job is done automatically when stop WiFi or BT. Users should not
156  * call this API in their application.
157  *
158  */
159 void esp_phy_disable(void);
160 
161 /**
162  * @brief Enable BTBB module
163  *
164  * BTBB module should be enabled in order to use IEEE802154 or BT.
165  * Now BTBB enabling job is done automatically when start IEEE802154 or BT. Users should not
166  * call this API in their application.
167  *
168  */
169 void esp_btbb_enable(void);
170 
171 /**
172  * @brief Disable BTBB module
173  *
174  * Dsiable BTBB module, used by IEEE802154 or Bluetooth.
175  * Users should not call this API in their application.
176  *
177  */
178 void esp_btbb_disable(void);
179 
180 /**
181  * @brief Load calibration data from NVS and initialize PHY and RF module
182  */
183 void esp_phy_load_cal_and_init(void);
184 
185 /**
186  * @brief Initialize backup memory for Phy power up/down
187  */
188 void esp_phy_modem_init(void);
189 
190 /**
191  * @brief Deinitialize backup memory for Phy power up/down
192  * Set phy_init_flag if all modems deinit on ESP32C3
193  */
194 void esp_phy_modem_deinit(void);
195 
196 #if CONFIG_MAC_BB_PD
197 /**
198  * @brief Initialize backup memory for MAC and Baseband power up/down
199  */
200 void esp_mac_bb_pd_mem_init(void);
201 
202 /**
203  * @brief Deinitialize backup memory for MAC and Baseband power up/down
204  */
205 void esp_mac_bb_pd_mem_deinit(void);
206 
207 /**
208  * @brief Power up MAC and Baseband
209  */
210 void esp_mac_bb_power_up(void);
211 
212 /**
213  * @brief Power down MAC and Baseband
214  */
215 void esp_mac_bb_power_down(void);
216 #endif
217 
218 /**
219  * @brief Enable WiFi/BT common clock
220  *
221  */
222 void esp_phy_common_clock_enable(void);
223 
224 /**
225  * @brief Disable WiFi/BT common clock
226  *
227  */
228 void esp_phy_common_clock_disable(void);
229 
230 /**
231  * @brief            Get the time stamp when PHY/RF was switched on
232  * @return           return 0 if PHY/RF is never switched on. Otherwise return time in
233  *                   microsecond since boot when phy/rf was last switched on
234 */
235 int64_t esp_phy_rf_get_on_ts(void);
236 
237 /**
238  * @brief Update the corresponding PHY init type according to the country code of Wi-Fi.
239  *
240  * @param country country code
241  * @return ESP_OK on success.
242  * @return esp_err_t code describing the error on fail
243  */
244 esp_err_t esp_phy_update_country_info(const char *country);
245 
246 
247 #if CONFIG_ESP_PHY_MULTIPLE_INIT_DATA_BIN
248 /**
249  * @brief Apply PHY init bin to PHY
250  * @return ESP_OK on success.
251  * @return ESP_FAIL on fail.
252  */
253 esp_err_t esp_phy_apply_phy_init_data(uint8_t *init_data);
254 #endif
255 
256 /**
257  * @brief Get PHY lib version
258  * @return PHY lib version.
259  */
260 char * get_phy_version_str(void);
261 
262 /**
263  * @brief Enable phy track pll
264  *
265  */
266 void phy_track_pll_init(void);
267 
268 /**
269  * @brief Disable phy track pll
270  *
271  */
272 void phy_track_pll_deinit(void);
273 #ifdef __cplusplus
274 }
275 #endif
276