2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-03-12 21:06:26 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2014-08-21 17:48:23 -04:00
|
|
|
#include <stdarg.h>
|
2014-03-12 21:06:26 -04:00
|
|
|
|
2015-01-01 16:06:20 -05:00
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/stream.h"
|
2016-05-10 18:22:54 -04:00
|
|
|
#include "py/mperrno.h"
|
2015-10-30 19:03:58 -04:00
|
|
|
#include "py/mphal.h"
|
2018-04-23 03:06:40 -04:00
|
|
|
#include "lib/utils/interrupt_char.h"
|
2018-12-09 11:07:56 -05:00
|
|
|
#include "lib/utils/mpirq.h"
|
2014-04-21 07:03:09 -04:00
|
|
|
#include "uart.h"
|
2015-10-31 13:44:20 -04:00
|
|
|
#include "irq.h"
|
2018-04-23 03:06:40 -04:00
|
|
|
#include "pendsv.h"
|
2014-03-12 21:06:26 -04:00
|
|
|
|
2018-12-09 18:44:52 -05:00
|
|
|
#if defined(STM32F4)
|
|
|
|
#define UART_RXNE_IS_SET(uart) ((uart)->SR & USART_SR_RXNE)
|
2019-07-03 09:40:49 -04:00
|
|
|
#elif defined(STM32H7)
|
|
|
|
#define UART_RXNE_IS_SET(uart) ((uart)->ISR & USART_ISR_RXNE_RXFNE)
|
2018-12-09 18:44:52 -05:00
|
|
|
#else
|
|
|
|
#define UART_RXNE_IS_SET(uart) ((uart)->ISR & USART_ISR_RXNE)
|
|
|
|
#endif
|
|
|
|
#define UART_RXNE_IT_EN(uart) do { (uart)->CR1 |= USART_CR1_RXNEIE; } while (0)
|
|
|
|
#define UART_RXNE_IT_DIS(uart) do { (uart)->CR1 &= ~USART_CR1_RXNEIE; } while (0)
|
|
|
|
|
2018-12-29 06:44:41 -05:00
|
|
|
#define USART_CR1_IE_BASE (USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RXNEIE | USART_CR1_IDLEIE)
|
|
|
|
#define USART_CR2_IE_BASE (USART_CR2_LBDIE)
|
|
|
|
#define USART_CR3_IE_BASE (USART_CR3_CTSIE | USART_CR3_EIE)
|
|
|
|
|
|
|
|
#if defined(STM32F0)
|
|
|
|
#define USART_CR1_IE_ALL (USART_CR1_IE_BASE | USART_CR1_EOBIE | USART_CR1_RTOIE | USART_CR1_CMIE)
|
|
|
|
#define USART_CR2_IE_ALL (USART_CR2_IE_BASE)
|
|
|
|
#define USART_CR3_IE_ALL (USART_CR3_IE_BASE | USART_CR3_WUFIE)
|
|
|
|
|
|
|
|
#elif defined(STM32F4)
|
|
|
|
#define USART_CR1_IE_ALL (USART_CR1_IE_BASE)
|
|
|
|
#define USART_CR2_IE_ALL (USART_CR2_IE_BASE)
|
|
|
|
#define USART_CR3_IE_ALL (USART_CR3_IE_BASE)
|
|
|
|
|
|
|
|
#elif defined(STM32F7)
|
|
|
|
#define USART_CR1_IE_ALL (USART_CR1_IE_BASE | USART_CR1_EOBIE | USART_CR1_RTOIE | USART_CR1_CMIE)
|
|
|
|
#define USART_CR2_IE_ALL (USART_CR2_IE_BASE)
|
|
|
|
#if defined(USART_CR3_TCBGTIE)
|
|
|
|
#define USART_CR3_IE_ALL (USART_CR3_IE_BASE | USART_CR3_TCBGTIE)
|
|
|
|
#else
|
|
|
|
#define USART_CR3_IE_ALL (USART_CR3_IE_BASE)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#elif defined(STM32H7)
|
|
|
|
#define USART_CR1_IE_ALL (USART_CR1_IE_BASE | USART_CR1_RXFFIE | USART_CR1_TXFEIE | USART_CR1_EOBIE | USART_CR1_RTOIE | USART_CR1_CMIE)
|
|
|
|
#define USART_CR2_IE_ALL (USART_CR2_IE_BASE)
|
|
|
|
#define USART_CR3_IE_ALL (USART_CR3_IE_BASE | USART_CR3_RXFTIE | USART_CR3_TCBGTIE | USART_CR3_TXFTIE | USART_CR3_WUFIE)
|
|
|
|
|
|
|
|
#elif defined(STM32L4)
|
|
|
|
#define USART_CR1_IE_ALL (USART_CR1_IE_BASE | USART_CR1_EOBIE | USART_CR1_RTOIE | USART_CR1_CMIE)
|
|
|
|
#define USART_CR2_IE_ALL (USART_CR2_IE_BASE)
|
|
|
|
#if defined(USART_CR3_TCBGTIE)
|
|
|
|
#define USART_CR3_IE_ALL (USART_CR3_IE_BASE | USART_CR3_TCBGTIE | USART_CR3_WUFIE)
|
|
|
|
#else
|
|
|
|
#define USART_CR3_IE_ALL (USART_CR3_IE_BASE | USART_CR3_WUFIE)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2018-02-23 11:53:20 -05:00
|
|
|
extern void NORETURN __fatal_error(const char *msg);
|
2014-10-11 12:57:10 -04:00
|
|
|
|
|
|
|
void uart_init0(void) {
|
2018-02-23 11:53:20 -05:00
|
|
|
#if defined(STM32H7)
|
|
|
|
RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit = {0};
|
|
|
|
// Configure USART1/6 clock source
|
|
|
|
RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART16;
|
|
|
|
RCC_PeriphClkInit.Usart16ClockSelection = RCC_USART16CLKSOURCE_D2PCLK2;
|
|
|
|
if (HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit) != HAL_OK) {
|
|
|
|
__fatal_error("HAL_RCCEx_PeriphCLKConfig");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure USART2/3/4/5/7/8 clock source
|
|
|
|
RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART234578;
|
|
|
|
RCC_PeriphClkInit.Usart16ClockSelection = RCC_USART234578CLKSOURCE_D2PCLK1;
|
|
|
|
if (HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit) != HAL_OK) {
|
|
|
|
__fatal_error("HAL_RCCEx_PeriphCLKConfig");
|
|
|
|
}
|
|
|
|
#endif
|
2014-10-11 12:57:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// unregister all interrupt sources
|
2018-12-07 02:36:43 -05:00
|
|
|
void uart_deinit_all(void) {
|
2015-01-07 18:38:50 -05:00
|
|
|
for (int i = 0; i < MP_ARRAY_SIZE(MP_STATE_PORT(pyb_uart_obj_all)); i++) {
|
|
|
|
pyb_uart_obj_t *uart_obj = MP_STATE_PORT(pyb_uart_obj_all)[i];
|
2018-12-09 21:08:31 -05:00
|
|
|
if (uart_obj != NULL && !uart_obj->is_static) {
|
2018-12-07 02:36:43 -05:00
|
|
|
uart_deinit(uart_obj);
|
2018-12-09 21:08:31 -05:00
|
|
|
MP_STATE_PORT(pyb_uart_obj_all)[i] = NULL;
|
2014-10-11 12:57:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-07 02:36:43 -05:00
|
|
|
bool uart_exists(int uart_id) {
|
2016-12-04 23:31:16 -05:00
|
|
|
if (uart_id > MP_ARRAY_SIZE(MP_STATE_PORT(pyb_uart_obj_all))) {
|
|
|
|
// safeguard against pyb_uart_obj_all array being configured too small
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch (uart_id) {
|
|
|
|
#if defined(MICROPY_HW_UART1_TX) && defined(MICROPY_HW_UART1_RX)
|
|
|
|
case PYB_UART_1: return true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MICROPY_HW_UART2_TX) && defined(MICROPY_HW_UART2_RX)
|
|
|
|
case PYB_UART_2: return true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MICROPY_HW_UART3_TX) && defined(MICROPY_HW_UART3_RX)
|
|
|
|
case PYB_UART_3: return true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MICROPY_HW_UART4_TX) && defined(MICROPY_HW_UART4_RX)
|
|
|
|
case PYB_UART_4: return true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MICROPY_HW_UART5_TX) && defined(MICROPY_HW_UART5_RX)
|
|
|
|
case PYB_UART_5: return true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MICROPY_HW_UART6_TX) && defined(MICROPY_HW_UART6_RX)
|
|
|
|
case PYB_UART_6: return true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MICROPY_HW_UART7_TX) && defined(MICROPY_HW_UART7_RX)
|
|
|
|
case PYB_UART_7: return true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MICROPY_HW_UART8_TX) && defined(MICROPY_HW_UART8_RX)
|
|
|
|
case PYB_UART_8: return true;
|
|
|
|
#endif
|
|
|
|
|
2019-04-19 01:15:18 -04:00
|
|
|
#if defined(MICROPY_HW_UART9_TX) && defined(MICROPY_HW_UART9_RX)
|
|
|
|
case PYB_UART_9: return true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MICROPY_HW_UART10_TX) && defined(MICROPY_HW_UART10_RX)
|
|
|
|
case PYB_UART_10: return true;
|
|
|
|
#endif
|
|
|
|
|
2016-12-04 23:31:16 -05:00
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-20 20:14:14 -04:00
|
|
|
// assumes Init parameters have been set up correctly
|
2018-12-09 19:25:06 -05:00
|
|
|
bool uart_init(pyb_uart_obj_t *uart_obj,
|
|
|
|
uint32_t baudrate, uint32_t bits, uint32_t parity, uint32_t stop, uint32_t flow) {
|
2014-10-11 12:57:10 -04:00
|
|
|
USART_TypeDef *UARTx;
|
|
|
|
IRQn_Type irqn;
|
2016-12-04 20:21:45 -05:00
|
|
|
int uart_unit;
|
2014-03-12 21:06:26 -04:00
|
|
|
|
2016-12-04 20:21:45 -05:00
|
|
|
const pin_obj_t *pins[4] = {0};
|
2014-04-13 20:45:58 -04:00
|
|
|
|
2016-12-04 20:21:45 -05:00
|
|
|
switch (uart_obj->uart_id) {
|
|
|
|
#if defined(MICROPY_HW_UART1_TX) && defined(MICROPY_HW_UART1_RX)
|
2015-08-02 19:23:47 -04:00
|
|
|
case PYB_UART_1:
|
2016-12-04 20:21:45 -05:00
|
|
|
uart_unit = 1;
|
2015-08-02 19:23:47 -04:00
|
|
|
UARTx = USART1;
|
|
|
|
irqn = USART1_IRQn;
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[0] = MICROPY_HW_UART1_TX;
|
|
|
|
pins[1] = MICROPY_HW_UART1_RX;
|
2018-02-12 23:37:35 -05:00
|
|
|
__HAL_RCC_USART1_CLK_ENABLE();
|
2015-08-02 19:23:47 -04:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2016-12-04 20:21:45 -05:00
|
|
|
#if defined(MICROPY_HW_UART2_TX) && defined(MICROPY_HW_UART2_RX)
|
2014-04-21 07:03:09 -04:00
|
|
|
case PYB_UART_2:
|
2016-12-04 20:21:45 -05:00
|
|
|
uart_unit = 2;
|
2014-04-21 07:03:09 -04:00
|
|
|
UARTx = USART2;
|
2014-10-11 12:57:10 -04:00
|
|
|
irqn = USART2_IRQn;
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[0] = MICROPY_HW_UART2_TX;
|
|
|
|
pins[1] = MICROPY_HW_UART2_RX;
|
2015-05-02 12:31:39 -04:00
|
|
|
#if defined(MICROPY_HW_UART2_RTS)
|
2018-12-09 19:25:06 -05:00
|
|
|
if (flow & UART_HWCONTROL_RTS) {
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[2] = MICROPY_HW_UART2_RTS;
|
2014-10-30 20:40:57 -04:00
|
|
|
}
|
2015-05-02 12:31:39 -04:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_UART2_CTS)
|
2018-12-09 19:25:06 -05:00
|
|
|
if (flow & UART_HWCONTROL_CTS) {
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[3] = MICROPY_HW_UART2_CTS;
|
2014-10-30 20:40:57 -04:00
|
|
|
}
|
2015-05-02 12:31:39 -04:00
|
|
|
#endif
|
2018-02-12 23:37:35 -05:00
|
|
|
__HAL_RCC_USART2_CLK_ENABLE();
|
2014-03-12 21:06:26 -04:00
|
|
|
break;
|
2015-05-02 12:31:39 -04:00
|
|
|
#endif
|
2014-04-13 20:45:58 -04:00
|
|
|
|
2016-12-04 20:21:45 -05:00
|
|
|
#if defined(MICROPY_HW_UART3_TX) && defined(MICROPY_HW_UART3_RX)
|
2014-04-21 07:03:09 -04:00
|
|
|
case PYB_UART_3:
|
2016-12-04 20:21:45 -05:00
|
|
|
uart_unit = 3;
|
2014-04-21 07:03:09 -04:00
|
|
|
UARTx = USART3;
|
2018-09-21 00:02:54 -04:00
|
|
|
#if defined(STM32F0)
|
|
|
|
irqn = USART3_8_IRQn;
|
|
|
|
#else
|
2014-10-11 12:57:10 -04:00
|
|
|
irqn = USART3_IRQn;
|
2018-09-21 00:02:54 -04:00
|
|
|
#endif
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[0] = MICROPY_HW_UART3_TX;
|
|
|
|
pins[1] = MICROPY_HW_UART3_RX;
|
2015-05-02 12:31:39 -04:00
|
|
|
#if defined(MICROPY_HW_UART3_RTS)
|
2018-12-09 19:25:06 -05:00
|
|
|
if (flow & UART_HWCONTROL_RTS) {
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[2] = MICROPY_HW_UART3_RTS;
|
2014-10-30 20:40:57 -04:00
|
|
|
}
|
2015-05-02 12:31:39 -04:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_UART3_CTS)
|
2018-12-09 19:25:06 -05:00
|
|
|
if (flow & UART_HWCONTROL_CTS) {
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[3] = MICROPY_HW_UART3_CTS;
|
2014-10-30 20:40:57 -04:00
|
|
|
}
|
2015-05-02 12:31:39 -04:00
|
|
|
#endif
|
2018-02-12 23:37:35 -05:00
|
|
|
__HAL_RCC_USART3_CLK_ENABLE();
|
2014-03-12 21:06:26 -04:00
|
|
|
break;
|
2015-04-18 10:59:08 -04:00
|
|
|
#endif
|
2014-04-13 20:45:58 -04:00
|
|
|
|
2016-12-04 20:21:45 -05:00
|
|
|
#if defined(MICROPY_HW_UART4_TX) && defined(MICROPY_HW_UART4_RX)
|
2014-04-21 07:03:09 -04:00
|
|
|
case PYB_UART_4:
|
2016-12-04 20:21:45 -05:00
|
|
|
uart_unit = 4;
|
2018-09-21 00:02:54 -04:00
|
|
|
#if defined(STM32F0)
|
|
|
|
UARTx = USART4;
|
|
|
|
irqn = USART3_8_IRQn;
|
|
|
|
__HAL_RCC_USART4_CLK_ENABLE();
|
|
|
|
#else
|
2014-04-21 07:03:09 -04:00
|
|
|
UARTx = UART4;
|
2014-10-11 12:57:10 -04:00
|
|
|
irqn = UART4_IRQn;
|
2018-09-21 00:02:54 -04:00
|
|
|
__HAL_RCC_UART4_CLK_ENABLE();
|
|
|
|
#endif
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[0] = MICROPY_HW_UART4_TX;
|
|
|
|
pins[1] = MICROPY_HW_UART4_RX;
|
2014-04-13 20:45:58 -04:00
|
|
|
break;
|
2015-04-18 10:59:08 -04:00
|
|
|
#endif
|
2014-04-13 20:45:58 -04:00
|
|
|
|
2016-12-04 20:21:45 -05:00
|
|
|
#if defined(MICROPY_HW_UART5_TX) && defined(MICROPY_HW_UART5_RX)
|
2015-05-31 18:37:37 -04:00
|
|
|
case PYB_UART_5:
|
2016-12-04 20:21:45 -05:00
|
|
|
uart_unit = 5;
|
2018-09-21 00:02:54 -04:00
|
|
|
#if defined(STM32F0)
|
|
|
|
UARTx = USART5;
|
|
|
|
irqn = USART3_8_IRQn;
|
|
|
|
__HAL_RCC_USART5_CLK_ENABLE();
|
|
|
|
#else
|
2015-05-31 18:37:37 -04:00
|
|
|
UARTx = UART5;
|
|
|
|
irqn = UART5_IRQn;
|
2018-09-21 00:02:54 -04:00
|
|
|
__HAL_RCC_UART5_CLK_ENABLE();
|
|
|
|
#endif
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[0] = MICROPY_HW_UART5_TX;
|
|
|
|
pins[1] = MICROPY_HW_UART5_RX;
|
2015-05-31 18:37:37 -04:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2016-12-04 20:21:45 -05:00
|
|
|
#if defined(MICROPY_HW_UART6_TX) && defined(MICROPY_HW_UART6_RX)
|
2014-04-21 07:03:09 -04:00
|
|
|
case PYB_UART_6:
|
2016-12-04 20:21:45 -05:00
|
|
|
uart_unit = 6;
|
2014-04-21 07:03:09 -04:00
|
|
|
UARTx = USART6;
|
2018-09-21 00:02:54 -04:00
|
|
|
#if defined(STM32F0)
|
|
|
|
irqn = USART3_8_IRQn;
|
|
|
|
#else
|
2014-10-11 12:57:10 -04:00
|
|
|
irqn = USART6_IRQn;
|
2018-09-21 00:02:54 -04:00
|
|
|
#endif
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[0] = MICROPY_HW_UART6_TX;
|
|
|
|
pins[1] = MICROPY_HW_UART6_RX;
|
2017-12-23 03:01:23 -05:00
|
|
|
#if defined(MICROPY_HW_UART6_RTS)
|
2018-12-09 19:25:06 -05:00
|
|
|
if (flow & UART_HWCONTROL_RTS) {
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[2] = MICROPY_HW_UART6_RTS;
|
2017-12-23 03:01:23 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_UART6_CTS)
|
2018-12-09 19:25:06 -05:00
|
|
|
if (flow & UART_HWCONTROL_CTS) {
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[3] = MICROPY_HW_UART6_CTS;
|
2017-12-23 03:01:23 -05:00
|
|
|
}
|
|
|
|
#endif
|
2018-02-12 23:37:35 -05:00
|
|
|
__HAL_RCC_USART6_CLK_ENABLE();
|
2014-03-12 21:06:26 -04:00
|
|
|
break;
|
2015-05-02 12:31:39 -04:00
|
|
|
#endif
|
2014-04-13 20:45:58 -04:00
|
|
|
|
2016-12-04 23:14:22 -05:00
|
|
|
#if defined(MICROPY_HW_UART7_TX) && defined(MICROPY_HW_UART7_RX)
|
|
|
|
case PYB_UART_7:
|
|
|
|
uart_unit = 7;
|
2018-09-21 00:02:54 -04:00
|
|
|
#if defined(STM32F0)
|
|
|
|
UARTx = USART7;
|
|
|
|
irqn = USART3_8_IRQn;
|
|
|
|
__HAL_RCC_USART7_CLK_ENABLE();
|
|
|
|
#else
|
2016-12-04 23:14:22 -05:00
|
|
|
UARTx = UART7;
|
|
|
|
irqn = UART7_IRQn;
|
2018-09-21 00:02:54 -04:00
|
|
|
__HAL_RCC_UART7_CLK_ENABLE();
|
|
|
|
#endif
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[0] = MICROPY_HW_UART7_TX;
|
|
|
|
pins[1] = MICROPY_HW_UART7_RX;
|
2016-12-04 23:14:22 -05:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MICROPY_HW_UART8_TX) && defined(MICROPY_HW_UART8_RX)
|
|
|
|
case PYB_UART_8:
|
|
|
|
uart_unit = 8;
|
2018-05-28 04:10:53 -04:00
|
|
|
#if defined(STM32F0)
|
|
|
|
UARTx = USART8;
|
|
|
|
irqn = USART3_8_IRQn;
|
|
|
|
__HAL_RCC_USART8_CLK_ENABLE();
|
|
|
|
#else
|
2016-12-04 23:14:22 -05:00
|
|
|
UARTx = UART8;
|
|
|
|
irqn = UART8_IRQn;
|
2018-05-28 04:10:53 -04:00
|
|
|
__HAL_RCC_UART8_CLK_ENABLE();
|
|
|
|
#endif
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[0] = MICROPY_HW_UART8_TX;
|
|
|
|
pins[1] = MICROPY_HW_UART8_RX;
|
2016-12-04 23:14:22 -05:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2019-04-19 01:15:18 -04:00
|
|
|
#if defined(MICROPY_HW_UART9_TX) && defined(MICROPY_HW_UART9_RX)
|
|
|
|
case PYB_UART_9:
|
|
|
|
uart_unit = 9;
|
|
|
|
UARTx = UART9;
|
|
|
|
irqn = UART9_IRQn;
|
|
|
|
__HAL_RCC_UART9_CLK_ENABLE();
|
|
|
|
pins[0] = MICROPY_HW_UART9_TX;
|
|
|
|
pins[1] = MICROPY_HW_UART9_RX;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MICROPY_HW_UART10_TX) && defined(MICROPY_HW_UART10_RX)
|
|
|
|
case PYB_UART_10:
|
|
|
|
uart_unit = 10;
|
|
|
|
UARTx = UART10;
|
|
|
|
irqn = UART10_IRQn;
|
|
|
|
__HAL_RCC_UART10_CLK_ENABLE();
|
|
|
|
pins[0] = MICROPY_HW_UART10_TX;
|
|
|
|
pins[1] = MICROPY_HW_UART10_RX;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2014-04-13 20:45:58 -04:00
|
|
|
default:
|
2015-05-02 12:31:39 -04:00
|
|
|
// UART does not exist or is not configured for this board
|
2014-04-13 20:45:58 -04:00
|
|
|
return false;
|
2014-03-12 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2016-12-04 20:21:45 -05:00
|
|
|
uint32_t mode = MP_HAL_PIN_MODE_ALT;
|
|
|
|
uint32_t pull = MP_HAL_PIN_PULL_UP;
|
2014-10-11 12:57:10 -04:00
|
|
|
|
2016-12-04 20:21:45 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-08-02 19:20:08 -04:00
|
|
|
}
|
|
|
|
|
2018-12-09 18:44:52 -05:00
|
|
|
uart_obj->uartx = UARTx;
|
2016-12-04 20:21:45 -05:00
|
|
|
|
2014-04-21 07:03:09 -04:00
|
|
|
// init UARTx
|
2018-12-09 18:44:52 -05:00
|
|
|
UART_HandleTypeDef huart;
|
|
|
|
memset(&huart, 0, sizeof(huart));
|
|
|
|
huart.Instance = UARTx;
|
2018-12-09 19:25:06 -05:00
|
|
|
huart.Init.BaudRate = baudrate;
|
|
|
|
huart.Init.WordLength = bits;
|
|
|
|
huart.Init.StopBits = stop;
|
|
|
|
huart.Init.Parity = parity;
|
|
|
|
huart.Init.Mode = UART_MODE_TX_RX;
|
|
|
|
huart.Init.HwFlowCtl = flow;
|
|
|
|
huart.Init.OverSampling = UART_OVERSAMPLING_16;
|
2018-12-09 18:44:52 -05:00
|
|
|
HAL_UART_Init(&huart);
|
2014-04-20 20:14:14 -04:00
|
|
|
|
2018-12-29 06:44:41 -05:00
|
|
|
// Disable all individual UART IRQs, but enable the global handler
|
|
|
|
uart_obj->uartx->CR1 &= ~USART_CR1_IE_ALL;
|
|
|
|
uart_obj->uartx->CR2 &= ~USART_CR2_IE_ALL;
|
|
|
|
uart_obj->uartx->CR3 &= ~USART_CR3_IE_ALL;
|
|
|
|
NVIC_SetPriority(IRQn_NONNEG(irqn), IRQ_PRI_UART);
|
|
|
|
HAL_NVIC_EnableIRQ(irqn);
|
|
|
|
|
2014-04-21 07:03:09 -04:00
|
|
|
uart_obj->is_enabled = true;
|
2018-04-23 06:44:30 -04:00
|
|
|
uart_obj->attached_to_repl = false;
|
2014-04-20 20:14:14 -04:00
|
|
|
|
2018-12-09 21:02:11 -05:00
|
|
|
if (bits == UART_WORDLENGTH_9B && parity == UART_PARITY_NONE) {
|
|
|
|
uart_obj->char_mask = 0x1ff;
|
|
|
|
uart_obj->char_width = CHAR_WIDTH_9BIT;
|
|
|
|
} else {
|
|
|
|
if (bits == UART_WORDLENGTH_9B || parity == UART_PARITY_NONE) {
|
|
|
|
uart_obj->char_mask = 0xff;
|
|
|
|
} else {
|
|
|
|
uart_obj->char_mask = 0x7f;
|
|
|
|
}
|
|
|
|
uart_obj->char_width = CHAR_WIDTH_8BIT;
|
|
|
|
}
|
|
|
|
|
2018-12-09 11:07:56 -05:00
|
|
|
uart_obj->mp_irq_trigger = 0;
|
|
|
|
uart_obj->mp_irq_obj = NULL;
|
|
|
|
|
2014-04-20 20:14:14 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-09 18:21:37 -05:00
|
|
|
void uart_set_rxbuf(pyb_uart_obj_t *self, size_t len, void *buf) {
|
|
|
|
self->read_buf_head = 0;
|
|
|
|
self->read_buf_tail = 0;
|
|
|
|
self->read_buf_len = len;
|
|
|
|
self->read_buf = buf;
|
|
|
|
if (len == 0) {
|
2018-12-09 18:44:52 -05:00
|
|
|
UART_RXNE_IT_DIS(self->uartx);
|
2018-12-09 18:21:37 -05:00
|
|
|
} else {
|
2018-12-09 18:44:52 -05:00
|
|
|
UART_RXNE_IT_EN(self->uartx);
|
2018-12-09 18:21:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-07 02:36:43 -05:00
|
|
|
void uart_deinit(pyb_uart_obj_t *self) {
|
|
|
|
self->is_enabled = false;
|
2018-12-09 18:44:52 -05:00
|
|
|
|
2018-12-09 18:58:08 -05:00
|
|
|
// Disable UART
|
|
|
|
self->uartx->CR1 &= ~USART_CR1_UE;
|
2018-12-09 18:44:52 -05:00
|
|
|
|
2018-12-09 18:58:08 -05:00
|
|
|
// Reset and turn off the UART peripheral
|
2018-12-09 18:44:52 -05:00
|
|
|
if (self->uart_id == 1) {
|
2018-12-07 02:36:43 -05:00
|
|
|
HAL_NVIC_DisableIRQ(USART1_IRQn);
|
|
|
|
__HAL_RCC_USART1_FORCE_RESET();
|
|
|
|
__HAL_RCC_USART1_RELEASE_RESET();
|
|
|
|
__HAL_RCC_USART1_CLK_DISABLE();
|
2018-12-09 18:44:52 -05:00
|
|
|
} else if (self->uart_id == 2) {
|
2018-12-07 02:36:43 -05:00
|
|
|
HAL_NVIC_DisableIRQ(USART2_IRQn);
|
|
|
|
__HAL_RCC_USART2_FORCE_RESET();
|
|
|
|
__HAL_RCC_USART2_RELEASE_RESET();
|
|
|
|
__HAL_RCC_USART2_CLK_DISABLE();
|
|
|
|
#if defined(USART3)
|
2018-12-09 18:44:52 -05:00
|
|
|
} else if (self->uart_id == 3) {
|
2018-12-07 02:36:43 -05:00
|
|
|
#if !defined(STM32F0)
|
|
|
|
HAL_NVIC_DisableIRQ(USART3_IRQn);
|
|
|
|
#endif
|
|
|
|
__HAL_RCC_USART3_FORCE_RESET();
|
|
|
|
__HAL_RCC_USART3_RELEASE_RESET();
|
|
|
|
__HAL_RCC_USART3_CLK_DISABLE();
|
|
|
|
#endif
|
|
|
|
#if defined(UART4)
|
2018-12-09 18:44:52 -05:00
|
|
|
} else if (self->uart_id == 4) {
|
2018-12-07 02:36:43 -05:00
|
|
|
HAL_NVIC_DisableIRQ(UART4_IRQn);
|
|
|
|
__HAL_RCC_UART4_FORCE_RESET();
|
|
|
|
__HAL_RCC_UART4_RELEASE_RESET();
|
|
|
|
__HAL_RCC_UART4_CLK_DISABLE();
|
|
|
|
#endif
|
|
|
|
#if defined(USART4)
|
2018-12-09 18:44:52 -05:00
|
|
|
} else if (self->uart_id == 4) {
|
2018-12-07 02:36:43 -05:00
|
|
|
__HAL_RCC_USART4_FORCE_RESET();
|
|
|
|
__HAL_RCC_USART4_RELEASE_RESET();
|
|
|
|
__HAL_RCC_USART4_CLK_DISABLE();
|
|
|
|
#endif
|
|
|
|
#if defined(UART5)
|
2018-12-09 18:44:52 -05:00
|
|
|
} else if (self->uart_id == 5) {
|
2018-12-07 02:36:43 -05:00
|
|
|
HAL_NVIC_DisableIRQ(UART5_IRQn);
|
|
|
|
__HAL_RCC_UART5_FORCE_RESET();
|
|
|
|
__HAL_RCC_UART5_RELEASE_RESET();
|
|
|
|
__HAL_RCC_UART5_CLK_DISABLE();
|
|
|
|
#endif
|
|
|
|
#if defined(USART5)
|
2018-12-09 18:44:52 -05:00
|
|
|
} else if (self->uart_id == 5) {
|
2018-12-07 02:36:43 -05:00
|
|
|
__HAL_RCC_USART5_FORCE_RESET();
|
|
|
|
__HAL_RCC_USART5_RELEASE_RESET();
|
|
|
|
__HAL_RCC_USART5_CLK_DISABLE();
|
|
|
|
#endif
|
|
|
|
#if defined(UART6)
|
2018-12-09 18:44:52 -05:00
|
|
|
} else if (self->uart_id == 6) {
|
2018-12-07 02:36:43 -05:00
|
|
|
HAL_NVIC_DisableIRQ(USART6_IRQn);
|
|
|
|
__HAL_RCC_USART6_FORCE_RESET();
|
|
|
|
__HAL_RCC_USART6_RELEASE_RESET();
|
|
|
|
__HAL_RCC_USART6_CLK_DISABLE();
|
|
|
|
#endif
|
|
|
|
#if defined(UART7)
|
2018-12-09 18:44:52 -05:00
|
|
|
} else if (self->uart_id == 7) {
|
2018-12-07 02:36:43 -05:00
|
|
|
HAL_NVIC_DisableIRQ(UART7_IRQn);
|
|
|
|
__HAL_RCC_UART7_FORCE_RESET();
|
|
|
|
__HAL_RCC_UART7_RELEASE_RESET();
|
|
|
|
__HAL_RCC_UART7_CLK_DISABLE();
|
|
|
|
#endif
|
|
|
|
#if defined(USART7)
|
2018-12-09 18:44:52 -05:00
|
|
|
} else if (self->uart_id == 7) {
|
2018-12-07 02:36:43 -05:00
|
|
|
__HAL_RCC_USART7_FORCE_RESET();
|
|
|
|
__HAL_RCC_USART7_RELEASE_RESET();
|
|
|
|
__HAL_RCC_USART7_CLK_DISABLE();
|
|
|
|
#endif
|
|
|
|
#if defined(UART8)
|
2018-12-09 18:44:52 -05:00
|
|
|
} else if (self->uart_id == 8) {
|
2018-12-07 02:36:43 -05:00
|
|
|
HAL_NVIC_DisableIRQ(UART8_IRQn);
|
|
|
|
__HAL_RCC_UART8_FORCE_RESET();
|
|
|
|
__HAL_RCC_UART8_RELEASE_RESET();
|
|
|
|
__HAL_RCC_UART8_CLK_DISABLE();
|
|
|
|
#endif
|
|
|
|
#if defined(USART8)
|
2018-12-09 18:44:52 -05:00
|
|
|
} else if (self->uart_id == 8) {
|
2018-12-07 02:36:43 -05:00
|
|
|
__HAL_RCC_USART8_FORCE_RESET();
|
|
|
|
__HAL_RCC_USART8_RELEASE_RESET();
|
|
|
|
__HAL_RCC_USART8_CLK_DISABLE();
|
|
|
|
#endif
|
2019-04-19 01:15:18 -04:00
|
|
|
#if defined(UART9)
|
|
|
|
} else if (self->uart_id == 9) {
|
|
|
|
HAL_NVIC_DisableIRQ(UART9_IRQn);
|
|
|
|
__HAL_RCC_UART9_FORCE_RESET();
|
|
|
|
__HAL_RCC_UART9_RELEASE_RESET();
|
|
|
|
__HAL_RCC_UART9_CLK_DISABLE();
|
|
|
|
#endif
|
|
|
|
#if defined(UART10)
|
|
|
|
} else if (self->uart_id == 10) {
|
|
|
|
HAL_NVIC_DisableIRQ(UART10_IRQn);
|
|
|
|
__HAL_RCC_UART10_FORCE_RESET();
|
|
|
|
__HAL_RCC_UART10_RELEASE_RESET();
|
|
|
|
__HAL_RCC_UART10_CLK_DISABLE();
|
|
|
|
#endif
|
2018-12-07 02:36:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-23 06:44:30 -04:00
|
|
|
void uart_attach_to_repl(pyb_uart_obj_t *self, bool attached) {
|
|
|
|
self->attached_to_repl = attached;
|
|
|
|
}
|
|
|
|
|
2018-12-09 02:10:57 -05:00
|
|
|
uint32_t uart_get_baudrate(pyb_uart_obj_t *self) {
|
|
|
|
uint32_t uart_clk = 0;
|
|
|
|
|
|
|
|
#if defined(STM32F0)
|
|
|
|
uart_clk = HAL_RCC_GetPCLK1Freq();
|
2018-12-09 06:51:25 -05:00
|
|
|
#elif defined(STM32F7)
|
|
|
|
switch ((RCC->DCKCFGR2 >> ((self->uart_id - 1) * 2)) & 3) {
|
|
|
|
case 0:
|
|
|
|
if (self->uart_id == 1 || self->uart_id == 6) {
|
|
|
|
uart_clk = HAL_RCC_GetPCLK2Freq();
|
|
|
|
} else {
|
|
|
|
uart_clk = HAL_RCC_GetPCLK1Freq();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
uart_clk = HAL_RCC_GetSysClockFreq();
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
uart_clk = HSI_VALUE;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
uart_clk = LSE_VALUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#elif defined(STM32H7)
|
|
|
|
uint32_t csel;
|
|
|
|
if (self->uart_id == 1 || self->uart_id == 6) {
|
|
|
|
csel = RCC->D2CCIP2R >> 3;
|
|
|
|
} else {
|
|
|
|
csel = RCC->D2CCIP2R;
|
|
|
|
}
|
|
|
|
switch (csel & 3) {
|
|
|
|
case 0:
|
|
|
|
if (self->uart_id == 1 || self->uart_id == 6) {
|
|
|
|
uart_clk = HAL_RCC_GetPCLK2Freq();
|
|
|
|
} else {
|
|
|
|
uart_clk = HAL_RCC_GetPCLK1Freq();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
uart_clk = HSI_VALUE;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
uart_clk = CSI_VALUE;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
uart_clk = LSE_VALUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2018-12-09 02:10:57 -05:00
|
|
|
}
|
|
|
|
#else
|
2018-12-09 06:51:25 -05:00
|
|
|
if (self->uart_id == 1
|
2018-12-09 02:10:57 -05:00
|
|
|
#if defined(USART6)
|
2018-12-09 06:51:25 -05:00
|
|
|
|| self->uart_id == 6
|
2018-12-09 02:10:57 -05:00
|
|
|
#endif
|
2019-04-19 01:15:18 -04:00
|
|
|
#if defined(UART9)
|
|
|
|
|| self->uart_id == 9
|
|
|
|
#endif
|
|
|
|
#if defined(UART10)
|
|
|
|
|| self->uart_id == 10
|
|
|
|
#endif
|
2018-12-09 02:10:57 -05:00
|
|
|
) {
|
|
|
|
uart_clk = HAL_RCC_GetPCLK2Freq();
|
|
|
|
} else {
|
|
|
|
uart_clk = HAL_RCC_GetPCLK1Freq();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// This formula assumes UART_OVERSAMPLING_16
|
2018-12-09 18:44:52 -05:00
|
|
|
uint32_t baudrate = uart_clk / self->uartx->BRR;
|
2018-12-09 02:10:57 -05:00
|
|
|
|
|
|
|
return baudrate;
|
|
|
|
}
|
|
|
|
|
2015-11-27 10:31:59 -05:00
|
|
|
mp_uint_t uart_rx_any(pyb_uart_obj_t *self) {
|
|
|
|
int buffer_bytes = self->read_buf_head - self->read_buf_tail;
|
|
|
|
if (buffer_bytes < 0) {
|
|
|
|
return buffer_bytes + self->read_buf_len;
|
|
|
|
} else if (buffer_bytes > 0) {
|
|
|
|
return buffer_bytes;
|
|
|
|
} else {
|
2018-12-29 06:43:35 -05:00
|
|
|
return UART_RXNE_IS_SET(self->uartx) != 0;
|
2015-11-27 10:31:59 -05:00
|
|
|
}
|
2014-03-12 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2014-10-11 12:57:10 -04:00
|
|
|
// Waits at most timeout milliseconds for at least 1 char to become ready for
|
|
|
|
// reading (from buf or for direct reading).
|
|
|
|
// Returns true if something available, false if not.
|
2018-12-07 02:36:43 -05:00
|
|
|
bool uart_rx_wait(pyb_uart_obj_t *self, uint32_t timeout) {
|
2014-10-11 12:57:10 -04:00
|
|
|
uint32_t start = HAL_GetTick();
|
|
|
|
for (;;) {
|
2018-12-09 18:44:52 -05:00
|
|
|
if (self->read_buf_tail != self->read_buf_head || UART_RXNE_IS_SET(self->uartx)) {
|
2014-10-11 12:57:10 -04:00
|
|
|
return true; // have at least 1 char ready for reading
|
|
|
|
}
|
|
|
|
if (HAL_GetTick() - start >= timeout) {
|
|
|
|
return false; // timeout
|
|
|
|
}
|
2017-02-05 23:10:03 -05:00
|
|
|
MICROPY_EVENT_POLL_HOOK
|
2014-10-11 12:57:10 -04:00
|
|
|
}
|
2014-03-12 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2014-10-11 12:57:10 -04:00
|
|
|
// assumes there is a character available
|
|
|
|
int uart_rx_char(pyb_uart_obj_t *self) {
|
|
|
|
if (self->read_buf_tail != self->read_buf_head) {
|
|
|
|
// buffering via IRQ
|
|
|
|
int data;
|
|
|
|
if (self->char_width == CHAR_WIDTH_9BIT) {
|
|
|
|
data = ((uint16_t*)self->read_buf)[self->read_buf_tail];
|
|
|
|
} else {
|
|
|
|
data = self->read_buf[self->read_buf_tail];
|
|
|
|
}
|
|
|
|
self->read_buf_tail = (self->read_buf_tail + 1) % self->read_buf_len;
|
2018-12-09 18:44:52 -05:00
|
|
|
if (UART_RXNE_IS_SET(self->uartx)) {
|
2016-03-25 06:20:12 -04:00
|
|
|
// UART was stalled by flow ctrl: re-enable IRQ now we have room in buffer
|
2018-12-09 18:44:52 -05:00
|
|
|
UART_RXNE_IT_EN(self->uartx);
|
2016-03-25 06:20:12 -04:00
|
|
|
}
|
2014-10-11 12:57:10 -04:00
|
|
|
return data;
|
|
|
|
} else {
|
|
|
|
// no buffering
|
2018-05-28 04:10:53 -04:00
|
|
|
#if defined(STM32F0) || defined(STM32F7) || defined(STM32L4) || defined(STM32H7)
|
2018-12-29 08:59:16 -05:00
|
|
|
int data = self->uartx->RDR & self->char_mask;
|
|
|
|
self->uartx->ICR = USART_ICR_ORECF; // clear ORE if it was set
|
|
|
|
return data;
|
2015-07-28 14:13:33 -04:00
|
|
|
#else
|
2018-12-09 18:44:52 -05:00
|
|
|
return self->uartx->DR & self->char_mask;
|
2015-07-28 14:13:33 -04:00
|
|
|
#endif
|
2014-03-12 21:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 12:29:52 -05:00
|
|
|
// Waits at most timeout milliseconds for TX register to become empty.
|
|
|
|
// Returns true if can write, false if can't.
|
2018-12-07 02:36:43 -05:00
|
|
|
bool uart_tx_wait(pyb_uart_obj_t *self, uint32_t timeout) {
|
2015-11-30 12:29:52 -05:00
|
|
|
uint32_t start = HAL_GetTick();
|
|
|
|
for (;;) {
|
2018-12-09 18:44:52 -05:00
|
|
|
if (uart_tx_avail(self)) {
|
2015-11-30 12:29:52 -05:00
|
|
|
return true; // tx register is empty
|
|
|
|
}
|
|
|
|
if (HAL_GetTick() - start >= timeout) {
|
|
|
|
return false; // timeout
|
|
|
|
}
|
2017-02-05 23:10:03 -05:00
|
|
|
MICROPY_EVENT_POLL_HOOK
|
2015-11-30 12:29:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 23:16:26 -05:00
|
|
|
// Waits at most timeout milliseconds for UART flag to be set.
|
|
|
|
// Returns true if flag is/was set, false on timeout.
|
|
|
|
STATIC bool uart_wait_flag_set(pyb_uart_obj_t *self, uint32_t flag, uint32_t timeout) {
|
|
|
|
// Note: we don't use WFI to idle in this loop because UART tx doesn't generate
|
|
|
|
// an interrupt and the flag can be set quickly if the baudrate is large.
|
|
|
|
uint32_t start = HAL_GetTick();
|
|
|
|
for (;;) {
|
2018-12-09 18:44:52 -05:00
|
|
|
#if defined(STM32F4)
|
|
|
|
if (self->uartx->SR & flag) {
|
2016-12-22 23:16:26 -05:00
|
|
|
return true;
|
|
|
|
}
|
2018-12-09 18:44:52 -05:00
|
|
|
#else
|
|
|
|
if (self->uartx->ISR & flag) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
2016-12-22 23:16:26 -05:00
|
|
|
if (timeout == 0 || HAL_GetTick() - start >= timeout) {
|
|
|
|
return false; // timeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// src - a pointer to the data to send (16-bit aligned for 9-bit chars)
|
|
|
|
// num_chars - number of characters to send (9-bit chars count for 2 bytes from src)
|
|
|
|
// *errcode - returns 0 for success, MP_Exxx on error
|
|
|
|
// returns the number of characters sent (valid even if there was an error)
|
2018-12-07 02:36:43 -05:00
|
|
|
size_t uart_tx_data(pyb_uart_obj_t *self, const void *src_in, size_t num_chars, int *errcode) {
|
2016-12-22 23:16:26 -05:00
|
|
|
if (num_chars == 0) {
|
|
|
|
*errcode = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t timeout;
|
2018-12-09 18:44:52 -05:00
|
|
|
if (self->uartx->CR3 & USART_CR3_CTSE) {
|
2016-03-25 06:20:12 -04:00
|
|
|
// CTS can hold off transmission for an arbitrarily long time. Apply
|
|
|
|
// the overall timeout rather than the character timeout.
|
2016-12-22 23:16:26 -05:00
|
|
|
timeout = self->timeout;
|
|
|
|
} else {
|
|
|
|
// The timeout specified here is for waiting for the TX data register to
|
|
|
|
// become empty (ie between chars), as well as for the final char to be
|
|
|
|
// completely transferred. The default value for timeout_char is long
|
|
|
|
// enough for 1 char, but we need to double it to wait for the last char
|
|
|
|
// to be transferred to the data register, and then to be transmitted.
|
|
|
|
timeout = 2 * self->timeout_char;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *src = (const uint8_t*)src_in;
|
|
|
|
size_t num_tx = 0;
|
2018-12-09 18:44:52 -05:00
|
|
|
USART_TypeDef *uart = self->uartx;
|
2016-12-22 23:16:26 -05:00
|
|
|
|
|
|
|
while (num_tx < num_chars) {
|
|
|
|
if (!uart_wait_flag_set(self, UART_FLAG_TXE, timeout)) {
|
|
|
|
*errcode = MP_ETIMEDOUT;
|
|
|
|
return num_tx;
|
|
|
|
}
|
|
|
|
uint32_t data;
|
|
|
|
if (self->char_width == CHAR_WIDTH_9BIT) {
|
|
|
|
data = *((uint16_t*)src) & 0x1ff;
|
|
|
|
src += 2;
|
|
|
|
} else {
|
|
|
|
data = *src++;
|
|
|
|
}
|
2018-03-16 19:42:50 -04:00
|
|
|
#if defined(STM32F4)
|
2016-12-22 23:16:26 -05:00
|
|
|
uart->DR = data;
|
|
|
|
#else
|
|
|
|
uart->TDR = data;
|
|
|
|
#endif
|
|
|
|
++num_tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for the UART frame to complete
|
|
|
|
if (!uart_wait_flag_set(self, UART_FLAG_TC, timeout)) {
|
|
|
|
*errcode = MP_ETIMEDOUT;
|
|
|
|
return num_tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
*errcode = 0;
|
|
|
|
return num_tx;
|
2015-11-30 12:29:52 -05:00
|
|
|
}
|
|
|
|
|
2014-04-21 07:03:09 -04:00
|
|
|
void uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len) {
|
2016-12-22 23:16:26 -05:00
|
|
|
int errcode;
|
|
|
|
uart_tx_data(uart_obj, str, len, &errcode);
|
2014-03-12 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2014-10-11 12:57:10 -04:00
|
|
|
// this IRQ handler is set up to handle RXNE interrupts only
|
|
|
|
void uart_irq_handler(mp_uint_t uart_id) {
|
|
|
|
// get the uart object
|
2015-01-07 18:38:50 -05:00
|
|
|
pyb_uart_obj_t *self = MP_STATE_PORT(pyb_uart_obj_all)[uart_id - 1];
|
2014-10-11 12:57:10 -04:00
|
|
|
|
|
|
|
if (self == NULL) {
|
|
|
|
// UART object has not been set, so we can't do anything, not
|
|
|
|
// even disable the IRQ. This should never happen.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-09 18:44:52 -05:00
|
|
|
if (UART_RXNE_IS_SET(self->uartx)) {
|
2014-10-11 12:57:10 -04:00
|
|
|
if (self->read_buf_len != 0) {
|
|
|
|
uint16_t next_head = (self->read_buf_head + 1) % self->read_buf_len;
|
|
|
|
if (next_head != self->read_buf_tail) {
|
2016-03-25 06:20:12 -04:00
|
|
|
// only read data if room in buf
|
2018-05-28 04:10:53 -04:00
|
|
|
#if defined(STM32F0) || defined(STM32F7) || defined(STM32L4) || defined(STM32H7)
|
2018-12-09 18:44:52 -05:00
|
|
|
int data = self->uartx->RDR; // clears UART_FLAG_RXNE
|
2018-12-29 08:59:16 -05:00
|
|
|
self->uartx->ICR = USART_ICR_ORECF; // clear ORE if it was set
|
2016-03-25 06:20:12 -04:00
|
|
|
#else
|
2018-12-09 18:44:52 -05:00
|
|
|
int data = self->uartx->DR; // clears UART_FLAG_RXNE
|
2016-03-25 06:20:12 -04:00
|
|
|
#endif
|
|
|
|
data &= self->char_mask;
|
2018-04-23 06:44:30 -04:00
|
|
|
if (self->attached_to_repl && data == mp_interrupt_char) {
|
2018-12-29 09:03:22 -05:00
|
|
|
// Handle interrupt coming in on a UART REPL
|
2018-04-23 03:06:40 -04:00
|
|
|
pendsv_kbd_intr();
|
2014-10-11 12:57:10 -04:00
|
|
|
} else {
|
2018-12-29 09:03:22 -05:00
|
|
|
if (self->char_width == CHAR_WIDTH_9BIT) {
|
|
|
|
((uint16_t*)self->read_buf)[self->read_buf_head] = data;
|
|
|
|
} else {
|
|
|
|
self->read_buf[self->read_buf_head] = data;
|
|
|
|
}
|
|
|
|
self->read_buf_head = next_head;
|
2014-10-11 12:57:10 -04:00
|
|
|
}
|
2016-03-25 06:20:12 -04:00
|
|
|
} else { // No room: leave char in buf, disable interrupt
|
2018-12-09 18:44:52 -05:00
|
|
|
UART_RXNE_IT_DIS(self->uartx);
|
2014-10-11 12:57:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-28 21:56:24 -04:00
|
|
|
// If RXNE is clear but ORE set then clear the ORE flag (it's tied to RXNE IRQ)
|
|
|
|
#if defined(STM32F4)
|
|
|
|
else if (self->uartx->SR & USART_SR_ORE) {
|
|
|
|
(void)self->uartx->DR;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
else if (self->uartx->ISR & USART_ISR_ORE) {
|
|
|
|
self->uartx->ICR = USART_ICR_ORECF;
|
|
|
|
}
|
|
|
|
#endif
|
2018-12-09 11:07:56 -05:00
|
|
|
|
|
|
|
// Set user IRQ flags
|
|
|
|
self->mp_irq_flags = 0;
|
|
|
|
#if defined(STM32F4)
|
|
|
|
if (self->uartx->SR & USART_SR_IDLE) {
|
|
|
|
(void)self->uartx->SR;
|
|
|
|
(void)self->uartx->DR;
|
|
|
|
self->mp_irq_flags |= UART_FLAG_IDLE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (self->uartx->ISR & USART_ISR_IDLE) {
|
|
|
|
self->uartx->ICR = USART_ICR_IDLECF;
|
|
|
|
self->mp_irq_flags |= UART_FLAG_IDLE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Check the flags to see if the user handler should be called
|
|
|
|
if (self->mp_irq_trigger & self->mp_irq_flags) {
|
|
|
|
mp_irq_handler(self->mp_irq_obj);
|
|
|
|
}
|
2014-10-11 12:57:10 -04:00
|
|
|
}
|