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