From 0b85172ba6358a1b57191cef52b051ba9fa4919b Mon Sep 17 00:00:00 2001 From: Hierophect Date: Fri, 4 Oct 2019 14:37:18 -0400 Subject: [PATCH] WIP --- ports/stm32f4/Makefile | 1 + .../boards/feather_f405/stm32f4xx_hal_conf.h | 2 +- ports/stm32f4/common-hal/busio/UART.c | 163 ++++++++++++++++-- ports/stm32f4/common-hal/busio/UART.h | 4 + .../peripherals/stm32f4/stm32f405xx/periph.h | 2 +- ports/stm32f4/supervisor/port.c | 2 + 6 files changed, 158 insertions(+), 16 deletions(-) diff --git a/ports/stm32f4/Makefile b/ports/stm32f4/Makefile index bef26d662a..50d0e980f4 100755 --- a/ports/stm32f4/Makefile +++ b/ports/stm32f4/Makefile @@ -152,6 +152,7 @@ SRC_STM32 = \ stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_utils.c \ stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_exti.c \ stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c \ + stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_usart.c \ stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c \ stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c \ stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c \ diff --git a/ports/stm32f4/boards/feather_f405/stm32f4xx_hal_conf.h b/ports/stm32f4/boards/feather_f405/stm32f4xx_hal_conf.h index 019b03ca7a..04cb75f2b5 100644 --- a/ports/stm32f4/boards/feather_f405/stm32f4xx_hal_conf.h +++ b/ports/stm32f4/boards/feather_f405/stm32f4xx_hal_conf.h @@ -63,7 +63,7 @@ #define HAL_SPI_MODULE_ENABLED /* #define HAL_TIM_MODULE_ENABLED */ #define HAL_UART_MODULE_ENABLED -/* #define HAL_USART_MODULE_ENABLED */ +#define HAL_USART_MODULE_ENABLED /* #define HAL_IRDA_MODULE_ENABLED */ /* #define HAL_SMARTCARD_MODULE_ENABLED */ /* #define HAL_WWDG_MODULE_ENABLED */ diff --git a/ports/stm32f4/common-hal/busio/UART.c b/ports/stm32f4/common-hal/busio/UART.c index d353592bf4..aef2f62631 100644 --- a/ports/stm32f4/common-hal/busio/UART.c +++ b/ports/stm32f4/common-hal/busio/UART.c @@ -35,34 +35,157 @@ #include "py/stream.h" #include "supervisor/shared/translate.h" +#include "common-hal/microcontroller/Pin.h" +#include "stm32f4xx_hal.h" + #include "tick.h" +STATIC bool reserved_uart[10]; + +void uart_reset(void) { + //ugh. reduce this + #ifdef USART1 + reserved_uart[0] = false; + __HAL_RCC_USART1_CLK_DISABLE(); + #endif + #ifdef USART2 + reserved_uart[1] = false; + __HAL_RCC_USART2_CLK_DISABLE(); + #endif + #ifdef USART3 + reserved_uart[2] = false; + __HAL_RCC_USART3_CLK_DISABLE(); + #endif + #ifdef UART4 + reserved_uart[3] = false; + __HAL_RCC_UART4_CLK_DISABLE(); + #endif + #ifdef UART5 + reserved_uart[4] = false; + __HAL_RCC_UART5_CLK_DISABLE(); + #endif + #ifdef USART6 + reserved_uart[5] = false; + __HAL_RCC_USART6_CLK_DISABLE(); + #endif +} + 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) { - GPIO_InitStruct.Pin = pin_mask(10)|pin_mask(11); + //match pins to UART objects + USART_TypeDef * USARTx; + + uint8_t tx_len = sizeof(mcu_uart_tx_list)/sizeof(*mcu_uart_tx_list); + uint8_t rx_len = sizeof(mcu_uart_rx_list)/sizeof(*mcu_uart_rx_list); + + bool uart_taken = false; + //tx + for(uint i=0; itx = &mcu_uart_tx_list[i]; + self->rx = &mcu_uart_rx_list[j]; + break; + } + } + } + } + + //handle typedef selection, errors + if(self->tx!=NULL && self->rx!=NULL) { + USARTx = mcu_uart_banks[self->tx->uart_index-1]; + } else { + if (uart_taken) { + mp_raise_ValueError(translate("Hardware busy, try alternative pins")); + } else { + mp_raise_ValueError(translate("Invalid UART pin selection")); + } + } + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + GPIO_InitStruct.Pin = pin_mask(tx->number); 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); + GPIO_InitStruct.Alternate = self->tx->altfn_index; + HAL_GPIO_Init(pin_port(tx->port), &GPIO_InitStruct); - __HAL_RCC_USART2_CLK_ENABLE(); + GPIO_InitStruct.Pin = pin_mask(rx->number); + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = self->rx->altfn_index; + HAL_GPIO_Init(pin_port(rx->port), &GPIO_InitStruct); - 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) + #ifdef USART1 + if(USARTx==USART1) { + reserved_uart[0] = true; + __HAL_RCC_USART1_CLK_ENABLE(); + mp_printf(&mp_plat_print, "USART1"); + } + #endif + #ifdef UART2 + if(USARTx==USART2) { + reserved_uart[1] = true; + __HAL_RCC_USART2_CLK_ENABLE(); + mp_printf(&mp_plat_print, "USART2"); + } + #endif + #ifdef USART3 + if(USARTx==USART3) { + reserved_uart[2] = true; + __HAL_RCC_USART3_CLK_ENABLE(); + mp_printf(&mp_plat_print, "USART3"); + } + #endif + #ifdef UART4 + if(USARTx==UART4) { + reserved_uart[3] = true; + __HAL_RCC_UART4_CLK_ENABLE(); + mp_printf(&mp_plat_print, "USART4"); + } + #endif + #ifdef UART5 + if(USARTx==UART5) { + reserved_uart[4] = true; + __HAL_RCC_UART5_CLK_ENABLE(); + mp_printf(&mp_plat_print, "USART5"); + } + #endif + #ifdef USART6 + if(USARTx==USART6) { + reserved_uart[5] = true; + __HAL_RCC_USART6_CLK_ENABLE(); + mp_printf(&mp_plat_print, "USART6"); + } + #endif + + self->handle.Instance = USARTx; + self->handle.Init.BaudRate = 115200; + self->handle.Init.WordLength = UART_WORDLENGTH_8B; + self->handle.Init.StopBits = UART_STOPBITS_1; + self->handle.Init.Parity = UART_PARITY_NONE; + self->handle.Init.Mode = UART_MODE_TX_RX; + self->handle.Init.HwFlowCtl = UART_HWCONTROL_NONE; + self->handle.Init.OverSampling = UART_OVERSAMPLING_16; + if (HAL_UART_Init(&self->handle) != HAL_OK) { - mp_raise_NotImplementedError(translate("UART explode")); + mp_raise_ValueError(translate("UART Init Error")); } + claim_pin(tx); + claim_pin(rx); } bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) { @@ -74,11 +197,23 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) { // Read characters. size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) { + if (HAL_UART_Receive(&self->handle, data, (uint16_t)len, 500) == HAL_OK) { + return len; + } else { + mp_raise_ValueError(translate("UART read error")); + } return 0; } // Write characters. size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) { + //const char aTxBuffer[] = "This is the internal message"; + + if (HAL_UART_Transmit(&self->handle, (uint8_t *)data, len, 500) == HAL_OK) { + return len; + } else { + mp_raise_ValueError(translate("UART write error")); + } return 0; } diff --git a/ports/stm32f4/common-hal/busio/UART.h b/ports/stm32f4/common-hal/busio/UART.h index 8a4b554c4a..fd1fd113a8 100644 --- a/ports/stm32f4/common-hal/busio/UART.h +++ b/ports/stm32f4/common-hal/busio/UART.h @@ -28,11 +28,13 @@ #define MICROPY_INCLUDED_STM32F4_COMMON_HAL_BUSIO_UART_H #include "common-hal/microcontroller/Pin.h" +#include "stm32f4/periph.h" #include "py/obj.h" typedef struct { mp_obj_base_t base; + UART_HandleTypeDef handle; const mcu_uart_tx_obj_t *tx; const mcu_uart_rx_obj_t *rx; uint8_t character_bits; @@ -43,4 +45,6 @@ typedef struct { uint8_t* buffer; } busio_uart_obj_t; +void uart_reset(void); + #endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_BUSIO_UART_H diff --git a/ports/stm32f4/peripherals/stm32f4/stm32f405xx/periph.h b/ports/stm32f4/peripherals/stm32f4/stm32f405xx/periph.h index 6737720266..5ab7f025ea 100644 --- a/ports/stm32f4/peripherals/stm32f4/stm32f405xx/periph.h +++ b/ports/stm32f4/peripherals/stm32f4/stm32f405xx/periph.h @@ -43,7 +43,7 @@ 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] +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]; diff --git a/ports/stm32f4/supervisor/port.c b/ports/stm32f4/supervisor/port.c index df7a6ca427..7d50c94c3b 100644 --- a/ports/stm32f4/supervisor/port.c +++ b/ports/stm32f4/supervisor/port.c @@ -33,6 +33,7 @@ #include "common-hal/microcontroller/Pin.h" #include "common-hal/busio/I2C.h" #include "common-hal/busio/SPI.h" +#include "common-hal/busio/UART.h" #include "stm32f4/clocks.h" #include "stm32f4/gpio.h" @@ -57,6 +58,7 @@ void reset_port(void) { reset_all_pins(); i2c_reset(); spi_reset(); + uart_reset(); } void reset_to_bootloader(void) {