Skip to content

Commit 135c297

Browse files
committed
Merge branch 'feature/add_set_tx_power_via_vdd33_function' into 'master'
feat(esp8266): add set tx power via vdd33 function See merge request sdk/ESP8266_RTOS_SDK!950
2 parents e27cb05 + 2084e8e commit 135c297

File tree

2 files changed

+83
-32
lines changed

2 files changed

+83
-32
lines changed

components/esp8266/include/esp_wifi.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,32 @@ esp_err_t esp_wifi_set_vendor_ie_cb(esp_vendor_ie_cb_t cb, void *ctx);
827827
*/
828828
esp_err_t esp_wifi_set_max_tx_power(int8_t power);
829829

830+
/**
831+
* @brief Adjust RF Tx Power according to VDD33; unit : 1/1024 V.
832+
*
833+
* @attention When TOUT pin is suspended, VDD33 can be got by esp_wifi_get_vdd33.
834+
* When TOUT pin is wired to external circuitry, esp_wifi_get_vdd33 can not be used.
835+
* @attention This api only worked when it is called, please call this api every day or hour
836+
* according to power consumption.
837+
*
838+
* @param vdd33 unit is 1/1024V, range [1900, 3300].
839+
*/
840+
void esp_wifi_set_max_tx_power_via_vdd33(uint16_t vdd33);
841+
842+
/**
843+
* @brief Measure the power voltage of VDD3P3 pin 3 and 4; unit: 1/1024 V
844+
*
845+
* @attention esp_wifi_get_vdd33 can only be called when TOUT pin is suspended.
846+
* @attention The 107th byte in esp_init_data_default.bin (0 ~ 127 bytes) is named as
847+
* vdd33_const. When TOUT pin is suspended, vdd33_const must be set as
848+
* 0xFF, which is 255.
849+
* @attention The return value of esp_wifi_get_vdd33 may be different in different Wi-Fi
850+
* modes, for example, in Modem-sleep mode or in normal Wi-Fi working mode.
851+
*
852+
* @return the power voltage of vdd33 pin 3 and 4
853+
*/
854+
uint16_t esp_wifi_get_vdd33(void);
855+
830856
/**
831857
* @brief Get maximum WiFi transmiting power
832858
*

components/esp8266/source/phy_init.c

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,24 @@
3030
#include "internal/phy_init_data.h"
3131
#include "phy.h"
3232

33-
static const char *TAG = "phy_init";
33+
static const char* TAG = "phy_init";
3434

35-
static uint8_t phy_check_calibration_data(uint8_t *rf_cal_data)
35+
static uint8_t phy_check_calibration_data(uint8_t* rf_cal_data)
3636
{
3737
#define CHECK_NUM 26
3838
#define CHIP_ID_L 24
3939
#define CHIP_ID_H 25
4040

4141
uint8_t i;
42-
uint32_t *cal_data_word = (uint32_t *)rf_cal_data;
42+
uint32_t* cal_data_word = (uint32_t*)rf_cal_data;
4343
uint32_t check_sum = 0;
4444

4545
/* L: flag_1[79:76], version[59:56], mac_map[55:48], mac_l[47:24] */
4646
uint32_t chip_id_l = ((REG_READ(0x3FF00058) & 0xF000) << 16) |
47-
(REG_READ(0x3ff00054) & 0xFFFFFFF);
47+
(REG_READ(0x3ff00054) & 0xFFFFFFF);
4848
/* H: mac_l[31:24], mac_h[119:96] */
4949
uint32_t chip_id_h = (REG_READ(0x3FF00050) & 0xFF000000) |
50-
(REG_READ(0x3ff0005C) & 0xFFFFFF);
50+
(REG_READ(0x3ff0005C) & 0xFFFFFF);
5151

5252
cal_data_word[CHIP_ID_L] = chip_id_l;
5353
cal_data_word[CHIP_ID_H] = chip_id_h;
@@ -63,12 +63,12 @@ static uint8_t phy_check_calibration_data(uint8_t *rf_cal_data)
6363
/* ToDo: use rx_gain_dc_table in nvs, need to modify internal libraries */
6464
uint32_t rx_gain_dc_table[125];
6565

