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:
Mon Sep 28 13:00:10 2015 +0100
Revision:
631:ff681937ffd8
Parent:
619:034e698bc035
Synchronized with git revision 1be56c8134a3ed6004f149a84f658de45dda6a5c

Full URL: https://github.com/mbedmicro/mbed/commit/1be56c8134a3ed6004f149a84f658de45dda6a5c/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 558:0880f51c4036 1 /* mbed Microcontroller Library
mbed_official 558:0880f51c4036 2 *******************************************************************************
mbed_official 558:0880f51c4036 3 * Copyright (c) 2015 WIZnet Co.,Ltd. All rights reserved.
mbed_official 558:0880f51c4036 4 * All rights reserved.
mbed_official 558:0880f51c4036 5 *
mbed_official 558:0880f51c4036 6 * Redistribution and use in source and binary forms, with or without
mbed_official 558:0880f51c4036 7 * modification, are permitted provided that the following conditions are met:
mbed_official 558:0880f51c4036 8 *
mbed_official 558:0880f51c4036 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 558:0880f51c4036 10 * this list of conditions and the following disclaimer.
mbed_official 558:0880f51c4036 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 558:0880f51c4036 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 558:0880f51c4036 13 * and/or other materials provided with the distribution.
mbed_official 558:0880f51c4036 14 * 3. Neither the name of ARM Limited nor the names of its contributors
mbed_official 558:0880f51c4036 15 * may be used to endorse or promote products derived from this software
mbed_official 558:0880f51c4036 16 * without specific prior written permission.
mbed_official 558:0880f51c4036 17 *
mbed_official 558:0880f51c4036 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 558:0880f51c4036 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 558:0880f51c4036 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 558:0880f51c4036 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 558:0880f51c4036 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 558:0880f51c4036 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 558:0880f51c4036 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 558:0880f51c4036 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 558:0880f51c4036 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 558:0880f51c4036 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 558:0880f51c4036 28 *******************************************************************************
mbed_official 558:0880f51c4036 29 */
mbed_official 558:0880f51c4036 30
mbed_official 558:0880f51c4036 31 #include "mbed_assert.h"
mbed_official 558:0880f51c4036 32 #include "serial_api.h"
mbed_official 558:0880f51c4036 33
mbed_official 558:0880f51c4036 34 #if DEVICE_SERIAL
mbed_official 558:0880f51c4036 35
mbed_official 558:0880f51c4036 36 #include "cmsis.h"
mbed_official 558:0880f51c4036 37 #include "pinmap.h"
mbed_official 558:0880f51c4036 38 #include <string.h>
mbed_official 558:0880f51c4036 39 #include "PeripheralPins.h"
mbed_official 619:034e698bc035 40 #include "W7500x_uart.h"
mbed_official 558:0880f51c4036 41
mbed_official 631:ff681937ffd8 42 #define UART_NUM (3)
mbed_official 558:0880f51c4036 43
mbed_official 631:ff681937ffd8 44 static uint32_t serial_irq_ids[UART_NUM] = {0, 0, 0};
mbed_official 558:0880f51c4036 45
mbed_official 558:0880f51c4036 46
mbed_official 558:0880f51c4036 47 static uart_irq_handler irq_handler;
mbed_official 558:0880f51c4036 48 static UART_TypeDef *UART;
mbed_official 558:0880f51c4036 49
mbed_official 558:0880f51c4036 50
mbed_official 558:0880f51c4036 51 UART_InitTypeDef UART_InitStructure;
mbed_official 558:0880f51c4036 52
mbed_official 558:0880f51c4036 53 int stdio_uart_inited = 0;
mbed_official 558:0880f51c4036 54 serial_t stdio_uart;
mbed_official 558:0880f51c4036 55
mbed_official 558:0880f51c4036 56 static void init_uart(serial_t *obj)
mbed_official 558:0880f51c4036 57 {
mbed_official 631:ff681937ffd8 58 if(obj->index == 2) // For UART2, It is simple UART.
mbed_official 631:ff681937ffd8 59 {
mbed_official 631:ff681937ffd8 60 SystemCoreClockUpdate();
mbed_official 631:ff681937ffd8 61 //S_UART_Init(obj->baudrate);
mbed_official 631:ff681937ffd8 62 S_UART_SetCTRL((S_UART_CTRL_RX_EN|S_UART_CTRL_TX_EN), DISABLE);
mbed_official 631:ff681937ffd8 63 S_UART_SetBaud(obj->baudrate);
mbed_official 631:ff681937ffd8 64
mbed_official 631:ff681937ffd8 65 if(obj->pin_rx == NC)
mbed_official 631:ff681937ffd8 66 {
mbed_official 631:ff681937ffd8 67 S_UART_SetCTRL(S_UART_CTRL_TX_EN, ENABLE);
mbed_official 631:ff681937ffd8 68 }
mbed_official 631:ff681937ffd8 69 else if(obj->pin_tx == NC)
mbed_official 631:ff681937ffd8 70 {
mbed_official 631:ff681937ffd8 71 S_UART_SetCTRL(S_UART_CTRL_RX_EN, ENABLE);
mbed_official 631:ff681937ffd8 72 }
mbed_official 631:ff681937ffd8 73 else
mbed_official 631:ff681937ffd8 74 {
mbed_official 631:ff681937ffd8 75 S_UART_SetCTRL((S_UART_CTRL_TX_EN|S_UART_CTRL_RX_EN),ENABLE);
mbed_official 631:ff681937ffd8 76 }
mbed_official 631:ff681937ffd8 77 }
mbed_official 631:ff681937ffd8 78 else // For UART0 and UART1.
mbed_official 631:ff681937ffd8 79 {
mbed_official 631:ff681937ffd8 80 UART = (UART_TypeDef *)(obj->uart);
mbed_official 631:ff681937ffd8 81 UART_InitStructure.UART_BaudRate = obj->baudrate;
mbed_official 631:ff681937ffd8 82 UART_InitStructure.UART_WordLength = obj->databits;
mbed_official 631:ff681937ffd8 83 UART_InitStructure.UART_StopBits = obj->stopbits;
mbed_official 631:ff681937ffd8 84 UART_InitStructure.UART_Parity = obj->parity;
mbed_official 631:ff681937ffd8 85 UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
mbed_official 558:0880f51c4036 86
mbed_official 558:0880f51c4036 87
mbed_official 631:ff681937ffd8 88 if (obj->pin_rx == NC) {
mbed_official 631:ff681937ffd8 89 UART_InitStructure.UART_Mode = UART_Mode_Tx;
mbed_official 631:ff681937ffd8 90 } else if (obj->pin_tx == NC) {
mbed_official 631:ff681937ffd8 91 UART_InitStructure.UART_Mode = UART_Mode_Rx;
mbed_official 631:ff681937ffd8 92 } else {
mbed_official 631:ff681937ffd8 93 UART_InitStructure.UART_Mode = (UART_Mode_Rx | UART_Mode_Tx);
mbed_official 631:ff681937ffd8 94 }
mbed_official 631:ff681937ffd8 95
mbed_official 631:ff681937ffd8 96 UART_Init(UART,&UART_InitStructure);
mbed_official 558:0880f51c4036 97 }
mbed_official 558:0880f51c4036 98 }
mbed_official 558:0880f51c4036 99
mbed_official 558:0880f51c4036 100 void serial_init(serial_t *obj, PinName tx, PinName rx)
mbed_official 558:0880f51c4036 101 {
mbed_official 558:0880f51c4036 102 // Determine the UART to use (UART_1, UART_2, ...)
mbed_official 558:0880f51c4036 103 UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
mbed_official 558:0880f51c4036 104 UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
mbed_official 558:0880f51c4036 105
mbed_official 558:0880f51c4036 106 // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
mbed_official 558:0880f51c4036 107 obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
mbed_official 558:0880f51c4036 108
mbed_official 558:0880f51c4036 109 MBED_ASSERT(obj->uart != (UARTName)NC);
mbed_official 558:0880f51c4036 110
mbed_official 558:0880f51c4036 111 // Enable USART clock
mbed_official 558:0880f51c4036 112 if (obj->uart == UART_0) {
mbed_official 558:0880f51c4036 113 obj->index = 0;
mbed_official 558:0880f51c4036 114 }
mbed_official 558:0880f51c4036 115
mbed_official 558:0880f51c4036 116 if (obj->uart == UART_1) {
mbed_official 558:0880f51c4036 117 obj->index = 1;
mbed_official 558:0880f51c4036 118 }
mbed_official 558:0880f51c4036 119
mbed_official 631:ff681937ffd8 120 if (obj->uart == UART_2) {
mbed_official 631:ff681937ffd8 121 obj->index = 2;
mbed_official 631:ff681937ffd8 122 }
mbed_official 631:ff681937ffd8 123
mbed_official 558:0880f51c4036 124 // Configure the UART pins
mbed_official 558:0880f51c4036 125 pinmap_pinout(tx, PinMap_UART_TX);
mbed_official 558:0880f51c4036 126 pinmap_pinout(rx, PinMap_UART_RX);
mbed_official 558:0880f51c4036 127 if (tx != NC) {
mbed_official 558:0880f51c4036 128 pin_mode(tx, PullUp);
mbed_official 558:0880f51c4036 129 }
mbed_official 558:0880f51c4036 130 if (rx != NC) {
mbed_official 558:0880f51c4036 131 pin_mode(rx, PullUp);
mbed_official 558:0880f51c4036 132 }
mbed_official 558:0880f51c4036 133
mbed_official 558:0880f51c4036 134 // Configure UART
mbed_official 558:0880f51c4036 135 obj->baudrate = 9600;
mbed_official 558:0880f51c4036 136 obj->databits = UART_WordLength_8b;
mbed_official 558:0880f51c4036 137 obj->stopbits = UART_StopBits_1;
mbed_official 558:0880f51c4036 138 obj->parity = UART_Parity_No;
mbed_official 558:0880f51c4036 139
mbed_official 558:0880f51c4036 140 obj->pin_tx = tx;
mbed_official 558:0880f51c4036 141 obj->pin_rx = rx;
mbed_official 558:0880f51c4036 142
mbed_official 558:0880f51c4036 143
mbed_official 558:0880f51c4036 144 init_uart(obj);
mbed_official 558:0880f51c4036 145
mbed_official 558:0880f51c4036 146 // For stdio management
mbed_official 558:0880f51c4036 147 if (obj->uart == STDIO_UART) {
mbed_official 558:0880f51c4036 148 stdio_uart_inited = 1;
mbed_official 558:0880f51c4036 149 memcpy(&stdio_uart, obj, sizeof(serial_t));
mbed_official 558:0880f51c4036 150 }
mbed_official 558:0880f51c4036 151 }
mbed_official 558:0880f51c4036 152
mbed_official 558:0880f51c4036 153 void serial_free(serial_t *obj)
mbed_official 558:0880f51c4036 154 {
mbed_official 558:0880f51c4036 155 // Reset UART and disable clock
mbed_official 558:0880f51c4036 156 if (obj->uart == UART_0) {
mbed_official 558:0880f51c4036 157 }
mbed_official 558:0880f51c4036 158
mbed_official 558:0880f51c4036 159 if (obj->uart == UART_1) {
mbed_official 558:0880f51c4036 160 }
mbed_official 631:ff681937ffd8 161 if (obj->uart == UART_2) {
mbed_official 631:ff681937ffd8 162 }
mbed_official 558:0880f51c4036 163
mbed_official 558:0880f51c4036 164 serial_irq_ids[obj->index] = 0;
mbed_official 558:0880f51c4036 165 }
mbed_official 558:0880f51c4036 166
mbed_official 558:0880f51c4036 167 void serial_baud(serial_t *obj, int baudrate)
mbed_official 558:0880f51c4036 168 {
mbed_official 558:0880f51c4036 169 obj->baudrate = baudrate;
mbed_official 558:0880f51c4036 170 init_uart(obj);
mbed_official 558:0880f51c4036 171 }
mbed_official 558:0880f51c4036 172
mbed_official 558:0880f51c4036 173 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
mbed_official 558:0880f51c4036 174 {
mbed_official 558:0880f51c4036 175 if (data_bits == 7) {
mbed_official 558:0880f51c4036 176 obj->databits = UART_WordLength_7b;
mbed_official 558:0880f51c4036 177 } else {
mbed_official 558:0880f51c4036 178 obj->databits = UART_WordLength_8b;
mbed_official 558:0880f51c4036 179 }
mbed_official 558:0880f51c4036 180
mbed_official 558:0880f51c4036 181 switch (parity) {
mbed_official 558:0880f51c4036 182 case ParityOdd:
mbed_official 558:0880f51c4036 183 case ParityForced0:
mbed_official 558:0880f51c4036 184 obj->parity = UART_Parity_Odd;
mbed_official 558:0880f51c4036 185 break;
mbed_official 558:0880f51c4036 186 case ParityEven:
mbed_official 558:0880f51c4036 187 case ParityForced1:
mbed_official 558:0880f51c4036 188 obj->parity = UART_Parity_Even;
mbed_official 558:0880f51c4036 189 break;
mbed_official 558:0880f51c4036 190 default: // ParityNone
mbed_official 558:0880f51c4036 191 obj->parity = UART_Parity_No;
mbed_official 558:0880f51c4036 192 break;
mbed_official 558:0880f51c4036 193 }
mbed_official 558:0880f51c4036 194
mbed_official 558:0880f51c4036 195 if (stop_bits == 2) {
mbed_official 558:0880f51c4036 196 obj->stopbits = UART_StopBits_2;
mbed_official 558:0880f51c4036 197 } else {
mbed_official 558:0880f51c4036 198 obj->stopbits = UART_StopBits_1;
mbed_official 558:0880f51c4036 199 }
mbed_official 558:0880f51c4036 200
mbed_official 558:0880f51c4036 201 init_uart(obj);
mbed_official 558:0880f51c4036 202 }
mbed_official 558:0880f51c4036 203
mbed_official 558:0880f51c4036 204 /******************************************************************************
mbed_official 558:0880f51c4036 205 * INTERRUPTS HANDLING
mbed_official 558:0880f51c4036 206 ******************************************************************************/
mbed_official 558:0880f51c4036 207
mbed_official 558:0880f51c4036 208 static void uart_irq(UARTName name, int id)
mbed_official 558:0880f51c4036 209 {
mbed_official 558:0880f51c4036 210 UART = (UART_TypeDef *)name;
mbed_official 558:0880f51c4036 211 if (serial_irq_ids[id] != 0) {
mbed_official 558:0880f51c4036 212 if( UART_GetITStatus(UART,UART_IT_FLAG_TXI) != RESET ){
mbed_official 558:0880f51c4036 213 irq_handler(serial_irq_ids[id], TxIrq);
mbed_official 558:0880f51c4036 214 UART_ClearITPendingBit(UART,UART_IT_FLAG_TXI);
mbed_official 558:0880f51c4036 215 }
mbed_official 558:0880f51c4036 216 if( UART_GetITStatus(UART,UART_IT_FLAG_RXI) != RESET ){
mbed_official 558:0880f51c4036 217 irq_handler(serial_irq_ids[id], RxIrq);
mbed_official 558:0880f51c4036 218 }
mbed_official 558:0880f51c4036 219 }
mbed_official 558:0880f51c4036 220 }
mbed_official 558:0880f51c4036 221
mbed_official 631:ff681937ffd8 222 static void uart2_irq()
mbed_official 631:ff681937ffd8 223 {
mbed_official 631:ff681937ffd8 224 if(serial_irq_ids[2] != 0){
mbed_official 631:ff681937ffd8 225 if( S_UART_GetITStatus(S_UART_INTSTATUS_TXI) != RESET ){
mbed_official 631:ff681937ffd8 226 S_UART_ClearITPendingBit(S_UART_INTSTATUS_TXI);
mbed_official 631:ff681937ffd8 227 irq_handler(serial_irq_ids[2], TxIrq);
mbed_official 631:ff681937ffd8 228 }
mbed_official 631:ff681937ffd8 229 if( S_UART_GetITStatus(S_UART_INTSTATUS_RXI) != RESET ) {
mbed_official 631:ff681937ffd8 230 S_UART_ClearITPendingBit(S_UART_INTSTATUS_RXI);
mbed_official 631:ff681937ffd8 231 irq_handler(serial_irq_ids[2], RxIrq);
mbed_official 631:ff681937ffd8 232 }
mbed_official 631:ff681937ffd8 233 }
mbed_official 631:ff681937ffd8 234 }
mbed_official 631:ff681937ffd8 235
mbed_official 558:0880f51c4036 236 #ifdef __cplusplus
mbed_official 558:0880f51c4036 237 extern "C"{
mbed_official 558:0880f51c4036 238 #endif
mbed_official 558:0880f51c4036 239 void UART0_Handler()
mbed_official 558:0880f51c4036 240 {
mbed_official 558:0880f51c4036 241 uart_irq(UART_0, 0);
mbed_official 558:0880f51c4036 242 }
mbed_official 558:0880f51c4036 243
mbed_official 558:0880f51c4036 244 void UART1_Handler()
mbed_official 558:0880f51c4036 245 {
mbed_official 558:0880f51c4036 246 uart_irq(UART_1, 1);
mbed_official 558:0880f51c4036 247 }
mbed_official 631:ff681937ffd8 248
mbed_official 631:ff681937ffd8 249 void UART2_Handler()
mbed_official 631:ff681937ffd8 250 {
mbed_official 631:ff681937ffd8 251 uart2_irq();
mbed_official 631:ff681937ffd8 252 }
mbed_official 558:0880f51c4036 253 #ifdef __cplusplus
mbed_official 558:0880f51c4036 254 }
mbed_official 558:0880f51c4036 255 #endif
mbed_official 558:0880f51c4036 256
mbed_official 558:0880f51c4036 257
mbed_official 558:0880f51c4036 258 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
mbed_official 558:0880f51c4036 259 {
mbed_official 558:0880f51c4036 260 irq_handler = handler;
mbed_official 558:0880f51c4036 261 serial_irq_ids[obj->index] = id;
mbed_official 558:0880f51c4036 262 }
mbed_official 558:0880f51c4036 263
mbed_official 558:0880f51c4036 264 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
mbed_official 558:0880f51c4036 265 {
mbed_official 558:0880f51c4036 266 IRQn_Type irq_n = (IRQn_Type)0;
mbed_official 558:0880f51c4036 267
mbed_official 558:0880f51c4036 268
mbed_official 631:ff681937ffd8 269 if (obj->uart == UART_2)
mbed_official 631:ff681937ffd8 270 {
mbed_official 631:ff681937ffd8 271 irq_n = UART2_IRQn;
mbed_official 558:0880f51c4036 272
mbed_official 631:ff681937ffd8 273 if (enable){
mbed_official 631:ff681937ffd8 274 if (irq == RxIrq){
mbed_official 631:ff681937ffd8 275 S_UART_ITConfig(S_UART_CTRL_RXI,ENABLE);
mbed_official 631:ff681937ffd8 276 } else {
mbed_official 631:ff681937ffd8 277 S_UART_ITConfig(S_UART_CTRL_TXI,ENABLE);
mbed_official 631:ff681937ffd8 278 }
mbed_official 631:ff681937ffd8 279 NVIC_ClearPendingIRQ(irq_n);
mbed_official 631:ff681937ffd8 280 NVIC_EnableIRQ(irq_n);
mbed_official 631:ff681937ffd8 281 } else { // disable
mbed_official 631:ff681937ffd8 282 S_UART_ITConfig((S_UART_CTRL_RXI|S_UART_CTRL_TXI),DISABLE);
mbed_official 631:ff681937ffd8 283 NVIC_DisableIRQ(irq_n);
mbed_official 631:ff681937ffd8 284 }
mbed_official 558:0880f51c4036 285 }
mbed_official 631:ff681937ffd8 286 else
mbed_official 631:ff681937ffd8 287 {
mbed_official 631:ff681937ffd8 288 UART = (UART_TypeDef *)(obj->uart);
mbed_official 631:ff681937ffd8 289
mbed_official 631:ff681937ffd8 290 if (obj->uart == UART_0) {
mbed_official 631:ff681937ffd8 291 irq_n = UART0_IRQn;
mbed_official 558:0880f51c4036 292 }
mbed_official 558:0880f51c4036 293
mbed_official 631:ff681937ffd8 294 if (obj->uart == UART_1) {
mbed_official 631:ff681937ffd8 295 irq_n = UART1_IRQn;
mbed_official 631:ff681937ffd8 296 }
mbed_official 631:ff681937ffd8 297
mbed_official 631:ff681937ffd8 298 if (enable) {
mbed_official 631:ff681937ffd8 299 if (irq == RxIrq) {
mbed_official 631:ff681937ffd8 300 UART_ITConfig(UART,UART_IT_FLAG_RXI,ENABLE);
mbed_official 631:ff681937ffd8 301 } else { // TxIrq
mbed_official 631:ff681937ffd8 302 UART_ITConfig(UART,UART_IT_FLAG_TXI,ENABLE);
mbed_official 631:ff681937ffd8 303 }
mbed_official 631:ff681937ffd8 304
mbed_official 631:ff681937ffd8 305 NVIC_ClearPendingIRQ(irq_n);
mbed_official 631:ff681937ffd8 306 NVIC_EnableIRQ(irq_n);
mbed_official 631:ff681937ffd8 307 } else { // disable
mbed_official 631:ff681937ffd8 308 UART_ITConfig(UART,(UART_IT_FLAG_RXI|UART_IT_FLAG_TXI),DISABLE);
mbed_official 631:ff681937ffd8 309 NVIC_DisableIRQ(irq_n);
mbed_official 631:ff681937ffd8 310 }
mbed_official 558:0880f51c4036 311 }
mbed_official 558:0880f51c4036 312 }
mbed_official 558:0880f51c4036 313
mbed_official 558:0880f51c4036 314 /******************************************************************************
mbed_official 558:0880f51c4036 315 * READ/WRITE
mbed_official 558:0880f51c4036 316 ******************************************************************************/
mbed_official 558:0880f51c4036 317
mbed_official 558:0880f51c4036 318 int serial_getc(serial_t *obj)
mbed_official 558:0880f51c4036 319 {
mbed_official 631:ff681937ffd8 320 if (obj->uart == UART_2)
mbed_official 631:ff681937ffd8 321 {
mbed_official 631:ff681937ffd8 322 S_UART_TypeDef *uart = (S_UART_TypeDef *)(obj->uart);
mbed_official 558:0880f51c4036 323
mbed_official 631:ff681937ffd8 324 while( (uart->STATE & S_UART_STATE_RX_BUF_FULL) == 0 );
mbed_official 631:ff681937ffd8 325 return (uint16_t)(uart->DATA);
mbed_official 631:ff681937ffd8 326 }
mbed_official 631:ff681937ffd8 327 else
mbed_official 631:ff681937ffd8 328 {
mbed_official 631:ff681937ffd8 329 UART_TypeDef *uart = (UART_TypeDef *)(obj->uart);
mbed_official 631:ff681937ffd8 330 while(uart->FR & UART_FR_RXFE);
mbed_official 631:ff681937ffd8 331
mbed_official 631:ff681937ffd8 332 return (uart->DR & 0xFF);
mbed_official 631:ff681937ffd8 333 }
mbed_official 558:0880f51c4036 334 }
mbed_official 558:0880f51c4036 335
mbed_official 558:0880f51c4036 336 void serial_putc(serial_t *obj, int c)
mbed_official 558:0880f51c4036 337 {
mbed_official 631:ff681937ffd8 338 if (obj->uart == UART_2)
mbed_official 631:ff681937ffd8 339 {
mbed_official 631:ff681937ffd8 340 S_UART_TypeDef *uart = (S_UART_TypeDef *)(obj->uart);
mbed_official 558:0880f51c4036 341
mbed_official 631:ff681937ffd8 342 while(uart->STATE & S_UART_STATE_TX_BUF_FULL);
mbed_official 631:ff681937ffd8 343 uart->DATA = (uint32_t)(c & (uint16_t)0xFF);
mbed_official 631:ff681937ffd8 344 }
mbed_official 631:ff681937ffd8 345 else
mbed_official 631:ff681937ffd8 346 {
mbed_official 631:ff681937ffd8 347 UART_TypeDef *uart = (UART_TypeDef *)(obj->uart);
mbed_official 631:ff681937ffd8 348
mbed_official 631:ff681937ffd8 349 uart->DR = (uint32_t)(c & (uint16_t)0xFF);
mbed_official 631:ff681937ffd8 350 while(uart->FR & UART_FR_BUSY);
mbed_official 631:ff681937ffd8 351 }
mbed_official 558:0880f51c4036 352 }
mbed_official 558:0880f51c4036 353
mbed_official 558:0880f51c4036 354 int serial_readable(serial_t *obj)
mbed_official 558:0880f51c4036 355 {
mbed_official 558:0880f51c4036 356 int status;
mbed_official 631:ff681937ffd8 357
mbed_official 631:ff681937ffd8 358 if (obj->uart == UART_2)
mbed_official 631:ff681937ffd8 359 {
mbed_official 631:ff681937ffd8 360 S_UART_TypeDef *uart = (S_UART_TypeDef *)(obj->uart);
mbed_official 631:ff681937ffd8 361 status = ((uart->STATE & S_UART_STATE_RX_BUF_FULL) ? 1 : 0);
mbed_official 631:ff681937ffd8 362 }
mbed_official 631:ff681937ffd8 363 else
mbed_official 631:ff681937ffd8 364 {
mbed_official 631:ff681937ffd8 365 UART_TypeDef *uart = (UART_TypeDef *)(obj->uart);
mbed_official 631:ff681937ffd8 366 // Check if data is received
mbed_official 631:ff681937ffd8 367 status = ((uart->FR & UART_FR_RXFE) ? 0: 1);
mbed_official 631:ff681937ffd8 368 }
mbed_official 631:ff681937ffd8 369
mbed_official 558:0880f51c4036 370 return status;
mbed_official 558:0880f51c4036 371 }
mbed_official 558:0880f51c4036 372
mbed_official 558:0880f51c4036 373 int serial_writable(serial_t *obj)
mbed_official 558:0880f51c4036 374 {
mbed_official 558:0880f51c4036 375 int status;
mbed_official 631:ff681937ffd8 376
mbed_official 631:ff681937ffd8 377 if (obj->uart == UART_2)
mbed_official 631:ff681937ffd8 378 {
mbed_official 631:ff681937ffd8 379 S_UART_TypeDef *uart = (S_UART_TypeDef *)(obj->uart);
mbed_official 631:ff681937ffd8 380 status = ((uart->STATE & S_UART_STATE_TX_BUF_FULL) ? 0 : 1);
mbed_official 631:ff681937ffd8 381 }
mbed_official 631:ff681937ffd8 382 else
mbed_official 631:ff681937ffd8 383 {
mbed_official 631:ff681937ffd8 384 UART_TypeDef *uart = (UART_TypeDef *)(obj->uart);
mbed_official 631:ff681937ffd8 385 // Check if data is transmitted
mbed_official 631:ff681937ffd8 386 status = ((uart->FR & UART_FR_BUSY) ? 0: 1);
mbed_official 631:ff681937ffd8 387 }
mbed_official 558:0880f51c4036 388 return status;
mbed_official 558:0880f51c4036 389 }
mbed_official 558:0880f51c4036 390
mbed_official 558:0880f51c4036 391 void serial_clear(serial_t *obj)
mbed_official 558:0880f51c4036 392 {
mbed_official 558:0880f51c4036 393 // UartHandle.Instance = (USART_TypeDef *)(obj->uart);
mbed_official 558:0880f51c4036 394 // __HAL_UART_CLEAR_IT(&UartHandle, UART_FLAG_TC);
mbed_official 558:0880f51c4036 395 // __HAL_UART_SEND_REQ(&UartHandle, UART_RXDATA_FLUSH_REQUEST);
mbed_official 558:0880f51c4036 396 }
mbed_official 558:0880f51c4036 397
mbed_official 558:0880f51c4036 398 void serial_pinout_tx(PinName tx)
mbed_official 558:0880f51c4036 399 {
mbed_official 558:0880f51c4036 400 //pinmap_pinout(tx, PinMap_UART_TX);
mbed_official 558:0880f51c4036 401 }
mbed_official 558:0880f51c4036 402
mbed_official 558:0880f51c4036 403 void serial_break_set(serial_t *obj)
mbed_official 558:0880f51c4036 404 {
mbed_official 558:0880f51c4036 405 // [TODO]
mbed_official 558:0880f51c4036 406 }
mbed_official 558:0880f51c4036 407
mbed_official 558:0880f51c4036 408 void serial_break_clear(serial_t *obj)
mbed_official 558:0880f51c4036 409 {
mbed_official 558:0880f51c4036 410 // [TODO]
mbed_official 558:0880f51c4036 411 }
mbed_official 558:0880f51c4036 412
mbed_official 558:0880f51c4036 413 #endif