mbed library sources
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_STM32L1/serial_api.c@496:543871686697, 2015-03-24 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Mar 24 09:00:08 2015 +0000
- Revision:
- 496:543871686697
- Parent:
- targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/serial_api.c@414:4ec4c5b614b0
Synchronized with git revision ea01d61fa18430564b78226045b196bb6bf6b66a
Full URL: https://github.com/mbedmicro/mbed/commit/ea01d61fa18430564b78226045b196bb6bf6b66a/
NUCLEO_L152RE - reorg hal
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 76:aeb1df146756 | 1 | /* mbed Microcontroller Library |
mbed_official | 76:aeb1df146756 | 2 | ******************************************************************************* |
mbed_official | 76:aeb1df146756 | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 76:aeb1df146756 | 4 | * All rights reserved. |
mbed_official | 76:aeb1df146756 | 5 | * |
mbed_official | 76:aeb1df146756 | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 76:aeb1df146756 | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 76:aeb1df146756 | 8 | * |
mbed_official | 76:aeb1df146756 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 76:aeb1df146756 | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 76:aeb1df146756 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 76:aeb1df146756 | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 76:aeb1df146756 | 13 | * and/or other materials provided with the distribution. |
mbed_official | 76:aeb1df146756 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 76:aeb1df146756 | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 76:aeb1df146756 | 16 | * without specific prior written permission. |
mbed_official | 76:aeb1df146756 | 17 | * |
mbed_official | 76:aeb1df146756 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 76:aeb1df146756 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 76:aeb1df146756 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 76:aeb1df146756 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 76:aeb1df146756 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 76:aeb1df146756 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 76:aeb1df146756 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 76:aeb1df146756 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 76:aeb1df146756 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 76:aeb1df146756 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 76:aeb1df146756 | 28 | ******************************************************************************* |
mbed_official | 76:aeb1df146756 | 29 | */ |
mbed_official | 227:7bd0639b8911 | 30 | #include "mbed_assert.h" |
mbed_official | 76:aeb1df146756 | 31 | #include "serial_api.h" |
mbed_official | 174:8bb9f3a33240 | 32 | |
mbed_official | 174:8bb9f3a33240 | 33 | #if DEVICE_SERIAL |
mbed_official | 174:8bb9f3a33240 | 34 | |
mbed_official | 76:aeb1df146756 | 35 | #include "cmsis.h" |
mbed_official | 76:aeb1df146756 | 36 | #include "pinmap.h" |
mbed_official | 76:aeb1df146756 | 37 | #include <string.h> |
mbed_official | 414:4ec4c5b614b0 | 38 | #include "PeripheralPins.h" |
mbed_official | 76:aeb1df146756 | 39 | |
mbed_official | 118:b44c45162f28 | 40 | #define UART_NUM (5) |
mbed_official | 76:aeb1df146756 | 41 | |
mbed_official | 354:e67efb2aab0e | 42 | static uint32_t serial_irq_ids[UART_NUM] = {0, 0, 0, 0, 0}; |
mbed_official | 76:aeb1df146756 | 43 | |
mbed_official | 76:aeb1df146756 | 44 | static uart_irq_handler irq_handler; |
mbed_official | 76:aeb1df146756 | 45 | |
mbed_official | 354:e67efb2aab0e | 46 | UART_HandleTypeDef UartHandle; |
mbed_official | 354:e67efb2aab0e | 47 | |
mbed_official | 76:aeb1df146756 | 48 | int stdio_uart_inited = 0; |
mbed_official | 76:aeb1df146756 | 49 | serial_t stdio_uart; |
mbed_official | 76:aeb1df146756 | 50 | |
mbed_official | 354:e67efb2aab0e | 51 | static void init_uart(serial_t *obj) |
mbed_official | 354:e67efb2aab0e | 52 | { |
mbed_official | 354:e67efb2aab0e | 53 | UartHandle.Instance = (USART_TypeDef *)(obj->uart); |
mbed_official | 76:aeb1df146756 | 54 | |
mbed_official | 354:e67efb2aab0e | 55 | UartHandle.Init.BaudRate = obj->baudrate; |
mbed_official | 354:e67efb2aab0e | 56 | UartHandle.Init.WordLength = obj->databits; |
mbed_official | 354:e67efb2aab0e | 57 | UartHandle.Init.StopBits = obj->stopbits; |
mbed_official | 354:e67efb2aab0e | 58 | UartHandle.Init.Parity = obj->parity; |
mbed_official | 354:e67efb2aab0e | 59 | UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
mbed_official | 242:7074e42da0b2 | 60 | |
mbed_official | 242:7074e42da0b2 | 61 | if (obj->pin_rx == NC) { |
mbed_official | 354:e67efb2aab0e | 62 | UartHandle.Init.Mode = UART_MODE_TX; |
mbed_official | 242:7074e42da0b2 | 63 | } else if (obj->pin_tx == NC) { |
mbed_official | 354:e67efb2aab0e | 64 | UartHandle.Init.Mode = UART_MODE_RX; |
mbed_official | 242:7074e42da0b2 | 65 | } else { |
mbed_official | 354:e67efb2aab0e | 66 | UartHandle.Init.Mode = UART_MODE_TX_RX; |
mbed_official | 242:7074e42da0b2 | 67 | } |
mbed_official | 242:7074e42da0b2 | 68 | |
mbed_official | 354:e67efb2aab0e | 69 | HAL_UART_Init(&UartHandle); |
mbed_official | 76:aeb1df146756 | 70 | } |
mbed_official | 76:aeb1df146756 | 71 | |
mbed_official | 354:e67efb2aab0e | 72 | void serial_init(serial_t *obj, PinName tx, PinName rx) |
mbed_official | 354:e67efb2aab0e | 73 | { |
mbed_official | 76:aeb1df146756 | 74 | // Determine the UART to use (UART_1, UART_2, ...) |
mbed_official | 76:aeb1df146756 | 75 | UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); |
mbed_official | 76:aeb1df146756 | 76 | UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); |
mbed_official | 174:8bb9f3a33240 | 77 | |
mbed_official | 76:aeb1df146756 | 78 | // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object |
mbed_official | 76:aeb1df146756 | 79 | obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); |
mbed_official | 227:7bd0639b8911 | 80 | MBED_ASSERT(obj->uart != (UARTName)NC); |
mbed_official | 76:aeb1df146756 | 81 | |
mbed_official | 354:e67efb2aab0e | 82 | // Enable UART clock |
mbed_official | 76:aeb1df146756 | 83 | if (obj->uart == UART_1) { |
mbed_official | 354:e67efb2aab0e | 84 | __USART1_CLK_ENABLE(); |
mbed_official | 215:83cf97a28428 | 85 | obj->index = 0; |
mbed_official | 76:aeb1df146756 | 86 | } |
mbed_official | 354:e67efb2aab0e | 87 | |
mbed_official | 76:aeb1df146756 | 88 | if (obj->uart == UART_2) { |
mbed_official | 354:e67efb2aab0e | 89 | __USART2_CLK_ENABLE(); |
mbed_official | 215:83cf97a28428 | 90 | obj->index = 1; |
mbed_official | 76:aeb1df146756 | 91 | } |
mbed_official | 354:e67efb2aab0e | 92 | |
mbed_official | 129:0182c99221bc | 93 | if (obj->uart == UART_3) { |
mbed_official | 354:e67efb2aab0e | 94 | __USART3_CLK_ENABLE(); |
mbed_official | 215:83cf97a28428 | 95 | obj->index = 2; |
mbed_official | 118:b44c45162f28 | 96 | } |
mbed_official | 354:e67efb2aab0e | 97 | |
mbed_official | 129:0182c99221bc | 98 | if (obj->uart == UART_4) { |
mbed_official | 354:e67efb2aab0e | 99 | __UART4_CLK_ENABLE(); |
mbed_official | 215:83cf97a28428 | 100 | obj->index = 3; |
mbed_official | 118:b44c45162f28 | 101 | } |
mbed_official | 354:e67efb2aab0e | 102 | |
mbed_official | 129:0182c99221bc | 103 | if (obj->uart == UART_5) { |
mbed_official | 354:e67efb2aab0e | 104 | __UART5_CLK_ENABLE(); |
mbed_official | 215:83cf97a28428 | 105 | obj->index = 4; |
mbed_official | 118:b44c45162f28 | 106 | } |
mbed_official | 129:0182c99221bc | 107 | |
mbed_official | 76:aeb1df146756 | 108 | // Configure the UART pins |
mbed_official | 76:aeb1df146756 | 109 | pinmap_pinout(tx, PinMap_UART_TX); |
mbed_official | 76:aeb1df146756 | 110 | pinmap_pinout(rx, PinMap_UART_RX); |
mbed_official | 414:4ec4c5b614b0 | 111 | if (tx != NC) { |
mbed_official | 339:40bd4701f3e2 | 112 | pin_mode(tx, PullUp); |
mbed_official | 339:40bd4701f3e2 | 113 | } |
mbed_official | 414:4ec4c5b614b0 | 114 | if (rx != NC) { |
mbed_official | 339:40bd4701f3e2 | 115 | pin_mode(rx, PullUp); |
mbed_official | 339:40bd4701f3e2 | 116 | } |
mbed_official | 76:aeb1df146756 | 117 | |
mbed_official | 76:aeb1df146756 | 118 | // Configure UART |
mbed_official | 76:aeb1df146756 | 119 | obj->baudrate = 9600; |
mbed_official | 354:e67efb2aab0e | 120 | obj->databits = UART_WORDLENGTH_8B; |
mbed_official | 354:e67efb2aab0e | 121 | obj->stopbits = UART_STOPBITS_1; |
mbed_official | 354:e67efb2aab0e | 122 | obj->parity = UART_PARITY_NONE; |
mbed_official | 354:e67efb2aab0e | 123 | obj->pin_tx = tx; |
mbed_official | 354:e67efb2aab0e | 124 | obj->pin_rx = rx; |
mbed_official | 76:aeb1df146756 | 125 | |
mbed_official | 354:e67efb2aab0e | 126 | init_uart(obj); |
mbed_official | 174:8bb9f3a33240 | 127 | |
mbed_official | 76:aeb1df146756 | 128 | // For stdio management |
mbed_official | 76:aeb1df146756 | 129 | if (obj->uart == STDIO_UART) { |
mbed_official | 76:aeb1df146756 | 130 | stdio_uart_inited = 1; |
mbed_official | 76:aeb1df146756 | 131 | memcpy(&stdio_uart, obj, sizeof(serial_t)); |
mbed_official | 174:8bb9f3a33240 | 132 | } |
mbed_official | 76:aeb1df146756 | 133 | } |
mbed_official | 76:aeb1df146756 | 134 | |
mbed_official | 354:e67efb2aab0e | 135 | void serial_free(serial_t *obj) |
mbed_official | 354:e67efb2aab0e | 136 | { |
mbed_official | 215:83cf97a28428 | 137 | // Reset UART and disable clock |
mbed_official | 215:83cf97a28428 | 138 | if (obj->uart == UART_1) { |
mbed_official | 354:e67efb2aab0e | 139 | __USART1_FORCE_RESET(); |
mbed_official | 354:e67efb2aab0e | 140 | __USART1_RELEASE_RESET(); |
mbed_official | 354:e67efb2aab0e | 141 | __USART1_CLK_DISABLE(); |
mbed_official | 215:83cf97a28428 | 142 | } |
mbed_official | 354:e67efb2aab0e | 143 | |
mbed_official | 215:83cf97a28428 | 144 | if (obj->uart == UART_2) { |
mbed_official | 354:e67efb2aab0e | 145 | __USART2_FORCE_RESET(); |
mbed_official | 354:e67efb2aab0e | 146 | __USART2_RELEASE_RESET(); |
mbed_official | 354:e67efb2aab0e | 147 | __USART2_CLK_DISABLE(); |
mbed_official | 215:83cf97a28428 | 148 | } |
mbed_official | 354:e67efb2aab0e | 149 | |
mbed_official | 215:83cf97a28428 | 150 | if (obj->uart == UART_3) { |
mbed_official | 354:e67efb2aab0e | 151 | __USART3_FORCE_RESET(); |
mbed_official | 354:e67efb2aab0e | 152 | __USART3_RELEASE_RESET(); |
mbed_official | 354:e67efb2aab0e | 153 | __USART3_CLK_DISABLE(); |
mbed_official | 215:83cf97a28428 | 154 | } |
mbed_official | 354:e67efb2aab0e | 155 | |
mbed_official | 215:83cf97a28428 | 156 | if (obj->uart == UART_4) { |
mbed_official | 354:e67efb2aab0e | 157 | __UART4_FORCE_RESET(); |
mbed_official | 354:e67efb2aab0e | 158 | __UART4_RELEASE_RESET(); |
mbed_official | 354:e67efb2aab0e | 159 | __UART4_CLK_DISABLE(); |
mbed_official | 215:83cf97a28428 | 160 | } |
mbed_official | 354:e67efb2aab0e | 161 | |
mbed_official | 215:83cf97a28428 | 162 | if (obj->uart == UART_5) { |
mbed_official | 354:e67efb2aab0e | 163 | __UART5_FORCE_RESET(); |
mbed_official | 354:e67efb2aab0e | 164 | __UART5_RELEASE_RESET(); |
mbed_official | 354:e67efb2aab0e | 165 | __UART5_CLK_DISABLE(); |
mbed_official | 215:83cf97a28428 | 166 | } |
mbed_official | 215:83cf97a28428 | 167 | |
mbed_official | 215:83cf97a28428 | 168 | // Configure GPIOs |
mbed_official | 354:e67efb2aab0e | 169 | pin_function(obj->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
mbed_official | 354:e67efb2aab0e | 170 | pin_function(obj->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
mbed_official | 215:83cf97a28428 | 171 | |
mbed_official | 76:aeb1df146756 | 172 | serial_irq_ids[obj->index] = 0; |
mbed_official | 76:aeb1df146756 | 173 | } |
mbed_official | 76:aeb1df146756 | 174 | |
mbed_official | 354:e67efb2aab0e | 175 | void serial_baud(serial_t *obj, int baudrate) |
mbed_official | 354:e67efb2aab0e | 176 | { |
mbed_official | 76:aeb1df146756 | 177 | obj->baudrate = baudrate; |
mbed_official | 354:e67efb2aab0e | 178 | init_uart(obj); |
mbed_official | 76:aeb1df146756 | 179 | } |
mbed_official | 76:aeb1df146756 | 180 | |
mbed_official | 354:e67efb2aab0e | 181 | void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) |
mbed_official | 354:e67efb2aab0e | 182 | { |
mbed_official | 242:7074e42da0b2 | 183 | if (data_bits == 9) { |
mbed_official | 354:e67efb2aab0e | 184 | obj->databits = UART_WORDLENGTH_9B; |
mbed_official | 242:7074e42da0b2 | 185 | } else { |
mbed_official | 354:e67efb2aab0e | 186 | obj->databits = UART_WORDLENGTH_8B; |
mbed_official | 76:aeb1df146756 | 187 | } |
mbed_official | 76:aeb1df146756 | 188 | |
mbed_official | 76:aeb1df146756 | 189 | switch (parity) { |
mbed_official | 174:8bb9f3a33240 | 190 | case ParityOdd: |
mbed_official | 174:8bb9f3a33240 | 191 | case ParityForced0: |
mbed_official | 354:e67efb2aab0e | 192 | obj->parity = UART_PARITY_ODD; |
mbed_official | 174:8bb9f3a33240 | 193 | break; |
mbed_official | 174:8bb9f3a33240 | 194 | case ParityEven: |
mbed_official | 174:8bb9f3a33240 | 195 | case ParityForced1: |
mbed_official | 354:e67efb2aab0e | 196 | obj->parity = UART_PARITY_EVEN; |
mbed_official | 174:8bb9f3a33240 | 197 | break; |
mbed_official | 174:8bb9f3a33240 | 198 | default: // ParityNone |
mbed_official | 354:e67efb2aab0e | 199 | obj->parity = UART_PARITY_NONE; |
mbed_official | 174:8bb9f3a33240 | 200 | break; |
mbed_official | 76:aeb1df146756 | 201 | } |
mbed_official | 174:8bb9f3a33240 | 202 | |
mbed_official | 76:aeb1df146756 | 203 | if (stop_bits == 2) { |
mbed_official | 354:e67efb2aab0e | 204 | obj->stopbits = UART_STOPBITS_2; |
mbed_official | 174:8bb9f3a33240 | 205 | } else { |
mbed_official | 354:e67efb2aab0e | 206 | obj->stopbits = UART_STOPBITS_1; |
mbed_official | 76:aeb1df146756 | 207 | } |
mbed_official | 76:aeb1df146756 | 208 | |
mbed_official | 354:e67efb2aab0e | 209 | init_uart(obj); |
mbed_official | 76:aeb1df146756 | 210 | } |
mbed_official | 76:aeb1df146756 | 211 | |
mbed_official | 76:aeb1df146756 | 212 | /****************************************************************************** |
mbed_official | 76:aeb1df146756 | 213 | * INTERRUPTS HANDLING |
mbed_official | 76:aeb1df146756 | 214 | ******************************************************************************/ |
mbed_official | 76:aeb1df146756 | 215 | |
mbed_official | 354:e67efb2aab0e | 216 | static void uart_irq(UARTName name, int id) |
mbed_official | 354:e67efb2aab0e | 217 | { |
mbed_official | 354:e67efb2aab0e | 218 | UartHandle.Instance = (USART_TypeDef *)name; |
mbed_official | 76:aeb1df146756 | 219 | if (serial_irq_ids[id] != 0) { |
mbed_official | 354:e67efb2aab0e | 220 | if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TC) != RESET) { |
mbed_official | 76:aeb1df146756 | 221 | irq_handler(serial_irq_ids[id], TxIrq); |
mbed_official | 354:e67efb2aab0e | 222 | __HAL_UART_CLEAR_FLAG(&UartHandle, UART_FLAG_TC); |
mbed_official | 76:aeb1df146756 | 223 | } |
mbed_official | 354:e67efb2aab0e | 224 | if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE) != RESET) { |
mbed_official | 76:aeb1df146756 | 225 | irq_handler(serial_irq_ids[id], RxIrq); |
mbed_official | 354:e67efb2aab0e | 226 | __HAL_UART_CLEAR_FLAG(&UartHandle, UART_FLAG_RXNE); |
mbed_official | 76:aeb1df146756 | 227 | } |
mbed_official | 76:aeb1df146756 | 228 | } |
mbed_official | 76:aeb1df146756 | 229 | } |
mbed_official | 76:aeb1df146756 | 230 | |
mbed_official | 354:e67efb2aab0e | 231 | static void uart1_irq(void) |
mbed_official | 354:e67efb2aab0e | 232 | { |
mbed_official | 354:e67efb2aab0e | 233 | uart_irq(UART_1, 0); |
mbed_official | 174:8bb9f3a33240 | 234 | } |
mbed_official | 354:e67efb2aab0e | 235 | |
mbed_official | 354:e67efb2aab0e | 236 | static void uart2_irq(void) |
mbed_official | 354:e67efb2aab0e | 237 | { |
mbed_official | 354:e67efb2aab0e | 238 | uart_irq(UART_2, 1); |
mbed_official | 174:8bb9f3a33240 | 239 | } |
mbed_official | 76:aeb1df146756 | 240 | |
mbed_official | 354:e67efb2aab0e | 241 | static void uart3_irq(void) |
mbed_official | 354:e67efb2aab0e | 242 | { |
mbed_official | 354:e67efb2aab0e | 243 | uart_irq(UART_3, 2); |
mbed_official | 354:e67efb2aab0e | 244 | } |
mbed_official | 354:e67efb2aab0e | 245 | |
mbed_official | 354:e67efb2aab0e | 246 | static void uart4_irq(void) |
mbed_official | 354:e67efb2aab0e | 247 | { |
mbed_official | 354:e67efb2aab0e | 248 | uart_irq(UART_4, 3); |
mbed_official | 354:e67efb2aab0e | 249 | } |
mbed_official | 354:e67efb2aab0e | 250 | |
mbed_official | 354:e67efb2aab0e | 251 | static void uart5_irq(void) |
mbed_official | 354:e67efb2aab0e | 252 | { |
mbed_official | 354:e67efb2aab0e | 253 | uart_irq(UART_5, 4); |
mbed_official | 354:e67efb2aab0e | 254 | } |
mbed_official | 354:e67efb2aab0e | 255 | |
mbed_official | 354:e67efb2aab0e | 256 | void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) |
mbed_official | 354:e67efb2aab0e | 257 | { |
mbed_official | 76:aeb1df146756 | 258 | irq_handler = handler; |
mbed_official | 76:aeb1df146756 | 259 | serial_irq_ids[obj->index] = id; |
mbed_official | 76:aeb1df146756 | 260 | } |
mbed_official | 76:aeb1df146756 | 261 | |
mbed_official | 354:e67efb2aab0e | 262 | void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) |
mbed_official | 354:e67efb2aab0e | 263 | { |
mbed_official | 76:aeb1df146756 | 264 | IRQn_Type irq_n = (IRQn_Type)0; |
mbed_official | 76:aeb1df146756 | 265 | uint32_t vector = 0; |
mbed_official | 354:e67efb2aab0e | 266 | |
mbed_official | 354:e67efb2aab0e | 267 | UartHandle.Instance = (USART_TypeDef *)(obj->uart); |
mbed_official | 76:aeb1df146756 | 268 | |
mbed_official | 76:aeb1df146756 | 269 | if (obj->uart == UART_1) { |
mbed_official | 174:8bb9f3a33240 | 270 | irq_n = USART1_IRQn; |
mbed_official | 174:8bb9f3a33240 | 271 | vector = (uint32_t)&uart1_irq; |
mbed_official | 76:aeb1df146756 | 272 | } |
mbed_official | 174:8bb9f3a33240 | 273 | |
mbed_official | 76:aeb1df146756 | 274 | if (obj->uart == UART_2) { |
mbed_official | 174:8bb9f3a33240 | 275 | irq_n = USART2_IRQn; |
mbed_official | 174:8bb9f3a33240 | 276 | vector = (uint32_t)&uart2_irq; |
mbed_official | 76:aeb1df146756 | 277 | } |
mbed_official | 129:0182c99221bc | 278 | |
mbed_official | 118:b44c45162f28 | 279 | if (obj->uart == UART_3) { |
mbed_official | 174:8bb9f3a33240 | 280 | irq_n = USART3_IRQn; |
mbed_official | 174:8bb9f3a33240 | 281 | vector = (uint32_t)&uart3_irq; |
mbed_official | 118:b44c45162f28 | 282 | } |
mbed_official | 129:0182c99221bc | 283 | |
mbed_official | 118:b44c45162f28 | 284 | if (obj->uart == UART_4) { |
mbed_official | 174:8bb9f3a33240 | 285 | irq_n = UART4_IRQn; |
mbed_official | 174:8bb9f3a33240 | 286 | vector = (uint32_t)&uart4_irq; |
mbed_official | 118:b44c45162f28 | 287 | } |
mbed_official | 129:0182c99221bc | 288 | |
mbed_official | 118:b44c45162f28 | 289 | if (obj->uart == UART_5) { |
mbed_official | 174:8bb9f3a33240 | 290 | irq_n = UART5_IRQn; |
mbed_official | 174:8bb9f3a33240 | 291 | vector = (uint32_t)&uart5_irq; |
mbed_official | 118:b44c45162f28 | 292 | } |
mbed_official | 174:8bb9f3a33240 | 293 | |
mbed_official | 76:aeb1df146756 | 294 | if (enable) { |
mbed_official | 174:8bb9f3a33240 | 295 | |
mbed_official | 76:aeb1df146756 | 296 | if (irq == RxIrq) { |
mbed_official | 354:e67efb2aab0e | 297 | __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_RXNE); |
mbed_official | 174:8bb9f3a33240 | 298 | } else { // TxIrq |
mbed_official | 354:e67efb2aab0e | 299 | __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_TC); |
mbed_official | 174:8bb9f3a33240 | 300 | } |
mbed_official | 174:8bb9f3a33240 | 301 | |
mbed_official | 76:aeb1df146756 | 302 | NVIC_SetVector(irq_n, vector); |
mbed_official | 76:aeb1df146756 | 303 | NVIC_EnableIRQ(irq_n); |
mbed_official | 174:8bb9f3a33240 | 304 | |
mbed_official | 76:aeb1df146756 | 305 | } else { // disable |
mbed_official | 174:8bb9f3a33240 | 306 | |
mbed_official | 76:aeb1df146756 | 307 | int all_disabled = 0; |
mbed_official | 174:8bb9f3a33240 | 308 | |
mbed_official | 76:aeb1df146756 | 309 | if (irq == RxIrq) { |
mbed_official | 354:e67efb2aab0e | 310 | __HAL_UART_DISABLE_IT(&UartHandle, UART_IT_RXNE); |
mbed_official | 76:aeb1df146756 | 311 | // Check if TxIrq is disabled too |
mbed_official | 354:e67efb2aab0e | 312 | if ((UartHandle.Instance->CR1 & USART_CR1_TXEIE) == 0) all_disabled = 1; |
mbed_official | 174:8bb9f3a33240 | 313 | } else { // TxIrq |
mbed_official | 354:e67efb2aab0e | 314 | __HAL_UART_DISABLE_IT(&UartHandle, UART_IT_TXE); |
mbed_official | 76:aeb1df146756 | 315 | // Check if RxIrq is disabled too |
mbed_official | 354:e67efb2aab0e | 316 | if ((UartHandle.Instance->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1; |
mbed_official | 76:aeb1df146756 | 317 | } |
mbed_official | 174:8bb9f3a33240 | 318 | |
mbed_official | 76:aeb1df146756 | 319 | if (all_disabled) NVIC_DisableIRQ(irq_n); |
mbed_official | 174:8bb9f3a33240 | 320 | |
mbed_official | 174:8bb9f3a33240 | 321 | } |
mbed_official | 76:aeb1df146756 | 322 | } |
mbed_official | 76:aeb1df146756 | 323 | |
mbed_official | 76:aeb1df146756 | 324 | /****************************************************************************** |
mbed_official | 76:aeb1df146756 | 325 | * READ/WRITE |
mbed_official | 76:aeb1df146756 | 326 | ******************************************************************************/ |
mbed_official | 76:aeb1df146756 | 327 | |
mbed_official | 354:e67efb2aab0e | 328 | int serial_getc(serial_t *obj) |
mbed_official | 354:e67efb2aab0e | 329 | { |
mbed_official | 354:e67efb2aab0e | 330 | USART_TypeDef *uart = (USART_TypeDef *)(obj->uart); |
mbed_official | 76:aeb1df146756 | 331 | while (!serial_readable(obj)); |
mbed_official | 354:e67efb2aab0e | 332 | return (int)(uart->DR & 0xFF); |
mbed_official | 76:aeb1df146756 | 333 | } |
mbed_official | 76:aeb1df146756 | 334 | |
mbed_official | 354:e67efb2aab0e | 335 | void serial_putc(serial_t *obj, int c) |
mbed_official | 354:e67efb2aab0e | 336 | { |
mbed_official | 354:e67efb2aab0e | 337 | USART_TypeDef *uart = (USART_TypeDef *)(obj->uart); |
mbed_official | 76:aeb1df146756 | 338 | while (!serial_writable(obj)); |
mbed_official | 354:e67efb2aab0e | 339 | uart->DR = (uint32_t)(c & 0xFF); |
mbed_official | 76:aeb1df146756 | 340 | } |
mbed_official | 76:aeb1df146756 | 341 | |
mbed_official | 354:e67efb2aab0e | 342 | int serial_readable(serial_t *obj) |
mbed_official | 354:e67efb2aab0e | 343 | { |
mbed_official | 76:aeb1df146756 | 344 | int status; |
mbed_official | 354:e67efb2aab0e | 345 | UartHandle.Instance = (USART_TypeDef *)(obj->uart); |
mbed_official | 76:aeb1df146756 | 346 | // Check if data is received |
mbed_official | 354:e67efb2aab0e | 347 | status = ((__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE) != RESET) ? 1 : 0); |
mbed_official | 76:aeb1df146756 | 348 | return status; |
mbed_official | 76:aeb1df146756 | 349 | } |
mbed_official | 76:aeb1df146756 | 350 | |
mbed_official | 354:e67efb2aab0e | 351 | int serial_writable(serial_t *obj) |
mbed_official | 354:e67efb2aab0e | 352 | { |
mbed_official | 76:aeb1df146756 | 353 | int status; |
mbed_official | 354:e67efb2aab0e | 354 | UartHandle.Instance = (USART_TypeDef *)(obj->uart); |
mbed_official | 76:aeb1df146756 | 355 | // Check if data is transmitted |
mbed_official | 354:e67efb2aab0e | 356 | status = ((__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE) != RESET) ? 1 : 0); |
mbed_official | 76:aeb1df146756 | 357 | return status; |
mbed_official | 76:aeb1df146756 | 358 | } |
mbed_official | 76:aeb1df146756 | 359 | |
mbed_official | 354:e67efb2aab0e | 360 | void serial_clear(serial_t *obj) |
mbed_official | 354:e67efb2aab0e | 361 | { |
mbed_official | 354:e67efb2aab0e | 362 | UartHandle.Instance = (USART_TypeDef *)(obj->uart); |
mbed_official | 354:e67efb2aab0e | 363 | __HAL_UART_CLEAR_FLAG(&UartHandle, UART_FLAG_TXE); |
mbed_official | 354:e67efb2aab0e | 364 | __HAL_UART_CLEAR_FLAG(&UartHandle, UART_FLAG_RXNE); |
mbed_official | 76:aeb1df146756 | 365 | } |
mbed_official | 76:aeb1df146756 | 366 | |
mbed_official | 354:e67efb2aab0e | 367 | void serial_pinout_tx(PinName tx) |
mbed_official | 354:e67efb2aab0e | 368 | { |
mbed_official | 76:aeb1df146756 | 369 | pinmap_pinout(tx, PinMap_UART_TX); |
mbed_official | 76:aeb1df146756 | 370 | } |
mbed_official | 76:aeb1df146756 | 371 | |
mbed_official | 354:e67efb2aab0e | 372 | void serial_break_set(serial_t *obj) |
mbed_official | 354:e67efb2aab0e | 373 | { |
mbed_official | 354:e67efb2aab0e | 374 | UartHandle.Instance = (USART_TypeDef *)(obj->uart); |
mbed_official | 354:e67efb2aab0e | 375 | HAL_LIN_SendBreak(&UartHandle); |
mbed_official | 76:aeb1df146756 | 376 | } |
mbed_official | 76:aeb1df146756 | 377 | |
mbed_official | 354:e67efb2aab0e | 378 | void serial_break_clear(serial_t *obj) |
mbed_official | 354:e67efb2aab0e | 379 | { |
mbed_official | 76:aeb1df146756 | 380 | } |
mbed_official | 174:8bb9f3a33240 | 381 | |
mbed_official | 174:8bb9f3a33240 | 382 | #endif |