Mouse code for the MacroRat
mbed-dev/targets/TARGET_STM/TARGET_STM32F3/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) 2014, 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 | #if DEVICE_SERIAL_FC |
sahilmgandhi | 18:6a4db94011d3 | 68 | huart->Init.HwFlowCtl = obj_s->hw_flow_ctl; |
sahilmgandhi | 18:6a4db94011d3 | 69 | #else |
sahilmgandhi | 18:6a4db94011d3 | 70 | huart->Init.HwFlowCtl = UART_HWCONTROL_NONE; |
sahilmgandhi | 18:6a4db94011d3 | 71 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 72 | huart->TxXferCount = 0; |
sahilmgandhi | 18:6a4db94011d3 | 73 | huart->TxXferSize = 0; |
sahilmgandhi | 18:6a4db94011d3 | 74 | huart->RxXferCount = 0; |
sahilmgandhi | 18:6a4db94011d3 | 75 | huart->RxXferSize = 0; |
sahilmgandhi | 18:6a4db94011d3 | 76 | |
sahilmgandhi | 18:6a4db94011d3 | 77 | if (obj_s->pin_rx == NC) { |
sahilmgandhi | 18:6a4db94011d3 | 78 | huart->Init.Mode = UART_MODE_TX; |
sahilmgandhi | 18:6a4db94011d3 | 79 | } else if (obj_s->pin_tx == NC) { |
sahilmgandhi | 18:6a4db94011d3 | 80 | huart->Init.Mode = UART_MODE_RX; |
sahilmgandhi | 18:6a4db94011d3 | 81 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 82 | huart->Init.Mode = UART_MODE_TX_RX; |
sahilmgandhi | 18:6a4db94011d3 | 83 | } |
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 USART clock + switch to SystemClock |
sahilmgandhi | 18:6a4db94011d3 | 104 | if (obj_s->uart == UART_1) { |
sahilmgandhi | 18:6a4db94011d3 | 105 | __USART1_FORCE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 106 | __USART1_RELEASE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 107 | __USART1_CLK_ENABLE(); |
sahilmgandhi | 18:6a4db94011d3 | 108 | #if defined(RCC_USART1CLKSOURCE_SYSCLK) |
sahilmgandhi | 18:6a4db94011d3 | 109 | __HAL_RCC_USART1_CONFIG(RCC_USART1CLKSOURCE_SYSCLK); |
sahilmgandhi | 18:6a4db94011d3 | 110 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 111 | obj_s->index = 0; |
sahilmgandhi | 18:6a4db94011d3 | 112 | } |
sahilmgandhi | 18:6a4db94011d3 | 113 | #if defined(USART2_BASE) |
sahilmgandhi | 18:6a4db94011d3 | 114 | if (obj_s->uart == UART_2) { |
sahilmgandhi | 18:6a4db94011d3 | 115 | __USART2_FORCE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 116 | __USART2_RELEASE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 117 | __USART2_CLK_ENABLE(); |
sahilmgandhi | 18:6a4db94011d3 | 118 | #if defined(RCC_USART2CLKSOURCE_SYSCLK) |
sahilmgandhi | 18:6a4db94011d3 | 119 | __HAL_RCC_USART2_CONFIG(RCC_USART2CLKSOURCE_SYSCLK); |
sahilmgandhi | 18:6a4db94011d3 | 120 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 121 | obj_s->index = 1; |
sahilmgandhi | 18:6a4db94011d3 | 122 | } |
sahilmgandhi | 18:6a4db94011d3 | 123 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 124 | #if defined(USART3_BASE) |
sahilmgandhi | 18:6a4db94011d3 | 125 | if (obj_s->uart == UART_3) { |
sahilmgandhi | 18:6a4db94011d3 | 126 | __USART3_FORCE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 127 | __USART3_RELEASE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 128 | __USART3_CLK_ENABLE(); |
sahilmgandhi | 18:6a4db94011d3 | 129 | #if defined(RCC_USART3CLKSOURCE_SYSCLK) |
sahilmgandhi | 18:6a4db94011d3 | 130 | __HAL_RCC_USART3_CONFIG(RCC_USART3CLKSOURCE_SYSCLK); |
sahilmgandhi | 18:6a4db94011d3 | 131 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 132 | obj_s->index = 2; |
sahilmgandhi | 18:6a4db94011d3 | 133 | } |
sahilmgandhi | 18:6a4db94011d3 | 134 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 135 | #if defined(UART4_BASE) |
sahilmgandhi | 18:6a4db94011d3 | 136 | if (obj_s->uart == UART_4) { |
sahilmgandhi | 18:6a4db94011d3 | 137 | __UART4_FORCE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 138 | __UART4_RELEASE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 139 | __UART4_CLK_ENABLE(); |
sahilmgandhi | 18:6a4db94011d3 | 140 | #if defined(RCC_UART4CLKSOURCE_SYSCLK) |
sahilmgandhi | 18:6a4db94011d3 | 141 | __HAL_RCC_UART4_CONFIG(RCC_UART4CLKSOURCE_SYSCLK); |
sahilmgandhi | 18:6a4db94011d3 | 142 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 143 | obj_s->index = 3; |
sahilmgandhi | 18:6a4db94011d3 | 144 | } |
sahilmgandhi | 18:6a4db94011d3 | 145 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 146 | #if defined(UART5_BASE) |
sahilmgandhi | 18:6a4db94011d3 | 147 | if (obj_s->uart == UART_5) { |
sahilmgandhi | 18:6a4db94011d3 | 148 | __HAL_RCC_UART5_FORCE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 149 | __HAL_RCC_UART5_RELEASE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 150 | __UART5_CLK_ENABLE(); |
sahilmgandhi | 18:6a4db94011d3 | 151 | #if defined(RCC_UART5CLKSOURCE_SYSCLK) |
sahilmgandhi | 18:6a4db94011d3 | 152 | __HAL_RCC_UART5_CONFIG(RCC_UART5CLKSOURCE_SYSCLK); |
sahilmgandhi | 18:6a4db94011d3 | 153 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 154 | obj_s->index = 4; |
sahilmgandhi | 18:6a4db94011d3 | 155 | } |
sahilmgandhi | 18:6a4db94011d3 | 156 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 157 | |
sahilmgandhi | 18:6a4db94011d3 | 158 | // Configure the UART pins |
sahilmgandhi | 18:6a4db94011d3 | 159 | pinmap_pinout(tx, PinMap_UART_TX); |
sahilmgandhi | 18:6a4db94011d3 | 160 | pinmap_pinout(rx, PinMap_UART_RX); |
sahilmgandhi | 18:6a4db94011d3 | 161 | |
sahilmgandhi | 18:6a4db94011d3 | 162 | if (tx != NC) { |
sahilmgandhi | 18:6a4db94011d3 | 163 | pin_mode(tx, PullUp); |
sahilmgandhi | 18:6a4db94011d3 | 164 | } |
sahilmgandhi | 18:6a4db94011d3 | 165 | if (rx != NC) { |
sahilmgandhi | 18:6a4db94011d3 | 166 | pin_mode(rx, PullUp); |
sahilmgandhi | 18:6a4db94011d3 | 167 | } |
sahilmgandhi | 18:6a4db94011d3 | 168 | |
sahilmgandhi | 18:6a4db94011d3 | 169 | // Configure UART |
sahilmgandhi | 18:6a4db94011d3 | 170 | obj_s->baudrate = 9600; |
sahilmgandhi | 18:6a4db94011d3 | 171 | obj_s->databits = UART_WORDLENGTH_8B; |
sahilmgandhi | 18:6a4db94011d3 | 172 | obj_s->stopbits = UART_STOPBITS_1; |
sahilmgandhi | 18:6a4db94011d3 | 173 | obj_s->parity = UART_PARITY_NONE; |
sahilmgandhi | 18:6a4db94011d3 | 174 | |
sahilmgandhi | 18:6a4db94011d3 | 175 | #if DEVICE_SERIAL_FC |
sahilmgandhi | 18:6a4db94011d3 | 176 | obj_s->hw_flow_ctl = UART_HWCONTROL_NONE; |
sahilmgandhi | 18:6a4db94011d3 | 177 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 178 | |
sahilmgandhi | 18:6a4db94011d3 | 179 | obj_s->pin_tx = tx; |
sahilmgandhi | 18:6a4db94011d3 | 180 | obj_s->pin_rx = rx; |
sahilmgandhi | 18:6a4db94011d3 | 181 | |
sahilmgandhi | 18:6a4db94011d3 | 182 | init_uart(obj); |
sahilmgandhi | 18:6a4db94011d3 | 183 | |
sahilmgandhi | 18:6a4db94011d3 | 184 | // For stdio management |
sahilmgandhi | 18:6a4db94011d3 | 185 | if (obj_s->uart == STDIO_UART) { |
sahilmgandhi | 18:6a4db94011d3 | 186 | stdio_uart_inited = 1; |
sahilmgandhi | 18:6a4db94011d3 | 187 | memcpy(&stdio_uart, obj, sizeof(serial_t)); |
sahilmgandhi | 18:6a4db94011d3 | 188 | } |
sahilmgandhi | 18:6a4db94011d3 | 189 | } |
sahilmgandhi | 18:6a4db94011d3 | 190 | |
sahilmgandhi | 18:6a4db94011d3 | 191 | void serial_free(serial_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 192 | { |
sahilmgandhi | 18:6a4db94011d3 | 193 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 194 | |
sahilmgandhi | 18:6a4db94011d3 | 195 | // Reset UART and disable clock |
sahilmgandhi | 18:6a4db94011d3 | 196 | if (obj_s->uart == UART_1) { |
sahilmgandhi | 18:6a4db94011d3 | 197 | __USART1_FORCE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 198 | __USART1_RELEASE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 199 | __USART1_CLK_DISABLE(); |
sahilmgandhi | 18:6a4db94011d3 | 200 | } |
sahilmgandhi | 18:6a4db94011d3 | 201 | if (obj_s->uart == UART_2) { |
sahilmgandhi | 18:6a4db94011d3 | 202 | __USART2_FORCE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 203 | __USART2_RELEASE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 204 | __USART2_CLK_DISABLE(); |
sahilmgandhi | 18:6a4db94011d3 | 205 | } |
sahilmgandhi | 18:6a4db94011d3 | 206 | #if defined(USART3_BASE) |
sahilmgandhi | 18:6a4db94011d3 | 207 | if (obj_s->uart == UART_3) { |
sahilmgandhi | 18:6a4db94011d3 | 208 | __USART3_FORCE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 209 | __USART3_RELEASE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 210 | __USART3_CLK_DISABLE(); |
sahilmgandhi | 18:6a4db94011d3 | 211 | } |
sahilmgandhi | 18:6a4db94011d3 | 212 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 213 | #if defined(UART4_BASE) |
sahilmgandhi | 18:6a4db94011d3 | 214 | if (obj_s->uart == UART_4) { |
sahilmgandhi | 18:6a4db94011d3 | 215 | __UART4_FORCE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 216 | __UART4_RELEASE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 217 | __UART4_CLK_DISABLE(); |
sahilmgandhi | 18:6a4db94011d3 | 218 | } |
sahilmgandhi | 18:6a4db94011d3 | 219 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 220 | #if defined(UART5_BASE) |
sahilmgandhi | 18:6a4db94011d3 | 221 | if (obj_s->uart == UART_5) { |
sahilmgandhi | 18:6a4db94011d3 | 222 | __UART5_FORCE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 223 | __UART5_RELEASE_RESET(); |
sahilmgandhi | 18:6a4db94011d3 | 224 | __UART5_CLK_DISABLE(); |
sahilmgandhi | 18:6a4db94011d3 | 225 | } |
sahilmgandhi | 18:6a4db94011d3 | 226 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 227 | |
sahilmgandhi | 18:6a4db94011d3 | 228 | // Configure GPIOs |
sahilmgandhi | 18:6a4db94011d3 | 229 | pin_function(obj_s->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
sahilmgandhi | 18:6a4db94011d3 | 230 | pin_function(obj_s->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
sahilmgandhi | 18:6a4db94011d3 | 231 | |
sahilmgandhi | 18:6a4db94011d3 | 232 | serial_irq_ids[obj_s->index] = 0; |
sahilmgandhi | 18:6a4db94011d3 | 233 | } |
sahilmgandhi | 18:6a4db94011d3 | 234 | |
sahilmgandhi | 18:6a4db94011d3 | 235 | void serial_baud(serial_t *obj, int baudrate) |
sahilmgandhi | 18:6a4db94011d3 | 236 | { |
sahilmgandhi | 18:6a4db94011d3 | 237 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 238 | |
sahilmgandhi | 18:6a4db94011d3 | 239 | obj_s->baudrate = baudrate; |
sahilmgandhi | 18:6a4db94011d3 | 240 | init_uart(obj); |
sahilmgandhi | 18:6a4db94011d3 | 241 | } |
sahilmgandhi | 18:6a4db94011d3 | 242 | |
sahilmgandhi | 18:6a4db94011d3 | 243 | void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) |
sahilmgandhi | 18:6a4db94011d3 | 244 | { |
sahilmgandhi | 18:6a4db94011d3 | 245 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 246 | |
sahilmgandhi | 18:6a4db94011d3 | 247 | if (data_bits == 9) { |
sahilmgandhi | 18:6a4db94011d3 | 248 | obj_s->databits = UART_WORDLENGTH_9B; |
sahilmgandhi | 18:6a4db94011d3 | 249 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 250 | obj_s->databits = UART_WORDLENGTH_8B; |
sahilmgandhi | 18:6a4db94011d3 | 251 | } |
sahilmgandhi | 18:6a4db94011d3 | 252 | |
sahilmgandhi | 18:6a4db94011d3 | 253 | switch (parity) { |
sahilmgandhi | 18:6a4db94011d3 | 254 | case ParityOdd: |
sahilmgandhi | 18:6a4db94011d3 | 255 | obj_s->parity = UART_PARITY_ODD; |
sahilmgandhi | 18:6a4db94011d3 | 256 | break; |
sahilmgandhi | 18:6a4db94011d3 | 257 | case ParityEven: |
sahilmgandhi | 18:6a4db94011d3 | 258 | obj_s->parity = UART_PARITY_EVEN; |
sahilmgandhi | 18:6a4db94011d3 | 259 | break; |
sahilmgandhi | 18:6a4db94011d3 | 260 | default: // ParityNone |
sahilmgandhi | 18:6a4db94011d3 | 261 | case ParityForced0: // unsupported! |
sahilmgandhi | 18:6a4db94011d3 | 262 | case ParityForced1: // unsupported! |
sahilmgandhi | 18:6a4db94011d3 | 263 | obj_s->parity = UART_PARITY_NONE; |
sahilmgandhi | 18:6a4db94011d3 | 264 | break; |
sahilmgandhi | 18:6a4db94011d3 | 265 | } |
sahilmgandhi | 18:6a4db94011d3 | 266 | |
sahilmgandhi | 18:6a4db94011d3 | 267 | if (stop_bits == 2) { |
sahilmgandhi | 18:6a4db94011d3 | 268 | obj_s->stopbits = UART_STOPBITS_2; |
sahilmgandhi | 18:6a4db94011d3 | 269 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 270 | obj_s->stopbits = UART_STOPBITS_1; |
sahilmgandhi | 18:6a4db94011d3 | 271 | } |
sahilmgandhi | 18:6a4db94011d3 | 272 | |
sahilmgandhi | 18:6a4db94011d3 | 273 | init_uart(obj); |
sahilmgandhi | 18:6a4db94011d3 | 274 | } |
sahilmgandhi | 18:6a4db94011d3 | 275 | |
sahilmgandhi | 18:6a4db94011d3 | 276 | /****************************************************************************** |
sahilmgandhi | 18:6a4db94011d3 | 277 | * INTERRUPTS HANDLING |
sahilmgandhi | 18:6a4db94011d3 | 278 | ******************************************************************************/ |
sahilmgandhi | 18:6a4db94011d3 | 279 | |
sahilmgandhi | 18:6a4db94011d3 | 280 | static void uart_irq(int id) |
sahilmgandhi | 18:6a4db94011d3 | 281 | { |
sahilmgandhi | 18:6a4db94011d3 | 282 | UART_HandleTypeDef * huart = &uart_handlers[id]; |
sahilmgandhi | 18:6a4db94011d3 | 283 | |
sahilmgandhi | 18:6a4db94011d3 | 284 | if (serial_irq_ids[id] != 0) { |
sahilmgandhi | 18:6a4db94011d3 | 285 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) { |
sahilmgandhi | 18:6a4db94011d3 | 286 | if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC) != RESET) { |
sahilmgandhi | 18:6a4db94011d3 | 287 | irq_handler(serial_irq_ids[id], TxIrq); |
sahilmgandhi | 18:6a4db94011d3 | 288 | __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF); |
sahilmgandhi | 18:6a4db94011d3 | 289 | } |
sahilmgandhi | 18:6a4db94011d3 | 290 | } |
sahilmgandhi | 18:6a4db94011d3 | 291 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { |
sahilmgandhi | 18:6a4db94011d3 | 292 | if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) != RESET) { |
sahilmgandhi | 18:6a4db94011d3 | 293 | irq_handler(serial_irq_ids[id], RxIrq); |
sahilmgandhi | 18:6a4db94011d3 | 294 | volatile uint32_t tmpval = huart->Instance->RDR; // Clear RXNE flag |
sahilmgandhi | 18:6a4db94011d3 | 295 | UNUSED(tmpval); |
sahilmgandhi | 18:6a4db94011d3 | 296 | } |
sahilmgandhi | 18:6a4db94011d3 | 297 | } |
sahilmgandhi | 18:6a4db94011d3 | 298 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { |
sahilmgandhi | 18:6a4db94011d3 | 299 | if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_ORE) != RESET) { |
sahilmgandhi | 18:6a4db94011d3 | 300 | __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF); |
sahilmgandhi | 18:6a4db94011d3 | 301 | } |
sahilmgandhi | 18:6a4db94011d3 | 302 | } |
sahilmgandhi | 18:6a4db94011d3 | 303 | } |
sahilmgandhi | 18:6a4db94011d3 | 304 | } |
sahilmgandhi | 18:6a4db94011d3 | 305 | |
sahilmgandhi | 18:6a4db94011d3 | 306 | static void uart1_irq(void) |
sahilmgandhi | 18:6a4db94011d3 | 307 | { |
sahilmgandhi | 18:6a4db94011d3 | 308 | uart_irq(0); |
sahilmgandhi | 18:6a4db94011d3 | 309 | } |
sahilmgandhi | 18:6a4db94011d3 | 310 | |
sahilmgandhi | 18:6a4db94011d3 | 311 | static void uart2_irq(void) |
sahilmgandhi | 18:6a4db94011d3 | 312 | { |
sahilmgandhi | 18:6a4db94011d3 | 313 | uart_irq(1); |
sahilmgandhi | 18:6a4db94011d3 | 314 | } |
sahilmgandhi | 18:6a4db94011d3 | 315 | |
sahilmgandhi | 18:6a4db94011d3 | 316 | #if defined(USART3_BASE) |
sahilmgandhi | 18:6a4db94011d3 | 317 | static void uart3_irq(void) |
sahilmgandhi | 18:6a4db94011d3 | 318 | { |
sahilmgandhi | 18:6a4db94011d3 | 319 | uart_irq(2); |
sahilmgandhi | 18:6a4db94011d3 | 320 | } |
sahilmgandhi | 18:6a4db94011d3 | 321 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 322 | |
sahilmgandhi | 18:6a4db94011d3 | 323 | #if defined(UART4_BASE) |
sahilmgandhi | 18:6a4db94011d3 | 324 | static void uart4_irq(void) |
sahilmgandhi | 18:6a4db94011d3 | 325 | { |
sahilmgandhi | 18:6a4db94011d3 | 326 | uart_irq(3); |
sahilmgandhi | 18:6a4db94011d3 | 327 | } |
sahilmgandhi | 18:6a4db94011d3 | 328 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 329 | |
sahilmgandhi | 18:6a4db94011d3 | 330 | #if defined(UART5_BASE) |
sahilmgandhi | 18:6a4db94011d3 | 331 | static void uart5_irq(void) |
sahilmgandhi | 18:6a4db94011d3 | 332 | { |
sahilmgandhi | 18:6a4db94011d3 | 333 | uart_irq(4); |
sahilmgandhi | 18:6a4db94011d3 | 334 | } |
sahilmgandhi | 18:6a4db94011d3 | 335 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 336 | |
sahilmgandhi | 18:6a4db94011d3 | 337 | void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) |
sahilmgandhi | 18:6a4db94011d3 | 338 | { |
sahilmgandhi | 18:6a4db94011d3 | 339 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 340 | |
sahilmgandhi | 18:6a4db94011d3 | 341 | irq_handler = handler; |
sahilmgandhi | 18:6a4db94011d3 | 342 | serial_irq_ids[obj_s->index] = id; |
sahilmgandhi | 18:6a4db94011d3 | 343 | } |
sahilmgandhi | 18:6a4db94011d3 | 344 | |
sahilmgandhi | 18:6a4db94011d3 | 345 | void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) |
sahilmgandhi | 18:6a4db94011d3 | 346 | { |
sahilmgandhi | 18:6a4db94011d3 | 347 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 348 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
sahilmgandhi | 18:6a4db94011d3 | 349 | IRQn_Type irq_n = (IRQn_Type)0; |
sahilmgandhi | 18:6a4db94011d3 | 350 | uint32_t vector = 0; |
sahilmgandhi | 18:6a4db94011d3 | 351 | |
sahilmgandhi | 18:6a4db94011d3 | 352 | if (obj_s->uart == UART_1) { |
sahilmgandhi | 18:6a4db94011d3 | 353 | irq_n = USART1_IRQn; |
sahilmgandhi | 18:6a4db94011d3 | 354 | vector = (uint32_t)&uart1_irq; |
sahilmgandhi | 18:6a4db94011d3 | 355 | } |
sahilmgandhi | 18:6a4db94011d3 | 356 | |
sahilmgandhi | 18:6a4db94011d3 | 357 | if (obj_s->uart == UART_2) { |
sahilmgandhi | 18:6a4db94011d3 | 358 | irq_n = USART2_IRQn; |
sahilmgandhi | 18:6a4db94011d3 | 359 | vector = (uint32_t)&uart2_irq; |
sahilmgandhi | 18:6a4db94011d3 | 360 | } |
sahilmgandhi | 18:6a4db94011d3 | 361 | |
sahilmgandhi | 18:6a4db94011d3 | 362 | #if defined(USART3_BASE) |
sahilmgandhi | 18:6a4db94011d3 | 363 | if (obj_s->uart == UART_3) { |
sahilmgandhi | 18:6a4db94011d3 | 364 | irq_n = USART3_IRQn; |
sahilmgandhi | 18:6a4db94011d3 | 365 | vector = (uint32_t)&uart3_irq; |
sahilmgandhi | 18:6a4db94011d3 | 366 | } |
sahilmgandhi | 18:6a4db94011d3 | 367 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 368 | |
sahilmgandhi | 18:6a4db94011d3 | 369 | #if defined(UART4_BASE) |
sahilmgandhi | 18:6a4db94011d3 | 370 | if (obj_s->uart == UART_4) { |
sahilmgandhi | 18:6a4db94011d3 | 371 | irq_n = UART4_IRQn; |
sahilmgandhi | 18:6a4db94011d3 | 372 | vector = (uint32_t)&uart4_irq; |
sahilmgandhi | 18:6a4db94011d3 | 373 | } |
sahilmgandhi | 18:6a4db94011d3 | 374 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 375 | |
sahilmgandhi | 18:6a4db94011d3 | 376 | #if defined(UART5_BASE) |
sahilmgandhi | 18:6a4db94011d3 | 377 | if (obj_s->uart == UART_5) { |
sahilmgandhi | 18:6a4db94011d3 | 378 | irq_n = UART5_IRQn; |
sahilmgandhi | 18:6a4db94011d3 | 379 | vector = (uint32_t)&uart5_irq; |
sahilmgandhi | 18:6a4db94011d3 | 380 | } |
sahilmgandhi | 18:6a4db94011d3 | 381 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 382 | |
sahilmgandhi | 18:6a4db94011d3 | 383 | if (enable) { |
sahilmgandhi | 18:6a4db94011d3 | 384 | if (irq == RxIrq) { |
sahilmgandhi | 18:6a4db94011d3 | 385 | __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE); |
sahilmgandhi | 18:6a4db94011d3 | 386 | } else { // TxIrq |
sahilmgandhi | 18:6a4db94011d3 | 387 | __HAL_UART_ENABLE_IT(huart, UART_IT_TC); |
sahilmgandhi | 18:6a4db94011d3 | 388 | } |
sahilmgandhi | 18:6a4db94011d3 | 389 | NVIC_SetVector(irq_n, vector); |
sahilmgandhi | 18:6a4db94011d3 | 390 | NVIC_EnableIRQ(irq_n); |
sahilmgandhi | 18:6a4db94011d3 | 391 | |
sahilmgandhi | 18:6a4db94011d3 | 392 | } else { // disable |
sahilmgandhi | 18:6a4db94011d3 | 393 | int all_disabled = 0; |
sahilmgandhi | 18:6a4db94011d3 | 394 | if (irq == RxIrq) { |
sahilmgandhi | 18:6a4db94011d3 | 395 | __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE); |
sahilmgandhi | 18:6a4db94011d3 | 396 | // Check if TxIrq is disabled too |
sahilmgandhi | 18:6a4db94011d3 | 397 | if ((huart->Instance->CR1 & USART_CR1_TXEIE) == 0) { |
sahilmgandhi | 18:6a4db94011d3 | 398 | all_disabled = 1; |
sahilmgandhi | 18:6a4db94011d3 | 399 | } |
sahilmgandhi | 18:6a4db94011d3 | 400 | } else { // TxIrq |
sahilmgandhi | 18:6a4db94011d3 | 401 | __HAL_UART_DISABLE_IT(huart, UART_IT_TC); |
sahilmgandhi | 18:6a4db94011d3 | 402 | // Check if RxIrq is disabled too |
sahilmgandhi | 18:6a4db94011d3 | 403 | if ((huart->Instance->CR1 & USART_CR1_RXNEIE) == 0) { |
sahilmgandhi | 18:6a4db94011d3 | 404 | all_disabled = 1; |
sahilmgandhi | 18:6a4db94011d3 | 405 | } |
sahilmgandhi | 18:6a4db94011d3 | 406 | } |
sahilmgandhi | 18:6a4db94011d3 | 407 | |
sahilmgandhi | 18:6a4db94011d3 | 408 | if (all_disabled) { |
sahilmgandhi | 18:6a4db94011d3 | 409 | NVIC_DisableIRQ(irq_n); |
sahilmgandhi | 18:6a4db94011d3 | 410 | } |
sahilmgandhi | 18:6a4db94011d3 | 411 | } |
sahilmgandhi | 18:6a4db94011d3 | 412 | } |
sahilmgandhi | 18:6a4db94011d3 | 413 | |
sahilmgandhi | 18:6a4db94011d3 | 414 | /****************************************************************************** |
sahilmgandhi | 18:6a4db94011d3 | 415 | * READ/WRITE |
sahilmgandhi | 18:6a4db94011d3 | 416 | ******************************************************************************/ |
sahilmgandhi | 18:6a4db94011d3 | 417 | |
sahilmgandhi | 18:6a4db94011d3 | 418 | int serial_getc(serial_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 419 | { |
sahilmgandhi | 18:6a4db94011d3 | 420 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 421 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
sahilmgandhi | 18:6a4db94011d3 | 422 | |
sahilmgandhi | 18:6a4db94011d3 | 423 | while (!serial_readable(obj)); |
sahilmgandhi | 18:6a4db94011d3 | 424 | if (obj_s->databits == UART_WORDLENGTH_8B) { |
sahilmgandhi | 18:6a4db94011d3 | 425 | return (int)(huart->Instance->RDR & (uint8_t)0xFF); |
sahilmgandhi | 18:6a4db94011d3 | 426 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 427 | return (int)(huart->Instance->RDR & (uint16_t)0x1FF); |
sahilmgandhi | 18:6a4db94011d3 | 428 | } |
sahilmgandhi | 18:6a4db94011d3 | 429 | } |
sahilmgandhi | 18:6a4db94011d3 | 430 | |
sahilmgandhi | 18:6a4db94011d3 | 431 | void serial_putc(serial_t *obj, int c) |
sahilmgandhi | 18:6a4db94011d3 | 432 | { |
sahilmgandhi | 18:6a4db94011d3 | 433 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 434 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
sahilmgandhi | 18:6a4db94011d3 | 435 | |
sahilmgandhi | 18:6a4db94011d3 | 436 | while (!serial_writable(obj)); |
sahilmgandhi | 18:6a4db94011d3 | 437 | if (obj_s->databits == UART_WORDLENGTH_8B) { |
sahilmgandhi | 18:6a4db94011d3 | 438 | huart->Instance->TDR = (uint8_t)(c & (uint8_t)0xFF); |
sahilmgandhi | 18:6a4db94011d3 | 439 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 440 | huart->Instance->TDR = (uint16_t)(c & (uint16_t)0x1FF); |
sahilmgandhi | 18:6a4db94011d3 | 441 | } |
sahilmgandhi | 18:6a4db94011d3 | 442 | } |
sahilmgandhi | 18:6a4db94011d3 | 443 | |
sahilmgandhi | 18:6a4db94011d3 | 444 | int serial_readable(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 | // Check if data is received |
sahilmgandhi | 18:6a4db94011d3 | 450 | return (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) ? 1 : 0; |
sahilmgandhi | 18:6a4db94011d3 | 451 | } |
sahilmgandhi | 18:6a4db94011d3 | 452 | |
sahilmgandhi | 18:6a4db94011d3 | 453 | int serial_writable(serial_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 454 | { |
sahilmgandhi | 18:6a4db94011d3 | 455 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 456 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
sahilmgandhi | 18:6a4db94011d3 | 457 | |
sahilmgandhi | 18:6a4db94011d3 | 458 | // Check if data is transmitted |
sahilmgandhi | 18:6a4db94011d3 | 459 | return (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) ? 1 : 0; |
sahilmgandhi | 18:6a4db94011d3 | 460 | } |
sahilmgandhi | 18:6a4db94011d3 | 461 | |
sahilmgandhi | 18:6a4db94011d3 | 462 | void serial_clear(serial_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 463 | { |
sahilmgandhi | 18:6a4db94011d3 | 464 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 465 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
sahilmgandhi | 18:6a4db94011d3 | 466 | |
sahilmgandhi | 18:6a4db94011d3 | 467 | __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF); |
sahilmgandhi | 18:6a4db94011d3 | 468 | __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST); |
sahilmgandhi | 18:6a4db94011d3 | 469 | } |
sahilmgandhi | 18:6a4db94011d3 | 470 | |
sahilmgandhi | 18:6a4db94011d3 | 471 | void serial_pinout_tx(PinName tx) |
sahilmgandhi | 18:6a4db94011d3 | 472 | { |
sahilmgandhi | 18:6a4db94011d3 | 473 | pinmap_pinout(tx, PinMap_UART_TX); |
sahilmgandhi | 18:6a4db94011d3 | 474 | } |
sahilmgandhi | 18:6a4db94011d3 | 475 | |
sahilmgandhi | 18:6a4db94011d3 | 476 | void serial_break_set(serial_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 477 | { |
sahilmgandhi | 18:6a4db94011d3 | 478 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 479 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
sahilmgandhi | 18:6a4db94011d3 | 480 | |
sahilmgandhi | 18:6a4db94011d3 | 481 | HAL_LIN_SendBreak(huart); |
sahilmgandhi | 18:6a4db94011d3 | 482 | } |
sahilmgandhi | 18:6a4db94011d3 | 483 | |
sahilmgandhi | 18:6a4db94011d3 | 484 | void serial_break_clear(serial_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 485 | { |
sahilmgandhi | 18:6a4db94011d3 | 486 | (void)obj; |
sahilmgandhi | 18:6a4db94011d3 | 487 | } |
sahilmgandhi | 18:6a4db94011d3 | 488 | |
sahilmgandhi | 18:6a4db94011d3 | 489 | #if DEVICE_SERIAL_ASYNCH |
sahilmgandhi | 18:6a4db94011d3 | 490 | |
sahilmgandhi | 18:6a4db94011d3 | 491 | /****************************************************************************** |
sahilmgandhi | 18:6a4db94011d3 | 492 | * LOCAL HELPER FUNCTIONS |
sahilmgandhi | 18:6a4db94011d3 | 493 | ******************************************************************************/ |
sahilmgandhi | 18:6a4db94011d3 | 494 | |
sahilmgandhi | 18:6a4db94011d3 | 495 | /** |
sahilmgandhi | 18:6a4db94011d3 | 496 | * Configure the TX buffer for an asynchronous write serial transaction |
sahilmgandhi | 18:6a4db94011d3 | 497 | * |
sahilmgandhi | 18:6a4db94011d3 | 498 | * @param obj The serial object. |
sahilmgandhi | 18:6a4db94011d3 | 499 | * @param tx The buffer for sending. |
sahilmgandhi | 18:6a4db94011d3 | 500 | * @param tx_length The number of words to transmit. |
sahilmgandhi | 18:6a4db94011d3 | 501 | */ |
sahilmgandhi | 18:6a4db94011d3 | 502 | static void serial_tx_buffer_set(serial_t *obj, void *tx, int tx_length, uint8_t width) |
sahilmgandhi | 18:6a4db94011d3 | 503 | { |
sahilmgandhi | 18:6a4db94011d3 | 504 | (void)width; |
sahilmgandhi | 18:6a4db94011d3 | 505 | |
sahilmgandhi | 18:6a4db94011d3 | 506 | // Exit if a transmit is already on-going |
sahilmgandhi | 18:6a4db94011d3 | 507 | if (serial_tx_active(obj)) { |
sahilmgandhi | 18:6a4db94011d3 | 508 | return; |
sahilmgandhi | 18:6a4db94011d3 | 509 | } |
sahilmgandhi | 18:6a4db94011d3 | 510 | |
sahilmgandhi | 18:6a4db94011d3 | 511 | obj->tx_buff.buffer = tx; |
sahilmgandhi | 18:6a4db94011d3 | 512 | obj->tx_buff.length = tx_length; |
sahilmgandhi | 18:6a4db94011d3 | 513 | obj->tx_buff.pos = 0; |
sahilmgandhi | 18:6a4db94011d3 | 514 | } |
sahilmgandhi | 18:6a4db94011d3 | 515 | |
sahilmgandhi | 18:6a4db94011d3 | 516 | /** |
sahilmgandhi | 18:6a4db94011d3 | 517 | * Configure the RX buffer for an asynchronous write serial transaction |
sahilmgandhi | 18:6a4db94011d3 | 518 | * |
sahilmgandhi | 18:6a4db94011d3 | 519 | * @param obj The serial object. |
sahilmgandhi | 18:6a4db94011d3 | 520 | * @param tx The buffer for sending. |
sahilmgandhi | 18:6a4db94011d3 | 521 | * @param tx_length The number of words to transmit. |
sahilmgandhi | 18:6a4db94011d3 | 522 | */ |
sahilmgandhi | 18:6a4db94011d3 | 523 | static void serial_rx_buffer_set(serial_t *obj, void *rx, int rx_length, uint8_t width) |
sahilmgandhi | 18:6a4db94011d3 | 524 | { |
sahilmgandhi | 18:6a4db94011d3 | 525 | (void)width; |
sahilmgandhi | 18:6a4db94011d3 | 526 | |
sahilmgandhi | 18:6a4db94011d3 | 527 | // Exit if a reception is already on-going |
sahilmgandhi | 18:6a4db94011d3 | 528 | if (serial_rx_active(obj)) { |
sahilmgandhi | 18:6a4db94011d3 | 529 | return; |
sahilmgandhi | 18:6a4db94011d3 | 530 | } |
sahilmgandhi | 18:6a4db94011d3 | 531 | |
sahilmgandhi | 18:6a4db94011d3 | 532 | obj->rx_buff.buffer = rx; |
sahilmgandhi | 18:6a4db94011d3 | 533 | obj->rx_buff.length = rx_length; |
sahilmgandhi | 18:6a4db94011d3 | 534 | obj->rx_buff.pos = 0; |
sahilmgandhi | 18:6a4db94011d3 | 535 | } |
sahilmgandhi | 18:6a4db94011d3 | 536 | |
sahilmgandhi | 18:6a4db94011d3 | 537 | /** |
sahilmgandhi | 18:6a4db94011d3 | 538 | * Configure events |
sahilmgandhi | 18:6a4db94011d3 | 539 | * |
sahilmgandhi | 18:6a4db94011d3 | 540 | * @param obj The serial object |
sahilmgandhi | 18:6a4db94011d3 | 541 | * @param event The logical OR of the events to configure |
sahilmgandhi | 18:6a4db94011d3 | 542 | * @param enable Set to non-zero to enable events, or zero to disable them |
sahilmgandhi | 18:6a4db94011d3 | 543 | */ |
sahilmgandhi | 18:6a4db94011d3 | 544 | static void serial_enable_event(serial_t *obj, int event, uint8_t enable) |
sahilmgandhi | 18:6a4db94011d3 | 545 | { |
sahilmgandhi | 18:6a4db94011d3 | 546 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 547 | |
sahilmgandhi | 18:6a4db94011d3 | 548 | // Shouldn't have to enable interrupt here, just need to keep track of the requested events. |
sahilmgandhi | 18:6a4db94011d3 | 549 | if (enable) { |
sahilmgandhi | 18:6a4db94011d3 | 550 | obj_s->events |= event; |
sahilmgandhi | 18:6a4db94011d3 | 551 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 552 | obj_s->events &= ~event; |
sahilmgandhi | 18:6a4db94011d3 | 553 | } |
sahilmgandhi | 18:6a4db94011d3 | 554 | } |
sahilmgandhi | 18:6a4db94011d3 | 555 | |
sahilmgandhi | 18:6a4db94011d3 | 556 | |
sahilmgandhi | 18:6a4db94011d3 | 557 | /** |
sahilmgandhi | 18:6a4db94011d3 | 558 | * Get index of serial object TX IRQ, relating it to the physical peripheral. |
sahilmgandhi | 18:6a4db94011d3 | 559 | * |
sahilmgandhi | 18:6a4db94011d3 | 560 | * @param obj pointer to serial object |
sahilmgandhi | 18:6a4db94011d3 | 561 | * @return internal NVIC TX IRQ index of U(S)ART peripheral |
sahilmgandhi | 18:6a4db94011d3 | 562 | */ |
sahilmgandhi | 18:6a4db94011d3 | 563 | static IRQn_Type serial_get_irq_n(serial_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 564 | { |
sahilmgandhi | 18:6a4db94011d3 | 565 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 566 | IRQn_Type irq_n; |
sahilmgandhi | 18:6a4db94011d3 | 567 | |
sahilmgandhi | 18:6a4db94011d3 | 568 | switch (obj_s->index) { |
sahilmgandhi | 18:6a4db94011d3 | 569 | case 0: |
sahilmgandhi | 18:6a4db94011d3 | 570 | irq_n = USART1_IRQn; |
sahilmgandhi | 18:6a4db94011d3 | 571 | break; |
sahilmgandhi | 18:6a4db94011d3 | 572 | |
sahilmgandhi | 18:6a4db94011d3 | 573 | case 1: |
sahilmgandhi | 18:6a4db94011d3 | 574 | irq_n = USART2_IRQn; |
sahilmgandhi | 18:6a4db94011d3 | 575 | break; |
sahilmgandhi | 18:6a4db94011d3 | 576 | |
sahilmgandhi | 18:6a4db94011d3 | 577 | #if defined(USART3_BASE) |
sahilmgandhi | 18:6a4db94011d3 | 578 | case 2: |
sahilmgandhi | 18:6a4db94011d3 | 579 | irq_n = USART3_IRQn; |
sahilmgandhi | 18:6a4db94011d3 | 580 | break; |
sahilmgandhi | 18:6a4db94011d3 | 581 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 582 | #if defined(UART4_BASE) |
sahilmgandhi | 18:6a4db94011d3 | 583 | case 3: |
sahilmgandhi | 18:6a4db94011d3 | 584 | irq_n = UART4_IRQn; |
sahilmgandhi | 18:6a4db94011d3 | 585 | break; |
sahilmgandhi | 18:6a4db94011d3 | 586 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 587 | #if defined(UART5_BASE) |
sahilmgandhi | 18:6a4db94011d3 | 588 | case 4: |
sahilmgandhi | 18:6a4db94011d3 | 589 | irq_n = UART5_IRQn; |
sahilmgandhi | 18:6a4db94011d3 | 590 | break; |
sahilmgandhi | 18:6a4db94011d3 | 591 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 592 | default: |
sahilmgandhi | 18:6a4db94011d3 | 593 | irq_n = (IRQn_Type)0; |
sahilmgandhi | 18:6a4db94011d3 | 594 | } |
sahilmgandhi | 18:6a4db94011d3 | 595 | |
sahilmgandhi | 18:6a4db94011d3 | 596 | return irq_n; |
sahilmgandhi | 18:6a4db94011d3 | 597 | } |
sahilmgandhi | 18:6a4db94011d3 | 598 | |
sahilmgandhi | 18:6a4db94011d3 | 599 | |
sahilmgandhi | 18:6a4db94011d3 | 600 | /****************************************************************************** |
sahilmgandhi | 18:6a4db94011d3 | 601 | * MBED API FUNCTIONS |
sahilmgandhi | 18:6a4db94011d3 | 602 | ******************************************************************************/ |
sahilmgandhi | 18:6a4db94011d3 | 603 | |
sahilmgandhi | 18:6a4db94011d3 | 604 | /** |
sahilmgandhi | 18:6a4db94011d3 | 605 | * Begin asynchronous TX transfer. The used buffer is specified in the serial |
sahilmgandhi | 18:6a4db94011d3 | 606 | * object, tx_buff |
sahilmgandhi | 18:6a4db94011d3 | 607 | * |
sahilmgandhi | 18:6a4db94011d3 | 608 | * @param obj The serial object |
sahilmgandhi | 18:6a4db94011d3 | 609 | * @param tx The buffer for sending |
sahilmgandhi | 18:6a4db94011d3 | 610 | * @param tx_length The number of words to transmit |
sahilmgandhi | 18:6a4db94011d3 | 611 | * @param tx_width The bit width of buffer word |
sahilmgandhi | 18:6a4db94011d3 | 612 | * @param handler The serial handler |
sahilmgandhi | 18:6a4db94011d3 | 613 | * @param event The logical OR of events to be registered |
sahilmgandhi | 18:6a4db94011d3 | 614 | * @param hint A suggestion for how to use DMA with this transfer |
sahilmgandhi | 18:6a4db94011d3 | 615 | * @return Returns number of data transfered, or 0 otherwise |
sahilmgandhi | 18:6a4db94011d3 | 616 | */ |
sahilmgandhi | 18:6a4db94011d3 | 617 | 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 | 618 | { |
sahilmgandhi | 18:6a4db94011d3 | 619 | // TODO: DMA usage is currently ignored |
sahilmgandhi | 18:6a4db94011d3 | 620 | (void) hint; |
sahilmgandhi | 18:6a4db94011d3 | 621 | |
sahilmgandhi | 18:6a4db94011d3 | 622 | // Check buffer is ok |
sahilmgandhi | 18:6a4db94011d3 | 623 | MBED_ASSERT(tx != (void*)0); |
sahilmgandhi | 18:6a4db94011d3 | 624 | MBED_ASSERT(tx_width == 8); // support only 8b width |
sahilmgandhi | 18:6a4db94011d3 | 625 | |
sahilmgandhi | 18:6a4db94011d3 | 626 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 627 | UART_HandleTypeDef * huart = &uart_handlers[obj_s->index]; |
sahilmgandhi | 18:6a4db94011d3 | 628 | |
sahilmgandhi | 18:6a4db94011d3 | 629 | if (tx_length == 0) { |
sahilmgandhi | 18:6a4db94011d3 | 630 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 631 | } |
sahilmgandhi | 18:6a4db94011d3 | 632 | |
sahilmgandhi | 18:6a4db94011d3 | 633 | // Set up buffer |
sahilmgandhi | 18:6a4db94011d3 | 634 | serial_tx_buffer_set(obj, (void *)tx, tx_length, tx_width); |
sahilmgandhi | 18:6a4db94011d3 | 635 | |
sahilmgandhi | 18:6a4db94011d3 | 636 | // Set up events |
sahilmgandhi | 18:6a4db94011d3 | 637 | serial_enable_event(obj, SERIAL_EVENT_TX_ALL, 0); // Clear all events |
sahilmgandhi | 18:6a4db94011d3 | 638 | serial_enable_event(obj, event, 1); // Set only the wanted events |
sahilmgandhi | 18:6a4db94011d3 | 639 | |
sahilmgandhi | 18:6a4db94011d3 | 640 | // Enable interrupt |
sahilmgandhi | 18:6a4db94011d3 | 641 | IRQn_Type irq_n = serial_get_irq_n(obj); |
sahilmgandhi | 18:6a4db94011d3 | 642 | NVIC_ClearPendingIRQ(irq_n); |
sahilmgandhi | 18:6a4db94011d3 | 643 | NVIC_DisableIRQ(irq_n); |
sahilmgandhi | 18:6a4db94011d3 | 644 | NVIC_SetPriority(irq_n, 1); |
sahilmgandhi | 18:6a4db94011d3 | 645 | NVIC_SetVector(irq_n, (uint32_t)handler); |
sahilmgandhi | 18:6a4db94011d3 | 646 | NVIC_EnableIRQ(irq_n); |
sahilmgandhi | 18:6a4db94011d3 | 647 | |
sahilmgandhi | 18:6a4db94011d3 | 648 | // the following function will enable UART_IT_TXE and error interrupts |
sahilmgandhi | 18:6a4db94011d3 | 649 | if (HAL_UART_Transmit_IT(huart, (uint8_t*)tx, tx_length) != HAL_OK) { |
sahilmgandhi | 18:6a4db94011d3 | 650 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 651 | } |
sahilmgandhi | 18:6a4db94011d3 | 652 | |
sahilmgandhi | 18:6a4db94011d3 | 653 | return tx_length; |
sahilmgandhi | 18:6a4db94011d3 | 654 | } |
sahilmgandhi | 18:6a4db94011d3 | 655 | |
sahilmgandhi | 18:6a4db94011d3 | 656 | /** |
sahilmgandhi | 18:6a4db94011d3 | 657 | * Begin asynchronous RX transfer (enable interrupt for data collecting) |
sahilmgandhi | 18:6a4db94011d3 | 658 | * The used buffer is specified in the serial object, rx_buff |
sahilmgandhi | 18:6a4db94011d3 | 659 | * |
sahilmgandhi | 18:6a4db94011d3 | 660 | * @param obj The serial object |
sahilmgandhi | 18:6a4db94011d3 | 661 | * @param rx The buffer for sending |
sahilmgandhi | 18:6a4db94011d3 | 662 | * @param rx_length The number of words to transmit |
sahilmgandhi | 18:6a4db94011d3 | 663 | * @param rx_width The bit width of buffer word |
sahilmgandhi | 18:6a4db94011d3 | 664 | * @param handler The serial handler |
sahilmgandhi | 18:6a4db94011d3 | 665 | * @param event The logical OR of events to be registered |
sahilmgandhi | 18:6a4db94011d3 | 666 | * @param handler The serial handler |
sahilmgandhi | 18:6a4db94011d3 | 667 | * @param char_match A character in range 0-254 to be matched |
sahilmgandhi | 18:6a4db94011d3 | 668 | * @param hint A suggestion for how to use DMA with this transfer |
sahilmgandhi | 18:6a4db94011d3 | 669 | */ |
sahilmgandhi | 18:6a4db94011d3 | 670 | 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 | 671 | { |
sahilmgandhi | 18:6a4db94011d3 | 672 | // TODO: DMA usage is currently ignored |
sahilmgandhi | 18:6a4db94011d3 | 673 | (void) hint; |
sahilmgandhi | 18:6a4db94011d3 | 674 | |
sahilmgandhi | 18:6a4db94011d3 | 675 | /* Sanity check arguments */ |
sahilmgandhi | 18:6a4db94011d3 | 676 | MBED_ASSERT(obj); |
sahilmgandhi | 18:6a4db94011d3 | 677 | MBED_ASSERT(rx != (void*)0); |
sahilmgandhi | 18:6a4db94011d3 | 678 | MBED_ASSERT(rx_width == 8); // support only 8b width |
sahilmgandhi | 18:6a4db94011d3 | 679 | |
sahilmgandhi | 18:6a4db94011d3 | 680 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 681 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
sahilmgandhi | 18:6a4db94011d3 | 682 | |
sahilmgandhi | 18:6a4db94011d3 | 683 | serial_enable_event(obj, SERIAL_EVENT_RX_ALL, 0); |
sahilmgandhi | 18:6a4db94011d3 | 684 | serial_enable_event(obj, event, 1); |
sahilmgandhi | 18:6a4db94011d3 | 685 | |
sahilmgandhi | 18:6a4db94011d3 | 686 | // set CharMatch |
sahilmgandhi | 18:6a4db94011d3 | 687 | obj->char_match = char_match; |
sahilmgandhi | 18:6a4db94011d3 | 688 | |
sahilmgandhi | 18:6a4db94011d3 | 689 | serial_rx_buffer_set(obj, rx, rx_length, rx_width); |
sahilmgandhi | 18:6a4db94011d3 | 690 | |
sahilmgandhi | 18:6a4db94011d3 | 691 | IRQn_Type irq_n = serial_get_irq_n(obj); |
sahilmgandhi | 18:6a4db94011d3 | 692 | NVIC_ClearPendingIRQ(irq_n); |
sahilmgandhi | 18:6a4db94011d3 | 693 | NVIC_DisableIRQ(irq_n); |
sahilmgandhi | 18:6a4db94011d3 | 694 | NVIC_SetPriority(irq_n, 0); |
sahilmgandhi | 18:6a4db94011d3 | 695 | NVIC_SetVector(irq_n, (uint32_t)handler); |
sahilmgandhi | 18:6a4db94011d3 | 696 | NVIC_EnableIRQ(irq_n); |
sahilmgandhi | 18:6a4db94011d3 | 697 | |
sahilmgandhi | 18:6a4db94011d3 | 698 | // following HAL function will enable the RXNE interrupt + error interrupts |
sahilmgandhi | 18:6a4db94011d3 | 699 | HAL_UART_Receive_IT(huart, (uint8_t*)rx, rx_length); |
sahilmgandhi | 18:6a4db94011d3 | 700 | } |
sahilmgandhi | 18:6a4db94011d3 | 701 | |
sahilmgandhi | 18:6a4db94011d3 | 702 | /** |
sahilmgandhi | 18:6a4db94011d3 | 703 | * Attempts to determine if the serial peripheral is already in use for TX |
sahilmgandhi | 18:6a4db94011d3 | 704 | * |
sahilmgandhi | 18:6a4db94011d3 | 705 | * @param obj The serial object |
sahilmgandhi | 18:6a4db94011d3 | 706 | * @return Non-zero if the TX transaction is ongoing, 0 otherwise |
sahilmgandhi | 18:6a4db94011d3 | 707 | */ |
sahilmgandhi | 18:6a4db94011d3 | 708 | uint8_t serial_tx_active(serial_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 709 | { |
sahilmgandhi | 18:6a4db94011d3 | 710 | MBED_ASSERT(obj); |
sahilmgandhi | 18:6a4db94011d3 | 711 | |
sahilmgandhi | 18:6a4db94011d3 | 712 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 713 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
sahilmgandhi | 18:6a4db94011d3 | 714 | |
sahilmgandhi | 18:6a4db94011d3 | 715 | return ((HAL_UART_GetState(huart) == HAL_UART_STATE_BUSY_TX) ? 1 : 0); |
sahilmgandhi | 18:6a4db94011d3 | 716 | } |
sahilmgandhi | 18:6a4db94011d3 | 717 | |
sahilmgandhi | 18:6a4db94011d3 | 718 | /** |
sahilmgandhi | 18:6a4db94011d3 | 719 | * Attempts to determine if the serial peripheral is already in use for RX |
sahilmgandhi | 18:6a4db94011d3 | 720 | * |
sahilmgandhi | 18:6a4db94011d3 | 721 | * @param obj The serial object |
sahilmgandhi | 18:6a4db94011d3 | 722 | * @return Non-zero if the RX transaction is ongoing, 0 otherwise |
sahilmgandhi | 18:6a4db94011d3 | 723 | */ |
sahilmgandhi | 18:6a4db94011d3 | 724 | uint8_t serial_rx_active(serial_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 725 | { |
sahilmgandhi | 18:6a4db94011d3 | 726 | MBED_ASSERT(obj); |
sahilmgandhi | 18:6a4db94011d3 | 727 | |
sahilmgandhi | 18:6a4db94011d3 | 728 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 729 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
sahilmgandhi | 18:6a4db94011d3 | 730 | |
sahilmgandhi | 18:6a4db94011d3 | 731 | return ((HAL_UART_GetState(huart) == HAL_UART_STATE_BUSY_RX) ? 1 : 0); |
sahilmgandhi | 18:6a4db94011d3 | 732 | } |
sahilmgandhi | 18:6a4db94011d3 | 733 | |
sahilmgandhi | 18:6a4db94011d3 | 734 | void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) { |
sahilmgandhi | 18:6a4db94011d3 | 735 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) { |
sahilmgandhi | 18:6a4db94011d3 | 736 | __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF); |
sahilmgandhi | 18:6a4db94011d3 | 737 | } |
sahilmgandhi | 18:6a4db94011d3 | 738 | } |
sahilmgandhi | 18:6a4db94011d3 | 739 | |
sahilmgandhi | 18:6a4db94011d3 | 740 | void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) { |
sahilmgandhi | 18:6a4db94011d3 | 741 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) { |
sahilmgandhi | 18:6a4db94011d3 | 742 | __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF); |
sahilmgandhi | 18:6a4db94011d3 | 743 | } |
sahilmgandhi | 18:6a4db94011d3 | 744 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) { |
sahilmgandhi | 18:6a4db94011d3 | 745 | __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF); |
sahilmgandhi | 18:6a4db94011d3 | 746 | } |
sahilmgandhi | 18:6a4db94011d3 | 747 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_NE) != RESET) { |
sahilmgandhi | 18:6a4db94011d3 | 748 | __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF); |
sahilmgandhi | 18:6a4db94011d3 | 749 | } |
sahilmgandhi | 18:6a4db94011d3 | 750 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { |
sahilmgandhi | 18:6a4db94011d3 | 751 | __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF); |
sahilmgandhi | 18:6a4db94011d3 | 752 | } |
sahilmgandhi | 18:6a4db94011d3 | 753 | } |
sahilmgandhi | 18:6a4db94011d3 | 754 | |
sahilmgandhi | 18:6a4db94011d3 | 755 | /** |
sahilmgandhi | 18:6a4db94011d3 | 756 | * The asynchronous TX and RX handler. |
sahilmgandhi | 18:6a4db94011d3 | 757 | * |
sahilmgandhi | 18:6a4db94011d3 | 758 | * @param obj The serial object |
sahilmgandhi | 18:6a4db94011d3 | 759 | * @return Returns event flags if a TX/RX transfer termination condition was met or 0 otherwise |
sahilmgandhi | 18:6a4db94011d3 | 760 | */ |
sahilmgandhi | 18:6a4db94011d3 | 761 | int serial_irq_handler_asynch(serial_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 762 | { |
sahilmgandhi | 18:6a4db94011d3 | 763 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 764 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
sahilmgandhi | 18:6a4db94011d3 | 765 | |
sahilmgandhi | 18:6a4db94011d3 | 766 | volatile int return_event = 0; |
sahilmgandhi | 18:6a4db94011d3 | 767 | uint8_t *buf = (uint8_t*)(obj->rx_buff.buffer); |
sahilmgandhi | 18:6a4db94011d3 | 768 | uint8_t i = 0; |
sahilmgandhi | 18:6a4db94011d3 | 769 | |
sahilmgandhi | 18:6a4db94011d3 | 770 | // TX PART: |
sahilmgandhi | 18:6a4db94011d3 | 771 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TC) != RESET) { |
sahilmgandhi | 18:6a4db94011d3 | 772 | if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC) != RESET) { |
sahilmgandhi | 18:6a4db94011d3 | 773 | // Return event SERIAL_EVENT_TX_COMPLETE if requested |
sahilmgandhi | 18:6a4db94011d3 | 774 | if ((obj_s->events & SERIAL_EVENT_TX_COMPLETE ) != 0) { |
sahilmgandhi | 18:6a4db94011d3 | 775 | return_event |= (SERIAL_EVENT_TX_COMPLETE & obj_s->events); |
sahilmgandhi | 18:6a4db94011d3 | 776 | } |
sahilmgandhi | 18:6a4db94011d3 | 777 | } |
sahilmgandhi | 18:6a4db94011d3 | 778 | } |
sahilmgandhi | 18:6a4db94011d3 | 779 | |
sahilmgandhi | 18:6a4db94011d3 | 780 | // Handle error events |
sahilmgandhi | 18:6a4db94011d3 | 781 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) { |
sahilmgandhi | 18:6a4db94011d3 | 782 | if (__HAL_UART_GET_IT_SOURCE(huart, USART_IT_ERR) != RESET) { |
sahilmgandhi | 18:6a4db94011d3 | 783 | return_event |= (SERIAL_EVENT_RX_PARITY_ERROR & obj_s->events); |
sahilmgandhi | 18:6a4db94011d3 | 784 | } |
sahilmgandhi | 18:6a4db94011d3 | 785 | } |
sahilmgandhi | 18:6a4db94011d3 | 786 | |
sahilmgandhi | 18:6a4db94011d3 | 787 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) { |
sahilmgandhi | 18:6a4db94011d3 | 788 | if (__HAL_UART_GET_IT_SOURCE(huart, USART_IT_ERR) != RESET) { |
sahilmgandhi | 18:6a4db94011d3 | 789 | return_event |= (SERIAL_EVENT_RX_FRAMING_ERROR & obj_s->events); |
sahilmgandhi | 18:6a4db94011d3 | 790 | } |
sahilmgandhi | 18:6a4db94011d3 | 791 | } |
sahilmgandhi | 18:6a4db94011d3 | 792 | |
sahilmgandhi | 18:6a4db94011d3 | 793 | if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { |
sahilmgandhi | 18:6a4db94011d3 | 794 | if (__HAL_UART_GET_IT_SOURCE(huart, USART_IT_ERR) != RESET) { |
sahilmgandhi | 18:6a4db94011d3 | 795 | return_event |= (SERIAL_EVENT_RX_OVERRUN_ERROR & obj_s->events); |
sahilmgandhi | 18:6a4db94011d3 | 796 | } |
sahilmgandhi | 18:6a4db94011d3 | 797 | } |
sahilmgandhi | 18:6a4db94011d3 | 798 | |
sahilmgandhi | 18:6a4db94011d3 | 799 | HAL_UART_IRQHandler(huart); |
sahilmgandhi | 18:6a4db94011d3 | 800 | |
sahilmgandhi | 18:6a4db94011d3 | 801 | // Abort if an error occurs |
sahilmgandhi | 18:6a4db94011d3 | 802 | if (return_event & SERIAL_EVENT_RX_PARITY_ERROR || |
sahilmgandhi | 18:6a4db94011d3 | 803 | return_event & SERIAL_EVENT_RX_FRAMING_ERROR || |
sahilmgandhi | 18:6a4db94011d3 | 804 | return_event & SERIAL_EVENT_RX_OVERRUN_ERROR) { |
sahilmgandhi | 18:6a4db94011d3 | 805 | return return_event; |
sahilmgandhi | 18:6a4db94011d3 | 806 | } |
sahilmgandhi | 18:6a4db94011d3 | 807 | |
sahilmgandhi | 18:6a4db94011d3 | 808 | //RX PART |
sahilmgandhi | 18:6a4db94011d3 | 809 | if (huart->RxXferSize != 0) { |
sahilmgandhi | 18:6a4db94011d3 | 810 | obj->rx_buff.pos = huart->RxXferSize - huart->RxXferCount; |
sahilmgandhi | 18:6a4db94011d3 | 811 | } |
sahilmgandhi | 18:6a4db94011d3 | 812 | if ((huart->RxXferCount == 0) && (obj->rx_buff.pos >= (obj->rx_buff.length - 1))) { |
sahilmgandhi | 18:6a4db94011d3 | 813 | return_event |= (SERIAL_EVENT_RX_COMPLETE & obj_s->events); |
sahilmgandhi | 18:6a4db94011d3 | 814 | } |
sahilmgandhi | 18:6a4db94011d3 | 815 | |
sahilmgandhi | 18:6a4db94011d3 | 816 | // Check if char_match is present |
sahilmgandhi | 18:6a4db94011d3 | 817 | if (obj_s->events & SERIAL_EVENT_RX_CHARACTER_MATCH) { |
sahilmgandhi | 18:6a4db94011d3 | 818 | if (buf != NULL) { |
sahilmgandhi | 18:6a4db94011d3 | 819 | for (i = 0; i < obj->rx_buff.pos; i++) { |
sahilmgandhi | 18:6a4db94011d3 | 820 | if (buf[i] == obj->char_match) { |
sahilmgandhi | 18:6a4db94011d3 | 821 | obj->rx_buff.pos = i; |
sahilmgandhi | 18:6a4db94011d3 | 822 | return_event |= (SERIAL_EVENT_RX_CHARACTER_MATCH & obj_s->events); |
sahilmgandhi | 18:6a4db94011d3 | 823 | serial_rx_abort_asynch(obj); |
sahilmgandhi | 18:6a4db94011d3 | 824 | break; |
sahilmgandhi | 18:6a4db94011d3 | 825 | } |
sahilmgandhi | 18:6a4db94011d3 | 826 | } |
sahilmgandhi | 18:6a4db94011d3 | 827 | } |
sahilmgandhi | 18:6a4db94011d3 | 828 | } |
sahilmgandhi | 18:6a4db94011d3 | 829 | |
sahilmgandhi | 18:6a4db94011d3 | 830 | return return_event; |
sahilmgandhi | 18:6a4db94011d3 | 831 | } |
sahilmgandhi | 18:6a4db94011d3 | 832 | |
sahilmgandhi | 18:6a4db94011d3 | 833 | /** |
sahilmgandhi | 18:6a4db94011d3 | 834 | * Abort the ongoing TX transaction. It disables the enabled interupt for TX and |
sahilmgandhi | 18:6a4db94011d3 | 835 | * flush TX hardware buffer if TX FIFO is used |
sahilmgandhi | 18:6a4db94011d3 | 836 | * |
sahilmgandhi | 18:6a4db94011d3 | 837 | * @param obj The serial object |
sahilmgandhi | 18:6a4db94011d3 | 838 | */ |
sahilmgandhi | 18:6a4db94011d3 | 839 | void serial_tx_abort_asynch(serial_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 840 | { |
sahilmgandhi | 18:6a4db94011d3 | 841 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 842 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
sahilmgandhi | 18:6a4db94011d3 | 843 | |
sahilmgandhi | 18:6a4db94011d3 | 844 | __HAL_UART_DISABLE_IT(huart, UART_IT_TC); |
sahilmgandhi | 18:6a4db94011d3 | 845 | __HAL_UART_DISABLE_IT(huart, UART_IT_TXE); |
sahilmgandhi | 18:6a4db94011d3 | 846 | |
sahilmgandhi | 18:6a4db94011d3 | 847 | // clear flags |
sahilmgandhi | 18:6a4db94011d3 | 848 | __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF); |
sahilmgandhi | 18:6a4db94011d3 | 849 | |
sahilmgandhi | 18:6a4db94011d3 | 850 | // reset states |
sahilmgandhi | 18:6a4db94011d3 | 851 | huart->TxXferCount = 0; |
sahilmgandhi | 18:6a4db94011d3 | 852 | // update handle state |
sahilmgandhi | 18:6a4db94011d3 | 853 | if(huart->gState == HAL_UART_STATE_BUSY_TX_RX) { |
sahilmgandhi | 18:6a4db94011d3 | 854 | huart->gState = HAL_UART_STATE_BUSY_RX; |
sahilmgandhi | 18:6a4db94011d3 | 855 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 856 | huart->gState = HAL_UART_STATE_READY; |
sahilmgandhi | 18:6a4db94011d3 | 857 | } |
sahilmgandhi | 18:6a4db94011d3 | 858 | } |
sahilmgandhi | 18:6a4db94011d3 | 859 | |
sahilmgandhi | 18:6a4db94011d3 | 860 | /** |
sahilmgandhi | 18:6a4db94011d3 | 861 | * Abort the ongoing RX transaction It disables the enabled interrupt for RX and |
sahilmgandhi | 18:6a4db94011d3 | 862 | * flush RX hardware buffer if RX FIFO is used |
sahilmgandhi | 18:6a4db94011d3 | 863 | * |
sahilmgandhi | 18:6a4db94011d3 | 864 | * @param obj The serial object |
sahilmgandhi | 18:6a4db94011d3 | 865 | */ |
sahilmgandhi | 18:6a4db94011d3 | 866 | void serial_rx_abort_asynch(serial_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 867 | { |
sahilmgandhi | 18:6a4db94011d3 | 868 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 869 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
sahilmgandhi | 18:6a4db94011d3 | 870 | |
sahilmgandhi | 18:6a4db94011d3 | 871 | // disable interrupts |
sahilmgandhi | 18:6a4db94011d3 | 872 | __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE); |
sahilmgandhi | 18:6a4db94011d3 | 873 | __HAL_UART_DISABLE_IT(huart, UART_IT_PE); |
sahilmgandhi | 18:6a4db94011d3 | 874 | __HAL_UART_DISABLE_IT(huart, UART_IT_ERR); |
sahilmgandhi | 18:6a4db94011d3 | 875 | |
sahilmgandhi | 18:6a4db94011d3 | 876 | // clear flags |
sahilmgandhi | 18:6a4db94011d3 | 877 | __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF | UART_CLEAR_FEF | UART_CLEAR_OREF); |
sahilmgandhi | 18:6a4db94011d3 | 878 | volatile uint32_t tmpval = huart->Instance->RDR; // Clear RXNE flag |
sahilmgandhi | 18:6a4db94011d3 | 879 | UNUSED(tmpval); |
sahilmgandhi | 18:6a4db94011d3 | 880 | |
sahilmgandhi | 18:6a4db94011d3 | 881 | // reset states |
sahilmgandhi | 18:6a4db94011d3 | 882 | huart->RxXferCount = 0; |
sahilmgandhi | 18:6a4db94011d3 | 883 | // update handle state |
sahilmgandhi | 18:6a4db94011d3 | 884 | if(huart->RxState == HAL_UART_STATE_BUSY_TX_RX) { |
sahilmgandhi | 18:6a4db94011d3 | 885 | huart->RxState = HAL_UART_STATE_BUSY_TX; |
sahilmgandhi | 18:6a4db94011d3 | 886 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 887 | huart->RxState = HAL_UART_STATE_READY; |
sahilmgandhi | 18:6a4db94011d3 | 888 | } |
sahilmgandhi | 18:6a4db94011d3 | 889 | } |
sahilmgandhi | 18:6a4db94011d3 | 890 | |
sahilmgandhi | 18:6a4db94011d3 | 891 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 892 | |
sahilmgandhi | 18:6a4db94011d3 | 893 | #if DEVICE_SERIAL_FC |
sahilmgandhi | 18:6a4db94011d3 | 894 | |
sahilmgandhi | 18:6a4db94011d3 | 895 | /** |
sahilmgandhi | 18:6a4db94011d3 | 896 | * Set HW Control Flow |
sahilmgandhi | 18:6a4db94011d3 | 897 | * @param obj The serial object |
sahilmgandhi | 18:6a4db94011d3 | 898 | * @param type The Control Flow type (FlowControlNone, FlowControlRTS, FlowControlCTS, FlowControlRTSCTS) |
sahilmgandhi | 18:6a4db94011d3 | 899 | * @param rxflow Pin for the rxflow |
sahilmgandhi | 18:6a4db94011d3 | 900 | * @param txflow Pin for the txflow |
sahilmgandhi | 18:6a4db94011d3 | 901 | */ |
sahilmgandhi | 18:6a4db94011d3 | 902 | void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow) |
sahilmgandhi | 18:6a4db94011d3 | 903 | { |
sahilmgandhi | 18:6a4db94011d3 | 904 | struct serial_s *obj_s = SERIAL_S(obj); |
sahilmgandhi | 18:6a4db94011d3 | 905 | |
sahilmgandhi | 18:6a4db94011d3 | 906 | // Determine the UART to use (UART_1, UART_2, ...) |
sahilmgandhi | 18:6a4db94011d3 | 907 | UARTName uart_rts = (UARTName)pinmap_peripheral(rxflow, PinMap_UART_RTS); |
sahilmgandhi | 18:6a4db94011d3 | 908 | UARTName uart_cts = (UARTName)pinmap_peripheral(txflow, PinMap_UART_CTS); |
sahilmgandhi | 18:6a4db94011d3 | 909 | |
sahilmgandhi | 18:6a4db94011d3 | 910 | // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object |
sahilmgandhi | 18:6a4db94011d3 | 911 | obj_s->uart = (UARTName)pinmap_merge(uart_cts, uart_rts); |
sahilmgandhi | 18:6a4db94011d3 | 912 | MBED_ASSERT(obj_s->uart != (UARTName)NC); |
sahilmgandhi | 18:6a4db94011d3 | 913 | |
sahilmgandhi | 18:6a4db94011d3 | 914 | if(type == FlowControlNone) { |
sahilmgandhi | 18:6a4db94011d3 | 915 | // Disable hardware flow control |
sahilmgandhi | 18:6a4db94011d3 | 916 | obj_s->hw_flow_ctl = UART_HWCONTROL_NONE; |
sahilmgandhi | 18:6a4db94011d3 | 917 | } |
sahilmgandhi | 18:6a4db94011d3 | 918 | if (type == FlowControlRTS) { |
sahilmgandhi | 18:6a4db94011d3 | 919 | // Enable RTS |
sahilmgandhi | 18:6a4db94011d3 | 920 | MBED_ASSERT(uart_rts != (UARTName)NC); |
sahilmgandhi | 18:6a4db94011d3 | 921 | obj_s->hw_flow_ctl = UART_HWCONTROL_RTS; |
sahilmgandhi | 18:6a4db94011d3 | 922 | obj_s->pin_rts = rxflow; |
sahilmgandhi | 18:6a4db94011d3 | 923 | // Enable the pin for RTS function |
sahilmgandhi | 18:6a4db94011d3 | 924 | pinmap_pinout(rxflow, PinMap_UART_RTS); |
sahilmgandhi | 18:6a4db94011d3 | 925 | } |
sahilmgandhi | 18:6a4db94011d3 | 926 | if (type == FlowControlCTS) { |
sahilmgandhi | 18:6a4db94011d3 | 927 | // Enable CTS |
sahilmgandhi | 18:6a4db94011d3 | 928 | MBED_ASSERT(uart_cts != (UARTName)NC); |
sahilmgandhi | 18:6a4db94011d3 | 929 | obj_s->hw_flow_ctl = UART_HWCONTROL_CTS; |
sahilmgandhi | 18:6a4db94011d3 | 930 | obj_s->pin_cts = txflow; |
sahilmgandhi | 18:6a4db94011d3 | 931 | // Enable the pin for CTS function |
sahilmgandhi | 18:6a4db94011d3 | 932 | pinmap_pinout(txflow, PinMap_UART_CTS); |
sahilmgandhi | 18:6a4db94011d3 | 933 | } |
sahilmgandhi | 18:6a4db94011d3 | 934 | if (type == FlowControlRTSCTS) { |
sahilmgandhi | 18:6a4db94011d3 | 935 | // Enable CTS & RTS |
sahilmgandhi | 18:6a4db94011d3 | 936 | MBED_ASSERT(uart_rts != (UARTName)NC); |
sahilmgandhi | 18:6a4db94011d3 | 937 | MBED_ASSERT(uart_cts != (UARTName)NC); |
sahilmgandhi | 18:6a4db94011d3 | 938 | obj_s->hw_flow_ctl = UART_HWCONTROL_RTS_CTS; |
sahilmgandhi | 18:6a4db94011d3 | 939 | obj_s->pin_rts = rxflow; |
sahilmgandhi | 18:6a4db94011d3 | 940 | obj_s->pin_cts = txflow; |
sahilmgandhi | 18:6a4db94011d3 | 941 | // Enable the pin for CTS function |
sahilmgandhi | 18:6a4db94011d3 | 942 | pinmap_pinout(txflow, PinMap_UART_CTS); |
sahilmgandhi | 18:6a4db94011d3 | 943 | // Enable the pin for RTS function |
sahilmgandhi | 18:6a4db94011d3 | 944 | pinmap_pinout(rxflow, PinMap_UART_RTS); |
sahilmgandhi | 18:6a4db94011d3 | 945 | } |
sahilmgandhi | 18:6a4db94011d3 | 946 | |
sahilmgandhi | 18:6a4db94011d3 | 947 | init_uart(obj); |
sahilmgandhi | 18:6a4db94011d3 | 948 | } |
sahilmgandhi | 18:6a4db94011d3 | 949 | |
sahilmgandhi | 18:6a4db94011d3 | 950 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 951 | |
sahilmgandhi | 18:6a4db94011d3 | 952 | #endif |