2121
2222
2323mp_lcd_err_t i2c_del (mp_obj_t obj );
24- mp_lcd_err_t i2c_init (mp_obj_t obj , uint16_t width , uint16_t height , uint8_t bpp , uint32_t buffer_size , bool rgb565_byte_swap , uint8_t cmd_bits , uint8_t param_bits , bool sw_rotation );
25- mp_lcd_err_t i2c_get_lane_count (mp_obj_t obj , uint8_t * lane_count );
24+ mp_lcd_err_t i2c_init (mp_obj_t obj , uint16_t width , uint16_t height , uint8_t bpp , uint32_t buffer_size , bool rgb565_byte_swap , uint8_t cmd_bits , uint8_t param_bits , bool sw_rotate );
2625mp_lcd_err_t i2c_tx_param (mp_obj_t obj , int lcd_cmd , void * param , size_t param_size , bool is_flush , bool last_flush_cmd );
2726
2827
2928static bool i2c_bus_trans_done_cb (esp_lcd_panel_io_handle_t panel_io , esp_lcd_panel_io_event_data_t * edata , void * user_ctx )
3029{
3130 LCD_UNUSED (panel_io );
3231
33- mp_lcd_spi_bus_obj_t * self = (mp_lcd_spi_bus_obj_t * )user_ctx ;
32+ mp_lcd_i2c_bus_obj_t * self = (mp_lcd_i2c_bus_obj_t * )user_ctx ;
3433 bus_event_set_from_isr (& self -> rotation -> task .swap_bufs );
3534 return false;
3635}
@@ -40,31 +39,34 @@ static mp_lcd_err_t i2c_rotation_init_func(void *self_in)
4039{
4140 mp_lcd_i2c_bus_obj_t * self = (mp_lcd_i2c_bus_obj_t * )self_in ;
4241
43- self -> panel_io_config . on_color_trans_done = & i2c_bus_trans_done_cb ;
42+ self -> panel_io_config -> on_color_trans_done = & i2c_bus_trans_done_cb ;
4443
45- rotation_t * rotation = self -> rotation ;
46- rotation_init_err_t * init_err = & rotation -> init_err ;
47- rotation_task_t * task = & rotation -> task ;
44+ rotation_init_err_t * init_err = & self -> rotation -> init_err ;
4845
46+ init_err -> code = i2c_param_config (self -> host , self -> bus_config );
47+ if (init_err -> code != LCD_OK ) {
48+ init_err -> msg = MP_ERROR_TEXT ("%d(i2c_param_config)" );
49+ return init_err -> code ;
50+ }
4951
50- init_err -> code = i2c_param_config (self -> host , & self -> bus_config );
52+ init_err -> code = i2c_driver_install (self -> host , I2C_MODE_MASTER , 0 , 0 , 0 );
5153 if (init_err -> code != LCD_OK ) {
52- init_err -> err_msg = MP_ERROR_TEXT ("%d(i2c_param_config)" );
53- bus_lock_release (& task -> init_lock );
54- } else {
55- init_err -> code = i2c_driver_install (self -> host , I2C_MODE_MASTER , 0 , 0 , 0 );
56- if (init_err -> code != LCD_OK ) {
57- init_err -> err_msg = MP_ERROR_TEXT ("%d(i2c_driver_install)" );
58- bus_lock_release (& task -> init_lock );
59- } else {
60- init_err -> code = esp_lcd_new_panel_io_i2c (self -> bus_handle , & self -> panel_io_config , & self -> panel_io_handle .panel_io );
61- if (init_err -> code != LCD_OK ) {
62- init_err -> err_msg = MP_ERROR_TEXT ("%d(esp_lcd_new_panel_io_i2c)" );
63- bus_lock_release (& task -> init_lock );
64- }
65- }
54+ init_err -> msg = MP_ERROR_TEXT ("%d(i2c_driver_install)" );
55+ return init_err -> code ;
56+ }
57+
58+ init_err -> code = esp_lcd_new_panel_io_i2c (self -> bus_handle , self -> panel_io_config , & self -> panel_io_handle .panel_io );
59+ if (init_err -> code != LCD_OK ) {
60+ init_err -> msg = MP_ERROR_TEXT ("%d(esp_lcd_new_panel_io_i2c)" );
61+ return init_err -> code ;
6662 }
6763
64+ free (self -> bus_config );
65+ self -> bus_config = NULL ;
66+
67+ free (self -> panel_io_config );
68+ self -> panel_io_config = NULL ;
69+
6870 return init_err -> code ;
6971}
7072
@@ -115,28 +117,29 @@ static mp_obj_t mp_lcd_i2c_bus_make_new(const mp_obj_type_t *type, size_t n_args
115117 self -> base .type = & mp_lcd_i2c_bus_type ;
116118
117119 self -> callback = mp_const_none ;
118-
120+ self -> lane_count = 1 ;
119121 self -> host = args [ARG_host ].u_int ;
120122 self -> bus_handle = (esp_lcd_i2c_bus_handle_t )((uint32_t )self -> host );
121123
122- self -> bus_config .mode = I2C_MODE_MASTER ;
123- self -> bus_config .sda_io_num = (int )args [ARG_sda ].u_int ;
124- self -> bus_config .scl_io_num = (int )args [ARG_scl ].u_int ;
125- self -> bus_config .sda_pullup_en = (bool )args [ARG_sda_pullup ].u_bool ;
126- self -> bus_config .scl_pullup_en = (bool )args [ARG_scl_pullup ].u_bool ;
127- self -> bus_config .master .clk_speed = (uint32_t )args [ARG_freq ].u_int ;
128- self -> bus_config .clk_flags = I2C_SCLK_SRC_FLAG_FOR_NOMAL ;
129-
130- self -> panel_io_config .dev_addr = (uint32_t )args [ARG_addr ].u_int ;
131- self -> panel_io_config .user_ctx = self ;
132- self -> panel_io_config .control_phase_bytes = (size_t )args [ARG_control_phase_bytes ].u_int ;
133- self -> panel_io_config .dc_bit_offset = (unsigned int )args [ARG_dc_bit_offset ].u_int ;
134- self -> panel_io_config .flags .dc_low_on_data = (unsigned int )args [ARG_dc_low_on_data ].u_bool ;
135- self -> panel_io_config .flags .disable_control_phase = (unsigned int )args [ARG_disable_control_phase ].u_bool ;
124+ self -> bus_config = (i2c_config_t * )malloc (sizeof (i2c_config_t ));
125+ self -> bus_config -> mode = I2C_MODE_MASTER ;
126+ self -> bus_config -> sda_io_num = (int )args [ARG_sda ].u_int ;
127+ self -> bus_config -> scl_io_num = (int )args [ARG_scl ].u_int ;
128+ self -> bus_config -> sda_pullup_en = (bool )args [ARG_sda_pullup ].u_bool ;
129+ self -> bus_config -> scl_pullup_en = (bool )args [ARG_scl_pullup ].u_bool ;
130+ self -> bus_config -> master .clk_speed = (uint32_t )args [ARG_freq ].u_int ;
131+ self -> bus_config -> clk_flags = I2C_SCLK_SRC_FLAG_FOR_NOMAL ;
132+
133+ self -> panel_io_config = (esp_lcd_panel_io_i2c_config_t * )malloc (sizeof (esp_lcd_panel_io_i2c_config_t ));
134+ self -> panel_io_config -> dev_addr = (uint32_t )args [ARG_addr ].u_int ;
135+ self -> panel_io_config -> user_ctx = self ;
136+ self -> panel_io_config -> control_phase_bytes = (size_t )args [ARG_control_phase_bytes ].u_int ;
137+ self -> panel_io_config -> dc_bit_offset = (unsigned int )args [ARG_dc_bit_offset ].u_int ;
138+ self -> panel_io_config -> flags .dc_low_on_data = (unsigned int )args [ARG_dc_low_on_data ].u_bool ;
139+ self -> panel_io_config -> flags .disable_control_phase = (unsigned int )args [ARG_disable_control_phase ].u_bool ;
136140
137141 self -> panel_io_handle .del = & i2c_del ;
138142 self -> panel_io_handle .init = & i2c_init ;
139- self -> panel_io_handle .get_lane_count = & i2c_get_lane_count ;
140143 self -> panel_io_handle .tx_param = & i2c_tx_param ;
141144
142145 return MP_OBJ_FROM_PTR (self );
@@ -160,7 +163,7 @@ mp_lcd_err_t i2c_del(mp_obj_t obj)
160163}
161164
162165
163- mp_lcd_err_t i2c_init (mp_obj_t obj , uint16_t width , uint16_t height , uint8_t bpp , uint32_t buffer_size , bool rgb565_byte_swap , uint8_t cmd_bits , uint8_t param_bits , bool sw_rotation )
166+ mp_lcd_err_t i2c_init (mp_obj_t obj , uint16_t width , uint16_t height , uint8_t bpp , uint32_t buffer_size , bool rgb565_byte_swap , uint8_t cmd_bits , uint8_t param_bits , bool sw_rotate )
164167{
165168 mp_lcd_i2c_bus_obj_t * self = (mp_lcd_i2c_bus_obj_t * )obj ;
166169
@@ -170,55 +173,64 @@ mp_lcd_err_t i2c_init(mp_obj_t obj, uint16_t width, uint16_t height, uint8_t bpp
170173 self -> rgb565_byte_swap = false;
171174 }
172175
173- self -> panel_io_config . lcd_cmd_bits = (int )cmd_bits ;
174- self -> panel_io_config . lcd_param_bits = (int )param_bits ;
176+ self -> panel_io_config -> lcd_cmd_bits = (int )cmd_bits ;
177+ self -> panel_io_config -> lcd_param_bits = (int )param_bits ;
175178
176179 esp_err_t ret ;
177180
178- if (sw_rotation ) {
179- self -> rotation = (rotation_t * )malloc (sizeof (rotation_t ));
180- self -> rotation -> init_func = & i2c_rotation_init_func ;
181+ if (sw_rotate ) {
182+ self -> rotation = (rotation_t * )malloc (sizeof (rotation_t ));
183+ self -> rotation -> init_func = & i2c_rotation_init_func ;
181184
182- rotation_task_start (self );
183- ret = self -> rotation -> init_err . code ;
185+ ret = rotation_set_buffers (self );
186+ if ( ret != LCD_OK ) mp_raise_msg ( & mp_type_MemoryError , MP_ERROR_TEXT ( "unable to allocate sw rotate fb" )) ;
184187
185- if (ret != LCD_OK ) {
186- mp_raise_msg_varg (& mp_type_ValueError , self -> rotation -> init_err .msg , self -> rotation -> init_err .code );
187- }
188+ rotation_task_start (self );
189+ ret = self -> rotation -> init_err .code ;
188190
189- } else {
190- self -> panel_io_config .on_color_trans_done = & bus_trans_done_cb ;
191-
192- ret = i2c_param_config (self -> host , & self -> bus_config );
193- if (ret != 0 ) {
194- mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("%d(i2c_param_config)" ), ret );
195- }
196-
197- ret = i2c_driver_install (self -> host , I2C_MODE_MASTER , 0 , 0 , 0 );
198- if (ret != 0 ) {
199- mp_raise_msg_varg (& mp_type_OSError , MP_ERROR_TEXT ("%d(i2c_driver_install)" ), ret );
200- }
201-
202- ret = esp_lcd_new_panel_io_i2c (self -> bus_handle , & self -> panel_io_config , & self -> panel_io_handle .panel_io );
203- if (ret != 0 ) {
204- mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("%d(esp_lcd_new_panel_io_i2c)" ), ret );
205- }
191+ if (ret != LCD_OK ) {
192+ mp_raise_msg_varg (& mp_type_ValueError , self -> rotation -> init_err .msg , self -> rotation -> init_err .code );
193+ }
194+ } else {
195+ self -> panel_io_config -> on_color_trans_done = & bus_trans_done_cb ;
196+
197+ ret = i2c_param_config (self -> host , self -> bus_config );
198+ if (ret != 0 ) {
199+ mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("%d(i2c_param_config)" ), ret );
200+ }
201+
202+ ret = i2c_driver_install (self -> host , I2C_MODE_MASTER , 0 , 0 , 0 );
203+ if (ret != 0 ) {
204+ mp_raise_msg_varg (& mp_type_OSError , MP_ERROR_TEXT ("%d(i2c_driver_install)" ), ret );
206205 }
207206
207+ ret = esp_lcd_new_panel_io_i2c (self -> bus_handle , self -> panel_io_config , & self -> panel_io_handle .panel_io );
208+ if (ret != 0 ) {
209+ mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("%d(esp_lcd_new_panel_io_i2c)" ), ret );
210+ }
211+
212+ free (self -> bus_config );
213+ self -> bus_config = NULL ;
214+
215+ free (self -> panel_io_config );
216+ self -> panel_io_config = NULL ;
217+ }
218+
208219 return ret ;
209220}
210221
222+
211223mp_lcd_err_t i2c_tx_param (mp_obj_t obj , int lcd_cmd , void * param , size_t param_size , bool is_flush , bool last_flush_cmd )
212224{
213- mp_lcd_spi_bus_obj_t * self = (mp_lcd_spi_bus_obj_t * )obj ;
225+ mp_lcd_i2c_bus_obj_t * self = (mp_lcd_i2c_bus_obj_t * )obj ;
214226
215227 mp_lcd_err_t ret ;
216228
217229 if (self -> rotation == NULL || !is_flush ) {
218230 LCD_UNUSED (last_flush_cmd );
219231 ret = esp_lcd_panel_io_tx_param (self -> panel_io_handle .panel_io , lcd_cmd , param , param_size );
220232 } else {
221- bus_lock_acquire (& self -> rotation -> task .tx_param_lock );
233+ bus_lock_acquire (& self -> rotation -> task .tx_param_lock , -1 );
222234
223235 if (self -> rotation -> data .tx_param_count == 24 ) {
224236 bus_lock_release (& self -> rotation -> task .tx_param_lock );
@@ -240,14 +252,6 @@ mp_lcd_err_t i2c_tx_param(mp_obj_t obj, int lcd_cmd, void *param, size_t param_s
240252}
241253
242254
243- mp_lcd_err_t i2c_get_lane_count (mp_obj_t obj , uint8_t * lane_count )
244- {
245- LCD_UNUSED (obj );
246- * lane_count = 1 ;
247- return LCD_OK ;
248- }
249-
250-
251255MP_DEFINE_CONST_OBJ_TYPE (
252256 mp_lcd_i2c_bus_type ,
253257 MP_QSTR_I2CBus ,
0 commit comments