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