Skip to content

Commit d15fe5a

Browse files
committed
stmhal: Make I2C busses and their pins configurable in mpconfigboard.h.
1 parent 8892f71 commit d15fe5a

8 files changed

Lines changed: 104 additions & 30 deletions

File tree

stmhal/boards/CERB40/mpconfigboard.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#define MICROPY_HW_ENABLE_TIMER (1)
1515
#define MICROPY_HW_ENABLE_SERVO (0)
1616
#define MICROPY_HW_ENABLE_DAC (1)
17-
#define MICROPY_HW_ENABLE_I2C1 (1)
1817
#define MICROPY_HW_ENABLE_SPI1 (1)
1918
#define MICROPY_HW_ENABLE_SPI2 (0)
2019
#define MICROPY_HW_ENABLE_SPI3 (1)
@@ -26,6 +25,12 @@
2625
#define MICROPY_HW_CLK_PLLP (RCC_PLLP_DIV2)
2726
#define MICROPY_HW_CLK_PLLQ (7)
2827

28+
// I2C busses
29+
#define MICROPY_HW_I2C1_SCL (pin_B6)
30+
#define MICROPY_HW_I2C1_SDA (pin_B7)
31+
#define MICROPY_HW_I2C2_SCL (pin_B10)
32+
#define MICROPY_HW_I2C2_SDA (pin_B11)
33+
2934
// The Cerb40 has No LEDs
3035

3136
// The Cerb40 has No SDCard

stmhal/boards/HYDRABUS/mpconfigboard.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#define MICROPY_HW_ENABLE_TIMER (1)
1414
#define MICROPY_HW_ENABLE_SERVO (0)
1515
#define MICROPY_HW_ENABLE_DAC (1)
16-
#define MICROPY_HW_ENABLE_I2C1 (1)
1716
#define MICROPY_HW_ENABLE_SPI1 (1)
1817
#define MICROPY_HW_ENABLE_SPI2 (1)
1918
#define MICROPY_HW_ENABLE_SPI3 (1)
@@ -25,6 +24,12 @@
2524
#define MICROPY_HW_CLK_PLLP (RCC_PLLP_DIV2)
2625
#define MICROPY_HW_CLK_PLLQ (7)
2726

27+
// I2C busses
28+
#define MICROPY_HW_I2C1_SCL (pin_B6)
29+
#define MICROPY_HW_I2C1_SDA (pin_B7)
30+
#define MICROPY_HW_I2C2_SCL (pin_B10)
31+
#define MICROPY_HW_I2C2_SDA (pin_B11)
32+
2833
// USRSW/UBTN (Needs Jumper UBTN) is pulled low. Pressing the button makes the input go high.
2934
#define MICROPY_HW_USRSW_PIN (pin_A0)
3035
#define MICROPY_HW_USRSW_PULL (GPIO_NOPULL)

stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#define MICROPY_HW_ENABLE_TIMER (1)
1818
#define MICROPY_HW_ENABLE_SERVO (1)
1919
#define MICROPY_HW_ENABLE_DAC (0)
20-
#define MICROPY_HW_ENABLE_I2C1 (0)
2120
#define MICROPY_HW_ENABLE_SPI1 (0)
2221
#define MICROPY_HW_ENABLE_SPI2 (1)
2322
#define MICROPY_HW_ENABLE_SPI3 (0)
@@ -29,6 +28,10 @@
2928
#define MICROPY_HW_CLK_PLLP (RCC_PLLP_DIV2)
3029
#define MICROPY_HW_CLK_PLLQ (7)
3130

31+
// I2C busses
32+
#define MICROPY_HW_I2C2_SCL (pin_B10)
33+
#define MICROPY_HW_I2C2_SDA (pin_B11)
34+
3235
// USRSW is pulled low. Pressing the button makes the input go high.
3336
#define MICROPY_HW_USRSW_PIN (pin_B11)
3437
#define MICROPY_HW_USRSW_PULL (GPIO_NOPULL)

