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 #include "esp_attr.h"
16 #include "freertos/portmacro.h"
17 #include "esp_phy_init.h"
18 #include "phy.h"
19 
20 #define PHY_ENABLE_VERSION_PRINT 1
21 
22 static DRAM_ATTR portMUX_TYPE s_phy_int_mux = portMUX_INITIALIZER_UNLOCKED;
23 extern void phy_version_print(void);
24 
25 static _lock_t s_phy_access_lock;
26 
27 /* Reference count of enabling PHY */
28 static uint8_t s_phy_access_ref = 0;
29 
30 extern void bt_bb_v2_init_cmplx(int print_version);
31 
phy_enter_critical(void)32 uint32_t IRAM_ATTR phy_enter_critical(void)
33 {
34     if (xPortInIsrContext()) {
35         portENTER_CRITICAL_ISR(&s_phy_int_mux);
36 
37     } else {
38         portENTER_CRITICAL(&s_phy_int_mux);
39     }
40     // Interrupt level will be stored in current tcb, so always return zero.
41     return 0;
42 }
43 
phy_exit_critical(uint32_t level)44 void IRAM_ATTR phy_exit_critical(uint32_t level)
45 {
46     // Param level don't need any more, ignore it.
47     if (xPortInIsrContext()) {
48         portEXIT_CRITICAL_ISR(&s_phy_int_mux);
49     } else {
50         portEXIT_CRITICAL(&s_phy_int_mux);
51     }
52 }
53 
esp_phy_enable(void)54 void esp_phy_enable(void)
55 {
56     _lock_acquire(&s_phy_access_lock);
57     if (s_phy_access_ref == 0) {
58         register_chipv7_phy(NULL, NULL, PHY_RF_CAL_FULL);
59         bt_bb_v2_init_cmplx(PHY_ENABLE_VERSION_PRINT);
60         phy_version_print();
61     }
62 
63     s_phy_access_ref++;
64 
65     _lock_release(&s_phy_access_lock);
66     // ESP32H2-TODO: enable common clk.
67 }
68 
esp_phy_disable(void)69 void esp_phy_disable(void)
70 {
71     // ESP32H2-TODO: close rf and disable clk for modem sleep and light sleep
72 }
73