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