Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lypinator 0:bb348c97df44 1 /* mbed Microcontroller Library
lypinator 0:bb348c97df44 2 *******************************************************************************
lypinator 0:bb348c97df44 3 * Copyright (c) 2017, STMicroelectronics
lypinator 0:bb348c97df44 4 * All rights reserved.
lypinator 0:bb348c97df44 5 *
lypinator 0:bb348c97df44 6 * Redistribution and use in source and binary forms, with or without
lypinator 0:bb348c97df44 7 * modification, are permitted provided that the following conditions are met:
lypinator 0:bb348c97df44 8 *
lypinator 0:bb348c97df44 9 * 1. Redistributions of source code must retain the above copyright notice,
lypinator 0:bb348c97df44 10 * this list of conditions and the following disclaimer.
lypinator 0:bb348c97df44 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
lypinator 0:bb348c97df44 12 * this list of conditions and the following disclaimer in the documentation
lypinator 0:bb348c97df44 13 * and/or other materials provided with the distribution.
lypinator 0:bb348c97df44 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
lypinator 0:bb348c97df44 15 * may be used to endorse or promote products derived from this software
lypinator 0:bb348c97df44 16 * without specific prior written permission.
lypinator 0:bb348c97df44 17 *
lypinator 0:bb348c97df44 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
lypinator 0:bb348c97df44 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
lypinator 0:bb348c97df44 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
lypinator 0:bb348c97df44 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
lypinator 0:bb348c97df44 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
lypinator 0:bb348c97df44 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
lypinator 0:bb348c97df44 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
lypinator 0:bb348c97df44 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
lypinator 0:bb348c97df44 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
lypinator 0:bb348c97df44 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
lypinator 0:bb348c97df44 28 *******************************************************************************
lypinator 0:bb348c97df44 29 */
lypinator 0:bb348c97df44 30
lypinator 0:bb348c97df44 31 #if DEVICE_SERIAL
lypinator 0:bb348c97df44 32
lypinator 0:bb348c97df44 33 #include "serial_api_hal.h"
lypinator 0:bb348c97df44 34
lypinator 0:bb348c97df44 35 // Possible choices of the LPUART_CLOCK_SOURCE configuration set in json file
lypinator 0:bb348c97df44 36 #define USE_LPUART_CLK_LSE 0x01
lypinator 0:bb348c97df44 37 #define USE_LPUART_CLK_PCLK1 0x02
lypinator 0:bb348c97df44 38 #define USE_LPUART_CLK_HSI 0x04
lypinator 0:bb348c97df44 39
lypinator 0:bb348c97df44 40 int stdio_uart_inited = 0; // used in platform/mbed_board.c and platform/mbed_retarget.cpp
lypinator 0:bb348c97df44 41 serial_t stdio_uart;
lypinator 0:bb348c97df44 42
lypinator 0:bb348c97df44 43 extern UART_HandleTypeDef uart_handlers[];
lypinator 0:bb348c97df44 44 extern uint32_t serial_irq_ids[];
lypinator 0:bb348c97df44 45
lypinator 0:bb348c97df44 46 // Utility functions
lypinator 0:bb348c97df44 47 HAL_StatusTypeDef init_uart(serial_t *obj);
lypinator 0:bb348c97df44 48 int8_t get_uart_index(UARTName uart_name);
lypinator 0:bb348c97df44 49
lypinator 0:bb348c97df44 50 void serial_init(serial_t *obj, PinName tx, PinName rx)
lypinator 0:bb348c97df44 51 {
lypinator 0:bb348c97df44 52 struct serial_s *obj_s = SERIAL_S(obj);
lypinator 0:bb348c97df44 53 uint8_t stdio_config = 0;
lypinator 0:bb348c97df44 54
lypinator 0:bb348c97df44 55 // Determine the UART to use (UART_1, UART_2, ...)
lypinator 0:bb348c97df44 56 UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
lypinator 0:bb348c97df44 57 UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
lypinator 0:bb348c97df44 58
lypinator 0:bb348c97df44 59 // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
lypinator 0:bb348c97df44 60 obj_s->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
lypinator 0:bb348c97df44 61 MBED_ASSERT(obj_s->uart != (UARTName)NC);
lypinator 0:bb348c97df44 62
lypinator 0:bb348c97df44 63 if ((tx == STDIO_UART_TX) || (rx == STDIO_UART_RX)) {
lypinator 0:bb348c97df44 64 stdio_config = 1;
lypinator 0:bb348c97df44 65 } else {
lypinator 0:bb348c97df44 66 if (uart_tx == pinmap_peripheral(STDIO_UART_TX, PinMap_UART_TX)) {
lypinator 0:bb348c97df44 67 error("Error: new serial object is using same UART as STDIO");
lypinator 0:bb348c97df44 68 }
lypinator 0:bb348c97df44 69 }
lypinator 0:bb348c97df44 70
lypinator 0:bb348c97df44 71 // Reset and enable clock
lypinator 0:bb348c97df44 72 #if defined(USART1_BASE)
lypinator 0:bb348c97df44 73 if (obj_s->uart == UART_1) {
lypinator 0:bb348c97df44 74 __HAL_RCC_USART1_CLK_ENABLE();
lypinator 0:bb348c97df44 75 }
lypinator 0:bb348c97df44 76 #endif
lypinator 0:bb348c97df44 77
lypinator 0:bb348c97df44 78 #if defined (USART2_BASE)
lypinator 0:bb348c97df44 79 if (obj_s->uart == UART_2) {
lypinator 0:bb348c97df44 80 __HAL_RCC_USART2_CLK_ENABLE();
lypinator 0:bb348c97df44 81 }
lypinator 0:bb348c97df44 82 #endif
lypinator 0:bb348c97df44 83
lypinator 0:bb348c97df44 84 #if defined(USART3_BASE)
lypinator 0:bb348c97df44 85 if (obj_s->uart == UART_3) {
lypinator 0:bb348c97df44 86 __HAL_RCC_USART3_CLK_ENABLE();
lypinator 0:bb348c97df44 87 }
lypinator 0:bb348c97df44 88 #endif
lypinator 0:bb348c97df44 89
lypinator 0:bb348c97df44 90 #if defined(UART4_BASE)
lypinator 0:bb348c97df44 91 if (obj_s->uart == UART_4) {
lypinator 0:bb348c97df44 92 __HAL_RCC_UART4_CLK_ENABLE();
lypinator 0:bb348c97df44 93 }
lypinator 0:bb348c97df44 94 #endif
lypinator 0:bb348c97df44 95
lypinator 0:bb348c97df44 96 #if defined(USART4_BASE)
lypinator 0:bb348c97df44 97 if (obj_s->uart == UART_4) {
lypinator 0:bb348c97df44 98 __HAL_RCC_USART4_CLK_ENABLE();
lypinator 0:bb348c97df44 99 }
lypinator 0:bb348c97df44 100 #endif
lypinator 0:bb348c97df44 101
lypinator 0:bb348c97df44 102 #if defined(UART5_BASE)
lypinator 0:bb348c97df44 103 if (obj_s->uart == UART_5) {
lypinator 0:bb348c97df44 104 __HAL_RCC_UART5_CLK_ENABLE();
lypinator 0:bb348c97df44 105 }
lypinator 0:bb348c97df44 106 #endif
lypinator 0:bb348c97df44 107
lypinator 0:bb348c97df44 108 #if defined(USART5_BASE)
lypinator 0:bb348c97df44 109 if (obj_s->uart == UART_5) {
lypinator 0:bb348c97df44 110 __HAL_RCC_USART5_CLK_ENABLE();
lypinator 0:bb348c97df44 111 }
lypinator 0:bb348c97df44 112 #endif
lypinator 0:bb348c97df44 113
lypinator 0:bb348c97df44 114 #if defined(USART6_BASE)
lypinator 0:bb348c97df44 115 if (obj_s->uart == UART_6) {
lypinator 0:bb348c97df44 116 __HAL_RCC_USART6_CLK_ENABLE();
lypinator 0:bb348c97df44 117 }
lypinator 0:bb348c97df44 118 #endif
lypinator 0:bb348c97df44 119
lypinator 0:bb348c97df44 120 #if defined(UART7_BASE)
lypinator 0:bb348c97df44 121 if (obj_s->uart == UART_7) {
lypinator 0:bb348c97df44 122 __HAL_RCC_UART7_CLK_ENABLE();
lypinator 0:bb348c97df44 123 }
lypinator 0:bb348c97df44 124 #endif
lypinator 0:bb348c97df44 125
lypinator 0:bb348c97df44 126 #if defined(USART7_BASE)
lypinator 0:bb348c97df44 127 if (obj_s->uart == UART_7) {
lypinator 0:bb348c97df44 128 __HAL_RCC_USART7_CLK_ENABLE();
lypinator 0:bb348c97df44 129 }
lypinator 0:bb348c97df44 130 #endif
lypinator 0:bb348c97df44 131
lypinator 0:bb348c97df44 132 #if defined(UART8_BASE)
lypinator 0:bb348c97df44 133 if (obj_s->uart == UART_8) {
lypinator 0:bb348c97df44 134 __HAL_RCC_UART8_CLK_ENABLE();
lypinator 0:bb348c97df44 135 }
lypinator 0:bb348c97df44 136 #endif
lypinator 0:bb348c97df44 137
lypinator 0:bb348c97df44 138 #if defined(USART8_BASE)
lypinator 0:bb348c97df44 139 if (obj_s->uart == UART_8) {
lypinator 0:bb348c97df44 140 __HAL_RCC_USART8_CLK_ENABLE();
lypinator 0:bb348c97df44 141 }
lypinator 0:bb348c97df44 142 #endif
lypinator 0:bb348c97df44 143
lypinator 0:bb348c97df44 144 #if defined(UART9_BASE)
lypinator 0:bb348c97df44 145 if (obj_s->uart == UART_9) {
lypinator 0:bb348c97df44 146 __HAL_RCC_UART9_CLK_ENABLE();
lypinator 0:bb348c97df44 147 }
lypinator 0:bb348c97df44 148 #endif
lypinator 0:bb348c97df44 149
lypinator 0:bb348c97df44 150 #if defined(UART10_BASE)
lypinator 0:bb348c97df44 151 if (obj_s->uart == UART_10) {
lypinator 0:bb348c97df44 152 __HAL_RCC_UART10_CLK_ENABLE();
lypinator 0:bb348c97df44 153 }
lypinator 0:bb348c97df44 154 #endif
lypinator 0:bb348c97df44 155
lypinator 0:bb348c97df44 156 #if defined(LPUART1_BASE)
lypinator 0:bb348c97df44 157 if (obj_s->uart == LPUART_1) {
lypinator 0:bb348c97df44 158 __HAL_RCC_LPUART1_CLK_ENABLE();
lypinator 0:bb348c97df44 159 }
lypinator 0:bb348c97df44 160 #endif
lypinator 0:bb348c97df44 161
lypinator 0:bb348c97df44 162 // Assign serial object index
lypinator 0:bb348c97df44 163 obj_s->index = get_uart_index(obj_s->uart);
lypinator 0:bb348c97df44 164 MBED_ASSERT(obj_s->index >= 0);
lypinator 0:bb348c97df44 165
lypinator 0:bb348c97df44 166 // Configure UART pins
lypinator 0:bb348c97df44 167 pinmap_pinout(tx, PinMap_UART_TX);
lypinator 0:bb348c97df44 168 pinmap_pinout(rx, PinMap_UART_RX);
lypinator 0:bb348c97df44 169
lypinator 0:bb348c97df44 170 if (tx != NC) {
lypinator 0:bb348c97df44 171 pin_mode(tx, PullUp);
lypinator 0:bb348c97df44 172 }
lypinator 0:bb348c97df44 173 if (rx != NC) {
lypinator 0:bb348c97df44 174 pin_mode(rx, PullUp);
lypinator 0:bb348c97df44 175 }
lypinator 0:bb348c97df44 176
lypinator 0:bb348c97df44 177 // Configure UART
lypinator 0:bb348c97df44 178 obj_s->baudrate = 9600; // baudrate default value
lypinator 0:bb348c97df44 179 if (stdio_config) {
lypinator 0:bb348c97df44 180 #if MBED_CONF_PLATFORM_STDIO_BAUD_RATE
lypinator 0:bb348c97df44 181 obj_s->baudrate = MBED_CONF_PLATFORM_STDIO_BAUD_RATE; // baudrate takes value from platform/mbed_lib.json
lypinator 0:bb348c97df44 182 #endif /* MBED_CONF_PLATFORM_STDIO_BAUD_RATE */
lypinator 0:bb348c97df44 183 } else {
lypinator 0:bb348c97df44 184 #if MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE
lypinator 0:bb348c97df44 185 obj_s->baudrate = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE; // baudrate takes value from platform/mbed_lib.json
lypinator 0:bb348c97df44 186 #endif /* MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE */
lypinator 0:bb348c97df44 187 }
lypinator 0:bb348c97df44 188 obj_s->databits = UART_WORDLENGTH_8B;
lypinator 0:bb348c97df44 189 obj_s->stopbits = UART_STOPBITS_1;
lypinator 0:bb348c97df44 190 obj_s->parity = UART_PARITY_NONE;
lypinator 0:bb348c97df44 191
lypinator 0:bb348c97df44 192 #if DEVICE_SERIAL_FC
lypinator 0:bb348c97df44 193 obj_s->hw_flow_ctl = UART_HWCONTROL_NONE;
lypinator 0:bb348c97df44 194 #endif
lypinator 0:bb348c97df44 195
lypinator 0:bb348c97df44 196 obj_s->pin_tx = tx;
lypinator 0:bb348c97df44 197 obj_s->pin_rx = rx;
lypinator 0:bb348c97df44 198
lypinator 0:bb348c97df44 199 init_uart(obj); /* init_uart will be called again in serial_baud function, so don't worry if init_uart returns HAL_ERROR */
lypinator 0:bb348c97df44 200
lypinator 0:bb348c97df44 201 // For stdio management in platform/mbed_board.c and platform/mbed_retarget.cpp
lypinator 0:bb348c97df44 202 if (stdio_config) {
lypinator 0:bb348c97df44 203 stdio_uart_inited = 1;
lypinator 0:bb348c97df44 204 memcpy(&stdio_uart, obj, sizeof(serial_t));
lypinator 0:bb348c97df44 205 }
lypinator 0:bb348c97df44 206 }
lypinator 0:bb348c97df44 207
lypinator 0:bb348c97df44 208 void serial_free(serial_t *obj)
lypinator 0:bb348c97df44 209 {
lypinator 0:bb348c97df44 210 struct serial_s *obj_s = SERIAL_S(obj);
lypinator 0:bb348c97df44 211
lypinator 0:bb348c97df44 212 // Reset UART and disable clock
lypinator 0:bb348c97df44 213 #if defined(USART1_BASE)
lypinator 0:bb348c97df44 214 if (obj_s->uart == UART_1) {
lypinator 0:bb348c97df44 215 __HAL_RCC_USART1_FORCE_RESET();
lypinator 0:bb348c97df44 216 __HAL_RCC_USART1_RELEASE_RESET();
lypinator 0:bb348c97df44 217 __HAL_RCC_USART1_CLK_DISABLE();
lypinator 0:bb348c97df44 218 }
lypinator 0:bb348c97df44 219 #endif
lypinator 0:bb348c97df44 220
lypinator 0:bb348c97df44 221 #if defined(USART2_BASE)
lypinator 0:bb348c97df44 222 if (obj_s->uart == UART_2) {
lypinator 0:bb348c97df44 223 __HAL_RCC_USART2_FORCE_RESET();
lypinator 0:bb348c97df44 224 __HAL_RCC_USART2_RELEASE_RESET();
lypinator 0:bb348c97df44 225 __HAL_RCC_USART2_CLK_DISABLE();
lypinator 0:bb348c97df44 226 }
lypinator 0:bb348c97df44 227 #endif
lypinator 0:bb348c97df44 228
lypinator 0:bb348c97df44 229 #if defined(USART3_BASE)
lypinator 0:bb348c97df44 230 if (obj_s->uart == UART_3) {
lypinator 0:bb348c97df44 231 __HAL_RCC_USART3_FORCE_RESET();
lypinator 0:bb348c97df44 232 __HAL_RCC_USART3_RELEASE_RESET();
lypinator 0:bb348c97df44 233 __HAL_RCC_USART3_CLK_DISABLE();
lypinator 0:bb348c97df44 234 }
lypinator 0:bb348c97df44 235 #endif
lypinator 0:bb348c97df44 236
lypinator 0:bb348c97df44 237 #if defined(UART4_BASE)
lypinator 0:bb348c97df44 238 if (obj_s->uart == UART_4) {
lypinator 0:bb348c97df44 239 __HAL_RCC_UART4_FORCE_RESET();
lypinator 0:bb348c97df44 240 __HAL_RCC_UART4_RELEASE_RESET();
lypinator 0:bb348c97df44 241 __HAL_RCC_UART4_CLK_DISABLE();
lypinator 0:bb348c97df44 242 }
lypinator 0:bb348c97df44 243 #endif
lypinator 0:bb348c97df44 244
lypinator 0:bb348c97df44 245 #if defined(USART4_BASE)
lypinator 0:bb348c97df44 246 if (obj_s->uart == UART_4) {
lypinator 0:bb348c97df44 247 __HAL_RCC_USART4_FORCE_RESET();
lypinator 0:bb348c97df44 248 __HAL_RCC_USART4_RELEASE_RESET();
lypinator 0:bb348c97df44 249 __HAL_RCC_USART4_CLK_DISABLE();
lypinator 0:bb348c97df44 250 }
lypinator 0:bb348c97df44 251 #endif
lypinator 0:bb348c97df44 252
lypinator 0:bb348c97df44 253 #if defined(UART5_BASE)
lypinator 0:bb348c97df44 254 if (obj_s->uart == UART_5) {
lypinator 0:bb348c97df44 255 __HAL_RCC_UART5_FORCE_RESET();
lypinator 0:bb348c97df44 256 __HAL_RCC_UART5_RELEASE_RESET();
lypinator 0:bb348c97df44 257 __HAL_RCC_UART5_CLK_DISABLE();
lypinator 0:bb348c97df44 258 }
lypinator 0:bb348c97df44 259 #endif
lypinator 0:bb348c97df44 260
lypinator 0:bb348c97df44 261 #if defined(USART5_BASE)
lypinator 0:bb348c97df44 262 if (obj_s->uart == UART_5) {
lypinator 0:bb348c97df44 263 __HAL_RCC_USART5_FORCE_RESET();
lypinator 0:bb348c97df44 264 __HAL_RCC_USART5_RELEASE_RESET();
lypinator 0:bb348c97df44 265 __HAL_RCC_USART5_CLK_DISABLE();
lypinator 0:bb348c97df44 266 }
lypinator 0:bb348c97df44 267 #endif
lypinator 0:bb348c97df44 268
lypinator 0:bb348c97df44 269 #if defined(USART6_BASE)
lypinator 0:bb348c97df44 270 if (obj_s->uart == UART_6) {
lypinator 0:bb348c97df44 271 __HAL_RCC_USART6_FORCE_RESET();
lypinator 0:bb348c97df44 272 __HAL_RCC_USART6_RELEASE_RESET();
lypinator 0:bb348c97df44 273 __HAL_RCC_USART6_CLK_DISABLE();
lypinator 0:bb348c97df44 274 }
lypinator 0:bb348c97df44 275 #endif
lypinator 0:bb348c97df44 276
lypinator 0:bb348c97df44 277 #if defined(UART7_BASE)
lypinator 0:bb348c97df44 278 if (obj_s->uart == UART_7) {
lypinator 0:bb348c97df44 279 __HAL_RCC_UART7_FORCE_RESET();
lypinator 0:bb348c97df44 280 __HAL_RCC_UART7_RELEASE_RESET();
lypinator 0:bb348c97df44 281 __HAL_RCC_UART7_CLK_DISABLE();
lypinator 0:bb348c97df44 282 }
lypinator 0:bb348c97df44 283 #endif
lypinator 0:bb348c97df44 284
lypinator 0:bb348c97df44 285 #if defined(USART7_BASE)
lypinator 0:bb348c97df44 286 if (obj_s->uart == UART_7) {
lypinator 0:bb348c97df44 287 __HAL_RCC_USART7_FORCE_RESET();
lypinator 0:bb348c97df44 288 __HAL_RCC_USART7_RELEASE_RESET();
lypinator 0:bb348c97df44 289 __HAL_RCC_USART7_CLK_DISABLE();
lypinator 0:bb348c97df44 290 }
lypinator 0:bb348c97df44 291 #endif
lypinator 0:bb348c97df44 292
lypinator 0:bb348c97df44 293 #if defined(UART8_BASE)
lypinator 0:bb348c97df44 294 if (obj_s->uart == UART_8) {
lypinator 0:bb348c97df44 295 __HAL_RCC_UART8_FORCE_RESET();
lypinator 0:bb348c97df44 296 __HAL_RCC_UART8_RELEASE_RESET();
lypinator 0:bb348c97df44 297 __HAL_RCC_UART8_CLK_DISABLE();
lypinator 0:bb348c97df44 298 }
lypinator 0:bb348c97df44 299 #endif
lypinator 0:bb348c97df44 300
lypinator 0:bb348c97df44 301 #if defined(USART8_BASE)
lypinator 0:bb348c97df44 302 if (obj_s->uart == UART_8) {
lypinator 0:bb348c97df44 303 __HAL_RCC_USART8_FORCE_RESET();
lypinator 0:bb348c97df44 304 __HAL_RCC_USART8_RELEASE_RESET();
lypinator 0:bb348c97df44 305 __HAL_RCC_USART8_CLK_DISABLE();
lypinator 0:bb348c97df44 306 }
lypinator 0:bb348c97df44 307 #endif
lypinator 0:bb348c97df44 308
lypinator 0:bb348c97df44 309 #if defined(UART9_BASE)
lypinator 0:bb348c97df44 310 if (obj_s->uart == UART_9) {
lypinator 0:bb348c97df44 311 __HAL_RCC_UART9_FORCE_RESET();
lypinator 0:bb348c97df44 312 __HAL_RCC_UART9_RELEASE_RESET();
lypinator 0:bb348c97df44 313 __HAL_RCC_UART9_CLK_DISABLE();
lypinator 0:bb348c97df44 314 }
lypinator 0:bb348c97df44 315 #endif
lypinator 0:bb348c97df44 316
lypinator 0:bb348c97df44 317 #if defined(UART10_BASE)
lypinator 0:bb348c97df44 318 if (obj_s->uart == UART_10) {
lypinator 0:bb348c97df44 319 __HAL_RCC_UART10_FORCE_RESET();
lypinator 0:bb348c97df44 320 __HAL_RCC_UART10_RELEASE_RESET();
lypinator 0:bb348c97df44 321 __HAL_RCC_UART10_CLK_DISABLE();
lypinator 0:bb348c97df44 322 }
lypinator 0:bb348c97df44 323 #endif
lypinator 0:bb348c97df44 324
lypinator 0:bb348c97df44 325 #if defined(LPUART1_BASE)
lypinator 0:bb348c97df44 326 if (obj_s->uart == LPUART_1) {
lypinator 0:bb348c97df44 327 __HAL_RCC_LPUART1_FORCE_RESET();
lypinator 0:bb348c97df44 328 __HAL_RCC_LPUART1_RELEASE_RESET();
lypinator 0:bb348c97df44 329 __HAL_RCC_LPUART1_CLK_DISABLE();
lypinator 0:bb348c97df44 330 }
lypinator 0:bb348c97df44 331 #endif
lypinator 0:bb348c97df44 332
lypinator 0:bb348c97df44 333 // Configure GPIOs
lypinator 0:bb348c97df44 334 pin_function(obj_s->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
lypinator 0:bb348c97df44 335 pin_function(obj_s->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
lypinator 0:bb348c97df44 336
lypinator 0:bb348c97df44 337 serial_irq_ids[obj_s->index] = 0;
lypinator 0:bb348c97df44 338 }
lypinator 0:bb348c97df44 339
lypinator 0:bb348c97df44 340 void serial_baud(serial_t *obj, int baudrate)
lypinator 0:bb348c97df44 341 {
lypinator 0:bb348c97df44 342 struct serial_s *obj_s = SERIAL_S(obj);
lypinator 0:bb348c97df44 343
lypinator 0:bb348c97df44 344 obj_s->baudrate = baudrate;
lypinator 0:bb348c97df44 345
lypinator 0:bb348c97df44 346 #if defined(LPUART1_BASE)
lypinator 0:bb348c97df44 347 /* Note that LPUART clock source must be in the range [3 x baud rate, 4096 x baud rate], check Ref Manual */
lypinator 0:bb348c97df44 348 if (obj_s->uart == LPUART_1) {
lypinator 0:bb348c97df44 349 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
lypinator 0:bb348c97df44 350 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
lypinator 0:bb348c97df44 351 #if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE) & USE_LPUART_CLK_LSE)
lypinator 0:bb348c97df44 352 if (baudrate <= 9600) {
lypinator 0:bb348c97df44 353 // Enable LSE in case it is not already done
lypinator 0:bb348c97df44 354 if (!__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY)) {
lypinator 0:bb348c97df44 355 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
lypinator 0:bb348c97df44 356 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
lypinator 0:bb348c97df44 357 RCC_OscInitStruct.HSIState = RCC_LSE_ON;
lypinator 0:bb348c97df44 358 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_OFF;
lypinator 0:bb348c97df44 359 HAL_RCC_OscConfig(&RCC_OscInitStruct);
lypinator 0:bb348c97df44 360 }
lypinator 0:bb348c97df44 361 // Keep it to verify if HAL_RCC_OscConfig didn't exit with a timeout
lypinator 0:bb348c97df44 362 if (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY)) {
lypinator 0:bb348c97df44 363 PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_LSE;
lypinator 0:bb348c97df44 364 HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
lypinator 0:bb348c97df44 365 if (init_uart(obj) == HAL_OK) {
lypinator 0:bb348c97df44 366 return;
lypinator 0:bb348c97df44 367 }
lypinator 0:bb348c97df44 368 }
lypinator 0:bb348c97df44 369 }
lypinator 0:bb348c97df44 370 #endif
lypinator 0:bb348c97df44 371 #if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE) & USE_LPUART_CLK_PCLK1)
lypinator 0:bb348c97df44 372 PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
lypinator 0:bb348c97df44 373 HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
lypinator 0:bb348c97df44 374 if (init_uart(obj) == HAL_OK) {
lypinator 0:bb348c97df44 375 return;
lypinator 0:bb348c97df44 376 }
lypinator 0:bb348c97df44 377 #endif
lypinator 0:bb348c97df44 378 #if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE) & USE_LPUART_CLK_HSI)
lypinator 0:bb348c97df44 379 // Enable HSI in case it is not already done
lypinator 0:bb348c97df44 380 if (!__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY)) {
lypinator 0:bb348c97df44 381 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
lypinator 0:bb348c97df44 382 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
lypinator 0:bb348c97df44 383 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
lypinator 0:bb348c97df44 384 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_OFF;
lypinator 0:bb348c97df44 385 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
lypinator 0:bb348c97df44 386 HAL_RCC_OscConfig(&RCC_OscInitStruct);
lypinator 0:bb348c97df44 387 }
lypinator 0:bb348c97df44 388 // Keep it to verify if HAL_RCC_OscConfig didn't exit with a timeout
lypinator 0:bb348c97df44 389 if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY)) {
lypinator 0:bb348c97df44 390 PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_HSI;
lypinator 0:bb348c97df44 391 HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
lypinator 0:bb348c97df44 392 if (init_uart(obj) == HAL_OK) {
lypinator 0:bb348c97df44 393 return;
lypinator 0:bb348c97df44 394 }
lypinator 0:bb348c97df44 395 }
lypinator 0:bb348c97df44 396 #endif
lypinator 0:bb348c97df44 397 // Last chance using SYSCLK
lypinator 0:bb348c97df44 398 PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_SYSCLK;
lypinator 0:bb348c97df44 399 HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
lypinator 0:bb348c97df44 400 }
lypinator 0:bb348c97df44 401 #endif /* LPUART1_BASE */
lypinator 0:bb348c97df44 402
lypinator 0:bb348c97df44 403 if (init_uart(obj) != HAL_OK) {
lypinator 0:bb348c97df44 404 debug("Cannot initialize UART with baud rate %u\n", baudrate);
lypinator 0:bb348c97df44 405 }
lypinator 0:bb348c97df44 406 }
lypinator 0:bb348c97df44 407
lypinator 0:bb348c97df44 408 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
lypinator 0:bb348c97df44 409 {
lypinator 0:bb348c97df44 410 struct serial_s *obj_s = SERIAL_S(obj);
lypinator 0:bb348c97df44 411
lypinator 0:bb348c97df44 412 switch (parity) {
lypinator 0:bb348c97df44 413 case ParityOdd:
lypinator 0:bb348c97df44 414 obj_s->parity = UART_PARITY_ODD;
lypinator 0:bb348c97df44 415 break;
lypinator 0:bb348c97df44 416 case ParityEven:
lypinator 0:bb348c97df44 417 obj_s->parity = UART_PARITY_EVEN;
lypinator 0:bb348c97df44 418 break;
lypinator 0:bb348c97df44 419 default: // ParityNone
lypinator 0:bb348c97df44 420 case ParityForced0: // unsupported!
lypinator 0:bb348c97df44 421 case ParityForced1: // unsupported!
lypinator 0:bb348c97df44 422 obj_s->parity = UART_PARITY_NONE;
lypinator 0:bb348c97df44 423 break;
lypinator 0:bb348c97df44 424 }
lypinator 0:bb348c97df44 425
lypinator 0:bb348c97df44 426 switch (data_bits) {
lypinator 0:bb348c97df44 427 case 7:
lypinator 0:bb348c97df44 428 if (parity != UART_PARITY_NONE) {
lypinator 0:bb348c97df44 429 obj_s->databits = UART_WORDLENGTH_8B;
lypinator 0:bb348c97df44 430 } else {
lypinator 0:bb348c97df44 431 #if defined UART_WORDLENGTH_7B
lypinator 0:bb348c97df44 432 obj_s->databits = UART_WORDLENGTH_7B;
lypinator 0:bb348c97df44 433 #else
lypinator 0:bb348c97df44 434 error("7-bit data format without parity is not supported");
lypinator 0:bb348c97df44 435 #endif
lypinator 0:bb348c97df44 436 }
lypinator 0:bb348c97df44 437 break;
lypinator 0:bb348c97df44 438 case 8:
lypinator 0:bb348c97df44 439 if (parity != UART_PARITY_NONE) {
lypinator 0:bb348c97df44 440 obj_s->databits = UART_WORDLENGTH_9B;
lypinator 0:bb348c97df44 441 } else {
lypinator 0:bb348c97df44 442 obj_s->databits = UART_WORDLENGTH_8B;
lypinator 0:bb348c97df44 443 }
lypinator 0:bb348c97df44 444 break;
lypinator 0:bb348c97df44 445 case 9:
lypinator 0:bb348c97df44 446 if (parity != UART_PARITY_NONE) {
lypinator 0:bb348c97df44 447 error("Parity is not supported with 9-bit data format");
lypinator 0:bb348c97df44 448 } else {
lypinator 0:bb348c97df44 449 obj_s->databits = UART_WORDLENGTH_9B;
lypinator 0:bb348c97df44 450 }
lypinator 0:bb348c97df44 451 break;
lypinator 0:bb348c97df44 452 default:
lypinator 0:bb348c97df44 453 error("Only 7, 8 or 9-bit data formats are supported");
lypinator 0:bb348c97df44 454 break;
lypinator 0:bb348c97df44 455 }
lypinator 0:bb348c97df44 456
lypinator 0:bb348c97df44 457 if (stop_bits == 2) {
lypinator 0:bb348c97df44 458 obj_s->stopbits = UART_STOPBITS_2;
lypinator 0:bb348c97df44 459 } else {
lypinator 0:bb348c97df44 460 obj_s->stopbits = UART_STOPBITS_1;
lypinator 0:bb348c97df44 461 }
lypinator 0:bb348c97df44 462
lypinator 0:bb348c97df44 463 init_uart(obj);
lypinator 0:bb348c97df44 464 }
lypinator 0:bb348c97df44 465
lypinator 0:bb348c97df44 466 /******************************************************************************
lypinator 0:bb348c97df44 467 * READ/WRITE
lypinator 0:bb348c97df44 468 ******************************************************************************/
lypinator 0:bb348c97df44 469
lypinator 0:bb348c97df44 470 int serial_readable(serial_t *obj)
lypinator 0:bb348c97df44 471 {
lypinator 0:bb348c97df44 472 struct serial_s *obj_s = SERIAL_S(obj);
lypinator 0:bb348c97df44 473 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
lypinator 0:bb348c97df44 474 /* To avoid a target blocking case, let's check for
lypinator 0:bb348c97df44 475 * possible OVERRUN error and discard it
lypinator 0:bb348c97df44 476 */
lypinator 0:bb348c97df44 477 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE)) {
lypinator 0:bb348c97df44 478 __HAL_UART_CLEAR_OREFLAG(huart);
lypinator 0:bb348c97df44 479 }
lypinator 0:bb348c97df44 480 // Check if data is received
lypinator 0:bb348c97df44 481 return (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) ? 1 : 0;
lypinator 0:bb348c97df44 482 }
lypinator 0:bb348c97df44 483
lypinator 0:bb348c97df44 484 int serial_writable(serial_t *obj)
lypinator 0:bb348c97df44 485 {
lypinator 0:bb348c97df44 486 struct serial_s *obj_s = SERIAL_S(obj);
lypinator 0:bb348c97df44 487 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
lypinator 0:bb348c97df44 488
lypinator 0:bb348c97df44 489 // Check if data is transmitted
lypinator 0:bb348c97df44 490 return (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) ? 1 : 0;
lypinator 0:bb348c97df44 491 }
lypinator 0:bb348c97df44 492
lypinator 0:bb348c97df44 493 void serial_pinout_tx(PinName tx)
lypinator 0:bb348c97df44 494 {
lypinator 0:bb348c97df44 495 pinmap_pinout(tx, PinMap_UART_TX);
lypinator 0:bb348c97df44 496 }
lypinator 0:bb348c97df44 497
lypinator 0:bb348c97df44 498 void serial_break_clear(serial_t *obj)
lypinator 0:bb348c97df44 499 {
lypinator 0:bb348c97df44 500 (void)obj;
lypinator 0:bb348c97df44 501 }
lypinator 0:bb348c97df44 502
lypinator 0:bb348c97df44 503 /******************************************************************************
lypinator 0:bb348c97df44 504 * UTILITY FUNCTIONS
lypinator 0:bb348c97df44 505 ******************************************************************************/
lypinator 0:bb348c97df44 506
lypinator 0:bb348c97df44 507 HAL_StatusTypeDef init_uart(serial_t *obj)
lypinator 0:bb348c97df44 508 {
lypinator 0:bb348c97df44 509 struct serial_s *obj_s = SERIAL_S(obj);
lypinator 0:bb348c97df44 510 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
lypinator 0:bb348c97df44 511 huart->Instance = (USART_TypeDef *)(obj_s->uart);
lypinator 0:bb348c97df44 512
lypinator 0:bb348c97df44 513 huart->Init.BaudRate = obj_s->baudrate;
lypinator 0:bb348c97df44 514 huart->Init.WordLength = obj_s->databits;
lypinator 0:bb348c97df44 515 huart->Init.StopBits = obj_s->stopbits;
lypinator 0:bb348c97df44 516 huart->Init.Parity = obj_s->parity;
lypinator 0:bb348c97df44 517 #if DEVICE_SERIAL_FC
lypinator 0:bb348c97df44 518 huart->Init.HwFlowCtl = obj_s->hw_flow_ctl;
lypinator 0:bb348c97df44 519 #else
lypinator 0:bb348c97df44 520 huart->Init.HwFlowCtl = UART_HWCONTROL_NONE;
lypinator 0:bb348c97df44 521 #endif
lypinator 0:bb348c97df44 522 huart->Init.OverSampling = UART_OVERSAMPLING_16;
lypinator 0:bb348c97df44 523 huart->TxXferCount = 0;
lypinator 0:bb348c97df44 524 huart->TxXferSize = 0;
lypinator 0:bb348c97df44 525 huart->RxXferCount = 0;
lypinator 0:bb348c97df44 526 huart->RxXferSize = 0;
lypinator 0:bb348c97df44 527
lypinator 0:bb348c97df44 528 if (obj_s->pin_rx == NC) {
lypinator 0:bb348c97df44 529 huart->Init.Mode = UART_MODE_TX;
lypinator 0:bb348c97df44 530 } else if (obj_s->pin_tx == NC) {
lypinator 0:bb348c97df44 531 huart->Init.Mode = UART_MODE_RX;
lypinator 0:bb348c97df44 532 } else {
lypinator 0:bb348c97df44 533 huart->Init.Mode = UART_MODE_TX_RX;
lypinator 0:bb348c97df44 534 }
lypinator 0:bb348c97df44 535
lypinator 0:bb348c97df44 536 #if defined(LPUART1_BASE)
lypinator 0:bb348c97df44 537 if (huart->Instance == LPUART1) {
lypinator 0:bb348c97df44 538 if (obj_s->baudrate <= 9600) {
lypinator 0:bb348c97df44 539 HAL_UARTEx_EnableClockStopMode(huart);
lypinator 0:bb348c97df44 540 HAL_UARTEx_EnableStopMode(huart);
lypinator 0:bb348c97df44 541 } else {
lypinator 0:bb348c97df44 542 HAL_UARTEx_DisableClockStopMode(huart);
lypinator 0:bb348c97df44 543 HAL_UARTEx_DisableStopMode(huart);
lypinator 0:bb348c97df44 544 }
lypinator 0:bb348c97df44 545 }
lypinator 0:bb348c97df44 546 #endif
lypinator 0:bb348c97df44 547
lypinator 0:bb348c97df44 548 return HAL_UART_Init(huart);
lypinator 0:bb348c97df44 549 }
lypinator 0:bb348c97df44 550
lypinator 0:bb348c97df44 551 int8_t get_uart_index(UARTName uart_name)
lypinator 0:bb348c97df44 552 {
lypinator 0:bb348c97df44 553 uint8_t index = 0;
lypinator 0:bb348c97df44 554
lypinator 0:bb348c97df44 555 #if defined(USART1_BASE)
lypinator 0:bb348c97df44 556 if (uart_name == UART_1) {
lypinator 0:bb348c97df44 557 return index;
lypinator 0:bb348c97df44 558 }
lypinator 0:bb348c97df44 559 index++;
lypinator 0:bb348c97df44 560 #endif
lypinator 0:bb348c97df44 561
lypinator 0:bb348c97df44 562 #if defined(USART2_BASE)
lypinator 0:bb348c97df44 563 if (uart_name == UART_2) {
lypinator 0:bb348c97df44 564 return index;
lypinator 0:bb348c97df44 565 }
lypinator 0:bb348c97df44 566 index++;
lypinator 0:bb348c97df44 567 #endif
lypinator 0:bb348c97df44 568
lypinator 0:bb348c97df44 569 #if defined(USART3_BASE)
lypinator 0:bb348c97df44 570 if (uart_name == UART_3) {
lypinator 0:bb348c97df44 571 return index;
lypinator 0:bb348c97df44 572 }
lypinator 0:bb348c97df44 573 index++;
lypinator 0:bb348c97df44 574 #endif
lypinator 0:bb348c97df44 575
lypinator 0:bb348c97df44 576 #if defined(UART4_BASE)
lypinator 0:bb348c97df44 577 if (uart_name == UART_4) {
lypinator 0:bb348c97df44 578 return index;
lypinator 0:bb348c97df44 579 }
lypinator 0:bb348c97df44 580 index++;
lypinator 0:bb348c97df44 581 #endif
lypinator 0:bb348c97df44 582
lypinator 0:bb348c97df44 583 #if defined(USART4_BASE)
lypinator 0:bb348c97df44 584 if (uart_name == UART_4) {
lypinator 0:bb348c97df44 585 return index;
lypinator 0:bb348c97df44 586 }
lypinator 0:bb348c97df44 587 index++;
lypinator 0:bb348c97df44 588 #endif
lypinator 0:bb348c97df44 589
lypinator 0:bb348c97df44 590 #if defined(UART5_BASE)
lypinator 0:bb348c97df44 591 if (uart_name == UART_5) {
lypinator 0:bb348c97df44 592 return index;
lypinator 0:bb348c97df44 593 }
lypinator 0:bb348c97df44 594 index++;
lypinator 0:bb348c97df44 595 #endif
lypinator 0:bb348c97df44 596
lypinator 0:bb348c97df44 597 #if defined(USART5_BASE)
lypinator 0:bb348c97df44 598 if (uart_name == UART_5) {
lypinator 0:bb348c97df44 599 return index;
lypinator 0:bb348c97df44 600 }
lypinator 0:bb348c97df44 601 index++;
lypinator 0:bb348c97df44 602 #endif
lypinator 0:bb348c97df44 603
lypinator 0:bb348c97df44 604 #if defined(USART6_BASE)
lypinator 0:bb348c97df44 605 if (uart_name == UART_6) {
lypinator 0:bb348c97df44 606 return index;
lypinator 0:bb348c97df44 607 }
lypinator 0:bb348c97df44 608 index++;
lypinator 0:bb348c97df44 609 #endif
lypinator 0:bb348c97df44 610
lypinator 0:bb348c97df44 611 #if defined(UART7_BASE)
lypinator 0:bb348c97df44 612 if (uart_name == UART_7) {
lypinator 0:bb348c97df44 613 return index;
lypinator 0:bb348c97df44 614 }
lypinator 0:bb348c97df44 615 index++;
lypinator 0:bb348c97df44 616 #endif
lypinator 0:bb348c97df44 617
lypinator 0:bb348c97df44 618 #if defined(USART7_BASE)
lypinator 0:bb348c97df44 619 if (uart_name == UART_7) {
lypinator 0:bb348c97df44 620 return index;
lypinator 0:bb348c97df44 621 }
lypinator 0:bb348c97df44 622 index++;
lypinator 0:bb348c97df44 623 #endif
lypinator 0:bb348c97df44 624
lypinator 0:bb348c97df44 625 #if defined(UART8_BASE)
lypinator 0:bb348c97df44 626 if (uart_name == UART_8) {
lypinator 0:bb348c97df44 627 return index;
lypinator 0:bb348c97df44 628 }
lypinator 0:bb348c97df44 629 index++;
lypinator 0:bb348c97df44 630 #endif
lypinator 0:bb348c97df44 631
lypinator 0:bb348c97df44 632 #if defined(USART8_BASE)
lypinator 0:bb348c97df44 633 if (uart_name == UART_8) {
lypinator 0:bb348c97df44 634 return index;
lypinator 0:bb348c97df44 635 }
lypinator 0:bb348c97df44 636 index++;
lypinator 0:bb348c97df44 637 #endif
lypinator 0:bb348c97df44 638
lypinator 0:bb348c97df44 639 #if defined(UART9_BASE)
lypinator 0:bb348c97df44 640 if (uart_name == UART_9) {
lypinator 0:bb348c97df44 641 return index;
lypinator 0:bb348c97df44 642 }
lypinator 0:bb348c97df44 643 index++;
lypinator 0:bb348c97df44 644 #endif
lypinator 0:bb348c97df44 645
lypinator 0:bb348c97df44 646 #if defined(UART10_BASE)
lypinator 0:bb348c97df44 647 if (uart_name == UART_10) {
lypinator 0:bb348c97df44 648 return index;
lypinator 0:bb348c97df44 649 }
lypinator 0:bb348c97df44 650 index++;
lypinator 0:bb348c97df44 651 #endif
lypinator 0:bb348c97df44 652
lypinator 0:bb348c97df44 653 #if defined(LPUART1_BASE)
lypinator 0:bb348c97df44 654 if (uart_name == LPUART_1) {
lypinator 0:bb348c97df44 655 return index;
lypinator 0:bb348c97df44 656 }
lypinator 0:bb348c97df44 657 index++;
lypinator 0:bb348c97df44 658 #endif
lypinator 0:bb348c97df44 659
lypinator 0:bb348c97df44 660 return -1;
lypinator 0:bb348c97df44 661 }
lypinator 0:bb348c97df44 662
lypinator 0:bb348c97df44 663 /* Function to protect deep sleep while a seral Tx is ongoing on not complete
lypinator 0:bb348c97df44 664 * yet. Returns 1 if there is at least 1 serial instance with ongoing ransfer
lypinator 0:bb348c97df44 665 * 0 otherwise.
lypinator 0:bb348c97df44 666 */
lypinator 0:bb348c97df44 667 int serial_is_tx_ongoing(void) {
lypinator 0:bb348c97df44 668 int TxOngoing = 0;
lypinator 0:bb348c97df44 669
lypinator 0:bb348c97df44 670 #if defined(USART1_BASE)
lypinator 0:bb348c97df44 671 if (LL_USART_IsEnabled(USART1) && !LL_USART_IsActiveFlag_TC(USART1)) {
lypinator 0:bb348c97df44 672 TxOngoing |= 1;
lypinator 0:bb348c97df44 673 }
lypinator 0:bb348c97df44 674 #endif
lypinator 0:bb348c97df44 675
lypinator 0:bb348c97df44 676 #if defined(USART2_BASE)
lypinator 0:bb348c97df44 677 if (LL_USART_IsEnabled(USART2) && !LL_USART_IsActiveFlag_TC(USART2)) {
lypinator 0:bb348c97df44 678 TxOngoing |= 1;
lypinator 0:bb348c97df44 679 }
lypinator 0:bb348c97df44 680 #endif
lypinator 0:bb348c97df44 681
lypinator 0:bb348c97df44 682 #if defined(USART3_BASE)
lypinator 0:bb348c97df44 683 if (LL_USART_IsEnabled(USART3) && !LL_USART_IsActiveFlag_TC(USART3)) {
lypinator 0:bb348c97df44 684 TxOngoing |= 1;
lypinator 0:bb348c97df44 685 }
lypinator 0:bb348c97df44 686 #endif
lypinator 0:bb348c97df44 687
lypinator 0:bb348c97df44 688 #if defined(UART4_BASE)
lypinator 0:bb348c97df44 689 if (LL_USART_IsEnabled(UART4) && !LL_USART_IsActiveFlag_TC(UART4)) {
lypinator 0:bb348c97df44 690 TxOngoing |= 1;
lypinator 0:bb348c97df44 691 }
lypinator 0:bb348c97df44 692 #endif
lypinator 0:bb348c97df44 693
lypinator 0:bb348c97df44 694 #if defined(USART4_BASE)
lypinator 0:bb348c97df44 695 if (LL_USART_IsEnabled(USART4) && !LL_USART_IsActiveFlag_TC(USART4)) {
lypinator 0:bb348c97df44 696 TxOngoing |= 1;
lypinator 0:bb348c97df44 697 }
lypinator 0:bb348c97df44 698 #endif
lypinator 0:bb348c97df44 699
lypinator 0:bb348c97df44 700 #if defined(UART5_BASE)
lypinator 0:bb348c97df44 701 if (LL_USART_IsEnabled(UART5) && !LL_USART_IsActiveFlag_TC(UART5)) {
lypinator 0:bb348c97df44 702 TxOngoing |= 1;
lypinator 0:bb348c97df44 703 }
lypinator 0:bb348c97df44 704 #endif
lypinator 0:bb348c97df44 705
lypinator 0:bb348c97df44 706 #if defined(USART5_BASE)
lypinator 0:bb348c97df44 707 if (LL_USART_IsEnabled(USART5) && !LL_USART_IsActiveFlag_TC(USART5)) {
lypinator 0:bb348c97df44 708 TxOngoing |= 1;
lypinator 0:bb348c97df44 709 }
lypinator 0:bb348c97df44 710 #endif
lypinator 0:bb348c97df44 711
lypinator 0:bb348c97df44 712 #if defined(USART6_BASE)
lypinator 0:bb348c97df44 713 if (LL_USART_IsEnabled(USART6) && !LL_USART_IsActiveFlag_TC(USART6)) {
lypinator 0:bb348c97df44 714 TxOngoing |= 1;
lypinator 0:bb348c97df44 715 }
lypinator 0:bb348c97df44 716 #endif
lypinator 0:bb348c97df44 717
lypinator 0:bb348c97df44 718 #if defined(UART7_BASE)
lypinator 0:bb348c97df44 719 if (LL_USART_IsEnabled(UART7) && !LL_USART_IsActiveFlag_TC(UART7)) {
lypinator 0:bb348c97df44 720 TxOngoing |= 1;
lypinator 0:bb348c97df44 721 }
lypinator 0:bb348c97df44 722 #endif
lypinator 0:bb348c97df44 723
lypinator 0:bb348c97df44 724 #if defined(USART7_BASE)
lypinator 0:bb348c97df44 725 if (LL_USART_IsEnabled(USART7) && !LL_USART_IsActiveFlag_TC(USART7)) {
lypinator 0:bb348c97df44 726 TxOngoing |= 1;
lypinator 0:bb348c97df44 727 }
lypinator 0:bb348c97df44 728 #endif
lypinator 0:bb348c97df44 729
lypinator 0:bb348c97df44 730 #if defined(UART8_BASE)
lypinator 0:bb348c97df44 731 if (LL_USART_IsEnabled(UART8) && !LL_USART_IsActiveFlag_TC(UART8)) {
lypinator 0:bb348c97df44 732 TxOngoing |= 1;
lypinator 0:bb348c97df44 733 }
lypinator 0:bb348c97df44 734 #endif
lypinator 0:bb348c97df44 735
lypinator 0:bb348c97df44 736 #if defined(USART8_BASE)
lypinator 0:bb348c97df44 737 if (LL_USART_IsEnabled(USART8) && !LL_USART_IsActiveFlag_TC(USART8)) {
lypinator 0:bb348c97df44 738 TxOngoing |= 1;
lypinator 0:bb348c97df44 739 }
lypinator 0:bb348c97df44 740 #endif
lypinator 0:bb348c97df44 741
lypinator 0:bb348c97df44 742 #if defined(UART9_BASE)
lypinator 0:bb348c97df44 743 if (LL_USART_IsEnabled(UART9) && !LL_USART_IsActiveFlag_TC(UART9)) {
lypinator 0:bb348c97df44 744 TxOngoing |= 1;
lypinator 0:bb348c97df44 745 }
lypinator 0:bb348c97df44 746 #endif
lypinator 0:bb348c97df44 747
lypinator 0:bb348c97df44 748 #if defined(UART10_BASE)
lypinator 0:bb348c97df44 749 if (LL_USART_IsEnabled(UART10) && !LL_USART_IsActiveFlag_TC(UART10)) {
lypinator 0:bb348c97df44 750 TxOngoing |= 1;
lypinator 0:bb348c97df44 751 }
lypinator 0:bb348c97df44 752 #endif
lypinator 0:bb348c97df44 753
lypinator 0:bb348c97df44 754 #if defined(LPUART1_BASE)
lypinator 0:bb348c97df44 755 if (LL_USART_IsEnabled(LPUART1) && !LL_USART_IsActiveFlag_TC(LPUART1)) {
lypinator 0:bb348c97df44 756 TxOngoing |= 1;
lypinator 0:bb348c97df44 757 }
lypinator 0:bb348c97df44 758 #endif
lypinator 0:bb348c97df44 759
lypinator 0:bb348c97df44 760 /* If Tx is ongoing, then transfer is */
lypinator 0:bb348c97df44 761 return TxOngoing;
lypinator 0:bb348c97df44 762 }
lypinator 0:bb348c97df44 763
lypinator 0:bb348c97df44 764 #endif /* DEVICE_SERIAL */