1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdbool.h>
8 #include "esp_attr.h"
9 #include "esp_private/regi2c_ctrl.h"
10 #include "esp_private/sar_periph_ctrl.h"
11 #include "esp_private/sar_periph_ctrl.h"
12 #include "freertos/FreeRTOS.h"
13 
14 
15 /*
16  * This file is used to override the hooks provided by the PHY lib for some system features.
17  * Call phy_override() so that this file will be linked.
18  */
19 
20 static bool s_wifi_adc_xpd_flag;
21 static bool s_wifi_pwdet_xpd_flag;
22 static bool s_wifi_tsens_xpd_flag;
23 
include_esp_phy_override(void)24 void include_esp_phy_override(void)
25 {
26     /* When this empty function is called, all functions below will be linked. */
27 }
28 
29 /* Coordinate ADC power with other modules. */
30 // It seems that it is only required on ESP32, but we still compile it for all chips, in case it is
31 // called by PHY unexpectedly.
set_xpd_sar(bool en)32 void set_xpd_sar(bool en)
33 {
34     if (s_wifi_adc_xpd_flag == en) {
35         /* ignore repeated calls to set_xpd_sar when the state is already correct */
36         return;
37     }
38 
39     s_wifi_adc_xpd_flag = en;
40     if (en) {
41         sar_periph_ctrl_pwdet_power_acquire();
42     } else {
43         sar_periph_ctrl_pwdet_power_release();
44     }
45 }
46 
47 //add spinlock protection
phy_i2c_enter_critical(void)48 IRAM_ATTR void phy_i2c_enter_critical(void)
49 {
50     regi2c_enter_critical();
51 }
52 
phy_i2c_exit_critical(void)53 IRAM_ATTR void phy_i2c_exit_critical(void)
54 {
55     regi2c_exit_critical();
56 }
57 
phy_set_pwdet_power(bool en)58 void phy_set_pwdet_power(bool en)
59 {
60     if (s_wifi_pwdet_xpd_flag == en) {
61         /* ignore repeated calls to phy_set_pwdet_power when the state is already correct */
62         return;
63     }
64 
65     s_wifi_pwdet_xpd_flag = en;
66     if (en) {
67         sar_periph_ctrl_pwdet_power_acquire();
68     } else {
69         sar_periph_ctrl_pwdet_power_release();
70     }
71 }
72 
phy_set_tsens_power(bool en)73 void phy_set_tsens_power(bool en)
74 {
75     if (s_wifi_tsens_xpd_flag == en) {
76         /* ignore repeated calls to phy_set_tsens_power when the state is already correct */
77         return;
78     }
79 
80     s_wifi_tsens_xpd_flag = en;
81     if (en) {
82         temperature_sensor_power_acquire();
83     } else {
84         temperature_sensor_power_release();
85     }
86 }
87 
phy_get_tsens_value(void)88 int16_t phy_get_tsens_value(void)
89 {
90     return temp_sensor_get_raw_value(NULL);
91 }
92