From 53521152a8999448e1ca6bb1b2a16685423bf373 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 17 Apr 2016 11:59:33 +0100 Subject: [PATCH] stmhal: L4: Modify uart.c to support L4 MCU. L4 does not have UART6, and has similar registers to the F7. Original patch was authored by Tobias Badertscher / @tobbad, but it was reworked to split UART edits from USB edits. --- stmhal/uart.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/stmhal/uart.c b/stmhal/uart.c index caa4d56007..108236ca1a 100644 --- a/stmhal/uart.c +++ b/stmhal/uart.c @@ -150,7 +150,6 @@ STATIC bool uart_init2(pyb_uart_obj_t *uart_obj) { #endif #if defined(MICROPY_HW_UART2_PORT) && defined(MICROPY_HW_UART2_PINS) - // USART2 is on PA2/PA3 (CTS,RTS,CK on PA0,PA1,PA4), PD5/PD6 (CK on PD7) case PYB_UART_2: UARTx = USART2; irqn = USART2_IRQn; @@ -327,7 +326,7 @@ int uart_rx_char(pyb_uart_obj_t *self) { return data; } else { // no buffering - #if defined(MCU_SERIES_F7) + #if defined(MCU_SERIES_F7) || defined(MCU_SERIES_L4) return self->uart.Instance->RDR & self->char_mask; #else return self->uart.Instance->DR & self->char_mask; @@ -398,7 +397,7 @@ void uart_irq_handler(mp_uint_t uart_id) { uint16_t next_head = (self->read_buf_head + 1) % self->read_buf_len; if (next_head != self->read_buf_tail) { // only read data if room in buf - #if defined(MCU_SERIES_F7) + #if defined(MCU_SERIES_F7) || defined(MCU_SERIES_L4) int data = self->uart.Instance->RDR; // clears UART_FLAG_RXNE #else int data = self->uart.Instance->DR; // clears UART_FLAG_RXNE @@ -571,12 +570,20 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con // compute actual baudrate that was configured // (this formula assumes UART_OVERSAMPLING_16) uint32_t actual_baudrate; - if (self->uart.Instance == USART1 || self->uart.Instance == USART6) { + if (self->uart.Instance == USART1 + #if defined(USART6) + || self->uart.Instance == USART6 + #endif + ) { actual_baudrate = HAL_RCC_GetPCLK2Freq(); } else { actual_baudrate = HAL_RCC_GetPCLK1Freq(); } + #if defined(MCU_SERIES_L4) + actual_baudrate = (actual_baudrate << 5) / (self->uart.Instance->BRR >> 3); + #else actual_baudrate /= self->uart.Instance->BRR; + #endif // check we could set the baudrate within 5% uint32_t baudrate_diff; @@ -716,11 +723,13 @@ STATIC mp_obj_t pyb_uart_deinit(mp_obj_t self_in) { __UART5_RELEASE_RESET(); __UART5_CLK_DISABLE(); #endif + #if defined(UART6) } else if (uart->Instance == USART6) { HAL_NVIC_DisableIRQ(USART6_IRQn); __USART6_FORCE_RESET(); __USART6_RELEASE_RESET(); __USART6_CLK_DISABLE(); + #endif } return mp_const_none; } @@ -776,7 +785,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_uart_readchar_obj, pyb_uart_readchar); // uart.sendbreak() STATIC mp_obj_t pyb_uart_sendbreak(mp_obj_t self_in) { pyb_uart_obj_t *self = self_in; - #if defined(MCU_SERIES_F7) + #if defined(MCU_SERIES_F7) || defined(MCU_SERIES_L4) self->uart.Instance->RQR = USART_RQR_SBKRQ; // write-only register #else self->uart.Instance->CR1 |= USART_CR1_SBK;