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