Skip to content

Commit 2b925d7

Browse files
committed
Merge branch 'fix-netduino-i2c-spi' of github.com:dhylands/micropython into dhylands-fix-netduino-i2c-spi
2 parents dde739d + f70630c commit 2b925d7

3 files changed

Lines changed: 35 additions & 27 deletions

File tree

stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
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)
2022

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

stmhal/i2c.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
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+
1620
I2C_HandleTypeDef I2CHandle1 = {.Instance = NULL};
1721
I2C_HandleTypeDef I2CHandle2 = {.Instance = NULL};
1822

@@ -32,14 +36,17 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
3236
GPIO_InitStructure.Pull = GPIO_NOPULL; // have external pull-up resistors on both lines
3337

3438
const pin_obj_t *pins[2];
39+
#if MICROPY_HW_ENABLE_I2C1
3540
if (i2c == &I2CHandle1) {
3641
// X-skin: X9=PB6=SCL, X10=PB7=SDA
3742
pins[0] = &pin_B6;
3843
pins[1] = &pin_B7;
3944
GPIO_InitStructure.Alternate = GPIO_AF4_I2C1;
4045
// enable the I2C clock
4146
__I2C1_CLK_ENABLE();
42-
} else {
47+
} else
48+
#endif
49+
if (i2c == &I2CHandle2) {
4350
// Y-skin: Y9=PB10=SCL, Y10=PB11=SDA
4451
pins[0] = &pin_B10;
4552
pins[1] = &pin_B11;
@@ -54,13 +61,6 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
5461
HAL_GPIO_Init(pins[i]->gpio, &GPIO_InitStructure);
5562
}
5663

57-
// enable the I2C clock
58-
if (i2c == &I2CHandle1) {
59-
__I2C1_CLK_ENABLE();
60-
} else {
61-
__I2C2_CLK_ENABLE();
62-
}
63-
6464
// init the I2C device
6565
i2c->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
6666
i2c->Init.ClockSpeed = 400000;
@@ -88,7 +88,10 @@ typedef struct _pyb_i2c_obj_t {
8888
I2C_HandleTypeDef *i2c;
8989
} pyb_i2c_obj_t;
9090

91-
STATIC const pyb_i2c_obj_t pyb_i2c_obj[PYB_NUM_I2C] = {{{&pyb_i2c_type}, &I2CHandle1}, {{&pyb_i2c_type}, &I2CHandle2}};
91+
STATIC const pyb_i2c_obj_t pyb_i2c_obj[PYB_NUM_I2C] = {
92+
{{&pyb_i2c_type}, &I2CHandle1},
93+
{{&pyb_i2c_type}, &I2CHandle2}
94+
};
9295

9396
STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
9497
// check arguments

stmhal/spi.c

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@
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+
1620
SPI_HandleTypeDef SPIHandle1 = {.Instance = NULL};
1721
SPI_HandleTypeDef SPIHandle2 = {.Instance = NULL};
18-
#if MICROPY_HW_ENABLE_SPI3
1922
SPI_HandleTypeDef SPIHandle3 = {.Instance = NULL};
20-
#endif
2123

2224
void spi_init0(void) {
2325
// reset the SPI handles
2426
memset(&SPIHandle1, 0, sizeof(SPI_HandleTypeDef));
2527
SPIHandle1.Instance = SPI1;
2628
memset(&SPIHandle2, 0, sizeof(SPI_HandleTypeDef));
2729
SPIHandle2.Instance = SPI2;
28-
#if MICROPY_HW_ENABLE_SPI3
2930
memset(&SPIHandle3, 0, sizeof(SPI_HandleTypeDef));
3031
SPIHandle3.Instance = SPI3;
31-
#endif
3232
}
3333

