41
41
#include "PeripheralPins.h"
42
42
#include "i2c_device.h" // family specific defines
43
43
#include "mbed_error.h"
44
+ #include "platform/mbed_power_mgmt.h"
44
45
45
46
#ifndef DEBUG_STDIO
46
47
# define DEBUG_STDIO 0
@@ -294,6 +295,11 @@ void i2c_init_internal(i2c_t *obj, const i2c_pinmap_t *pinmap)
294
295
// Configure I2C pins
295
296
obj_s -> event_i2cIRQ = I2C1_EV_IRQn ;
296
297
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
297
303
}
298
304
#endif
299
305
#if defined I2C2_BASE
@@ -303,6 +309,11 @@ void i2c_init_internal(i2c_t *obj, const i2c_pinmap_t *pinmap)
303
309
__HAL_RCC_I2C2_CLK_ENABLE ();
304
310
obj_s -> event_i2cIRQ = I2C2_EV_IRQn ;
305
311
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
306
317
}
307
318
#endif
308
319
#if defined I2C3_BASE
@@ -380,11 +391,17 @@ void i2c_deinit_internal(i2c_t *obj)
380
391
#if defined I2C1_BASE
381
392
if (obj_s -> i2c == I2C_1 ) {
382
393
__HAL_RCC_I2C1_CLK_DISABLE ();
394
+ #if defined(TARGET_STM32WL )
395
+ sleep_manager_unlock_deep_sleep ();
396
+ #endif
383
397
}
384
398
#endif
385
399
#if defined I2C2_BASE
386
400
if (obj_s -> i2c == I2C_2 ) {
387
401
__HAL_RCC_I2C2_CLK_DISABLE ();
402
+ #if defined(TARGET_STM32WL )
403
+ sleep_manager_unlock_deep_sleep ();
404
+ #endif
388
405
}
389
406
#endif
390
407
#if defined I2C3_BASE
@@ -758,7 +775,7 @@ int i2c_byte_read(i2c_t *obj, int last)
758
775
if ((tmpreg & I2C_CR2_RELOAD ) != 0 ) {
759
776
while (!__HAL_I2C_GET_FLAG (handle , I2C_FLAG_TCR )) {
760
777
if ((timeout -- ) == 0 ) {
761
- DEBUG_PRINTF ("timeout in byte_read \r\n" );
778
+ DEBUG_PRINTF ("timeout in i2c_byte_read \r\n" );
762
779
return -1 ;
763
780
}
764
781
}
@@ -827,7 +844,7 @@ int i2c_byte_write(i2c_t *obj, int data)
827
844
if ((tmpreg & I2C_CR2_RELOAD ) != 0 ) {
828
845
while (!__HAL_I2C_GET_FLAG (handle , I2C_FLAG_TCR )) {
829
846
if ((timeout -- ) == 0 ) {
830
- DEBUG_PRINTF ("timeout in byte_write \r\n" );
847
+ DEBUG_PRINTF ("timeout in i2c_byte_write \r\n" );
831
848
return 2 ;
832
849
}
833
850
}
@@ -912,7 +929,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
912
929
i2c_ev_err_disable (obj );
913
930
914
931
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" );
916
933
/* re-init IP to try and get back in a working state */
917
934
i2c_init_internal (obj , NULL );
918
935
} else {
@@ -986,7 +1003,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
986
1003
count = length ;
987
1004
}
988
1005
} else {
989
- DEBUG_PRINTF ("ERROR in i2c_read \r\n" );
1006
+ DEBUG_PRINTF ("ERROR in i2c_write \r\n" );
990
1007
}
991
1008
992
1009
return count ;
0 commit comments