mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Tue Jul 29 19:00:07 2014 +0100
Revision:
268:402bcc0c870b
Parent:
237:f3da66175598
Child:
293:2a9cf2ed1474
Synchronized with git revision 490d1a6606b3138f165c5edf2f2370ca616587c0

Full URL: https://github.com/mbedmicro/mbed/commit/490d1a6606b3138f165c5edf2f2370ca616587c0/

[LPC1114] Sleep fix + some device.h settings

Who changed what in which revision?

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