Forked.

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Wed Jun 21 17:46:44 2017 +0100
Revision:
167:e84263d55307
Child:
170:19eb464bc2be
This updates the lib to the mbed lib v 145

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 167:e84263d55307 1 /* mbed Microcontroller Library
AnnaBridge 167:e84263d55307 2 *******************************************************************************
AnnaBridge 167:e84263d55307 3 * Copyright (c) 2014, STMicroelectronics
AnnaBridge 167:e84263d55307 4 * All rights reserved.
AnnaBridge 167:e84263d55307 5 *
AnnaBridge 167:e84263d55307 6 * Redistribution and use in source and binary forms, with or without
AnnaBridge 167:e84263d55307 7 * modification, are permitted provided that the following conditions are met:
AnnaBridge 167:e84263d55307 8 *
AnnaBridge 167:e84263d55307 9 * 1. Redistributions of source code must retain the above copyright notice,
AnnaBridge 167:e84263d55307 10 * this list of conditions and the following disclaimer.
AnnaBridge 167:e84263d55307 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
AnnaBridge 167:e84263d55307 12 * this list of conditions and the following disclaimer in the documentation
AnnaBridge 167:e84263d55307 13 * and/or other materials provided with the distribution.
AnnaBridge 167:e84263d55307 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
AnnaBridge 167:e84263d55307 15 * may be used to endorse or promote products derived from this software
AnnaBridge 167:e84263d55307 16 * without specific prior written permission.
AnnaBridge 167:e84263d55307 17 *
AnnaBridge 167:e84263d55307 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AnnaBridge 167:e84263d55307 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
AnnaBridge 167:e84263d55307 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
AnnaBridge 167:e84263d55307 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
AnnaBridge 167:e84263d55307 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
AnnaBridge 167:e84263d55307 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
AnnaBridge 167:e84263d55307 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
AnnaBridge 167:e84263d55307 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
AnnaBridge 167:e84263d55307 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
AnnaBridge 167:e84263d55307 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
AnnaBridge 167:e84263d55307 28 *******************************************************************************
AnnaBridge 167:e84263d55307 29 */
AnnaBridge 167:e84263d55307 30 #include "mbed_assert.h"
AnnaBridge 167:e84263d55307 31 #include "serial_api.h"
AnnaBridge 167:e84263d55307 32 #include "serial_api_hal.h"
AnnaBridge 167:e84263d55307 33
AnnaBridge 167:e84263d55307 34 #if DEVICE_SERIAL
AnnaBridge 167:e84263d55307 35
AnnaBridge 167:e84263d55307 36 #include "cmsis.h"
AnnaBridge 167:e84263d55307 37 #include "pinmap.h"
AnnaBridge 167:e84263d55307 38 #include "mbed_error.h"
AnnaBridge 167:e84263d55307 39 #include <string.h>
AnnaBridge 167:e84263d55307 40 #include "PeripheralPins.h"
AnnaBridge 167:e84263d55307 41
AnnaBridge 167:e84263d55307 42 #define UART_NUM (3)
AnnaBridge 167:e84263d55307 43
AnnaBridge 167:e84263d55307 44 static uint32_t serial_irq_ids[UART_NUM] = {0};
AnnaBridge 167:e84263d55307 45 UART_HandleTypeDef uart_handlers[UART_NUM];
AnnaBridge 167:e84263d55307 46
AnnaBridge 167:e84263d55307 47 static uart_irq_handler irq_handler;
AnnaBridge 167:e84263d55307 48
AnnaBridge 167:e84263d55307 49 int stdio_uart_inited = 0;
AnnaBridge 167:e84263d55307 50 serial_t stdio_uart;
AnnaBridge 167:e84263d55307 51
AnnaBridge 167:e84263d55307 52 void serial_init(serial_t *obj, PinName tx, PinName rx)
AnnaBridge 167:e84263d55307 53 {
AnnaBridge 167:e84263d55307 54 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 55
AnnaBridge 167:e84263d55307 56 // Determine the UART to use (UART_1, UART_2, ...)
AnnaBridge 167:e84263d55307 57 UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
AnnaBridge 167:e84263d55307 58 UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
AnnaBridge 167:e84263d55307 59
AnnaBridge 167:e84263d55307 60 // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
AnnaBridge 167:e84263d55307 61 obj_s->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
AnnaBridge 167:e84263d55307 62 MBED_ASSERT(obj_s->uart != (UARTName)NC);
AnnaBridge 167:e84263d55307 63
AnnaBridge 167:e84263d55307 64 // Enable USART clock
AnnaBridge 167:e84263d55307 65 if (obj_s->uart == UART_1) {
AnnaBridge 167:e84263d55307 66 __HAL_RCC_USART1_FORCE_RESET();
AnnaBridge 167:e84263d55307 67 __HAL_RCC_USART1_RELEASE_RESET();
AnnaBridge 167:e84263d55307 68 __HAL_RCC_USART1_CLK_ENABLE();
AnnaBridge 167:e84263d55307 69 obj_s->index = 0;
AnnaBridge 167:e84263d55307 70 }
AnnaBridge 167:e84263d55307 71 if (obj_s->uart == UART_2) {
AnnaBridge 167:e84263d55307 72 __HAL_RCC_USART2_FORCE_RESET();
AnnaBridge 167:e84263d55307 73 __HAL_RCC_USART2_RELEASE_RESET();
AnnaBridge 167:e84263d55307 74 __HAL_RCC_USART2_CLK_ENABLE();
AnnaBridge 167:e84263d55307 75 obj_s->index = 1;
AnnaBridge 167:e84263d55307 76 }
AnnaBridge 167:e84263d55307 77 if (obj_s->uart == UART_3) {
AnnaBridge 167:e84263d55307 78 __HAL_RCC_USART3_FORCE_RESET();
AnnaBridge 167:e84263d55307 79 __HAL_RCC_USART3_RELEASE_RESET();
AnnaBridge 167:e84263d55307 80 __HAL_RCC_USART3_CLK_ENABLE();
AnnaBridge 167:e84263d55307 81 obj_s->index = 2;
AnnaBridge 167:e84263d55307 82 }
AnnaBridge 167:e84263d55307 83
AnnaBridge 167:e84263d55307 84 // Configure UART pins
AnnaBridge 167:e84263d55307 85 pinmap_pinout(tx, PinMap_UART_TX);
AnnaBridge 167:e84263d55307 86 pinmap_pinout(rx, PinMap_UART_RX);
AnnaBridge 167:e84263d55307 87
AnnaBridge 167:e84263d55307 88 if (tx != NC) {
AnnaBridge 167:e84263d55307 89 pin_mode(tx, PullUp);
AnnaBridge 167:e84263d55307 90 }
AnnaBridge 167:e84263d55307 91 if (rx != NC) {
AnnaBridge 167:e84263d55307 92 pin_mode(rx, PullUp);
AnnaBridge 167:e84263d55307 93 }
AnnaBridge 167:e84263d55307 94
AnnaBridge 167:e84263d55307 95 // Configure UART
AnnaBridge 167:e84263d55307 96 obj_s->baudrate = 9600;
AnnaBridge 167:e84263d55307 97 obj_s->databits = UART_WORDLENGTH_8B;
AnnaBridge 167:e84263d55307 98 obj_s->stopbits = UART_STOPBITS_1;
AnnaBridge 167:e84263d55307 99 obj_s->parity = UART_PARITY_NONE;
AnnaBridge 167:e84263d55307 100
AnnaBridge 167:e84263d55307 101 #if DEVICE_SERIAL_FC
AnnaBridge 167:e84263d55307 102 obj_s->hw_flow_ctl = UART_HWCONTROL_NONE;
AnnaBridge 167:e84263d55307 103 #endif
AnnaBridge 167:e84263d55307 104
AnnaBridge 167:e84263d55307 105 obj_s->pin_tx = tx;
AnnaBridge 167:e84263d55307 106 obj_s->pin_rx = rx;
AnnaBridge 167:e84263d55307 107
AnnaBridge 167:e84263d55307 108 init_uart(obj);
AnnaBridge 167:e84263d55307 109
AnnaBridge 167:e84263d55307 110 // For stdio management
AnnaBridge 167:e84263d55307 111 if (obj_s->uart == STDIO_UART) {
AnnaBridge 167:e84263d55307 112 stdio_uart_inited = 1;
AnnaBridge 167:e84263d55307 113 memcpy(&stdio_uart, obj, sizeof(serial_t));
AnnaBridge 167:e84263d55307 114 }
AnnaBridge 167:e84263d55307 115 }
AnnaBridge 167:e84263d55307 116
AnnaBridge 167:e84263d55307 117 void serial_free(serial_t *obj)
AnnaBridge 167:e84263d55307 118 {
AnnaBridge 167:e84263d55307 119 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 120
AnnaBridge 167:e84263d55307 121 // Reset UART and disable clock
AnnaBridge 167:e84263d55307 122 if (obj_s->uart == UART_1) {
AnnaBridge 167:e84263d55307 123 __HAL_RCC_USART1_FORCE_RESET();
AnnaBridge 167:e84263d55307 124 __HAL_RCC_USART1_RELEASE_RESET();
AnnaBridge 167:e84263d55307 125 __HAL_RCC_USART1_CLK_DISABLE();
AnnaBridge 167:e84263d55307 126 }
AnnaBridge 167:e84263d55307 127 if (obj_s->uart == UART_2) {
AnnaBridge 167:e84263d55307 128 __HAL_RCC_USART2_FORCE_RESET();
AnnaBridge 167:e84263d55307 129 __HAL_RCC_USART2_RELEASE_RESET();
AnnaBridge 167:e84263d55307 130 __HAL_RCC_USART2_CLK_DISABLE();
AnnaBridge 167:e84263d55307 131 }
AnnaBridge 167:e84263d55307 132 if (obj_s->uart == UART_3) {
AnnaBridge 167:e84263d55307 133 __HAL_RCC_USART3_FORCE_RESET();
AnnaBridge 167:e84263d55307 134 __HAL_RCC_USART3_RELEASE_RESET();
AnnaBridge 167:e84263d55307 135 __HAL_RCC_USART3_CLK_DISABLE();
AnnaBridge 167:e84263d55307 136 }
AnnaBridge 167:e84263d55307 137
AnnaBridge 167:e84263d55307 138 // Configure GPIOs
AnnaBridge 167:e84263d55307 139 pin_function(obj_s->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
AnnaBridge 167:e84263d55307 140 pin_function(obj_s->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
AnnaBridge 167:e84263d55307 141
AnnaBridge 167:e84263d55307 142 serial_irq_ids[obj_s->index] = 0;
AnnaBridge 167:e84263d55307 143 }
AnnaBridge 167:e84263d55307 144
AnnaBridge 167:e84263d55307 145 void serial_baud(serial_t *obj, int baudrate)
AnnaBridge 167:e84263d55307 146 {
AnnaBridge 167:e84263d55307 147 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 148
AnnaBridge 167:e84263d55307 149 obj_s->baudrate = baudrate;
AnnaBridge 167:e84263d55307 150 init_uart(obj);
AnnaBridge 167:e84263d55307 151 }
AnnaBridge 167:e84263d55307 152
AnnaBridge 167:e84263d55307 153 /******************************************************************************
AnnaBridge 167:e84263d55307 154 * INTERRUPTS HANDLING
AnnaBridge 167:e84263d55307 155 ******************************************************************************/
AnnaBridge 167:e84263d55307 156
AnnaBridge 167:e84263d55307 157 static void uart_irq(int id)
AnnaBridge 167:e84263d55307 158 {
AnnaBridge 167:e84263d55307 159 UART_HandleTypeDef * huart = &uart_handlers[id];
AnnaBridge 167:e84263d55307 160
AnnaBridge 167:e84263d55307 161 if (serial_irq_ids[id] != 0) {
AnnaBridge 167:e84263d55307 162 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) {
AnnaBridge 167:e84263d55307 163 if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC) != RESET) {
AnnaBridge 167:e84263d55307 164 irq_handler(serial_irq_ids[id], TxIrq);
AnnaBridge 167:e84263d55307 165 __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC);
AnnaBridge 167:e84263d55307 166 }
AnnaBridge 167:e84263d55307 167 }
AnnaBridge 167:e84263d55307 168 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) {
AnnaBridge 167:e84263d55307 169 if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) != RESET) {
AnnaBridge 167:e84263d55307 170 irq_handler(serial_irq_ids[id], RxIrq);
AnnaBridge 167:e84263d55307 171 __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_RXNE);
AnnaBridge 167:e84263d55307 172 }
AnnaBridge 167:e84263d55307 173 }
AnnaBridge 167:e84263d55307 174 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) {
AnnaBridge 167:e84263d55307 175 if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR) != RESET) {
AnnaBridge 167:e84263d55307 176 volatile uint32_t tmpval = huart->Instance->DR; // Clear ORE flag
AnnaBridge 167:e84263d55307 177 }
AnnaBridge 167:e84263d55307 178 }
AnnaBridge 167:e84263d55307 179 }
AnnaBridge 167:e84263d55307 180 }
AnnaBridge 167:e84263d55307 181
AnnaBridge 167:e84263d55307 182 static void uart1_irq(void)
AnnaBridge 167:e84263d55307 183 {
AnnaBridge 167:e84263d55307 184 uart_irq(0);
AnnaBridge 167:e84263d55307 185 }
AnnaBridge 167:e84263d55307 186
AnnaBridge 167:e84263d55307 187 static void uart2_irq(void)
AnnaBridge 167:e84263d55307 188 {
AnnaBridge 167:e84263d55307 189 uart_irq(1);
AnnaBridge 167:e84263d55307 190 }
AnnaBridge 167:e84263d55307 191
AnnaBridge 167:e84263d55307 192 static void uart3_irq(void)
AnnaBridge 167:e84263d55307 193 {
AnnaBridge 167:e84263d55307 194 uart_irq(2);
AnnaBridge 167:e84263d55307 195 }
AnnaBridge 167:e84263d55307 196
AnnaBridge 167:e84263d55307 197 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
AnnaBridge 167:e84263d55307 198 {
AnnaBridge 167:e84263d55307 199 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 200
AnnaBridge 167:e84263d55307 201 irq_handler = handler;
AnnaBridge 167:e84263d55307 202 serial_irq_ids[obj_s->index] = id;
AnnaBridge 167:e84263d55307 203 }
AnnaBridge 167:e84263d55307 204
AnnaBridge 167:e84263d55307 205 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
AnnaBridge 167:e84263d55307 206 {
AnnaBridge 167:e84263d55307 207 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 208 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
AnnaBridge 167:e84263d55307 209 IRQn_Type irq_n = (IRQn_Type)0;
AnnaBridge 167:e84263d55307 210 uint32_t vector = 0;
AnnaBridge 167:e84263d55307 211
AnnaBridge 167:e84263d55307 212 if (obj_s->uart == UART_1) {
AnnaBridge 167:e84263d55307 213 irq_n = USART1_IRQn;
AnnaBridge 167:e84263d55307 214 vector = (uint32_t)&uart1_irq;
AnnaBridge 167:e84263d55307 215 }
AnnaBridge 167:e84263d55307 216
AnnaBridge 167:e84263d55307 217 if (obj_s->uart == UART_2) {
AnnaBridge 167:e84263d55307 218 irq_n = USART2_IRQn;
AnnaBridge 167:e84263d55307 219 vector = (uint32_t)&uart2_irq;
AnnaBridge 167:e84263d55307 220 }
AnnaBridge 167:e84263d55307 221
AnnaBridge 167:e84263d55307 222 if (obj_s->uart == UART_3) {
AnnaBridge 167:e84263d55307 223 irq_n = USART3_IRQn;
AnnaBridge 167:e84263d55307 224 vector = (uint32_t)&uart3_irq;
AnnaBridge 167:e84263d55307 225 }
AnnaBridge 167:e84263d55307 226
AnnaBridge 167:e84263d55307 227 if (enable) {
AnnaBridge 167:e84263d55307 228 if (irq == RxIrq) {
AnnaBridge 167:e84263d55307 229 __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
AnnaBridge 167:e84263d55307 230 } else { // TxIrq
AnnaBridge 167:e84263d55307 231 __HAL_UART_ENABLE_IT(huart, UART_IT_TC);
AnnaBridge 167:e84263d55307 232 }
AnnaBridge 167:e84263d55307 233 NVIC_SetVector(irq_n, vector);
AnnaBridge 167:e84263d55307 234 NVIC_EnableIRQ(irq_n);
AnnaBridge 167:e84263d55307 235
AnnaBridge 167:e84263d55307 236 } else { // disable
AnnaBridge 167:e84263d55307 237 int all_disabled = 0;
AnnaBridge 167:e84263d55307 238 if (irq == RxIrq) {
AnnaBridge 167:e84263d55307 239 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
AnnaBridge 167:e84263d55307 240 // Check if TxIrq is disabled too
AnnaBridge 167:e84263d55307 241 if ((huart->Instance->CR1 & USART_CR1_TXEIE) == 0) {
AnnaBridge 167:e84263d55307 242 all_disabled = 1;
AnnaBridge 167:e84263d55307 243 }
AnnaBridge 167:e84263d55307 244 } else { // TxIrq
AnnaBridge 167:e84263d55307 245 __HAL_UART_DISABLE_IT(huart, UART_IT_TC);
AnnaBridge 167:e84263d55307 246 // Check if RxIrq is disabled too
AnnaBridge 167:e84263d55307 247 if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) {
AnnaBridge 167:e84263d55307 248 all_disabled = 1;
AnnaBridge 167:e84263d55307 249 }
AnnaBridge 167:e84263d55307 250 }
AnnaBridge 167:e84263d55307 251
AnnaBridge 167:e84263d55307 252 if (all_disabled) {
AnnaBridge 167:e84263d55307 253 NVIC_DisableIRQ(irq_n);
AnnaBridge 167:e84263d55307 254 }
AnnaBridge 167:e84263d55307 255 }
AnnaBridge 167:e84263d55307 256 }
AnnaBridge 167:e84263d55307 257
AnnaBridge 167:e84263d55307 258 /******************************************************************************
AnnaBridge 167:e84263d55307 259 * READ/WRITE
AnnaBridge 167:e84263d55307 260 ******************************************************************************/
AnnaBridge 167:e84263d55307 261
AnnaBridge 167:e84263d55307 262 int serial_getc(serial_t *obj)
AnnaBridge 167:e84263d55307 263 {
AnnaBridge 167:e84263d55307 264 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 265 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
AnnaBridge 167:e84263d55307 266
AnnaBridge 167:e84263d55307 267 while (!serial_readable(obj));
AnnaBridge 167:e84263d55307 268 if (obj_s->databits == UART_WORDLENGTH_8B) {
AnnaBridge 167:e84263d55307 269 return (int)(huart->Instance->DR & (uint8_t)0xFF);
AnnaBridge 167:e84263d55307 270 } else {
AnnaBridge 167:e84263d55307 271 return (int)(huart->Instance->DR & (uint16_t)0x1FF);
AnnaBridge 167:e84263d55307 272 }
AnnaBridge 167:e84263d55307 273 }
AnnaBridge 167:e84263d55307 274
AnnaBridge 167:e84263d55307 275 void serial_putc(serial_t *obj, int c)
AnnaBridge 167:e84263d55307 276 {
AnnaBridge 167:e84263d55307 277 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 278 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
AnnaBridge 167:e84263d55307 279
AnnaBridge 167:e84263d55307 280 while (!serial_writable(obj));
AnnaBridge 167:e84263d55307 281 if (obj_s->databits == UART_WORDLENGTH_8B) {
AnnaBridge 167:e84263d55307 282 huart->Instance->DR = (uint8_t)(c & (uint8_t)0xFF);
AnnaBridge 167:e84263d55307 283 } else {
AnnaBridge 167:e84263d55307 284 huart->Instance->DR = (uint16_t)(c & (uint16_t)0x1FF);
AnnaBridge 167:e84263d55307 285 }
AnnaBridge 167:e84263d55307 286 }
AnnaBridge 167:e84263d55307 287
AnnaBridge 167:e84263d55307 288 void serial_clear(serial_t *obj)
AnnaBridge 167:e84263d55307 289 {
AnnaBridge 167:e84263d55307 290 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 291 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
AnnaBridge 167:e84263d55307 292
AnnaBridge 167:e84263d55307 293 huart->TxXferCount = 0;
AnnaBridge 167:e84263d55307 294 huart->RxXferCount = 0;
AnnaBridge 167:e84263d55307 295 }
AnnaBridge 167:e84263d55307 296
AnnaBridge 167:e84263d55307 297 void serial_break_set(serial_t *obj)
AnnaBridge 167:e84263d55307 298 {
AnnaBridge 167:e84263d55307 299 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 300 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
AnnaBridge 167:e84263d55307 301
AnnaBridge 167:e84263d55307 302 HAL_LIN_SendBreak(huart);
AnnaBridge 167:e84263d55307 303 }
AnnaBridge 167:e84263d55307 304
AnnaBridge 167:e84263d55307 305 #if DEVICE_SERIAL_ASYNCH
AnnaBridge 167:e84263d55307 306
AnnaBridge 167:e84263d55307 307 /******************************************************************************
AnnaBridge 167:e84263d55307 308 * LOCAL HELPER FUNCTIONS
AnnaBridge 167:e84263d55307 309 ******************************************************************************/
AnnaBridge 167:e84263d55307 310
AnnaBridge 167:e84263d55307 311 /**
AnnaBridge 167:e84263d55307 312 * Configure the TX buffer for an asynchronous write serial transaction
AnnaBridge 167:e84263d55307 313 *
AnnaBridge 167:e84263d55307 314 * @param obj The serial object.
AnnaBridge 167:e84263d55307 315 * @param tx The buffer for sending.
AnnaBridge 167:e84263d55307 316 * @param tx_length The number of words to transmit.
AnnaBridge 167:e84263d55307 317 */
AnnaBridge 167:e84263d55307 318 static void serial_tx_buffer_set(serial_t *obj, void *tx, int tx_length, uint8_t width)
AnnaBridge 167:e84263d55307 319 {
AnnaBridge 167:e84263d55307 320 (void)width;
AnnaBridge 167:e84263d55307 321
AnnaBridge 167:e84263d55307 322 // Exit if a transmit is already on-going
AnnaBridge 167:e84263d55307 323 if (serial_tx_active(obj)) {
AnnaBridge 167:e84263d55307 324 return;
AnnaBridge 167:e84263d55307 325 }
AnnaBridge 167:e84263d55307 326
AnnaBridge 167:e84263d55307 327 obj->tx_buff.buffer = tx;
AnnaBridge 167:e84263d55307 328 obj->tx_buff.length = tx_length;
AnnaBridge 167:e84263d55307 329 obj->tx_buff.pos = 0;
AnnaBridge 167:e84263d55307 330 }
AnnaBridge 167:e84263d55307 331
AnnaBridge 167:e84263d55307 332 /**
AnnaBridge 167:e84263d55307 333 * Configure the RX buffer for an asynchronous write serial transaction
AnnaBridge 167:e84263d55307 334 *
AnnaBridge 167:e84263d55307 335 * @param obj The serial object.
AnnaBridge 167:e84263d55307 336 * @param tx The buffer for sending.
AnnaBridge 167:e84263d55307 337 * @param tx_length The number of words to transmit.
AnnaBridge 167:e84263d55307 338 */
AnnaBridge 167:e84263d55307 339 static void serial_rx_buffer_set(serial_t *obj, void *rx, int rx_length, uint8_t width)
AnnaBridge 167:e84263d55307 340 {
AnnaBridge 167:e84263d55307 341 (void)width;
AnnaBridge 167:e84263d55307 342
AnnaBridge 167:e84263d55307 343 // Exit if a reception is already on-going
AnnaBridge 167:e84263d55307 344 if (serial_rx_active(obj)) {
AnnaBridge 167:e84263d55307 345 return;
AnnaBridge 167:e84263d55307 346 }
AnnaBridge 167:e84263d55307 347
AnnaBridge 167:e84263d55307 348 obj->rx_buff.buffer = rx;
AnnaBridge 167:e84263d55307 349 obj->rx_buff.length = rx_length;
AnnaBridge 167:e84263d55307 350 obj->rx_buff.pos = 0;
AnnaBridge 167:e84263d55307 351 }
AnnaBridge 167:e84263d55307 352
AnnaBridge 167:e84263d55307 353 /**
AnnaBridge 167:e84263d55307 354 * Configure events
AnnaBridge 167:e84263d55307 355 *
AnnaBridge 167:e84263d55307 356 * @param obj The serial object
AnnaBridge 167:e84263d55307 357 * @param event The logical OR of the events to configure
AnnaBridge 167:e84263d55307 358 * @param enable Set to non-zero to enable events, or zero to disable them
AnnaBridge 167:e84263d55307 359 */
AnnaBridge 167:e84263d55307 360 static void serial_enable_event(serial_t *obj, int event, uint8_t enable)
AnnaBridge 167:e84263d55307 361 {
AnnaBridge 167:e84263d55307 362 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 363
AnnaBridge 167:e84263d55307 364 // Shouldn't have to enable interrupt here, just need to keep track of the requested events.
AnnaBridge 167:e84263d55307 365 if (enable) {
AnnaBridge 167:e84263d55307 366 obj_s->events |= event;
AnnaBridge 167:e84263d55307 367 } else {
AnnaBridge 167:e84263d55307 368 obj_s->events &= ~event;
AnnaBridge 167:e84263d55307 369 }
AnnaBridge 167:e84263d55307 370 }
AnnaBridge 167:e84263d55307 371
AnnaBridge 167:e84263d55307 372
AnnaBridge 167:e84263d55307 373 /**
AnnaBridge 167:e84263d55307 374 * Get index of serial object TX IRQ, relating it to the physical peripheral.
AnnaBridge 167:e84263d55307 375 *
AnnaBridge 167:e84263d55307 376 * @param obj pointer to serial object
AnnaBridge 167:e84263d55307 377 * @return internal NVIC TX IRQ index of U(S)ART peripheral
AnnaBridge 167:e84263d55307 378 */
AnnaBridge 167:e84263d55307 379 static IRQn_Type serial_get_irq_n(serial_t *obj)
AnnaBridge 167:e84263d55307 380 {
AnnaBridge 167:e84263d55307 381 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 382 IRQn_Type irq_n;
AnnaBridge 167:e84263d55307 383
AnnaBridge 167:e84263d55307 384 switch (obj_s->index) {
AnnaBridge 167:e84263d55307 385 case 0:
AnnaBridge 167:e84263d55307 386 irq_n = USART1_IRQn;
AnnaBridge 167:e84263d55307 387 break;
AnnaBridge 167:e84263d55307 388
AnnaBridge 167:e84263d55307 389 case 1:
AnnaBridge 167:e84263d55307 390 irq_n = USART2_IRQn;
AnnaBridge 167:e84263d55307 391 break;
AnnaBridge 167:e84263d55307 392
AnnaBridge 167:e84263d55307 393 case 2:
AnnaBridge 167:e84263d55307 394 irq_n = USART3_IRQn;
AnnaBridge 167:e84263d55307 395 break;
AnnaBridge 167:e84263d55307 396
AnnaBridge 167:e84263d55307 397 default:
AnnaBridge 167:e84263d55307 398 irq_n = (IRQn_Type)0;
AnnaBridge 167:e84263d55307 399 }
AnnaBridge 167:e84263d55307 400
AnnaBridge 167:e84263d55307 401 return irq_n;
AnnaBridge 167:e84263d55307 402 }
AnnaBridge 167:e84263d55307 403
AnnaBridge 167:e84263d55307 404 /******************************************************************************
AnnaBridge 167:e84263d55307 405 * MBED API FUNCTIONS
AnnaBridge 167:e84263d55307 406 ******************************************************************************/
AnnaBridge 167:e84263d55307 407
AnnaBridge 167:e84263d55307 408 /**
AnnaBridge 167:e84263d55307 409 * Begin asynchronous TX transfer. The used buffer is specified in the serial
AnnaBridge 167:e84263d55307 410 * object, tx_buff
AnnaBridge 167:e84263d55307 411 *
AnnaBridge 167:e84263d55307 412 * @param obj The serial object
AnnaBridge 167:e84263d55307 413 * @param tx The buffer for sending
AnnaBridge 167:e84263d55307 414 * @param tx_length The number of words to transmit
AnnaBridge 167:e84263d55307 415 * @param tx_width The bit width of buffer word
AnnaBridge 167:e84263d55307 416 * @param handler The serial handler
AnnaBridge 167:e84263d55307 417 * @param event The logical OR of events to be registered
AnnaBridge 167:e84263d55307 418 * @param hint A suggestion for how to use DMA with this transfer
AnnaBridge 167:e84263d55307 419 * @return Returns number of data transfered, or 0 otherwise
AnnaBridge 167:e84263d55307 420 */
AnnaBridge 167:e84263d55307 421 int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx_width, uint32_t handler, uint32_t event, DMAUsage hint)
AnnaBridge 167:e84263d55307 422 {
AnnaBridge 167:e84263d55307 423 // TODO: DMA usage is currently ignored
AnnaBridge 167:e84263d55307 424 (void) hint;
AnnaBridge 167:e84263d55307 425
AnnaBridge 167:e84263d55307 426 // Check buffer is ok
AnnaBridge 167:e84263d55307 427 MBED_ASSERT(tx != (void*)0);
AnnaBridge 167:e84263d55307 428 MBED_ASSERT(tx_width == 8); // support only 8b width
AnnaBridge 167:e84263d55307 429
AnnaBridge 167:e84263d55307 430 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 431 UART_HandleTypeDef * huart = &uart_handlers[obj_s->index];
AnnaBridge 167:e84263d55307 432
AnnaBridge 167:e84263d55307 433 if (tx_length == 0) {
AnnaBridge 167:e84263d55307 434 return 0;
AnnaBridge 167:e84263d55307 435 }
AnnaBridge 167:e84263d55307 436
AnnaBridge 167:e84263d55307 437 // Set up buffer
AnnaBridge 167:e84263d55307 438 serial_tx_buffer_set(obj, (void *)tx, tx_length, tx_width);
AnnaBridge 167:e84263d55307 439
AnnaBridge 167:e84263d55307 440 // Set up events
AnnaBridge 167:e84263d55307 441 serial_enable_event(obj, SERIAL_EVENT_TX_ALL, 0); // Clear all events
AnnaBridge 167:e84263d55307 442 serial_enable_event(obj, event, 1); // Set only the wanted events
AnnaBridge 167:e84263d55307 443
AnnaBridge 167:e84263d55307 444 // Enable interrupt
AnnaBridge 167:e84263d55307 445 IRQn_Type irq_n = serial_get_irq_n(obj);
AnnaBridge 167:e84263d55307 446 NVIC_ClearPendingIRQ(irq_n);
AnnaBridge 167:e84263d55307 447 NVIC_DisableIRQ(irq_n);
AnnaBridge 167:e84263d55307 448 NVIC_SetPriority(irq_n, 1);
AnnaBridge 167:e84263d55307 449 NVIC_SetVector(irq_n, (uint32_t)handler);
AnnaBridge 167:e84263d55307 450 NVIC_EnableIRQ(irq_n);
AnnaBridge 167:e84263d55307 451
AnnaBridge 167:e84263d55307 452 // the following function will enable UART_IT_TXE and error interrupts
AnnaBridge 167:e84263d55307 453 if (HAL_UART_Transmit_IT(huart, (uint8_t*)tx, tx_length) != HAL_OK) {
AnnaBridge 167:e84263d55307 454 return 0;
AnnaBridge 167:e84263d55307 455 }
AnnaBridge 167:e84263d55307 456
AnnaBridge 167:e84263d55307 457 return tx_length;
AnnaBridge 167:e84263d55307 458 }
AnnaBridge 167:e84263d55307 459
AnnaBridge 167:e84263d55307 460 /**
AnnaBridge 167:e84263d55307 461 * Begin asynchronous RX transfer (enable interrupt for data collecting)
AnnaBridge 167:e84263d55307 462 * The used buffer is specified in the serial object, rx_buff
AnnaBridge 167:e84263d55307 463 *
AnnaBridge 167:e84263d55307 464 * @param obj The serial object
AnnaBridge 167:e84263d55307 465 * @param rx The buffer for sending
AnnaBridge 167:e84263d55307 466 * @param rx_length The number of words to transmit
AnnaBridge 167:e84263d55307 467 * @param rx_width The bit width of buffer word
AnnaBridge 167:e84263d55307 468 * @param handler The serial handler
AnnaBridge 167:e84263d55307 469 * @param event The logical OR of events to be registered
AnnaBridge 167:e84263d55307 470 * @param handler The serial handler
AnnaBridge 167:e84263d55307 471 * @param char_match A character in range 0-254 to be matched
AnnaBridge 167:e84263d55307 472 * @param hint A suggestion for how to use DMA with this transfer
AnnaBridge 167:e84263d55307 473 */
AnnaBridge 167:e84263d55307 474 void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_width, uint32_t handler, uint32_t event, uint8_t char_match, DMAUsage hint)
AnnaBridge 167:e84263d55307 475 {
AnnaBridge 167:e84263d55307 476 // TODO: DMA usage is currently ignored
AnnaBridge 167:e84263d55307 477 (void) hint;
AnnaBridge 167:e84263d55307 478
AnnaBridge 167:e84263d55307 479 /* Sanity check arguments */
AnnaBridge 167:e84263d55307 480 MBED_ASSERT(obj);
AnnaBridge 167:e84263d55307 481 MBED_ASSERT(rx != (void*)0);
AnnaBridge 167:e84263d55307 482 MBED_ASSERT(rx_width == 8); // support only 8b width
AnnaBridge 167:e84263d55307 483
AnnaBridge 167:e84263d55307 484 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 485 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
AnnaBridge 167:e84263d55307 486
AnnaBridge 167:e84263d55307 487 serial_enable_event(obj, SERIAL_EVENT_RX_ALL, 0);
AnnaBridge 167:e84263d55307 488 serial_enable_event(obj, event, 1);
AnnaBridge 167:e84263d55307 489
AnnaBridge 167:e84263d55307 490 // set CharMatch
AnnaBridge 167:e84263d55307 491 obj->char_match = char_match;
AnnaBridge 167:e84263d55307 492
AnnaBridge 167:e84263d55307 493 serial_rx_buffer_set(obj, rx, rx_length, rx_width);
AnnaBridge 167:e84263d55307 494
AnnaBridge 167:e84263d55307 495 IRQn_Type irq_n = serial_get_irq_n(obj);
AnnaBridge 167:e84263d55307 496 NVIC_ClearPendingIRQ(irq_n);
AnnaBridge 167:e84263d55307 497 NVIC_DisableIRQ(irq_n);
AnnaBridge 167:e84263d55307 498 NVIC_SetPriority(irq_n, 0);
AnnaBridge 167:e84263d55307 499 NVIC_SetVector(irq_n, (uint32_t)handler);
AnnaBridge 167:e84263d55307 500 NVIC_EnableIRQ(irq_n);
AnnaBridge 167:e84263d55307 501
AnnaBridge 167:e84263d55307 502 // following HAL function will enable the RXNE interrupt + error interrupts
AnnaBridge 167:e84263d55307 503 HAL_UART_Receive_IT(huart, (uint8_t*)rx, rx_length);
AnnaBridge 167:e84263d55307 504 }
AnnaBridge 167:e84263d55307 505
AnnaBridge 167:e84263d55307 506 /**
AnnaBridge 167:e84263d55307 507 * Attempts to determine if the serial peripheral is already in use for TX
AnnaBridge 167:e84263d55307 508 *
AnnaBridge 167:e84263d55307 509 * @param obj The serial object
AnnaBridge 167:e84263d55307 510 * @return Non-zero if the TX transaction is ongoing, 0 otherwise
AnnaBridge 167:e84263d55307 511 */
AnnaBridge 167:e84263d55307 512 uint8_t serial_tx_active(serial_t *obj)
AnnaBridge 167:e84263d55307 513 {
AnnaBridge 167:e84263d55307 514 MBED_ASSERT(obj);
AnnaBridge 167:e84263d55307 515
AnnaBridge 167:e84263d55307 516 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 517 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
AnnaBridge 167:e84263d55307 518
AnnaBridge 167:e84263d55307 519 return ((HAL_UART_GetState(huart) == HAL_UART_STATE_BUSY_TX) ? 1 : 0);
AnnaBridge 167:e84263d55307 520 }
AnnaBridge 167:e84263d55307 521
AnnaBridge 167:e84263d55307 522 /**
AnnaBridge 167:e84263d55307 523 * Attempts to determine if the serial peripheral is already in use for RX
AnnaBridge 167:e84263d55307 524 *
AnnaBridge 167:e84263d55307 525 * @param obj The serial object
AnnaBridge 167:e84263d55307 526 * @return Non-zero if the RX transaction is ongoing, 0 otherwise
AnnaBridge 167:e84263d55307 527 */
AnnaBridge 167:e84263d55307 528 uint8_t serial_rx_active(serial_t *obj)
AnnaBridge 167:e84263d55307 529 {
AnnaBridge 167:e84263d55307 530 MBED_ASSERT(obj);
AnnaBridge 167:e84263d55307 531
AnnaBridge 167:e84263d55307 532 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 533 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
AnnaBridge 167:e84263d55307 534
AnnaBridge 167:e84263d55307 535 return ((HAL_UART_GetState(huart) == HAL_UART_STATE_BUSY_RX) ? 1 : 0);
AnnaBridge 167:e84263d55307 536 }
AnnaBridge 167:e84263d55307 537
AnnaBridge 167:e84263d55307 538 void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
AnnaBridge 167:e84263d55307 539 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) {
AnnaBridge 167:e84263d55307 540 __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC);
AnnaBridge 167:e84263d55307 541 }
AnnaBridge 167:e84263d55307 542 }
AnnaBridge 167:e84263d55307 543
AnnaBridge 167:e84263d55307 544 void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) {
AnnaBridge 167:e84263d55307 545 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) {
AnnaBridge 167:e84263d55307 546 volatile uint32_t tmpval = huart->Instance->DR; // Clear PE flag
AnnaBridge 167:e84263d55307 547 } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) {
AnnaBridge 167:e84263d55307 548 volatile uint32_t tmpval = huart->Instance->DR; // Clear FE flag
AnnaBridge 167:e84263d55307 549 } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_NE) != RESET) {
AnnaBridge 167:e84263d55307 550 volatile uint32_t tmpval = huart->Instance->DR; // Clear NE flag
AnnaBridge 167:e84263d55307 551 } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) {
AnnaBridge 167:e84263d55307 552 volatile uint32_t tmpval = huart->Instance->DR; // Clear ORE flag
AnnaBridge 167:e84263d55307 553 }
AnnaBridge 167:e84263d55307 554 }
AnnaBridge 167:e84263d55307 555
AnnaBridge 167:e84263d55307 556 /**
AnnaBridge 167:e84263d55307 557 * The asynchronous TX and RX handler.
AnnaBridge 167:e84263d55307 558 *
AnnaBridge 167:e84263d55307 559 * @param obj The serial object
AnnaBridge 167:e84263d55307 560 * @return Returns event flags if a TX/RX transfer termination condition was met or 0 otherwise
AnnaBridge 167:e84263d55307 561 */
AnnaBridge 167:e84263d55307 562 int serial_irq_handler_asynch(serial_t *obj)
AnnaBridge 167:e84263d55307 563 {
AnnaBridge 167:e84263d55307 564 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 565 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
AnnaBridge 167:e84263d55307 566
AnnaBridge 167:e84263d55307 567 volatile int return_event = 0;
AnnaBridge 167:e84263d55307 568 uint8_t *buf = (uint8_t*)(obj->rx_buff.buffer);
AnnaBridge 167:e84263d55307 569 uint8_t i = 0;
AnnaBridge 167:e84263d55307 570
AnnaBridge 167:e84263d55307 571 // TX PART:
AnnaBridge 167:e84263d55307 572 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) {
AnnaBridge 167:e84263d55307 573 if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC) != RESET) {
AnnaBridge 167:e84263d55307 574 // Return event SERIAL_EVENT_TX_COMPLETE if requested
AnnaBridge 167:e84263d55307 575 if ((obj_s->events & SERIAL_EVENT_TX_COMPLETE ) != 0) {
AnnaBridge 167:e84263d55307 576 return_event |= (SERIAL_EVENT_TX_COMPLETE & obj_s->events);
AnnaBridge 167:e84263d55307 577 }
AnnaBridge 167:e84263d55307 578 }
AnnaBridge 167:e84263d55307 579 }
AnnaBridge 167:e84263d55307 580
AnnaBridge 167:e84263d55307 581 // Handle error events
AnnaBridge 167:e84263d55307 582 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) {
AnnaBridge 167:e84263d55307 583 if (__HAL_UART_GET_IT_SOURCE(huart, USART_IT_ERR) != RESET) {
AnnaBridge 167:e84263d55307 584 return_event |= (SERIAL_EVENT_RX_PARITY_ERROR & obj_s->events);
AnnaBridge 167:e84263d55307 585 }
AnnaBridge 167:e84263d55307 586 }
AnnaBridge 167:e84263d55307 587
AnnaBridge 167:e84263d55307 588 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) {
AnnaBridge 167:e84263d55307 589 if (__HAL_UART_GET_IT_SOURCE(huart, USART_IT_ERR) != RESET) {
AnnaBridge 167:e84263d55307 590 return_event |= (SERIAL_EVENT_RX_FRAMING_ERROR & obj_s->events);
AnnaBridge 167:e84263d55307 591 }
AnnaBridge 167:e84263d55307 592 }
AnnaBridge 167:e84263d55307 593
AnnaBridge 167:e84263d55307 594 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) {
AnnaBridge 167:e84263d55307 595 if (__HAL_UART_GET_IT_SOURCE(huart, USART_IT_ERR) != RESET) {
AnnaBridge 167:e84263d55307 596 return_event |= (SERIAL_EVENT_RX_OVERRUN_ERROR & obj_s->events);
AnnaBridge 167:e84263d55307 597 }
AnnaBridge 167:e84263d55307 598 }
AnnaBridge 167:e84263d55307 599
AnnaBridge 167:e84263d55307 600 HAL_UART_IRQHandler(huart);
AnnaBridge 167:e84263d55307 601
AnnaBridge 167:e84263d55307 602 // Abort if an error occurs
AnnaBridge 167:e84263d55307 603 if (return_event & SERIAL_EVENT_RX_PARITY_ERROR ||
AnnaBridge 167:e84263d55307 604 return_event & SERIAL_EVENT_RX_FRAMING_ERROR ||
AnnaBridge 167:e84263d55307 605 return_event & SERIAL_EVENT_RX_OVERRUN_ERROR) {
AnnaBridge 167:e84263d55307 606 return return_event;
AnnaBridge 167:e84263d55307 607 }
AnnaBridge 167:e84263d55307 608
AnnaBridge 167:e84263d55307 609 //RX PART
AnnaBridge 167:e84263d55307 610 if (huart->RxXferSize != 0) {
AnnaBridge 167:e84263d55307 611 obj->rx_buff.pos = huart->RxXferSize - huart->RxXferCount;
AnnaBridge 167:e84263d55307 612 }
AnnaBridge 167:e84263d55307 613 if ((huart->RxXferCount == 0) && (obj->rx_buff.pos >= (obj->rx_buff.length - 1))) {
AnnaBridge 167:e84263d55307 614 return_event |= (SERIAL_EVENT_RX_COMPLETE & obj_s->events);
AnnaBridge 167:e84263d55307 615 }
AnnaBridge 167:e84263d55307 616
AnnaBridge 167:e84263d55307 617 // Check if char_match is present
AnnaBridge 167:e84263d55307 618 if (obj_s->events & SERIAL_EVENT_RX_CHARACTER_MATCH) {
AnnaBridge 167:e84263d55307 619 if (buf != NULL) {
AnnaBridge 167:e84263d55307 620 for (i = 0; i < obj->rx_buff.pos; i++) {
AnnaBridge 167:e84263d55307 621 if (buf[i] == obj->char_match) {
AnnaBridge 167:e84263d55307 622 obj->rx_buff.pos = i;
AnnaBridge 167:e84263d55307 623 return_event |= (SERIAL_EVENT_RX_CHARACTER_MATCH & obj_s->events);
AnnaBridge 167:e84263d55307 624 serial_rx_abort_asynch(obj);
AnnaBridge 167:e84263d55307 625 break;
AnnaBridge 167:e84263d55307 626 }
AnnaBridge 167:e84263d55307 627 }
AnnaBridge 167:e84263d55307 628 }
AnnaBridge 167:e84263d55307 629 }
AnnaBridge 167:e84263d55307 630
AnnaBridge 167:e84263d55307 631 return return_event;
AnnaBridge 167:e84263d55307 632 }
AnnaBridge 167:e84263d55307 633
AnnaBridge 167:e84263d55307 634 /**
AnnaBridge 167:e84263d55307 635 * Abort the ongoing TX transaction. It disables the enabled interupt for TX and
AnnaBridge 167:e84263d55307 636 * flush TX hardware buffer if TX FIFO is used
AnnaBridge 167:e84263d55307 637 *
AnnaBridge 167:e84263d55307 638 * @param obj The serial object
AnnaBridge 167:e84263d55307 639 */
AnnaBridge 167:e84263d55307 640 void serial_tx_abort_asynch(serial_t *obj)
AnnaBridge 167:e84263d55307 641 {
AnnaBridge 167:e84263d55307 642 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 643 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
AnnaBridge 167:e84263d55307 644
AnnaBridge 167:e84263d55307 645 __HAL_UART_DISABLE_IT(huart, UART_IT_TC);
AnnaBridge 167:e84263d55307 646 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
AnnaBridge 167:e84263d55307 647
AnnaBridge 167:e84263d55307 648 // clear flags
AnnaBridge 167:e84263d55307 649 __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC);
AnnaBridge 167:e84263d55307 650
AnnaBridge 167:e84263d55307 651 // reset states
AnnaBridge 167:e84263d55307 652 huart->TxXferCount = 0;
AnnaBridge 167:e84263d55307 653 // update handle state
AnnaBridge 167:e84263d55307 654 if(huart->gState == HAL_UART_STATE_BUSY_TX_RX) {
AnnaBridge 167:e84263d55307 655 huart->gState = HAL_UART_STATE_BUSY_RX;
AnnaBridge 167:e84263d55307 656 } else {
AnnaBridge 167:e84263d55307 657 huart->gState = HAL_UART_STATE_READY;
AnnaBridge 167:e84263d55307 658 }
AnnaBridge 167:e84263d55307 659 }
AnnaBridge 167:e84263d55307 660
AnnaBridge 167:e84263d55307 661 /**
AnnaBridge 167:e84263d55307 662 * Abort the ongoing RX transaction It disables the enabled interrupt for RX and
AnnaBridge 167:e84263d55307 663 * flush RX hardware buffer if RX FIFO is used
AnnaBridge 167:e84263d55307 664 *
AnnaBridge 167:e84263d55307 665 * @param obj The serial object
AnnaBridge 167:e84263d55307 666 */
AnnaBridge 167:e84263d55307 667 void serial_rx_abort_asynch(serial_t *obj)
AnnaBridge 167:e84263d55307 668 {
AnnaBridge 167:e84263d55307 669 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 670 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
AnnaBridge 167:e84263d55307 671
AnnaBridge 167:e84263d55307 672 // disable interrupts
AnnaBridge 167:e84263d55307 673 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
AnnaBridge 167:e84263d55307 674 __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
AnnaBridge 167:e84263d55307 675 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
AnnaBridge 167:e84263d55307 676
AnnaBridge 167:e84263d55307 677 // clear flags
AnnaBridge 167:e84263d55307 678 __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_RXNE);
AnnaBridge 167:e84263d55307 679 volatile uint32_t tmpval = huart->Instance->DR; // Clear errors flag
AnnaBridge 167:e84263d55307 680
AnnaBridge 167:e84263d55307 681 // reset states
AnnaBridge 167:e84263d55307 682 huart->RxXferCount = 0;
AnnaBridge 167:e84263d55307 683 // update handle state
AnnaBridge 167:e84263d55307 684 if(huart->RxState == HAL_UART_STATE_BUSY_TX_RX) {
AnnaBridge 167:e84263d55307 685 huart->RxState = HAL_UART_STATE_BUSY_TX;
AnnaBridge 167:e84263d55307 686 } else {
AnnaBridge 167:e84263d55307 687 huart->RxState = HAL_UART_STATE_READY;
AnnaBridge 167:e84263d55307 688 }
AnnaBridge 167:e84263d55307 689 }
AnnaBridge 167:e84263d55307 690
AnnaBridge 167:e84263d55307 691 #endif
AnnaBridge 167:e84263d55307 692
AnnaBridge 167:e84263d55307 693 #if DEVICE_SERIAL_FC
AnnaBridge 167:e84263d55307 694
AnnaBridge 167:e84263d55307 695 /**
AnnaBridge 167:e84263d55307 696 * Set HW Control Flow
AnnaBridge 167:e84263d55307 697 * @param obj The serial object
AnnaBridge 167:e84263d55307 698 * @param type The Control Flow type (FlowControlNone, FlowControlRTS, FlowControlCTS, FlowControlRTSCTS)
AnnaBridge 167:e84263d55307 699 * @param rxflow Pin for the rxflow
AnnaBridge 167:e84263d55307 700 * @param txflow Pin for the txflow
AnnaBridge 167:e84263d55307 701 */
AnnaBridge 167:e84263d55307 702 void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow)
AnnaBridge 167:e84263d55307 703 {
AnnaBridge 167:e84263d55307 704 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 167:e84263d55307 705
AnnaBridge 167:e84263d55307 706 // Determine the UART to use (UART_1, UART_2, ...)
AnnaBridge 167:e84263d55307 707 UARTName uart_rts = (UARTName)pinmap_peripheral(rxflow, PinMap_UART_RTS);
AnnaBridge 167:e84263d55307 708 UARTName uart_cts = (UARTName)pinmap_peripheral(txflow, PinMap_UART_CTS);
AnnaBridge 167:e84263d55307 709
AnnaBridge 167:e84263d55307 710 // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
AnnaBridge 167:e84263d55307 711 obj_s->uart = (UARTName)pinmap_merge(uart_cts, uart_rts);
AnnaBridge 167:e84263d55307 712 MBED_ASSERT(obj_s->uart != (UARTName)NC);
AnnaBridge 167:e84263d55307 713
AnnaBridge 167:e84263d55307 714 if(type == FlowControlNone) {
AnnaBridge 167:e84263d55307 715 // Disable hardware flow control
AnnaBridge 167:e84263d55307 716 obj_s->hw_flow_ctl = UART_HWCONTROL_NONE;
AnnaBridge 167:e84263d55307 717 }
AnnaBridge 167:e84263d55307 718 if (type == FlowControlRTS) {
AnnaBridge 167:e84263d55307 719 // Enable RTS
AnnaBridge 167:e84263d55307 720 MBED_ASSERT(uart_rts != (UARTName)NC);
AnnaBridge 167:e84263d55307 721 obj_s->hw_flow_ctl = UART_HWCONTROL_RTS;
AnnaBridge 167:e84263d55307 722 obj_s->pin_rts = rxflow;
AnnaBridge 167:e84263d55307 723 // Enable the pin for RTS function
AnnaBridge 167:e84263d55307 724 pinmap_pinout(rxflow, PinMap_UART_RTS);
AnnaBridge 167:e84263d55307 725 }
AnnaBridge 167:e84263d55307 726 if (type == FlowControlCTS) {
AnnaBridge 167:e84263d55307 727 // Enable CTS
AnnaBridge 167:e84263d55307 728 MBED_ASSERT(uart_cts != (UARTName)NC);
AnnaBridge 167:e84263d55307 729 obj_s->hw_flow_ctl = UART_HWCONTROL_CTS;
AnnaBridge 167:e84263d55307 730 obj_s->pin_cts = txflow;
AnnaBridge 167:e84263d55307 731 // Enable the pin for CTS function
AnnaBridge 167:e84263d55307 732 pinmap_pinout(txflow, PinMap_UART_CTS);
AnnaBridge 167:e84263d55307 733 }
AnnaBridge 167:e84263d55307 734 if (type == FlowControlRTSCTS) {
AnnaBridge 167:e84263d55307 735 // Enable CTS & RTS
AnnaBridge 167:e84263d55307 736 MBED_ASSERT(uart_rts != (UARTName)NC);
AnnaBridge 167:e84263d55307 737 MBED_ASSERT(uart_cts != (UARTName)NC);
AnnaBridge 167:e84263d55307 738 obj_s->hw_flow_ctl = UART_HWCONTROL_RTS_CTS;
AnnaBridge 167:e84263d55307 739 obj_s->pin_rts = rxflow;
AnnaBridge 167:e84263d55307 740 obj_s->pin_cts = txflow;
AnnaBridge 167:e84263d55307 741 // Enable the pin for CTS function
AnnaBridge 167:e84263d55307 742 pinmap_pinout(txflow, PinMap_UART_CTS);
AnnaBridge 167:e84263d55307 743 // Enable the pin for RTS function
AnnaBridge 167:e84263d55307 744 pinmap_pinout(rxflow, PinMap_UART_RTS);
AnnaBridge 167:e84263d55307 745 }
AnnaBridge 167:e84263d55307 746
AnnaBridge 167:e84263d55307 747 init_uart(obj);
AnnaBridge 167:e84263d55307 748 }
AnnaBridge 167:e84263d55307 749
AnnaBridge 167:e84263d55307 750 #endif
AnnaBridge 167:e84263d55307 751
AnnaBridge 167:e84263d55307 752 #endif