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