Jolyon Hill / mbed-dev

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Fri Sep 30 18:07:01 2016 +0100
Revision:
148:21d94c44109e
Parent:
144:ef7eb2e8f9f7
This updates the lib to the mbed lib v127

Who changed what in which revision?

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