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