Skip to content

Commit ff91156

Browse files
committed
stmhal: Improved configurability for I2C and SPI busses.
The HAL handles for the I2C/SPI objects are rather large, so we don't want to unnecessarily include them.
1 parent 2b925d7 commit ff91156

8 files changed

Lines changed: 80 additions & 43 deletions

File tree

stmhal/accel.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "i2c.h"
1313
#include "accel.h"
1414

15+
#if MICROPY_HW_HAS_MMA7660
16+
1517
#define MMA_ADDR (0x98)
1618
#define MMA_REG_X (0)
1719
#define MMA_REG_Y (1)
@@ -174,3 +176,5 @@ const mp_obj_type_t pyb_accel_type = {
174176
.make_new = pyb_accel_make_new,
175177
.locals_dict = (mp_obj_t)&pyb_accel_locals_dict,
176178
};
179+
180+
#endif // MICROPY_HW_HAS_MMA7660

stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
#define MICROPY_HW_ENABLE_TIMER (1)
1818
#define MICROPY_HW_ENABLE_SERVO (1)
1919
#define MICROPY_HW_ENABLE_DAC (0)
20-
#define MICROPU_HW_ENABLE_I2C1 (0)
21-
#define MICROPU_HW_ENABLE_SPI1 (0)
20+
#define MICROPY_HW_ENABLE_I2C1 (0)
21+
#define MICROPY_HW_ENABLE_SPI1 (0)
22+
#define MICROPY_HW_ENABLE_SPI3 (0)
2223

2324
// USRSW is pulled low. Pressing the button makes the input go high.
2425
#define MICROPY_HW_USRSW_PIN (pin_B11)

stmhal/boards/PYBV10/mpconfigboard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
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)
17+
#define MICROPY_HW_ENABLE_SPI1 (1)
18+
#define MICROPY_HW_ENABLE_SPI3 (0)
1619

1720
// USRSW has no pullup or pulldown, and pressing the switch makes the input go low
1821
#define MICROPY_HW_USRSW_PIN (pin_B3)

stmhal/boards/PYBV3/mpconfigboard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#define MICROPY_HW_ENABLE_TIMER (1)
1414
#define MICROPY_HW_ENABLE_SERVO (1)
1515
#define MICROPY_HW_ENABLE_DAC (0)
16+
#define MICROPY_HW_ENABLE_I2C1 (1)
17+
#define MICROPY_HW_ENABLE_SPI1 (1)
18+
#define MICROPY_HW_ENABLE_SPI3 (0)
1619

1720
// USRSW has no pullup or pulldown, and pressing the switch makes the input go low
1821
#define MICROPY_HW_USRSW_PIN (pin_A13)

stmhal/boards/PYBV4/mpconfigboard.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
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)
17+
#define MICROPY_HW_ENABLE_SPI1 (1)
18+
#define MICROPY_HW_ENABLE_SPI3 (0)
1619

1720
// USRSW has no pullup or pulldown, and pressing the switch makes the input go low
1821
#define MICROPY_HW_USRSW_PIN (pin_B3)
1922
#define MICROPY_HW_USRSW_PULL (GPIO_PULLUP)
2023
#define MICROPY_HW_USRSW_EXTI_MODE (GPIO_MODE_IT_FALLING)
2124
#define MICROPY_HW_USRSW_PRESSED (0)
2225

23-
// LEDs
26+
// The pyboard has 4 LEDs
2427
#define MICROPY_HW_LED1 (pin_A13) // red
2528
#define MICROPY_HW_LED2 (pin_A14) // green
2629
#define MICROPY_HW_LED3 (pin_A15) // yellow

stmhal/boards/STM32F4DISC/mpconfigboard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#define MICROPY_HW_ENABLE_TIMER (1)
1414
#define MICROPY_HW_ENABLE_SERVO (0)
1515
#define MICROPY_HW_ENABLE_DAC (0)
16+
#define MICROPY_HW_ENABLE_I2C1 (1)
17+
#define MICROPY_HW_ENABLE_SPI1 (1)
18+
#define MICROPY_HW_ENABLE_SPI3 (0)
1619

