00
targets/TARGET_STM/TARGET_STM32F4/serial_device.c@0:13413ea9a877, 2022-06-12 (annotated)
- Committer:
- ganlikun
- Date:
- Sun Jun 12 14:02:44 2022 +0000
- Revision:
- 0:13413ea9a877
00
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ganlikun | 0:13413ea9a877 | 1 | /* mbed Microcontroller Library |
ganlikun | 0:13413ea9a877 | 2 | ******************************************************************************* |
ganlikun | 0:13413ea9a877 | 3 | * Copyright (c) 2015, STMicroelectronics |
ganlikun | 0:13413ea9a877 | 4 | * All rights reserved. |
ganlikun | 0:13413ea9a877 | 5 | * |
ganlikun | 0:13413ea9a877 | 6 | * Redistribution and use in source and binary forms, with or without |
ganlikun | 0:13413ea9a877 | 7 | * modification, are permitted provided that the following conditions are met: |
ganlikun | 0:13413ea9a877 | 8 | * |
ganlikun | 0:13413ea9a877 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
ganlikun | 0:13413ea9a877 | 10 | * this list of conditions and the following disclaimer. |
ganlikun | 0:13413ea9a877 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
ganlikun | 0:13413ea9a877 | 12 | * this list of conditions and the following disclaimer in the documentation |
ganlikun | 0:13413ea9a877 | 13 | * and/or other materials provided with the distribution. |
ganlikun | 0:13413ea9a877 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
ganlikun | 0:13413ea9a877 | 15 | * may be used to endorse or promote products derived from this software |
ganlikun | 0:13413ea9a877 | 16 | * without specific prior written permission. |
ganlikun | 0:13413ea9a877 | 17 | * |
ganlikun | 0:13413ea9a877 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
ganlikun | 0:13413ea9a877 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
ganlikun | 0:13413ea9a877 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
ganlikun | 0:13413ea9a877 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
ganlikun | 0:13413ea9a877 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
ganlikun | 0:13413ea9a877 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
ganlikun | 0:13413ea9a877 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
ganlikun | 0:13413ea9a877 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
ganlikun | 0:13413ea9a877 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
ganlikun | 0:13413ea9a877 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
ganlikun | 0:13413ea9a877 | 28 | ******************************************************************************* |
ganlikun | 0:13413ea9a877 | 29 | */ |
ganlikun | 0:13413ea9a877 | 30 | |
ganlikun | 0:13413ea9a877 | 31 | #include "mbed_assert.h" |
ganlikun | 0:13413ea9a877 | 32 | #include "serial_api.h" |
ganlikun | 0:13413ea9a877 | 33 | #include "serial_api_hal.h" |
ganlikun | 0:13413ea9a877 | 34 | |
ganlikun | 0:13413ea9a877 | 35 | #if DEVICE_SERIAL |
ganlikun | 0:13413ea9a877 | 36 | |
ganlikun | 0:13413ea9a877 | 37 | #include "cmsis.h" |
ganlikun | 0:13413ea9a877 | 38 | #include "pinmap.h" |
ganlikun | 0:13413ea9a877 | 39 | #include <string.h> |
ganlikun | 0:13413ea9a877 | 40 | #include "PeripheralPins.h" |
ganlikun | 0:13413ea9a877 | 41 | #include "mbed_error.h" |
ganlikun | 0:13413ea9a877 | 42 | |
ganlikun | 0:13413ea9a877 | 43 | #define UART_NUM (10) |
ganlikun | 0:13413ea9a877 | 44 | static uint32_t serial_irq_ids[UART_NUM] = {0}; |
ganlikun | 0:13413ea9a877 | 45 | UART_HandleTypeDef uart_handlers[UART_NUM]; |
ganlikun | 0:13413ea9a877 | 46 | |
ganlikun | 0:13413ea9a877 | 47 | static uart_irq_handler irq_handler; |
ganlikun | 0:13413ea9a877 | 48 | |
ganlikun | 0:13413ea9a877 | 49 | int stdio_uart_inited = 0; |
ganlikun | 0:13413ea9a877 | 50 | serial_t stdio_uart; |
ganlikun | 0:13413ea9a877 | 51 | |
ganlikun | 0:13413ea9a877 | 52 | void serial_init(serial_t *obj, PinName tx, PinName rx) |
ganlikun | 0:13413ea9a877 | 53 | { |
ganlikun | 0:13413ea9a877 | 54 | struct serial_s *obj_s = SERIAL_S(obj); |
ganlikun | 0:13413ea9a877 | 55 | |
ganlikun | 0:13413ea9a877 | 56 | // Determine the UART to use (UART_1, UART_2, ...) |
ganlikun | 0:13413ea9a877 | 57 | UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); |
ganlikun | 0:13413ea9a877 | 58 | UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); |
ganlikun | 0:13413ea9a877 | 59 | |
ganlikun | 0:13413ea9a877 | 60 | // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object |
ganlikun | 0:13413ea9a877 | 61 | obj_s->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); |
ganlikun | 0:13413ea9a877 | 62 | MBED_ASSERT(obj_s->uart != (UARTName)NC); |
ganlikun | 0:13413ea9a877 | 63 | |
ganlikun | 0:13413ea9a877 | 64 | // Enable USART clock |
ganlikun | 0:13413ea9a877 | 65 | switch (obj_s->uart) { |
ganlikun | 0:13413ea9a877 | 66 | case UART_1: |
ganlikun | 0:13413ea9a877 | 67 | __HAL_RCC_USART1_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 68 | __HAL_RCC_USART1_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 69 | __HAL_RCC_USART1_CLK_ENABLE(); |
ganlikun | 0:13413ea9a877 | 70 | obj_s->index = 0; |
ganlikun | 0:13413ea9a877 | 71 | break; |
ganlikun | 0:13413ea9a877 | 72 | |
ganlikun | 0:13413ea9a877 | 73 | case UART_2: |
ganlikun | 0:13413ea9a877 | 74 | __HAL_RCC_USART2_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 75 | __HAL_RCC_USART2_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 76 | __HAL_RCC_USART2_CLK_ENABLE(); |
ganlikun | 0:13413ea9a877 | 77 | obj_s->index = 1; |
ganlikun | 0:13413ea9a877 | 78 | break; |
ganlikun | 0:13413ea9a877 | 79 | #if defined(USART3_BASE) |
ganlikun | 0:13413ea9a877 | 80 | case UART_3: |
ganlikun | 0:13413ea9a877 | 81 | __HAL_RCC_USART3_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 82 | __HAL_RCC_USART3_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 83 | __HAL_RCC_USART3_CLK_ENABLE(); |
ganlikun | 0:13413ea9a877 | 84 | obj_s->index = 2; |
ganlikun | 0:13413ea9a877 | 85 | break; |
ganlikun | 0:13413ea9a877 | 86 | #endif |
ganlikun | 0:13413ea9a877 | 87 | #if defined(UART4_BASE) |
ganlikun | 0:13413ea9a877 | 88 | case UART_4: |
ganlikun | 0:13413ea9a877 | 89 | __HAL_RCC_UART4_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 90 | __HAL_RCC_UART4_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 91 | __HAL_RCC_UART4_CLK_ENABLE(); |
ganlikun | 0:13413ea9a877 | 92 | obj_s->index = 3; |
ganlikun | 0:13413ea9a877 | 93 | break; |
ganlikun | 0:13413ea9a877 | 94 | #endif |
ganlikun | 0:13413ea9a877 | 95 | #if defined(UART5_BASE) |
ganlikun | 0:13413ea9a877 | 96 | case UART_5: |
ganlikun | 0:13413ea9a877 | 97 | __HAL_RCC_UART5_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 98 | __HAL_RCC_UART5_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 99 | __HAL_RCC_UART5_CLK_ENABLE(); |
ganlikun | 0:13413ea9a877 | 100 | obj_s->index = 4; |
ganlikun | 0:13413ea9a877 | 101 | break; |
ganlikun | 0:13413ea9a877 | 102 | #endif |
ganlikun | 0:13413ea9a877 | 103 | #if defined(USART6_BASE) |
ganlikun | 0:13413ea9a877 | 104 | case UART_6: |
ganlikun | 0:13413ea9a877 | 105 | __HAL_RCC_USART6_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 106 | __HAL_RCC_USART6_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 107 | __HAL_RCC_USART6_CLK_ENABLE(); |
ganlikun | 0:13413ea9a877 | 108 | obj_s->index = 5; |
ganlikun | 0:13413ea9a877 | 109 | break; |
ganlikun | 0:13413ea9a877 | 110 | #endif |
ganlikun | 0:13413ea9a877 | 111 | #if defined(UART7_BASE) |
ganlikun | 0:13413ea9a877 | 112 | case UART_7: |
ganlikun | 0:13413ea9a877 | 113 | __HAL_RCC_UART7_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 114 | __HAL_RCC_UART7_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 115 | __HAL_RCC_UART7_CLK_ENABLE(); |
ganlikun | 0:13413ea9a877 | 116 | obj_s->index = 6; |
ganlikun | 0:13413ea9a877 | 117 | break; |
ganlikun | 0:13413ea9a877 | 118 | #endif |
ganlikun | 0:13413ea9a877 | 119 | #if defined(UART8_BASE) |
ganlikun | 0:13413ea9a877 | 120 | case UART_8: |
ganlikun | 0:13413ea9a877 | 121 | __HAL_RCC_UART8_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 122 | __HAL_RCC_UART8_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 123 | __HAL_RCC_UART8_CLK_ENABLE(); |
ganlikun | 0:13413ea9a877 | 124 | obj_s->index = 7; |
ganlikun | 0:13413ea9a877 | 125 | break; |
ganlikun | 0:13413ea9a877 | 126 | #endif |
ganlikun | 0:13413ea9a877 | 127 | #if defined(UART9_BASE) |
ganlikun | 0:13413ea9a877 | 128 | case UART_9: |
ganlikun | 0:13413ea9a877 | 129 | __HAL_RCC_UART9_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 130 | __HAL_RCC_UART9_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 131 | __HAL_RCC_UART9_CLK_ENABLE(); |
ganlikun | 0:13413ea9a877 | 132 | obj_s->index = 8; |
ganlikun | 0:13413ea9a877 | 133 | break; |
ganlikun | 0:13413ea9a877 | 134 | #endif |
ganlikun | 0:13413ea9a877 | 135 | #if defined(UART10_BASE) |
ganlikun | 0:13413ea9a877 | 136 | case UART_10: |
ganlikun | 0:13413ea9a877 | 137 | __HAL_RCC_UART10_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 138 | __HAL_RCC_UART10_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 139 | __HAL_RCC_UART10_CLK_ENABLE(); |
ganlikun | 0:13413ea9a877 | 140 | obj_s->index = 9; |
ganlikun | 0:13413ea9a877 | 141 | break; |
ganlikun | 0:13413ea9a877 | 142 | #endif |
ganlikun | 0:13413ea9a877 | 143 | } |
ganlikun | 0:13413ea9a877 | 144 | |
ganlikun | 0:13413ea9a877 | 145 | // Configure the UART pins |
ganlikun | 0:13413ea9a877 | 146 | pinmap_pinout(tx, PinMap_UART_TX); |
ganlikun | 0:13413ea9a877 | 147 | pinmap_pinout(rx, PinMap_UART_RX); |
ganlikun | 0:13413ea9a877 | 148 | |
ganlikun | 0:13413ea9a877 | 149 | if (tx != NC) { |
ganlikun | 0:13413ea9a877 | 150 | pin_mode(tx, PullUp); |
ganlikun | 0:13413ea9a877 | 151 | } |
ganlikun | 0:13413ea9a877 | 152 | if (rx != NC) { |
ganlikun | 0:13413ea9a877 | 153 | pin_mode(rx, PullUp); |
ganlikun | 0:13413ea9a877 | 154 | } |
ganlikun | 0:13413ea9a877 | 155 | |
ganlikun | 0:13413ea9a877 | 156 | // Configure UART |
ganlikun | 0:13413ea9a877 | 157 | obj_s->baudrate = 9600; |
ganlikun | 0:13413ea9a877 | 158 | obj_s->databits = UART_WORDLENGTH_8B; |
ganlikun | 0:13413ea9a877 | 159 | obj_s->stopbits = UART_STOPBITS_1; |
ganlikun | 0:13413ea9a877 | 160 | obj_s->parity = UART_PARITY_NONE; |
ganlikun | 0:13413ea9a877 | 161 | |
ganlikun | 0:13413ea9a877 | 162 | #if DEVICE_SERIAL_FC |
ganlikun | 0:13413ea9a877 | 163 | obj_s->hw_flow_ctl = UART_HWCONTROL_NONE; |
ganlikun | 0:13413ea9a877 | 164 | #endif |
ganlikun | 0:13413ea9a877 | 165 | |
ganlikun | 0:13413ea9a877 | 166 | obj_s->pin_tx = tx; |
ganlikun | 0:13413ea9a877 | 167 | obj_s->pin_rx = rx; |
ganlikun | 0:13413ea9a877 | 168 | |
ganlikun | 0:13413ea9a877 | 169 | init_uart(obj); |
ganlikun | 0:13413ea9a877 | 170 | |
ganlikun | 0:13413ea9a877 | 171 | // For stdio management |
ganlikun | 0:13413ea9a877 | 172 | if (obj_s->uart == STDIO_UART) { |
ganlikun | 0:13413ea9a877 | 173 | stdio_uart_inited = 1; |
ganlikun | 0:13413ea9a877 | 174 | memcpy(&stdio_uart, obj, sizeof(serial_t)); |
ganlikun | 0:13413ea9a877 | 175 | } |
ganlikun | 0:13413ea9a877 | 176 | } |
ganlikun | 0:13413ea9a877 | 177 | |
ganlikun | 0:13413ea9a877 | 178 | void serial_free(serial_t *obj) |
ganlikun | 0:13413ea9a877 | 179 | { |
ganlikun | 0:13413ea9a877 | 180 | struct serial_s *obj_s = SERIAL_S(obj); |
ganlikun | 0:13413ea9a877 | 181 | |
ganlikun | 0:13413ea9a877 | 182 | // Reset UART and disable clock |
ganlikun | 0:13413ea9a877 | 183 | switch (obj_s->index) { |
ganlikun | 0:13413ea9a877 | 184 | case 0: |
ganlikun | 0:13413ea9a877 | 185 | __HAL_RCC_USART1_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 186 | __HAL_RCC_USART1_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 187 | __HAL_RCC_USART1_CLK_DISABLE(); |
ganlikun | 0:13413ea9a877 | 188 | break; |
ganlikun | 0:13413ea9a877 | 189 | case 1: |
ganlikun | 0:13413ea9a877 | 190 | __HAL_RCC_USART2_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 191 | __HAL_RCC_USART2_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 192 | __HAL_RCC_USART2_CLK_DISABLE(); |
ganlikun | 0:13413ea9a877 | 193 | break; |
ganlikun | 0:13413ea9a877 | 194 | #if defined(USART3_BASE) |
ganlikun | 0:13413ea9a877 | 195 | case 2: |
ganlikun | 0:13413ea9a877 | 196 | __HAL_RCC_USART3_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 197 | __HAL_RCC_USART3_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 198 | __HAL_RCC_USART3_CLK_DISABLE(); |
ganlikun | 0:13413ea9a877 | 199 | break; |
ganlikun | 0:13413ea9a877 | 200 | #endif |
ganlikun | 0:13413ea9a877 | 201 | #if defined(UART4_BASE) |
ganlikun | 0:13413ea9a877 | 202 | case 3: |
ganlikun | 0:13413ea9a877 | 203 | __HAL_RCC_UART4_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 204 | __HAL_RCC_UART4_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 205 | __HAL_RCC_UART4_CLK_DISABLE(); |
ganlikun | 0:13413ea9a877 | 206 | break; |
ganlikun | 0:13413ea9a877 | 207 | #endif |
ganlikun | 0:13413ea9a877 | 208 | #if defined(UART5_BASE) |
ganlikun | 0:13413ea9a877 | 209 | case 4: |
ganlikun | 0:13413ea9a877 | 210 | __HAL_RCC_UART5_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 211 | __HAL_RCC_UART5_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 212 | __HAL_RCC_UART5_CLK_DISABLE(); |
ganlikun | 0:13413ea9a877 | 213 | break; |
ganlikun | 0:13413ea9a877 | 214 | #endif |
ganlikun | 0:13413ea9a877 | 215 | #if defined(USART6_BASE) |
ganlikun | 0:13413ea9a877 | 216 | case 5: |
ganlikun | 0:13413ea9a877 | 217 | __HAL_RCC_USART6_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 218 | __HAL_RCC_USART6_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 219 | __HAL_RCC_USART6_CLK_DISABLE(); |
ganlikun | 0:13413ea9a877 | 220 | break; |
ganlikun | 0:13413ea9a877 | 221 | #endif |
ganlikun | 0:13413ea9a877 | 222 | #if defined(UART7_BASE) |
ganlikun | 0:13413ea9a877 | 223 | case 6: |
ganlikun | 0:13413ea9a877 | 224 | __HAL_RCC_UART7_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 225 | __HAL_RCC_UART7_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 226 | __HAL_RCC_UART7_CLK_DISABLE(); |
ganlikun | 0:13413ea9a877 | 227 | break; |
ganlikun | 0:13413ea9a877 | 228 | #endif |
ganlikun | 0:13413ea9a877 | 229 | #if defined(UART8_BASE) |
ganlikun | 0:13413ea9a877 | 230 | case 7: |
ganlikun | 0:13413ea9a877 | 231 | __HAL_RCC_UART8_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 232 | __HAL_RCC_UART8_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 233 | __HAL_RCC_UART8_CLK_DISABLE(); |
ganlikun | 0:13413ea9a877 | 234 | break; |
ganlikun | 0:13413ea9a877 | 235 | #endif |
ganlikun | 0:13413ea9a877 | 236 | #if defined(UART9_BASE) |
ganlikun | 0:13413ea9a877 | 237 | case 8: |
ganlikun | 0:13413ea9a877 | 238 | __HAL_RCC_UART9_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 239 | __HAL_RCC_UART9_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 240 | __HAL_RCC_UART9_CLK_DISABLE(); |
ganlikun | 0:13413ea9a877 | 241 | break; |
ganlikun | 0:13413ea9a877 | 242 | #endif |
ganlikun | 0:13413ea9a877 | 243 | #if defined(UART10_BASE) |
ganlikun | 0:13413ea9a877 | 244 | case 9: |
ganlikun | 0:13413ea9a877 | 245 | __HAL_RCC_UART10_FORCE_RESET(); |
ganlikun | 0:13413ea9a877 | 246 | __HAL_RCC_UART10_RELEASE_RESET(); |
ganlikun | 0:13413ea9a877 | 247 | __HAL_RCC_UART10_CLK_DISABLE(); |
ganlikun | 0:13413ea9a877 | 248 | break; |
ganlikun | 0:13413ea9a877 | 249 | #endif |
ganlikun | 0:13413ea9a877 | 250 | } |
ganlikun | 0:13413ea9a877 | 251 | |
ganlikun | 0:13413ea9a877 | 252 | // Configure GPIOs |
ganlikun | 0:13413ea9a877 | 253 | pin_function(obj_s->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
ganlikun | 0:13413ea9a877 | 254 | pin_function(obj_s->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
ganlikun | 0:13413ea9a877 | 255 | |
ganlikun | 0:13413ea9a877 | 256 | serial_irq_ids[obj_s->index] = 0; |
ganlikun | 0:13413ea9a877 | 257 | } |
ganlikun | 0:13413ea9a877 | 258 | |
ganlikun | 0:13413ea9a877 | 259 | void serial_baud(serial_t *obj, int baudrate) |
ganlikun | 0:13413ea9a877 | 260 | { |
ganlikun | 0:13413ea9a877 | 261 | struct serial_s *obj_s = SERIAL_S(obj); |
ganlikun | 0:13413ea9a877 | 262 | |
ganlikun | 0:13413ea9a877 | 263 | obj_s->baudrate = baudrate; |
ganlikun | 0:13413ea9a877 | 264 | init_uart(obj); |
ganlikun | 0:13413ea9a877 | 265 | } |
ganlikun | 0:13413ea9a877 | 266 | |
ganlikun | 0:13413ea9a877 | 267 | /****************************************************************************** |
ganlikun | 0:13413ea9a877 | 268 | * INTERRUPTS HANDLING |
ganlikun | 0:13413ea9a877 | 269 | ******************************************************************************/ |
ganlikun | 0:13413ea9a877 | 270 | |
ganlikun | 0:13413ea9a877 | 271 | static void uart_irq(int id) |
ganlikun | 0:13413ea9a877 | 272 | { |
ganlikun | 0:13413ea9a877 | 273 | UART_HandleTypeDef * huart = &uart_handlers[id]; |
ganlikun | 0:13413ea9a877 | 274 | |
ganlikun | 0:13413ea9a877 | 275 | if (serial_irq_ids[id] != 0) { |
ganlikun | 0:13413ea9a877 | 276 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { |
ganlikun | 0:13413ea9a877 | 277 | if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE) != RESET) { |
ganlikun | 0:13413ea9a877 | 278 | irq_handler(serial_irq_ids[id], TxIrq); |
ganlikun | 0:13413ea9a877 | 279 | } |
ganlikun | 0:13413ea9a877 | 280 | } |
ganlikun | 0:13413ea9a877 | 281 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { |
ganlikun | 0:13413ea9a877 | 282 | if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) != RESET) { |
ganlikun | 0:13413ea9a877 | 283 | irq_handler(serial_irq_ids[id], RxIrq); |
ganlikun | 0:13413ea9a877 | 284 | /* Flag has been cleared when reading the content */ |
ganlikun | 0:13413ea9a877 | 285 | } |
ganlikun | 0:13413ea9a877 | 286 | } |
ganlikun | 0:13413ea9a877 | 287 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { |
ganlikun | 0:13413ea9a877 | 288 | if (__HAL_UART_GET_IT_SOURCE(huart, USART_IT_ERR) != RESET) { |
ganlikun | 0:13413ea9a877 | 289 | volatile uint32_t tmpval = huart->Instance->DR; // Clear ORE flag |
ganlikun | 0:13413ea9a877 | 290 | } |
ganlikun | 0:13413ea9a877 | 291 | } |
ganlikun | 0:13413ea9a877 | 292 | } |
ganlikun | 0:13413ea9a877 | 293 | } |
ganlikun | 0:13413ea9a877 | 294 | |
ganlikun | 0:13413ea9a877 | 295 | static void uart1_irq(void) |
ganlikun | 0:13413ea9a877 | 296 | { |
ganlikun | 0:13413ea9a877 | 297 | uart_irq(0); |
ganlikun | 0:13413ea9a877 | 298 | } |
ganlikun | 0:13413ea9a877 | 299 | |
ganlikun | 0:13413ea9a877 | 300 | static void uart2_irq(void) |
ganlikun | 0:13413ea9a877 | 301 | { |
ganlikun | 0:13413ea9a877 | 302 | uart_irq(1); |
ganlikun | 0:13413ea9a877 | 303 | } |
ganlikun | 0:13413ea9a877 | 304 | |
ganlikun | 0:13413ea9a877 | 305 | #if defined(USART3_BASE) |
ganlikun | 0:13413ea9a877 | 306 | static void uart3_irq(void) |
ganlikun | 0:13413ea9a877 | 307 | { |
ganlikun | 0:13413ea9a877 | 308 | uart_irq(2); |
ganlikun | 0:13413ea9a877 | 309 | } |
ganlikun | 0:13413ea9a877 | 310 | #endif |
ganlikun | 0:13413ea9a877 | 311 | |
ganlikun | 0:13413ea9a877 | 312 | #if defined(UART4_BASE) |
ganlikun | 0:13413ea9a877 | 313 | static void uart4_irq(void) |
ganlikun | 0:13413ea9a877 | 314 | { |
ganlikun | 0:13413ea9a877 | 315 | uart_irq(3); |
ganlikun | 0:13413ea9a877 | 316 | } |
ganlikun | 0:13413ea9a877 | 317 | #endif |
ganlikun | 0:13413ea9a877 | 318 | |
ganlikun | 0:13413ea9a877 | 319 | #if defined(UART5_BASE) |
ganlikun | 0:13413ea9a877 | 320 | static void uart5_irq(void) |
ganlikun | 0:13413ea9a877 | 321 | { |
ganlikun | 0:13413ea9a877 | 322 | uart_irq(4); |
ganlikun | 0:13413ea9a877 | 323 | } |
ganlikun | 0:13413ea9a877 | 324 | #endif |
ganlikun | 0:13413ea9a877 | 325 | |
ganlikun | 0:13413ea9a877 | 326 | #if defined(USART6_BASE) |
ganlikun | 0:13413ea9a877 | 327 | static void uart6_irq(void) |
ganlikun | 0:13413ea9a877 | 328 | { |
ganlikun | 0:13413ea9a877 | 329 | uart_irq(5); |
ganlikun | 0:13413ea9a877 | 330 | } |
ganlikun | 0:13413ea9a877 | 331 | #endif |
ganlikun | 0:13413ea9a877 | 332 | |
ganlikun | 0:13413ea9a877 | 333 | #if defined(UART7_BASE) |
ganlikun | 0:13413ea9a877 | 334 | static void uart7_irq(void) |
ganlikun | 0:13413ea9a877 | 335 | { |
ganlikun | 0:13413ea9a877 | 336 | uart_irq(6); |
ganlikun | 0:13413ea9a877 | 337 | } |
ganlikun | 0:13413ea9a877 | 338 | #endif |
ganlikun | 0:13413ea9a877 | 339 | |
ganlikun | 0:13413ea9a877 | 340 | #if defined(UART8_BASE) |
ganlikun | 0:13413ea9a877 | 341 | static void uart8_irq(void) |
ganlikun | 0:13413ea9a877 | 342 | { |
ganlikun | 0:13413ea9a877 | 343 | uart_irq(7); |
ganlikun | 0:13413ea9a877 | 344 | } |
ganlikun | 0:13413ea9a877 | 345 | #endif |
ganlikun | 0:13413ea9a877 | 346 | |
ganlikun | 0:13413ea9a877 | 347 | #if defined(UART9_BASE) |
ganlikun | 0:13413ea9a877 | 348 | static void uart9_irq(void) |
ganlikun | 0:13413ea9a877 | 349 | { |
ganlikun | 0:13413ea9a877 | 350 | uart_irq(8); |
ganlikun | 0:13413ea9a877 | 351 | } |
ganlikun | 0:13413ea9a877 | 352 | #endif |
ganlikun | 0:13413ea9a877 | 353 | |
ganlikun | 0:13413ea9a877 | 354 | #if defined(UART10_BASE) |
ganlikun | 0:13413ea9a877 | 355 | static void uart10_irq(void) |
ganlikun | 0:13413ea9a877 | 356 | { |
ganlikun | 0:13413ea9a877 | 357 | uart_irq(9); |
ganlikun | 0:13413ea9a877 | 358 | } |
ganlikun | 0:13413ea9a877 | 359 | #endif |
ganlikun | 0:13413ea9a877 | 360 | |
ganlikun | 0:13413ea9a877 | 361 | void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) |
ganlikun | 0:13413ea9a877 | 362 | { |
ganlikun | 0:13413ea9a877 | 363 | struct serial_s *obj_s = SERIAL_S(obj); |
ganlikun | 0:13413ea9a877 | 364 | |
ganlikun | 0:13413ea9a877 | 365 | irq_handler = handler; |
ganlikun | 0:13413ea9a877 | 366 | serial_irq_ids[obj_s->index] = id; |
ganlikun | 0:13413ea9a877 | 367 | } |
ganlikun | 0:13413ea9a877 | 368 | |
ganlikun | 0:13413ea9a877 | 369 | void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) |
ganlikun | 0:13413ea9a877 | 370 | { |
ganlikun | 0:13413ea9a877 | 371 | struct serial_s *obj_s = SERIAL_S(obj); |
ganlikun | 0:13413ea9a877 | 372 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
ganlikun | 0:13413ea9a877 | 373 | IRQn_Type irq_n = (IRQn_Type)0; |
ganlikun | 0:13413ea9a877 | 374 | uint32_t vector = 0; |
ganlikun | 0:13413ea9a877 | 375 | |
ganlikun | 0:13413ea9a877 | 376 | switch (obj_s->index) { |
ganlikun | 0:13413ea9a877 | 377 | case 0: |
ganlikun | 0:13413ea9a877 | 378 | irq_n = USART1_IRQn; |
ganlikun | 0:13413ea9a877 | 379 | vector = (uint32_t)&uart1_irq; |
ganlikun | 0:13413ea9a877 | 380 | break; |
ganlikun | 0:13413ea9a877 | 381 | |
ganlikun | 0:13413ea9a877 | 382 | case 1: |
ganlikun | 0:13413ea9a877 | 383 | irq_n = USART2_IRQn; |
ganlikun | 0:13413ea9a877 | 384 | vector = (uint32_t)&uart2_irq; |
ganlikun | 0:13413ea9a877 | 385 | break; |
ganlikun | 0:13413ea9a877 | 386 | #if defined(USART3_BASE) |
ganlikun | 0:13413ea9a877 | 387 | case 2: |
ganlikun | 0:13413ea9a877 | 388 | irq_n = USART3_IRQn; |
ganlikun | 0:13413ea9a877 | 389 | vector = (uint32_t)&uart3_irq; |
ganlikun | 0:13413ea9a877 | 390 | break; |
ganlikun | 0:13413ea9a877 | 391 | #endif |
ganlikun | 0:13413ea9a877 | 392 | #if defined(UART4_BASE) |
ganlikun | 0:13413ea9a877 | 393 | case 3: |
ganlikun | 0:13413ea9a877 | 394 | irq_n = UART4_IRQn; |
ganlikun | 0:13413ea9a877 | 395 | vector = (uint32_t)&uart4_irq; |
ganlikun | 0:13413ea9a877 | 396 | break; |
ganlikun | 0:13413ea9a877 | 397 | #endif |
ganlikun | 0:13413ea9a877 | 398 | #if defined(UART5_BASE) |
ganlikun | 0:13413ea9a877 | 399 | case 4: |
ganlikun | 0:13413ea9a877 | 400 | irq_n = UART5_IRQn; |
ganlikun | 0:13413ea9a877 | 401 | vector = (uint32_t)&uart5_irq; |
ganlikun | 0:13413ea9a877 | 402 | break; |
ganlikun | 0:13413ea9a877 | 403 | #endif |
ganlikun | 0:13413ea9a877 | 404 | #if defined(USART6_BASE) |
ganlikun | 0:13413ea9a877 | 405 | case 5: |
ganlikun | 0:13413ea9a877 | 406 | irq_n = USART6_IRQn; |
ganlikun | 0:13413ea9a877 | 407 | vector = (uint32_t)&uart6_irq; |
ganlikun | 0:13413ea9a877 | 408 | break; |
ganlikun | 0:13413ea9a877 | 409 | #endif |
ganlikun | 0:13413ea9a877 | 410 | #if defined(UART7_BASE) |
ganlikun | 0:13413ea9a877 | 411 | case 6: |
ganlikun | 0:13413ea9a877 | 412 | irq_n = UART7_IRQn; |
ganlikun | 0:13413ea9a877 | 413 | vector = (uint32_t)&uart7_irq; |
ganlikun | 0:13413ea9a877 | 414 | break; |
ganlikun | 0:13413ea9a877 | 415 | #endif |
ganlikun | 0:13413ea9a877 | 416 | #if defined(UART8_BASE) |
ganlikun | 0:13413ea9a877 | 417 | case 7: |
ganlikun | 0:13413ea9a877 | 418 | irq_n = UART8_IRQn; |
ganlikun | 0:13413ea9a877 | 419 | vector = (uint32_t)&uart8_irq; |
ganlikun | 0:13413ea9a877 | 420 | break; |
ganlikun | 0:13413ea9a877 | 421 | #endif |
ganlikun | 0:13413ea9a877 | 422 | #if defined(UART9_BASE) |
ganlikun | 0:13413ea9a877 | 423 | case 8: |
ganlikun | 0:13413ea9a877 | 424 | irq_n = UART9_IRQn; |
ganlikun | 0:13413ea9a877 | 425 | vector = (uint32_t)&uart9_irq; |
ganlikun | 0:13413ea9a877 | 426 | break; |
ganlikun | 0:13413ea9a877 | 427 | #endif |
ganlikun | 0:13413ea9a877 | 428 | #if defined(UART10_BASE) |
ganlikun | 0:13413ea9a877 | 429 | case 9: |
ganlikun | 0:13413ea9a877 | 430 | irq_n = UART10_IRQn; |
ganlikun | 0:13413ea9a877 | 431 | vector = (uint32_t)&uart10_irq; |
ganlikun | 0:13413ea9a877 | 432 | break; |
ganlikun | 0:13413ea9a877 | 433 | #endif |
ganlikun | 0:13413ea9a877 | 434 | } |
ganlikun | 0:13413ea9a877 | 435 | |
ganlikun | 0:13413ea9a877 | 436 | if (enable) { |
ganlikun | 0:13413ea9a877 | 437 | if (irq == RxIrq) { |
ganlikun | 0:13413ea9a877 | 438 | __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE); |
ganlikun | 0:13413ea9a877 | 439 | } else { // TxIrq |
ganlikun | 0:13413ea9a877 | 440 | __HAL_UART_ENABLE_IT(huart, UART_IT_TXE); |
ganlikun | 0:13413ea9a877 | 441 | } |
ganlikun | 0:13413ea9a877 | 442 | NVIC_SetVector(irq_n, vector); |
ganlikun | 0:13413ea9a877 | 443 | NVIC_EnableIRQ(irq_n); |
ganlikun | 0:13413ea9a877 | 444 | |
ganlikun | 0:13413ea9a877 | 445 | } else { // disable |
ganlikun | 0:13413ea9a877 | 446 | int all_disabled = 0; |
ganlikun | 0:13413ea9a877 | 447 | if (irq == RxIrq) { |
ganlikun | 0:13413ea9a877 | 448 | __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE); |
ganlikun | 0:13413ea9a877 | 449 | // Check if TxIrq is disabled too |
ganlikun | 0:13413ea9a877 | 450 | if ((huart->Instance->CR1 & USART_CR1_TXEIE) == 0) { |
ganlikun | 0:13413ea9a877 | 451 | all_disabled = 1; |
ganlikun | 0:13413ea9a877 | 452 | } |
ganlikun | 0:13413ea9a877 | 453 | } else { // TxIrq |
ganlikun | 0:13413ea9a877 | 454 | __HAL_UART_DISABLE_IT(huart, UART_IT_TXE); |
ganlikun | 0:13413ea9a877 | 455 | // Check if RxIrq is disabled too |
ganlikun | 0:13413ea9a877 | 456 | if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) { |
ganlikun | 0:13413ea9a877 | 457 | all_disabled = 1; |
ganlikun | 0:13413ea9a877 | 458 | } |
ganlikun | 0:13413ea9a877 | 459 | } |
ganlikun | 0:13413ea9a877 | 460 | |
ganlikun | 0:13413ea9a877 | 461 | if (all_disabled) { |
ganlikun | 0:13413ea9a877 | 462 | NVIC_DisableIRQ(irq_n); |
ganlikun | 0:13413ea9a877 | 463 | } |
ganlikun | 0:13413ea9a877 | 464 | } |
ganlikun | 0:13413ea9a877 | 465 | } |
ganlikun | 0:13413ea9a877 | 466 | |
ganlikun | 0:13413ea9a877 | 467 | /****************************************************************************** |
ganlikun | 0:13413ea9a877 | 468 | * READ/WRITE |
ganlikun | 0:13413ea9a877 | 469 | ******************************************************************************/ |
ganlikun | 0:13413ea9a877 | 470 | |
ganlikun | 0:13413ea9a877 | 471 | int serial_getc(serial_t *obj) |
ganlikun | 0:13413ea9a877 | 472 | { |
ganlikun | 0:13413ea9a877 | 473 | struct serial_s *obj_s = SERIAL_S(obj); |
ganlikun | 0:13413ea9a877 | 474 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
ganlikun | 0:13413ea9a877 | 475 | |
ganlikun | 0:13413ea9a877 | 476 | while (!serial_readable(obj)); |
ganlikun | 0:13413ea9a877 | 477 | return (int)(huart->Instance->DR & 0x1FF); |
ganlikun | 0:13413ea9a877 | 478 | } |
ganlikun | 0:13413ea9a877 | 479 | |
ganlikun | 0:13413ea9a877 | 480 | void serial_putc(serial_t *obj, int c) |
ganlikun | 0:13413ea9a877 | 481 | { |
ganlikun | 0:13413ea9a877 | 482 | struct serial_s *obj_s = SERIAL_S(obj); |
ganlikun | 0:13413ea9a877 | 483 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
ganlikun | 0:13413ea9a877 | 484 | |
ganlikun | 0:13413ea9a877 | 485 | while (!serial_writable(obj)); |
ganlikun | 0:13413ea9a877 | 486 | huart->Instance->DR = (uint32_t)(c & 0x1FF); |
ganlikun | 0:13413ea9a877 | 487 | } |
ganlikun | 0:13413ea9a877 | 488 | |
ganlikun | 0:13413ea9a877 | 489 | void serial_clear(serial_t *obj) |
ganlikun | 0:13413ea9a877 | 490 | { |
ganlikun | 0:13413ea9a877 | 491 | struct serial_s *obj_s = SERIAL_S(obj); |
ganlikun | 0:13413ea9a877 | 492 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
ganlikun | 0:13413ea9a877 | 493 | |
ganlikun | 0:13413ea9a877 | 494 | huart->TxXferCount = 0; |
ganlikun | 0:13413ea9a877 | 495 | huart->RxXferCount = 0; |
ganlikun | 0:13413ea9a877 | 496 | } |
ganlikun | 0:13413ea9a877 | 497 | |
ganlikun | 0:13413ea9a877 | 498 | void serial_break_set(serial_t *obj) |
ganlikun | 0:13413ea9a877 | 499 | { |
ganlikun | 0:13413ea9a877 | 500 | struct serial_s *obj_s = SERIAL_S(obj); |
ganlikun | 0:13413ea9a877 | 501 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
ganlikun | 0:13413ea9a877 | 502 | |
ganlikun | 0:13413ea9a877 | 503 | HAL_LIN_SendBreak(huart); |
ganlikun | 0:13413ea9a877 | 504 | } |
ganlikun | 0:13413ea9a877 | 505 | |
ganlikun | 0:13413ea9a877 | 506 | #if DEVICE_SERIAL_ASYNCH |
ganlikun | 0:13413ea9a877 | 507 | |
ganlikun | 0:13413ea9a877 | 508 | /****************************************************************************** |
ganlikun | 0:13413ea9a877 | 509 | * LOCAL HELPER FUNCTIONS |
ganlikun | 0:13413ea9a877 | 510 | ******************************************************************************/ |
ganlikun | 0:13413ea9a877 | 511 | |
ganlikun | 0:13413ea9a877 | 512 | /** |
ganlikun | 0:13413ea9a877 | 513 | * Configure the TX buffer for an asynchronous write serial transaction |
ganlikun | 0:13413ea9a877 | 514 | * |
ganlikun | 0:13413ea9a877 | 515 | * @param obj The serial object. |
ganlikun | 0:13413ea9a877 | 516 | * @param tx The buffer for sending. |
ganlikun | 0:13413ea9a877 | 517 | * @param tx_length The number of words to transmit. |
ganlikun | 0:13413ea9a877 | 518 | */ |
ganlikun | 0:13413ea9a877 | 519 | static void serial_tx_buffer_set(serial_t *obj, void *tx, int tx_length, uint8_t width) |
ganlikun | 0:13413ea9a877 | 520 | { |
ganlikun | 0:13413ea9a877 | 521 | (void)width; |
ganlikun | 0:13413ea9a877 | 522 | |
ganlikun | 0:13413ea9a877 | 523 | // Exit if a transmit is already on-going |
ganlikun | 0:13413ea9a877 | 524 | if (serial_tx_active(obj)) { |
ganlikun | 0:13413ea9a877 | 525 | return; |
ganlikun | 0:13413ea9a877 | 526 | } |
ganlikun | 0:13413ea9a877 | 527 | |
ganlikun | 0:13413ea9a877 | 528 | obj->tx_buff.buffer = tx; |
ganlikun | 0:13413ea9a877 | 529 | obj->tx_buff.length = tx_length; |
ganlikun | 0:13413ea9a877 | 530 | obj->tx_buff.pos = 0; |
ganlikun | 0:13413ea9a877 | 531 | } |
ganlikun | 0:13413ea9a877 | 532 | |
ganlikun | 0:13413ea9a877 | 533 | /** |
ganlikun | 0:13413ea9a877 | 534 | * Configure the RX buffer for an asynchronous write serial transaction |
ganlikun | 0:13413ea9a877 | 535 | * |
ganlikun | 0:13413ea9a877 | 536 | * @param obj The serial object. |
ganlikun | 0:13413ea9a877 | 537 | * @param tx The buffer for sending. |
ganlikun | 0:13413ea9a877 | 538 | * @param tx_length The number of words to transmit. |
ganlikun | 0:13413ea9a877 | 539 | */ |
ganlikun | 0:13413ea9a877 | 540 | static void serial_rx_buffer_set(serial_t *obj, void *rx, int rx_length, uint8_t width) |
ganlikun | 0:13413ea9a877 | 541 | { |
ganlikun | 0:13413ea9a877 | 542 | (void)width; |
ganlikun | 0:13413ea9a877 | 543 | |
ganlikun | 0:13413ea9a877 | 544 | // Exit if a reception is already on-going |
ganlikun | 0:13413ea9a877 | 545 | if (serial_rx_active(obj)) { |
ganlikun | 0:13413ea9a877 | 546 | return; |
ganlikun | 0:13413ea9a877 | 547 | } |
ganlikun | 0:13413ea9a877 | 548 | |
ganlikun | 0:13413ea9a877 | 549 | obj->rx_buff.buffer = rx; |
ganlikun | 0:13413ea9a877 | 550 | obj->rx_buff.length = rx_length; |
ganlikun | 0:13413ea9a877 | 551 | obj->rx_buff.pos = 0; |
ganlikun | 0:13413ea9a877 | 552 | } |
ganlikun | 0:13413ea9a877 | 553 | |
ganlikun | 0:13413ea9a877 | 554 | /** |
ganlikun | 0:13413ea9a877 | 555 | * Configure events |
ganlikun | 0:13413ea9a877 | 556 | * |
ganlikun | 0:13413ea9a877 | 557 | * @param obj The serial object |
ganlikun | 0:13413ea9a877 | 558 | * @param event The logical OR of the events to configure |
ganlikun | 0:13413ea9a877 | 559 | * @param enable Set to non-zero to enable events, or zero to disable them |
ganlikun | 0:13413ea9a877 | 560 | */ |
ganlikun | 0:13413ea9a877 | 561 | static void serial_enable_event(serial_t *obj, int event, uint8_t enable) |
ganlikun | 0:13413ea9a877 | 562 | { |
ganlikun | 0:13413ea9a877 | 563 | struct serial_s *obj_s = SERIAL_S(obj); |
ganlikun | 0:13413ea9a877 | 564 | |
ganlikun | 0:13413ea9a877 | 565 | // Shouldn't have to enable interrupt here, just need to keep track of the requested events. |
ganlikun | 0:13413ea9a877 | 566 | if (enable) { |
ganlikun | 0:13413ea9a877 | 567 | obj_s->events |= event; |
ganlikun | 0:13413ea9a877 | 568 | } else { |
ganlikun | 0:13413ea9a877 | 569 | obj_s->events &= ~event; |
ganlikun | 0:13413ea9a877 | 570 | } |
ganlikun | 0:13413ea9a877 | 571 | } |
ganlikun | 0:13413ea9a877 | 572 | |
ganlikun | 0:13413ea9a877 | 573 | |
ganlikun | 0:13413ea9a877 | 574 | /** |
ganlikun | 0:13413ea9a877 | 575 | * Get index of serial object TX IRQ, relating it to the physical peripheral. |
ganlikun | 0:13413ea9a877 | 576 | * |
ganlikun | 0:13413ea9a877 | 577 | * @param obj pointer to serial object |
ganlikun | 0:13413ea9a877 | 578 | * @return internal NVIC TX IRQ index of U(S)ART peripheral |
ganlikun | 0:13413ea9a877 | 579 | */ |
ganlikun | 0:13413ea9a877 | 580 | static IRQn_Type serial_get_irq_n(serial_t *obj) |
ganlikun | 0:13413ea9a877 | 581 | { |
ganlikun | 0:13413ea9a877 | 582 | struct serial_s *obj_s = SERIAL_S(obj); |
ganlikun | 0:13413ea9a877 | 583 | IRQn_Type irq_n; |
ganlikun | 0:13413ea9a877 | 584 | |
ganlikun | 0:13413ea9a877 | 585 | switch (obj_s->index) { |
ganlikun | 0:13413ea9a877 | 586 | #if defined(USART1_BASE) |
ganlikun | 0:13413ea9a877 | 587 | case 0: |
ganlikun | 0:13413ea9a877 | 588 | irq_n = USART1_IRQn; |
ganlikun | 0:13413ea9a877 | 589 | break; |
ganlikun | 0:13413ea9a877 | 590 | #endif |
ganlikun | 0:13413ea9a877 | 591 | #if defined(USART2_BASE) |
ganlikun | 0:13413ea9a877 | 592 | case 1: |
ganlikun | 0:13413ea9a877 | 593 | irq_n = USART2_IRQn; |
ganlikun | 0:13413ea9a877 | 594 | break; |
ganlikun | 0:13413ea9a877 | 595 | #endif |
ganlikun | 0:13413ea9a877 | 596 | #if defined(USART3_BASE) |
ganlikun | 0:13413ea9a877 | 597 | case 2: |
ganlikun | 0:13413ea9a877 | 598 | irq_n = USART3_IRQn; |
ganlikun | 0:13413ea9a877 | 599 | break; |
ganlikun | 0:13413ea9a877 | 600 | #endif |
ganlikun | 0:13413ea9a877 | 601 | #if defined(UART4_BASE) |
ganlikun | 0:13413ea9a877 | 602 | case 3: |
ganlikun | 0:13413ea9a877 | 603 | irq_n = UART4_IRQn; |
ganlikun | 0:13413ea9a877 | 604 | break; |
ganlikun | 0:13413ea9a877 | 605 | #endif |
ganlikun | 0:13413ea9a877 | 606 | #if defined(USART5_BASE) |
ganlikun | 0:13413ea9a877 | 607 | case 4: |
ganlikun | 0:13413ea9a877 | 608 | irq_n = UART5_IRQn; |
ganlikun | 0:13413ea9a877 | 609 | break; |
ganlikun | 0:13413ea9a877 | 610 | #endif |
ganlikun | 0:13413ea9a877 | 611 | #if defined(USART6_BASE) |
ganlikun | 0:13413ea9a877 | 612 | case 5: |
ganlikun | 0:13413ea9a877 | 613 | irq_n = USART6_IRQn; |
ganlikun | 0:13413ea9a877 | 614 | break; |
ganlikun | 0:13413ea9a877 | 615 | #endif |
ganlikun | 0:13413ea9a877 | 616 | #if defined(UART7_BASE) |
ganlikun | 0:13413ea9a877 | 617 | case 6: |
ganlikun | 0:13413ea9a877 | 618 | irq_n = UART7_IRQn; |
ganlikun | 0:13413ea9a877 | 619 | break; |
ganlikun | 0:13413ea9a877 | 620 | #endif |
ganlikun | 0:13413ea9a877 | 621 | #if defined(UART8_BASE) |
ganlikun | 0:13413ea9a877 | 622 | case 7: |
ganlikun | 0:13413ea9a877 | 623 | irq_n = UART8_IRQn; |
ganlikun | 0:13413ea9a877 | 624 | break; |
ganlikun | 0:13413ea9a877 | 625 | #endif |
ganlikun | 0:13413ea9a877 | 626 | #if defined(UART9_BASE) |
ganlikun | 0:13413ea9a877 | 627 | case 8: |
ganlikun | 0:13413ea9a877 | 628 | irq_n = UART9_IRQn; |
ganlikun | 0:13413ea9a877 | 629 | break; |
ganlikun | 0:13413ea9a877 | 630 | #endif |
ganlikun | 0:13413ea9a877 | 631 | #if defined(UART10_BASE) |
ganlikun | 0:13413ea9a877 | 632 | case 9: |
ganlikun | 0:13413ea9a877 | 633 | irq_n = UART10_IRQn; |
ganlikun | 0:13413ea9a877 | 634 | break; |
ganlikun | 0:13413ea9a877 | 635 | #endif |
ganlikun | 0:13413ea9a877 | 636 | default: |
ganlikun | 0:13413ea9a877 | 637 | irq_n = (IRQn_Type)0; |
ganlikun | 0:13413ea9a877 | 638 | } |
ganlikun | 0:13413ea9a877 | 639 | |
ganlikun | 0:13413ea9a877 | 640 | return irq_n; |
ganlikun | 0:13413ea9a877 | 641 | } |
ganlikun | 0:13413ea9a877 | 642 | |
ganlikun | 0:13413ea9a877 | 643 | /****************************************************************************** |
ganlikun | 0:13413ea9a877 | 644 | * MBED API FUNCTIONS |
ganlikun | 0:13413ea9a877 | 645 | ******************************************************************************/ |
ganlikun | 0:13413ea9a877 | 646 | |
ganlikun | 0:13413ea9a877 | 647 | /** |
ganlikun | 0:13413ea9a877 | 648 | * Begin asynchronous TX transfer. The used buffer is specified in the serial |
ganlikun | 0:13413ea9a877 | 649 | * object, tx_buff |
ganlikun | 0:13413ea9a877 | 650 | * |
ganlikun | 0:13413ea9a877 | 651 | * @param obj The serial object |
ganlikun | 0:13413ea9a877 | 652 | * @param tx The buffer for sending |
ganlikun | 0:13413ea9a877 | 653 | * @param tx_length The number of words to transmit |
ganlikun | 0:13413ea9a877 | 654 | * @param tx_width The bit width of buffer word |
ganlikun | 0:13413ea9a877 | 655 | * @param handler The serial handler |
ganlikun | 0:13413ea9a877 | 656 | * @param event The logical OR of events to be registered |
ganlikun | 0:13413ea9a877 | 657 | * @param hint A suggestion for how to use DMA with this transfer |
ganlikun | 0:13413ea9a877 | 658 | * @return Returns number of data transfered, or 0 otherwise |
ganlikun | 0:13413ea9a877 | 659 | */ |
ganlikun | 0:13413ea9a877 | 660 | int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx_width, uint32_t handler, uint32_t event, DMAUsage hint) |
ganlikun | 0:13413ea9a877 | 661 | { |
ganlikun | 0:13413ea9a877 | 662 | // TODO: DMA usage is currently ignored |
ganlikun | 0:13413ea9a877 | 663 | (void) hint; |
ganlikun | 0:13413ea9a877 | 664 | |
ganlikun | 0:13413ea9a877 | 665 | // Check buffer is ok |
ganlikun | 0:13413ea9a877 | 666 | MBED_ASSERT(tx != (void*)0); |
ganlikun | 0:13413ea9a877 | 667 | MBED_ASSERT(tx_width == 8); // support only 8b width |
ganlikun | 0:13413ea9a877 | 668 | |
ganlikun | 0:13413ea9a877 | 669 | struct serial_s *obj_s = SERIAL_S(obj); |
ganlikun | 0:13413ea9a877 | 670 | UART_HandleTypeDef * huart = &uart_handlers[obj_s->index]; |
ganlikun | 0:13413ea9a877 | 671 | |
ganlikun | 0:13413ea9a877 | 672 | if (tx_length == 0) { |
ganlikun | 0:13413ea9a877 | 673 | return 0; |
ganlikun | 0:13413ea9a877 | 674 | } |
ganlikun | 0:13413ea9a877 | 675 | |
ganlikun | 0:13413ea9a877 | 676 | // Set up buffer |
ganlikun | 0:13413ea9a877 | 677 | serial_tx_buffer_set(obj, (void *)tx, tx_length, tx_width); |
ganlikun | 0:13413ea9a877 | 678 | |
ganlikun | 0:13413ea9a877 | 679 | // Set up events |
ganlikun | 0:13413ea9a877 | 680 | serial_enable_event(obj, SERIAL_EVENT_TX_ALL, 0); // Clear all events |
ganlikun | 0:13413ea9a877 | 681 | serial_enable_event(obj, event, 1); // Set only the wanted events |
ganlikun | 0:13413ea9a877 | 682 | |
ganlikun | 0:13413ea9a877 | 683 | // Enable interrupt |
ganlikun | 0:13413ea9a877 | 684 | IRQn_Type irq_n = serial_get_irq_n(obj); |
ganlikun | 0:13413ea9a877 | 685 | NVIC_ClearPendingIRQ(irq_n); |
ganlikun | 0:13413ea9a877 | 686 | NVIC_DisableIRQ(irq_n); |
ganlikun | 0:13413ea9a877 | 687 | NVIC_SetPriority(irq_n, 1); |
ganlikun | 0:13413ea9a877 | 688 | NVIC_SetVector(irq_n, (uint32_t)handler); |
ganlikun | 0:13413ea9a877 | 689 | NVIC_EnableIRQ(irq_n); |
ganlikun | 0:13413ea9a877 | 690 | |
ganlikun | 0:13413ea9a877 | 691 | // the following function will enable UART_IT_TXE and error interrupts |
ganlikun | 0:13413ea9a877 | 692 | if (HAL_UART_Transmit_IT(huart, (uint8_t*)tx, tx_length) != HAL_OK) { |
ganlikun | 0:13413ea9a877 | 693 | return 0; |
ganlikun | 0:13413ea9a877 | 694 | } |
ganlikun | 0:13413ea9a877 | 695 | |
ganlikun | 0:13413ea9a877 | 696 | return tx_length; |
ganlikun | 0:13413ea9a877 | 697 | } |
ganlikun | 0:13413ea9a877 | 698 | |
ganlikun | 0:13413ea9a877 | 699 | /** |
ganlikun | 0:13413ea9a877 | 700 | * Begin asynchronous RX transfer (enable interrupt for data collecting) |
ganlikun | 0:13413ea9a877 | 701 | * The used buffer is specified in the serial object, rx_buff |
ganlikun | 0:13413ea9a877 | 702 | * |
ganlikun | 0:13413ea9a877 | 703 | * @param obj The serial object |
ganlikun | 0:13413ea9a877 | 704 | * @param rx The buffer for sending |
ganlikun | 0:13413ea9a877 | 705 | * @param rx_length The number of words to transmit |
ganlikun | 0:13413ea9a877 | 706 | * @param rx_width The bit width of buffer word |
ganlikun | 0:13413ea9a877 | 707 | * @param handler The serial handler |
ganlikun | 0:13413ea9a877 | 708 | * @param event The logical OR of events to be registered |
ganlikun | 0:13413ea9a877 | 709 | * @param handler The serial handler |
ganlikun | 0:13413ea9a877 | 710 | * @param char_match A character in range 0-254 to be matched |
ganlikun | 0:13413ea9a877 | 711 | * @param hint A suggestion for how to use DMA with this transfer |
ganlikun | 0:13413ea9a877 | 712 | */ |
ganlikun | 0:13413ea9a877 | 713 | void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_width, uint32_t handler, uint32_t event, uint8_t char_match, DMAUsage hint) |
ganlikun | 0:13413ea9a877 | 714 | { |
ganlikun | 0:13413ea9a877 | 715 | // TODO: DMA usage is currently ignored |
ganlikun | 0:13413ea9a877 | 716 | (void) hint; |
ganlikun | 0:13413ea9a877 | 717 | |
ganlikun | 0:13413ea9a877 | 718 | /* Sanity check arguments */ |
ganlikun | 0:13413ea9a877 | 719 | MBED_ASSERT(obj); |
ganlikun | 0:13413ea9a877 | 720 | MBED_ASSERT(rx != (void*)0); |
ganlikun | 0:13413ea9a877 | 721 | MBED_ASSERT(rx_width == 8); // support only 8b width |
ganlikun | 0:13413ea9a877 | 722 | |
ganlikun | 0:13413ea9a877 | 723 | struct serial_s *obj_s = SERIAL_S(obj); |
ganlikun | 0:13413ea9a877 | 724 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
ganlikun | 0:13413ea9a877 | 725 | |
ganlikun | 0:13413ea9a877 | 726 | serial_enable_event(obj, SERIAL_EVENT_RX_ALL, 0); |
ganlikun | 0:13413ea9a877 | 727 | serial_enable_event(obj, event, 1); |
ganlikun | 0:13413ea9a877 | 728 | |
ganlikun | 0:13413ea9a877 | 729 | // set CharMatch |
ganlikun | 0:13413ea9a877 | 730 | obj->char_match = char_match; |
ganlikun | 0:13413ea9a877 | 731 | |
ganlikun | 0:13413ea9a877 | 732 | serial_rx_buffer_set(obj, rx, rx_length, rx_width); |
ganlikun | 0:13413ea9a877 | 733 | |
ganlikun | 0:13413ea9a877 | 734 | IRQn_Type irq_n = serial_get_irq_n(obj); |
ganlikun | 0:13413ea9a877 | 735 | NVIC_ClearPendingIRQ(irq_n); |
ganlikun | 0:13413ea9a877 | 736 | NVIC_DisableIRQ(irq_n); |
ganlikun | 0:13413ea9a877 | 737 | NVIC_SetPriority(irq_n, 0); |
ganlikun | 0:13413ea9a877 | 738 | NVIC_SetVector(irq_n, (uint32_t)handler); |
ganlikun | 0:13413ea9a877 | 739 | NVIC_EnableIRQ(irq_n); |
ganlikun | 0:13413ea9a877 | 740 | |
ganlikun | 0:13413ea9a877 | 741 | // following HAL function will enable the RXNE interrupt + error interrupts |
ganlikun | 0:13413ea9a877 | 742 | HAL_UART_Receive_IT(huart, (uint8_t*)rx, rx_length); |
ganlikun | 0:13413ea9a877 | 743 | } |
ganlikun | 0:13413ea9a877 | 744 | |
ganlikun | 0:13413ea9a877 | 745 | /** |
ganlikun | 0:13413ea9a877 | 746 | * Attempts to determine if the serial peripheral is already in use for TX |
ganlikun | 0:13413ea9a877 | 747 | * |
ganlikun | 0:13413ea9a877 | 748 | * @param obj The serial object |
ganlikun | 0:13413ea9a877 | 749 | * @return Non-zero if the TX transaction is ongoing, 0 otherwise |
ganlikun | 0:13413ea9a877 | 750 | */ |
ganlikun | 0:13413ea9a877 | 751 | uint8_t serial_tx_active(serial_t *obj) |
ganlikun | 0:13413ea9a877 | 752 | { |
ganlikun | 0:13413ea9a877 | 753 | MBED_ASSERT(obj); |
ganlikun | 0:13413ea9a877 | 754 | |
ganlikun | 0:13413ea9a877 | 755 | struct serial_s *obj_s = SERIAL_S(obj); |
ganlikun | 0:13413ea9a877 | 756 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
ganlikun | 0:13413ea9a877 | 757 | |
ganlikun | 0:13413ea9a877 | 758 | return ((HAL_UART_GetState(huart) == HAL_UART_STATE_BUSY_TX) ? 1 : 0); |
ganlikun | 0:13413ea9a877 | 759 | } |
ganlikun | 0:13413ea9a877 | 760 | |
ganlikun | 0:13413ea9a877 | 761 | /** |
ganlikun | 0:13413ea9a877 | 762 | * Attempts to determine if the serial peripheral is already in use for RX |
ganlikun | 0:13413ea9a877 | 763 | * |
ganlikun | 0:13413ea9a877 | 764 | * @param obj The serial object |
ganlikun | 0:13413ea9a877 | 765 | * @return Non-zero if the RX transaction is ongoing, 0 otherwise |
ganlikun | 0:13413ea9a877 | 766 | */ |
ganlikun | 0:13413ea9a877 | 767 | uint8_t serial_rx_active(serial_t *obj) |
ganlikun | 0:13413ea9a877 | 768 | { |
ganlikun | 0:13413ea9a877 | 769 | MBED_ASSERT(obj); |
ganlikun | 0:13413ea9a877 | 770 | |
ganlikun | 0:13413ea9a877 | 771 | struct serial_s *obj_s = SERIAL_S(obj); |
ganlikun | 0:13413ea9a877 | 772 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
ganlikun | 0:13413ea9a877 | 773 | |
ganlikun | 0:13413ea9a877 | 774 | return ((HAL_UART_GetState(huart) == HAL_UART_STATE_BUSY_RX) ? 1 : 0); |
ganlikun | 0:13413ea9a877 | 775 | } |
ganlikun | 0:13413ea9a877 | 776 | |
ganlikun | 0:13413ea9a877 | 777 | void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) { |
ganlikun | 0:13413ea9a877 | 778 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) { |
ganlikun | 0:13413ea9a877 | 779 | __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC); |
ganlikun | 0:13413ea9a877 | 780 | } |
ganlikun | 0:13413ea9a877 | 781 | } |
ganlikun | 0:13413ea9a877 | 782 | |
ganlikun | 0:13413ea9a877 | 783 | void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) { |
ganlikun | 0:13413ea9a877 | 784 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) { |
ganlikun | 0:13413ea9a877 | 785 | volatile uint32_t tmpval = huart->Instance->DR; // Clear PE flag |
ganlikun | 0:13413ea9a877 | 786 | } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) { |
ganlikun | 0:13413ea9a877 | 787 | volatile uint32_t tmpval = huart->Instance->DR; // Clear FE flag |
ganlikun | 0:13413ea9a877 | 788 | } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_NE) != RESET) { |
ganlikun | 0:13413ea9a877 | 789 | volatile uint32_t tmpval = huart->Instance->DR; // Clear NE flag |
ganlikun | 0:13413ea9a877 | 790 | } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { |
ganlikun | 0:13413ea9a877 | 791 | volatile uint32_t tmpval = huart->Instance->DR; // Clear ORE flag |
ganlikun | 0:13413ea9a877 | 792 | } |
ganlikun | 0:13413ea9a877 | 793 | } |
ganlikun | 0:13413ea9a877 | 794 | |
ganlikun | 0:13413ea9a877 | 795 | /** |
ganlikun | 0:13413ea9a877 | 796 | * The asynchronous TX and RX handler. |
ganlikun | 0:13413ea9a877 | 797 | * |
ganlikun | 0:13413ea9a877 | 798 | * @param obj The serial object |
ganlikun | 0:13413ea9a877 | 799 | * @return Returns event flags if a TX/RX transfer termination condition was met or 0 otherwise |
ganlikun | 0:13413ea9a877 | 800 | */ |
ganlikun | 0:13413ea9a877 | 801 | int serial_irq_handler_asynch(serial_t *obj) |
ganlikun | 0:13413ea9a877 | 802 | { |
ganlikun | 0:13413ea9a877 | 803 | struct serial_s *obj_s = SERIAL_S(obj); |
ganlikun | 0:13413ea9a877 | 804 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
ganlikun | 0:13413ea9a877 | 805 | |
ganlikun | 0:13413ea9a877 | 806 | volatile int return_event = 0; |
ganlikun | 0:13413ea9a877 | 807 | uint8_t *buf = (uint8_t*)(obj->rx_buff.buffer); |
ganlikun | 0:13413ea9a877 | 808 | uint8_t i = 0; |
ganlikun | 0:13413ea9a877 | 809 | |
ganlikun | 0:13413ea9a877 | 810 | // TX PART: |
ganlikun | 0:13413ea9a877 | 811 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) { |
ganlikun | 0:13413ea9a877 | 812 | if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC) != RESET) { |
ganlikun | 0:13413ea9a877 | 813 | // Return event SERIAL_EVENT_TX_COMPLETE if requested |
ganlikun | 0:13413ea9a877 | 814 | if ((obj_s->events & SERIAL_EVENT_TX_COMPLETE ) != 0) { |
ganlikun | 0:13413ea9a877 | 815 | return_event |= (SERIAL_EVENT_TX_COMPLETE & obj_s->events); |
ganlikun | 0:13413ea9a877 | 816 | } |
ganlikun | 0:13413ea9a877 | 817 | } |
ganlikun | 0:13413ea9a877 | 818 | } |
ganlikun | 0:13413ea9a877 | 819 | |
ganlikun | 0:13413ea9a877 | 820 | // Handle error events |
ganlikun | 0:13413ea9a877 | 821 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) { |
ganlikun | 0:13413ea9a877 | 822 | if (__HAL_UART_GET_IT_SOURCE(huart, USART_IT_ERR) != RESET) { |
ganlikun | 0:13413ea9a877 | 823 | return_event |= (SERIAL_EVENT_RX_PARITY_ERROR & obj_s->events); |
ganlikun | 0:13413ea9a877 | 824 | } |
ganlikun | 0:13413ea9a877 | 825 | } |
ganlikun | 0:13413ea9a877 | 826 | |
ganlikun | 0:13413ea9a877 | 827 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) { |
ganlikun | 0:13413ea9a877 | 828 | if (__HAL_UART_GET_IT_SOURCE(huart, USART_IT_ERR) != RESET) { |
ganlikun | 0:13413ea9a877 | 829 | return_event |= (SERIAL_EVENT_RX_FRAMING_ERROR & obj_s->events); |
ganlikun | 0:13413ea9a877 | 830 | } |
ganlikun | 0:13413ea9a877 | 831 | } |
ganlikun | 0:13413ea9a877 | 832 | |
ganlikun | 0:13413ea9a877 | 833 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { |
ganlikun | 0:13413ea9a877 | 834 | if (__HAL_UART_GET_IT_SOURCE(huart, USART_IT_ERR) != RESET) { |
ganlikun | 0:13413ea9a877 | 835 | return_event |= (SERIAL_EVENT_RX_OVERRUN_ERROR & obj_s->events); |
ganlikun | 0:13413ea9a877 | 836 | } |
ganlikun | 0:13413ea9a877 | 837 | } |
ganlikun | 0:13413ea9a877 | 838 | |
ganlikun | 0:13413ea9a877 | 839 | HAL_UART_IRQHandler(huart); |
ganlikun | 0:13413ea9a877 | 840 | |
ganlikun | 0:13413ea9a877 | 841 | // Abort if an error occurs |
ganlikun | 0:13413ea9a877 | 842 | if (return_event & SERIAL_EVENT_RX_PARITY_ERROR || |
ganlikun | 0:13413ea9a877 | 843 | return_event & SERIAL_EVENT_RX_FRAMING_ERROR || |
ganlikun | 0:13413ea9a877 | 844 | return_event & SERIAL_EVENT_RX_OVERRUN_ERROR) { |
ganlikun | 0:13413ea9a877 | 845 | return return_event; |
ganlikun | 0:13413ea9a877 | 846 | } |
ganlikun | 0:13413ea9a877 | 847 | |
ganlikun | 0:13413ea9a877 | 848 | //RX PART |
ganlikun | 0:13413ea9a877 | 849 | if (huart->RxXferSize != 0) { |
ganlikun | 0:13413ea9a877 | 850 | obj->rx_buff.pos = huart->RxXferSize - huart->RxXferCount; |
ganlikun | 0:13413ea9a877 | 851 | } |
ganlikun | 0:13413ea9a877 | 852 | if ((huart->RxXferCount == 0) && (obj->rx_buff.pos >= (obj->rx_buff.length - 1))) { |
ganlikun | 0:13413ea9a877 | 853 | return_event |= (SERIAL_EVENT_RX_COMPLETE & obj_s->events); |
ganlikun | 0:13413ea9a877 | 854 | } |
ganlikun | 0:13413ea9a877 | 855 | |
ganlikun | 0:13413ea9a877 | 856 | // Check if char_match is present |
ganlikun | 0:13413ea9a877 | 857 | if (obj_s->events & SERIAL_EVENT_RX_CHARACTER_MATCH) { |
ganlikun | 0:13413ea9a877 | 858 | if (buf != NULL) { |
ganlikun | 0:13413ea9a877 | 859 | for (i = 0; i < obj->rx_buff.pos; i++) { |
ganlikun | 0:13413ea9a877 | 860 | if (buf[i] == obj->char_match) { |
ganlikun | 0:13413ea9a877 | 861 | obj->rx_buff.pos = i; |
ganlikun | 0:13413ea9a877 | 862 | return_event |= (SERIAL_EVENT_RX_CHARACTER_MATCH & obj_s->events); |
ganlikun | 0:13413ea9a877 | 863 | serial_rx_abort_asynch(obj); |
ganlikun | 0:13413ea9a877 | 864 | break; |
ganlikun | 0:13413ea9a877 | 865 | } |
ganlikun | 0:13413ea9a877 | 866 | } |
ganlikun | 0:13413ea9a877 | 867 | } |
ganlikun | 0:13413ea9a877 | 868 | } |
ganlikun | 0:13413ea9a877 | 869 | |
ganlikun | 0:13413ea9a877 | 870 | return return_event; |
ganlikun | 0:13413ea9a877 | 871 | } |
ganlikun | 0:13413ea9a877 | 872 | |
ganlikun | 0:13413ea9a877 | 873 | /** |
ganlikun | 0:13413ea9a877 | 874 | * Abort the ongoing TX transaction. It disables the enabled interupt for TX and |
ganlikun | 0:13413ea9a877 | 875 | * flush TX hardware buffer if TX FIFO is used |
ganlikun | 0:13413ea9a877 | 876 | * |
ganlikun | 0:13413ea9a877 | 877 | * @param obj The serial object |
ganlikun | 0:13413ea9a877 | 878 | */ |
ganlikun | 0:13413ea9a877 | 879 | void serial_tx_abort_asynch(serial_t *obj) |
ganlikun | 0:13413ea9a877 | 880 | { |
ganlikun | 0:13413ea9a877 | 881 | struct serial_s *obj_s = SERIAL_S(obj); |
ganlikun | 0:13413ea9a877 | 882 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
ganlikun | 0:13413ea9a877 | 883 | |
ganlikun | 0:13413ea9a877 | 884 | __HAL_UART_DISABLE_IT(huart, UART_IT_TC); |
ganlikun | 0:13413ea9a877 | 885 | __HAL_UART_DISABLE_IT(huart, UART_IT_TXE); |
ganlikun | 0:13413ea9a877 | 886 | |
ganlikun | 0:13413ea9a877 | 887 | // clear flags |
ganlikun | 0:13413ea9a877 | 888 | __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC); |
ganlikun | 0:13413ea9a877 | 889 | |
ganlikun | 0:13413ea9a877 | 890 | // reset states |
ganlikun | 0:13413ea9a877 | 891 | huart->TxXferCount = 0; |
ganlikun | 0:13413ea9a877 | 892 | // update handle state |
ganlikun | 0:13413ea9a877 | 893 | if(huart->gState == HAL_UART_STATE_BUSY_TX_RX) { |
ganlikun | 0:13413ea9a877 | 894 | huart->gState = HAL_UART_STATE_BUSY_RX; |
ganlikun | 0:13413ea9a877 | 895 | } else { |
ganlikun | 0:13413ea9a877 | 896 | huart->gState = HAL_UART_STATE_READY; |
ganlikun | 0:13413ea9a877 | 897 | } |
ganlikun | 0:13413ea9a877 | 898 | } |
ganlikun | 0:13413ea9a877 | 899 | |
ganlikun | 0:13413ea9a877 | 900 | /** |
ganlikun | 0:13413ea9a877 | 901 | * Abort the ongoing RX transaction It disables the enabled interrupt for RX and |
ganlikun | 0:13413ea9a877 | 902 | * flush RX hardware buffer if RX FIFO is used |
ganlikun | 0:13413ea9a877 | 903 | * |
ganlikun | 0:13413ea9a877 | 904 | * @param obj The serial object |
ganlikun | 0:13413ea9a877 | 905 | */ |
ganlikun | 0:13413ea9a877 | 906 | void serial_rx_abort_asynch(serial_t *obj) |
ganlikun | 0:13413ea9a877 | 907 | { |
ganlikun | 0:13413ea9a877 | 908 | struct serial_s *obj_s = SERIAL_S(obj); |
ganlikun | 0:13413ea9a877 | 909 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
ganlikun | 0:13413ea9a877 | 910 | |
ganlikun | 0:13413ea9a877 | 911 | // disable interrupts |
ganlikun | 0:13413ea9a877 | 912 | __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE); |
ganlikun | 0:13413ea9a877 | 913 | __HAL_UART_DISABLE_IT(huart, UART_IT_PE); |
ganlikun | 0:13413ea9a877 | 914 | __HAL_UART_DISABLE_IT(huart, UART_IT_ERR); |
ganlikun | 0:13413ea9a877 | 915 | |
ganlikun | 0:13413ea9a877 | 916 | // clear flags |
ganlikun | 0:13413ea9a877 | 917 | __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_RXNE); |
ganlikun | 0:13413ea9a877 | 918 | volatile uint32_t tmpval = huart->Instance->DR; // Clear errors flag |
ganlikun | 0:13413ea9a877 | 919 | |
ganlikun | 0:13413ea9a877 | 920 | // reset states |
ganlikun | 0:13413ea9a877 | 921 | huart->RxXferCount = 0; |
ganlikun | 0:13413ea9a877 | 922 | // update handle state |
ganlikun | 0:13413ea9a877 | 923 | if(huart->RxState == HAL_UART_STATE_BUSY_TX_RX) { |
ganlikun | 0:13413ea9a877 | 924 | huart->RxState = HAL_UART_STATE_BUSY_TX; |
ganlikun | 0:13413ea9a877 | 925 | } else { |
ganlikun | 0:13413ea9a877 | 926 | huart->RxState = HAL_UART_STATE_READY; |
ganlikun | 0:13413ea9a877 | 927 | } |
ganlikun | 0:13413ea9a877 | 928 | } |
ganlikun | 0:13413ea9a877 | 929 | |
ganlikun | 0:13413ea9a877 | 930 | #endif |
ganlikun | 0:13413ea9a877 | 931 | |
ganlikun | 0:13413ea9a877 | 932 | #if DEVICE_SERIAL_FC |
ganlikun | 0:13413ea9a877 | 933 | |
ganlikun | 0:13413ea9a877 | 934 | /** |
ganlikun | 0:13413ea9a877 | 935 | * Set HW Control Flow |
ganlikun | 0:13413ea9a877 | 936 | * @param obj The serial object |
ganlikun | 0:13413ea9a877 | 937 | * @param type The Control Flow type (FlowControlNone, FlowControlRTS, FlowControlCTS, FlowControlRTSCTS) |
ganlikun | 0:13413ea9a877 | 938 | * @param rxflow Pin for the rxflow |
ganlikun | 0:13413ea9a877 | 939 | * @param txflow Pin for the txflow |
ganlikun | 0:13413ea9a877 | 940 | */ |
ganlikun | 0:13413ea9a877 | 941 | void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow) |
ganlikun | 0:13413ea9a877 | 942 | { |
ganlikun | 0:13413ea9a877 | 943 | struct serial_s *obj_s = SERIAL_S(obj); |
ganlikun | 0:13413ea9a877 | 944 | |
ganlikun | 0:13413ea9a877 | 945 | // Determine the UART to use (UART_1, UART_2, ...) |
ganlikun | 0:13413ea9a877 | 946 | UARTName uart_rts = (UARTName)pinmap_peripheral(rxflow, PinMap_UART_RTS); |
ganlikun | 0:13413ea9a877 | 947 | UARTName uart_cts = (UARTName)pinmap_peripheral(txflow, PinMap_UART_CTS); |
ganlikun | 0:13413ea9a877 | 948 | |
ganlikun | 0:13413ea9a877 | 949 | // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object |
ganlikun | 0:13413ea9a877 | 950 | obj_s->uart = (UARTName)pinmap_merge(uart_cts, uart_rts); |
ganlikun | 0:13413ea9a877 | 951 | MBED_ASSERT(obj_s->uart != (UARTName)NC); |
ganlikun | 0:13413ea9a877 | 952 | |
ganlikun | 0:13413ea9a877 | 953 | if(type == FlowControlNone) { |
ganlikun | 0:13413ea9a877 | 954 | // Disable hardware flow control |
ganlikun | 0:13413ea9a877 | 955 | obj_s->hw_flow_ctl = UART_HWCONTROL_NONE; |
ganlikun | 0:13413ea9a877 | 956 | } |
ganlikun | 0:13413ea9a877 | 957 | if (type == FlowControlRTS) { |
ganlikun | 0:13413ea9a877 | 958 | // Enable RTS |
ganlikun | 0:13413ea9a877 | 959 | MBED_ASSERT(uart_rts != (UARTName)NC); |
ganlikun | 0:13413ea9a877 | 960 | obj_s->hw_flow_ctl = UART_HWCONTROL_RTS; |
ganlikun | 0:13413ea9a877 | 961 | obj_s->pin_rts = rxflow; |
ganlikun | 0:13413ea9a877 | 962 | // Enable the pin for RTS function |
ganlikun | 0:13413ea9a877 | 963 | pinmap_pinout(rxflow, PinMap_UART_RTS); |
ganlikun | 0:13413ea9a877 | 964 | } |
ganlikun | 0:13413ea9a877 | 965 | if (type == FlowControlCTS) { |
ganlikun | 0:13413ea9a877 | 966 | // Enable CTS |
ganlikun | 0:13413ea9a877 | 967 | MBED_ASSERT(uart_cts != (UARTName)NC); |
ganlikun | 0:13413ea9a877 | 968 | obj_s->hw_flow_ctl = UART_HWCONTROL_CTS; |
ganlikun | 0:13413ea9a877 | 969 | obj_s->pin_cts = txflow; |
ganlikun | 0:13413ea9a877 | 970 | // Enable the pin for CTS function |
ganlikun | 0:13413ea9a877 | 971 | pinmap_pinout(txflow, PinMap_UART_CTS); |
ganlikun | 0:13413ea9a877 | 972 | } |
ganlikun | 0:13413ea9a877 | 973 | if (type == FlowControlRTSCTS) { |
ganlikun | 0:13413ea9a877 | 974 | // Enable CTS & RTS |
ganlikun | 0:13413ea9a877 | 975 | MBED_ASSERT(uart_rts != (UARTName)NC); |
ganlikun | 0:13413ea9a877 | 976 | MBED_ASSERT(uart_cts != (UARTName)NC); |
ganlikun | 0:13413ea9a877 | 977 | obj_s->hw_flow_ctl = UART_HWCONTROL_RTS_CTS; |
ganlikun | 0:13413ea9a877 | 978 | obj_s->pin_rts = rxflow; |
ganlikun | 0:13413ea9a877 | 979 | obj_s->pin_cts = txflow; |
ganlikun | 0:13413ea9a877 | 980 | // Enable the pin for CTS function |
ganlikun | 0:13413ea9a877 | 981 | pinmap_pinout(txflow, PinMap_UART_CTS); |
ganlikun | 0:13413ea9a877 | 982 | // Enable the pin for RTS function |
ganlikun | 0:13413ea9a877 | 983 | pinmap_pinout(rxflow, PinMap_UART_RTS); |
ganlikun | 0:13413ea9a877 | 984 | } |
ganlikun | 0:13413ea9a877 | 985 | |
ganlikun | 0:13413ea9a877 | 986 | init_uart(obj); |
ganlikun | 0:13413ea9a877 | 987 | } |
ganlikun | 0:13413ea9a877 | 988 | |
ganlikun | 0:13413ea9a877 | 989 | #endif |
ganlikun | 0:13413ea9a877 | 990 | |
ganlikun | 0:13413ea9a877 | 991 | #endif |
ganlikun | 0:13413ea9a877 | 992 |