Skip to content

Commit 5e11d2b

Browse files
dhylandsdpgeorge
authored andcommitted
stmhal: Enable SPI support for F7 MCUs.
1 parent 34fe5a3 commit 5e11d2b

4 files changed

Lines changed: 42 additions & 24 deletions

File tree

stmhal/boards/STM32F7DISC/mpconfigboard.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
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_SPI1 (1)
16+
#define MICROPY_HW_ENABLE_SPI1 (0)
1717
#define MICROPY_HW_ENABLE_SPI2 (1)
1818
#define MICROPY_HW_ENABLE_SPI3 (0)
1919
#define MICROPY_HW_ENABLE_CAN (1)
@@ -60,6 +60,12 @@ void STM32F7DISC_board_early_init(void);
6060
#define MICROPY_HW_I2C_BAUDRATE_DEFAULT 100000
6161
#define MICROPY_HW_I2C_BAUDRATE_MAX 100000
6262

63+
// SPI
64+
#define MICROPY_HW_SPI2_NSS (pin_I0)
65+
#define MICROPY_HW_SPI2_SCK (pin_I1)
66+
#define MICROPY_HW_SPI2_MISO (pin_B14)
67+
#define MICROPY_HW_SPI2_MOSI (pin_B15)
68+
6369
// USRSW is pulled low. Pressing the button makes the input go high.
6470
#define MICROPY_HW_USRSW_PIN (pin_I11)
6571
#define MICROPY_HW_USRSW_PULL (GPIO_NOPULL)