1720
// USRSW is pulled low. Pressing the button makes the input go high.
1821
#define MICROPY_HW_USRSW_PIN (pin_A0)

stmhal/i2c.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
#include "genhdr/pins.h"
1414
#include "i2c.h"
1515

16-
#if !defined(MICROPU_HW_ENABLE_I2C1)
17-
#define MICROPY_HW_ENABLE_I2C1 (1)
18-
#endif
19-
16+
#if MICROPY_HW_ENABLE_I2C1
2017
I2C_HandleTypeDef I2CHandle1 = {.Instance = NULL};
18+
#endif
2119
I2C_HandleTypeDef I2CHandle2 = {.Instance = NULL};
2220

2321
void i2c_init0(void) {
2422
// reset the I2C1 handles
23+
#if MICROPY_HW_ENABLE_I2C1
2524
memset(&I2CHandle1, 0, sizeof(I2C_HandleTypeDef));
2625
I2CHandle1.Instance = I2C1;
26+
#endif
2727
memset(&I2CHandle2, 0, sizeof(I2C_HandleTypeDef));
2828
I2CHandle2.Instance = I2C2;
2929
}
@@ -36,23 +36,26 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
3636
GPIO_InitStructure.Pull = GPIO_NOPULL; // have external pull-up resistors on both lines
3737

