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