Modified for BG96
Fork of mbed-dev by
targets/TARGET_STM/serial_api.c@181:57724642e740, 2018-02-16 (annotated)
- Committer:
- AnnaBridge
- Date:
- Fri Feb 16 16:09:33 2018 +0000
- Revision:
- 181:57724642e740
- Parent:
- 180:96ed750bd169
- Child:
- 182:a56a73fd2a6f
mbed-dev library. Release version 159.
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 | |
Anna Bridge |
180:96ed750bd169 | 35 | int stdio_uart_inited = 0; // used in platform/mbed_board.c and platform/mbed_retarget.cpp |
Anna Bridge |
180:96ed750bd169 | 36 | serial_t stdio_uart; |
Anna Bridge |
180:96ed750bd169 | 37 | |
Anna Bridge |
180:96ed750bd169 | 38 | extern UART_HandleTypeDef uart_handlers[]; |
Anna Bridge |
180:96ed750bd169 | 39 | extern uint32_t serial_irq_ids[]; |
Anna Bridge |
180:96ed750bd169 | 40 | |
AnnaBridge | 181:57724642e740 | 41 | // Utility functions |
AnnaBridge | 181:57724642e740 | 42 | HAL_StatusTypeDef init_uart(serial_t *obj); |
AnnaBridge | 181:57724642e740 | 43 | int8_t get_uart_index(UARTName uart_name); |
AnnaBridge | 181:57724642e740 | 44 | |
Anna Bridge |
180:96ed750bd169 | 45 | void serial_init(serial_t *obj, PinName tx, PinName rx) |
Anna Bridge |
180:96ed750bd169 | 46 | { |
Anna Bridge |
180:96ed750bd169 | 47 | struct serial_s *obj_s = SERIAL_S(obj); |
AnnaBridge | 181:57724642e740 | 48 | uint8_t stdio_config = 0; |
Anna Bridge |
180:96ed750bd169 | 49 | |
Anna Bridge |
180:96ed750bd169 | 50 | // Determine the UART to use (UART_1, UART_2, ...) |
Anna Bridge |
180:96ed750bd169 | 51 | UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); |
Anna Bridge |
180:96ed750bd169 | 52 | UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); |
Anna Bridge |
180:96ed750bd169 | 53 | |
Anna Bridge |
180:96ed750bd169 | 54 | // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object |
Anna Bridge |
180:96ed750bd169 | 55 | obj_s->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); |
Anna Bridge |
180:96ed750bd169 | 56 | MBED_ASSERT(obj_s->uart != (UARTName)NC); |
Anna Bridge |
180:96ed750bd169 | 57 | |
AnnaBridge | 181:57724642e740 | 58 | if ((tx == STDIO_UART_TX) || (rx == STDIO_UART_RX)) { |
AnnaBridge | 181:57724642e740 | 59 | stdio_config = 1; |
AnnaBridge | 181:57724642e740 | 60 | } |
AnnaBridge | 181:57724642e740 | 61 | else { |
AnnaBridge | 181:57724642e740 | 62 | if (uart_tx == pinmap_peripheral(STDIO_UART_TX, PinMap_UART_TX)) { |
AnnaBridge | 181:57724642e740 | 63 | error("Error: new serial object is using same UART as STDIO"); |
AnnaBridge | 181:57724642e740 | 64 | } |
AnnaBridge | 181:57724642e740 | 65 | } |
AnnaBridge | 181:57724642e740 | 66 | |
AnnaBridge | 181:57724642e740 | 67 | // Reset and enable clock |
Anna Bridge |
180:96ed750bd169 | 68 | #if defined(USART1_BASE) |
Anna Bridge |
180:96ed750bd169 | 69 | if (obj_s->uart == UART_1) { |
Anna Bridge |
180:96ed750bd169 | 70 | __HAL_RCC_USART1_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 71 | __HAL_RCC_USART1_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 72 | __HAL_RCC_USART1_CLK_ENABLE(); |
Anna Bridge |
180:96ed750bd169 | 73 | } |
Anna Bridge |
180:96ed750bd169 | 74 | #endif |
Anna Bridge |
180:96ed750bd169 | 75 | |
Anna Bridge |
180:96ed750bd169 | 76 | #if defined (USART2_BASE) |
Anna Bridge |
180:96ed750bd169 | 77 | if (obj_s->uart == UART_2) { |
Anna Bridge |
180:96ed750bd169 | 78 | __HAL_RCC_USART2_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 79 | __HAL_RCC_USART2_RELEASE_RESET(); |
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_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 87 | __HAL_RCC_USART3_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 88 | __HAL_RCC_USART3_CLK_ENABLE(); |
Anna Bridge |
180:96ed750bd169 | 89 | } |
Anna Bridge |
180:96ed750bd169 | 90 | #endif |
Anna Bridge |
180:96ed750bd169 | 91 | |
Anna Bridge |
180:96ed750bd169 | 92 | #if defined(UART4_BASE) |
Anna Bridge |
180:96ed750bd169 | 93 | if (obj_s->uart == UART_4) { |
Anna Bridge |
180:96ed750bd169 | 94 | __HAL_RCC_UART4_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 95 | __HAL_RCC_UART4_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 96 | __HAL_RCC_UART4_CLK_ENABLE(); |
Anna Bridge |
180:96ed750bd169 | 97 | } |
Anna Bridge |
180:96ed750bd169 | 98 | #endif |
Anna Bridge |
180:96ed750bd169 | 99 | |
Anna Bridge |
180:96ed750bd169 | 100 | #if defined(USART4_BASE) |
Anna Bridge |
180:96ed750bd169 | 101 | if (obj_s->uart == UART_4) { |
Anna Bridge |
180:96ed750bd169 | 102 | __HAL_RCC_USART4_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 103 | __HAL_RCC_USART4_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 104 | __HAL_RCC_USART4_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(UART5_BASE) |
Anna Bridge |
180:96ed750bd169 | 109 | if (obj_s->uart == UART_5) { |
Anna Bridge |
180:96ed750bd169 | 110 | __HAL_RCC_UART5_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 111 | __HAL_RCC_UART5_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 112 | __HAL_RCC_UART5_CLK_ENABLE(); |
Anna Bridge |
180:96ed750bd169 | 113 | } |
Anna Bridge |
180:96ed750bd169 | 114 | #endif |
Anna Bridge |
180:96ed750bd169 | 115 | |
Anna Bridge |
180:96ed750bd169 | 116 | #if defined(USART5_BASE) |
Anna Bridge |
180:96ed750bd169 | 117 | if (obj_s->uart == UART_5) { |
Anna Bridge |
180:96ed750bd169 | 118 | __HAL_RCC_USART5_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 119 | __HAL_RCC_USART5_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 120 | __HAL_RCC_USART5_CLK_ENABLE(); |
Anna Bridge |
180:96ed750bd169 | 121 | } |
Anna Bridge |
180:96ed750bd169 | 122 | #endif |
Anna Bridge |
180:96ed750bd169 | 123 | |
Anna Bridge |
180:96ed750bd169 | 124 | #if defined(USART6_BASE) |
Anna Bridge |
180:96ed750bd169 | 125 | if (obj_s->uart == UART_6) { |
Anna Bridge |
180:96ed750bd169 | 126 | __HAL_RCC_USART6_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 127 | __HAL_RCC_USART6_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 128 | __HAL_RCC_USART6_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(UART7_BASE) |
Anna Bridge |
180:96ed750bd169 | 133 | if (obj_s->uart == UART_7) { |
Anna Bridge |
180:96ed750bd169 | 134 | __HAL_RCC_UART7_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 135 | __HAL_RCC_UART7_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 136 | __HAL_RCC_UART7_CLK_ENABLE(); |
Anna Bridge |
180:96ed750bd169 | 137 | } |
Anna Bridge |
180:96ed750bd169 | 138 | #endif |
Anna Bridge |
180:96ed750bd169 | 139 | |
Anna Bridge |
180:96ed750bd169 | 140 | #if defined(USART7_BASE) |
Anna Bridge |
180:96ed750bd169 | 141 | if (obj_s->uart == UART_7) { |
Anna Bridge |
180:96ed750bd169 | 142 | __HAL_RCC_USART7_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 143 | __HAL_RCC_USART7_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 144 | __HAL_RCC_USART7_CLK_ENABLE(); |
Anna Bridge |
180:96ed750bd169 | 145 | } |
Anna Bridge |
180:96ed750bd169 | 146 | #endif |
Anna Bridge |
180:96ed750bd169 | 147 | |
Anna Bridge |
180:96ed750bd169 | 148 | #if defined(UART8_BASE) |
Anna Bridge |
180:96ed750bd169 | 149 | if (obj_s->uart == UART_8) { |
Anna Bridge |
180:96ed750bd169 | 150 | __HAL_RCC_UART8_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 151 | __HAL_RCC_UART8_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 152 | __HAL_RCC_UART8_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(USART8_BASE) |
Anna Bridge |
180:96ed750bd169 | 157 | if (obj_s->uart == UART_8) { |
Anna Bridge |
180:96ed750bd169 | 158 | __HAL_RCC_USART8_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 159 | __HAL_RCC_USART8_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 160 | __HAL_RCC_USART8_CLK_ENABLE(); |
Anna Bridge |
180:96ed750bd169 | 161 | } |
Anna Bridge |
180:96ed750bd169 | 162 | #endif |
Anna Bridge |
180:96ed750bd169 | 163 | |
Anna Bridge |
180:96ed750bd169 | 164 | #if defined(UART9_BASE) |
Anna Bridge |
180:96ed750bd169 | 165 | if (obj_s->uart == UART_9) { |
Anna Bridge |
180:96ed750bd169 | 166 | __HAL_RCC_UART9_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 167 | __HAL_RCC_UART9_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 168 | __HAL_RCC_UART9_CLK_ENABLE(); |
Anna Bridge |
180:96ed750bd169 | 169 | } |
Anna Bridge |
180:96ed750bd169 | 170 | #endif |
Anna Bridge |
180:96ed750bd169 | 171 | |
Anna Bridge |
180:96ed750bd169 | 172 | #if defined(UART10_BASE) |
Anna Bridge |
180:96ed750bd169 | 173 | if (obj_s->uart == UART_10) { |
Anna Bridge |
180:96ed750bd169 | 174 | __HAL_RCC_UART10_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 175 | __HAL_RCC_UART10_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 176 | __HAL_RCC_UART10_CLK_ENABLE(); |
Anna Bridge |
180:96ed750bd169 | 177 | } |
Anna Bridge |
180:96ed750bd169 | 178 | #endif |
Anna Bridge |
180:96ed750bd169 | 179 | |
Anna Bridge |
180:96ed750bd169 | 180 | #if defined(LPUART1_BASE) |
Anna Bridge |
180:96ed750bd169 | 181 | if (obj_s->uart == LPUART_1) { |
Anna Bridge |
180:96ed750bd169 | 182 | __HAL_RCC_LPUART1_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 183 | __HAL_RCC_LPUART1_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 184 | __HAL_RCC_LPUART1_CLK_ENABLE(); |
Anna Bridge |
180:96ed750bd169 | 185 | } |
Anna Bridge |
180:96ed750bd169 | 186 | #endif |
Anna Bridge |
180:96ed750bd169 | 187 | |
AnnaBridge | 181:57724642e740 | 188 | // Assign serial object index |
AnnaBridge | 181:57724642e740 | 189 | obj_s->index = get_uart_index(obj_s->uart); |
AnnaBridge | 181:57724642e740 | 190 | MBED_ASSERT(obj_s->index >= 0); |
AnnaBridge | 181:57724642e740 | 191 | |
Anna Bridge |
180:96ed750bd169 | 192 | // Configure UART pins |
Anna Bridge |
180:96ed750bd169 | 193 | pinmap_pinout(tx, PinMap_UART_TX); |
Anna Bridge |
180:96ed750bd169 | 194 | pinmap_pinout(rx, PinMap_UART_RX); |
Anna Bridge |
180:96ed750bd169 | 195 | |
Anna Bridge |
180:96ed750bd169 | 196 | if (tx != NC) { |
Anna Bridge |
180:96ed750bd169 | 197 | pin_mode(tx, PullUp); |
Anna Bridge |
180:96ed750bd169 | 198 | } |
Anna Bridge |
180:96ed750bd169 | 199 | if (rx != NC) { |
Anna Bridge |
180:96ed750bd169 | 200 | pin_mode(rx, PullUp); |
Anna Bridge |
180:96ed750bd169 | 201 | } |
Anna Bridge |
180:96ed750bd169 | 202 | |
Anna Bridge |
180:96ed750bd169 | 203 | // Configure UART |
Anna Bridge |
180:96ed750bd169 | 204 | obj_s->baudrate = 9600; // baudrate default value |
AnnaBridge | 181:57724642e740 | 205 | if (stdio_config) { |
Anna Bridge |
180:96ed750bd169 | 206 | #if MBED_CONF_PLATFORM_STDIO_BAUD_RATE |
Anna Bridge |
180:96ed750bd169 | 207 | obj_s->baudrate = MBED_CONF_PLATFORM_STDIO_BAUD_RATE; // baudrate takes value from platform/mbed_lib.json |
Anna Bridge |
180:96ed750bd169 | 208 | #endif /* MBED_CONF_PLATFORM_STDIO_BAUD_RATE */ |
Anna Bridge |
180:96ed750bd169 | 209 | } |
Anna Bridge |
180:96ed750bd169 | 210 | else { |
Anna Bridge |
180:96ed750bd169 | 211 | #if MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE |
Anna Bridge |
180:96ed750bd169 | 212 | obj_s->baudrate = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE; // baudrate takes value from platform/mbed_lib.json |
Anna Bridge |
180:96ed750bd169 | 213 | #endif /* MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE */ |
Anna Bridge |
180:96ed750bd169 | 214 | } |
Anna Bridge |
180:96ed750bd169 | 215 | obj_s->databits = UART_WORDLENGTH_8B; |
Anna Bridge |
180:96ed750bd169 | 216 | obj_s->stopbits = UART_STOPBITS_1; |
Anna Bridge |
180:96ed750bd169 | 217 | obj_s->parity = UART_PARITY_NONE; |
Anna Bridge |
180:96ed750bd169 | 218 | |
Anna Bridge |
180:96ed750bd169 | 219 | #if DEVICE_SERIAL_FC |
Anna Bridge |
180:96ed750bd169 | 220 | obj_s->hw_flow_ctl = UART_HWCONTROL_NONE; |
Anna Bridge |
180:96ed750bd169 | 221 | #endif |
Anna Bridge |
180:96ed750bd169 | 222 | |
Anna Bridge |
180:96ed750bd169 | 223 | obj_s->pin_tx = tx; |
Anna Bridge |
180:96ed750bd169 | 224 | obj_s->pin_rx = rx; |
Anna Bridge |
180:96ed750bd169 | 225 | |
Anna Bridge |
180:96ed750bd169 | 226 | 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 | 227 | |
AnnaBridge | 181:57724642e740 | 228 | // For stdio management in platform/mbed_board.c and platform/mbed_retarget.cpp |
AnnaBridge | 181:57724642e740 | 229 | if (stdio_config) { |
Anna Bridge |
180:96ed750bd169 | 230 | stdio_uart_inited = 1; |
Anna Bridge |
180:96ed750bd169 | 231 | memcpy(&stdio_uart, obj, sizeof(serial_t)); |
Anna Bridge |
180:96ed750bd169 | 232 | } |
Anna Bridge |
180:96ed750bd169 | 233 | } |
Anna Bridge |
180:96ed750bd169 | 234 | |
Anna Bridge |
180:96ed750bd169 | 235 | void serial_free(serial_t *obj) |
Anna Bridge |
180:96ed750bd169 | 236 | { |
Anna Bridge |
180:96ed750bd169 | 237 | struct serial_s *obj_s = SERIAL_S(obj); |
Anna Bridge |
180:96ed750bd169 | 238 | |
Anna Bridge |
180:96ed750bd169 | 239 | // Reset UART and disable clock |
Anna Bridge |
180:96ed750bd169 | 240 | #if defined(USART1_BASE) |
Anna Bridge |
180:96ed750bd169 | 241 | if (obj_s->uart == UART_1) { |
Anna Bridge |
180:96ed750bd169 | 242 | __HAL_RCC_USART1_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 243 | __HAL_RCC_USART1_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 244 | __HAL_RCC_USART1_CLK_DISABLE(); |
Anna Bridge |
180:96ed750bd169 | 245 | } |
Anna Bridge |
180:96ed750bd169 | 246 | #endif |
Anna Bridge |
180:96ed750bd169 | 247 | |
Anna Bridge |
180:96ed750bd169 | 248 | #if defined(USART2_BASE) |
Anna Bridge |
180:96ed750bd169 | 249 | if (obj_s->uart == UART_2) { |
Anna Bridge |
180:96ed750bd169 | 250 | __HAL_RCC_USART2_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 251 | __HAL_RCC_USART2_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 252 | __HAL_RCC_USART2_CLK_DISABLE(); |
Anna Bridge |
180:96ed750bd169 | 253 | } |
Anna Bridge |
180:96ed750bd169 | 254 | #endif |
Anna Bridge |
180:96ed750bd169 | 255 | |
Anna Bridge |
180:96ed750bd169 | 256 | #if defined(USART3_BASE) |
Anna Bridge |
180:96ed750bd169 | 257 | if (obj_s->uart == UART_3) { |
Anna Bridge |
180:96ed750bd169 | 258 | __HAL_RCC_USART3_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 259 | __HAL_RCC_USART3_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 260 | __HAL_RCC_USART3_CLK_DISABLE(); |
Anna Bridge |
180:96ed750bd169 | 261 | } |
Anna Bridge |
180:96ed750bd169 | 262 | #endif |
Anna Bridge |
180:96ed750bd169 | 263 | |
Anna Bridge |
180:96ed750bd169 | 264 | #if defined(UART4_BASE) |
Anna Bridge |
180:96ed750bd169 | 265 | if (obj_s->uart == UART_4) { |
Anna Bridge |
180:96ed750bd169 | 266 | __HAL_RCC_UART4_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 267 | __HAL_RCC_UART4_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 268 | __HAL_RCC_UART4_CLK_DISABLE(); |
Anna Bridge |
180:96ed750bd169 | 269 | } |
Anna Bridge |
180:96ed750bd169 | 270 | #endif |
Anna Bridge |
180:96ed750bd169 | 271 | |
Anna Bridge |
180:96ed750bd169 | 272 | #if defined(USART4_BASE) |
Anna Bridge |
180:96ed750bd169 | 273 | if (obj_s->uart == UART_4) { |
Anna Bridge |
180:96ed750bd169 | 274 | __HAL_RCC_USART4_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 275 | __HAL_RCC_USART4_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 276 | __HAL_RCC_USART4_CLK_DISABLE(); |
Anna Bridge |
180:96ed750bd169 | 277 | } |
Anna Bridge |
180:96ed750bd169 | 278 | #endif |
Anna Bridge |
180:96ed750bd169 | 279 | |
Anna Bridge |
180:96ed750bd169 | 280 | #if defined(UART5_BASE) |
Anna Bridge |
180:96ed750bd169 | 281 | if (obj_s->uart == UART_5) { |
Anna Bridge |
180:96ed750bd169 | 282 | __HAL_RCC_UART5_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 283 | __HAL_RCC_UART5_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 284 | __HAL_RCC_UART5_CLK_DISABLE(); |
Anna Bridge |
180:96ed750bd169 | 285 | } |
Anna Bridge |
180:96ed750bd169 | 286 | #endif |
Anna Bridge |
180:96ed750bd169 | 287 | |
Anna Bridge |
180:96ed750bd169 | 288 | #if defined(USART5_BASE) |
Anna Bridge |
180:96ed750bd169 | 289 | if (obj_s->uart == UART_5) { |
Anna Bridge |
180:96ed750bd169 | 290 | __HAL_RCC_USART5_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 291 | __HAL_RCC_USART5_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 292 | __HAL_RCC_USART5_CLK_DISABLE(); |
Anna Bridge |
180:96ed750bd169 | 293 | } |
Anna Bridge |
180:96ed750bd169 | 294 | #endif |
Anna Bridge |
180:96ed750bd169 | 295 | |
Anna Bridge |
180:96ed750bd169 | 296 | #if defined(USART6_BASE) |
Anna Bridge |
180:96ed750bd169 | 297 | if (obj_s->uart == UART_6) { |
Anna Bridge |
180:96ed750bd169 | 298 | __HAL_RCC_USART6_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 299 | __HAL_RCC_USART6_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 300 | __HAL_RCC_USART6_CLK_DISABLE(); |
Anna Bridge |
180:96ed750bd169 | 301 | } |
Anna Bridge |
180:96ed750bd169 | 302 | #endif |
Anna Bridge |
180:96ed750bd169 | 303 | |
Anna Bridge |
180:96ed750bd169 | 304 | #if defined(UART7_BASE) |
Anna Bridge |
180:96ed750bd169 | 305 | if (obj_s->uart == UART_7) { |
Anna Bridge |
180:96ed750bd169 | 306 | __HAL_RCC_UART7_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 307 | __HAL_RCC_UART7_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 308 | __HAL_RCC_UART7_CLK_DISABLE(); |
Anna Bridge |
180:96ed750bd169 | 309 | } |
Anna Bridge |
180:96ed750bd169 | 310 | #endif |
Anna Bridge |
180:96ed750bd169 | 311 | |
Anna Bridge |
180:96ed750bd169 | 312 | #if defined(USART7_BASE) |
Anna Bridge |
180:96ed750bd169 | 313 | if (obj_s->uart == UART_7) { |
Anna Bridge |
180:96ed750bd169 | 314 | __HAL_RCC_USART7_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 315 | __HAL_RCC_USART7_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 316 | __HAL_RCC_USART7_CLK_DISABLE(); |
Anna Bridge |
180:96ed750bd169 | 317 | } |
Anna Bridge |
180:96ed750bd169 | 318 | #endif |
Anna Bridge |
180:96ed750bd169 | 319 | |
Anna Bridge |
180:96ed750bd169 | 320 | #if defined(UART8_BASE) |
Anna Bridge |
180:96ed750bd169 | 321 | if (obj_s->uart == UART_8) { |
Anna Bridge |
180:96ed750bd169 | 322 | __HAL_RCC_UART8_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 323 | __HAL_RCC_UART8_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 324 | __HAL_RCC_UART8_CLK_DISABLE(); |
Anna Bridge |
180:96ed750bd169 | 325 | } |
Anna Bridge |
180:96ed750bd169 | 326 | #endif |
Anna Bridge |
180:96ed750bd169 | 327 | |
Anna Bridge |
180:96ed750bd169 | 328 | #if defined(USART8_BASE) |
Anna Bridge |
180:96ed750bd169 | 329 | if (obj_s->uart == UART_8) { |
Anna Bridge |
180:96ed750bd169 | 330 | __HAL_RCC_USART8_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 331 | __HAL_RCC_USART8_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 332 | __HAL_RCC_USART8_CLK_DISABLE(); |
Anna Bridge |
180:96ed750bd169 | 333 | } |
Anna Bridge |
180:96ed750bd169 | 334 | #endif |
Anna Bridge |
180:96ed750bd169 | 335 | |
Anna Bridge |
180:96ed750bd169 | 336 | #if defined(UART9_BASE) |
Anna Bridge |
180:96ed750bd169 | 337 | if (obj_s->uart == UART_9) { |
Anna Bridge |
180:96ed750bd169 | 338 | __HAL_RCC_UART9_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 339 | __HAL_RCC_UART9_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 340 | __HAL_RCC_UART9_CLK_DISABLE(); |
Anna Bridge |
180:96ed750bd169 | 341 | } |
Anna Bridge |
180:96ed750bd169 | 342 | #endif |
Anna Bridge |
180:96ed750bd169 | 343 | |
Anna Bridge |
180:96ed750bd169 | 344 | #if defined(UART10_BASE) |
Anna Bridge |
180:96ed750bd169 | 345 | if (obj_s->uart == UART_10) { |
Anna Bridge |
180:96ed750bd169 | 346 | __HAL_RCC_UART10_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 347 | __HAL_RCC_UART10_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 348 | __HAL_RCC_UART10_CLK_DISABLE(); |
Anna Bridge |
180:96ed750bd169 | 349 | } |
Anna Bridge |
180:96ed750bd169 | 350 | #endif |
Anna Bridge |
180:96ed750bd169 | 351 | |
Anna Bridge |
180:96ed750bd169 | 352 | #if defined(LPUART1_BASE) |
Anna Bridge |
180:96ed750bd169 | 353 | if (obj_s->uart == LPUART_1) { |
Anna Bridge |
180:96ed750bd169 | 354 | __HAL_RCC_LPUART1_FORCE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 355 | __HAL_RCC_LPUART1_RELEASE_RESET(); |
Anna Bridge |
180:96ed750bd169 | 356 | __HAL_RCC_LPUART1_CLK_DISABLE(); |
Anna Bridge |
180:96ed750bd169 | 357 | } |
Anna Bridge |
180:96ed750bd169 | 358 | #endif |
Anna Bridge |
180:96ed750bd169 | 359 | |
Anna Bridge |
180:96ed750bd169 | 360 | // Configure GPIOs |
Anna Bridge |
180:96ed750bd169 | 361 | pin_function(obj_s->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
Anna Bridge |
180:96ed750bd169 | 362 | pin_function(obj_s->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
Anna Bridge |
180:96ed750bd169 | 363 | |
Anna Bridge |
180:96ed750bd169 | 364 | serial_irq_ids[obj_s->index] = 0; |
Anna Bridge |
180:96ed750bd169 | 365 | } |
Anna Bridge |
180:96ed750bd169 | 366 | |
Anna Bridge |
180:96ed750bd169 | 367 | void serial_baud(serial_t *obj, int baudrate) |
Anna Bridge |
180:96ed750bd169 | 368 | { |
Anna Bridge |
180:96ed750bd169 | 369 | struct serial_s *obj_s = SERIAL_S(obj); |
Anna Bridge |
180:96ed750bd169 | 370 | |
Anna Bridge |
180:96ed750bd169 | 371 | obj_s->baudrate = baudrate; |
Anna Bridge |
180:96ed750bd169 | 372 | if (init_uart(obj) != HAL_OK) { |
Anna Bridge |
180:96ed750bd169 | 373 | |
Anna Bridge |
180:96ed750bd169 | 374 | #if defined(LPUART1_BASE) |
Anna Bridge |
180:96ed750bd169 | 375 | /* Note that LPUART clock source must be in the range [3 x baud rate, 4096 x baud rate], check Ref Manual */ |
Anna Bridge |
180:96ed750bd169 | 376 | if (obj_s->uart == LPUART_1) { |
Anna Bridge |
180:96ed750bd169 | 377 | /* Try to change LPUART clock source */ |
Anna Bridge |
180:96ed750bd169 | 378 | RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; |
Anna Bridge |
180:96ed750bd169 | 379 | if (baudrate == 9600) { |
Anna Bridge |
180:96ed750bd169 | 380 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1; |
Anna Bridge |
180:96ed750bd169 | 381 | PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_LSE; |
Anna Bridge |
180:96ed750bd169 | 382 | HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); |
Anna Bridge |
180:96ed750bd169 | 383 | if (init_uart(obj) == HAL_OK){ |
Anna Bridge |
180:96ed750bd169 | 384 | return; |
Anna Bridge |
180:96ed750bd169 | 385 | } |
Anna Bridge |
180:96ed750bd169 | 386 | } |
Anna Bridge |
180:96ed750bd169 | 387 | else { |
Anna Bridge |
180:96ed750bd169 | 388 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1; |
Anna Bridge |
180:96ed750bd169 | 389 | PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_SYSCLK; |
Anna Bridge |
180:96ed750bd169 | 390 | HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); |
Anna Bridge |
180:96ed750bd169 | 391 | if (init_uart(obj) == HAL_OK){ |
Anna Bridge |
180:96ed750bd169 | 392 | return; |
Anna Bridge |
180:96ed750bd169 | 393 | } |
Anna Bridge |
180:96ed750bd169 | 394 | } |
Anna Bridge |
180:96ed750bd169 | 395 | } |
Anna Bridge |
180:96ed750bd169 | 396 | #endif /* LPUART1_BASE */ |
Anna Bridge |
180:96ed750bd169 | 397 | |
Anna Bridge |
180:96ed750bd169 | 398 | debug("Cannot initialize UART with baud rate %u\n", baudrate); |
Anna Bridge |
180:96ed750bd169 | 399 | } |
Anna Bridge |
180:96ed750bd169 | 400 | } |
Anna Bridge |
180:96ed750bd169 | 401 | |
AnnaBridge | 167:e84263d55307 | 402 | void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) |
AnnaBridge | 167:e84263d55307 | 403 | { |
AnnaBridge | 167:e84263d55307 | 404 | struct serial_s *obj_s = SERIAL_S(obj); |
AnnaBridge | 167:e84263d55307 | 405 | |
AnnaBridge | 167:e84263d55307 | 406 | switch (parity) { |
AnnaBridge | 167:e84263d55307 | 407 | case ParityOdd: |
AnnaBridge | 167:e84263d55307 | 408 | obj_s->parity = UART_PARITY_ODD; |
AnnaBridge | 167:e84263d55307 | 409 | break; |
AnnaBridge | 167:e84263d55307 | 410 | case ParityEven: |
AnnaBridge | 167:e84263d55307 | 411 | obj_s->parity = UART_PARITY_EVEN; |
AnnaBridge | 167:e84263d55307 | 412 | break; |
AnnaBridge | 167:e84263d55307 | 413 | default: // ParityNone |
AnnaBridge | 167:e84263d55307 | 414 | case ParityForced0: // unsupported! |
AnnaBridge | 167:e84263d55307 | 415 | case ParityForced1: // unsupported! |
AnnaBridge | 167:e84263d55307 | 416 | obj_s->parity = UART_PARITY_NONE; |
AnnaBridge | 167:e84263d55307 | 417 | break; |
AnnaBridge | 167:e84263d55307 | 418 | } |
AnnaBridge | 167:e84263d55307 | 419 | |
AnnaBridge | 167:e84263d55307 | 420 | switch (data_bits) { |
AnnaBridge | 181:57724642e740 | 421 | case 7: |
AnnaBridge | 181:57724642e740 | 422 | if (parity != UART_PARITY_NONE) { |
AnnaBridge | 181:57724642e740 | 423 | obj_s->databits = UART_WORDLENGTH_8B; |
AnnaBridge | 181:57724642e740 | 424 | } else { |
AnnaBridge | 181:57724642e740 | 425 | #if defined UART_WORDLENGTH_7B |
AnnaBridge | 181:57724642e740 | 426 | obj_s->databits = UART_WORDLENGTH_7B; |
AnnaBridge | 181:57724642e740 | 427 | #else |
AnnaBridge | 181:57724642e740 | 428 | error("7-bit data format without parity is not supported"); |
AnnaBridge | 181:57724642e740 | 429 | #endif |
AnnaBridge | 181:57724642e740 | 430 | } |
AnnaBridge | 167:e84263d55307 | 431 | break; |
AnnaBridge | 167:e84263d55307 | 432 | case 8: |
AnnaBridge | 167:e84263d55307 | 433 | if (parity != UART_PARITY_NONE) { |
AnnaBridge | 167:e84263d55307 | 434 | obj_s->databits = UART_WORDLENGTH_9B; |
AnnaBridge | 167:e84263d55307 | 435 | } else { |
AnnaBridge | 167:e84263d55307 | 436 | obj_s->databits = UART_WORDLENGTH_8B; |
AnnaBridge | 167:e84263d55307 | 437 | } |
AnnaBridge | 167:e84263d55307 | 438 | break; |
AnnaBridge | 181:57724642e740 | 439 | case 9: |
AnnaBridge | 167:e84263d55307 | 440 | if (parity != UART_PARITY_NONE) { |
AnnaBridge | 181:57724642e740 | 441 | error("Parity is not supported with 9-bit data format"); |
AnnaBridge | 167:e84263d55307 | 442 | } else { |
AnnaBridge | 181:57724642e740 | 443 | obj_s->databits = UART_WORDLENGTH_9B; |
AnnaBridge | 167:e84263d55307 | 444 | } |
AnnaBridge | 167:e84263d55307 | 445 | break; |
AnnaBridge | 181:57724642e740 | 446 | default: |
AnnaBridge | 181:57724642e740 | 447 | error("Only 7, 8 or 9-bit data formats are supported"); |
AnnaBridge | 181:57724642e740 | 448 | break; |
AnnaBridge | 167:e84263d55307 | 449 | } |
AnnaBridge | 167:e84263d55307 | 450 | |
AnnaBridge | 167:e84263d55307 | 451 | if (stop_bits == 2) { |
AnnaBridge | 167:e84263d55307 | 452 | obj_s->stopbits = UART_STOPBITS_2; |
AnnaBridge | 167:e84263d55307 | 453 | } else { |
AnnaBridge | 167:e84263d55307 | 454 | obj_s->stopbits = UART_STOPBITS_1; |
AnnaBridge | 167:e84263d55307 | 455 | } |
AnnaBridge | 167:e84263d55307 | 456 | |
AnnaBridge | 167:e84263d55307 | 457 | init_uart(obj); |
AnnaBridge | 167:e84263d55307 | 458 | } |
AnnaBridge | 167:e84263d55307 | 459 | |
AnnaBridge | 167:e84263d55307 | 460 | /****************************************************************************** |
AnnaBridge | 167:e84263d55307 | 461 | * READ/WRITE |
AnnaBridge | 167:e84263d55307 | 462 | ******************************************************************************/ |
AnnaBridge | 167:e84263d55307 | 463 | |
AnnaBridge | 167:e84263d55307 | 464 | int serial_readable(serial_t *obj) |
AnnaBridge | 167:e84263d55307 | 465 | { |
AnnaBridge | 167:e84263d55307 | 466 | struct serial_s *obj_s = SERIAL_S(obj); |
AnnaBridge | 167:e84263d55307 | 467 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
AnnaBridge | 167:e84263d55307 | 468 | /* To avoid a target blocking case, let's check for |
AnnaBridge | 167:e84263d55307 | 469 | * possible OVERRUN error and discard it |
AnnaBridge | 167:e84263d55307 | 470 | */ |
AnnaBridge | 167:e84263d55307 | 471 | if(__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE)) { |
AnnaBridge | 167:e84263d55307 | 472 | __HAL_UART_CLEAR_OREFLAG(huart); |
AnnaBridge | 167:e84263d55307 | 473 | } |
AnnaBridge | 167:e84263d55307 | 474 | // Check if data is received |
AnnaBridge | 167:e84263d55307 | 475 | return (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) ? 1 : 0; |
AnnaBridge | 167:e84263d55307 | 476 | } |
AnnaBridge | 167:e84263d55307 | 477 | |
AnnaBridge | 167:e84263d55307 | 478 | int serial_writable(serial_t *obj) |
AnnaBridge | 167:e84263d55307 | 479 | { |
AnnaBridge | 167:e84263d55307 | 480 | struct serial_s *obj_s = SERIAL_S(obj); |
AnnaBridge | 167:e84263d55307 | 481 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
AnnaBridge | 167:e84263d55307 | 482 | |
AnnaBridge | 167:e84263d55307 | 483 | // Check if data is transmitted |
AnnaBridge | 167:e84263d55307 | 484 | return (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) ? 1 : 0; |
AnnaBridge | 167:e84263d55307 | 485 | } |
AnnaBridge | 167:e84263d55307 | 486 | |
AnnaBridge | 167:e84263d55307 | 487 | void serial_pinout_tx(PinName tx) |
AnnaBridge | 167:e84263d55307 | 488 | { |
AnnaBridge | 167:e84263d55307 | 489 | pinmap_pinout(tx, PinMap_UART_TX); |
AnnaBridge | 167:e84263d55307 | 490 | } |
AnnaBridge | 167:e84263d55307 | 491 | |
AnnaBridge | 167:e84263d55307 | 492 | void serial_break_clear(serial_t *obj) |
AnnaBridge | 167:e84263d55307 | 493 | { |
AnnaBridge | 167:e84263d55307 | 494 | (void)obj; |
AnnaBridge | 167:e84263d55307 | 495 | } |
AnnaBridge | 167:e84263d55307 | 496 | |
AnnaBridge | 181:57724642e740 | 497 | /****************************************************************************** |
AnnaBridge | 181:57724642e740 | 498 | * UTILITY FUNCTIONS |
AnnaBridge | 181:57724642e740 | 499 | ******************************************************************************/ |
AnnaBridge | 181:57724642e740 | 500 | |
AnnaBridge | 181:57724642e740 | 501 | HAL_StatusTypeDef init_uart(serial_t *obj) |
AnnaBridge | 181:57724642e740 | 502 | { |
AnnaBridge | 181:57724642e740 | 503 | struct serial_s *obj_s = SERIAL_S(obj); |
AnnaBridge | 181:57724642e740 | 504 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
AnnaBridge | 181:57724642e740 | 505 | huart->Instance = (USART_TypeDef *)(obj_s->uart); |
AnnaBridge | 181:57724642e740 | 506 | |
AnnaBridge | 181:57724642e740 | 507 | huart->Init.BaudRate = obj_s->baudrate; |
AnnaBridge | 181:57724642e740 | 508 | huart->Init.WordLength = obj_s->databits; |
AnnaBridge | 181:57724642e740 | 509 | huart->Init.StopBits = obj_s->stopbits; |
AnnaBridge | 181:57724642e740 | 510 | huart->Init.Parity = obj_s->parity; |
AnnaBridge | 181:57724642e740 | 511 | #if DEVICE_SERIAL_FC |
AnnaBridge | 181:57724642e740 | 512 | huart->Init.HwFlowCtl = obj_s->hw_flow_ctl; |
AnnaBridge | 181:57724642e740 | 513 | #else |
AnnaBridge | 181:57724642e740 | 514 | huart->Init.HwFlowCtl = UART_HWCONTROL_NONE; |
AnnaBridge | 181:57724642e740 | 515 | #endif |
AnnaBridge | 181:57724642e740 | 516 | huart->Init.OverSampling = UART_OVERSAMPLING_16; |
AnnaBridge | 181:57724642e740 | 517 | huart->TxXferCount = 0; |
AnnaBridge | 181:57724642e740 | 518 | huart->TxXferSize = 0; |
AnnaBridge | 181:57724642e740 | 519 | huart->RxXferCount = 0; |
AnnaBridge | 181:57724642e740 | 520 | huart->RxXferSize = 0; |
AnnaBridge | 181:57724642e740 | 521 | |
AnnaBridge | 181:57724642e740 | 522 | if (obj_s->pin_rx == NC) { |
AnnaBridge | 181:57724642e740 | 523 | huart->Init.Mode = UART_MODE_TX; |
AnnaBridge | 181:57724642e740 | 524 | } else if (obj_s->pin_tx == NC) { |
AnnaBridge | 181:57724642e740 | 525 | huart->Init.Mode = UART_MODE_RX; |
AnnaBridge | 181:57724642e740 | 526 | } else { |
AnnaBridge | 181:57724642e740 | 527 | huart->Init.Mode = UART_MODE_TX_RX; |
AnnaBridge | 181:57724642e740 | 528 | } |
AnnaBridge | 181:57724642e740 | 529 | |
AnnaBridge | 181:57724642e740 | 530 | return HAL_UART_Init(huart); |
AnnaBridge | 181:57724642e740 | 531 | } |
AnnaBridge | 181:57724642e740 | 532 | |
AnnaBridge | 181:57724642e740 | 533 | int8_t get_uart_index(UARTName uart_name) |
AnnaBridge | 181:57724642e740 | 534 | { |
AnnaBridge | 181:57724642e740 | 535 | uint8_t index = 0; |
AnnaBridge | 181:57724642e740 | 536 | |
AnnaBridge | 181:57724642e740 | 537 | #if defined(USART1_BASE) |
AnnaBridge | 181:57724642e740 | 538 | if (uart_name == UART_1) { |
AnnaBridge | 181:57724642e740 | 539 | return index; |
AnnaBridge | 181:57724642e740 | 540 | } |
AnnaBridge | 181:57724642e740 | 541 | index++; |
AnnaBridge | 181:57724642e740 | 542 | #endif |
AnnaBridge | 181:57724642e740 | 543 | |
AnnaBridge | 181:57724642e740 | 544 | #if defined(USART2_BASE) |
AnnaBridge | 181:57724642e740 | 545 | if (uart_name == UART_2) { |
AnnaBridge | 181:57724642e740 | 546 | return index; |
AnnaBridge | 181:57724642e740 | 547 | } |
AnnaBridge | 181:57724642e740 | 548 | index++; |
AnnaBridge | 181:57724642e740 | 549 | #endif |
AnnaBridge | 181:57724642e740 | 550 | |
AnnaBridge | 181:57724642e740 | 551 | #if defined(USART3_BASE) |
AnnaBridge | 181:57724642e740 | 552 | if (uart_name == UART_3) { |
AnnaBridge | 181:57724642e740 | 553 | return index; |
AnnaBridge | 181:57724642e740 | 554 | } |
AnnaBridge | 181:57724642e740 | 555 | index++; |
AnnaBridge | 181:57724642e740 | 556 | #endif |
AnnaBridge | 181:57724642e740 | 557 | |
AnnaBridge | 181:57724642e740 | 558 | #if defined(UART4_BASE) |
AnnaBridge | 181:57724642e740 | 559 | if (uart_name == UART_4) { |
AnnaBridge | 181:57724642e740 | 560 | return index; |
AnnaBridge | 181:57724642e740 | 561 | } |
AnnaBridge | 181:57724642e740 | 562 | index++; |
AnnaBridge | 181:57724642e740 | 563 | #endif |
AnnaBridge | 181:57724642e740 | 564 | |
AnnaBridge | 181:57724642e740 | 565 | #if defined(USART4_BASE) |
AnnaBridge | 181:57724642e740 | 566 | if (uart_name == UART_4) { |
AnnaBridge | 181:57724642e740 | 567 | return index; |
AnnaBridge | 181:57724642e740 | 568 | } |
AnnaBridge | 181:57724642e740 | 569 | index++; |
AnnaBridge | 181:57724642e740 | 570 | #endif |
AnnaBridge | 181:57724642e740 | 571 | |
AnnaBridge | 181:57724642e740 | 572 | #if defined(UART5_BASE) |
AnnaBridge | 181:57724642e740 | 573 | if (uart_name == UART_5) { |
AnnaBridge | 181:57724642e740 | 574 | return index; |
AnnaBridge | 181:57724642e740 | 575 | } |
AnnaBridge | 181:57724642e740 | 576 | index++; |
AnnaBridge | 181:57724642e740 | 577 | #endif |
AnnaBridge | 181:57724642e740 | 578 | |
AnnaBridge | 181:57724642e740 | 579 | #if defined(USART5_BASE) |
AnnaBridge | 181:57724642e740 | 580 | if (uart_name == UART_5) { |
AnnaBridge | 181:57724642e740 | 581 | return index; |
AnnaBridge | 181:57724642e740 | 582 | } |
AnnaBridge | 181:57724642e740 | 583 | index++; |
AnnaBridge | 181:57724642e740 | 584 | #endif |
AnnaBridge | 181:57724642e740 | 585 | |
AnnaBridge | 181:57724642e740 | 586 | #if defined(USART6_BASE) |
AnnaBridge | 181:57724642e740 | 587 | if (uart_name == UART_6) { |
AnnaBridge | 181:57724642e740 | 588 | return index; |
AnnaBridge | 181:57724642e740 | 589 | } |
AnnaBridge | 181:57724642e740 | 590 | index++; |
AnnaBridge | 181:57724642e740 | 591 | #endif |
AnnaBridge | 181:57724642e740 | 592 | |
AnnaBridge | 181:57724642e740 | 593 | #if defined(UART7_BASE) |
AnnaBridge | 181:57724642e740 | 594 | if (uart_name == UART_7) { |
AnnaBridge | 181:57724642e740 | 595 | return index; |
AnnaBridge | 181:57724642e740 | 596 | } |
AnnaBridge | 181:57724642e740 | 597 | index++; |
AnnaBridge | 181:57724642e740 | 598 | #endif |
AnnaBridge | 181:57724642e740 | 599 | |
AnnaBridge | 181:57724642e740 | 600 | #if defined(USART7_BASE) |
AnnaBridge | 181:57724642e740 | 601 | if (uart_name == UART_7) { |
AnnaBridge | 181:57724642e740 | 602 | return index; |
AnnaBridge | 181:57724642e740 | 603 | } |
AnnaBridge | 181:57724642e740 | 604 | index++; |
AnnaBridge | 181:57724642e740 | 605 | #endif |
AnnaBridge | 181:57724642e740 | 606 | |
AnnaBridge | 181:57724642e740 | 607 | #if defined(UART8_BASE) |
AnnaBridge | 181:57724642e740 | 608 | if (uart_name == UART_8) { |
AnnaBridge | 181:57724642e740 | 609 | return index; |
AnnaBridge | 181:57724642e740 | 610 | } |
AnnaBridge | 181:57724642e740 | 611 | index++; |
AnnaBridge | 181:57724642e740 | 612 | #endif |
AnnaBridge | 181:57724642e740 | 613 | |
AnnaBridge | 181:57724642e740 | 614 | #if defined(USART8_BASE) |
AnnaBridge | 181:57724642e740 | 615 | if (uart_name == UART_8) { |
AnnaBridge | 181:57724642e740 | 616 | return index; |
AnnaBridge | 181:57724642e740 | 617 | } |
AnnaBridge | 181:57724642e740 | 618 | index++; |
AnnaBridge | 181:57724642e740 | 619 | #endif |
AnnaBridge | 181:57724642e740 | 620 | |
AnnaBridge | 181:57724642e740 | 621 | #if defined(UART9_BASE) |
AnnaBridge | 181:57724642e740 | 622 | if (uart_name == UART_9) { |
AnnaBridge | 181:57724642e740 | 623 | return index; |
AnnaBridge | 181:57724642e740 | 624 | } |
AnnaBridge | 181:57724642e740 | 625 | index++; |
AnnaBridge | 181:57724642e740 | 626 | #endif |
AnnaBridge | 181:57724642e740 | 627 | |
AnnaBridge | 181:57724642e740 | 628 | #if defined(UART10_BASE) |
AnnaBridge | 181:57724642e740 | 629 | if (uart_name == UART_10) { |
AnnaBridge | 181:57724642e740 | 630 | return index; |
AnnaBridge | 181:57724642e740 | 631 | } |
AnnaBridge | 181:57724642e740 | 632 | index++; |
AnnaBridge | 181:57724642e740 | 633 | #endif |
AnnaBridge | 181:57724642e740 | 634 | |
AnnaBridge | 181:57724642e740 | 635 | #if defined(LPUART1_BASE) |
AnnaBridge | 181:57724642e740 | 636 | if (uart_name == LPUART_1) { |
AnnaBridge | 181:57724642e740 | 637 | return index; |
AnnaBridge | 181:57724642e740 | 638 | } |
AnnaBridge | 181:57724642e740 | 639 | index++; |
AnnaBridge | 181:57724642e740 | 640 | #endif |
AnnaBridge | 181:57724642e740 | 641 | |
AnnaBridge | 181:57724642e740 | 642 | return -1; |
AnnaBridge | 181:57724642e740 | 643 | } |
AnnaBridge | 181:57724642e740 | 644 | |
AnnaBridge | 167:e84263d55307 | 645 | #endif /* DEVICE_SERIAL */ |