Skip to content

Commit d15a7c3

Browse files
committed
chore(U0): add I2C support
Signed-off-by: Frederic Pillon <[email protected]>
1 parent 3091896 commit d15a7c3

File tree

2 files changed

+63
-30
lines changed

2 files changed

+63
-30
lines changed

libraries/Wire/src/utility/twi.c

+44-25
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ static uint32_t i2c_getClkFreq(I2C_TypeDef *i2c)
300300
Error_Handler();
301301
}
302302
#else
303-
/* STM32 L0/G0 I2C2 has no independent clock */
303+
/* STM32 L0/G0/U0 I2C2 has no independent clock */
304304
clkSrcFreq = HAL_RCC_GetPCLK1Freq();
305305
#endif
306306
}
@@ -406,7 +406,8 @@ static uint32_t i2c_getClkFreq(I2C_TypeDef *i2c)
406406
Error_Handler();
407407
}
408408
#else
409-
Error_Handler();
409+
/* STM32 U0 I2C4 has no independent clock */
410+
clkSrcFreq = HAL_RCC_GetPCLK1Freq();
410411
#endif
411412
}
412413
}
@@ -675,9 +676,10 @@ void i2c_init(i2c_t *obj, uint32_t timing, uint32_t ownAddress)
675676
__HAL_RCC_I2C1_RELEASE_RESET();
676677

677678
obj->irq = I2C1_EV_IRQn;
678-
#if !defined(STM32C0xx) && !defined(STM32F0xx) && !defined(STM32G0xx) && !defined(STM32L0xx)
679+
#if !defined(STM32C0xx) && !defined(STM32F0xx) && !defined(STM32G0xx) && \
680+
!defined(STM32L0xx) && !defined(STM32U0xx)
679681
obj->irqER = I2C1_ER_IRQn;
680-
#endif /* !STM32C0xx && !STM32F0xx && !STM32G0xx && !STM32L0xx */
682+
#endif /* !STM32C0xx && !STM32F0xx && !STM32G0xx && !STM32L0xx && !STM32U0xx */
681683
i2c_handles[I2C1_INDEX] = handle;
682684
}
683685
#endif // I2C1_BASE
@@ -688,9 +690,10 @@ void i2c_init(i2c_t *obj, uint32_t timing, uint32_t ownAddress)
688690
__HAL_RCC_I2C2_FORCE_RESET();
689691
__HAL_RCC_I2C2_RELEASE_RESET();
690692
obj->irq = I2C2_EV_IRQn;
691-
#if !defined(STM32F0xx) && !defined(STM32G0xx) && !defined(STM32L0xx)
693+
#if !defined(STM32F0xx) && !defined(STM32G0xx) && !defined(STM32L0xx) && \
694+
!defined(STM32U0xx)
692695
obj->irqER = I2C2_ER_IRQn;
693-
#endif /* !STM32F0xx && !STM32G0xx && !STM32L0xx */
696+
#endif /* !STM32F0xx && !STM32G0xx && !STM32L0xx && !STM32U0xx */
694697
i2c_handles[I2C2_INDEX] = handle;
695698
}
696699
#endif // I2C2_BASE
@@ -701,9 +704,9 @@ void i2c_init(i2c_t *obj, uint32_t timing, uint32_t ownAddress)
701704
__HAL_RCC_I2C3_FORCE_RESET();
702705
__HAL_RCC_I2C3_RELEASE_RESET();
703706
obj->irq = I2C3_EV_IRQn;
704-
#if !defined(STM32G0xx) && !defined(STM32L0xx)
707+
#if !defined(STM32G0xx) && !defined(STM32L0xx) && !defined(STM32U0xx)
705708
obj->irqER = I2C3_ER_IRQn;
706-
#endif /* !STM32G0xx && !STM32L0xx */
709+
#endif /* !STM32G0xx && !STM32L0xx && !STM32U0xx*/
707710
i2c_handles[I2C3_INDEX] = handle;
708711
}
709712
#endif // I2C3_BASE
@@ -714,7 +717,9 @@ void i2c_init(i2c_t *obj, uint32_t timing, uint32_t ownAddress)
714717
__HAL_RCC_I2C4_FORCE_RESET();
715718
__HAL_RCC_I2C4_RELEASE_RESET();
716719
obj->irq = I2C4_EV_IRQn;
720+
#if !defined(STM32U0xx)
717721
obj->irqER = I2C4_ER_IRQn;
722+
#endif /* !STM32U0xx */
718723
i2c_handles[I2C4_INDEX] = handle;
719724
}
720725
#endif // I2C4_BASE
@@ -769,10 +774,11 @@ void i2c_init(i2c_t *obj, uint32_t timing, uint32_t ownAddress)
769774

