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