Skip to content

Commit 0e97762

Browse files
committed
Dynamically handle I2C index
Harden the code to avoid wrong index. Signed-off-by: Frederic.Pillon <[email protected]>
1 parent f00ba5f commit 0e97762

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

Diff for: cores/arduino/stm32/twi.c

+26-20
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,21 @@ extern "C" {
5353
#define SLAVE_MODE_LISTEN 2
5454

5555
/* Family specific description for I2C */
56-
#if defined(STM32F7xx) || defined(STM32H7xx) || defined(STM32L4xx)
57-
#define I2C_NUM (4)
58-
#elif defined(STM32F2xx) || defined(STM32F3xx) || defined(STM32F4xx) || defined(STM32L0xx)
59-
#define I2C_NUM (3)
60-
#elif defined(STM32F0xx) || defined(STM32F1xx) || defined(STM32L1xx)
61-
#define I2C_NUM (2)
62-
#else
63-
#error "Unknown Family - unknown I2C_NUM"
56+
typedef enum {
57+
#if defined(I2C1_BASE)
58+
I2C1_INDEX,
59+
#endif
60+
#if defined(I2C2_BASE)
61+
I2C2_INDEX,
62+
#endif
63+
#if defined(I2C3_BASE)
64+
I2C3_INDEX,
65+
#endif
66+
#if defined(I2C4_BASE)
67+
I2C4_INDEX,
6468
#endif
69+
I2C_NUM
70+
} i2c_index_t;
6571

6672
/* Private Variables */
6773
static I2C_HandleTypeDef *i2c_handles[I2C_NUM];
@@ -121,7 +127,7 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
121127
#if !defined(STM32F0xx) && !defined(STM32L0xx)
122128
obj->irqER = I2C1_ER_IRQn;
123129
#endif // !defined(STM32F0xx) && !defined(STM32L0xx)
124-
i2c_handles[0] = handle;
130+
i2c_handles[I2C1_INDEX] = handle;
125131
}
126132
#endif // I2C1_BASE
127133
#if defined I2C2_BASE
@@ -134,7 +140,7 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
134140
#if !defined(STM32F0xx) && !defined(STM32L0xx)
135141
obj->irqER = I2C2_ER_IRQn;
136142
#endif // !defined(STM32F0xx) && !defined(STM32L0xx)
137-
i2c_handles[1] = handle;
143+
i2c_handles[I2C2_INDEX] = handle;
138144
}
139145
#endif // I2C2_BASE
140146
#if defined I2C3_BASE
@@ -147,7 +153,7 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
147153
#if !defined(STM32F0xx) && !defined(STM32L0xx)
148154
obj->irqER = I2C3_ER_IRQn;
149155
#endif // !defined(STM32F0xx) && !defined(STM32L0xx)
150-
i2c_handles[2] = handle;
156+
i2c_handles[I2C3_INDEX] = handle;
151157
}
152158
#endif // I2C3_BASE
153159
#if defined I2C4_BASE
@@ -160,7 +166,7 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
160166
#if !defined(STM32F0xx) && !defined(STM32L0xx)
161167
obj->irqER = I2C4_ER_IRQn;
162168
#endif // !defined(STM32F0xx) && !defined(STM32L0xx)
163-
i2c_handles[3] = handle;
169+
i2c_handles[I2C4_INDEX] = handle;
164170
}
165171
#endif // I2C4_BASE
166172

@@ -577,7 +583,7 @@ void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
577583
*/
578584
void I2C1_EV_IRQHandler(void)
579585
{
580-
I2C_HandleTypeDef *handle = i2c_handles[0];
586+
I2C_HandleTypeDef *handle = i2c_handles[I2C1_INDEX];
581587
HAL_I2C_EV_IRQHandler(handle);
582588
#if defined(STM32F0xx) || defined(STM32L0xx)
583589
HAL_I2C_ER_IRQHandler(handle);
@@ -592,7 +598,7 @@ void I2C1_EV_IRQHandler(void)
592598
*/
593599
void I2C1_ER_IRQHandler(void)
594600
{
595-
I2C_HandleTypeDef *handle = i2c_handles[0];
601+
I2C_HandleTypeDef *handle = i2c_handles[I2C1_INDEX];
596602
HAL_I2C_ER_IRQHandler(handle);
597603
}
598604
#endif // !defined(STM32F0xx) && !defined(STM32L0xx)
@@ -606,7 +612,7 @@ void I2C1_ER_IRQHandler(void)
606612
*/
607613
void I2C2_EV_IRQHandler(void)
608614
{
609-
I2C_HandleTypeDef *handle = i2c_handles[1];
615+
I2C_HandleTypeDef *handle = i2c_handles[I2C2_INDEX];
610616
HAL_I2C_EV_IRQHandler(handle);
611617
#if defined(STM32F0xx) || defined(STM32L0xx)
612618
HAL_I2C_ER_IRQHandler(handle);
@@ -621,7 +627,7 @@ void I2C2_EV_IRQHandler(void)
621627
*/
622628
void I2C2_ER_IRQHandler(void)
623629
{
624-
I2C_HandleTypeDef *handle = i2c_handles[1];
630+
I2C_HandleTypeDef *handle = i2c_handles[I2C2_INDEX];
625631
HAL_I2C_ER_IRQHandler(handle);
626632
}
627633
#endif // !defined(STM32F0xx) && !defined(STM32L0xx)
@@ -635,7 +641,7 @@ void I2C2_ER_IRQHandler(void)
635641
*/
636642
void I2C3_EV_IRQHandler(void)
637643
{
638-
I2C_HandleTypeDef *handle = i2c_handles[2];
644+
I2C_HandleTypeDef *handle = i2c_handles[I2C3_INDEX];
639645
HAL_I2C_EV_IRQHandler(handle);
640646
#if defined(STM32F0xx) || defined(STM32L0xx)
641647
HAL_I2C_ER_IRQHandler(handle);
@@ -650,7 +656,7 @@ void I2C3_EV_IRQHandler(void)
650656
*/
651657
void I2C3_ER_IRQHandler(void)
652658
{
653-
I2C_HandleTypeDef *handle = i2c_handles[2];
659+
I2C_HandleTypeDef *handle = i2c_handles[I2C3_INDEX];
654660
HAL_I2C_ER_IRQHandler(handle);
655661
}
656662
#endif // !defined(STM32F0xx) && !defined(STM32L0xx)
@@ -664,7 +670,7 @@ void I2C3_ER_IRQHandler(void)
664670
*/
665671
void I2C4_EV_IRQHandler(void)
666672
{
667-
I2C_HandleTypeDef *handle = i2c_handles[3];
673+
I2C_HandleTypeDef *handle = i2c_handles[I2C4_INDEX];
668674
HAL_I2C_EV_IRQHandler(handle);
669675
#if defined(STM32F0xx) || defined(STM32L0xx)
670676
HAL_I2C_ER_IRQHandler(handle);
@@ -679,7 +685,7 @@ void I2C4_EV_IRQHandler(void)
679685
*/
680686
void I2C4_ER_IRQHandler(void)
681687
{
682-
I2C_HandleTypeDef *handle = i2c_handles[3];
688+
I2C_HandleTypeDef *handle = i2c_handles[I2C4_INDEX];
683689
HAL_I2C_ER_IRQHandler(handle);
684690
}
685691
#endif // !defined(STM32F0xx) && !defined(STM32L0xx)

0 commit comments

Comments
 (0)