1414#include "esp_lcd_panel_vendor.h"
1515#include "esp_lcd_panel_ops.h"
1616#include "esp_vfs_fat.h"
17- #include "driver/sdmmc_host.h"
1817#include "driver/sdspi_host.h"
19- #include "driver/i2c.h"
2018
2119#include "bsp/m5stack_core_s3.h"
2220#include "bsp/display.h"
@@ -46,7 +44,17 @@ static lv_indev_t *disp_indev = NULL;
4644#endif // (BSP_CONFIG_NO_GRAPHIC_LIB == 0)
4745static esp_lcd_touch_handle_t tp ; // LCD touch handle
4846sdmmc_card_t * bsp_sdcard = NULL ; // Global SD card handler
47+
48+ /**
49+ * @brief I2C handle for BSP usage
50+ *
51+ * You can call i2c_master_get_bus_handle(BSP_I2C_NUM, i2c_master_bus_handle_t *ret_handle)
52+ * from #include "esp_private/i2c_platforma.h"
53+ */
54+ static i2c_master_bus_handle_t i2c_handle = NULL ;
4955static bool i2c_initialized = false;
56+ static i2c_master_dev_handle_t axp2101_h = NULL ;
57+ static i2c_master_dev_handle_t aw9523_h = NULL ;
5058static bool spi_initialized = false;
5159
5260esp_err_t bsp_i2c_init (void )
@@ -56,25 +64,35 @@ esp_err_t bsp_i2c_init(void)
5664 return ESP_OK ;
5765 }
5866
59- const i2c_config_t i2c_conf = {
60- .mode = I2C_MODE_MASTER ,
67+ const i2c_master_bus_config_t i2c_config = {
68+ .i2c_port = BSP_I2C_NUM ,
6169 .sda_io_num = BSP_I2C_SDA ,
62- .sda_pullup_en = GPIO_PULLUP_DISABLE ,
6370 .scl_io_num = BSP_I2C_SCL ,
64- .scl_pullup_en = GPIO_PULLUP_DISABLE ,
65- .master .clk_speed = CONFIG_BSP_I2C_CLK_SPEED_HZ
71+ .clk_source = 1 ,
6672 };
67- BSP_ERROR_CHECK_RETURN_ERR (i2c_param_config (BSP_I2C_NUM , & i2c_conf ));
68- BSP_ERROR_CHECK_RETURN_ERR (i2c_driver_install (BSP_I2C_NUM , i2c_conf .mode , 0 , 0 , 0 ));
73+ BSP_ERROR_CHECK_RETURN_ERR (i2c_new_master_bus (& i2c_config , & i2c_handle ));
6974
70- i2c_initialized = true;
75+ // AXP2101 and AW9523 are managed by this BSP
76+ const i2c_device_config_t axp2101_config = {
77+ .dev_addr_length = I2C_ADDR_BIT_LEN_7 ,
78+ .device_address = BSP_AXP2101_ADDR ,
79+ .scl_speed_hz = 400000 ,
80+ };
81+ BSP_ERROR_CHECK_RETURN_ERR (i2c_master_bus_add_device (i2c_handle , & axp2101_config , & axp2101_h ));
82+ const i2c_device_config_t aw9523_config = {
83+ .dev_addr_length = I2C_ADDR_BIT_LEN_7 ,
84+ .device_address = BSP_AW9523_ADDR ,
85+ .scl_speed_hz = 400000 ,
86+ };
87+ BSP_ERROR_CHECK_RETURN_ERR (i2c_master_bus_add_device (i2c_handle , & aw9523_config , & aw9523_h ));
7188
89+ i2c_initialized = true;
7290 return ESP_OK ;
7391}
7492
7593esp_err_t bsp_i2c_deinit (void )
7694{
77- BSP_ERROR_CHECK_RETURN_ERR (i2c_driver_delete ( BSP_I2C_NUM ));
95+ BSP_ERROR_CHECK_RETURN_ERR (i2c_del_master_bus ( i2c_handle ));
7896 i2c_initialized = false;
7997 return ESP_OK ;
8098}
@@ -102,27 +120,27 @@ static esp_err_t bsp_enable_feature(bsp_feature_t feature)
102120 /* AXP ALDO4 voltage / SD Card / 3V3 */
103121 data [0 ] = 0x95 ;
104122 data [1 ] = 0b00011100 ; //3V3
105- err |= i2c_master_write_to_device ( BSP_I2C_NUM , BSP_AXP2101_ADDR , data , sizeof (data ), 1000 / portTICK_PERIOD_MS );
123+ err |= i2c_master_transmit ( axp2101_h , data , sizeof (data ), 1000 );
106124 /* Enable SD */
107125 aw9523_P0 |= (1 << 4 );
108126 break ;
109127 case BSP_FEATURE_SPEAKER :
110128 /* AXP ALDO1 voltage / PA PVDD / 1V8 */
111129 data [0 ] = 0x92 ;
112130 data [1 ] = 0b00001101 ; //1V8
113- err |= i2c_master_write_to_device ( BSP_I2C_NUM , BSP_AXP2101_ADDR , data , sizeof (data ), 1000 / portTICK_PERIOD_MS );
131+ err |= i2c_master_transmit ( axp2101_h , data , sizeof (data ), 1000 );
114132 /* AXP ALDO2 voltage / Codec / 3V3 */
115133 data [0 ] = 0x93 ;
116134 data [1 ] = 0b00011100 ; //3V3
117- err |= i2c_master_write_to_device ( BSP_I2C_NUM , BSP_AXP2101_ADDR , data , sizeof (data ), 1000 / portTICK_PERIOD_MS );
135+ err |= i2c_master_transmit ( axp2101_h , data , sizeof (data ), 1000 );
118136 /* AXP ALDO3 voltage / Codec+Mic / 3V3 */
119137 data [0 ] = 0x94 ;
120138 data [1 ] = 0b00011100 ; //3V3
121- err |= i2c_master_write_to_device ( BSP_I2C_NUM , BSP_AXP2101_ADDR , data , sizeof (data ), 1000 / portTICK_PERIOD_MS );
139+ err |= i2c_master_transmit ( axp2101_h , data , sizeof (data ), 1000 );
122140 /* AW9523 P0 is in push-pull mode */
123141 data [0 ] = 0x11 ;
124142 data [1 ] = 0x10 ;
125- err |= i2c_master_write_to_device ( BSP_I2C_NUM , BSP_AW9523_ADDR , data , sizeof (data ), 1000 / portTICK_PERIOD_MS );
143+ err |= i2c_master_transmit ( aw9523_h , data , sizeof (data ), 1000 );
126144 /* Enable Codec AW88298 */
127145 aw9523_P0 |= (1 << 2 );
128146 break ;
@@ -134,11 +152,11 @@ static esp_err_t bsp_enable_feature(bsp_feature_t feature)
134152
135153 data [0 ] = 0x02 ;
136154 data [1 ] = aw9523_P0 ;
137- err |= i2c_master_write_to_device ( BSP_I2C_NUM , BSP_AW9523_ADDR , data , sizeof (data ), 1000 / portTICK_PERIOD_MS );
155+ err |= i2c_master_transmit ( aw9523_h , data , sizeof (data ), 1000 );
138156
139157 data [0 ] = 0x03 ;
140158 data [1 ] = aw9523_P1 ;
141- err |= i2c_master_write_to_device ( BSP_I2C_NUM , BSP_AW9523_ADDR , data , sizeof (data ), 1000 / portTICK_PERIOD_MS );
159+ err |= i2c_master_transmit ( aw9523_h , data , sizeof (data ), 1000 );
142160
143161 return err ;
144162}
@@ -246,6 +264,7 @@ esp_codec_dev_handle_t bsp_audio_codec_speaker_init(void)
246264 audio_codec_i2c_cfg_t i2c_cfg = {
247265 .port = BSP_I2C_NUM ,
248266 .addr = AW88298_CODEC_DEFAULT_ADDR ,
267+ .bus_handle = i2c_handle ,
249268 };
250269 const audio_codec_ctrl_if_t * out_ctrl_if = audio_codec_new_i2c_ctrl (& i2c_cfg );
251270
@@ -272,7 +291,7 @@ esp_codec_dev_handle_t bsp_audio_codec_microphone_init(void)
272291{
273292 const audio_codec_data_if_t * i2s_data_if = bsp_audio_get_codec_itf ();
274293 if (i2s_data_if == NULL ) {
275- /* Initilize I2C */
294+ /* Initialize I2C */
276295 BSP_ERROR_CHECK_RETURN_NULL (bsp_i2c_init ());
277296 /* Configure I2S peripheral and Power Amplifier */
278297 BSP_ERROR_CHECK_RETURN_NULL (bsp_audio_init (NULL ));
@@ -283,6 +302,7 @@ esp_codec_dev_handle_t bsp_audio_codec_microphone_init(void)
283302 audio_codec_i2c_cfg_t i2c_cfg = {
284303 .port = BSP_I2C_NUM ,
285304 .addr = ES7210_CODEC_DEFAULT_ADDR ,
305+ .bus_handle = i2c_handle ,
286306 };
287307 const audio_codec_ctrl_if_t * i2c_ctrl_if = audio_codec_new_i2c_ctrl (& i2c_cfg );
288308 BSP_NULL_CHECK (i2c_ctrl_if , NULL );
@@ -312,9 +332,9 @@ esp_err_t bsp_display_brightness_init(void)
312332 BSP_ERROR_CHECK_RETURN_ERR (bsp_i2c_init ());
313333
314334 const uint8_t lcd_bl_en [] = { 0x90 , 0xBF }; // AXP DLDO1 Enable
315- ESP_RETURN_ON_ERROR (i2c_master_write_to_device ( BSP_I2C_NUM , BSP_AXP2101_ADDR , lcd_bl_en , sizeof (lcd_bl_en ), 1000 / portTICK_PERIOD_MS ), TAG , "I2C write failed" );
335+ ESP_RETURN_ON_ERROR (i2c_master_transmit ( axp2101_h , lcd_bl_en , sizeof (lcd_bl_en ), 1000 ), TAG , "I2C write failed" );
316336 const uint8_t lcd_bl_val [] = { 0x99 , 0b00011000 }; // AXP DLDO1 voltage
317- ESP_RETURN_ON_ERROR (i2c_master_write_to_device ( BSP_I2C_NUM , BSP_AXP2101_ADDR , lcd_bl_val , sizeof (lcd_bl_val ), 1000 / portTICK_PERIOD_MS ), TAG , "I2C write failed" );
337+ ESP_RETURN_ON_ERROR (i2c_master_transmit ( axp2101_h , lcd_bl_val , sizeof (lcd_bl_val ), 1000 ), TAG , "I2C write failed" );
318338
319339 return ESP_OK ;
320340}
@@ -331,7 +351,7 @@ esp_err_t bsp_display_brightness_set(int brightness_percent)
331351 ESP_LOGI (TAG , "Setting LCD backlight: %d%%" , brightness_percent );
332352 const uint8_t reg_val = 20 + ((8 * brightness_percent ) / 100 ); // 0b00000 ~ 0b11100; under 20, it is too dark
333353 const uint8_t lcd_bl_val [] = { 0x99 , reg_val }; // AXP DLDO1 voltage
334- ESP_RETURN_ON_ERROR (i2c_master_write_to_device ( BSP_I2C_NUM , BSP_AXP2101_ADDR , lcd_bl_val , sizeof (lcd_bl_val ), 1000 / portTICK_PERIOD_MS ), TAG , "I2C write failed" );
354+ ESP_RETURN_ON_ERROR (i2c_master_transmit ( axp2101_h , lcd_bl_val , sizeof (lcd_bl_val ), 1000 ), TAG , "I2C write failed" );
335355
336356 return ESP_OK ;
337357}
@@ -414,8 +434,9 @@ esp_err_t bsp_touch_new(const bsp_touch_config_t *config, esp_lcd_touch_handle_t
414434 },
415435 };
416436 esp_lcd_panel_io_handle_t tp_io_handle = NULL ;
417- const esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_FT5x06_CONFIG ();
418- ESP_RETURN_ON_ERROR (esp_lcd_new_panel_io_i2c ((esp_lcd_i2c_bus_handle_t )BSP_I2C_NUM , & tp_io_config , & tp_io_handle ), TAG , "" );
437+ esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_FT5x06_CONFIG ();
438+ tp_io_config .scl_speed_hz = 400000 ; // This parameter was introduce together with I2C Driver-NG in IDF v5.2
439+ ESP_RETURN_ON_ERROR (esp_lcd_new_panel_io_i2c (i2c_handle , & tp_io_config , & tp_io_handle ), TAG , "" );
419440 return esp_lcd_touch_new_i2c_ft5x06 (tp_io_handle , & tp_cfg , ret_touch );
420441}
421442
0 commit comments