66-
esp_err_t esp_phy_rf_init(const esp_phy_init_data_t *init_data, esp_phy_calibration_mode_t mode,
67-
esp_phy_calibration_data_t *calibration_data, phy_rf_module_t module)
66+
esp_err_t esp_phy_rf_init(const esp_phy_init_data_t* init_data, esp_phy_calibration_mode_t mode,
67+
esp_phy_calibration_data_t* calibration_data, phy_rf_module_t module)
6868
{
6969
esp_err_t status = ESP_OK;
7070
uint8_t sta_mac[6];
71-
uint8_t *local_init_data = calloc(1, 256);
71+
uint8_t* local_init_data = calloc(1, 256);
7272
#ifdef CONFIG_CONSOLE_UART_BAUDRATE
7373
const uint32_t uart_baudrate = CONFIG_CONSOLE_UART_BAUDRATE;
7474
#else
@@ -77,10 +77,10 @@ esp_err_t esp_phy_rf_init(const esp_phy_init_data_t *init_data, esp_phy_calibrat
7777

7878
memcpy(local_init_data, init_data->params, 128);
7979

80-
extern uint32_t *phy_rx_gain_dc_table;
80+
extern uint32_t* phy_rx_gain_dc_table;
8181
phy_rx_gain_dc_table = calibration_data->rx_gain_dc_table;
8282
uint8_t cal_data_check = phy_check_calibration_data(calibration_data->rf_cal_data) ||
83-
phy_check_data_table(phy_rx_gain_dc_table, 125, 1);
83+
phy_check_data_table(phy_rx_gain_dc_table, 125, 1);
8484

8585
phy_afterwake_set_rfoption(1);
8686

@@ -90,8 +90,8 @@ esp_err_t esp_phy_rf_init(const esp_phy_init_data_t *init_data, esp_phy_calibrat
9090

9191
esp_efuse_mac_get_default(sta_mac);
9292
chip_init(local_init_data, sta_mac, uart_baudrate);
93-
ESP_LOGI(TAG, "phy ver: %d_%d", (READ_PERI_REG(0x6000107C)>>16)&0xFFF, READ_PERI_REG(0x6000107C)>>28);
94-
get_data_from_rtc((uint8_t *)calibration_data);
93+
ESP_LOGI(TAG, "phy ver: %d_%d", (READ_PERI_REG(0x6000107C) >> 16) & 0xFFF, READ_PERI_REG(0x6000107C) >> 28);
94+
get_data_from_rtc((uint8_t*)calibration_data);
9595

9696
memcpy(rx_gain_dc_table, calibration_data->rx_gain_dc_table, 4 * 125);
9797
phy_rx_gain_dc_table = rx_gain_dc_table;
@@ -101,9 +101,11 @@ esp_err_t esp_phy_rf_init(const esp_phy_init_data_t *init_data, esp_phy_calibrat
101101
if (cal_data_check == ESP_CAL_DATA_CHECK_FAIL) {
102102
#ifdef CONFIG_ESP_PHY_CALIBRATION_AND_DATA_STORAGE
103103
ESP_LOGW(TAG, "saving new calibration data because of checksum failure, mode(%d)", mode);
104+
104105
if (mode != PHY_RF_CAL_FULL) {
105106
esp_phy_store_cal_data_to_nvs(calibration_data);
106107
}
108+
107109
#endif
108110
}
109111

@@ -121,9 +123,9 @@ esp_err_t esp_phy_rf_deinit(phy_rf_module_t module)
121123
#if CONFIG_ESP_PHY_INIT_DATA_IN_PARTITION
122124
#include "esp_partition.h"
123125

124-
const esp_phy_init_data_t *esp_phy_get_init_data()
126+
const esp_phy_init_data_t* esp_phy_get_init_data()
125127
{
126-
const esp_partition_t *partition = esp_partition_find_first(
128+
const esp_partition_t* partition = esp_partition_find_first(
127129
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_PHY, NULL);
128130

129131
if (partition == NULL) {
@@ -134,7 +136,7 @@ const esp_phy_init_data_t *esp_phy_get_init_data()
134136
ESP_LOGD(TAG, "loading PHY init data from partition at offset 0x%x", partition->address);
135137
size_t init_data_store_length = sizeof(phy_init_magic_pre) +
136138
sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post);
137-
uint8_t *init_data_store = (uint8_t *) malloc(init_data_store_length);
139+
uint8_t* init_data_store = (uint8_t*) malloc(init_data_store_length);
138140

139141
if (init_data_store == NULL) {
140142
ESP_LOGE(TAG, "failed to allocate memory for PHY init data");
@@ -156,43 +158,43 @@ const esp_phy_init_data_t *esp_phy_get_init_data()
156158
}
157159

158160
ESP_LOGD(TAG, "PHY data partition validated");
159-
return (const esp_phy_init_data_t *)(init_data_store + sizeof(phy_init_magic_pre));
161+
return (const esp_phy_init_data_t*)(init_data_store + sizeof(phy_init_magic_pre));
160162
}
161163

162-
void esp_phy_release_init_data(const esp_phy_init_data_t *init_data)
164+
void esp_phy_release_init_data(const esp_phy_init_data_t* init_data)
163165
{
164-
free((uint8_t *) init_data - sizeof(phy_init_magic_pre));
166+
free((uint8_t*) init_data - sizeof(phy_init_magic_pre));
165167
}
166168

167169
#else // CONFIG_ESP_PHY_INIT_DATA_IN_PARTITION
168170

169171
// phy_init_data.h will declare static 'phy_init_data' variable initialized with default init data
170172

171-
const esp_phy_init_data_t *esp_phy_get_init_data()
173+
const esp_phy_init_data_t* esp_phy_get_init_data()
172174
{
173175
ESP_LOGD(TAG, "loading PHY init data from application binary");
174176
return &phy_init_data;
175177
}
176178

177-
void esp_phy_release_init_data(const esp_phy_init_data_t *init_data)
179+
void esp_phy_release_init_data(const esp_phy_init_data_t* init_data)
178180
{
179181
// no-op
180182
}
181183
#endif // CONFIG_ESP_PHY_INIT_DATA_IN_PARTITION
182184

183185

184186
// PHY calibration data handling functions
185-
static const char *PHY_NAMESPACE = "phy";
186-
static const char *PHY_CAL_DATA_KEY = "cal_data";
187-
static const char *PHY_RX_GAIN_DC_TABLE_KEY = "dc_table";
187+
static const char* PHY_NAMESPACE = "phy";
188+
static const char* PHY_CAL_DATA_KEY = "cal_data";
189+
static const char* PHY_RX_GAIN_DC_TABLE_KEY = "dc_table";
188190

189191
static esp_err_t load_cal_data_from_nvs_handle(nvs_handle handle,
190-
esp_phy_calibration_data_t *out_cal_data);
192+
esp_phy_calibration_data_t* out_cal_data);
191193

192194
static esp_err_t store_cal_data_to_nvs_handle(nvs_handle handle,
193-
const esp_phy_calibration_data_t *cal_data);
195+
const esp_phy_calibration_data_t* cal_data);
194196

195-
esp_err_t esp_phy_load_cal_data_from_nvs(esp_phy_calibration_data_t *out_cal_data)
197+
esp_err_t esp_phy_load_cal_data_from_nvs(esp_phy_calibration_data_t* out_cal_data)
196198
{
197199
nvs_handle handle;
198200
esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READONLY, &handle);
@@ -210,7 +212,7 @@ esp_err_t esp_phy_load_cal_data_from_nvs(esp_phy_calibration_data_t *out_cal_dat
210212
return err;
211213
}
212214

213-
esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t *cal_data)
215+
esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t* cal_data)
214216
{
215217
nvs_handle handle;
216218
esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READWRITE, &handle);
@@ -226,7 +228,7 @@ esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t *cal_da
226228
}
227229