stmhal/boards/PYBV10/mpconfigboard.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#define MICROPY_HW_ENABLE_TIMER (1)
1515
#define MICROPY_HW_ENABLE_SERVO (1)
1616
#define MICROPY_HW_ENABLE_DAC (1)
17-
#define MICROPY_HW_ENABLE_I2C1 (1)
1817
#define MICROPY_HW_ENABLE_SPI1 (1)
1918
#define MICROPY_HW_ENABLE_SPI2 (1)
2019
#define MICROPY_HW_ENABLE_SPI3 (0)
@@ -29,6 +28,14 @@
2928
// The pyboard has a 32kHz crystal for the RTC
3029
#define MICROPY_HW_RTC_USE_LSE (1)
3130

31+
// X-skin: X9=PB6=SCL, X10=PB7=SDA
32+
#define MICROPY_HW_I2C1_SCL (pin_B6)
33+
#define MICROPY_HW_I2C1_SDA (pin_B7)
34+
35+
// Y-skin: Y9=PB10=SCL, Y10=PB11=SDA
36+
#define MICROPY_HW_I2C2_SCL (pin_B10)
37+
#define MICROPY_HW_I2C2_SDA (pin_B11)
38+
3239
// USRSW has no pullup or pulldown, and pressing the switch makes the input go low
3340
#define MICROPY_HW_USRSW_PIN (pin_B3)
3441
#define MICROPY_HW_USRSW_PULL (GPIO_PULLUP)

stmhal/boards/PYBV3/mpconfigboard.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#define MICROPY_HW_ENABLE_TIMER (1)
1414
#define MICROPY_HW_ENABLE_SERVO (1)
1515
#define MICROPY_HW_ENABLE_DAC (1)
16-
#define MICROPY_HW_ENABLE_I2C1 (1)
1716
#define MICROPY_HW_ENABLE_SPI1 (1)
1817
#define MICROPY_HW_ENABLE_SPI2 (1)
1918
#define MICROPY_HW_ENABLE_SPI3 (0)
@@ -28,6 +27,14 @@
2827
// The pyboard has a 32kHz crystal for the RTC
2928
#define MICROPY_HW_RTC_USE_LSE (1)
3029

30+
// X-skin: X9=PB6=SCL, X10=PB7=SDA
31+
#define MICROPY_HW_I2C1_SCL (pin_B6)
32+
#define MICROPY_HW_I2C1_SDA (pin_B7)
33+
34+
// Y-skin: Y9=PB10=SCL, Y10=PB11=SDA
35+
#define MICROPY_HW_I2C2_SCL (pin_B10)
36+
#define MICROPY_HW_I2C2_SDA (pin_B11)
37+
3138
// USRSW has no pullup or pulldown, and pressing the switch makes the input go low
3239
#define MICROPY_HW_USRSW_PIN (pin_A13)
3340
#define MICROPY_HW_USRSW_PULL (GPIO_PULLUP)

stmhal/boards/PYBV4/mpconfigboard.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#define MICROPY_HW_ENABLE_TIMER (1)
1414
#define MICROPY_HW_ENABLE_SERVO (1)
1515
#define MICROPY_HW_ENABLE_DAC (1)
16-
#define MICROPY_HW_ENABLE_I2C1 (1)
1716
#define MICROPY_HW_ENABLE_SPI1 (1)
1817
#define MICROPY_HW_ENABLE_SPI2 (1)
1918
#define MICROPY_HW_ENABLE_SPI3 (0)
@@ -28,6 +27,14 @@
2827
// The pyboard has a 32kHz crystal for the RTC
2928
#define MICROPY_HW_RTC_USE_LSE (1)
3029

30+
// X-skin: X9=PB6=SCL, X10=PB7=SDA
31+
#define MICROPY_HW_I2C1_SCL (pin_B6)
32+
#define MICROPY_HW_I2C1_SDA (pin_B7)
33+
34+
// Y-skin: Y9=PB10=SCL, Y10=PB11=SDA
35+
#define MICROPY_HW_I2C2_SCL (pin_B10)
36+
#define MICROPY_HW_I2C2_SDA (pin_B11)
37+
3138
// USRSW has no pullup or pulldown, and pressing the switch makes the input go low
3239
#define MICROPY_HW_USRSW_PIN (pin_B3)
3340
#define MICROPY_HW_USRSW_PULL (GPIO_PULLUP)

