Skip to content

Commit 494cd67

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

File tree

2 files changed

+60
-28
lines changed

2 files changed

+60
-28
lines changed

libraries/Wire/src/utility/twi.c

+41-23
Original file line numberDiff line numberDiff line change
@@ -675,9 +675,10 @@ void i2c_init(i2c_t *obj, uint32_t timing, uint32_t ownAddress)
675675
__HAL_RCC_I2C1_RELEASE_RESET();
676676

677677
obj->irq = I2C1_EV_IRQn;
678-
#if !defined(STM32C0xx) && !defined(STM32F0xx) && !defined(STM32G0xx) && !defined(STM32L0xx)
678+
#if !defined(STM32C0xx) && !defined(STM32F0xx) && !defined(STM32G0xx) && \
679+
!defined(STM32L0xx) && !defined(STM32U0xx)
679680
obj->irqER = I2C1_ER_IRQn;
680-
#endif /* !STM32C0xx && !STM32F0xx && !STM32G0xx && !STM32L0xx */
681+
#endif /* !STM32C0xx && !STM32F0xx && !STM32G0xx && !STM32L0xx && !STM32U0xx */
681682
i2c_handles[I2C1_INDEX] = handle;
682683
}
683684
#endif // I2C1_BASE
@@ -688,9 +689,10 @@ void i2c_init(i2c_t *obj, uint32_t timing, uint32_t ownAddress)
688689
__HAL_RCC_I2C2_FORCE_RESET();
689690
__HAL_RCC_I2C2_RELEASE_RESET();
690691
obj->irq = I2C2_EV_IRQn;
691-
#if !defined(STM32F0xx) && !defined(STM32G0xx) && !defined(STM32L0xx)
692+
#if !defined(STM32F0xx) && !defined(STM32G0xx) && !defined(STM32L0xx) && \
693+
!defined(STM32U0xx)
692694
obj->irqER = I2C2_ER_IRQn;
693-
#endif /* !STM32F0xx && !STM32G0xx && !STM32L0xx */
695+
#endif /* !STM32F0xx && !STM32G0xx && !STM32L0xx && !STM32U0xx */
694696
i2c_handles[I2C2_INDEX] = handle;
695697
}
696698
#endif // I2C2_BASE
@@ -701,9 +703,9 @@ void i2c_init(i2c_t *obj, uint32_t timing, uint32_t ownAddress)
701703
__HAL_RCC_I2C3_FORCE_RESET();
702704
__HAL_RCC_I2C3_RELEASE_RESET();
703705
obj->irq = I2C3_EV_IRQn;
704-
#if !defined(STM32G0xx) && !defined(STM32L0xx)
706+
#if !defined(STM32G0xx) && !defined(STM32L0xx) && !defined(STM32U0xx)
705707
obj->irqER = I2C3_ER_IRQn;
706-
#endif /* !STM32G0xx && !STM32L0xx */
708+
#endif /* !STM32G0xx && !STM32L0xx && !STM32U0xx*/
707709
i2c_handles[I2C3_INDEX] = handle;
708710
}
709711
#endif // I2C3_BASE
@@ -714,7 +716,9 @@ void i2c_init(i2c_t *obj, uint32_t timing, uint32_t ownAddress)
714716
__HAL_RCC_I2C4_FORCE_RESET();
715717
__HAL_RCC_I2C4_RELEASE_RESET();
716718
obj->irq = I2C4_EV_IRQn;
719+
#if !defined(STM32U0xx)
717720
obj->irqER = I2C4_ER_IRQn;
721+
#endif /* !STM32U0xx */
718722
i2c_handles[I2C4_INDEX] = handle;
719723
}
720724
#endif // I2C4_BASE
@@ -769,10 +773,11 @@ void i2c_init(i2c_t *obj, uint32_t timing, uint32_t ownAddress)
769773

