Modified for BG96

Fork of mbed-dev by mbed official

Committer:
Anna Bridge
Date:
Wed Jan 17 15:23:54 2018 +0000
Revision:
180:96ed750bd169
Parent:
167:e84263d55307
Child:
181:57724642e740
mbed-dev libray. Release version 158

Who changed what in which revision?

UserRevisionLine numberNew 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
Anna Bridge 180:96ed750bd169 41 void serial_init(serial_t *obj, PinName tx, PinName rx)
Anna Bridge 180:96ed750bd169 42 {
Anna Bridge 180:96ed750bd169 43 struct serial_s *obj_s = SERIAL_S(obj);
Anna Bridge 180:96ed750bd169 44 int IndexNumber = 0;
Anna Bridge 180:96ed750bd169 45
Anna Bridge 180:96ed750bd169 46 // Determine the UART to use (UART_1, UART_2, ...)
Anna Bridge 180:96ed750bd169 47 UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
Anna Bridge 180:96ed750bd169 48 UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
Anna Bridge 180:96ed750bd169 49
Anna Bridge 180:96ed750bd169 50 // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
Anna Bridge 180:96ed750bd169 51 obj_s->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
Anna Bridge 180:96ed750bd169 52 MBED_ASSERT(obj_s->uart != (UARTName)NC);
Anna Bridge 180:96ed750bd169 53
Anna Bridge 180:96ed750bd169 54 // Enable USART clock
Anna Bridge 180:96ed750bd169 55 #if defined(USART1_BASE)
Anna Bridge 180:96ed750bd169 56 if (obj_s->uart == UART_1) {
Anna Bridge 180:96ed750bd169 57 __HAL_RCC_USART1_FORCE_RESET();
Anna Bridge 180:96ed750bd169 58 __HAL_RCC_USART1_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 59 __HAL_RCC_USART1_CLK_ENABLE();
Anna Bridge 180:96ed750bd169 60 obj_s->index = IndexNumber;
Anna Bridge 180:96ed750bd169 61 }
Anna Bridge 180:96ed750bd169 62 IndexNumber++;
Anna Bridge 180:96ed750bd169 63 #endif
Anna Bridge 180:96ed750bd169 64
Anna Bridge 180:96ed750bd169 65 #if defined (USART2_BASE)
Anna Bridge 180:96ed750bd169 66 if (obj_s->uart == UART_2) {
Anna Bridge 180:96ed750bd169 67 __HAL_RCC_USART2_FORCE_RESET();
Anna Bridge 180:96ed750bd169 68 __HAL_RCC_USART2_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 69 __HAL_RCC_USART2_CLK_ENABLE();
Anna Bridge 180:96ed750bd169 70 obj_s->index = IndexNumber;
Anna Bridge 180:96ed750bd169 71 }
Anna Bridge 180:96ed750bd169 72 IndexNumber++;
Anna Bridge 180:96ed750bd169 73 #endif
Anna Bridge 180:96ed750bd169 74
Anna Bridge 180:96ed750bd169 75 #if defined(USART3_BASE)
Anna Bridge 180:96ed750bd169 76 if (obj_s->uart == UART_3) {
Anna Bridge 180:96ed750bd169 77 __HAL_RCC_USART3_FORCE_RESET();
Anna Bridge 180:96ed750bd169 78 __HAL_RCC_USART3_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 79 __HAL_RCC_USART3_CLK_ENABLE();
Anna Bridge 180:96ed750bd169 80 obj_s->index = IndexNumber;
Anna Bridge 180:96ed750bd169 81 }
Anna Bridge 180:96ed750bd169 82 IndexNumber++;
Anna Bridge 180:96ed750bd169 83 #endif
Anna Bridge 180:96ed750bd169 84
Anna Bridge 180:96ed750bd169 85 #if defined(UART4_BASE)
Anna Bridge 180:96ed750bd169 86 if (obj_s->uart == UART_4) {
Anna Bridge 180:96ed750bd169 87 __HAL_RCC_UART4_FORCE_RESET();
Anna Bridge 180:96ed750bd169 88 __HAL_RCC_UART4_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 89 __HAL_RCC_UART4_CLK_ENABLE();
Anna Bridge 180:96ed750bd169 90 obj_s->index = IndexNumber;
Anna Bridge 180:96ed750bd169 91 }
Anna Bridge 180:96ed750bd169 92 IndexNumber++;
Anna Bridge 180:96ed750bd169 93 #endif
Anna Bridge 180:96ed750bd169 94
Anna Bridge 180:96ed750bd169 95 #if defined(USART4_BASE)
Anna Bridge 180:96ed750bd169 96 if (obj_s->uart == UART_4) {
Anna Bridge 180:96ed750bd169 97 __HAL_RCC_USART4_FORCE_RESET();
Anna Bridge 180:96ed750bd169 98 __HAL_RCC_USART4_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 99 __HAL_RCC_USART4_CLK_ENABLE();
Anna Bridge 180:96ed750bd169 100 obj_s->index = IndexNumber;
Anna Bridge 180:96ed750bd169 101 }
Anna Bridge 180:96ed750bd169 102 IndexNumber++;
Anna Bridge 180:96ed750bd169 103 #endif
Anna Bridge 180:96ed750bd169 104
Anna Bridge 180:96ed750bd169 105 #if defined(UART5_BASE)
Anna Bridge 180:96ed750bd169 106 if (obj_s->uart == UART_5) {
Anna Bridge 180:96ed750bd169 107 __HAL_RCC_UART5_FORCE_RESET();
Anna Bridge 180:96ed750bd169 108 __HAL_RCC_UART5_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 109 __HAL_RCC_UART5_CLK_ENABLE();
Anna Bridge 180:96ed750bd169 110 obj_s->index = IndexNumber;
Anna Bridge 180:96ed750bd169 111 }
Anna Bridge 180:96ed750bd169 112 IndexNumber++;
Anna Bridge 180:96ed750bd169 113 #endif
Anna Bridge 180:96ed750bd169 114
Anna Bridge 180:96ed750bd169 115 #if defined(USART5_BASE)
Anna Bridge 180:96ed750bd169 116 if (obj_s->uart == UART_5) {
Anna Bridge 180:96ed750bd169 117 __HAL_RCC_USART5_FORCE_RESET();
Anna Bridge 180:96ed750bd169 118 __HAL_RCC_USART5_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 119 __HAL_RCC_USART5_CLK_ENABLE();
Anna Bridge 180:96ed750bd169 120 obj_s->index = IndexNumber;
Anna Bridge 180:96ed750bd169 121 }
Anna Bridge 180:96ed750bd169 122 IndexNumber++;
Anna Bridge 180:96ed750bd169 123 #endif
Anna Bridge 180:96ed750bd169 124
Anna Bridge 180:96ed750bd169 125 #if defined(USART6_BASE)
Anna Bridge 180:96ed750bd169 126 if (obj_s->uart == UART_6) {
Anna Bridge 180:96ed750bd169 127 __HAL_RCC_USART6_FORCE_RESET();
Anna Bridge 180:96ed750bd169 128 __HAL_RCC_USART6_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 129 __HAL_RCC_USART6_CLK_ENABLE();
Anna Bridge 180:96ed750bd169 130 obj_s->index = IndexNumber;
Anna Bridge 180:96ed750bd169 131 }
Anna Bridge 180:96ed750bd169 132 IndexNumber++;
Anna Bridge 180:96ed750bd169 133 #endif
Anna Bridge 180:96ed750bd169 134
Anna Bridge 180:96ed750bd169 135 #if defined(UART7_BASE)
Anna Bridge 180:96ed750bd169 136 if (obj_s->uart == UART_7) {
Anna Bridge 180:96ed750bd169 137 __HAL_RCC_UART7_FORCE_RESET();
Anna Bridge 180:96ed750bd169 138 __HAL_RCC_UART7_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 139 __HAL_RCC_UART7_CLK_ENABLE();
Anna Bridge 180:96ed750bd169 140 obj_s->index = IndexNumber;
Anna Bridge 180:96ed750bd169 141 }
Anna Bridge 180:96ed750bd169 142 IndexNumber++;
Anna Bridge 180:96ed750bd169 143 #endif
Anna Bridge 180:96ed750bd169 144
Anna Bridge 180:96ed750bd169 145 #if defined(USART7_BASE)
Anna Bridge 180:96ed750bd169 146 if (obj_s->uart == UART_7) {
Anna Bridge 180:96ed750bd169 147 __HAL_RCC_USART7_FORCE_RESET();
Anna Bridge 180:96ed750bd169 148 __HAL_RCC_USART7_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 149 __HAL_RCC_USART7_CLK_ENABLE();
Anna Bridge 180:96ed750bd169 150 obj_s->index = IndexNumber;
Anna Bridge 180:96ed750bd169 151 }
Anna Bridge 180:96ed750bd169 152 IndexNumber++;
Anna Bridge 180:96ed750bd169 153 #endif
Anna Bridge 180:96ed750bd169 154
Anna Bridge 180:96ed750bd169 155 #if defined(UART8_BASE)
Anna Bridge 180:96ed750bd169 156 if (obj_s->uart == UART_8) {
Anna Bridge 180:96ed750bd169 157 __HAL_RCC_UART8_FORCE_RESET();
Anna Bridge 180:96ed750bd169 158 __HAL_RCC_UART8_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 159 __HAL_RCC_UART8_CLK_ENABLE();
Anna Bridge 180:96ed750bd169 160 obj_s->index = IndexNumber;
Anna Bridge 180:96ed750bd169 161 }
Anna Bridge 180:96ed750bd169 162 IndexNumber++;
Anna Bridge 180:96ed750bd169 163 #endif
Anna Bridge 180:96ed750bd169 164
Anna Bridge 180:96ed750bd169 165 #if defined(USART8_BASE)
Anna Bridge 180:96ed750bd169 166 if (obj_s->uart == UART_8) {
Anna Bridge 180:96ed750bd169 167 __HAL_RCC_USART8_FORCE_RESET();
Anna Bridge 180:96ed750bd169 168 __HAL_RCC_USART8_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 169 __HAL_RCC_USART8_CLK_ENABLE();
Anna Bridge 180:96ed750bd169 170 obj_s->index = IndexNumber;
Anna Bridge 180:96ed750bd169 171 }
Anna Bridge 180:96ed750bd169 172 IndexNumber++;
Anna Bridge 180:96ed750bd169 173 #endif
Anna Bridge 180:96ed750bd169 174
Anna Bridge 180:96ed750bd169 175 #if defined(UART9_BASE)
Anna Bridge 180:96ed750bd169 176 if (obj_s->uart == UART_9) {
Anna Bridge 180:96ed750bd169 177 __HAL_RCC_UART9_FORCE_RESET();
Anna Bridge 180:96ed750bd169 178 __HAL_RCC_UART9_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 179 __HAL_RCC_UART9_CLK_ENABLE();
Anna Bridge 180:96ed750bd169 180 obj_s->index = IndexNumber;
Anna Bridge 180:96ed750bd169 181 }
Anna Bridge 180:96ed750bd169 182 IndexNumber++;
Anna Bridge 180:96ed750bd169 183 #endif
Anna Bridge 180:96ed750bd169 184
Anna Bridge 180:96ed750bd169 185 #if defined(UART10_BASE)
Anna Bridge 180:96ed750bd169 186 if (obj_s->uart == UART_10) {
Anna Bridge 180:96ed750bd169 187 __HAL_RCC_UART10_FORCE_RESET();
Anna Bridge 180:96ed750bd169 188 __HAL_RCC_UART10_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 189 __HAL_RCC_UART10_CLK_ENABLE();
Anna Bridge 180:96ed750bd169 190 obj_s->index = IndexNumber;
Anna Bridge 180:96ed750bd169 191 }
Anna Bridge 180:96ed750bd169 192 IndexNumber++;
Anna Bridge 180:96ed750bd169 193 #endif
Anna Bridge 180:96ed750bd169 194
Anna Bridge 180:96ed750bd169 195
Anna Bridge 180:96ed750bd169 196 #if defined(LPUART1_BASE)
Anna Bridge 180:96ed750bd169 197 if (obj_s->uart == LPUART_1) {
Anna Bridge 180:96ed750bd169 198 __HAL_RCC_LPUART1_FORCE_RESET();
Anna Bridge 180:96ed750bd169 199 __HAL_RCC_LPUART1_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 200 __HAL_RCC_LPUART1_CLK_ENABLE();
Anna Bridge 180:96ed750bd169 201 obj_s->index = IndexNumber;
Anna Bridge 180:96ed750bd169 202 }
Anna Bridge 180:96ed750bd169 203 IndexNumber++;
Anna Bridge 180:96ed750bd169 204 #endif
Anna Bridge 180:96ed750bd169 205
Anna Bridge 180:96ed750bd169 206 // Configure UART pins
Anna Bridge 180:96ed750bd169 207 pinmap_pinout(tx, PinMap_UART_TX);
Anna Bridge 180:96ed750bd169 208 pinmap_pinout(rx, PinMap_UART_RX);
Anna Bridge 180:96ed750bd169 209
Anna Bridge 180:96ed750bd169 210 if (tx != NC) {
Anna Bridge 180:96ed750bd169 211 pin_mode(tx, PullUp);
Anna Bridge 180:96ed750bd169 212 }
Anna Bridge 180:96ed750bd169 213 if (rx != NC) {
Anna Bridge 180:96ed750bd169 214 pin_mode(rx, PullUp);
Anna Bridge 180:96ed750bd169 215 }
Anna Bridge 180:96ed750bd169 216
Anna Bridge 180:96ed750bd169 217 // Configure UART
Anna Bridge 180:96ed750bd169 218 obj_s->baudrate = 9600; // baudrate default value
Anna Bridge 180:96ed750bd169 219 if (obj_s->uart == STDIO_UART) {
Anna Bridge 180:96ed750bd169 220 #if MBED_CONF_PLATFORM_STDIO_BAUD_RATE
Anna Bridge 180:96ed750bd169 221 obj_s->baudrate = MBED_CONF_PLATFORM_STDIO_BAUD_RATE; // baudrate takes value from platform/mbed_lib.json
Anna Bridge 180:96ed750bd169 222 #endif /* MBED_CONF_PLATFORM_STDIO_BAUD_RATE */
Anna Bridge 180:96ed750bd169 223 }
Anna Bridge 180:96ed750bd169 224 else {
Anna Bridge 180:96ed750bd169 225 #if MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE
Anna Bridge 180:96ed750bd169 226 obj_s->baudrate = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE; // baudrate takes value from platform/mbed_lib.json
Anna Bridge 180:96ed750bd169 227 #endif /* MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE */
Anna Bridge 180:96ed750bd169 228 }
Anna Bridge 180:96ed750bd169 229 obj_s->databits = UART_WORDLENGTH_8B;
Anna Bridge 180:96ed750bd169 230 obj_s->stopbits = UART_STOPBITS_1;
Anna Bridge 180:96ed750bd169 231 obj_s->parity = UART_PARITY_NONE;
Anna Bridge 180:96ed750bd169 232
Anna Bridge 180:96ed750bd169 233 #if DEVICE_SERIAL_FC
Anna Bridge 180:96ed750bd169 234 obj_s->hw_flow_ctl = UART_HWCONTROL_NONE;
Anna Bridge 180:96ed750bd169 235 #endif
Anna Bridge 180:96ed750bd169 236
Anna Bridge 180:96ed750bd169 237 obj_s->pin_tx = tx;
Anna Bridge 180:96ed750bd169 238 obj_s->pin_rx = rx;
Anna Bridge 180:96ed750bd169 239
Anna Bridge 180:96ed750bd169 240 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 241
Anna Bridge 180:96ed750bd169 242 // For stdio management
Anna Bridge 180:96ed750bd169 243 if (obj_s->uart == STDIO_UART) { // STDIO_UART defined in PeripheralNames.h
Anna Bridge 180:96ed750bd169 244 stdio_uart_inited = 1;
Anna Bridge 180:96ed750bd169 245 memcpy(&stdio_uart, obj, sizeof(serial_t));
Anna Bridge 180:96ed750bd169 246 }
Anna Bridge 180:96ed750bd169 247 }
Anna Bridge 180:96ed750bd169 248
Anna Bridge 180:96ed750bd169 249 void serial_free(serial_t *obj)
Anna Bridge 180:96ed750bd169 250 {
Anna Bridge 180:96ed750bd169 251 struct serial_s *obj_s = SERIAL_S(obj);
Anna Bridge 180:96ed750bd169 252
Anna Bridge 180:96ed750bd169 253 // Reset UART and disable clock
Anna Bridge 180:96ed750bd169 254 #if defined(USART1_BASE)
Anna Bridge 180:96ed750bd169 255 if (obj_s->uart == UART_1) {
Anna Bridge 180:96ed750bd169 256 __HAL_RCC_USART1_FORCE_RESET();
Anna Bridge 180:96ed750bd169 257 __HAL_RCC_USART1_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 258 __HAL_RCC_USART1_CLK_DISABLE();
Anna Bridge 180:96ed750bd169 259 }
Anna Bridge 180:96ed750bd169 260 #endif
Anna Bridge 180:96ed750bd169 261
Anna Bridge 180:96ed750bd169 262 #if defined(USART2_BASE)
Anna Bridge 180:96ed750bd169 263 if (obj_s->uart == UART_2) {
Anna Bridge 180:96ed750bd169 264 __HAL_RCC_USART2_FORCE_RESET();
Anna Bridge 180:96ed750bd169 265 __HAL_RCC_USART2_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 266 __HAL_RCC_USART2_CLK_DISABLE();
Anna Bridge 180:96ed750bd169 267 }
Anna Bridge 180:96ed750bd169 268 #endif
Anna Bridge 180:96ed750bd169 269
Anna Bridge 180:96ed750bd169 270 #if defined(USART3_BASE)
Anna Bridge 180:96ed750bd169 271 if (obj_s->uart == UART_3) {
Anna Bridge 180:96ed750bd169 272 __HAL_RCC_USART3_FORCE_RESET();
Anna Bridge 180:96ed750bd169 273 __HAL_RCC_USART3_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 274 __HAL_RCC_USART3_CLK_DISABLE();
Anna Bridge 180:96ed750bd169 275 }
Anna Bridge 180:96ed750bd169 276 #endif
Anna Bridge 180:96ed750bd169 277
Anna Bridge 180:96ed750bd169 278 #if defined(UART4_BASE)
Anna Bridge 180:96ed750bd169 279 if (obj_s->uart == UART_4) {
Anna Bridge 180:96ed750bd169 280 __HAL_RCC_UART4_FORCE_RESET();
Anna Bridge 180:96ed750bd169 281 __HAL_RCC_UART4_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 282 __HAL_RCC_UART4_CLK_DISABLE();
Anna Bridge 180:96ed750bd169 283 }
Anna Bridge 180:96ed750bd169 284 #endif
Anna Bridge 180:96ed750bd169 285
Anna Bridge 180:96ed750bd169 286 #if defined(USART4_BASE)
Anna Bridge 180:96ed750bd169 287 if (obj_s->uart == UART_4) {
Anna Bridge 180:96ed750bd169 288 __HAL_RCC_USART4_FORCE_RESET();
Anna Bridge 180:96ed750bd169 289 __HAL_RCC_USART4_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 290 __HAL_RCC_USART4_CLK_DISABLE();
Anna Bridge 180:96ed750bd169 291 }
Anna Bridge 180:96ed750bd169 292 #endif
Anna Bridge 180:96ed750bd169 293
Anna Bridge 180:96ed750bd169 294 #if defined(UART5_BASE)
Anna Bridge 180:96ed750bd169 295 if (obj_s->uart == UART_5) {
Anna Bridge 180:96ed750bd169 296 __HAL_RCC_UART5_FORCE_RESET();
Anna Bridge 180:96ed750bd169 297 __HAL_RCC_UART5_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 298 __HAL_RCC_UART5_CLK_DISABLE();
Anna Bridge 180:96ed750bd169 299 }
Anna Bridge 180:96ed750bd169 300 #endif
Anna Bridge 180:96ed750bd169 301
Anna Bridge 180:96ed750bd169 302 #if defined(USART5_BASE)
Anna Bridge 180:96ed750bd169 303 if (obj_s->uart == UART_5) {
Anna Bridge 180:96ed750bd169 304 __HAL_RCC_USART5_FORCE_RESET();
Anna Bridge 180:96ed750bd169 305 __HAL_RCC_USART5_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 306 __HAL_RCC_USART5_CLK_DISABLE();
Anna Bridge 180:96ed750bd169 307 }
Anna Bridge 180:96ed750bd169 308 #endif
Anna Bridge 180:96ed750bd169 309
Anna Bridge 180:96ed750bd169 310 #if defined(USART6_BASE)
Anna Bridge 180:96ed750bd169 311 if (obj_s->uart == UART_6) {
Anna Bridge 180:96ed750bd169 312 __HAL_RCC_USART6_FORCE_RESET();
Anna Bridge 180:96ed750bd169 313 __HAL_RCC_USART6_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 314 __HAL_RCC_USART6_CLK_DISABLE();
Anna Bridge 180:96ed750bd169 315 }
Anna Bridge 180:96ed750bd169 316 #endif
Anna Bridge 180:96ed750bd169 317
Anna Bridge 180:96ed750bd169 318 #if defined(UART7_BASE)
Anna Bridge 180:96ed750bd169 319 if (obj_s->uart == UART_7) {
Anna Bridge 180:96ed750bd169 320 __HAL_RCC_UART7_FORCE_RESET();
Anna Bridge 180:96ed750bd169 321 __HAL_RCC_UART7_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 322 __HAL_RCC_UART7_CLK_DISABLE();
Anna Bridge 180:96ed750bd169 323 }
Anna Bridge 180:96ed750bd169 324 #endif
Anna Bridge 180:96ed750bd169 325
Anna Bridge 180:96ed750bd169 326 #if defined(USART7_BASE)
Anna Bridge 180:96ed750bd169 327 if (obj_s->uart == UART_7) {
Anna Bridge 180:96ed750bd169 328 __HAL_RCC_USART7_FORCE_RESET();
Anna Bridge 180:96ed750bd169 329 __HAL_RCC_USART7_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 330 __HAL_RCC_USART7_CLK_DISABLE();
Anna Bridge 180:96ed750bd169 331 }
Anna Bridge 180:96ed750bd169 332 #endif
Anna Bridge 180:96ed750bd169 333
Anna Bridge 180:96ed750bd169 334 #if defined(UART8_BASE)
Anna Bridge 180:96ed750bd169 335 if (obj_s->uart == UART_8) {
Anna Bridge 180:96ed750bd169 336 __HAL_RCC_UART8_FORCE_RESET();
Anna Bridge 180:96ed750bd169 337 __HAL_RCC_UART8_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 338 __HAL_RCC_UART8_CLK_DISABLE();
Anna Bridge 180:96ed750bd169 339 }
Anna Bridge 180:96ed750bd169 340 #endif
Anna Bridge 180:96ed750bd169 341
Anna Bridge 180:96ed750bd169 342 #if defined(USART8_BASE)
Anna Bridge 180:96ed750bd169 343 if (obj_s->uart == UART_8) {
Anna Bridge 180:96ed750bd169 344 __HAL_RCC_USART8_FORCE_RESET();
Anna Bridge 180:96ed750bd169 345 __HAL_RCC_USART8_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 346 __HAL_RCC_USART8_CLK_DISABLE();
Anna Bridge 180:96ed750bd169 347 }
Anna Bridge 180:96ed750bd169 348 #endif
Anna Bridge 180:96ed750bd169 349
Anna Bridge 180:96ed750bd169 350 #if defined(UART9_BASE)
Anna Bridge 180:96ed750bd169 351 if (obj_s->uart == UART_9) {
Anna Bridge 180:96ed750bd169 352 __HAL_RCC_UART9_FORCE_RESET();
Anna Bridge 180:96ed750bd169 353 __HAL_RCC_UART9_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 354 __HAL_RCC_UART9_CLK_DISABLE();
Anna Bridge 180:96ed750bd169 355 }
Anna Bridge 180:96ed750bd169 356 #endif
Anna Bridge 180:96ed750bd169 357
Anna Bridge 180:96ed750bd169 358 #if defined(UART10_BASE)
Anna Bridge 180:96ed750bd169 359 if (obj_s->uart == UART_10) {
Anna Bridge 180:96ed750bd169 360 __HAL_RCC_UART10_FORCE_RESET();
Anna Bridge 180:96ed750bd169 361 __HAL_RCC_UART10_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 362 __HAL_RCC_UART10_CLK_DISABLE();
Anna Bridge 180:96ed750bd169 363 }
Anna Bridge 180:96ed750bd169 364 #endif
Anna Bridge 180:96ed750bd169 365
Anna Bridge 180:96ed750bd169 366 #if defined(LPUART1_BASE)
Anna Bridge 180:96ed750bd169 367 if (obj_s->uart == LPUART_1) {
Anna Bridge 180:96ed750bd169 368 __HAL_RCC_LPUART1_FORCE_RESET();
Anna Bridge 180:96ed750bd169 369 __HAL_RCC_LPUART1_RELEASE_RESET();
Anna Bridge 180:96ed750bd169 370 __HAL_RCC_LPUART1_CLK_DISABLE();
Anna Bridge 180:96ed750bd169 371 }
Anna Bridge 180:96ed750bd169 372 #endif
Anna Bridge 180:96ed750bd169 373
Anna Bridge 180:96ed750bd169 374 // Configure GPIOs
Anna Bridge 180:96ed750bd169 375 pin_function(obj_s->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
Anna Bridge 180:96ed750bd169 376 pin_function(obj_s->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
Anna Bridge 180:96ed750bd169 377
Anna Bridge 180:96ed750bd169 378 serial_irq_ids[obj_s->index] = 0;
Anna Bridge 180:96ed750bd169 379 }
Anna Bridge 180:96ed750bd169 380
Anna Bridge 180:96ed750bd169 381 void serial_baud(serial_t *obj, int baudrate)
Anna Bridge 180:96ed750bd169 382 {
Anna Bridge 180:96ed750bd169 383 struct serial_s *obj_s = SERIAL_S(obj);
Anna Bridge 180:96ed750bd169 384
Anna Bridge 180:96ed750bd169 385 obj_s->baudrate = baudrate;
Anna Bridge 180:96ed750bd169 386 if (init_uart(obj) != HAL_OK) {
Anna Bridge 180:96ed750bd169 387
Anna Bridge 180:96ed750bd169 388 #if defined(LPUART1_BASE)
Anna Bridge 180:96ed750bd169 389 /* 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 390 if (obj_s->uart == LPUART_1) {
Anna Bridge 180:96ed750bd169 391 /* Try to change LPUART clock source */
Anna Bridge 180:96ed750bd169 392 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
Anna Bridge 180:96ed750bd169 393 if (baudrate == 9600) {
Anna Bridge 180:96ed750bd169 394 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
Anna Bridge 180:96ed750bd169 395 PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_LSE;
Anna Bridge 180:96ed750bd169 396 HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
Anna Bridge 180:96ed750bd169 397 if (init_uart(obj) == HAL_OK){
Anna Bridge 180:96ed750bd169 398 return;
Anna Bridge 180:96ed750bd169 399 }
Anna Bridge 180:96ed750bd169 400 }
Anna Bridge 180:96ed750bd169 401 else {
Anna Bridge 180:96ed750bd169 402 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
Anna Bridge 180:96ed750bd169 403 PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_SYSCLK;
Anna Bridge 180:96ed750bd169 404 HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
Anna Bridge 180:96ed750bd169 405 if (init_uart(obj) == HAL_OK){
Anna Bridge 180:96ed750bd169 406 return;
Anna Bridge 180:96ed750bd169 407 }
Anna Bridge 180:96ed750bd169 408 }
Anna Bridge 180:96ed750bd169 409 }
Anna Bridge 180:96ed750bd169 410 #endif /* LPUART1_BASE */
Anna Bridge 180:96ed750bd169 411
Anna Bridge 180:96ed750bd169 412 debug("Cannot initialize UART with baud rate %u\n", baudrate);
Anna Bridge 180:96ed750bd169 413 }
Anna Bridge 180:96ed750bd169 414 }
Anna Bridge 180:96ed750bd169 415
Anna Bridge 180:96ed750bd169 416 HAL_StatusTypeDef init_uart(serial_t *obj)
AnnaBridge 167:e84263d55307 417 {
AnnaBridge 167:e84263d55307 418 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 419 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
AnnaBridge 167:e84263d55307 420 huart->Instance = (USART_TypeDef *)(obj_s->uart);
AnnaBridge 167:e84263d55307 421
AnnaBridge 167:e84263d55307 422 huart->Init.BaudRate = obj_s->baudrate;
AnnaBridge 167:e84263d55307 423 huart->Init.WordLength = obj_s->databits;
AnnaBridge 167:e84263d55307 424 huart->Init.StopBits = obj_s->stopbits;
AnnaBridge 167:e84263d55307 425 huart->Init.Parity = obj_s->parity;
AnnaBridge 167:e84263d55307 426 #if DEVICE_SERIAL_FC
AnnaBridge 167:e84263d55307 427 huart->Init.HwFlowCtl = obj_s->hw_flow_ctl;
AnnaBridge 167:e84263d55307 428 #else
AnnaBridge 167:e84263d55307 429 huart->Init.HwFlowCtl = UART_HWCONTROL_NONE;
AnnaBridge 167:e84263d55307 430 #endif
AnnaBridge 167:e84263d55307 431 huart->Init.OverSampling = UART_OVERSAMPLING_16;
AnnaBridge 167:e84263d55307 432 huart->TxXferCount = 0;
AnnaBridge 167:e84263d55307 433 huart->TxXferSize = 0;
AnnaBridge 167:e84263d55307 434 huart->RxXferCount = 0;
AnnaBridge 167:e84263d55307 435 huart->RxXferSize = 0;
AnnaBridge 167:e84263d55307 436
AnnaBridge 167:e84263d55307 437 if (obj_s->pin_rx == NC) {
AnnaBridge 167:e84263d55307 438 huart->Init.Mode = UART_MODE_TX;
AnnaBridge 167:e84263d55307 439 } else if (obj_s->pin_tx == NC) {
AnnaBridge 167:e84263d55307 440 huart->Init.Mode = UART_MODE_RX;
AnnaBridge 167:e84263d55307 441 } else {
AnnaBridge 167:e84263d55307 442 huart->Init.Mode = UART_MODE_TX_RX;
AnnaBridge 167:e84263d55307 443 }
AnnaBridge 167:e84263d55307 444
Anna Bridge 180:96ed750bd169 445 return HAL_UART_Init(huart);
AnnaBridge 167:e84263d55307 446 }
AnnaBridge 167:e84263d55307 447
AnnaBridge 167:e84263d55307 448 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
AnnaBridge 167:e84263d55307 449 {
AnnaBridge 167:e84263d55307 450 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 451
AnnaBridge 167:e84263d55307 452 switch (parity) {
AnnaBridge 167:e84263d55307 453 case ParityOdd:
AnnaBridge 167:e84263d55307 454 obj_s->parity = UART_PARITY_ODD;
AnnaBridge 167:e84263d55307 455 break;
AnnaBridge 167:e84263d55307 456 case ParityEven:
AnnaBridge 167:e84263d55307 457 obj_s->parity = UART_PARITY_EVEN;
AnnaBridge 167:e84263d55307 458 break;
AnnaBridge 167:e84263d55307 459 default: // ParityNone
AnnaBridge 167:e84263d55307 460 case ParityForced0: // unsupported!
AnnaBridge 167:e84263d55307 461 case ParityForced1: // unsupported!
AnnaBridge 167:e84263d55307 462 obj_s->parity = UART_PARITY_NONE;
AnnaBridge 167:e84263d55307 463 break;
AnnaBridge 167:e84263d55307 464 }
AnnaBridge 167:e84263d55307 465
AnnaBridge 167:e84263d55307 466 switch (data_bits) {
AnnaBridge 167:e84263d55307 467 case 9:
AnnaBridge 167:e84263d55307 468 MBED_ASSERT(parity == UART_PARITY_NONE);
AnnaBridge 167:e84263d55307 469 obj_s->databits = UART_WORDLENGTH_9B;
AnnaBridge 167:e84263d55307 470 break;
AnnaBridge 167:e84263d55307 471 default:
AnnaBridge 167:e84263d55307 472 case 8:
AnnaBridge 167:e84263d55307 473 if (parity != UART_PARITY_NONE) {
AnnaBridge 167:e84263d55307 474 obj_s->databits = UART_WORDLENGTH_9B;
AnnaBridge 167:e84263d55307 475 } else {
AnnaBridge 167:e84263d55307 476 obj_s->databits = UART_WORDLENGTH_8B;
AnnaBridge 167:e84263d55307 477 }
AnnaBridge 167:e84263d55307 478 break;
AnnaBridge 167:e84263d55307 479 #if defined UART_WORDLENGTH_7B
AnnaBridge 167:e84263d55307 480 case 7:
AnnaBridge 167:e84263d55307 481 if (parity != UART_PARITY_NONE) {
AnnaBridge 167:e84263d55307 482 obj_s->databits = UART_WORDLENGTH_8B;
AnnaBridge 167:e84263d55307 483 } else {
AnnaBridge 167:e84263d55307 484 obj_s->databits = UART_WORDLENGTH_7B;
AnnaBridge 167:e84263d55307 485 }
AnnaBridge 167:e84263d55307 486 break;
AnnaBridge 167:e84263d55307 487 #endif
AnnaBridge 167:e84263d55307 488 }
AnnaBridge 167:e84263d55307 489
AnnaBridge 167:e84263d55307 490 if (stop_bits == 2) {
AnnaBridge 167:e84263d55307 491 obj_s->stopbits = UART_STOPBITS_2;
AnnaBridge 167:e84263d55307 492 } else {
AnnaBridge 167:e84263d55307 493 obj_s->stopbits = UART_STOPBITS_1;
AnnaBridge 167:e84263d55307 494 }
AnnaBridge 167:e84263d55307 495
AnnaBridge 167:e84263d55307 496 init_uart(obj);
AnnaBridge 167:e84263d55307 497 }
AnnaBridge 167:e84263d55307 498
AnnaBridge 167:e84263d55307 499 /******************************************************************************
AnnaBridge 167:e84263d55307 500 * READ/WRITE
AnnaBridge 167:e84263d55307 501 ******************************************************************************/
AnnaBridge 167:e84263d55307 502
AnnaBridge 167:e84263d55307 503 int serial_readable(serial_t *obj)
AnnaBridge 167:e84263d55307 504 {
AnnaBridge 167:e84263d55307 505 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 506 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
AnnaBridge 167:e84263d55307 507 /* To avoid a target blocking case, let's check for
AnnaBridge 167:e84263d55307 508 * possible OVERRUN error and discard it
AnnaBridge 167:e84263d55307 509 */
AnnaBridge 167:e84263d55307 510 if(__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE)) {
AnnaBridge 167:e84263d55307 511 __HAL_UART_CLEAR_OREFLAG(huart);
AnnaBridge 167:e84263d55307 512 }
AnnaBridge 167:e84263d55307 513 // Check if data is received
AnnaBridge 167:e84263d55307 514 return (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) ? 1 : 0;
AnnaBridge 167:e84263d55307 515 }
AnnaBridge 167:e84263d55307 516
AnnaBridge 167:e84263d55307 517 int serial_writable(serial_t *obj)
AnnaBridge 167:e84263d55307 518 {
AnnaBridge 167:e84263d55307 519 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 520 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
AnnaBridge 167:e84263d55307 521
AnnaBridge 167:e84263d55307 522 // Check if data is transmitted
AnnaBridge 167:e84263d55307 523 return (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) ? 1 : 0;
AnnaBridge 167:e84263d55307 524 }
AnnaBridge 167:e84263d55307 525
AnnaBridge 167:e84263d55307 526 void serial_pinout_tx(PinName tx)
AnnaBridge 167:e84263d55307 527 {
AnnaBridge 167:e84263d55307 528 pinmap_pinout(tx, PinMap_UART_TX);
AnnaBridge 167:e84263d55307 529 }
AnnaBridge 167:e84263d55307 530
AnnaBridge 167:e84263d55307 531 void serial_break_clear(serial_t *obj)
AnnaBridge 167:e84263d55307 532 {
AnnaBridge 167:e84263d55307 533 (void)obj;
AnnaBridge 167:e84263d55307 534 }
AnnaBridge 167:e84263d55307 535
AnnaBridge 167:e84263d55307 536 #endif /* DEVICE_SERIAL */