From 27a503f63284a1751f20d7f5295690fec5e1c462 Mon Sep 17 00:00:00 2001 From: Rami Ali Date: Mon, 5 Dec 2016 12:21:45 +1100 Subject: [PATCH] stmhal: Refactor UART configuration to use pin objects. This follows the pattern of other peripherals (I2C, SPI) to specify the pins using pin objects instead of a pair of GPIO port and pin number. It makes it easier to customise the UART pins for a particular board. --- stmhal/boards/CERB40/mpconfigboard.h | 34 +++-- stmhal/boards/CERB40/pins.csv | 6 + stmhal/boards/ESPRUINO_PICO/mpconfigboard.h | 16 +-- stmhal/boards/HYDRABUS/mpconfigboard.h | 28 ++--- stmhal/boards/HYDRABUS/pins.csv | 4 + stmhal/boards/LIMIFROG/mpconfigboard.h | 4 +- stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h | 30 ++--- stmhal/boards/NETDUINO_PLUS_2/pins.csv | 7 +- stmhal/boards/NUCLEO_F401RE/mpconfigboard.h | 9 +- stmhal/boards/NUCLEO_F411RE/mpconfigboard.h | 9 +- stmhal/boards/OLIMEX_E407/mpconfigboard.h | 32 +++-- stmhal/boards/PYBLITEV10/mpconfigboard.h | 22 ++-- stmhal/boards/PYBV10/mpconfigboard.h | 36 +++--- stmhal/boards/PYBV11/mpconfigboard.h | 36 +++--- stmhal/boards/PYBV3/mpconfigboard.h | 28 ++--- stmhal/boards/PYBV3/pins.csv | 1 + stmhal/boards/STM32F411DISC/mpconfigboard.h | 16 +-- stmhal/boards/STM32F429DISC/mpconfigboard.h | 8 +- stmhal/boards/STM32F439/mpconfigboard.h | 28 ++--- stmhal/boards/STM32F4DISC/mpconfigboard.h | 32 +++-- stmhal/boards/STM32F7DISC/mpconfigboard.h | 16 +-- stmhal/boards/STM32L476DISC/mpconfigboard.h | 5 +- stmhal/boards/make-pins.py | 6 +- stmhal/uart.c | 119 +++++++----------- 24 files changed, 250 insertions(+), 282 deletions(-) diff --git a/stmhal/boards/CERB40/mpconfigboard.h b/stmhal/boards/CERB40/mpconfigboard.h index 414eceb64a..579ec84337 100644 --- a/stmhal/boards/CERB40/mpconfigboard.h +++ b/stmhal/boards/CERB40/mpconfigboard.h @@ -24,24 +24,22 @@ #define MICROPY_HW_CLK_PLLQ (7) // UART config -#define MICROPY_HW_UART1_PORT (GPIOA) -#define MICROPY_HW_UART1_PINS (GPIO_PIN_9 | GPIO_PIN_10) -#define MICROPY_HW_UART2_PORT (GPIOA) -#define MICROPY_HW_UART2_PINS (GPIO_PIN_2 | GPIO_PIN_3) -#define MICROPY_HW_UART2_RTS (GPIO_PIN_1) -#define MICROPY_HW_UART2_CTS (GPIO_PIN_0) -#define MICROPY_HW_UART3_PORT (GPIOD) -#define MICROPY_HW_UART3_PINS (GPIO_PIN_8 | GPIO_PIN_9) -#define MICROPY_HW_UART3_RTS (GPIO_PIN_12) -#define MICROPY_HW_UART3_CTS (GPIO_PIN_11) -#define MICROPY_HW_UART4_PORT (GPIOA) -#define MICROPY_HW_UART4_PINS (GPIO_PIN_0 | GPIO_PIN_1) -#define MICROPY_HW_UART5_TX_PORT (GPIOC) -#define MICROPY_HW_UART5_TX_PIN (GPIO_PIN_12) -#define MICROPY_HW_UART5_RX_PORT (GPIOD) -#define MICROPY_HW_UART5_RX_PIN (GPIO_PIN_2) -#define MICROPY_HW_UART6_PORT (GPIOC) -#define MICROPY_HW_UART6_PINS (GPIO_PIN_6 | GPIO_PIN_7) +#define MICROPY_HW_UART1_TX (pin_A9) +#define MICROPY_HW_UART1_RX (pin_A10) +#define MICROPY_HW_UART2_TX (pin_A2) +#define MICROPY_HW_UART2_RX (pin_A3) +#define MICROPY_HW_UART2_RTS (pin_A1) +#define MICROPY_HW_UART2_CTS (pin_A0) +#define MICROPY_HW_UART3_TX (pin_D8) +#define MICROPY_HW_UART3_RX (pin_D9) +#define MICROPY_HW_UART3_RTS (pin_D12) +#define MICROPY_HW_UART3_CTS (pin_D11) +#define MICROPY_HW_UART4_TX (pin_A0) +#define MICROPY_HW_UART4_RX (pin_A1) +#define MICROPY_HW_UART5_TX (pin_C12) +#define MICROPY_HW_UART5_RX (pin_D2) +#define MICROPY_HW_UART6_TX (pin_C6) +#define MICROPY_HW_UART6_RX (pin_C7) // I2C busses #define MICROPY_HW_I2C1_SCL (pin_B6) diff --git a/stmhal/boards/CERB40/pins.csv b/stmhal/boards/CERB40/pins.csv index 6df6d61c77..411031e8f5 100644 --- a/stmhal/boards/CERB40/pins.csv +++ b/stmhal/boards/CERB40/pins.csv @@ -38,3 +38,9 @@ JP37,PC7 JP38,PC8 JP39,PC9 JP40,VUSB +UART1_TX,PA9 +UART1_RX,PA10 +UART3_TX,PD8 +UART3_RX,PD9 +UART3_RTS,PD12 +UART3_CTS,PD11 diff --git a/stmhal/boards/ESPRUINO_PICO/mpconfigboard.h b/stmhal/boards/ESPRUINO_PICO/mpconfigboard.h index d18abb908c..6854f4e285 100644 --- a/stmhal/boards/ESPRUINO_PICO/mpconfigboard.h +++ b/stmhal/boards/ESPRUINO_PICO/mpconfigboard.h @@ -28,14 +28,14 @@ #define MICROPY_HW_RTC_USE_LSE (0) // UART config -#define MICROPY_HW_UART1_PORT (GPIOB) -#define MICROPY_HW_UART1_PINS (GPIO_PIN_6 | GPIO_PIN_7) -#define MICROPY_HW_UART2_PORT (GPIOA) -#define MICROPY_HW_UART2_PINS (GPIO_PIN_2 | GPIO_PIN_3) -#define MICROPY_HW_UART2_RTS (GPIO_PIN_1) -#define MICROPY_HW_UART2_CTS (GPIO_PIN_0) -#define MICROPY_HW_UART6_PORT (GPIOA) -#define MICROPY_HW_UART6_PINS (GPIO_PIN_11 | GPIO_PIN_12) +#define MICROPY_HW_UART1_TX (pin_B6) +#define MICROPY_HW_UART1_RX (pin_B7) +#define MICROPY_HW_UART2_TX (pin_A2) +#define MICROPY_HW_UART2_RX (pin_A3) +#define MICROPY_HW_UART2_RTS (pin_A1) +#define MICROPY_HW_UART2_CTS (pin_A0) +#define MICROPY_HW_UART6_TX (pin_A11) +#define MICROPY_HW_UART6_RX (pin_A12) // I2C busses #define MICROPY_HW_I2C1_SCL (pin_B6) diff --git a/stmhal/boards/HYDRABUS/mpconfigboard.h b/stmhal/boards/HYDRABUS/mpconfigboard.h index 06d1692360..38fba9787a 100644 --- a/stmhal/boards/HYDRABUS/mpconfigboard.h +++ b/stmhal/boards/HYDRABUS/mpconfigboard.h @@ -23,20 +23,20 @@ #define MICROPY_HW_CLK_PLLQ (7) // UART config -#define MICROPY_HW_UART1_PORT (GPIOA) -#define MICROPY_HW_UART1_PINS (GPIO_PIN_9 | GPIO_PIN_10) -#define MICROPY_HW_UART2_PORT (GPIOA) -#define MICROPY_HW_UART2_PINS (GPIO_PIN_2 | GPIO_PIN_3) -#define MICROPY_HW_UART2_RTS (GPIO_PIN_1) -#define MICROPY_HW_UART2_CTS (GPIO_PIN_0) -#define MICROPY_HW_UART3_PORT (GPIOD) -#define MICROPY_HW_UART3_PINS (GPIO_PIN_8 | GPIO_PIN_9) -#define MICROPY_HW_UART3_RTS (GPIO_PIN_12) -#define MICROPY_HW_UART3_CTS (GPIO_PIN_11) -#define MICROPY_HW_UART4_PORT (GPIOA) -#define MICROPY_HW_UART4_PINS (GPIO_PIN_0 | GPIO_PIN_1) -#define MICROPY_HW_UART6_PORT (GPIOC) -#define MICROPY_HW_UART6_PINS (GPIO_PIN_6 | GPIO_PIN_7) +#define MICROPY_HW_UART1_TX (pin_A9) +#define MICROPY_HW_UART1_RX (pin_A10) +#define MICROPY_HW_UART2_TX (pin_A2) +#define MICROPY_HW_UART2_RX (pin_A3) +#define MICROPY_HW_UART2_RTS (pin_A1) +#define MICROPY_HW_UART2_CTS (pin_A0) +#define MICROPY_HW_UART3_TX (pin_D8) +#define MICROPY_HW_UART3_RX (pin_D9) +#define MICROPY_HW_UART3_RTS (pin_D12) +#define MICROPY_HW_UART3_CTS (pin_D11) +#define MICROPY_HW_UART4_TX (pin_A0) +#define MICROPY_HW_UART4_RX (pin_A1) +#define MICROPY_HW_UART6_TX (pin_C6) +#define MICROPY_HW_UART6_RX (pin_C7) // I2C busses #define MICROPY_HW_I2C1_SCL (pin_B6) diff --git a/stmhal/boards/HYDRABUS/pins.csv b/stmhal/boards/HYDRABUS/pins.csv index a8d1cf7c0a..47e1f43139 100644 --- a/stmhal/boards/HYDRABUS/pins.csv +++ b/stmhal/boards/HYDRABUS/pins.csv @@ -46,3 +46,7 @@ PA12,PA12 PD2,PD2 BOOT0,BOOT0 PA15,PA15 +UART3_TX,PD8 +UART3_RX,PD9 +UART3_RTS,PD12 +UART3_CTS,PD11 diff --git a/stmhal/boards/LIMIFROG/mpconfigboard.h b/stmhal/boards/LIMIFROG/mpconfigboard.h index 47b8922336..95fd6bf506 100644 --- a/stmhal/boards/LIMIFROG/mpconfigboard.h +++ b/stmhal/boards/LIMIFROG/mpconfigboard.h @@ -29,8 +29,8 @@ void LIMIFROG_board_early_init(void); #define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_4 // USART config -#define MICROPY_HW_UART3_PORT (GPIOC) -#define MICROPY_HW_UART3_PINS (GPIO_PIN_10 | GPIO_PIN_11) +#define MICROPY_HW_UART3_TX (pin_C10) +#define MICROPY_HW_UART3_RX (pin_C11) // I2C busses #define MICROPY_HW_I2C1_SCL (pin_B8) diff --git a/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h b/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h index 3155b63098..9586ae4e55 100644 --- a/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h +++ b/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h @@ -28,20 +28,22 @@ void NETDUINO_PLUS_2_board_early_init(void); #define MICROPY_HW_CLK_PLLQ (7) // UART config -#define MICROPY_HW_UART1_PORT (GPIOA) -#define MICROPY_HW_UART1_PINS (GPIO_PIN_9 | GPIO_PIN_10) -#define MICROPY_HW_UART2_PORT (GPIOA) -#define MICROPY_HW_UART2_PINS (GPIO_PIN_2 | GPIO_PIN_3) -#define MICROPY_HW_UART2_RTS (GPIO_PIN_1) -#define MICROPY_HW_UART2_CTS (GPIO_PIN_0) -#define MICROPY_HW_UART3_PORT (GPIOD) -#define MICROPY_HW_UART3_PINS (GPIO_PIN_8 | GPIO_PIN_9) -#define MICROPY_HW_UART3_RTS (GPIO_PIN_12) -#define MICROPY_HW_UART3_CTS (GPIO_PIN_11) -#define MICROPY_HW_UART4_PORT (GPIOA) -#define MICROPY_HW_UART4_PINS (GPIO_PIN_0 | GPIO_PIN_1) -#define MICROPY_HW_UART6_PORT (GPIOC) -#define MICROPY_HW_UART6_PINS (GPIO_PIN_6 | GPIO_PIN_7) +#define MICROPY_HW_UART1_TX (pin_A9) +#define MICROPY_HW_UART1_RX (pin_A10) +#define MICROPY_HW_UART2_TX (pin_A2) +#define MICROPY_HW_UART2_RX (pin_A3) +#define MICROPY_HW_UART2_RTS (pin_A1) +#define MICROPY_HW_UART2_CTS (pin_A0) +#define MICROPY_HW_UART3_TX (pin_D8) +#define MICROPY_HW_UART3_RX (pin_D9) +#define MICROPY_HW_UART3_RTS (pin_D12) +#define MICROPY_HW_UART3_CTS (pin_D11) +#define MICROPY_HW_UART4_TX (pin_A0) +#define MICROPY_HW_UART4_RX (pin_A1) +#define MICROPY_HW_UART5_TX (pin_C12) +#define MICROPY_HW_UART5_RX (pin_D2) +#define MICROPY_HW_UART6_TX (pin_C6) +#define MICROPY_HW_UART6_RX (pin_C7) // I2C busses #define MICROPY_HW_I2C1_SCL (pin_B6) diff --git a/stmhal/boards/NETDUINO_PLUS_2/pins.csv b/stmhal/boards/NETDUINO_PLUS_2/pins.csv index 7857f4e870..3e71fade6e 100644 --- a/stmhal/boards/NETDUINO_PLUS_2/pins.csv +++ b/stmhal/boards/NETDUINO_PLUS_2/pins.csv @@ -27,4 +27,9 @@ PWR_SD,PB1 PWR_HDR,PB2 PWR_ETH,PC15 RST_ETH,PD2 - +UART1_TX,PA9 +UART3_TX,PD8 +UART3_RX,PD9 +UART3_RTS,PD12 +UART3_CTS,PD11 +UART5_TX,PC12 diff --git a/stmhal/boards/NUCLEO_F401RE/mpconfigboard.h b/stmhal/boards/NUCLEO_F401RE/mpconfigboard.h index 518f65824b..c12d52756c 100644 --- a/stmhal/boards/NUCLEO_F401RE/mpconfigboard.h +++ b/stmhal/boards/NUCLEO_F401RE/mpconfigboard.h @@ -12,11 +12,10 @@ #define MICROPY_HW_CLK_PLLQ (7) // UART config -#define MICROPY_HW_UART2_PORT (GPIOA) -#define MICROPY_HW_UART2_PINS (GPIO_PIN_2 | GPIO_PIN_3) -#define MICROPY_HW_UART6_PORT (GPIOC) -#define MICROPY_HW_UART6_PINS (GPIO_PIN_6 | GPIO_PIN_7) - +#define MICROPY_HW_UART2_TX (pin_A2) +#define MICROPY_HW_UART2_RX (pin_A3) +#define MICROPY_HW_UART6_TX (pin_C6) +#define MICROPY_HW_UART6_RX (pin_C7) // UART 2 connects to the STM32F103 (STLINK) on the Nucleo board // and this is exposed as a USB Serial port. #define MICROPY_HW_UART_REPL PYB_UART_2 diff --git a/stmhal/boards/NUCLEO_F411RE/mpconfigboard.h b/stmhal/boards/NUCLEO_F411RE/mpconfigboard.h index 4dac804931..1f7f0a23b7 100644 --- a/stmhal/boards/NUCLEO_F411RE/mpconfigboard.h +++ b/stmhal/boards/NUCLEO_F411RE/mpconfigboard.h @@ -12,11 +12,10 @@ #define MICROPY_HW_CLK_PLLQ (4) // UART config -#define MICROPY_HW_UART2_PORT (GPIOA) -#define MICROPY_HW_UART2_PINS (GPIO_PIN_2 | GPIO_PIN_3) -#define MICROPY_HW_UART6_PORT (GPIOC) -#define MICROPY_HW_UART6_PINS (GPIO_PIN_6 | GPIO_PIN_7) - +#define MICROPY_HW_UART2_TX (pin_A2) +#define MICROPY_HW_UART2_RX (pin_A3) +#define MICROPY_HW_UART6_TX (pin_C6) +#define MICROPY_HW_UART6_RX (pin_C7) // UART 2 connects to the STM32F103 (STLINK) on the Nucleo board // and this is exposed as a USB Serial port. #define MICROPY_HW_UART_REPL PYB_UART_2 diff --git a/stmhal/boards/OLIMEX_E407/mpconfigboard.h b/stmhal/boards/OLIMEX_E407/mpconfigboard.h index 63ddcfb5d6..5ede682647 100644 --- a/stmhal/boards/OLIMEX_E407/mpconfigboard.h +++ b/stmhal/boards/OLIMEX_E407/mpconfigboard.h @@ -23,29 +23,25 @@ #define MICROPY_HW_CLK_PLLQ (7) // UART config -#define MICROPY_HW_UART1_PORT (GPIOB) -#define MICROPY_HW_UART1_PINS (GPIO_PIN_6 | GPIO_PIN_7) - -#define MICROPY_HW_UART2_PORT (GPIOA) -#define MICROPY_HW_UART2_PINS (GPIO_PIN_2 | GPIO_PIN_3) -#define MICROPY_HW_UART2_RTS (GPIO_PIN_1) -#define MICROPY_HW_UART2_CTS (GPIO_PIN_0) - -#define MICROPY_HW_UART3_PORT (GPIOD) -#define MICROPY_HW_UART3_PINS (GPIO_PIN_8 | GPIO_PIN_9) -#define MICROPY_HW_UART3_RTS (GPIO_PIN_12) -#define MICROPY_HW_UART3_CTS (GPIO_PIN_11) - +#define MICROPY_HW_UART1_TX (pin_B6) +#define MICROPY_HW_UART1_RX (pin_B7) +#define MICROPY_HW_UART2_TX (pin_A2) +#define MICROPY_HW_UART2_RX (pin_A3) +#define MICROPY_HW_UART2_RTS (pin_A1) +#define MICROPY_HW_UART2_CTS (pin_A0) +#define MICROPY_HW_UART3_TX (pin_D8) +#define MICROPY_HW_UART3_RX (pin_D9) +#define MICROPY_HW_UART3_RTS (pin_D12) +#define MICROPY_HW_UART3_CTS (pin_D11) #if MICROPY_HW_HAS_SWITCH == 0 // NOTE: A0 also connects to the user switch. To use UART4 you should // set MICROPY_HW_HAS_SWITCH to 0, and also remove SB20 (on the back // of the board near the USER switch). -#define MICROPY_HW_UART4_PORT (GPIOA) -#define MICROPY_HW_UART4_PINS (GPIO_PIN_0 | GPIO_PIN_1) +#define MICROPY_HW_UART4_TX (pin_A0) +#define MICROPY_HW_UART4_RX (pin_A1) #endif - -#define MICROPY_HW_UART6_PORT (GPIOC) -#define MICROPY_HW_UART6_PINS (GPIO_PIN_6 | GPIO_PIN_7) +#define MICROPY_HW_UART6_TX (pin_C6) +#define MICROPY_HW_UART6_RX (pin_C7) // I2C busses #define MICROPY_HW_I2C1_SCL (pin_B8) diff --git a/stmhal/boards/PYBLITEV10/mpconfigboard.h b/stmhal/boards/PYBLITEV10/mpconfigboard.h index 3147d2d7ab..c952120310 100644 --- a/stmhal/boards/PYBLITEV10/mpconfigboard.h +++ b/stmhal/boards/PYBLITEV10/mpconfigboard.h @@ -28,17 +28,17 @@ #define MICROPY_HW_RTC_USE_CALOUT (1) // UART config -#define MICROPY_HW_UART1_NAME "XB" -#define MICROPY_HW_UART1_PORT (GPIOB) -#define MICROPY_HW_UART1_PINS (GPIO_PIN_6 | GPIO_PIN_7) -#define MICROPY_HW_UART2_NAME "XA" -#define MICROPY_HW_UART2_PORT (GPIOA) -#define MICROPY_HW_UART2_PINS (GPIO_PIN_2 | GPIO_PIN_3) -#define MICROPY_HW_UART2_RTS (GPIO_PIN_1) -#define MICROPY_HW_UART2_CTS (GPIO_PIN_0) -#define MICROPY_HW_UART6_NAME "YA" -#define MICROPY_HW_UART6_PORT (GPIOC) -#define MICROPY_HW_UART6_PINS (GPIO_PIN_6 | GPIO_PIN_7) +#define MICROPY_HW_UART1_NAME "XB" +#define MICROPY_HW_UART1_TX (pin_B6) +#define MICROPY_HW_UART1_RX (pin_B7) +#define MICROPY_HW_UART2_NAME "XA" +#define MICROPY_HW_UART2_TX (pin_A2) +#define MICROPY_HW_UART2_RX (pin_A3) +#define MICROPY_HW_UART2_RTS (pin_A1) +#define MICROPY_HW_UART2_CTS (pin_A0) +#define MICROPY_HW_UART6_NAME "YA" +#define MICROPY_HW_UART6_TX (pin_C6) +#define MICROPY_HW_UART6_RX (pin_C7) // I2C busses #define MICROPY_HW_I2C1_NAME "X" diff --git a/stmhal/boards/PYBV10/mpconfigboard.h b/stmhal/boards/PYBV10/mpconfigboard.h index 2540cc4eb3..4cdbb06568 100644 --- a/stmhal/boards/PYBV10/mpconfigboard.h +++ b/stmhal/boards/PYBV10/mpconfigboard.h @@ -28,24 +28,24 @@ #define MICROPY_HW_RTC_USE_CALOUT (1) // UART config -#define MICROPY_HW_UART1_NAME "XB" -#define MICROPY_HW_UART1_PORT (GPIOB) -#define MICROPY_HW_UART1_PINS (GPIO_PIN_6 | GPIO_PIN_7) -#define MICROPY_HW_UART2_PORT (GPIOA) -#define MICROPY_HW_UART2_PINS (GPIO_PIN_2 | GPIO_PIN_3) -#define MICROPY_HW_UART2_RTS (GPIO_PIN_1) -#define MICROPY_HW_UART2_CTS (GPIO_PIN_0) -#define MICROPY_HW_UART3_NAME "YB" -#define MICROPY_HW_UART3_PORT (GPIOB) -#define MICROPY_HW_UART3_PINS (GPIO_PIN_10 | GPIO_PIN_11) -#define MICROPY_HW_UART3_RTS (GPIO_PIN_14) -#define MICROPY_HW_UART3_CTS (GPIO_PIN_13) -#define MICROPY_HW_UART4_NAME "XA" -#define MICROPY_HW_UART4_PORT (GPIOA) -#define MICROPY_HW_UART4_PINS (GPIO_PIN_0 | GPIO_PIN_1) -#define MICROPY_HW_UART6_NAME "YA" -#define MICROPY_HW_UART6_PORT (GPIOC) -#define MICROPY_HW_UART6_PINS (GPIO_PIN_6 | GPIO_PIN_7) +#define MICROPY_HW_UART1_NAME "XB" +#define MICROPY_HW_UART1_TX (pin_B6) +#define MICROPY_HW_UART1_RX (pin_B7) +#define MICROPY_HW_UART2_TX (pin_A2) +#define MICROPY_HW_UART2_RX (pin_A3) +#define MICROPY_HW_UART2_RTS (pin_A1) +#define MICROPY_HW_UART2_CTS (pin_A0) +#define MICROPY_HW_UART3_NAME "YB" +#define MICROPY_HW_UART3_TX (pin_B10) +#define MICROPY_HW_UART3_RX (pin_B11) +#define MICROPY_HW_UART3_RTS (pin_B14) +#define MICROPY_HW_UART3_CTS (pin_B13) +#define MICROPY_HW_UART4_NAME "XA" +#define MICROPY_HW_UART4_TX (pin_A0) +#define MICROPY_HW_UART4_RX (pin_A1) +#define MICROPY_HW_UART6_NAME "YA" +#define MICROPY_HW_UART6_TX (pin_C6) +#define MICROPY_HW_UART6_RX (pin_C7) // I2C busses #define MICROPY_HW_I2C1_NAME "X" diff --git a/stmhal/boards/PYBV11/mpconfigboard.h b/stmhal/boards/PYBV11/mpconfigboard.h index 881e1ccf02..82bd54f0d3 100644 --- a/stmhal/boards/PYBV11/mpconfigboard.h +++ b/stmhal/boards/PYBV11/mpconfigboard.h @@ -28,24 +28,24 @@ #define MICROPY_HW_RTC_USE_CALOUT (1) // UART config -#define MICROPY_HW_UART1_NAME "XB" -#define MICROPY_HW_UART1_PORT (GPIOB) -#define MICROPY_HW_UART1_PINS (GPIO_PIN_6 | GPIO_PIN_7) -#define MICROPY_HW_UART2_PORT (GPIOA) -#define MICROPY_HW_UART2_PINS (GPIO_PIN_2 | GPIO_PIN_3) -#define MICROPY_HW_UART2_RTS (GPIO_PIN_1) -#define MICROPY_HW_UART2_CTS (GPIO_PIN_0) -#define MICROPY_HW_UART3_NAME "YB" -#define MICROPY_HW_UART3_PORT (GPIOB) -#define MICROPY_HW_UART3_PINS (GPIO_PIN_10 | GPIO_PIN_11) -#define MICROPY_HW_UART3_RTS (GPIO_PIN_14) -#define MICROPY_HW_UART3_CTS (GPIO_PIN_13) -#define MICROPY_HW_UART4_NAME "XA" -#define MICROPY_HW_UART4_PORT (GPIOA) -#define MICROPY_HW_UART4_PINS (GPIO_PIN_0 | GPIO_PIN_1) -#define MICROPY_HW_UART6_NAME "YA" -#define MICROPY_HW_UART6_PORT (GPIOC) -#define MICROPY_HW_UART6_PINS (GPIO_PIN_6 | GPIO_PIN_7) +#define MICROPY_HW_UART1_NAME "XB" +#define MICROPY_HW_UART1_TX (pin_B6) +#define MICROPY_HW_UART1_RX (pin_B7) +#define MICROPY_HW_UART2_TX (pin_A2) +#define MICROPY_HW_UART2_RX (pin_A3) +#define MICROPY_HW_UART2_RTS (pin_A1) +#define MICROPY_HW_UART2_CTS (pin_A0) +#define MICROPY_HW_UART3_NAME "YB" +#define MICROPY_HW_UART3_TX (pin_B10) +#define MICROPY_HW_UART3_RX (pin_B11) +#define MICROPY_HW_UART3_RTS (pin_B14) +#define MICROPY_HW_UART3_CTS (pin_B13) +#define MICROPY_HW_UART4_NAME "XA" +#define MICROPY_HW_UART4_TX (pin_A0) +#define MICROPY_HW_UART4_RX (pin_A1) +#define MICROPY_HW_UART6_NAME "YA" +#define MICROPY_HW_UART6_TX (pin_C6) +#define MICROPY_HW_UART6_RX (pin_C7) // I2C busses #define MICROPY_HW_I2C1_NAME "X" diff --git a/stmhal/boards/PYBV3/mpconfigboard.h b/stmhal/boards/PYBV3/mpconfigboard.h index 48d5549253..1f49af4da1 100644 --- a/stmhal/boards/PYBV3/mpconfigboard.h +++ b/stmhal/boards/PYBV3/mpconfigboard.h @@ -24,20 +24,20 @@ #define MICROPY_HW_RTC_USE_LSE (1) // UART config -#define MICROPY_HW_UART1_PORT (GPIOA) -#define MICROPY_HW_UART1_PINS (GPIO_PIN_9 | GPIO_PIN_10) -#define MICROPY_HW_UART2_PORT (GPIOA) -#define MICROPY_HW_UART2_PINS (GPIO_PIN_2 | GPIO_PIN_3) -#define MICROPY_HW_UART2_RTS (GPIO_PIN_1) -#define MICROPY_HW_UART2_CTS (GPIO_PIN_0) -#define MICROPY_HW_UART3_PORT (GPIOB) -#define MICROPY_HW_UART3_PINS (GPIO_PIN_10 | GPIO_PIN_11) -#define MICROPY_HW_UART3_RTS (GPIO_PIN_14) -#define MICROPY_HW_UART3_CTS (GPIO_PIN_13) -#define MICROPY_HW_UART4_PORT (GPIOA) -#define MICROPY_HW_UART4_PINS (GPIO_PIN_0 | GPIO_PIN_1) -#define MICROPY_HW_UART6_PORT (GPIOC) -#define MICROPY_HW_UART6_PINS (GPIO_PIN_6 | GPIO_PIN_7) +#define MICROPY_HW_UART1_TX (pin_A9) +#define MICROPY_HW_UART1_RX (pin_A10) +#define MICROPY_HW_UART2_TX (pin_A2) +#define MICROPY_HW_UART2_RX (pin_A3) +#define MICROPY_HW_UART2_RTS (pin_A1) +#define MICROPY_HW_UART2_CTS (pin_A0) +#define MICROPY_HW_UART3_TX (pin_B10) +#define MICROPY_HW_UART3_RX (pin_B11) +#define MICROPY_HW_UART3_RTS (pin_B14) +#define MICROPY_HW_UART3_CTS (pin_B13) +#define MICROPY_HW_UART4_TX (pin_A0) +#define MICROPY_HW_UART4_RX (pin_A1) +#define MICROPY_HW_UART6_TX (pin_C6) +#define MICROPY_HW_UART6_RX (pin_C7) // X-skin: X9=PB6=SCL, X10=PB7=SDA #define MICROPY_HW_I2C1_SCL (pin_B6) diff --git a/stmhal/boards/PYBV3/pins.csv b/stmhal/boards/PYBV3/pins.csv index 5be7069888..6172b3bc12 100644 --- a/stmhal/boards/PYBV3/pins.csv +++ b/stmhal/boards/PYBV3/pins.csv @@ -37,3 +37,4 @@ SW,PA13 SD,PC13 MMA_INT,PB2 MMA_AVDD,PB5 +UART1_TX,PA9 diff --git a/stmhal/boards/STM32F411DISC/mpconfigboard.h b/stmhal/boards/STM32F411DISC/mpconfigboard.h index be3589cd9f..31e6d9755b 100644 --- a/stmhal/boards/STM32F411DISC/mpconfigboard.h +++ b/stmhal/boards/STM32F411DISC/mpconfigboard.h @@ -25,14 +25,14 @@ #define MICROPY_HW_RTC_USE_LSE (0) // UART config -#define MICROPY_HW_UART1_PORT (GPIOA) -#define MICROPY_HW_UART1_PINS (GPIO_PIN_9 | GPIO_PIN_10) -#define MICROPY_HW_UART2_PORT (GPIOA) -#define MICROPY_HW_UART2_PINS (GPIO_PIN_2 | GPIO_PIN_3) -#define MICROPY_HW_UART2_RTS (GPIO_PIN_1) -#define MICROPY_HW_UART2_CTS (GPIO_PIN_0) -#define MICROPY_HW_UART6_PORT (GPIOC) -#define MICROPY_HW_UART6_PINS (GPIO_PIN_6 | GPIO_PIN_7) +#define MICROPY_HW_UART1_TX (pin_A9) +#define MICROPY_HW_UART1_RX (pin_A10) +#define MICROPY_HW_UART2_TX (pin_A2) +#define MICROPY_HW_UART2_RX (pin_A3) +#define MICROPY_HW_UART2_RTS (pin_A1) +#define MICROPY_HW_UART2_CTS (pin_A0) +#define MICROPY_HW_UART6_TX (pin_C6) +#define MICROPY_HW_UART6_RX (pin_C7) // I2C busses #define MICROPY_HW_I2C1_SCL (pin_B6) diff --git a/stmhal/boards/STM32F429DISC/mpconfigboard.h b/stmhal/boards/STM32F429DISC/mpconfigboard.h index 63f22e02c2..a8e379020c 100644 --- a/stmhal/boards/STM32F429DISC/mpconfigboard.h +++ b/stmhal/boards/STM32F429DISC/mpconfigboard.h @@ -23,10 +23,10 @@ #define MICROPY_HW_CLK_PLLQ (7) // UART config -#define MICROPY_HW_UART1_PORT (GPIOA) -#define MICROPY_HW_UART1_PINS (GPIO_PIN_9 | GPIO_PIN_10) -#define MICROPY_HW_UART2_PORT (GPIOD) -#define MICROPY_HW_UART2_PINS (GPIO_PIN_8 | GPIO_PIN_9) +#define MICROPY_HW_UART1_TX (pin_A9) +#define MICROPY_HW_UART1_RX (pin_A10) +#define MICROPY_HW_UART2_TX (pin_D8) +#define MICROPY_HW_UART2_RX (pin_D9) // I2C busses #define MICROPY_HW_I2C3_SCL (pin_A8) diff --git a/stmhal/boards/STM32F439/mpconfigboard.h b/stmhal/boards/STM32F439/mpconfigboard.h index ee06b3aa43..eca79bf582 100644 --- a/stmhal/boards/STM32F439/mpconfigboard.h +++ b/stmhal/boards/STM32F439/mpconfigboard.h @@ -28,20 +28,20 @@ #define MICROPY_HW_CLK_PLLQ (8) //divide core clock by this to get 48MHz // UART config -#define MICROPY_HW_UART1_PORT (GPIOA) -#define MICROPY_HW_UART1_PINS (GPIO_PIN_9 | GPIO_PIN_10) -#define MICROPY_HW_UART2_PORT (GPIOD) -#define MICROPY_HW_UART2_PINS (GPIO_PIN_5 | GPIO_PIN_6) -#define MICROPY_HW_UART2_RTS (GPIO_PIN_1) -#define MICROPY_HW_UART2_CTS (GPIO_PIN_0) -#define MICROPY_HW_UART3_PORT (GPIOD) -#define MICROPY_HW_UART3_PINS (GPIO_PIN_8 | GPIO_PIN_9) -#define MICROPY_HW_UART3_RTS (GPIO_PIN_12) -#define MICROPY_HW_UART3_CTS (GPIO_PIN_11) -#define MICROPY_HW_UART4_PORT (GPIOA) -#define MICROPY_HW_UART4_PINS (GPIO_PIN_0 | GPIO_PIN_1) -#define MICROPY_HW_UART6_PORT (GPIOC) -#define MICROPY_HW_UART6_PINS (GPIO_PIN_6 | GPIO_PIN_7) +#define MICROPY_HW_UART1_TX (pin_A9) +#define MICROPY_HW_UART1_RX (pin_A10) +#define MICROPY_HW_UART2_TX (pin_D5) +#define MICROPY_HW_UART2_RX (pin_D6) +#define MICROPY_HW_UART2_RTS (pin_D1) +#define MICROPY_HW_UART2_CTS (pin_D0) +#define MICROPY_HW_UART3_TX (pin_D8) +#define MICROPY_HW_UART3_RX (pin_D9) +#define MICROPY_HW_UART3_RTS (pin_D12) +#define MICROPY_HW_UART3_CTS (pin_D11) +#define MICROPY_HW_UART4_TX (pin_A0) +#define MICROPY_HW_UART4_RX (pin_A1) +#define MICROPY_HW_UART6_TX (pin_C6) +#define MICROPY_HW_UART6_RX (pin_C7) // I2C busses #define MICROPY_HW_I2C1_SCL (pin_A8) diff --git a/stmhal/boards/STM32F4DISC/mpconfigboard.h b/stmhal/boards/STM32F4DISC/mpconfigboard.h index 841bf7d15b..1058755158 100644 --- a/stmhal/boards/STM32F4DISC/mpconfigboard.h +++ b/stmhal/boards/STM32F4DISC/mpconfigboard.h @@ -27,33 +27,29 @@ // A9 is used for USB VBUS detect, and A10 is used for USB_FS_ID. // UART1 is also on PB6/7 but PB6 is tied to the Audio SCL line. // Without board modifications, this makes UART1 unusable on this board. -#define MICROPY_HW_UART1_PORT (GPIOA) -#define MICROPY_HW_UART1_PINS (GPIO_PIN_9 | GPIO_PIN_10) +#define MICROPY_HW_UART1_TX (pin_A9) +#define MICROPY_HW_UART1_RX (pin_A10) #endif - -#define MICROPY_HW_UART2_PORT (GPIOA) -#define MICROPY_HW_UART2_PINS (GPIO_PIN_2 | GPIO_PIN_3) -#define MICROPY_HW_UART2_RTS (GPIO_PIN_1) -#define MICROPY_HW_UART2_CTS (GPIO_PIN_0) - -#define MICROPY_HW_UART3_PORT (GPIOD) -#define MICROPY_HW_UART3_PINS (GPIO_PIN_8 | GPIO_PIN_9) -#define MICROPY_HW_UART3_RTS (GPIO_PIN_12) -#define MICROPY_HW_UART3_CTS (GPIO_PIN_11) - +#define MICROPY_HW_UART2_TX (pin_A2) +#define MICROPY_HW_UART2_RX (pin_A3) +#define MICROPY_HW_UART2_RTS (pin_A1) +#define MICROPY_HW_UART2_CTS (pin_A0) +#define MICROPY_HW_UART3_TX (pin_D8) +#define MICROPY_HW_UART3_RX (pin_D9) +#define MICROPY_HW_UART3_RTS (pin_D12) +#define MICROPY_HW_UART3_CTS (pin_D11) #if MICROPY_HW_HAS_SWITCH == 0 // NOTE: A0 also connects to the user switch. To use UART4 you should // set MICROPY_HW_HAS_SWITCH to 0, and also remove SB20 (on the back // of the board near the USER switch). -#define MICROPY_HW_UART4_PORT (GPIOA) -#define MICROPY_HW_UART4_PINS (GPIO_PIN_0 | GPIO_PIN_1) +#define MICROPY_HW_UART4_TX (pin_A0) +#define MICROPY_HW_UART4_RX (pin_A1) #endif - // NOTE: PC7 is connected to MCLK on the Audio chip. This is an input signal // so I think as long as you're not using the audio chip then it should // be fine to use as a UART pin. -#define MICROPY_HW_UART6_PORT (GPIOC) -#define MICROPY_HW_UART6_PINS (GPIO_PIN_6 | GPIO_PIN_7) +#define MICROPY_HW_UART6_TX (pin_C6) +#define MICROPY_HW_UART6_RX (pin_C7) // I2C busses #define MICROPY_HW_I2C1_SCL (pin_B6) diff --git a/stmhal/boards/STM32F7DISC/mpconfigboard.h b/stmhal/boards/STM32F7DISC/mpconfigboard.h index acdc4d0055..38665055f1 100644 --- a/stmhal/boards/STM32F7DISC/mpconfigboard.h +++ b/stmhal/boards/STM32F7DISC/mpconfigboard.h @@ -35,16 +35,12 @@ void STM32F7DISC_board_early_init(void); #define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_7 // 210-216 MHz needs 7 wait states // UART config -#define MICROPY_HW_UART1_TX_PORT (GPIOA) -#define MICROPY_HW_UART1_TX_PIN (GPIO_PIN_9) -#define MICROPY_HW_UART1_RX_PORT (GPIOB) -#define MICROPY_HW_UART1_RX_PIN (GPIO_PIN_7) - -#define MICROPY_HW_UART6_PORT (GPIOC) -#define MICROPY_HW_UART6_PINS (GPIO_PIN_6 | GPIO_PIN_7) -#define MICROPY_HW_UART7_PORT (GPIOF) -#define MICROPY_HW_UART7_PINS (GPIO_PIN_6 | GPIO_PIN_7) - +#define MICROPY_HW_UART1_TX (pin_A9) +#define MICROPY_HW_UART1_RX (pin_B7) +#define MICROPY_HW_UART6_TX (pin_C6) +#define MICROPY_HW_UART6_RX (pin_C7) +#define MICROPY_HW_UART7_TX (pin_F6) +#define MICROPY_HW_UART7_RX (pin_F7) #define MICROPY_HW_UART_REPL PYB_UART_1 #define MICROPY_HW_UART_REPL_BAUD 115200 diff --git a/stmhal/boards/STM32L476DISC/mpconfigboard.h b/stmhal/boards/STM32L476DISC/mpconfigboard.h index 55e7c9b563..2be5c9de65 100644 --- a/stmhal/boards/STM32L476DISC/mpconfigboard.h +++ b/stmhal/boards/STM32L476DISC/mpconfigboard.h @@ -26,9 +26,8 @@ #define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_4 // USART config -#define MICROPY_HW_UART2_PORT (GPIOD) -#define MICROPY_HW_UART2_PINS (GPIO_PIN_5 | GPIO_PIN_6) - +#define MICROPY_HW_UART2_TX (pin_D5) +#define MICROPY_HW_UART2_RX (pin_D6) // USART 2 is connected to the virtual com port on the ST-LINK #define MICROPY_HW_UART_REPL PYB_UART_2 #define MICROPY_HW_UART_REPL_BAUD 115200 diff --git a/stmhal/boards/make-pins.py b/stmhal/boards/make-pins.py index ae614877d5..b33c92edc3 100755 --- a/stmhal/boards/make-pins.py +++ b/stmhal/boards/make-pins.py @@ -21,10 +21,8 @@ CONDITIONAL_VAR = { 'I2C' : 'MICROPY_HW_I2C{num}_SCL', 'I2S' : 'MICROPY_HW_ENABLE_I2S{num}', 'SPI' : 'MICROPY_HW_SPI{num}_SCK', - 'UART' : 'MICROPY_HW_UART{num}_PORT', - 'UART5' : 'MICROPY_HW_UART5_TX_PORT', - 'USART' : 'MICROPY_HW_UART{num}_PORT', - 'USART1': 'MICROPY_HW_UART1_TX_PORT', + 'UART' : 'MICROPY_HW_UART{num}_TX', + 'USART' : 'MICROPY_HW_UART{num}_TX', } def parse_port_pin(name_str): diff --git a/stmhal/uart.c b/stmhal/uart.c index b16cf3481c..ec40592a2d 100644 --- a/stmhal/uart.c +++ b/stmhal/uart.c @@ -35,6 +35,7 @@ #include "py/mphal.h" #include "uart.h" #include "irq.h" +#include "genhdr/pins.h" //TODO: Add UART7/8 support for MCU_SERIES_F7 @@ -114,120 +115,93 @@ void uart_deinit(void) { STATIC bool uart_init2(pyb_uart_obj_t *uart_obj) { USART_TypeDef *UARTx; IRQn_Type irqn; - uint32_t GPIO_Pin, GPIO_Pin2 = 0; - uint8_t GPIO_AF_UARTx = 0; - GPIO_TypeDef* GPIO_Port = NULL; - GPIO_TypeDef* GPIO_Port2 = NULL; + int uart_unit; + + const pin_obj_t *pins[4] = {0}; switch (uart_obj->uart_id) { - #if defined(MICROPY_HW_UART1_PORT) && defined(MICROPY_HW_UART1_PINS) - // USART1 is on PA9/PA10 (CK on PA8), PB6/PB7 + #if defined(MICROPY_HW_UART1_TX) && defined(MICROPY_HW_UART1_RX) case PYB_UART_1: + uart_unit = 1; UARTx = USART1; irqn = USART1_IRQn; - GPIO_AF_UARTx = GPIO_AF7_USART1; - GPIO_Port = MICROPY_HW_UART1_PORT; - GPIO_Pin = MICROPY_HW_UART1_PINS; + pins[0] = &MICROPY_HW_UART1_TX; + pins[1] = &MICROPY_HW_UART1_RX; __USART1_CLK_ENABLE(); break; #endif - #if defined(MICROPY_HW_UART1_TX_PORT) && \ - defined(MICROPY_HW_UART1_TX_PIN) && \ - defined(MICROPY_HW_UART1_RX_PORT) && \ - defined(MICROPY_HW_UART1_RX_PIN) - case PYB_UART_1: - UARTx = USART1; - irqn = USART1_IRQn; - GPIO_AF_UARTx = GPIO_AF7_USART1; - GPIO_Port = MICROPY_HW_UART1_TX_PORT; - GPIO_Pin = MICROPY_HW_UART1_TX_PIN; - GPIO_Port2 = MICROPY_HW_UART1_RX_PORT; - GPIO_Pin2 = MICROPY_HW_UART1_RX_PIN; - __USART1_CLK_ENABLE(); - break; - #endif - - #if defined(MICROPY_HW_UART2_PORT) && defined(MICROPY_HW_UART2_PINS) + #if defined(MICROPY_HW_UART2_TX) && defined(MICROPY_HW_UART2_RX) case PYB_UART_2: + uart_unit = 2; UARTx = USART2; irqn = USART2_IRQn; - GPIO_AF_UARTx = GPIO_AF7_USART2; - GPIO_Port = MICROPY_HW_UART2_PORT; - GPIO_Pin = MICROPY_HW_UART2_PINS; + pins[0] = &MICROPY_HW_UART2_TX; + pins[1] = &MICROPY_HW_UART2_RX; #if defined(MICROPY_HW_UART2_RTS) if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_RTS) { - GPIO_Pin |= MICROPY_HW_UART2_RTS; + pins[2] = &MICROPY_HW_UART2_RTS; } #endif #if defined(MICROPY_HW_UART2_CTS) if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_CTS) { - GPIO_Pin |= MICROPY_HW_UART2_CTS; + pins[3] = &MICROPY_HW_UART2_CTS; } #endif __USART2_CLK_ENABLE(); break; #endif - #if defined(USART3) && defined(MICROPY_HW_UART3_PORT) && defined(MICROPY_HW_UART3_PINS) - // USART3 is on PB10/PB11 (CK,CTS,RTS on PB12,PB13,PB14), PC10/PC11 (CK on PC12), PD8/PD9 (CK on PD10) + #if defined(MICROPY_HW_UART3_TX) && defined(MICROPY_HW_UART3_RX) case PYB_UART_3: + uart_unit = 3; UARTx = USART3; irqn = USART3_IRQn; - GPIO_AF_UARTx = GPIO_AF7_USART3; - GPIO_Port = MICROPY_HW_UART3_PORT; - GPIO_Pin = MICROPY_HW_UART3_PINS; + pins[0] = &MICROPY_HW_UART3_TX; + pins[1] = &MICROPY_HW_UART3_RX; #if defined(MICROPY_HW_UART3_RTS) if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_RTS) { - GPIO_Pin |= MICROPY_HW_UART3_RTS; + pins[2] = &MICROPY_HW_UART3_RTS; } #endif #if defined(MICROPY_HW_UART3_CTS) if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_CTS) { - GPIO_Pin |= MICROPY_HW_UART3_CTS; + pins[3] = &MICROPY_HW_UART3_CTS; } #endif __USART3_CLK_ENABLE(); break; #endif - #if defined(UART4) && defined(MICROPY_HW_UART4_PORT) && defined(MICROPY_HW_UART4_PINS) - // UART4 is on PA0/PA1, PC10/PC11 + #if defined(MICROPY_HW_UART4_TX) && defined(MICROPY_HW_UART4_RX) case PYB_UART_4: + uart_unit = 4; UARTx = UART4; irqn = UART4_IRQn; - GPIO_AF_UARTx = GPIO_AF8_UART4; - GPIO_Port = MICROPY_HW_UART4_PORT; - GPIO_Pin = MICROPY_HW_UART4_PINS; + pins[0] = &MICROPY_HW_UART4_TX; + pins[1] = &MICROPY_HW_UART4_RX; __UART4_CLK_ENABLE(); break; #endif - #if defined(UART5) && \ - defined(MICROPY_HW_UART5_TX_PORT) && \ - defined(MICROPY_HW_UART5_TX_PIN) && \ - defined(MICROPY_HW_UART5_RX_PORT) && \ - defined(MICROPY_HW_UART5_RX_PIN) + #if defined(MICROPY_HW_UART5_TX) && defined(MICROPY_HW_UART5_RX) case PYB_UART_5: + uart_unit = 5; UARTx = UART5; irqn = UART5_IRQn; - GPIO_AF_UARTx = GPIO_AF8_UART5; - GPIO_Port = MICROPY_HW_UART5_TX_PORT; - GPIO_Port2 = MICROPY_HW_UART5_RX_PORT; - GPIO_Pin = MICROPY_HW_UART5_TX_PIN; - GPIO_Pin2 = MICROPY_HW_UART5_RX_PIN; + pins[0] = &MICROPY_HW_UART5_TX; + pins[1] = &MICROPY_HW_UART5_RX; __UART5_CLK_ENABLE(); break; #endif - #if defined(MICROPY_HW_UART6_PORT) && defined(MICROPY_HW_UART6_PINS) - // USART6 is on PC6/PC7 (CK on PC8) + #if defined(MICROPY_HW_UART6_TX) && defined(MICROPY_HW_UART6_RX) case PYB_UART_6: + uart_unit = 6; UARTx = USART6; irqn = USART6_IRQn; - GPIO_AF_UARTx = GPIO_AF8_USART6; - GPIO_Port = MICROPY_HW_UART6_PORT; - GPIO_Pin = MICROPY_HW_UART6_PINS; + pins[0] = &MICROPY_HW_UART6_TX; + pins[1] = &MICROPY_HW_UART6_RX; __USART6_CLK_ENABLE(); break; #endif @@ -237,26 +211,21 @@ STATIC bool uart_init2(pyb_uart_obj_t *uart_obj) { return false; } + uint32_t mode = MP_HAL_PIN_MODE_ALT; + uint32_t pull = MP_HAL_PIN_PULL_UP; + + for (uint i = 0; i < 4; i++) { + if (pins[i] != NULL) { + bool ret = mp_hal_pin_config_alt(pins[i], mode, pull, AF_FN_UART, uart_unit); + if (!ret) { + return false; + } + } + } + uart_obj->irqn = irqn; uart_obj->uart.Instance = UARTx; - // init GPIO - mp_hal_gpio_clock_enable(GPIO_Port); - GPIO_InitTypeDef GPIO_InitStructure; - GPIO_InitStructure.Pin = GPIO_Pin; - GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; - GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; - GPIO_InitStructure.Pull = GPIO_PULLUP; - GPIO_InitStructure.Alternate = GPIO_AF_UARTx; - HAL_GPIO_Init(GPIO_Port, &GPIO_InitStructure); - - // init GPIO for second pin if needed - if (GPIO_Port2 != NULL) { - mp_hal_gpio_clock_enable(GPIO_Port2); - GPIO_InitStructure.Pin = GPIO_Pin2; - HAL_GPIO_Init(GPIO_Port2, &GPIO_InitStructure); - } - // init UARTx HAL_UART_Init(&uart_obj->uart);