1 
2 // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include "hal/brownout_hal.h"
17 #include "soc/rtc_cntl_struct.h"
18 #include "soc/rtc_cntl_reg.h"
19 #include "regi2c_ctrl.h"
20 #include "i2c_pmu.h"
21 #include "regi2c_brownout.h"
22 
23 
brownout_hal_config(const brownout_hal_config_t * cfg)24 void brownout_hal_config(const brownout_hal_config_t *cfg)
25 {
26     REGI2C_WRITE_MASK(I2C_PMU, I2C_PMU_OC_DREF_LVDET, cfg->threshold);
27     typeof(RTCCNTL.brown_out) brown_out_reg = {
28         .close_flash_ena = cfg->flash_power_down,
29         .pd_rf_ena = cfg->rf_power_down,
30         .rst_wait = 0x3ff,
31         .rst_ena = cfg->reset_enabled,
32         .ena = cfg->enabled,
33         .rst_sel = 1,
34     };
35     RTCCNTL.brown_out = brown_out_reg;
36 }
37 
brownout_hal_intr_enable(bool enable)38 void brownout_hal_intr_enable(bool enable)
39 {
40     RTCCNTL.int_ena.rtc_brown_out = enable;
41 }
42 
brownout_hal_intr_clear(void)43 void brownout_hal_intr_clear(void)
44 {
45     RTCCNTL.int_clr.rtc_brown_out = 1;
46 }
47