770774
HAL_NVIC_SetPriority(obj->irq, I2C_IRQ_PRIO, I2C_IRQ_SUBPRIO);
771775
HAL_NVIC_EnableIRQ(obj->irq);
772-
#if !defined(STM32C0xx) && !defined(STM32F0xx) && !defined(STM32G0xx) && !defined(STM32L0xx)
776+
#if !defined(STM32C0xx) && !defined(STM32F0xx) && !defined(STM32G0xx) && \
777+
!defined(STM32L0xx) && !defined(STM32U0xx)
773778
HAL_NVIC_SetPriority(obj->irqER, I2C_IRQ_PRIO, I2C_IRQ_SUBPRIO);
774779
HAL_NVIC_EnableIRQ(obj->irqER);
775-
#endif /* !STM32C0xx && !STM32F0xx && !STM32G0xx && !STM32L0xx */
780+
#endif /* !STM32C0xx && !STM32F0xx && !STM32G0xx && !STM32L0xx && !STM32U0xx */
776781

777782
/* Init the I2C */
778783
if (HAL_I2C_Init(handle) != HAL_OK) {
@@ -796,9 +801,10 @@ void i2c_init(i2c_t *obj, uint32_t timing, uint32_t ownAddress)
796801
void i2c_deinit(i2c_t *obj)
797802
{
798803
HAL_NVIC_DisableIRQ(obj->irq);
799-
#if !defined(STM32C0xx) && !defined(STM32F0xx) && !defined(STM32G0xx) && !defined(STM32L0xx)
804+
#if !defined(STM32C0xx) && !defined(STM32F0xx) && !defined(STM32G0xx) && \
805+
!defined(STM32L0xx) && !defined(STM32U0xx)
800806
HAL_NVIC_DisableIRQ(obj->irqER);
801-
#endif /* !STM32C0xx && !STM32F0xx && !STM32G0xx && !STM32L0xx */
807+
#endif /* !STM32C0xx && !STM32F0xx && !STM32G0xx && !STM32L0xx && !STM32U0xx */
802808
HAL_I2C_DeInit(&(obj->handle));
803809
/* Reset I2C GPIO pins as INPUT_ANALOG */
804810
pin_function(obj->scl, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
@@ -1221,12 +1227,14 @@ void I2C1_EV_IRQHandler(void)
12211227
{
12221228
I2C_HandleTypeDef *handle = i2c_handles[I2C1_INDEX];
12231229
HAL_I2C_EV_IRQHandler(handle);
1224-
#if defined(STM32C0xx) || defined(STM32F0xx) || defined(STM32G0xx) || defined(STM32L0xx)
1230+
#if defined(STM32C0xx) || defined(STM32F0xx) || defined(STM32G0xx) || \
1231+
defined(STM32L0xx) || defined(STM32U0xx)
12251232
HAL_I2C_ER_IRQHandler(handle);
1226-
#endif /* STM32C0xx || STM32F0xx || STM32G0xx || STM32L0xx */
1233+
#endif /* STM32C0xx || STM32F0xx || STM32G0xx || STM32L0xx || STM32U0xx*/
12271234
}
12281235

1229-
#if !defined(STM32C0xx) && !defined(STM32F0xx) && !defined(STM32G0xx) && !defined(STM32L0xx)
1236+
#if !defined(STM32C0xx) && !defined(STM32F0xx) && !defined(STM32G0xx) && \
1237+
!defined(STM32L0xx) && !defined(STM32U0xx)
12301238
/**
12311239
* @brief This function handles I2C1 interrupt.
12321240
* @param None
@@ -1237,7 +1245,7 @@ void I2C1_ER_IRQHandler(void)
12371245
I2C_HandleTypeDef *handle = i2c_handles[I2C1_INDEX];
12381246
HAL_I2C_ER_IRQHandler(handle);
12391247
}
1240-
#endif /* !STM32C0xx && !STM32F0xx && !STM32G0xx && !STM32L0xx */
1248+
#endif /* !STM32C0xx && !STM32F0xx && !STM32G0xx && !STM32L0xx && !STM32U0xx */
12411249
#endif // I2C1_BASE
12421250

12431251
#if defined(I2C2_BASE)
@@ -1248,7 +1256,7 @@ void I2C1_ER_IRQHandler(void)
12481256
*/
12491257
void I2C2_EV_IRQHandler(void)
12501258
{
1251-
#if defined(I2C3_BASE) && defined(STM32G0xx)
1259+
#if defined(I2C3_BASE) && (defined(STM32G0xx) || defined(STM32U0xx))
12521260
/* I2C2_3_IRQHandler */
12531261
I2C_HandleTypeDef *handle2 = i2c_handles[I2C2_INDEX];
12541262
I2C_HandleTypeDef *handle3 = i2c_handles[I2C3_INDEX];
@@ -1260,16 +1268,26 @@ void I2C2_EV_IRQHandler(void)
12601268
HAL_I2C_EV_IRQHandler(handle3);
12611269
HAL_I2C_ER_IRQHandler(handle3);
12621270
}
1271+
#if defined(I2C4_BASE)
1272+
/* I2C2_3_4_IRQHandler */
1273+
I2C_HandleTypeDef *handle4 = i2c_handles[I2C4_INDEX];
1274+
if (handle4) {
1275+
HAL_I2C_EV_IRQHandler(handle4);
1276+
HAL_I2C_ER_IRQHandler(handle4);
1277+
}
1278+
#endif /* I2C4_BASE */
12631279
#else
12641280
I2C_HandleTypeDef *handle = i2c_handles[I2C2_INDEX];
12651281
HAL_I2C_EV_IRQHandler(handle);
1266-
#if defined(STM32F0xx) || defined(STM32G0xx) || defined(STM32L0xx)
1282+
#if defined(STM32F0xx) || defined(STM32G0xx) || defined(STM32L0xx) || \
1283+
defined(STM32U0xx)
12671284
HAL_I2C_ER_IRQHandler(handle);
1268-
#endif /* STM32F0xx || STM32G0xx || STM32L0xx */
1285+
#endif /* STM32F0xx || STM32G0xx || STM32L0xx || STM32U0xx*/
12691286
#endif
12701287
}
12711288

1272-
#if !defined(STM32F0xx) && !defined(STM32G0xx) && !defined(STM32L0xx)
1289+
#if !defined(STM32F0xx) && !defined(STM32G0xx) && !defined(STM32L0xx) && \
1290+
!defined(STM32U0xx)
12731291
/**
12741292
* @brief This function handles I2C2 interrupt.
12751293
* @param None
@@ -1280,10 +1298,10 @@ void I2C2_ER_IRQHandler(void)
12801298
I2C_HandleTypeDef *handle = i2c_handles[I2C2_INDEX];
12811299
HAL_I2C_ER_IRQHandler(handle);
12821300
}
1283-
#endif /* !STM32F0xx && !STM32G0xx && !STM32L0xx */
1301+
#endif /* !STM32F0xx && !STM32G0xx && !STM32L0xx && !STM32U0xx */
12841302
#endif // I2C2_BASE
12851303

1286-
#if defined(I2C3_BASE) && !defined(STM32G0xx)
1304+
#if defined(I2C3_BASE) && !defined(STM32G0xx) && !defined(STM32U0xx)
12871305
/**
12881306
* @brief This function handles I2C3 interrupt.
12891307
* @param None
@@ -1310,9 +1328,9 @@ void I2C3_ER_IRQHandler(void)
13101328
HAL_I2C_ER_IRQHandler(handle);
13111329
}
13121330
#endif /* !STM32L0xx */
1313-
#endif /* I2C3_BASE && ! STM32G0xx */
1331+
#endif /* I2C3_BASE && ! STM32G0xx && !STM32U0xx */
13141332

1315-
#if defined(I2C4_BASE)
1333+
#if defined(I2C4_BASE) && !defined(STM32U0xx)
13161334
/**
13171335
* @brief This function handles I2C4 interrupt.
13181336
* @param None
@@ -1335,7 +1353,7 @@ void I2C4_ER_IRQHandler(void)
13351353
I2C_HandleTypeDef *handle = i2c_handles[I2C4_INDEX];
13361354
HAL_I2C_ER_IRQHandler(handle);
13371355
}
1338-
#endif // I2C4_BASE
1356+
#endif // I2C4_BASE && !STM32U0xx
13391357

13401358
#if defined(I2C5_BASE)
13411359
/**

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)