stmhal/main.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,7 @@ int main(void) {
472472
#endif
473473

474474
i2c_init0();
475-
#if !defined(STM32F7) // Temp hack
476475
spi_init0();
477-
#endif
478476
pyb_usb_init0();
479477

480478
// Initialise the local flash filesystem.

stmhal/modpyb.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,9 +590,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
590590
{ MP_OBJ_NEW_QSTR(MP_QSTR_LED), (mp_obj_t)&pyb_led_type },
591591
#endif
592592
{ MP_OBJ_NEW_QSTR(MP_QSTR_I2C), (mp_obj_t)&pyb_i2c_type },
593-
#if !defined(STM32F7) // Temp hack
594593
{ MP_OBJ_NEW_QSTR(MP_QSTR_SPI), (mp_obj_t)&pyb_spi_type },
595-
#endif
596594
{ MP_OBJ_NEW_QSTR(MP_QSTR_UART), (mp_obj_t)&pyb_uart_type },
597595
#if MICROPY_HW_ENABLE_CAN
598596
{ MP_OBJ_NEW_QSTR(MP_QSTR_CAN), (mp_obj_t)&pyb_can_type },

stmhal/spi.c

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,29 @@
3737
#include "spi.h"
3838
#include MICROPY_HAL_H
3939

40-
#if !defined(STM32F7)
41-
// The STM32F7 has the SPI pins mapped differently. Need to figure this out
42-
// before enabling SPI for the F7
40+
// The following defines are for compatability with the '405
41+
#if !defined(MICROPY_HW_SPI1_NSS)
42+
// X-skin: X5=PA4=SPI1_NSS, X6=PA5=SPI1_SCK, X7=PA6=SPI1_MISO, X8=PA7=SPI1_MOSI
43+
#define MICROPY_HW_SPI1_NSS (pin_A4)
44+
#define MICROPY_HW_SPI1_SCK (pin_A5)
45+
#define MICROPY_HW_SPI1_MISO (pin_A6)
46+
#define MICROPY_HW_SPI1_MOSI (pin_A7)
47+
#endif
48+
49+
#if !defined(MICROPY_HW_SPI2_NSS)
50+
// Y-skin: Y5=PB12=SPI2_NSS, Y6=PB13=SPI2_SCK, Y7=PB14=SPI2_MISO, Y8=PB15=SPI2_MOSI
51+
#define MICROPY_HW_SPI2_NSS (pin_B12)
52+
#define MICROPY_HW_SPI2_SCK (pin_B13)
53+
#define MICROPY_HW_SPI2_MISO (pin_B14)
54+
#define MICROPY_HW_SPI2_MOSI (pin_B15)
55+
#endif
56+
57+
#if !defined(MICROPY_HW_SPI3_NSS)
58+
#define MICROPY_HW_SPI3_NSS (pin_A4)
59+
#define MICROPY_HW_SPI3_SCK (pin_B3)
60+
#define MICROPY_HW_SPI3_MISO (pin_B4)
61+
#define MICROPY_HW_SPI3_MOSI (pin_B5)
62+
#endif
4363

4464
/// \moduleref pyb
4565
/// \class SPI - a master-driven serial protocol
@@ -140,35 +160,33 @@ void spi_init(SPI_HandleTypeDef *spi, bool enable_nss_pin) {
140160
if (0) {
141161
#if MICROPY_HW_ENABLE_SPI1
142162
} else if (spi->Instance == SPI1) {
143-
// X-skin: X5=PA4=SPI1_NSS, X6=PA5=SPI1_SCK, X7=PA6=SPI1_MISO, X8=PA7=SPI1_MOSI
144163
self = &pyb_spi_obj[0];
145-
pins[0] = &pin_A4;
146-
pins[1] = &pin_A5;
147-
pins[2] = &pin_A6;
148-
pins[3] = &pin_A7;
164+
pins[0] = &MICROPY_HW_SPI1_NSS;
165+
pins[1] = &MICROPY_HW_SPI1_SCK;
166+
pins[2] = &MICROPY_HW_SPI1_MISO;
167+
pins[3] = &MICROPY_HW_SPI1_MOSI;
149168
GPIO_InitStructure.Alternate = GPIO_AF5_SPI1;
150169
// enable the SPI clock
151170
__SPI1_CLK_ENABLE();
152171
#endif
153172
#if MICROPY_HW_ENABLE_SPI2
154173
} else if (spi->Instance == SPI2) {
155-
// Y-skin: Y5=PB12=SPI2_NSS, Y6=PB13=SPI2_SCK, Y7=PB14=SPI2_MISO, Y8=PB15=SPI2_MOSI
156174
self = &pyb_spi_obj[1];
157-
pins[0] = &pin_B12;
158-
pins[1] = &pin_B13;
159-
pins[2] = &pin_B14;
160-
pins[3] = &pin_B15;
175+
pins[0] = &MICROPY_HW_SPI2_NSS;
176+
pins[1] = &MICROPY_HW_SPI2_SCK;
177+
pins[2] = &MICROPY_HW_SPI2_MISO;
178+
pins[3] = &MICROPY_HW_SPI2_MOSI;
161179
GPIO_InitStructure.Alternate = GPIO_AF5_SPI2;
162180
// enable the SPI clock
163181
__SPI2_CLK_ENABLE();
164182
#endif
165183
#if MICROPY_HW_ENABLE_SPI3
166184
} else if (spi->Instance == SPI3) {
167185
self = &pyb_spi_obj[2];
168-
pins[0] = &pin_A4;
169-
pins[1] = &pin_B3;
170-
pins[2] = &pin_B4;
171-
pins[3] = &pin_B5;
186+
pins[0] = &MICROPY_HW_SPI3_NSS;
187+
pins[1] = &MICROPY_HW_SPI3_SCK;
188+
pins[2] = &MICROPY_HW_SPI3_MISO;
189+
pins[3] = &MICROPY_HW_SPI3_MOSI;
172190
GPIO_InitStructure.Alternate = GPIO_AF6_SPI3;
173191
// enable the SPI clock
174192
__SPI3_CLK_ENABLE();
@@ -670,5 +688,3 @@ const mp_obj_type_t pyb_spi_type = {
670688
.make_new = pyb_spi_make_new,
671689
.locals_dict = (mp_obj_t)&pyb_spi_locals_dict,
672690
};
673-
674-
#endif // STM32F7

0 commit comments

Comments
 (0)