Skip to content

Commit 54213f4

Browse files
committed
I2C: G0: add I2C3 support
Signed-off-by: Frederic Pillon <[email protected]>
1 parent a7099fe commit 54213f4

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

libraries/Wire/src/utility/twi.c

+22-3
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ static uint32_t i2c_getClkFreq(I2C_TypeDef *i2c)
274274
#endif // I2C2_BASE
275275
#if defined I2C3_BASE
276276
if (i2c == I2C3) {
277+
#if defined(__HAL_RCC_GET_I2C3_SOURCE)
277278
switch (__HAL_RCC_GET_I2C3_SOURCE()) {
278279
case RCC_I2C3CLKSOURCE_HSI:
279280
clkSrcFreq = HSI_VALUE;
@@ -307,6 +308,10 @@ static uint32_t i2c_getClkFreq(I2C_TypeDef *i2c)
307308
default:
308309
Error_Handler();
309310
}
311+
#else
312+
/* STM32 G0 I2C3 has no independent clock */
313+
clkSrcFreq = HAL_RCC_GetPCLK1Freq();
314+
#endif
310315
}
311316
#endif // I2C3_BASE
312317
#if defined I2C4_BASE
@@ -648,7 +653,7 @@ void i2c_custom_init(i2c_t *obj, uint32_t timing, uint32_t addressingMode, uint3
648653
__HAL_RCC_I2C3_FORCE_RESET();
649654
__HAL_RCC_I2C3_RELEASE_RESET();
650655
obj->irq = I2C3_EV_IRQn;
651-
#if !defined(STM32L0xx)
656+
#if !defined(STM32G0xx) && !defined(STM32L0xx)
652657
obj->irqER = I2C3_ER_IRQn;
653658
#endif /* !STM32L0xx */
654659
i2c_handles[I2C3_INDEX] = handle;
@@ -1138,11 +1143,25 @@ void I2C1_ER_IRQHandler(void)
11381143
*/
11391144
void I2C2_EV_IRQHandler(void)
11401145
{
1146+
#if defined(I2C3_BASE) && defined(STM32G0xx)
1147+
/* I2C2_3_IRQHandler */
1148+
I2C_HandleTypeDef *handle2 = i2c_handles[I2C2_INDEX];
1149+
I2C_HandleTypeDef *handle3 = i2c_handles[I2C3_INDEX];
1150+
if (handle2) {
1151+
HAL_I2C_EV_IRQHandler(handle2);
1152+
HAL_I2C_ER_IRQHandler(handle2);
1153+
}
1154+
if (handle3) {
1155+
HAL_I2C_EV_IRQHandler(handle3);
1156+
HAL_I2C_ER_IRQHandler(handle3);
1157+
}
1158+
#else
11411159
I2C_HandleTypeDef *handle = i2c_handles[I2C2_INDEX];
11421160
HAL_I2C_EV_IRQHandler(handle);
11431161
#if defined(STM32F0xx) || defined(STM32G0xx) || defined(STM32L0xx)
11441162
HAL_I2C_ER_IRQHandler(handle);
11451163
#endif /* STM32F0xx || STM32G0xx || STM32L0xx */
1164+
#endif
11461165
}
11471166

11481167
#if !defined(STM32F0xx) && !defined(STM32G0xx) && !defined(STM32L0xx)
@@ -1159,7 +1178,7 @@ void I2C2_ER_IRQHandler(void)
11591178
#endif /* !STM32F0xx && !STM32G0xx && !STM32L0xx */
11601179
#endif // I2C2_BASE
11611180

1162-
#if defined(I2C3_BASE)
1181+
#if defined(I2C3_BASE) && !defined(STM32G0xx)
11631182
/**
11641183
* @brief This function handles I2C3 interrupt.
11651184
* @param None
@@ -1186,7 +1205,7 @@ void I2C3_ER_IRQHandler(void)
11861205
HAL_I2C_ER_IRQHandler(handle);
11871206
}
11881207
#endif /* !STM32L0xx */
1189-
#endif // I2C3_BASE
1208+
#endif /* I2C3_BASE && ! STM32G0xx */
11901209

11911210
#if defined(I2C4_BASE)
11921211
/**

libraries/Wire/src/utility/twi.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,21 @@ extern "C" {
7474
#define I2C1_EV_IRQHandler I2C1_IRQHandler
7575
#endif // defined(I2C1_BASE)
7676
#if defined(I2C2_BASE)
77+
#if defined(STM32G0xx) && defined(I2C3_BASE)
78+
#define I2C2_EV_IRQn I2C2_3_IRQn
79+
#define I2C2_EV_IRQHandler I2C2_3_IRQHandler
80+
#else
7781
#define I2C2_EV_IRQn I2C2_IRQn
7882
#define I2C2_EV_IRQHandler I2C2_IRQHandler
83+
#endif
7984
#endif // defined(I2C2_BASE)
80-
/* Only for STM32L0xx */
8185
#if defined(I2C3_BASE)
86+
#if defined(STM32G0xx)
87+
#define I2C3_EV_IRQn I2C2_3_IRQn
88+
#else
8289
#define I2C3_EV_IRQn I2C3_IRQn
8390
#define I2C3_EV_IRQHandler I2C3_IRQHandler
91+
#endif
8492
#endif // defined(I2C3_BASE)
8593
/* Defined but no one has it */
8694
#if defined(I2C4_BASE)

0 commit comments

Comments
 (0)