From 51901f7de02675d3cae0ba57ece7b464f9479767 Mon Sep 17 00:00:00 2001 From: Hierophect Date: Fri, 4 Oct 2019 11:42:38 -0400 Subject: [PATCH] Add peripheral definitions --- ports/stm32f4/common-hal/busio/UART.c | 23 ++++++++++++- ports/stm32f4/common-hal/busio/UART.h | 4 +-- ports/stm32f4/peripherals/stm32f4/periph.h | 31 ++++++++++++++--- .../peripherals/stm32f4/stm32f405xx/periph.c | 34 +++++++++++++++++++ .../peripherals/stm32f4/stm32f405xx/periph.h | 7 ++++ 5 files changed, 91 insertions(+), 8 deletions(-) diff --git a/ports/stm32f4/common-hal/busio/UART.c b/ports/stm32f4/common-hal/busio/UART.c index e1810b131e..d353592bf4 100644 --- a/ports/stm32f4/common-hal/busio/UART.c +++ b/ports/stm32f4/common-hal/busio/UART.c @@ -41,7 +41,28 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self, const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate, uint8_t bits, uart_parity_t parity, uint8_t stop, mp_float_t timeout, uint16_t receiver_buffer_size) { - mp_raise_NotImplementedError(translate("UART not yet supported")); + + GPIO_InitStruct.Pin = pin_mask(10)|pin_mask(11); + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF7_USART3; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + __HAL_RCC_USART2_CLK_ENABLE(); + + huart2.Instance = USART2; + huart2.Init.BaudRate = 115200; + huart2.Init.WordLength = UART_WORDLENGTH_8B; + huart2.Init.StopBits = UART_STOPBITS_1; + huart2.Init.Parity = UART_PARITY_NONE; + huart2.Init.Mode = UART_MODE_TX_RX; + huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart2.Init.OverSampling = UART_OVERSAMPLING_16; + if (HAL_UART_Init(&huart2) != HAL_OK) + { + mp_raise_NotImplementedError(translate("UART explode")); + } } bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) { diff --git a/ports/stm32f4/common-hal/busio/UART.h b/ports/stm32f4/common-hal/busio/UART.h index 6992339340..8a4b554c4a 100644 --- a/ports/stm32f4/common-hal/busio/UART.h +++ b/ports/stm32f4/common-hal/busio/UART.h @@ -33,8 +33,8 @@ typedef struct { mp_obj_base_t base; - uint8_t rx_pin; - uint8_t tx_pin; + const mcu_uart_tx_obj_t *tx; + const mcu_uart_rx_obj_t *rx; uint8_t character_bits; bool rx_error; uint32_t baudrate; diff --git a/ports/stm32f4/peripherals/stm32f4/periph.h b/ports/stm32f4/peripherals/stm32f4/periph.h index 06ab2d3e4f..c24d205862 100644 --- a/ports/stm32f4/peripherals/stm32f4/periph.h +++ b/ports/stm32f4/peripherals/stm32f4/periph.h @@ -34,8 +34,8 @@ #include "stm32f4/pins.h" // I2C -//TODO: these objects should be condensed into a single 'periph_pin' unless we -//find a compelling reason to store more unique data in them. +// TODO: these objects should be condensed into a single 'periph_pin' unless we +// find a compelling reason to store more unique data in them. typedef struct { uint8_t i2c_index:4; // Index of the I2C unit (1 to 3) @@ -65,8 +65,8 @@ typedef struct { } // SPI -//TODO: these objects should be condensed into a single 'periph_pin' unless we -//find a compelling reason to store more unique data in them. +// TODO: these objects should be condensed into a single 'periph_pin' unless we +// find a compelling reason to store more unique data in them. typedef struct { uint8_t spi_index:4; //Up to 6 SPI units @@ -99,7 +99,28 @@ typedef struct { .pin = spi_pin, \ } -// TODO: SPI, UART, etc +// UART +// TODO: these objects should be condensed into a single 'periph_pin' unless we +// find a compelling reason to store more unique data in them. + +typedef struct { + uint8_t uart_index:4; + uint8_t altfn_index:4; + const mcu_pin_obj_t * pin; +} mcu_uart_tx_obj_t; + +typedef struct { + uint8_t uart_index:4; + uint8_t altfn_index:4; + const mcu_pin_obj_t * pin; +} mcu_uart_rx_obj_t; + +#define UART(index, alt, uart_pin) \ +{ \ + .uart_index = index, \ + .altfn_index = alt, \ + .pin = uart_pin, \ +} // Choose based on chip #ifdef STM32F412Zx diff --git a/ports/stm32f4/peripherals/stm32f4/stm32f405xx/periph.c b/ports/stm32f4/peripherals/stm32f4/stm32f405xx/periph.c index 7b0a54e55e..c1c78b3bf8 100644 --- a/ports/stm32f4/peripherals/stm32f4/stm32f405xx/periph.c +++ b/ports/stm32f4/peripherals/stm32f4/stm32f405xx/periph.c @@ -85,4 +85,38 @@ const mcu_spi_nss_obj_t mcu_spi_nss_list[6] = { SPI(3, 6, &pin_PA04), SPI(3, 6, &pin_PA15), }; + +USART_TypeDef * mcu_uart_banks[6] = {USART1, USART2, USART3, UART4, UART5, USART6}; +bool mcu_uart_has_usart[6] = {true, true, true, false, false, true}; + +const mcu_uart_tx_obj_t mcu_uart_tx_list[12] = { + UART(4, 8, &pin_PA00), + UART(2, 7, &pin_PA02), + UART(1, 7, &pin_PA09), + UART(1, 7, &pin_PB06), + UART(3, 7, &pin_PB10), + UART(6, 8, &pin_PC06), + UART(3, 7, &pin_PC10), + UART(4, 8, &pin_PC10), + UART(5, 8, &pin_PC12), + UART(2, 7, &pin_PD05), + UART(3, 7, &pin_PD08), + UART(6, 8, &pin_PG14), +}; + +const mcu_uart_rx_obj_t mcu_uart_rx_list[12] = { + UART(4, 8, &pin_PA01), + UART(2, 7, &pin_PA03), + UART(1, 7, &pin_PA10), + UART(1, 7, &pin_PB07), + UART(3, 7, &pin_PB11), + UART(6, 8, &pin_PC07), + UART(3, 7, &pin_PC11), + UART(4, 8, &pin_PC11), + UART(5, 8, &pin_PD02), + UART(2, 7, &pin_PD06), + UART(3, 7, &pin_PD09), + UART(6, 8, &pin_PG09), +}; + //UART, Etc diff --git a/ports/stm32f4/peripherals/stm32f4/stm32f405xx/periph.h b/ports/stm32f4/peripherals/stm32f4/stm32f405xx/periph.h index cb9b33f6eb..6737720266 100644 --- a/ports/stm32f4/peripherals/stm32f4/stm32f405xx/periph.h +++ b/ports/stm32f4/peripherals/stm32f4/stm32f405xx/periph.h @@ -41,5 +41,12 @@ extern const mcu_spi_mosi_obj_t mcu_spi_mosi_list[6]; extern const mcu_spi_miso_obj_t mcu_spi_miso_list[6]; extern const mcu_spi_nss_obj_t mcu_spi_nss_list[6]; +//UART +extern USART_TypeDef * mcu_uart_banks[6]; +bool mcu_uart_has_usart[6] + +extern const mcu_uart_tx_obj_t mcu_uart_tx_list[12]; +extern const mcu_uart_rx_obj_t mcu_uart_rx_list[12]; + #endif // MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F405XX_PERIPH_H \ No newline at end of file