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