mbed library sources
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F334R8/serial_api.c@445:9a3ffe6cfa19, 2015-01-14 (annotated)
- Committer:
- moirans2
- Date:
- Wed Jan 14 20:53:08 2015 +0000
- Revision:
- 445:9a3ffe6cfa19
- Parent:
- 414:4ec4c5b614b0
internal clock stm32L051
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 | 414:4ec4c5b614b0 | 38 | #include "PeripheralPins.h" |
mbed_official | 237:f3da66175598 | 39 | |
mbed_official | 237:f3da66175598 | 40 | #define UART_NUM (3) |
mbed_official | 237:f3da66175598 | 41 | |
mbed_official | 237:f3da66175598 | 42 | static uint32_t serial_irq_ids[UART_NUM] = {0, 0, 0}; |
mbed_official | 237:f3da66175598 | 43 | |
mbed_official | 237:f3da66175598 | 44 | static uart_irq_handler irq_handler; |
mbed_official | 237:f3da66175598 | 45 | |
mbed_official | 237:f3da66175598 | 46 | UART_HandleTypeDef UartHandle; |
mbed_official | 237:f3da66175598 | 47 | |
mbed_official | 237:f3da66175598 | 48 | int stdio_uart_inited = 0; |
mbed_official | 237:f3da66175598 | 49 | serial_t stdio_uart; |
mbed_official | 237:f3da66175598 | 50 | |
mbed_official | 237:f3da66175598 | 51 | static void init_uart(serial_t *obj) |
mbed_official | 237:f3da66175598 | 52 | { |
mbed_official | 237:f3da66175598 | 53 | UartHandle.Instance = (USART_TypeDef *)(obj->uart); |
mbed_official | 237:f3da66175598 | 54 | |
mbed_official | 237:f3da66175598 | 55 | UartHandle.Init.BaudRate = obj->baudrate; |
mbed_official | 237:f3da66175598 | 56 | UartHandle.Init.WordLength = obj->databits; |
mbed_official | 237:f3da66175598 | 57 | UartHandle.Init.StopBits = obj->stopbits; |
mbed_official | 237:f3da66175598 | 58 | UartHandle.Init.Parity = obj->parity; |
mbed_official | 237:f3da66175598 | 59 | UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
mbed_official | 237:f3da66175598 | 60 | |
mbed_official | 237:f3da66175598 | 61 | if (obj->pin_rx == NC) { |
mbed_official | 237:f3da66175598 | 62 | UartHandle.Init.Mode = UART_MODE_TX; |
mbed_official | 237:f3da66175598 | 63 | } else if (obj->pin_tx == NC) { |
mbed_official | 237:f3da66175598 | 64 | UartHandle.Init.Mode = UART_MODE_RX; |
mbed_official | 237:f3da66175598 | 65 | } else { |
mbed_official | 237:f3da66175598 | 66 | UartHandle.Init.Mode = UART_MODE_TX_RX; |
mbed_official | 237:f3da66175598 | 67 | } |
mbed_official | 237:f3da66175598 | 68 | |
mbed_official | 293:2a9cf2ed1474 | 69 | // Disable the reception overrun detection |
mbed_official | 293:2a9cf2ed1474 | 70 | UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_RXOVERRUNDISABLE_INIT; |
mbed_official | 293:2a9cf2ed1474 | 71 | UartHandle.AdvancedInit.OverrunDisable = UART_ADVFEATURE_OVERRUN_DISABLE; |
mbed_official | 402:09075a3b15e3 | 72 | |
mbed_official | 237:f3da66175598 | 73 | HAL_UART_Init(&UartHandle); |
mbed_official | 237:f3da66175598 | 74 | } |
mbed_official | 237:f3da66175598 | 75 | |
mbed_official | 237:f3da66175598 | 76 | void serial_init(serial_t *obj, PinName tx, PinName rx) |
mbed_official | 237:f3da66175598 | 77 | { |
mbed_official | 237:f3da66175598 | 78 | // Determine the UART to use (UART_1, UART_2, ...) |
mbed_official | 237:f3da66175598 | 79 | UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); |
mbed_official | 237:f3da66175598 | 80 | UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); |
mbed_official | 237:f3da66175598 | 81 | |
mbed_official | 237:f3da66175598 | 82 | // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object |
mbed_official | 237:f3da66175598 | 83 | obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); |
mbed_official | 237:f3da66175598 | 84 | MBED_ASSERT(obj->uart != (UARTName)NC); |
mbed_official | 237:f3da66175598 | 85 | |
mbed_official | 237:f3da66175598 | 86 | // Enable USART clock |
mbed_official | 237:f3da66175598 | 87 | if (obj->uart == UART_1) { |
mbed_official | 237:f3da66175598 | 88 | __USART1_CLK_ENABLE(); |
mbed_official | 237:f3da66175598 | 89 | obj->index = 0; |
mbed_official | 237:f3da66175598 | 90 | } |
mbed_official | 237:f3da66175598 | 91 | if (obj->uart == UART_2) { |
mbed_official | 237:f3da66175598 | 92 | __USART2_CLK_ENABLE(); |
mbed_official | 237:f3da66175598 | 93 | obj->index = 1; |
mbed_official | 237:f3da66175598 | 94 | } |
mbed_official | 237:f3da66175598 | 95 | if (obj->uart == UART_3) { |
mbed_official | 237:f3da66175598 | 96 | __USART3_CLK_ENABLE(); |
mbed_official | 237:f3da66175598 | 97 | obj->index = 2; |
mbed_official | 237:f3da66175598 | 98 | } |
mbed_official | 237:f3da66175598 | 99 | |
mbed_official | 237:f3da66175598 | 100 | // Configure the UART pins |
mbed_official | 237:f3da66175598 | 101 | pinmap_pinout(tx, PinMap_UART_TX); |
mbed_official | 237:f3da66175598 | 102 | pinmap_pinout(rx, PinMap_UART_RX); |
mbed_official | 339:40bd4701f3e2 | 103 | if (tx != NC) { |
mbed_official | 339:40bd4701f3e2 | 104 | pin_mode(tx, PullUp); |
mbed_official | 339:40bd4701f3e2 | 105 | } |
mbed_official | 339:40bd4701f3e2 | 106 | if (rx != NC) { |
mbed_official | 339:40bd4701f3e2 | 107 | pin_mode(rx, PullUp); |
mbed_official | 339:40bd4701f3e2 | 108 | } |
mbed_official | 237:f3da66175598 | 109 | |
mbed_official | 237:f3da66175598 | 110 | // Configure UART |
mbed_official | 237:f3da66175598 | 111 | obj->baudrate = 9600; |
mbed_official | 237:f3da66175598 | 112 | obj->databits = UART_WORDLENGTH_8B; |
mbed_official | 237:f3da66175598 | 113 | obj->stopbits = UART_STOPBITS_1; |
mbed_official | 237:f3da66175598 | 114 | obj->parity = UART_PARITY_NONE; |
mbed_official | 237:f3da66175598 | 115 | |
mbed_official | 237:f3da66175598 | 116 | obj->pin_tx = tx; |
mbed_official | 237:f3da66175598 | 117 | obj->pin_rx = rx; |
mbed_official | 237:f3da66175598 | 118 | |
mbed_official | 237:f3da66175598 | 119 | init_uart(obj); |
mbed_official | 237:f3da66175598 | 120 | |
mbed_official | 237:f3da66175598 | 121 | // For stdio management |
mbed_official | 237:f3da66175598 | 122 | if (obj->uart == STDIO_UART) { |
mbed_official | 237:f3da66175598 | 123 | stdio_uart_inited = 1; |
mbed_official | 237:f3da66175598 | 124 | memcpy(&stdio_uart, obj, sizeof(serial_t)); |
mbed_official | 237:f3da66175598 | 125 | } |
mbed_official | 237:f3da66175598 | 126 | } |
mbed_official | 237:f3da66175598 | 127 | |
mbed_official | 237:f3da66175598 | 128 | void serial_free(serial_t *obj) |
mbed_official | 237:f3da66175598 | 129 | { |
mbed_official | 237:f3da66175598 | 130 | // Reset UART and disable clock |
mbed_official | 237:f3da66175598 | 131 | if (obj->uart == UART_1) { |
mbed_official | 237:f3da66175598 | 132 | __USART1_FORCE_RESET(); |
mbed_official | 237:f3da66175598 | 133 | __USART1_RELEASE_RESET(); |
mbed_official | 237:f3da66175598 | 134 | __USART1_CLK_DISABLE(); |
mbed_official | 237:f3da66175598 | 135 | } |
mbed_official | 237:f3da66175598 | 136 | if (obj->uart == UART_2) { |
mbed_official | 237:f3da66175598 | 137 | __USART2_FORCE_RESET(); |
mbed_official | 237:f3da66175598 | 138 | __USART2_RELEASE_RESET(); |
mbed_official | 237:f3da66175598 | 139 | __USART2_CLK_DISABLE(); |
mbed_official | 237:f3da66175598 | 140 | } |
mbed_official | 237:f3da66175598 | 141 | if (obj->uart == UART_3) { |
mbed_official | 237:f3da66175598 | 142 | __USART3_FORCE_RESET(); |
mbed_official | 237:f3da66175598 | 143 | __USART3_RELEASE_RESET(); |
mbed_official | 237:f3da66175598 | 144 | __USART3_CLK_DISABLE(); |
mbed_official | 237:f3da66175598 | 145 | } |
mbed_official | 237:f3da66175598 | 146 | |
mbed_official | 237:f3da66175598 | 147 | // Configure GPIOs |
mbed_official | 237:f3da66175598 | 148 | pin_function(obj->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
mbed_official | 237:f3da66175598 | 149 | pin_function(obj->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
mbed_official | 237:f3da66175598 | 150 | |
mbed_official | 237:f3da66175598 | 151 | serial_irq_ids[obj->index] = 0; |
mbed_official | 237:f3da66175598 | 152 | } |
mbed_official | 237:f3da66175598 | 153 | |
mbed_official | 237:f3da66175598 | 154 | void serial_baud(serial_t *obj, int baudrate) |
mbed_official | 237:f3da66175598 | 155 | { |
mbed_official | 237:f3da66175598 | 156 | obj->baudrate = baudrate; |
mbed_official | 237:f3da66175598 | 157 | init_uart(obj); |
mbed_official | 237:f3da66175598 | 158 | } |
mbed_official | 237:f3da66175598 | 159 | |
mbed_official | 237:f3da66175598 | 160 | void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) |
mbed_official | 237:f3da66175598 | 161 | { |
mbed_official | 237:f3da66175598 | 162 | if (data_bits == 9) { |
mbed_official | 237:f3da66175598 | 163 | obj->databits = UART_WORDLENGTH_9B; |
mbed_official | 237:f3da66175598 | 164 | } else { |
mbed_official | 237:f3da66175598 | 165 | obj->databits = UART_WORDLENGTH_8B; |
mbed_official | 237:f3da66175598 | 166 | } |
mbed_official | 237:f3da66175598 | 167 | |
mbed_official | 237:f3da66175598 | 168 | switch (parity) { |
mbed_official | 237:f3da66175598 | 169 | case ParityOdd: |
mbed_official | 237:f3da66175598 | 170 | case ParityForced0: |
mbed_official | 237:f3da66175598 | 171 | obj->parity = UART_PARITY_ODD; |
mbed_official | 237:f3da66175598 | 172 | break; |
mbed_official | 237:f3da66175598 | 173 | case ParityEven: |
mbed_official | 237:f3da66175598 | 174 | case ParityForced1: |
mbed_official | 237:f3da66175598 | 175 | obj->parity = UART_PARITY_EVEN; |
mbed_official | 237:f3da66175598 | 176 | break; |
mbed_official | 237:f3da66175598 | 177 | default: // ParityNone |
mbed_official | 237:f3da66175598 | 178 | obj->parity = UART_PARITY_NONE; |
mbed_official | 237:f3da66175598 | 179 | break; |
mbed_official | 237:f3da66175598 | 180 | } |
mbed_official | 237:f3da66175598 | 181 | |
mbed_official | 237:f3da66175598 | 182 | if (stop_bits == 2) { |
mbed_official | 237:f3da66175598 | 183 | obj->stopbits = UART_STOPBITS_2; |
mbed_official | 237:f3da66175598 | 184 | } else { |
mbed_official | 237:f3da66175598 | 185 | obj->stopbits = UART_STOPBITS_1; |
mbed_official | 237:f3da66175598 | 186 | } |
mbed_official | 237:f3da66175598 | 187 | |
mbed_official | 237:f3da66175598 | 188 | init_uart(obj); |
mbed_official | 237:f3da66175598 | 189 | } |
mbed_official | 237:f3da66175598 | 190 | |
mbed_official | 237:f3da66175598 | 191 | /****************************************************************************** |
mbed_official | 237:f3da66175598 | 192 | * INTERRUPTS HANDLING |
mbed_official | 237:f3da66175598 | 193 | ******************************************************************************/ |
mbed_official | 237:f3da66175598 | 194 | |
mbed_official | 237:f3da66175598 | 195 | static void uart_irq(UARTName name, int id) |
mbed_official | 237:f3da66175598 | 196 | { |
mbed_official | 237:f3da66175598 | 197 | UartHandle.Instance = (USART_TypeDef *)name; |
mbed_official | 237:f3da66175598 | 198 | if (serial_irq_ids[id] != 0) { |
mbed_official | 237:f3da66175598 | 199 | if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TC) != RESET) { |
mbed_official | 237:f3da66175598 | 200 | irq_handler(serial_irq_ids[id], TxIrq); |
mbed_official | 237:f3da66175598 | 201 | __HAL_UART_CLEAR_IT(&UartHandle, UART_FLAG_TC); |
mbed_official | 237:f3da66175598 | 202 | } |
mbed_official | 237:f3da66175598 | 203 | if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE) != RESET) { |
mbed_official | 237:f3da66175598 | 204 | irq_handler(serial_irq_ids[id], RxIrq); |
mbed_official | 293:2a9cf2ed1474 | 205 | volatile uint32_t tmpval = UartHandle.Instance->RDR; // Clear RXNE bit |
mbed_official | 237:f3da66175598 | 206 | } |
mbed_official | 237:f3da66175598 | 207 | } |
mbed_official | 237:f3da66175598 | 208 | } |
mbed_official | 237:f3da66175598 | 209 | |
mbed_official | 237:f3da66175598 | 210 | static void uart1_irq(void) |
mbed_official | 237:f3da66175598 | 211 | { |
mbed_official | 237:f3da66175598 | 212 | uart_irq(UART_1, 0); |
mbed_official | 237:f3da66175598 | 213 | } |
mbed_official | 237:f3da66175598 | 214 | |
mbed_official | 237:f3da66175598 | 215 | static void uart2_irq(void) |
mbed_official | 237:f3da66175598 | 216 | { |
mbed_official | 237:f3da66175598 | 217 | uart_irq(UART_2, 1); |
mbed_official | 237:f3da66175598 | 218 | } |
mbed_official | 237:f3da66175598 | 219 | |
mbed_official | 237:f3da66175598 | 220 | static void uart3_irq(void) |
mbed_official | 237:f3da66175598 | 221 | { |
mbed_official | 237:f3da66175598 | 222 | uart_irq(UART_3, 2); |
mbed_official | 237:f3da66175598 | 223 | } |
mbed_official | 237:f3da66175598 | 224 | |
mbed_official | 237:f3da66175598 | 225 | void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) |
mbed_official | 237:f3da66175598 | 226 | { |
mbed_official | 237:f3da66175598 | 227 | irq_handler = handler; |
mbed_official | 237:f3da66175598 | 228 | serial_irq_ids[obj->index] = id; |
mbed_official | 237:f3da66175598 | 229 | } |
mbed_official | 237:f3da66175598 | 230 | |
mbed_official | 237:f3da66175598 | 231 | void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) |
mbed_official | 237:f3da66175598 | 232 | { |
mbed_official | 237:f3da66175598 | 233 | IRQn_Type irq_n = (IRQn_Type)0; |
mbed_official | 237:f3da66175598 | 234 | uint32_t vector = 0; |
mbed_official | 237:f3da66175598 | 235 | |
mbed_official | 237:f3da66175598 | 236 | UartHandle.Instance = (USART_TypeDef *)(obj->uart); |
mbed_official | 237:f3da66175598 | 237 | |
mbed_official | 237:f3da66175598 | 238 | if (obj->uart == UART_1) { |
mbed_official | 237:f3da66175598 | 239 | irq_n = USART1_IRQn; |
mbed_official | 237:f3da66175598 | 240 | vector = (uint32_t)&uart1_irq; |
mbed_official | 237:f3da66175598 | 241 | } |
mbed_official | 237:f3da66175598 | 242 | |
mbed_official | 237:f3da66175598 | 243 | if (obj->uart == UART_2) { |
mbed_official | 237:f3da66175598 | 244 | irq_n = USART2_IRQn; |
mbed_official | 237:f3da66175598 | 245 | vector = (uint32_t)&uart2_irq; |
mbed_official | 237:f3da66175598 | 246 | } |
mbed_official | 237:f3da66175598 | 247 | |
mbed_official | 237:f3da66175598 | 248 | if (obj->uart == UART_3) { |
mbed_official | 237:f3da66175598 | 249 | irq_n = USART3_IRQn; |
mbed_official | 237:f3da66175598 | 250 | vector = (uint32_t)&uart3_irq; |
mbed_official | 237:f3da66175598 | 251 | } |
mbed_official | 237:f3da66175598 | 252 | |
mbed_official | 237:f3da66175598 | 253 | if (enable) { |
mbed_official | 237:f3da66175598 | 254 | |
mbed_official | 237:f3da66175598 | 255 | if (irq == RxIrq) { |
mbed_official | 237:f3da66175598 | 256 | __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_RXNE); |
mbed_official | 237:f3da66175598 | 257 | } else { // TxIrq |
mbed_official | 237:f3da66175598 | 258 | __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_TC); |
mbed_official | 237:f3da66175598 | 259 | } |
mbed_official | 237:f3da66175598 | 260 | |
mbed_official | 237:f3da66175598 | 261 | NVIC_SetVector(irq_n, vector); |
mbed_official | 237:f3da66175598 | 262 | NVIC_EnableIRQ(irq_n); |
mbed_official | 237:f3da66175598 | 263 | |
mbed_official | 237:f3da66175598 | 264 | } else { // disable |
mbed_official | 237:f3da66175598 | 265 | |
mbed_official | 237:f3da66175598 | 266 | int all_disabled = 0; |
mbed_official | 237:f3da66175598 | 267 | |
mbed_official | 237:f3da66175598 | 268 | if (irq == RxIrq) { |
mbed_official | 237:f3da66175598 | 269 | __HAL_UART_DISABLE_IT(&UartHandle, UART_IT_RXNE); |
mbed_official | 237:f3da66175598 | 270 | // Check if TxIrq is disabled too |
mbed_official | 293:2a9cf2ed1474 | 271 | if ((UartHandle.Instance->CR1 & USART_CR1_TCIE) == 0) all_disabled = 1; |
mbed_official | 237:f3da66175598 | 272 | } else { // TxIrq |
mbed_official | 293:2a9cf2ed1474 | 273 | __HAL_UART_DISABLE_IT(&UartHandle, UART_IT_TC); |
mbed_official | 237:f3da66175598 | 274 | // Check if RxIrq is disabled too |
mbed_official | 237:f3da66175598 | 275 | if ((UartHandle.Instance->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1; |
mbed_official | 237:f3da66175598 | 276 | } |
mbed_official | 237:f3da66175598 | 277 | |
mbed_official | 237:f3da66175598 | 278 | if (all_disabled) NVIC_DisableIRQ(irq_n); |
mbed_official | 237:f3da66175598 | 279 | |
mbed_official | 237:f3da66175598 | 280 | } |
mbed_official | 237:f3da66175598 | 281 | } |
mbed_official | 237:f3da66175598 | 282 | |
mbed_official | 237:f3da66175598 | 283 | /****************************************************************************** |
mbed_official | 237:f3da66175598 | 284 | * READ/WRITE |
mbed_official | 237:f3da66175598 | 285 | ******************************************************************************/ |
mbed_official | 237:f3da66175598 | 286 | |
mbed_official | 237:f3da66175598 | 287 | int serial_getc(serial_t *obj) |
mbed_official | 237:f3da66175598 | 288 | { |
mbed_official | 237:f3da66175598 | 289 | USART_TypeDef *uart = (USART_TypeDef *)(obj->uart); |
mbed_official | 237:f3da66175598 | 290 | while (!serial_readable(obj)); |
mbed_official | 237:f3da66175598 | 291 | if (obj->databits == UART_WORDLENGTH_8B) { |
mbed_official | 237:f3da66175598 | 292 | return (int)(uart->RDR & (uint8_t)0xFF); |
mbed_official | 237:f3da66175598 | 293 | } else { |
mbed_official | 237:f3da66175598 | 294 | return (int)(uart->RDR & (uint16_t)0x1FF); |
mbed_official | 237:f3da66175598 | 295 | } |
mbed_official | 237:f3da66175598 | 296 | } |
mbed_official | 237:f3da66175598 | 297 | |
mbed_official | 237:f3da66175598 | 298 | void serial_putc(serial_t *obj, int c) |
mbed_official | 237:f3da66175598 | 299 | { |
mbed_official | 237:f3da66175598 | 300 | USART_TypeDef *uart = (USART_TypeDef *)(obj->uart); |
mbed_official | 237:f3da66175598 | 301 | while (!serial_writable(obj)); |
mbed_official | 237:f3da66175598 | 302 | if (obj->databits == UART_WORDLENGTH_8B) { |
mbed_official | 237:f3da66175598 | 303 | uart->TDR = (uint8_t)(c & (uint8_t)0xFF); |
mbed_official | 237:f3da66175598 | 304 | } else { |
mbed_official | 237:f3da66175598 | 305 | uart->TDR = (uint16_t)(c & (uint16_t)0x1FF); |
mbed_official | 237:f3da66175598 | 306 | } |
mbed_official | 237:f3da66175598 | 307 | } |
mbed_official | 237:f3da66175598 | 308 | |
mbed_official | 237:f3da66175598 | 309 | int serial_readable(serial_t *obj) |
mbed_official | 237:f3da66175598 | 310 | { |
mbed_official | 237:f3da66175598 | 311 | int status; |
mbed_official | 237:f3da66175598 | 312 | UartHandle.Instance = (USART_TypeDef *)(obj->uart); |
mbed_official | 237:f3da66175598 | 313 | // Check if data is received |
mbed_official | 237:f3da66175598 | 314 | status = ((__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE) != RESET) ? 1 : 0); |
mbed_official | 237:f3da66175598 | 315 | return status; |
mbed_official | 237:f3da66175598 | 316 | } |
mbed_official | 237:f3da66175598 | 317 | |
mbed_official | 237:f3da66175598 | 318 | int serial_writable(serial_t *obj) |
mbed_official | 237:f3da66175598 | 319 | { |
mbed_official | 237:f3da66175598 | 320 | int status; |
mbed_official | 237:f3da66175598 | 321 | UartHandle.Instance = (USART_TypeDef *)(obj->uart); |
mbed_official | 237:f3da66175598 | 322 | // Check if data is transmitted |
mbed_official | 237:f3da66175598 | 323 | status = ((__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE) != RESET) ? 1 : 0); |
mbed_official | 237:f3da66175598 | 324 | return status; |
mbed_official | 237:f3da66175598 | 325 | } |
mbed_official | 237:f3da66175598 | 326 | |
mbed_official | 237:f3da66175598 | 327 | void serial_clear(serial_t *obj) |
mbed_official | 237:f3da66175598 | 328 | { |
mbed_official | 237:f3da66175598 | 329 | UartHandle.Instance = (USART_TypeDef *)(obj->uart); |
mbed_official | 293:2a9cf2ed1474 | 330 | __HAL_UART_CLEAR_IT(&UartHandle, UART_FLAG_TC); |
mbed_official | 293:2a9cf2ed1474 | 331 | __HAL_UART_SEND_REQ(&UartHandle, UART_RXDATA_FLUSH_REQUEST); |
mbed_official | 237:f3da66175598 | 332 | } |
mbed_official | 237:f3da66175598 | 333 | |
mbed_official | 237:f3da66175598 | 334 | void serial_pinout_tx(PinName tx) |
mbed_official | 237:f3da66175598 | 335 | { |
mbed_official | 237:f3da66175598 | 336 | pinmap_pinout(tx, PinMap_UART_TX); |
mbed_official | 237:f3da66175598 | 337 | } |
mbed_official | 237:f3da66175598 | 338 | |
mbed_official | 237:f3da66175598 | 339 | void serial_break_set(serial_t *obj) |
mbed_official | 237:f3da66175598 | 340 | { |
mbed_official | 237:f3da66175598 | 341 | UartHandle.Instance = (USART_TypeDef *)(obj->uart); |
mbed_official | 237:f3da66175598 | 342 | HAL_LIN_SendBreak(&UartHandle); |
mbed_official | 237:f3da66175598 | 343 | } |
mbed_official | 237:f3da66175598 | 344 | |
mbed_official | 237:f3da66175598 | 345 | void serial_break_clear(serial_t *obj) |
mbed_official | 237:f3da66175598 | 346 | { |
mbed_official | 237:f3da66175598 | 347 | } |
mbed_official | 237:f3da66175598 | 348 | |
mbed_official | 237:f3da66175598 | 349 | #endif |