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:
Fri May 23 10:30:07 2014 +0100
Revision:
208:4557f4bb2dd5
Parent:
174:8bb9f3a33240
Child:
215:83cf97a28428
Synchronized with git revision bf99806838a107a0a72f7824356e720c46afdc63

Full URL: https://github.com/mbedmicro/mbed/commit/bf99806838a107a0a72f7824356e720c46afdc63/

[NUCLEO_F103RB] Update xxx_free() function + typo corrections

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 52:a51c77007319 1 /* mbed Microcontroller Library
mbed_official 70:c1fbde68b492 2 *******************************************************************************
mbed_official 70:c1fbde68b492 3 * Copyright (c) 2014, STMicroelectronics
mbed_official 70:c1fbde68b492 4 * All rights reserved.
mbed_official 52:a51c77007319 5 *
mbed_official 70:c1fbde68b492 6 * Redistribution and use in source and binary forms, with or without
mbed_official 70:c1fbde68b492 7 * modification, are permitted provided that the following conditions are met:
mbed_official 52:a51c77007319 8 *
mbed_official 70:c1fbde68b492 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 70:c1fbde68b492 10 * this list of conditions and the following disclaimer.
mbed_official 70:c1fbde68b492 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 70:c1fbde68b492 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 70:c1fbde68b492 13 * and/or other materials provided with the distribution.
mbed_official 70:c1fbde68b492 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 70:c1fbde68b492 15 * may be used to endorse or promote products derived from this software
mbed_official 70:c1fbde68b492 16 * without specific prior written permission.
mbed_official 52:a51c77007319 17 *
mbed_official 70:c1fbde68b492 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 70:c1fbde68b492 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 70:c1fbde68b492 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 70:c1fbde68b492 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 70:c1fbde68b492 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 70:c1fbde68b492 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 70:c1fbde68b492 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 70:c1fbde68b492 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 70:c1fbde68b492 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 70:c1fbde68b492 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 70:c1fbde68b492 28 *******************************************************************************
mbed_official 52:a51c77007319 29 */
mbed_official 52:a51c77007319 30 #include "serial_api.h"
mbed_official 174:8bb9f3a33240 31
mbed_official 174:8bb9f3a33240 32 #if DEVICE_SERIAL
mbed_official 174:8bb9f3a33240 33
mbed_official 52:a51c77007319 34 #include "cmsis.h"
mbed_official 52:a51c77007319 35 #include "pinmap.h"
mbed_official 52:a51c77007319 36 #include "error.h"
mbed_official 52:a51c77007319 37 #include <string.h>
mbed_official 52:a51c77007319 38
mbed_official 52:a51c77007319 39 static const PinMap PinMap_UART_TX[] = {
mbed_official 167:d5744491c362 40 {PA_2, UART_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
mbed_official 52:a51c77007319 41 {PA_9, UART_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
mbed_official 167:d5744491c362 42 {PB_6, UART_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 3)}, // GPIO_Remap_USART1
mbed_official 167:d5744491c362 43 {PB_10, UART_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
mbed_official 167:d5744491c362 44 {PC_10, UART_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 5)}, // GPIO_PartialRemap_USART3
mbed_official 52:a51c77007319 45 {NC, NC, 0}
mbed_official 52:a51c77007319 46 };
mbed_official 52:a51c77007319 47
mbed_official 52:a51c77007319 48 static const PinMap PinMap_UART_RX[] = {
mbed_official 167:d5744491c362 49 {PA_3, UART_2, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)},
mbed_official 52:a51c77007319 50 {PA_10, UART_1, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)},
mbed_official 167:d5744491c362 51 {PB_7, UART_1, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 3)}, // GPIO_Remap_USART1
mbed_official 167:d5744491c362 52 {PB_11, UART_3, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)},
mbed_official 167:d5744491c362 53 {PC_11, UART_3, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 5)}, // GPIO_PartialRemap_USART3
mbed_official 52:a51c77007319 54 {NC, NC, 0}
mbed_official 52:a51c77007319 55 };
mbed_official 52:a51c77007319 56
mbed_official 167:d5744491c362 57 #define UART_NUM (3)
mbed_official 52:a51c77007319 58
mbed_official 167:d5744491c362 59 static uint32_t serial_irq_ids[UART_NUM] = {0, 0, 0};
mbed_official 52:a51c77007319 60
mbed_official 52:a51c77007319 61 static uart_irq_handler irq_handler;
mbed_official 52:a51c77007319 62
mbed_official 52:a51c77007319 63 int stdio_uart_inited = 0;
mbed_official 52:a51c77007319 64 serial_t stdio_uart;
mbed_official 52:a51c77007319 65
mbed_official 56:99eb381a3269 66 static void init_usart(serial_t *obj) {
mbed_official 56:99eb381a3269 67 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 68 USART_InitTypeDef USART_InitStructure;
mbed_official 174:8bb9f3a33240 69
mbed_official 56:99eb381a3269 70 USART_Cmd(usart, DISABLE);
mbed_official 56:99eb381a3269 71
mbed_official 167:d5744491c362 72 USART_InitStructure.USART_BaudRate = obj->baudrate;
mbed_official 167:d5744491c362 73 USART_InitStructure.USART_WordLength = obj->databits;
mbed_official 167:d5744491c362 74 USART_InitStructure.USART_StopBits = obj->stopbits;
mbed_official 167:d5744491c362 75 USART_InitStructure.USART_Parity = obj->parity;
mbed_official 56:99eb381a3269 76 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
mbed_official 167:d5744491c362 77 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
mbed_official 56:99eb381a3269 78 USART_Init(usart, &USART_InitStructure);
mbed_official 174:8bb9f3a33240 79
mbed_official 56:99eb381a3269 80 USART_Cmd(usart, ENABLE);
mbed_official 56:99eb381a3269 81 }
mbed_official 56:99eb381a3269 82
mbed_official 174:8bb9f3a33240 83 void serial_init(serial_t *obj, PinName tx, PinName rx) {
mbed_official 52:a51c77007319 84 // Determine the UART to use (UART_1, UART_2, ...)
mbed_official 52:a51c77007319 85 UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
mbed_official 52:a51c77007319 86 UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
mbed_official 174:8bb9f3a33240 87
mbed_official 52:a51c77007319 88 // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
mbed_official 52:a51c77007319 89 obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
mbed_official 52:a51c77007319 90
mbed_official 52:a51c77007319 91 if (obj->uart == (UARTName)NC) {
mbed_official 52:a51c77007319 92 error("Serial pinout mapping failed");
mbed_official 52:a51c77007319 93 }
mbed_official 56:99eb381a3269 94
mbed_official 52:a51c77007319 95 // Enable USART clock
mbed_official 52:a51c77007319 96 if (obj->uart == UART_1) {
mbed_official 174:8bb9f3a33240 97 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
mbed_official 52:a51c77007319 98 }
mbed_official 52:a51c77007319 99 if (obj->uart == UART_2) {
mbed_official 174:8bb9f3a33240 100 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
mbed_official 52:a51c77007319 101 }
mbed_official 167:d5744491c362 102 if (obj->uart == UART_3) {
mbed_official 174:8bb9f3a33240 103 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
mbed_official 167:d5744491c362 104 }
mbed_official 174:8bb9f3a33240 105
mbed_official 52:a51c77007319 106 // Configure the UART pins
mbed_official 52:a51c77007319 107 pinmap_pinout(tx, PinMap_UART_TX);
mbed_official 52:a51c77007319 108 pinmap_pinout(rx, PinMap_UART_RX);
mbed_official 52:a51c77007319 109
mbed_official 52:a51c77007319 110 // Configure UART
mbed_official 52:a51c77007319 111 obj->baudrate = 9600;
mbed_official 52:a51c77007319 112 obj->databits = USART_WordLength_8b;
mbed_official 52:a51c77007319 113 obj->stopbits = USART_StopBits_1;
mbed_official 174:8bb9f3a33240 114 obj->parity = USART_Parity_No;
mbed_official 52:a51c77007319 115
mbed_official 208:4557f4bb2dd5 116 obj->pin_tx = tx;
mbed_official 208:4557f4bb2dd5 117 obj->pin_rx = rx;
mbed_official 208:4557f4bb2dd5 118
mbed_official 56:99eb381a3269 119 init_usart(obj);
mbed_official 52:a51c77007319 120
mbed_official 52:a51c77007319 121 // The index is used by irq
mbed_official 52:a51c77007319 122 if (obj->uart == UART_1) obj->index = 0;
mbed_official 52:a51c77007319 123 if (obj->uart == UART_2) obj->index = 1;
mbed_official 167:d5744491c362 124 if (obj->uart == UART_3) obj->index = 2;
mbed_official 174:8bb9f3a33240 125
mbed_official 52:a51c77007319 126 // For stdio management
mbed_official 52:a51c77007319 127 if (obj->uart == STDIO_UART) {
mbed_official 52:a51c77007319 128 stdio_uart_inited = 1;
mbed_official 52:a51c77007319 129 memcpy(&stdio_uart, obj, sizeof(serial_t));
mbed_official 52:a51c77007319 130 }
mbed_official 52:a51c77007319 131 }
mbed_official 52:a51c77007319 132
mbed_official 52:a51c77007319 133 void serial_free(serial_t *obj) {
mbed_official 208:4557f4bb2dd5 134 // Reset UART and disable clock
mbed_official 208:4557f4bb2dd5 135 if (obj->uart == UART_1) {
mbed_official 208:4557f4bb2dd5 136 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
mbed_official 208:4557f4bb2dd5 137 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
mbed_official 208:4557f4bb2dd5 138 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, DISABLE);
mbed_official 208:4557f4bb2dd5 139 }
mbed_official 208:4557f4bb2dd5 140 if (obj->uart == UART_2) {
mbed_official 208:4557f4bb2dd5 141 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
mbed_official 208:4557f4bb2dd5 142 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
mbed_official 208:4557f4bb2dd5 143 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, DISABLE);
mbed_official 208:4557f4bb2dd5 144 }
mbed_official 208:4557f4bb2dd5 145 if (obj->uart == UART_3) {
mbed_official 208:4557f4bb2dd5 146 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
mbed_official 208:4557f4bb2dd5 147 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
mbed_official 208:4557f4bb2dd5 148 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, DISABLE);
mbed_official 208:4557f4bb2dd5 149 }
mbed_official 208:4557f4bb2dd5 150
mbed_official 208:4557f4bb2dd5 151 // Configure GPIOs
mbed_official 208:4557f4bb2dd5 152 pin_function(obj->pin_tx, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0));
mbed_official 208:4557f4bb2dd5 153 pin_function(obj->pin_rx, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0));
mbed_official 208:4557f4bb2dd5 154
mbed_official 52:a51c77007319 155 serial_irq_ids[obj->index] = 0;
mbed_official 52:a51c77007319 156 }
mbed_official 52:a51c77007319 157
mbed_official 52:a51c77007319 158 void serial_baud(serial_t *obj, int baudrate) {
mbed_official 52:a51c77007319 159 obj->baudrate = baudrate;
mbed_official 56:99eb381a3269 160 init_usart(obj);
mbed_official 52:a51c77007319 161 }
mbed_official 52:a51c77007319 162
mbed_official 52:a51c77007319 163 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
mbed_official 52:a51c77007319 164 if (data_bits == 8) {
mbed_official 52:a51c77007319 165 obj->databits = USART_WordLength_8b;
mbed_official 174:8bb9f3a33240 166 } else {
mbed_official 52:a51c77007319 167 obj->databits = USART_WordLength_9b;
mbed_official 52:a51c77007319 168 }
mbed_official 52:a51c77007319 169
mbed_official 52:a51c77007319 170 switch (parity) {
mbed_official 174:8bb9f3a33240 171 case ParityOdd:
mbed_official 174:8bb9f3a33240 172 case ParityForced0:
mbed_official 174:8bb9f3a33240 173 obj->parity = USART_Parity_Odd;
mbed_official 174:8bb9f3a33240 174 break;
mbed_official 174:8bb9f3a33240 175 case ParityEven:
mbed_official 174:8bb9f3a33240 176 case ParityForced1:
mbed_official 174:8bb9f3a33240 177 obj->parity = USART_Parity_Even;
mbed_official 174:8bb9f3a33240 178 break;
mbed_official 174:8bb9f3a33240 179 default: // ParityNone
mbed_official 174:8bb9f3a33240 180 obj->parity = USART_Parity_No;
mbed_official 174:8bb9f3a33240 181 break;
mbed_official 52:a51c77007319 182 }
mbed_official 174:8bb9f3a33240 183
mbed_official 52:a51c77007319 184 if (stop_bits == 2) {
mbed_official 52:a51c77007319 185 obj->stopbits = USART_StopBits_2;
mbed_official 174:8bb9f3a33240 186 } else {
mbed_official 52:a51c77007319 187 obj->stopbits = USART_StopBits_1;
mbed_official 52:a51c77007319 188 }
mbed_official 52:a51c77007319 189
mbed_official 56:99eb381a3269 190 init_usart(obj);
mbed_official 52:a51c77007319 191 }
mbed_official 52:a51c77007319 192
mbed_official 52:a51c77007319 193 /******************************************************************************
mbed_official 52:a51c77007319 194 * INTERRUPTS HANDLING
mbed_official 52:a51c77007319 195 ******************************************************************************/
mbed_official 52:a51c77007319 196
mbed_official 52:a51c77007319 197 // not api
mbed_official 55:3b765ca737a5 198 static void uart_irq(USART_TypeDef* usart, int id) {
mbed_official 55:3b765ca737a5 199 if (serial_irq_ids[id] != 0) {
mbed_official 55:3b765ca737a5 200 if (USART_GetITStatus(usart, USART_IT_TC) != RESET) {
mbed_official 55:3b765ca737a5 201 irq_handler(serial_irq_ids[id], TxIrq);
mbed_official 55:3b765ca737a5 202 USART_ClearITPendingBit(usart, USART_IT_TC);
mbed_official 52:a51c77007319 203 }
mbed_official 52:a51c77007319 204 if (USART_GetITStatus(usart, USART_IT_RXNE) != RESET) {
mbed_official 55:3b765ca737a5 205 irq_handler(serial_irq_ids[id], RxIrq);
mbed_official 55:3b765ca737a5 206 USART_ClearITPendingBit(usart, USART_IT_RXNE);
mbed_official 52:a51c77007319 207 }
mbed_official 52:a51c77007319 208 }
mbed_official 52:a51c77007319 209 }
mbed_official 52:a51c77007319 210
mbed_official 167:d5744491c362 211 static void uart1_irq(void) {
mbed_official 167:d5744491c362 212 uart_irq((USART_TypeDef*)UART_1, 0);
mbed_official 167:d5744491c362 213 }
mbed_official 167:d5744491c362 214 static void uart2_irq(void) {
mbed_official 167:d5744491c362 215 uart_irq((USART_TypeDef*)UART_2, 1);
mbed_official 167:d5744491c362 216 }
mbed_official 167:d5744491c362 217 static void uart3_irq(void) {
mbed_official 167:d5744491c362 218 uart_irq((USART_TypeDef*)UART_3, 2);
mbed_official 167:d5744491c362 219 }
mbed_official 52:a51c77007319 220
mbed_official 52:a51c77007319 221 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) {
mbed_official 52:a51c77007319 222 irq_handler = handler;
mbed_official 52:a51c77007319 223 serial_irq_ids[obj->index] = id;
mbed_official 52:a51c77007319 224 }
mbed_official 52:a51c77007319 225
mbed_official 52:a51c77007319 226 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
mbed_official 52:a51c77007319 227 IRQn_Type irq_n = (IRQn_Type)0;
mbed_official 52:a51c77007319 228 uint32_t vector = 0;
mbed_official 52:a51c77007319 229 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 230
mbed_official 52:a51c77007319 231 if (obj->uart == UART_1) {
mbed_official 174:8bb9f3a33240 232 irq_n = USART1_IRQn;
mbed_official 174:8bb9f3a33240 233 vector = (uint32_t)&uart1_irq;
mbed_official 52:a51c77007319 234 }
mbed_official 174:8bb9f3a33240 235
mbed_official 52:a51c77007319 236 if (obj->uart == UART_2) {
mbed_official 174:8bb9f3a33240 237 irq_n = USART2_IRQn;
mbed_official 174:8bb9f3a33240 238 vector = (uint32_t)&uart2_irq;
mbed_official 52:a51c77007319 239 }
mbed_official 167:d5744491c362 240
mbed_official 167:d5744491c362 241 if (obj->uart == UART_3) {
mbed_official 174:8bb9f3a33240 242 irq_n = USART3_IRQn;
mbed_official 174:8bb9f3a33240 243 vector = (uint32_t)&uart3_irq;
mbed_official 167:d5744491c362 244 }
mbed_official 174:8bb9f3a33240 245
mbed_official 52:a51c77007319 246 if (enable) {
mbed_official 174:8bb9f3a33240 247
mbed_official 52:a51c77007319 248 if (irq == RxIrq) {
mbed_official 52:a51c77007319 249 USART_ITConfig(usart, USART_IT_RXNE, ENABLE);
mbed_official 174:8bb9f3a33240 250 } else { // TxIrq
mbed_official 55:3b765ca737a5 251 USART_ITConfig(usart, USART_IT_TC, ENABLE);
mbed_official 174:8bb9f3a33240 252 }
mbed_official 174:8bb9f3a33240 253
mbed_official 52:a51c77007319 254 NVIC_SetVector(irq_n, vector);
mbed_official 52:a51c77007319 255 NVIC_EnableIRQ(irq_n);
mbed_official 174:8bb9f3a33240 256
mbed_official 52:a51c77007319 257 } else { // disable
mbed_official 174:8bb9f3a33240 258
mbed_official 52:a51c77007319 259 int all_disabled = 0;
mbed_official 174:8bb9f3a33240 260
mbed_official 52:a51c77007319 261 if (irq == RxIrq) {
mbed_official 52:a51c77007319 262 USART_ITConfig(usart, USART_IT_RXNE, DISABLE);
mbed_official 52:a51c77007319 263 // Check if TxIrq is disabled too
mbed_official 52:a51c77007319 264 if ((usart->CR1 & USART_CR1_TXEIE) == 0) all_disabled = 1;
mbed_official 174:8bb9f3a33240 265 } else { // TxIrq
mbed_official 52:a51c77007319 266 USART_ITConfig(usart, USART_IT_TXE, DISABLE);
mbed_official 52:a51c77007319 267 // Check if RxIrq is disabled too
mbed_official 174:8bb9f3a33240 268 if ((usart->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
mbed_official 52:a51c77007319 269 }
mbed_official 174:8bb9f3a33240 270
mbed_official 52:a51c77007319 271 if (all_disabled) NVIC_DisableIRQ(irq_n);
mbed_official 174:8bb9f3a33240 272
mbed_official 174:8bb9f3a33240 273 }
mbed_official 52:a51c77007319 274 }
mbed_official 52:a51c77007319 275
mbed_official 52:a51c77007319 276 /******************************************************************************
mbed_official 52:a51c77007319 277 * READ/WRITE
mbed_official 52:a51c77007319 278 ******************************************************************************/
mbed_official 52:a51c77007319 279
mbed_official 52:a51c77007319 280 int serial_getc(serial_t *obj) {
mbed_official 52:a51c77007319 281 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 282 while (!serial_readable(obj));
mbed_official 52:a51c77007319 283 return (int)(USART_ReceiveData(usart));
mbed_official 52:a51c77007319 284 }
mbed_official 52:a51c77007319 285
mbed_official 52:a51c77007319 286 void serial_putc(serial_t *obj, int c) {
mbed_official 52:a51c77007319 287 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 288 while (!serial_writable(obj));
mbed_official 52:a51c77007319 289 USART_SendData(usart, (uint16_t)c);
mbed_official 52:a51c77007319 290 }
mbed_official 52:a51c77007319 291
mbed_official 52:a51c77007319 292 int serial_readable(serial_t *obj) {
mbed_official 52:a51c77007319 293 int status;
mbed_official 52:a51c77007319 294 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 295 // Check if data is received
mbed_official 52:a51c77007319 296 status = ((USART_GetFlagStatus(usart, USART_FLAG_RXNE) != RESET) ? 1 : 0);
mbed_official 52:a51c77007319 297 return status;
mbed_official 52:a51c77007319 298 }
mbed_official 52:a51c77007319 299
mbed_official 52:a51c77007319 300 int serial_writable(serial_t *obj) {
mbed_official 52:a51c77007319 301 int status;
mbed_official 52:a51c77007319 302 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 303 // Check if data is transmitted
mbed_official 52:a51c77007319 304 status = ((USART_GetFlagStatus(usart, USART_FLAG_TXE) != RESET) ? 1 : 0);
mbed_official 52:a51c77007319 305 return status;
mbed_official 52:a51c77007319 306 }
mbed_official 52:a51c77007319 307
mbed_official 52:a51c77007319 308 void serial_clear(serial_t *obj) {
mbed_official 52:a51c77007319 309 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 310 USART_ClearFlag(usart, USART_FLAG_TXE);
mbed_official 52:a51c77007319 311 USART_ClearFlag(usart, USART_FLAG_RXNE);
mbed_official 52:a51c77007319 312 }
mbed_official 52:a51c77007319 313
mbed_official 52:a51c77007319 314 void serial_pinout_tx(PinName tx) {
mbed_official 52:a51c77007319 315 pinmap_pinout(tx, PinMap_UART_TX);
mbed_official 52:a51c77007319 316 }
mbed_official 52:a51c77007319 317
mbed_official 52:a51c77007319 318 void serial_break_set(serial_t *obj) {
mbed_official 52:a51c77007319 319 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 52:a51c77007319 320 USART_SendBreak(usart);
mbed_official 52:a51c77007319 321 }
mbed_official 52:a51c77007319 322
mbed_official 52:a51c77007319 323 void serial_break_clear(serial_t *obj) {
mbed_official 52:a51c77007319 324 }
mbed_official 174:8bb9f3a33240 325
mbed_official 174:8bb9f3a33240 326 #endif