@@ -38,47 +38,64 @@ static const char* TAG = "sccb";
3838#define ACK_VAL 0x0 /*!< I2C ack value */
3939#define NACK_VAL 0x1 /*!< I2C nack value */
4040#if CONFIG_SCCB_HARDWARE_I2C_PORT1
41- const int SCCB_I2C_PORT = 1 ;
41+ const int SCCB_I2C_PORT_DEFAULT = 1 ;
4242#else
43- const int SCCB_I2C_PORT = 0 ;
43+ const int SCCB_I2C_PORT_DEFAULT = 0 ;
4444#endif
4545
46+ static int sccb_i2c_port ;
47+ static bool sccb_owns_i2c_port ;
48+
4649int SCCB_Init (int pin_sda , int pin_scl )
4750{
4851 ESP_LOGI (TAG , "pin_sda %d pin_scl %d" , pin_sda , pin_scl );
4952 i2c_config_t conf ;
53+ esp_err_t ret ;
54+
5055 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+
5161 conf .mode = I2C_MODE_MASTER ;
5262 conf .sda_io_num = pin_sda ;
5363 conf .sda_pullup_en = GPIO_PULLUP_ENABLE ;
5464 conf .scl_io_num = pin_scl ;
5565 conf .scl_pullup_en = GPIO_PULLUP_ENABLE ;
5666 conf .master .clk_speed = SCCB_FREQ ;
5767
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 ;
6184}
6285
6386int SCCB_Deinit (void )
6487{
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 );
6693}
6794
6895uint8_t SCCB_Probe (void )
6996{
7097 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+
8299 for (size_t i = 0 ; i < CAMERA_MODEL_MAX ; i ++ ) {
83100 if (slave_addr == camera_sensor [i ].sccb_addr ) {
84101 continue ;
@@ -88,7 +105,7 @@ uint8_t SCCB_Probe(void)
88105 i2c_master_start (cmd );
89106 i2c_master_write_byte (cmd , ( slave_addr << 1 ) | WRITE_BIT , ACK_CHECK_EN );
90107 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 );
92109 i2c_cmd_link_delete (cmd );
93110 if ( ret == ESP_OK ) {
94111 return slave_addr ;
@@ -106,15 +123,15 @@ uint8_t SCCB_Read(uint8_t slv_addr, uint8_t reg)
106123 i2c_master_write_byte (cmd , ( slv_addr << 1 ) | WRITE_BIT , ACK_CHECK_EN );
107124 i2c_master_write_byte (cmd , reg , ACK_CHECK_EN );
108125 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 );
110127 i2c_cmd_link_delete (cmd );
111128 if (ret != ESP_OK ) return -1 ;
112129 cmd = i2c_cmd_link_create ();
113130 i2c_master_start (cmd );
114131 i2c_master_write_byte (cmd , ( slv_addr << 1 ) | READ_BIT , ACK_CHECK_EN );
115132 i2c_master_read_byte (cmd , & data , NACK_VAL );
116133 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 );
118135 i2c_cmd_link_delete (cmd );
119136 if (ret != ESP_OK ) {
120137 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)
131148 i2c_master_write_byte (cmd , reg , ACK_CHECK_EN );
132149 i2c_master_write_byte (cmd , data , ACK_CHECK_EN );
133150 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 );
135152 i2c_cmd_link_delete (cmd );
136153 if (ret != ESP_OK ) {
137154 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)
151168 i2c_master_write_byte (cmd , reg_u8 [0 ], ACK_CHECK_EN );
152169 i2c_master_write_byte (cmd , reg_u8 [1 ], ACK_CHECK_EN );
153170 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 );
155172 i2c_cmd_link_delete (cmd );
156173 if (ret != ESP_OK ) return -1 ;
157174 cmd = i2c_cmd_link_create ();
158175 i2c_master_start (cmd );
159176 i2c_master_write_byte (cmd , ( slv_addr << 1 ) | READ_BIT , ACK_CHECK_EN );
160177 i2c_master_read_byte (cmd , & data , NACK_VAL );
161178 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 );
163180 i2c_cmd_link_delete (cmd );
164181 if (ret != ESP_OK ) {
165182 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)
180197 i2c_master_write_byte (cmd , reg_u8 [1 ], ACK_CHECK_EN );
181198 i2c_master_write_byte (cmd , data , ACK_CHECK_EN );
182199 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 );
184201 i2c_cmd_link_delete (cmd );
185202 if (ret != ESP_OK ) {
186203 ESP_LOGE (TAG , "W [%04x]=%02x %d fail\n" , reg , data , i ++ );
0 commit comments