stmhal/boards/STM32F4DISC/mpconfigboard.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#define MICROPY_HW_ENABLE_TIMER (1)
1414
#define MICROPY_HW_ENABLE_SERVO (0)
1515
#define MICROPY_HW_ENABLE_DAC (1)
16-
#define MICROPY_HW_ENABLE_I2C1 (1)
1716
#define MICROPY_HW_ENABLE_SPI1 (1)
1817
#define MICROPY_HW_ENABLE_SPI2 (1)
1918
#define MICROPY_HW_ENABLE_SPI3 (0)
@@ -25,6 +24,12 @@
2524
#define MICROPY_HW_CLK_PLLP (RCC_PLLP_DIV2)
2625
#define MICROPY_HW_CLK_PLLQ (7)
2726

27+
// I2C busses
28+
#define MICROPY_HW_I2C1_SCL (pin_B6)
29+
#define MICROPY_HW_I2C1_SDA (pin_B7)
30+
#define MICROPY_HW_I2C2_SCL (pin_B10)
31+
#define MICROPY_HW_I2C2_SDA (pin_B11)
32+
2833
// USRSW is pulled low. Pressing the button makes the input go high.
2934
#define MICROPY_HW_USRSW_PIN (pin_A0)
3035
#define MICROPY_HW_USRSW_PULL (GPIO_NOPULL)

stmhal/i2c.c

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,30 @@
8787
#define PYB_I2C_MASTER (0)
8888
#define PYB_I2C_SLAVE (1)
8989

90-
#if MICROPY_HW_ENABLE_I2C1
90+
#if defined(MICROPY_HW_I2C1_SCL)
9191
I2C_HandleTypeDef I2CHandle1 = {.Instance = NULL};
9292
#endif
93+
#if defined(MICROPY_HW_I2C2_SCL)
9394
I2C_HandleTypeDef I2CHandle2 = {.Instance = NULL};
95+
#endif
96+
#if defined(MICROPY_HW_I2C3_SCL)
97+
I2C_HandleTypeDef I2CHandle3 = {.Instance = NULL};
98+
#endif
9499

95100
void i2c_init0(void) {
96101
// reset the I2C1 handles
97-
#if MICROPY_HW_ENABLE_I2C1
102+
#if defined(MICROPY_HW_I2C1_SCL)
98103
memset(&I2CHandle1, 0, sizeof(I2C_HandleTypeDef));
99104
I2CHandle1.Instance = I2C1;
100-
#endif
105+
#endif
106+
#if defined(MICROPY_HW_I2C2_SCL)
101107
memset(&I2CHandle2, 0, sizeof(I2C_HandleTypeDef));
102108
I2CHandle2.Instance = I2C2;
109+
#endif
110+
#if defined(MICROPY_HW_I2C3_SCL)
111+
memset(&I2CHandle3, 0, sizeof(I2C_HandleTypeDef));
112+
I2CHandle3.Instance = I2C3;
113+
#endif
103114
}
104115