3838
const pin_obj_t *pins[2];
39+
if (0) {
3940
#if MICROPY_HW_ENABLE_I2C1
40-
if (i2c == &I2CHandle1) {
41+
} else if (i2c == &I2CHandle1) {
4142
// X-skin: X9=PB6=SCL, X10=PB7=SDA
4243
pins[0] = &pin_B6;
4344
pins[1] = &pin_B7;
4445
GPIO_InitStructure.Alternate = GPIO_AF4_I2C1;
4546
// enable the I2C clock
4647
__I2C1_CLK_ENABLE();
47-
} else
4848
#endif
49-
if (i2c == &I2CHandle2) {
49+
} else if (i2c == &I2CHandle2) {
5050
// Y-skin: Y9=PB10=SCL, Y10=PB11=SDA
5151
pins[0] = &pin_B10;
5252
pins[1] = &pin_B11;
5353
GPIO_InitStructure.Alternate = GPIO_AF4_I2C2;
5454
// enable the I2C clock
5555
__I2C2_CLK_ENABLE();
56+
} else {
57+
// I2C does not exist for this board (shouldn't get here, should be checked by caller)
58+
return;
5659
}
5760

5861
// init the GPIO lines
@@ -73,6 +76,8 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
7376

7477
if (HAL_I2C_Init(i2c) != HAL_OK) {
7578
// init error
79+
// TODO should raise an exception, but this function is not necessarily going to be
80+
// called via Python, so may not be properly wrapped in an NLR handler
7681
printf("HardwareError: HAL_I2C_Init failed\n");
7782
return;
7883
}
@@ -81,17 +86,20 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
8186
/******************************************************************************/
8287
/* Micro Python bindings */
8388

84-
#define PYB_NUM_I2C (2)
85-
8689
typedef struct _pyb_i2c_obj_t {
8790
mp_obj_base_t base;
8891
I2C_HandleTypeDef *i2c;
8992
} pyb_i2c_obj_t;
9093

91-
STATIC const pyb_i2c_obj_t pyb_i2c_obj[PYB_NUM_I2C] = {
94+
STATIC const pyb_i2c_obj_t pyb_i2c_obj[] = {
95+
#if MICROPY_HW_ENABLE_I2C1
9296
{{&pyb_i2c_type}, &I2CHandle1},
97+
#else
98+
{{&pyb_i2c_type}, NULL},
99+
#endif
93100
{{&pyb_i2c_type}, &I2CHandle2}
94101
};
102+
#define PYB_NUM_I2C (sizeof(pyb_i2c_obj) / sizeof(pyb_i2c_obj[0]))
95103

96104
STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
97105
// check arguments
@@ -101,7 +109,7 @@ STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
101109
machine_int_t i2c_id = mp_obj_get_int(args[0]) - 1;
102110

103111
// check i2c number
104-
if (!(0 <= i2c_id && i2c_id < PYB_NUM_I2C)) {
112+
if (!(0 <= i2c_id && i2c_id < PYB_NUM_I2C && pyb_i2c_obj[i2c_id].i2c != NULL)) {
105113
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "I2C bus %d does not exist", i2c_id + 1));
106114
}
107115

stmhal/spi.c

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,26 @@
1313
#include "genhdr/pins.h"
1414
#include "spi.h"
1515

16-
#if !defined(MICROPU_HW_ENABLE_SPI1)
17-
#define MICROPY_HW_ENABLE_SPI1 (1)
18-
#endif
19-
16+
#if MICROPY_HW_ENABLE_SPI1
2017
SPI_HandleTypeDef SPIHandle1 = {.Instance = NULL};
18+
#endif
2119
SPI_HandleTypeDef SPIHandle2 = {.Instance = NULL};
20+
#if MICROPY_HW_ENABLE_SPI3
2221
SPI_HandleTypeDef SPIHandle3 = {.Instance = NULL};
22+
#endif
2323

2424
void spi_init0(void) {
2525
// reset the SPI handles
26+
#if MICROPY_HW_ENABLE_SPI1
2627
memset(&SPIHandle1, 0, sizeof(SPI_HandleTypeDef));
2728
SPIHandle1.Instance = SPI1;
29+
#endif
2830
memset(&SPIHandle2, 0, sizeof(SPI_HandleTypeDef));
2931
SPIHandle2.Instance = SPI2;
32+
#if MICROPY_HW_ENABLE_SPI3
3033
memset(&SPIHandle3, 0, sizeof(SPI_HandleTypeDef));
3134
SPIHandle3.Instance = SPI3;
35+
#endif
3236
}
3337

3438
// TODO allow to take a list of pins to use
@@ -40,53 +44,48 @@ void spi_init(SPI_HandleTypeDef *spi) {
4044
GPIO_InitStructure.Pull = GPIO_PULLUP; // ST examples use PULLUP
4145

4246
const pin_obj_t *pins[4];
47+
if (0) {
4348
#if MICROPY_HW_ENABLE_SPI1
44-
if (spi->Instance == SPI1) {
49+
} else if (spi->Instance == SPI1) {
4550
// X-skin: X5=PA4=SPI1_NSS, X6=PA5=SPI1_SCK, X7=PA6=SPI1_MISO, X8=PA7=SPI1_MOSI
4651
pins[0] = &pin_A4;
4752
pins[1] = &pin_A5;
4853
pins[2] = &pin_A6;
4954
pins[3] = &pin_A7;
5055
GPIO_InitStructure.Alternate = GPIO_AF5_SPI1;
51-
} else
56+
// enable the SPI clock
57+
__SPI1_CLK_ENABLE();
5258
#endif
53-
if (spi->Instance == SPI2) {
59+
} else if (spi->Instance == SPI2) {
5460
// Y-skin: Y5=PB12=SPI2_NSS, Y6=PB13=SPI2_SCK, Y7=PB14=SPI2_MISO, Y8=PB15=SPI2_MOSI
5561
pins[0] = &pin_B12;
5662
pins[1] = &pin_B13;
5763
pins[2] = &pin_B14;
5864
pins[3] = &pin_B15;
5965
GPIO_InitStructure.Alternate = GPIO_AF5_SPI2;
60-
} else
66+
// enable the SPI clock
67+
__SPI2_CLK_ENABLE();
6168
#if MICROPY_HW_ENABLE_SPI3
62-
if (spi->Instance == SPI3) {
69+
} else if (spi->Instance == SPI3) {
6370
pins[0] = &pin_A4;
6471
pins[1] = &pin_B3;
6572
pins[2] = &pin_B4;
6673
pins[3] = &pin_B5;
6774
GPIO_InitStructure.Alternate = GPIO_AF6_SPI3;
68-
} else
75+
// enable the SPI clock
76+
__SPI3_CLK_ENABLE();
6977
#endif
70-
{
71-
// SPI does not exist for this board
72-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "SPI bus does not exist"));
78+
} else {
79+
// SPI does not exist for this board (shouldn't get here, should be checked by caller)
80+
return;
7381
}
7482

7583
for (uint i = 0; i < 4; i++) {
7684
GPIO_InitStructure.Pin = pins[i]->pin_mask;
7785
HAL_GPIO_Init(pins[i]->gpio, &GPIO_InitStructure);
7886
}
7987

80-
// enable the SPI clock
81-
if (spi->Instance == SPI1) {
82-
__SPI1_CLK_ENABLE();
83-
} else if (spi->Instance == SPI2) {
84-
__SPI2_CLK_ENABLE();
85-
} else if (spi->Instance == SPI3) {
86-
__SPI3_CLK_ENABLE();
87-
}
88-
89-
// init the I2C device
88+
// init the SPI device
9089
if (HAL_SPI_Init(spi) != HAL_OK) {
9190
// init error
9291
// TODO should raise an exception, but this function is not necessarily going to be
@@ -98,29 +97,42 @@ void spi_init(SPI_HandleTypeDef *spi) {
9897

9998
void spi_deinit(SPI_HandleTypeDef *spi) {
10099
HAL_SPI_DeInit(spi);
101-
if (spi->Instance == SPI1) {
100+
if (0) {
101+
#if MICROPY_HW_ENABLE_SPI1
102+
} else if (spi->Instance == SPI1) {
102103
__SPI1_CLK_DISABLE();
104+
#endif
103105
} else if (spi->Instance == SPI2) {
104106
__SPI2_CLK_DISABLE();
107+
#if MICROPY_HW_ENABLE_SPI3
105108
} else if (spi->Instance == SPI3) {
106109
__SPI3_CLK_DISABLE();
110+
#endif
107111
}
108112
}
109113

110114
/******************************************************************************/
111115
/* Micro Python bindings */
112116

113-
#define PYB_NUM_SPI (2)
114-
115117
typedef struct _pyb_spi_obj_t {
116118
mp_obj_base_t base;
117119
SPI_HandleTypeDef *spi;
118120
} pyb_spi_obj_t;
119121

120-
STATIC const pyb_spi_obj_t pyb_spi_obj[PYB_NUM_SPI] = {
122+
STATIC const pyb_spi_obj_t pyb_spi_obj[] = {
123+
#if MICROPY_HW_ENABLE_SPI1
121124
{{&pyb_spi_type}, &SPIHandle1},
122-
{{&pyb_spi_type}, &SPIHandle2}
125+
#else
126+
{{&pyb_spi_type}, NULL},
127+
#endif
128+
{{&pyb_spi_type}, &SPIHandle2},
129+
#if MICROPY_HW_ENABLE_SPI3
130+
{{&pyb_spi_type}, &SPIHandle3},
131+
#else
132+
{{&pyb_spi_type}, NULL},
133+
#endif
123134
};
135+
#define PYB_NUM_SPI (sizeof(pyb_spi_obj) / sizeof(pyb_spi_obj[0]))
124136

125137
STATIC void pyb_spi_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
126138
pyb_spi_obj_t *self = self_in;
@@ -223,7 +235,7 @@ STATIC mp_obj_t pyb_spi_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
223235
machine_int_t spi_id = mp_obj_get_int(args[0]) - 1;
224236

225237
// check SPI number
226-
if (!(0 <= spi_id && spi_id < PYB_NUM_SPI)) {
238+
if (!(0 <= spi_id && spi_id < PYB_NUM_SPI && pyb_spi_obj[spi_id].spi != NULL)) {
227239
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "SPI bus %d does not exist", spi_id + 1));
228240
}
229241

0 commit comments

Comments
 (0)