@@ -38,47 +38,64 @@ static const char* TAG = "sccb";
38
38
#define ACK_VAL 0x0 /*!< I2C ack value */
39
39
#define NACK_VAL 0x1 /*!< I2C nack value */
40
40
#if CONFIG_SCCB_HARDWARE_I2C_PORT1
41
- const int SCCB_I2C_PORT = 1 ;
41
+ const int SCCB_I2C_PORT_DEFAULT = 1 ;
42
42
#else
43
- const int SCCB_I2C_PORT = 0 ;
43
+ const int SCCB_I2C_PORT_DEFAULT = 0 ;
44
44
#endif
45
45
46
+ static int sccb_i2c_port ;
47
+ static bool sccb_owns_i2c_port ;
48
+
46
49
int SCCB_Init (int pin_sda , int pin_scl )
47
50
{
48
51
ESP_LOGI (TAG , "pin_sda %d pin_scl %d" , pin_sda , pin_scl );
49
52
i2c_config_t conf ;
53
+ esp_err_t ret ;
54
+
50
55
memset (& conf , 0 , sizeof (i2c_config_t ));
56
+
57
+ sccb_i2c_port = SCCB_I2C_PORT_DEFAULT ;
58
+ sccb_owns_i2c_port = true;
59
+ ESP_LOGI (TAG , "sccb_i2c_port=%d\n" , sccb_i2c_port );
60
+
51
61
conf .mode = I2C_MODE_MASTER ;
52
62
conf .sda_io_num = pin_sda ;
53
63
conf .sda_pullup_en = GPIO_PULLUP_ENABLE ;
54
64
conf .scl_io_num = pin_scl ;
55
65
conf .scl_pullup_en = GPIO_PULLUP_ENABLE ;
56
66
conf .master .clk_speed = SCCB_FREQ ;
57
67
58
- i2c_param_config (SCCB_I2C_PORT , & conf );
59
- i2c_driver_install (SCCB_I2C_PORT , conf .mode , 0 , 0 , 0 );
60
- return 0 ;
68
+ if ((ret = i2c_param_config (sccb_i2c_port , & conf )) != ESP_OK ) {
69
+ return ret ;
70
+ }
71
+
72
+ return i2c_driver_install (sccb_i2c_port , conf .mode , 0 , 0 , 0 );
73
+ }
74
+
75
+ int SCCB_Use_Port (int i2c_num ) { // sccb use an already initialized I2C port
76
+ if (sccb_owns_i2c_port ) {
77
+ SCCB_Deinit ();
78
+ }
79
+ if (i2c_num < 0 || i2c_num > I2C_NUM_MAX ) {
80
+ return ESP_ERR_INVALID_ARG ;
81
+ }
82
+ sccb_i2c_port = i2c_num ;
83
+ return ESP_OK ;
61
84
}
62
85
63
86
int SCCB_Deinit (void )
64
87
{
65
- return i2c_driver_delete (SCCB_I2C_PORT );
88
+ if (!sccb_owns_i2c_port ) {
89
+ return ESP_OK ;
90
+ }
91
+ sccb_owns_i2c_port = false;
92
+ return i2c_driver_delete (sccb_i2c_port );
66
93
}
67
94
68
95
uint8_t SCCB_Probe (void )
69
96
{
70
97
uint8_t slave_addr = 0x0 ;
71
- // for (size_t i = 1; i < 0x80; i++) {
72
- // i2c_cmd_handle_t cmd = i2c_cmd_link_create();
73
- // i2c_master_start(cmd);
74
- // i2c_master_write_byte(cmd, ( i << 1 ) | WRITE_BIT, ACK_CHECK_EN);
75
- // i2c_master_stop(cmd);
76
- // esp_err_t ret = i2c_master_cmd_begin(SCCB_I2C_PORT, cmd, 1000 / portTICK_RATE_MS);
77
- // i2c_cmd_link_delete(cmd);
78
- // if( ret == ESP_OK) {
79
- // ESP_LOGW(TAG, "Found I2C Device at 0x%02X", i);
80
- // }
81
- // }
98
+
82
99
for (size_t i = 0 ; i < CAMERA_MODEL_MAX ; i ++ ) {
83
100
if (slave_addr == camera_sensor [i ].sccb_addr ) {
84
101
continue ;
@@ -88,7 +105,7 @@ uint8_t SCCB_Probe(void)
88
105
i2c_master_start (cmd );
89
106
i2c_master_write_byte (cmd , ( slave_addr << 1 ) | WRITE_BIT , ACK_CHECK_EN );
90
107
i2c_master_stop (cmd );
91
- esp_err_t ret = i2c_master_cmd_begin (SCCB_I2C_PORT , cmd , 1000 / portTICK_RATE_MS );
108
+ esp_err_t ret = i2c_master_cmd_begin (sccb_i2c_port , cmd , 1000 / portTICK_RATE_MS );
92
109
i2c_cmd_link_delete (cmd );
93
110
if ( ret == ESP_OK ) {
94
111
return slave_addr ;
@@ -106,15 +123,15 @@ uint8_t SCCB_Read(uint8_t slv_addr, uint8_t reg)
106
123
i2c_master_write_byte (cmd , ( slv_addr << 1 ) | WRITE_BIT , ACK_CHECK_EN );
107
124
i2c_master_write_byte (cmd , reg , ACK_CHECK_EN );
108
125
i2c_master_stop (cmd );
109
- ret = i2c_master_cmd_begin (SCCB_I2C_PORT , cmd , 1000 / portTICK_RATE_MS );
126
+ ret = i2c_master_cmd_begin (sccb_i2c_port , cmd , 1000 / portTICK_RATE_MS );
110
127
i2c_cmd_link_delete (cmd );
111
128
if (ret != ESP_OK ) return -1 ;
112
129
cmd = i2c_cmd_link_create ();
113
130
i2c_master_start (cmd );
114
131
i2c_master_write_byte (cmd , ( slv_addr << 1 ) | READ_BIT , ACK_CHECK_EN );
115
132
i2c_master_read_byte (cmd , & data , NACK_VAL );
116
133
i2c_master_stop (cmd );
117
- ret = i2c_master_cmd_begin (SCCB_I2C_PORT , cmd , 1000 / portTICK_RATE_MS );
134
+ ret = i2c_master_cmd_begin (sccb_i2c_port , cmd , 1000 / portTICK_RATE_MS );
118
135
i2c_cmd_link_delete (cmd );
119
136
if (ret != ESP_OK ) {
120
137
ESP_LOGE (TAG , "SCCB_Read Failed addr:0x%02x, reg:0x%02x, data:0x%02x, ret:%d" , slv_addr , reg , data , ret );
@@ -131,7 +148,7 @@ uint8_t SCCB_Write(uint8_t slv_addr, uint8_t reg, uint8_t data)
131
148
i2c_master_write_byte (cmd , reg , ACK_CHECK_EN );
132
149
i2c_master_write_byte (cmd , data , ACK_CHECK_EN );
133
150
i2c_master_stop (cmd );
134
- ret = i2c_master_cmd_begin (SCCB_I2C_PORT , cmd , 1000 / portTICK_RATE_MS );
151
+ ret = i2c_master_cmd_begin (sccb_i2c_port , cmd , 1000 / portTICK_RATE_MS );
135
152
i2c_cmd_link_delete (cmd );
136
153
if (ret != ESP_OK ) {
137
154
ESP_LOGE (TAG , "SCCB_Write Failed addr:0x%02x, reg:0x%02x, data:0x%02x, ret:%d" , slv_addr , reg , data , ret );
@@ -151,15 +168,15 @@ uint8_t SCCB_Read16(uint8_t slv_addr, uint16_t reg)
151
168
i2c_master_write_byte (cmd , reg_u8 [0 ], ACK_CHECK_EN );
152
169
i2c_master_write_byte (cmd , reg_u8 [1 ], ACK_CHECK_EN );
153
170
i2c_master_stop (cmd );
154
- ret = i2c_master_cmd_begin (SCCB_I2C_PORT , cmd , 1000 / portTICK_RATE_MS );
171
+ ret = i2c_master_cmd_begin (sccb_i2c_port , cmd , 1000 / portTICK_RATE_MS );
155
172
i2c_cmd_link_delete (cmd );
156
173
if (ret != ESP_OK ) return -1 ;
157
174
cmd = i2c_cmd_link_create ();
158
175
i2c_master_start (cmd );
159
176
i2c_master_write_byte (cmd , ( slv_addr << 1 ) | READ_BIT , ACK_CHECK_EN );
160
177
i2c_master_read_byte (cmd , & data , NACK_VAL );
161
178
i2c_master_stop (cmd );
162
- ret = i2c_master_cmd_begin (SCCB_I2C_PORT , cmd , 1000 / portTICK_RATE_MS );
179
+ ret = i2c_master_cmd_begin (sccb_i2c_port , cmd , 1000 / portTICK_RATE_MS );
163
180
i2c_cmd_link_delete (cmd );
164
181
if (ret != ESP_OK ) {
165
182
ESP_LOGE (TAG , "W [%04x]=%02x fail\n" , reg , data );
@@ -180,7 +197,7 @@ uint8_t SCCB_Write16(uint8_t slv_addr, uint16_t reg, uint8_t data)
180
197
i2c_master_write_byte (cmd , reg_u8 [1 ], ACK_CHECK_EN );
181
198
i2c_master_write_byte (cmd , data , ACK_CHECK_EN );
182
199
i2c_master_stop (cmd );
183
- ret = i2c_master_cmd_begin (SCCB_I2C_PORT , cmd , 1000 / portTICK_RATE_MS );
200
+ ret = i2c_master_cmd_begin (sccb_i2c_port , cmd , 1000 / portTICK_RATE_MS );
184
201
i2c_cmd_link_delete (cmd );
185
202
if (ret != ESP_OK ) {
186
203
ESP_LOGE (TAG , "W [%04x]=%02x %d fail\n" , reg , data , i ++ );
0 commit comments