1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include <stdint.h>
10 #include "sdkconfig.h"
11 #include "esp_rom_regi2c.h"
12 #include "soc/regi2c_defs.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 
19 #define regi2c_read_reg_raw        esp_rom_regi2c_read
20 #define regi2c_read_reg_mask_raw   esp_rom_regi2c_read_mask
21 #define regi2c_write_reg_raw       esp_rom_regi2c_write
22 #define regi2c_write_reg_mask_raw  esp_rom_regi2c_write_mask
23 
24 
25 #ifdef BOOTLOADER_BUILD
26 /**
27  * If compiling for the bootloader, ROM functions can be called directly,
28  * without the need of a lock.
29  */
30 #define regi2c_ctrl_read_reg         regi2c_read_reg_raw
31 #define regi2c_ctrl_read_reg_mask    regi2c_read_reg_mask_raw
32 #define regi2c_ctrl_write_reg        regi2c_write_reg_raw
33 #define regi2c_ctrl_write_reg_mask   regi2c_write_reg_mask_raw
34 
35 #else
36 
37 /* Access internal registers, don't use in application */
38 uint8_t regi2c_ctrl_read_reg(uint8_t block, uint8_t host_id, uint8_t reg_add);
39 uint8_t regi2c_ctrl_read_reg_mask(uint8_t block, uint8_t host_id, uint8_t reg_add, uint8_t msb, uint8_t lsb);
40 void regi2c_ctrl_write_reg(uint8_t block, uint8_t host_id, uint8_t reg_add, uint8_t data);
41 void regi2c_ctrl_write_reg_mask(uint8_t block, uint8_t host_id, uint8_t reg_add, uint8_t msb, uint8_t lsb, uint8_t data);
42 
43 /* enter the critical section that protects internal registers. Don't use it in SDK. Use the functions above. */
44 void regi2c_enter_critical(void);
45 void regi2c_exit_critical(void);
46 
47 #endif // BOOTLOADER_BUILD
48 
49 /* Convenience macros for the above functions, these use register definitions
50  * from regi2c_xxx.h header files.
51  */
52 #define REGI2C_WRITE_MASK(block, reg_add, indata) \
53       regi2c_ctrl_write_reg_mask(block, block##_HOSTID,  reg_add,  reg_add##_MSB,  reg_add##_LSB,  indata)
54 
55 #define REGI2C_READ_MASK(block, reg_add) \
56       regi2c_ctrl_read_reg_mask(block, block##_HOSTID,  reg_add,  reg_add##_MSB,  reg_add##_LSB)
57 
58 #define REGI2C_WRITE(block, reg_add, indata) \
59       regi2c_ctrl_write_reg(block, block##_HOSTID,  reg_add, indata)
60 
61 #define REGI2C_READ(block, reg_add) \
62       regi2c_ctrl_read_reg(block, block##_HOSTID,  reg_add)
63 
64 /**
65  * Restore regi2c analog calibration related configuration registers.
66  * This is a workaround, and is fixed on later chips
67  */
68 #if REGI2C_ANA_CALI_PD_WORKAROUND
69 void regi2c_analog_cali_reg_read(void);
70 void regi2c_analog_cali_reg_write(void);
71 #endif   //#if ADC_CALI_PD_WORKAROUND
72 
73 /* Enable/Disable regi2c_saradc with calling these two functions.
74    With reference count protection inside.
75    Internal use only.
76 */
77 void regi2c_saradc_enable(void);
78 void regi2c_saradc_disable(void);
79 
80 #ifdef __cplusplus
81 }
82 #endif
83