770775
HAL_NVIC_SetPriority(obj->irq, I2C_IRQ_PRIO, I2C_IRQ_SUBPRIO);
771776
HAL_NVIC_EnableIRQ(obj->irq);
772-
#if !defined(STM32C0xx) && !defined(STM32F0xx) && !defined(STM32G0xx) && !defined(STM32L0xx)
777+
#if !defined(STM32C0xx) && !defined(STM32F0xx) && !defined(STM32G0xx) && \
778+
!defined(STM32L0xx) && !defined(STM32U0xx)
773779
HAL_NVIC_SetPriority(obj->irqER, I2C_IRQ_PRIO, I2C_IRQ_SUBPRIO);
774780
HAL_NVIC_EnableIRQ(obj->irqER);
775-
#endif /* !STM32C0xx && !STM32F0xx && !STM32G0xx && !STM32L0xx */
781+
#endif /* !STM32C0xx && !STM32F0xx && !STM32G0xx && !STM32L0xx && !STM32U0xx */
776782

777783
/* Init the I2C */
778784
if (HAL_I2C_Init(handle) != HAL_OK) {
@@ -796,9 +802,10 @@ void i2c_init(i2c_t *obj, uint32_t timing, uint32_t ownAddress)
796802
void i2c_deinit(i2c_t *obj)
797803
{
798804
HAL_NVIC_DisableIRQ(obj->irq);
799-
#if !defined(STM32C0xx) && !defined(STM32F0xx) && !defined(STM32G0xx) && !defined(STM32L0xx)
805+
#if !defined(STM32C0xx) && !defined(STM32F0xx) && !defined(STM32G0xx) && \
806+
!defined(STM32L0xx) && !defined(STM32U0xx)
800807
HAL_NVIC_DisableIRQ(obj->irqER);
801-
#endif /* !STM32C0xx && !STM32F0xx && !STM32G0xx && !STM32L0xx */
808+
#endif /* !STM32C0xx && !STM32F0xx && !STM32G0xx && !STM32L0xx && !STM32U0xx */
802809
HAL_I2C_DeInit(&(obj->handle));
803810
/* Reset I2C GPIO pins as INPUT_ANALOG */
804811
pin_function(obj->scl, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
@@ -1221,12 +1228,14 @@ void I2C1_EV_IRQHandler(void)
12211228
{
12221229
I2C_HandleTypeDef *handle = i2c_handles[I2C1_INDEX];
12231230
HAL_I2C_EV_IRQHandler(handle);
1224-
#if defined(STM32C0xx) || defined(STM32F0xx) || defined(STM32G0xx) || defined(STM32L0xx)
1231+
#if defined(STM32C0xx) || defined(STM32F0xx) || defined(STM32G0xx) || \
1232+
defined(STM32L0xx) || defined(STM32U0xx)
12251233
HAL_I2C_ER_IRQHandler(handle);
1226-
#endif /* STM32C0xx || STM32F0xx || STM32G0xx || STM32L0xx */
1234+
#endif /* STM32C0xx || STM32F0xx || STM32G0xx || STM32L0xx || STM32U0xx*/
12271235
}
12281236

1229-
#if !defined(STM32C0xx) && !defined(STM32F0xx) && !defined(STM32G0xx) && !defined(STM32L0xx)
1237+
#if !defined(STM32C0xx) && !defined(STM32F0xx) && !defined(STM32G0xx) && \
1238+
!defined(STM32L0xx) && !defined(STM32U0xx)
12301239
/**
12311240
* @brief This function handles I2C1 interrupt.
12321241
* @param None
@@ -1237,7 +1246,7 @@ void I2C1_ER_IRQHandler(void)
12371246
I2C_HandleTypeDef *handle = i2c_handles[I2C1_INDEX];
12381247
HAL_I2C_ER_IRQHandler(handle);
12391248
}
1240-
#endif /* !STM32C0xx && !STM32F0xx && !STM32G0xx && !STM32L0xx */
1249+
#endif /* !STM32C0xx && !STM32F0xx && !STM32G0xx && !STM32L0xx && !STM32U0xx */
12411250
#endif // I2C1_BASE
12421251

12431252
#if defined(I2C2_BASE)
@@ -1248,7 +1257,7 @@ void I2C1_ER_IRQHandler(void)
12481257
*/
12491258
void I2C2_EV_IRQHandler(void)
12501259
{
1251-
#if defined(I2C3_BASE) && defined(STM32G0xx)
1260+
#if defined(I2C3_BASE) && (defined(STM32G0xx) || defined(STM32U0xx))
12521261
/* I2C2_3_IRQHandler */
12531262
I2C_HandleTypeDef *handle2 = i2c_handles[I2C2_INDEX];
12541263
I2C_HandleTypeDef *handle3 = i2c_handles[I2C3_INDEX];
@@ -1260,16 +1269,26 @@ void I2C2_EV_IRQHandler(void)
12601269
HAL_I2C_EV_IRQHandler(handle3);
12611270
HAL_I2C_ER_IRQHandler(handle3);
12621271
}
1272+
#if defined(I2C4_BASE)
1273+
/* I2C2_3_4_IRQHandler */
1274+
I2C_HandleTypeDef *handle4 = i2c_handles[I2C4_INDEX];
1275+
if (handle4) {
1276+
HAL_I2C_EV_IRQHandler(handle4);
1277+
HAL_I2C_ER_IRQHandler(handle4);
1278+
}
1279+
#endif /* I2C4_BASE */
12631280
#else
12641281
I2C_HandleTypeDef *handle = i2c_handles[I2C2_INDEX];
12651282
HAL_I2C_EV_IRQHandler(handle);
1266-
#if defined(STM32F0xx) || defined(STM32G0xx) || defined(STM32L0xx)
1283+
#if defined(STM32F0xx) || defined(STM32G0xx) || defined(STM32L0xx) || \
1284+
defined(STM32U0xx)
12671285
HAL_I2C_ER_IRQHandler(handle);
1268-
#endif /* STM32F0xx || STM32G0xx || STM32L0xx */
1286+
#endif /* STM32F0xx || STM32G0xx || STM32L0xx || STM32U0xx*/
12691287
#endif
12701288
}
12711289

1272-
#if !defined(STM32F0xx) && !defined(STM32G0xx) && !defined(STM32L0xx)
1290+
#if !defined(STM32F0xx) && !defined(STM32G0xx) && !defined(STM32L0xx) && \
1291+
!defined(STM32U0xx)
12731292
/**
12741293
* @brief This function handles I2C2 interrupt.
12751294
* @param None
@@ -1280,10 +1299,10 @@ void I2C2_ER_IRQHandler(void)
12801299
I2C_HandleTypeDef *handle = i2c_handles[I2C2_INDEX];
12811300
HAL_I2C_ER_IRQHandler(handle);
12821301
}
1283-
#endif /* !STM32F0xx && !STM32G0xx && !STM32L0xx */
1302+
#endif /* !STM32F0xx && !STM32G0xx && !STM32L0xx && !STM32U0xx */
12841303
#endif // I2C2_BASE
12851304

1286-
#if defined(I2C3_BASE) && !defined(STM32G0xx)
1305+
#if defined(I2C3_BASE) && !defined(STM32G0xx) && !defined(STM32U0xx)
12871306
/**
12881307
* @brief This function handles I2C3 interrupt.
12891308
* @param None
@@ -1310,9 +1329,9 @@ void I2C3_ER_IRQHandler(void)
13101329
HAL_I2C_ER_IRQHandler(handle);
13111330
}
13121331
#endif /* !STM32L0xx */
1313-
#endif /* I2C3_BASE && ! STM32G0xx */
1332+
#endif /* I2C3_BASE && ! STM32G0xx && !STM32U0xx */
13141333

1315-
#if defined(I2C4_BASE)
1334+
#if defined(I2C4_BASE) && !defined(STM32U0xx)
13161335
/**
13171336
* @brief This function handles I2C4 interrupt.
13181337
* @param None
@@ -1335,7 +1354,7 @@ void I2C4_ER_IRQHandler(void)
13351354
I2C_HandleTypeDef *handle = i2c_handles[I2C4_INDEX];
13361355
HAL_I2C_ER_IRQHandler(handle);
13371356
}
1338-
#endif // I2C4_BASE
1357+
#endif // I2C4_BASE && !STM32U0xx
13391358

13401359
#if defined(I2C5_BASE)
13411360
/**

libraries/Wire/src/utility/twi.h

+19-5
Original file line numberDiff line numberDiff line change
@@ -67,35 +67,49 @@ extern "C" {
6767
#error I2C buffer size cannot exceed 255
6868
#endif
6969

70-
/* Redefinition of IRQ for C0/F0/G0/L0 families */
71-
#if defined(STM32C0xx) || defined(STM32F0xx) || defined(STM32G0xx) || defined(STM32L0xx)
70+
/* Redefinition of IRQ for C0/F0/G0/L0/U0 families */
71+
#if defined(STM32C0xx) || defined(STM32F0xx) || defined(STM32G0xx) ||\
72+
defined(STM32L0xx) || defined(STM32U0xx)
7273
#if defined(I2C1_BASE)
7374
#define I2C1_EV_IRQn I2C1_IRQn
7475
#define I2C1_EV_IRQHandler I2C1_IRQHandler
7576
#endif // defined(I2C1_BASE)
7677
#if defined(I2C2_BASE)
77-
#if defined(STM32G0xx) && defined(I2C3_BASE)
78+
#if (defined(STM32G0xx) || defined(STM32U0xx)) && defined(I2C3_BASE)
79+
#if defined(I2C4_BASE)
80+
#define I2C2_EV_IRQn I2C2_3_4_IRQn
81+
#define I2C2_EV_IRQHandler I2C2_3_4_IRQHandler
82+
#else
7883
#define I2C2_EV_IRQn I2C2_3_IRQn
7984
#define I2C2_EV_IRQHandler I2C2_3_IRQHandler
85+
#endif // defined(I2C4_BASE)
8086
#else
8187
#define I2C2_EV_IRQn I2C2_IRQn
8288
#define I2C2_EV_IRQHandler I2C2_IRQHandler
8389
#endif
8490
#endif // defined(I2C2_BASE)
8591
#if defined(I2C3_BASE)
86-
#if defined(STM32G0xx)
92+
#if defined(STM32G0xx) || defined(STM32U0xx)
93+
#if defined(I2C4_BASE)
94+
#define I2C3_EV_IRQn I2C2_3_4_IRQn
95+
#else
8796
#define I2C3_EV_IRQn I2C2_3_IRQn
97+
#endif
8898
#else
8999
#define I2C3_EV_IRQn I2C3_IRQn
90100
#define I2C3_EV_IRQHandler I2C3_IRQHandler
91101
#endif
92102
#endif // defined(I2C3_BASE)
93103
/* Defined but no one has it */
94104
#if defined(I2C4_BASE)
105+
#if defined(STM32U0xx)
106+
#define I2C4_EV_IRQn I2C2_3_4_IRQn
107+
#else
95108
#define I2C4_EV_IRQn I2C4_IRQn
96109
#define I2C4_EV_IRQHandler I2C4_IRQHandler
110+
#endif
97111
#endif // defined(I2C4_BASE)
98-
#endif /* STM32C0xx || STM32F0xx || STM32G0xx || STM32L0xx */
112+
#endif /* STM32C0xx || STM32F0xx || STM32G0xx || STM32L0xx || STM32U0xx */
99113

100114
typedef struct i2c_s i2c_t;
101115

0 commit comments

Comments
 (0)