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