1 // Copyright 2015-2020 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 #include "esp_osal/esp_osal.h" 15 #include "hal/clk_gate_ll.h" 16 #include "esp_attr.h" 17 #include "driver/periph_ctrl.h" 18 19 static portMUX_TYPE periph_spinlock = portMUX_INITIALIZER_UNLOCKED; 20 21 static uint8_t ref_counts[PERIPH_MODULE_MAX] = {0}; 22 periph_module_enable(periph_module_t periph)23void periph_module_enable(periph_module_t periph) 24 { 25 assert(periph < PERIPH_MODULE_MAX); 26 portENTER_CRITICAL_SAFE(&periph_spinlock); 27 if (ref_counts[periph] == 0) { 28 periph_ll_enable_clk_clear_rst(periph); 29 } 30 ref_counts[periph]++; 31 portEXIT_CRITICAL_SAFE(&periph_spinlock); 32 } 33 periph_module_disable(periph_module_t periph)34void periph_module_disable(periph_module_t periph) 35 { 36 assert(periph < PERIPH_MODULE_MAX); 37 portENTER_CRITICAL_SAFE(&periph_spinlock); 38 ref_counts[periph]--; 39 if (ref_counts[periph] == 0) { 40 periph_ll_disable_clk_set_rst(periph); 41 } 42 portEXIT_CRITICAL_SAFE(&periph_spinlock); 43 } 44 periph_module_reset(periph_module_t periph)45void periph_module_reset(periph_module_t periph) 46 { 47 assert(periph < PERIPH_MODULE_MAX); 48 portENTER_CRITICAL_SAFE(&periph_spinlock); 49 periph_ll_reset(periph); 50 portEXIT_CRITICAL_SAFE(&periph_spinlock); 51 } 52 wifi_bt_common_module_enable(void)53IRAM_ATTR void wifi_bt_common_module_enable(void) 54 { 55 portENTER_CRITICAL_SAFE(&periph_spinlock); 56 if (ref_counts[PERIPH_WIFI_BT_COMMON_MODULE] == 0) { 57 periph_ll_wifi_bt_module_enable_clk_clear_rst(); 58 } 59 ref_counts[PERIPH_WIFI_BT_COMMON_MODULE]++; 60 portEXIT_CRITICAL_SAFE(&periph_spinlock); 61 } 62 wifi_bt_common_module_disable(void)63IRAM_ATTR void wifi_bt_common_module_disable(void) 64 { 65 portENTER_CRITICAL_SAFE(&periph_spinlock); 66 ref_counts[PERIPH_WIFI_BT_COMMON_MODULE]--; 67 if (ref_counts[PERIPH_WIFI_BT_COMMON_MODULE] == 0) { 68 periph_ll_wifi_bt_module_disable_clk_set_rst(); 69 } 70 portEXIT_CRITICAL_SAFE(&periph_spinlock); 71 } 72 wifi_module_enable(void)73void wifi_module_enable(void) 74 { 75 periph_ll_wifi_module_enable_clk_clear_rst(); 76 } 77 wifi_module_disable(void)78void wifi_module_disable(void) 79 { 80 periph_ll_wifi_module_disable_clk_set_rst(); 81 } 82