228230
static esp_err_t load_cal_data_from_nvs_handle(nvs_handle handle,
229-
esp_phy_calibration_data_t *out_cal_data)
231+
esp_phy_calibration_data_t* out_cal_data)
230232
{
231233
esp_err_t err;
232234

@@ -261,7 +263,7 @@ static esp_err_t load_cal_data_from_nvs_handle(nvs_handle handle,
261263
}
262264

263265
static esp_err_t store_cal_data_to_nvs_handle(nvs_handle handle,
264-
const esp_phy_calibration_data_t *cal_data)
266+
const esp_phy_calibration_data_t* cal_data)
265267
{
266268
esp_err_t err;
267269

@@ -290,15 +292,15 @@ static esp_err_t store_cal_data_to_nvs_handle(nvs_handle handle,
290292

291293
void esp_phy_load_cal_and_init(phy_rf_module_t module)
292294
{
293-
esp_phy_calibration_data_t *cal_data =
294-
(esp_phy_calibration_data_t *) calloc(sizeof(esp_phy_calibration_data_t), 1);
295+
esp_phy_calibration_data_t* cal_data =
296+
(esp_phy_calibration_data_t*) calloc(sizeof(esp_phy_calibration_data_t), 1);
295297

296298
if (cal_data == NULL) {
297299
ESP_LOGE(TAG, "failed to allocate memory for RF calibration data");
298300
abort();
299301
}
300302

301-
const esp_phy_init_data_t *init_data = esp_phy_get_init_data();
303+
const esp_phy_init_data_t* init_data = esp_phy_get_init_data();
302304

303305
if (init_data == NULL) {
304306
ESP_LOGE(TAG, "failed to obtain PHY init data");
@@ -335,3 +337,26 @@ void esp_phy_load_cal_and_init(phy_rf_module_t module)
335337

336338
free(cal_data); // PHY maintains a copy of calibration data, so we can free this
337339
}
340+
341+
uint16_t esp_wifi_get_vdd33(void)
342+
{
343+
if (phy_init_data.params[107] != 0xFF) {
344+
ESP_LOGE(TAG, "Please set VDD33 const to 0xff");
345+
return 0xFFFF;
346+
}
347+
348+
extern uint16_t phy_get_vdd33();
349+
uint16_t ret = phy_get_vdd33();
350+
351+
if (ret != 0xFFFF) {
352+
ret = ret * 12 / 11;
353+
}
354+
355+
return ret;
356+
}
357+
358+
void esp_wifi_set_max_tx_power_via_vdd33(uint16_t vdd33)
359+
{
360+
extern void phy_vdd33_set_tpw(uint16_t vdd33);
361+
phy_vdd33_set_tpw(vdd33);
362+
}

0 commit comments

Comments
 (0)