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