@@ -369,6 +369,48 @@ void i2c_init_internal(i2c_t *obj, const i2c_pinmap_t *pinmap)
369369#endif
370370}
371371
372+ void i2c_deinit_internal (i2c_t * obj )
373+ {
374+ struct i2c_s * obj_s = I2C_S (obj );
375+
376+ i2c_hw_reset (obj );
377+
378+ HAL_I2C_DeInit (& (obj_s -> handle ));
379+
380+ #if defined I2C1_BASE
381+ if (obj_s -> i2c == I2C_1 ) {
382+ __HAL_RCC_I2C1_CLK_DISABLE ();
383+ }
384+ #endif
385+ #if defined I2C2_BASE
386+ if (obj_s -> i2c == I2C_2 ) {
387+ __HAL_RCC_I2C2_CLK_DISABLE ();
388+ }
389+ #endif
390+ #if defined I2C3_BASE
391+ if (obj_s -> i2c == I2C_3 ) {
392+ __HAL_RCC_I2C3_CLK_DISABLE ();
393+ }
394+ #endif
395+ #if defined I2C4_BASE
396+ if (obj_s -> i2c == I2C_4 ) {
397+ __HAL_RCC_I2C4_CLK_DISABLE ();
398+ }
399+ #endif
400+ #if defined FMPI2C1_BASE
401+ if (obj_s -> i2c == FMPI2C_1 ) {
402+ __HAL_RCC_FMPI2C1_CLK_DISABLE ();
403+ }
404+ #endif
405+
406+ pin_mode (obj_s -> sda , PullNone );
407+ pin_mode (obj_s -> scl , PullNone );
408+
409+ obj_s -> sda = NC ;
410+ obj_s -> scl = NC ;
411+ obj_s -> i2c = (I2CName )NC ;
412+ }
413+
372414#if STATIC_PINMAP_READY
373415#define I2C_INIT_DIRECT i2c_init_direct
374416void i2c_init_direct (i2c_t * obj , const i2c_pinmap_t * pinmap )
@@ -396,6 +438,11 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
396438 I2C_INIT_DIRECT (obj , & explicit_i2c_pinmap );
397439}
398440
441+ void i2c_free (i2c_t * obj )
442+ {
443+ i2c_deinit_internal (obj );
444+ }
445+
399446
400447void i2c_frequency (i2c_t * obj , int hz )
401448{
0 commit comments