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