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"
@@ -47,6 +45,9 @@ static lv_indev_t *disp_indev = NULL;
4745static esp_lcd_touch_handle_t tp ; // LCD touch handle
4846sdmmc_card_t * bsp_sdcard = NULL ; // Global SD card handler
4947static bool i2c_initialized = false;
48+ static i2c_master_bus_handle_t i2c_handle = NULL ; // I2C Handle
49+ static i2c_master_dev_handle_t axp2101_h = NULL ;
50+ static i2c_master_dev_handle_t aw9523_h = NULL ;
5051static bool spi_initialized = false;
5152
5253esp_err_t bsp_i2c_init (void )
@@ -56,25 +57,35 @@ esp_err_t bsp_i2c_init(void)
5657 return ESP_OK ;
5758 }
5859
59- const i2c_config_t i2c_conf = {
60- .mode = I2C_MODE_MASTER ,
60+ const i2c_master_bus_config_t i2c_config = {
61+ .i2c_port = BSP_I2C_NUM ,
6162 .sda_io_num = BSP_I2C_SDA ,
62- .sda_pullup_en = GPIO_PULLUP_DISABLE ,
6363 .scl_io_num = BSP_I2C_SCL ,
64- .scl_pullup_en = GPIO_PULLUP_DISABLE ,
65- .master .clk_speed = CONFIG_BSP_I2C_CLK_SPEED_HZ
64+ .clk_source = 1 ,
6665 };
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 ));
66+ BSP_ERROR_CHECK_RETURN_ERR (i2c_new_master_bus (& i2c_config , & i2c_handle ));
6967
70- i2c_initialized = true;
68+ // AXP2101 and AW9523 are managed by this BSP
69+ const i2c_device_config_t axp2101_config = {
70+ .dev_addr_length = I2C_ADDR_BIT_LEN_7 ,
71+ .device_address = BSP_AXP2101_ADDR ,
72+ .scl_speed_hz = 400000 ,
73+ };
74+ BSP_ERROR_CHECK_RETURN_ERR (i2c_master_bus_add_device (i2c_handle , & axp2101_config , & axp2101_h ));
75+ const i2c_device_config_t aw9523_config = {
76+ .dev_addr_length = I2C_ADDR_BIT_LEN_7 ,
77+ .device_address = BSP_AW9523_ADDR ,
78+ .scl_speed_hz = 400000 ,
79+ };
80+ BSP_ERROR_CHECK_RETURN_ERR (i2c_master_bus_add_device (i2c_handle , & aw9523_config , & aw9523_h ));
7181
82+ i2c_initialized = true;
7283 return ESP_OK ;
7384}
7485
7586esp_err_t bsp_i2c_deinit (void )
7687{
77- BSP_ERROR_CHECK_RETURN_ERR (i2c_driver_delete ( BSP_I2C_NUM ));
88+ BSP_ERROR_CHECK_RETURN_ERR (i2c_del_master_bus ( i2c_handle ));
7889 i2c_initialized = false;
7990 return ESP_OK ;
8091}
@@ -102,27 +113,27 @@ static esp_err_t bsp_enable_feature(bsp_feature_t feature)
102113 /* AXP ALDO4 voltage / SD Card / 3V3 */
103114 data [0 ] = 0x95 ;
104115 data [1 ] = 0b00011100 ; //3V3
105- err |= i2c_master_write_to_device ( BSP_I2C_NUM , BSP_AXP2101_ADDR , data , sizeof (data ), 1000 / portTICK_PERIOD_MS );
116+ err |= i2c_master_transmit ( axp2101_h , data , sizeof (data ), 1000 );
106117 /* Enable SD */
107118 aw9523_P0 |= (1 << 4 );
108119 break ;
109120 case BSP_FEATURE_SPEAKER :
110121 /* AXP ALDO1 voltage / PA PVDD / 1V8 */
111122 data [0 ] = 0x92 ;
112123 data [1 ] = 0b00001101 ; //1V8
113- err |= i2c_master_write_to_device ( BSP_I2C_NUM , BSP_AXP2101_ADDR , data , sizeof (data ), 1000 / portTICK_PERIOD_MS );
124+ err |= i2c_master_transmit ( axp2101_h , data , sizeof (data ), 1000 );
114125 /* AXP ALDO2 voltage / Codec / 3V3 */
115126 data [0 ] = 0x93 ;
116127 data [1 ] = 0b00011100 ; //3V3
117- err |= i2c_master_write_to_device ( BSP_I2C_NUM , BSP_AXP2101_ADDR , data , sizeof (data ), 1000 / portTICK_PERIOD_MS );
128+ err |= i2c_master_transmit ( axp2101_h , data , sizeof (data ), 1000 );
118129 /* AXP ALDO3 voltage / Codec+Mic / 3V3 */
119130 data [0 ] = 0x94 ;
120131 data [1 ] = 0b00011100 ; //3V3
121- err |= i2c_master_write_to_device ( BSP_I2C_NUM , BSP_AXP2101_ADDR , data , sizeof (data ), 1000 / portTICK_PERIOD_MS );
132+ err |= i2c_master_transmit ( axp2101_h , data , sizeof (data ), 1000 );
122133 /* AW9523 P0 is in push-pull mode */
123134 data [0 ] = 0x11 ;
124135 data [1 ] = 0x10 ;
125- err |= i2c_master_write_to_device ( BSP_I2C_NUM , BSP_AW9523_ADDR , data , sizeof (data ), 1000 / portTICK_PERIOD_MS );
136+ err |= i2c_master_transmit ( aw9523_h , data , sizeof (data ), 1000 );
126137 /* Enable Codec AW88298 */
127138 aw9523_P0 |= (1 << 2 );
128139 break ;
@@ -134,11 +145,11 @@ static esp_err_t bsp_enable_feature(bsp_feature_t feature)
134145
135146 data [0 ] = 0x02 ;
136147 data [1 ] = aw9523_P0 ;
137- err |= i2c_master_write_to_device ( BSP_I2C_NUM , BSP_AW9523_ADDR , data , sizeof (data ), 1000 / portTICK_PERIOD_MS );
148+ err |= i2c_master_transmit ( aw9523_h , data , sizeof (data ), 1000 );
138149
139150 data [0 ] = 0x03 ;
140151 data [1 ] = aw9523_P1 ;
141- err |= i2c_master_write_to_device ( BSP_I2C_NUM , BSP_AW9523_ADDR , data , sizeof (data ), 1000 / portTICK_PERIOD_MS );
152+ err |= i2c_master_transmit ( aw9523_h , data , sizeof (data ), 1000 );
142153
143154 return err ;
144155}
@@ -246,6 +257,7 @@ esp_codec_dev_handle_t bsp_audio_codec_speaker_init(void)
246257 audio_codec_i2c_cfg_t i2c_cfg = {
247258 .port = BSP_I2C_NUM ,
248259 .addr = AW88298_CODEC_DEFAULT_ADDR ,
260+ .bus_handle = i2c_handle ,
249261 };
250262 const audio_codec_ctrl_if_t * out_ctrl_if = audio_codec_new_i2c_ctrl (& i2c_cfg );
251263
@@ -272,7 +284,7 @@ esp_codec_dev_handle_t bsp_audio_codec_microphone_init(void)
272284{
273285 const audio_codec_data_if_t * i2s_data_if = bsp_audio_get_codec_itf ();
274286 if (i2s_data_if == NULL ) {
275- /* Initilize I2C */
287+ /* Initialize I2C */
276288 BSP_ERROR_CHECK_RETURN_NULL (bsp_i2c_init ());
277289 /* Configure I2S peripheral and Power Amplifier */
278290 BSP_ERROR_CHECK_RETURN_NULL (bsp_audio_init (NULL ));
@@ -283,6 +295,7 @@ esp_codec_dev_handle_t bsp_audio_codec_microphone_init(void)
283295 audio_codec_i2c_cfg_t i2c_cfg = {
284296 .port = BSP_I2C_NUM ,
285297 .addr = ES7210_CODEC_DEFAULT_ADDR ,
298+ .bus_handle = i2c_handle ,
286299 };
287300 const audio_codec_ctrl_if_t * i2c_ctrl_if = audio_codec_new_i2c_ctrl (& i2c_cfg );
288301 BSP_NULL_CHECK (i2c_ctrl_if , NULL );
@@ -312,9 +325,9 @@ esp_err_t bsp_display_brightness_init(void)
312325 BSP_ERROR_CHECK_RETURN_ERR (bsp_i2c_init ());
313326
314327 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" );
328+ ESP_RETURN_ON_ERROR (i2c_master_transmit ( axp2101_h , lcd_bl_en , sizeof (lcd_bl_en ), 1000 ), TAG , "I2C write failed" );
316329 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" );
330+ ESP_RETURN_ON_ERROR (i2c_master_transmit ( axp2101_h , lcd_bl_val , sizeof (lcd_bl_val ), 1000 ), TAG , "I2C write failed" );
318331
319332 return ESP_OK ;
320333}
@@ -331,7 +344,7 @@ esp_err_t bsp_display_brightness_set(int brightness_percent)
331344 ESP_LOGI (TAG , "Setting LCD backlight: %d%%" , brightness_percent );
332345 const uint8_t reg_val = 20 + ((8 * brightness_percent ) / 100 ); // 0b00000 ~ 0b11100; under 20, it is too dark
333346 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" );
347+ ESP_RETURN_ON_ERROR (i2c_master_transmit ( axp2101_h , lcd_bl_val , sizeof (lcd_bl_val ), 1000 ), TAG , "I2C write failed" );
335348
336349 return ESP_OK ;
337350}
@@ -414,8 +427,9 @@ esp_err_t bsp_touch_new(const bsp_touch_config_t *config, esp_lcd_touch_handle_t
414427 },
415428 };
416429 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 , "" );
430+ esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_FT5x06_CONFIG ();
431+ tp_io_config .scl_speed_hz = 400000 ; // This parameter was introduce together with I2C Driver-NG in IDF v5.2
432+ ESP_RETURN_ON_ERROR (esp_lcd_new_panel_io_i2c (i2c_handle , & tp_io_config , & tp_io_handle ), TAG , "" );
419433 return esp_lcd_touch_new_i2c_ft5x06 (tp_io_handle , & tp_cfg , ret_touch );
420434}
421435
0 commit comments