Skip to content

Commit 4550f92

Browse files
committed
STM32WL : I2C full support
1 parent 96e6d8b commit 4550f92

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

targets/TARGET_STM/TARGET_STM32WL/i2c_device.h

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,20 @@ extern "C" {
3737
static inline uint32_t get_i2c_timing(int hz)
3838
{
3939
uint32_t tim = 0;
40-
if (SystemCoreClock == 64000000) {
41-
// Common settings: I2C clock = 64 MHz, Analog filter = ON, Digital filter coefficient = 0
42-
switch (hz) {
43-
case 100000:
44-
tim = 0x10707DBC; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
45-
break;
46-
case 400000:
47-
tim = 0x00602173; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
48-
break;
49-
case 1000000:
50-
tim = 0x00300B29; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
51-
break;
52-
default:
53-
break;
54-
}
55-
} else if (SystemCoreClock == 32000000) {
56-
// Common settings: I2C clock = 32 MHz, Analog filter = ON, Digital filter coefficient = 0
57-
switch (hz) {
58-
case 100000:
59-
tim = 0x00707CBB; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
60-
break;
61-
case 400000:
62-
tim = 0x00300F38; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
63-
break;
64-
case 1000000:
65-
tim = 0x00100413; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
66-
break;
67-
default:
68-
break;
69-
}
40+
41+
// Common settings: I2C clock = 48 MHz, Analog filter = ON, Digital filter coefficient = 0
42+
switch (hz) {
43+
case 100000:
44+
tim = 0x20E03F53; // Standard mode with Rise Time = 640ns and Fall Time = 20ns
45+
break;
46+
case 400000:
47+
tim = 0x20500817; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
48+
break;
49+
case 1000000:
50+
tim = 0x00500A18; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
51+
break;
52+
default:
53+
break;
7054
}
7155
return tim;
7256
}

targets/TARGET_STM/i2c_api.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
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

Comments
 (0)