4141#include "PeripheralPins.h"
4242#include "i2c_device.h" // family specific defines
4343#include "mbed_error.h"
44+ #include "platform/mbed_power_mgmt.h"
4445
4546#ifndef DEBUG_STDIO
4647# define DEBUG_STDIO 0
@@ -294,6 +295,11 @@ void i2c_init_internal(i2c_t *obj, const i2c_pinmap_t *pinmap)
294295 // Configure I2C pins
295296 obj_s -> event_i2cIRQ = I2C1_EV_IRQn ;
296297 obj_s -> error_i2cIRQ = I2C1_ER_IRQn ;
298+
299+ #if defined(TARGET_STM32WL )
300+ /* In Stop2 mode, I2C1 and I2C2 instances are powered down (only I2C3 register content is kept) */
301+ sleep_manager_lock_deep_sleep ();
302+ #endif
297303 }
298304#endif
299305#if defined I2C2_BASE
@@ -303,6 +309,11 @@ void i2c_init_internal(i2c_t *obj, const i2c_pinmap_t *pinmap)
303309 __HAL_RCC_I2C2_CLK_ENABLE ();
304310 obj_s -> event_i2cIRQ = I2C2_EV_IRQn ;
305311 obj_s -> error_i2cIRQ = I2C2_ER_IRQn ;
312+
313+ #if defined(TARGET_STM32WL )
314+ /* In Stop2 mode, I2C1 and I2C2 instances are powered down (only I2C3 register content is kept) */
315+ sleep_manager_lock_deep_sleep ();
316+ #endif
306317 }
307318#endif
308319#if defined I2C3_BASE
@@ -380,11 +391,17 @@ void i2c_deinit_internal(i2c_t *obj)
380391#if defined I2C1_BASE
381392 if (obj_s -> i2c == I2C_1 ) {
382393 __HAL_RCC_I2C1_CLK_DISABLE ();
394+ #if defined(TARGET_STM32WL )
395+ sleep_manager_unlock_deep_sleep ();
396+ #endif
383397 }
384398#endif
385399#if defined I2C2_BASE
386400 if (obj_s -> i2c == I2C_2 ) {
387401 __HAL_RCC_I2C2_CLK_DISABLE ();
402+ #if defined(TARGET_STM32WL )
403+ sleep_manager_unlock_deep_sleep ();
404+ #endif
388405 }
389406#endif
390407#if defined I2C3_BASE
@@ -758,7 +775,7 @@ int i2c_byte_read(i2c_t *obj, int last)
758775 if ((tmpreg & I2C_CR2_RELOAD ) != 0 ) {
759776 while (!__HAL_I2C_GET_FLAG (handle , I2C_FLAG_TCR )) {
760777 if ((timeout -- ) == 0 ) {
761- DEBUG_PRINTF ("timeout in byte_read \r\n" );
778+ DEBUG_PRINTF ("timeout in i2c_byte_read \r\n" );
762779 return -1 ;
763780 }
764781 }
@@ -827,7 +844,7 @@ int i2c_byte_write(i2c_t *obj, int data)
827844 if ((tmpreg & I2C_CR2_RELOAD ) != 0 ) {
828845 while (!__HAL_I2C_GET_FLAG (handle , I2C_FLAG_TCR )) {
829846 if ((timeout -- ) == 0 ) {
830- DEBUG_PRINTF ("timeout in byte_write \r\n" );
847+ DEBUG_PRINTF ("timeout in i2c_byte_write \r\n" );
831848 return 2 ;
832849 }
833850 }
@@ -912,7 +929,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
912929 i2c_ev_err_disable (obj );
913930
914931 if ((timeout == 0 ) || (obj_s -> event != I2C_EVENT_TRANSFER_COMPLETE )) {
915- DEBUG_PRINTF (" TIMEOUT or error in i2c_read\r\n" );
932+ DEBUG_PRINTF ("TIMEOUT or error in i2c_read\r\n" );
916933 /* re-init IP to try and get back in a working state */
917934 i2c_init_internal (obj , NULL );
918935 } else {
@@ -986,7 +1003,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
9861003 count = length ;
9871004 }
9881005 } else {
989- DEBUG_PRINTF ("ERROR in i2c_read \r\n" );
1006+ DEBUG_PRINTF ("ERROR in i2c_write \r\n" );
9901007 }
9911008
9921009 return count ;
0 commit comments