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