@@ -113,8 +113,11 @@ I2C_HandleTypeDef I2CHandle2 = {.Instance = NULL};
113113#if defined(MICROPY_HW_I2C3_SCL )
114114I2C_HandleTypeDef I2CHandle3 = {.Instance = NULL };
115115#endif
116+ #if defined(MICROPY_HW_I2C4_SCL )
117+ I2C_HandleTypeDef I2CHandle4 = {.Instance = NULL };
118+ #endif
116119
117- STATIC bool pyb_i2c_use_dma [3 ];
120+ STATIC bool pyb_i2c_use_dma [4 ];
118121
119122STATIC const pyb_i2c_obj_t pyb_i2c_obj [] = {
120123 #if defined(MICROPY_HW_I2C1_SCL )
@@ -132,6 +135,11 @@ STATIC const pyb_i2c_obj_t pyb_i2c_obj[] = {
132135 #else
133136 {{& pyb_i2c_type }, NULL , NULL , NULL , NULL },
134137 #endif
138+ #if defined(MICROPY_HW_I2C4_SCL )
139+ {{& pyb_i2c_type }, & I2CHandle4 , & dma_I2C_4_TX , & dma_I2C_4_RX , & pyb_i2c_use_dma [3 ]},
140+ #else
141+ {{& pyb_i2c_type }, NULL , NULL , NULL , NULL },
142+ #endif
135143};
136144
137145#if defined(MICROPY_HW_I2C_BAUDRATE_TIMING )
@@ -192,6 +200,10 @@ void i2c_init0(void) {
192200 memset (& I2CHandle3 , 0 , sizeof (I2C_HandleTypeDef ));
193201 I2CHandle3 .Instance = I2C3 ;
194202 #endif
203+ #if defined(MICROPY_HW_I2C4_SCL )
204+ memset (& I2CHandle4 , 0 , sizeof (I2C_HandleTypeDef ));
205+ I2CHandle3 .Instance = I2C4 ;
206+ #endif
195207}
196208
197209void i2c_init (I2C_HandleTypeDef * i2c ) {
@@ -221,6 +233,13 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
221233 sda_pin = & MICROPY_HW_I2C3_SDA ;
222234 __I2C3_CLK_ENABLE ();
223235 #endif
236+ #if defined(MICROPY_HW_I2C4_SCL )
237+ } else if (i2c == & I2CHandle4 ) {
238+ i2c_unit = 4 ;
239+ scl_pin = & MICROPY_HW_I2C4_SCL ;
240+ sda_pin = & MICROPY_HW_I2C4_SDA ;
241+ __I2C3_CLK_ENABLE ();
242+ #endif
224243 } else {
225244 // I2C does not exist for this board (shouldn't get here, should be checked by caller)
226245 return ;
@@ -262,6 +281,11 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
262281 HAL_NVIC_EnableIRQ (I2C3_EV_IRQn );
263282 HAL_NVIC_EnableIRQ (I2C3_ER_IRQn );
264283 #endif
284+ #if defined(MICROPY_HW_I2C4_SCL )
285+ } else if (i2c -> Instance == I2C4 ) {
286+ HAL_NVIC_EnableIRQ (I2C4_EV_IRQn );
287+ HAL_NVIC_EnableIRQ (I2C4_ER_IRQn );
288+ #endif
265289 }
266290}
267291
@@ -292,6 +316,14 @@ void i2c_deinit(I2C_HandleTypeDef *i2c) {
292316 HAL_NVIC_DisableIRQ (I2C3_EV_IRQn );
293317 HAL_NVIC_DisableIRQ (I2C3_ER_IRQn );
294318 #endif
319+ #if defined(MICROPY_HW_I2C4_SCL )
320+ } else if (i2c -> Instance == I2C4 ) {
321+ __HAL_RCC_I2C4_FORCE_RESET ();
322+ __HAL_RCC_I2C4_RELEASE_RESET ();
323+ __HAL_RCC_I2C4_CLK_DISABLE ();
324+ HAL_NVIC_DisableIRQ (I2C4_EV_IRQn );
325+ HAL_NVIC_DisableIRQ (I2C4_ER_IRQn );
326+ #endif
295327 }
296328}
297329
@@ -328,6 +360,11 @@ void i2c_ev_irq_handler(mp_uint_t i2c_id) {
328360 hi2c = & I2CHandle3 ;
329361 break ;
330362 #endif
363+ #if defined(MICROPY_HW_I2C4_SCL )
364+ case 4 :
365+ hi2c = & I2CHandle4 ;
366+ break ;
367+ #endif
331368 default :
332369 return ;
333370 }
@@ -375,6 +412,11 @@ void i2c_er_irq_handler(mp_uint_t i2c_id) {
375412 hi2c = & I2CHandle3 ;
376413 break ;
377414 #endif
415+ #if defined(MICROPY_HW_I2C4_SCL )
416+ case 4 :
417+ hi2c = & I2CHandle4 ;
418+ break ;
419+ #endif
378420 default :
379421 return ;
380422 }
@@ -448,6 +490,9 @@ STATIC void pyb_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
448490 #if defined(MICROPY_HW_I2C3_SCL )
449491 else if (self -> i2c -> Instance == I2C3 ) { i2c_num = 3 ; }
450492 #endif
493+ #if defined(MICROPY_HW_I2C4_SCL )
494+ else if (self -> i2c -> Instance == I2C4 ) { i2c_num = 4 ; }
495+ #endif
451496
452497 if (self -> i2c -> State == HAL_I2C_STATE_RESET ) {
453498 mp_printf (print , "I2C(%u)" , i2c_num );
0 commit comments