mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon Nov 03 10:30:07 2014 +0000
Revision:
381:5460fc57b6e4
Synchronized with git revision 02478cd1f27fc7b9643486472635eb515b2bca81

Full URL: https://github.com/mbedmicro/mbed/commit/02478cd1f27fc7b9643486472635eb515b2bca81/

Target: LPC1549 - Fix serial interrupt issues (issue report #616)

Who changed what in which revision?

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