• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015-2019 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 #pragma once
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include "soc/soc_caps.h"
24 
25 /**
26  * @brief I2C port number, can be I2C_NUM_0 ~ (I2C_NUM_MAX-1).
27  */
28 typedef int i2c_port_t;
29 
30 typedef enum{
31     I2C_MODE_SLAVE = 0,   /*!< I2C slave mode */
32     I2C_MODE_MASTER,      /*!< I2C master mode */
33     I2C_MODE_MAX,
34 } i2c_mode_t;
35 
36 typedef enum {
37     I2C_MASTER_WRITE = 0,   /*!< I2C write data */
38     I2C_MASTER_READ,        /*!< I2C read data */
39 } i2c_rw_t;
40 
41 typedef enum {
42     I2C_DATA_MODE_MSB_FIRST = 0,  /*!< I2C data msb first */
43     I2C_DATA_MODE_LSB_FIRST = 1,  /*!< I2C data lsb first */
44     I2C_DATA_MODE_MAX
45 } i2c_trans_mode_t;
46 
47 typedef enum {
48     I2C_ADDR_BIT_7 = 0,    /*!< I2C 7bit address for slave mode */
49     I2C_ADDR_BIT_10,       /*!< I2C 10bit address for slave mode */
50     I2C_ADDR_BIT_MAX,
51 } i2c_addr_mode_t;
52 
53 typedef enum {
54     I2C_MASTER_ACK = 0x0,        /*!< I2C ack for each byte read */
55     I2C_MASTER_NACK = 0x1,       /*!< I2C nack for each byte read */
56     I2C_MASTER_LAST_NACK = 0x2,   /*!< I2C nack for the last byte*/
57     I2C_MASTER_ACK_MAX,
58 } i2c_ack_type_t;
59 
60 /**
61  * @brief I2C clock source, sorting from smallest to largest,
62  *        place them in order.
63  *        This can be expanded in the future use.
64  */
65 typedef enum {
66     I2C_SCLK_DEFAULT = 0,    /*!< I2C source clock not selected*/
67 #if SOC_I2C_SUPPORT_APB
68     I2C_SCLK_APB,            /*!< I2C source clock from APB, 80M*/
69 #endif
70 #if SOC_I2C_SUPPORT_XTAL
71     I2C_SCLK_XTAL,           /*!< I2C source clock from XTAL, 40M */
72 #endif
73 #if SOC_I2C_SUPPORT_RTC
74     I2C_SCLK_RTC,            /*!< I2C source clock from 8M RTC, 8M */
75 #endif
76 #if SOC_I2C_SUPPORT_REF_TICK
77     I2C_SCLK_REF_TICK,       /*!< I2C source clock from REF_TICK, 1M */
78 #endif
79     I2C_SCLK_MAX,
80 } i2c_sclk_t;
81 
82 // I2C clk flags for users to use, can be expanded in the future.
83 #define I2C_SCLK_SRC_FLAG_FOR_NOMAL       (0)         /*!< Any one clock source that is available for the specified frequency may be choosen*/
84 #define I2C_SCLK_SRC_FLAG_AWARE_DFS       (1 << 0)    /*!< For REF tick clock, it won't change with APB.*/
85 #define I2C_SCLK_SRC_FLAG_LIGHT_SLEEP     (1 << 1)    /*!< For light sleep mode.*/
86 
87 /// Use the highest speed that is available for the clock source picked by clk_flags
88 #define I2C_CLK_FREQ_MAX                  (-1)
89 
90 /**
91  * @brief I2C initialization parameters
92  */
93 typedef struct{
94     i2c_mode_t mode;     /*!< I2C mode */
95     int sda_io_num;      /*!< GPIO number for I2C sda signal */
96     int scl_io_num;      /*!< GPIO number for I2C scl signal */
97     bool sda_pullup_en;  /*!< Internal GPIO pull mode for I2C sda signal*/
98     bool scl_pullup_en;  /*!< Internal GPIO pull mode for I2C scl signal*/
99 
100     union {
101         struct {
102             uint32_t clk_speed;     /*!< I2C clock frequency for master mode, (no higher than 1MHz for now) */
103         } master;                   /*!< I2C master config */
104         struct {
105             uint8_t addr_10bit_en;  /*!< I2C 10bit address mode enable for slave mode */
106             uint16_t slave_addr;    /*!< I2C address for slave mode */
107         } slave;                    /*!< I2C slave config */
108     };
109     uint32_t clk_flags;             /*!< Bitwise of ``I2C_SCLK_SRC_FLAG_**FOR_DFS**`` for clk source choice*/
110 } i2c_config_t;
111 
112 #if CONFIG_IDF_TARGET_ESP32
113 typedef enum{
114     I2C_CMD_RESTART = 0,   /*!<I2C restart command */
115     I2C_CMD_WRITE,         /*!<I2C write command */
116     I2C_CMD_READ,          /*!<I2C read command */
117     I2C_CMD_STOP,          /*!<I2C stop command */
118     I2C_CMD_END            /*!<I2C end command */
119 } i2c_opmode_t __attribute__((deprecated));
120 #endif
121 
122 #ifdef __cplusplus
123 }
124 #endif
125