mbed library sources modified for open wear

Dependents:   openwear-lifelogger-example

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Thu Aug 21 10:45:06 2014 +0100
Revision:
294:2a9cf2ed1474
Parent:
242:7074e42da0b2
Synchronized with git revision 0f8a06d8186efc5ba0f63d93600e113de217fb45

Full URL: https://github.com/mbedmicro/mbed/commit/0f8a06d8186efc5ba0f63d93600e113de217fb45/

[NUCLEOs] Improvement of Serial hal

Who changed what in which revision?

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