105116
void i2c_init(I2C_HandleTypeDef *i2c) {
@@ -111,22 +122,27 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
111122

112123
const pin_obj_t *pins[2];
113124
if (0) {
114-
#if MICROPY_HW_ENABLE_I2C1
125+
#if defined(MICROPY_HW_I2C1_SCL)
115126
} else if (i2c == &I2CHandle1) {
116-
// X-skin: X9=PB6=SCL, X10=PB7=SDA
117-
pins[0] = &pin_B6;
118-
pins[1] = &pin_B7;
127+
pins[0] = &MICROPY_HW_I2C1_SCL;
128+
pins[1] = &MICROPY_HW_I2C1_SDA;
119129
GPIO_InitStructure.Alternate = GPIO_AF4_I2C1;
120-
// enable the I2C clock
121130
__I2C1_CLK_ENABLE();
122-
#endif
131+
#endif
132+
#if defined(MICROPY_HW_I2C2_SCL)
123133
} else if (i2c == &I2CHandle2) {
124-
// Y-skin: Y9=PB10=SCL, Y10=PB11=SDA
125-
pins[0] = &pin_B10;
126-
pins[1] = &pin_B11;
134+
pins[0] = &MICROPY_HW_I2C2_SCL;
135+
pins[1] = &MICROPY_HW_I2C2_SDA;
127136
GPIO_InitStructure.Alternate = GPIO_AF4_I2C2;
128-
// enable the I2C clock
129137
__I2C2_CLK_ENABLE();
138+
#endif
139+
#if defined(MICROPY_HW_I2C3_SCL)
140+
} else if (i2c == &I2CHandle3) {
141+
pins[0] = &MICROPY_HW_I2C3_SCL;
142+
pins[1] = &MICROPY_HW_I2C3_SDA;
143+
GPIO_InitStructure.Alternate = GPIO_AF4_I2C3;
144+
__I2C3_CLK_ENABLE();
145+
#endif
130146
} else {
131147
// I2C does not exist for this board (shouldn't get here, should be checked by caller)
132148
return;
@@ -151,16 +167,24 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
151167
void i2c_deinit(I2C_HandleTypeDef *i2c) {
152168
HAL_I2C_DeInit(i2c);
153169
if (0) {
154-
#if MICROPY_HW_ENABLE_I2C1
170+
#if defined(MICROPY_HW_I2C1_SCL)
155171
} else if (i2c->Instance == I2C1) {
156172
__I2C1_FORCE_RESET();
157173
__I2C1_RELEASE_RESET();
158174
__I2C1_CLK_DISABLE();
159-
#endif
175+
#endif
176+
#if defined(MICROPY_HW_I2C2_SCL)
160177
} else if (i2c->Instance == I2C2) {
161178
__I2C2_FORCE_RESET();
162179
__I2C2_RELEASE_RESET();
163180
__I2C2_CLK_DISABLE();
181+
#endif
182+
#if defined(MICROPY_HW_I2C3_SCL)
183+
} else if (i2c->Instance == I2C3) {
184+
__I2C3_FORCE_RESET();
185+
__I2C3_RELEASE_RESET();
186+
__I2C3_CLK_DISABLE();
187+
#endif
164188
}
165189
}
166190

@@ -175,20 +199,31 @@ typedef struct _pyb_i2c_obj_t {
175199
STATIC inline bool in_master_mode(pyb_i2c_obj_t *self) { return self->i2c->Init.OwnAddress1 == PYB_I2C_MASTER_ADDRESS; }
176200

177201
STATIC const pyb_i2c_obj_t pyb_i2c_obj[] = {
178-
#if MICROPY_HW_ENABLE_I2C1
202+
#if defined(MICROPY_HW_I2C1_SCL)
179203
{{&pyb_i2c_type}, &I2CHandle1},
180-
#else
181-
{{&pyb_i2c_type}, NULL},
182-
#endif
183-
{{&pyb_i2c_type}, &I2CHandle2}
204+
#endif
205+
#if defined(MICROPY_HW_I2C2_SCL)
206+
{{&pyb_i2c_type}, &I2CHandle2},
207+
#endif
208+
#if defined(MICROPY_HW_I2C3_SCL)
209+
{{&pyb_i2c_type}, &I2CHandle3},
210+
#endif
184211
};
185212

186213
STATIC void pyb_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
187214
pyb_i2c_obj_t *self = self_in;
188215

189-
uint i2c_num;
190-
if (self->i2c->Instance == I2C1) { i2c_num = 1; }
191-
else { i2c_num = 2; }
216+
uint i2c_num = 0;
217+
if (0) { }
218+
#if defined(MICROPY_HW_I2C1_SCL)
219+
else if (self->i2c->Instance == I2C1) { i2c_num = 1; }
220+
#endif
221+
#if defined(MICROPY_HW_I2C2_SCL)
222+
else if (self->i2c->Instance == I2C2) { i2c_num = 2; }
223+
#endif
224+
#if defined(MICROPY_HW_I2C3_SCL)
225+
else if (self->i2c->Instance == I2C3) { i2c_num = 3; }
226+
#endif
192227

193228
if (self->i2c->State == HAL_I2C_STATE_RESET) {
194229
mp_printf(print, "I2C(%u)", i2c_num);

0 commit comments

Comments
 (0)