3434
// TODO allow to take a list of pins to use
@@ -40,32 +40,36 @@ void spi_init(SPI_HandleTypeDef *spi) {
4040
GPIO_InitStructure.Pull = GPIO_PULLUP; // ST examples use PULLUP
4141

4242
const pin_obj_t *pins[4];
43+
#if MICROPY_HW_ENABLE_SPI1
4344
if (spi->Instance == SPI1) {
4445
// X-skin: X5=PA4=SPI1_NSS, X6=PA5=SPI1_SCK, X7=PA6=SPI1_MISO, X8=PA7=SPI1_MOSI
4546
pins[0] = &pin_A4;
4647
pins[1] = &pin_A5;
4748
pins[2] = &pin_A6;
4849
pins[3] = &pin_A7;
4950
GPIO_InitStructure.Alternate = GPIO_AF5_SPI1;
50-
} else if (spi->Instance == SPI2) {
51+
} else
52+
#endif
53+
if (spi->Instance == SPI2) {
5154
// Y-skin: Y5=PB12=SPI2_NSS, Y6=PB13=SPI2_SCK, Y7=PB14=SPI2_MISO, Y8=PB15=SPI2_MOSI
5255
pins[0] = &pin_B12;
5356
pins[1] = &pin_B13;
5457
pins[2] = &pin_B14;
5558
pins[3] = &pin_B15;
5659
GPIO_InitStructure.Alternate = GPIO_AF5_SPI2;
60+
} else
5761
#if MICROPY_HW_ENABLE_SPI3
58-
} else if (spi->Instance == SPI3) {
62+
if (spi->Instance == SPI3) {
5963
pins[0] = &pin_A4;
6064
pins[1] = &pin_B3;
6165
pins[2] = &pin_B4;
6266
pins[3] = &pin_B5;
6367
GPIO_InitStructure.Alternate = GPIO_AF6_SPI3;
68+
} else
6469
#endif
65-
} else {
70+
{
6671
// SPI does not exist for this board
67-
printf("HardwareError: invalid SPI\n");
68-
return;
72+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "SPI bus does not exist"));
6973
}
7074

7175
for (uint i = 0; i < 4; i++) {
@@ -78,10 +82,8 @@ void spi_init(SPI_HandleTypeDef *spi) {
7882
__SPI1_CLK_ENABLE();
7983
} else if (spi->Instance == SPI2) {
8084
__SPI2_CLK_ENABLE();
81-
#if MICROPY_HW_ENABLE_SPI3
82-
} else {
85+
} else if (spi->Instance == SPI3) {
8386
__SPI3_CLK_ENABLE();
84-
#endif
8587
}
8688

8789
// init the I2C device
@@ -100,24 +102,25 @@ void spi_deinit(SPI_HandleTypeDef *spi) {
100102
__SPI1_CLK_DISABLE();
101103
} else if (spi->Instance == SPI2) {
102104
__SPI2_CLK_DISABLE();
103-
#if MICROPY_HW_ENABLE_SPI3
104-
} else {
105+
} else if (spi->Instance == SPI3) {
105106
__SPI3_CLK_DISABLE();
106-
#endif
107107
}
108108
}
109109

110110
/******************************************************************************/
111111
/* Micro Python bindings */
112112

113-
#define PYB_SPI_NUM (2)
113+
#define PYB_NUM_SPI (2)
114114

115115
typedef struct _pyb_spi_obj_t {
116116
mp_obj_base_t base;
117117
SPI_HandleTypeDef *spi;
118118
} pyb_spi_obj_t;
119119

120-
STATIC const pyb_spi_obj_t pyb_spi_obj[PYB_SPI_NUM] = {{{&pyb_spi_type}, &SPIHandle1}, {{&pyb_spi_type}, &SPIHandle2}};
120+
STATIC const pyb_spi_obj_t pyb_spi_obj[PYB_NUM_SPI] = {
121+
{{&pyb_spi_type}, &SPIHandle1},
122+
{{&pyb_spi_type}, &SPIHandle2}
123+
};
121124

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

222225
// check SPI number
223-
if (!(0 <= spi_id && spi_id < PYB_SPI_NUM)) {
226+
if (!(0 <= spi_id && spi_id < PYB_NUM_SPI)) {
224227
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "SPI bus %d does not exist", spi_id + 1));
225228
}
226229

0 commit comments

Comments
 (0)