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:
Wed Jun 03 09:00:09 2015 +0100
Revision:
558:0880f51c4036
Child:
619:034e698bc035
Synchronized with git revision 927c31ab8457cfef0ee8a8316117b7a41fd79133

Full URL: https://github.com/mbedmicro/mbed/commit/927c31ab8457cfef0ee8a8316117b7a41fd79133/

Add WIZwiki-W7500

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 558:0880f51c4036 40
mbed_official 558:0880f51c4036 41 #define UART_NUM (2)
mbed_official 558:0880f51c4036 42
mbed_official 558:0880f51c4036 43 static uint32_t serial_irq_ids[UART_NUM] = {0, 0};
mbed_official 558:0880f51c4036 44
mbed_official 558:0880f51c4036 45
mbed_official 558:0880f51c4036 46 static uart_irq_handler irq_handler;
mbed_official 558:0880f51c4036 47 static UART_TypeDef *UART;
mbed_official 558:0880f51c4036 48
mbed_official 558:0880f51c4036 49
mbed_official 558:0880f51c4036 50 UART_InitTypeDef UART_InitStructure;
mbed_official 558:0880f51c4036 51
mbed_official 558:0880f51c4036 52 int stdio_uart_inited = 0;
mbed_official 558:0880f51c4036 53 serial_t stdio_uart;
mbed_official 558:0880f51c4036 54
mbed_official 558:0880f51c4036 55 static void init_uart(serial_t *obj)
mbed_official 558:0880f51c4036 56 {
mbed_official 558:0880f51c4036 57 UART = (UART_TypeDef *)(obj->uart);
mbed_official 558:0880f51c4036 58 UART_InitStructure.UART_BaudRate = obj->baudrate;
mbed_official 558:0880f51c4036 59 UART_InitStructure.UART_WordLength = obj->databits;
mbed_official 558:0880f51c4036 60 UART_InitStructure.UART_StopBits = obj->stopbits;
mbed_official 558:0880f51c4036 61 UART_InitStructure.UART_Parity = obj->parity;
mbed_official 558:0880f51c4036 62 UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
mbed_official 558:0880f51c4036 63
mbed_official 558:0880f51c4036 64
mbed_official 558:0880f51c4036 65 if (obj->pin_rx == NC) {
mbed_official 558:0880f51c4036 66 UART_InitStructure.UART_Mode = UART_Mode_Tx;
mbed_official 558:0880f51c4036 67 } else if (obj->pin_tx == NC) {
mbed_official 558:0880f51c4036 68 UART_InitStructure.UART_Mode = UART_Mode_Rx;
mbed_official 558:0880f51c4036 69 } else {
mbed_official 558:0880f51c4036 70 UART_InitStructure.UART_Mode = (UART_Mode_Rx | UART_Mode_Tx);
mbed_official 558:0880f51c4036 71 }
mbed_official 558:0880f51c4036 72
mbed_official 558:0880f51c4036 73 UART_Init(UART,&UART_InitStructure);
mbed_official 558:0880f51c4036 74 }
mbed_official 558:0880f51c4036 75
mbed_official 558:0880f51c4036 76 void serial_init(serial_t *obj, PinName tx, PinName rx)
mbed_official 558:0880f51c4036 77 {
mbed_official 558:0880f51c4036 78 // Determine the UART to use (UART_1, UART_2, ...)
mbed_official 558:0880f51c4036 79 UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
mbed_official 558:0880f51c4036 80 UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
mbed_official 558:0880f51c4036 81
mbed_official 558:0880f51c4036 82 // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
mbed_official 558:0880f51c4036 83 obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
mbed_official 558:0880f51c4036 84
mbed_official 558:0880f51c4036 85 MBED_ASSERT(obj->uart != (UARTName)NC);
mbed_official 558:0880f51c4036 86
mbed_official 558:0880f51c4036 87 // Enable USART clock
mbed_official 558:0880f51c4036 88 if (obj->uart == UART_0) {
mbed_official 558:0880f51c4036 89 obj->index = 0;
mbed_official 558:0880f51c4036 90 }
mbed_official 558:0880f51c4036 91
mbed_official 558:0880f51c4036 92 if (obj->uart == UART_1) {
mbed_official 558:0880f51c4036 93 obj->index = 1;
mbed_official 558:0880f51c4036 94 }
mbed_official 558:0880f51c4036 95
mbed_official 558:0880f51c4036 96 // Configure the UART pins
mbed_official 558:0880f51c4036 97 pinmap_pinout(tx, PinMap_UART_TX);
mbed_official 558:0880f51c4036 98 pinmap_pinout(rx, PinMap_UART_RX);
mbed_official 558:0880f51c4036 99 if (tx != NC) {
mbed_official 558:0880f51c4036 100 pin_mode(tx, PullUp);
mbed_official 558:0880f51c4036 101 }
mbed_official 558:0880f51c4036 102 if (rx != NC) {
mbed_official 558:0880f51c4036 103 pin_mode(rx, PullUp);
mbed_official 558:0880f51c4036 104 }
mbed_official 558:0880f51c4036 105
mbed_official 558:0880f51c4036 106 // Configure UART
mbed_official 558:0880f51c4036 107 obj->baudrate = 9600;
mbed_official 558:0880f51c4036 108 obj->databits = UART_WordLength_8b;
mbed_official 558:0880f51c4036 109 obj->stopbits = UART_StopBits_1;
mbed_official 558:0880f51c4036 110 obj->parity = UART_Parity_No;
mbed_official 558:0880f51c4036 111
mbed_official 558:0880f51c4036 112 obj->pin_tx = tx;
mbed_official 558:0880f51c4036 113 obj->pin_rx = rx;
mbed_official 558:0880f51c4036 114
mbed_official 558:0880f51c4036 115
mbed_official 558:0880f51c4036 116 init_uart(obj);
mbed_official 558:0880f51c4036 117
mbed_official 558:0880f51c4036 118 // For stdio management
mbed_official 558:0880f51c4036 119 if (obj->uart == STDIO_UART) {
mbed_official 558:0880f51c4036 120 stdio_uart_inited = 1;
mbed_official 558:0880f51c4036 121 memcpy(&stdio_uart, obj, sizeof(serial_t));
mbed_official 558:0880f51c4036 122 }
mbed_official 558:0880f51c4036 123 }
mbed_official 558:0880f51c4036 124
mbed_official 558:0880f51c4036 125 void serial_free(serial_t *obj)
mbed_official 558:0880f51c4036 126 {
mbed_official 558:0880f51c4036 127 // Reset UART and disable clock
mbed_official 558:0880f51c4036 128 if (obj->uart == UART_0) {
mbed_official 558:0880f51c4036 129 }
mbed_official 558:0880f51c4036 130
mbed_official 558:0880f51c4036 131 if (obj->uart == UART_1) {
mbed_official 558:0880f51c4036 132 }
mbed_official 558:0880f51c4036 133
mbed_official 558:0880f51c4036 134
mbed_official 558:0880f51c4036 135 // Configure GPIOs
mbed_official 558:0880f51c4036 136 // pin_function(obj->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 558:0880f51c4036 137 // pin_function(obj->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 558:0880f51c4036 138
mbed_official 558:0880f51c4036 139 serial_irq_ids[obj->index] = 0;
mbed_official 558:0880f51c4036 140 }
mbed_official 558:0880f51c4036 141
mbed_official 558:0880f51c4036 142 void serial_baud(serial_t *obj, int baudrate)
mbed_official 558:0880f51c4036 143 {
mbed_official 558:0880f51c4036 144 obj->baudrate = baudrate;
mbed_official 558:0880f51c4036 145 init_uart(obj);
mbed_official 558:0880f51c4036 146 }
mbed_official 558:0880f51c4036 147
mbed_official 558:0880f51c4036 148 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
mbed_official 558:0880f51c4036 149 {
mbed_official 558:0880f51c4036 150 if (data_bits == 7) {
mbed_official 558:0880f51c4036 151 obj->databits = UART_WordLength_7b;
mbed_official 558:0880f51c4036 152 } else {
mbed_official 558:0880f51c4036 153 obj->databits = UART_WordLength_8b;
mbed_official 558:0880f51c4036 154 }
mbed_official 558:0880f51c4036 155
mbed_official 558:0880f51c4036 156 switch (parity) {
mbed_official 558:0880f51c4036 157 case ParityOdd:
mbed_official 558:0880f51c4036 158 case ParityForced0:
mbed_official 558:0880f51c4036 159 obj->parity = UART_Parity_Odd;
mbed_official 558:0880f51c4036 160 break;
mbed_official 558:0880f51c4036 161 case ParityEven:
mbed_official 558:0880f51c4036 162 case ParityForced1:
mbed_official 558:0880f51c4036 163 obj->parity = UART_Parity_Even;
mbed_official 558:0880f51c4036 164 break;
mbed_official 558:0880f51c4036 165 default: // ParityNone
mbed_official 558:0880f51c4036 166 obj->parity = UART_Parity_No;
mbed_official 558:0880f51c4036 167 break;
mbed_official 558:0880f51c4036 168 }
mbed_official 558:0880f51c4036 169
mbed_official 558:0880f51c4036 170 if (stop_bits == 2) {
mbed_official 558:0880f51c4036 171 obj->stopbits = UART_StopBits_2;
mbed_official 558:0880f51c4036 172 } else {
mbed_official 558:0880f51c4036 173 obj->stopbits = UART_StopBits_1;
mbed_official 558:0880f51c4036 174 }
mbed_official 558:0880f51c4036 175
mbed_official 558:0880f51c4036 176 init_uart(obj);
mbed_official 558:0880f51c4036 177 }
mbed_official 558:0880f51c4036 178
mbed_official 558:0880f51c4036 179 /******************************************************************************
mbed_official 558:0880f51c4036 180 * INTERRUPTS HANDLING
mbed_official 558:0880f51c4036 181 ******************************************************************************/
mbed_official 558:0880f51c4036 182
mbed_official 558:0880f51c4036 183 static void uart_irq(UARTName name, int id)
mbed_official 558:0880f51c4036 184 {
mbed_official 558:0880f51c4036 185 UART = (UART_TypeDef *)name;
mbed_official 558:0880f51c4036 186 if (serial_irq_ids[id] != 0) {
mbed_official 558:0880f51c4036 187 if( UART_GetITStatus(UART,UART_IT_FLAG_TXI) != RESET ){
mbed_official 558:0880f51c4036 188 irq_handler(serial_irq_ids[id], TxIrq);
mbed_official 558:0880f51c4036 189 UART_ClearITPendingBit(UART,UART_IT_FLAG_TXI);
mbed_official 558:0880f51c4036 190 }
mbed_official 558:0880f51c4036 191 if( UART_GetITStatus(UART,UART_IT_FLAG_RXI) != RESET ){
mbed_official 558:0880f51c4036 192 irq_handler(serial_irq_ids[id], RxIrq);
mbed_official 558:0880f51c4036 193 }
mbed_official 558:0880f51c4036 194 }
mbed_official 558:0880f51c4036 195 }
mbed_official 558:0880f51c4036 196
mbed_official 558:0880f51c4036 197 #ifdef __cplusplus
mbed_official 558:0880f51c4036 198 extern "C"{
mbed_official 558:0880f51c4036 199 #endif
mbed_official 558:0880f51c4036 200 void UART0_Handler()
mbed_official 558:0880f51c4036 201 {
mbed_official 558:0880f51c4036 202 uart_irq(UART_0, 0);
mbed_official 558:0880f51c4036 203 }
mbed_official 558:0880f51c4036 204
mbed_official 558:0880f51c4036 205 void UART1_Handler()
mbed_official 558:0880f51c4036 206 {
mbed_official 558:0880f51c4036 207 uart_irq(UART_1, 1);
mbed_official 558:0880f51c4036 208 }
mbed_official 558:0880f51c4036 209 #ifdef __cplusplus
mbed_official 558:0880f51c4036 210 }
mbed_official 558:0880f51c4036 211 #endif
mbed_official 558:0880f51c4036 212
mbed_official 558:0880f51c4036 213
mbed_official 558:0880f51c4036 214 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
mbed_official 558:0880f51c4036 215 {
mbed_official 558:0880f51c4036 216 irq_handler = handler;
mbed_official 558:0880f51c4036 217 serial_irq_ids[obj->index] = id;
mbed_official 558:0880f51c4036 218 }
mbed_official 558:0880f51c4036 219
mbed_official 558:0880f51c4036 220 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
mbed_official 558:0880f51c4036 221 {
mbed_official 558:0880f51c4036 222 IRQn_Type irq_n = (IRQn_Type)0;
mbed_official 558:0880f51c4036 223
mbed_official 558:0880f51c4036 224 UART = (UART_TypeDef *)(obj->uart);
mbed_official 558:0880f51c4036 225
mbed_official 558:0880f51c4036 226 if (obj->uart == UART_0) {
mbed_official 558:0880f51c4036 227 irq_n = UART0_IRQn;
mbed_official 558:0880f51c4036 228 }
mbed_official 558:0880f51c4036 229
mbed_official 558:0880f51c4036 230 if (obj->uart == UART_1) {
mbed_official 558:0880f51c4036 231 irq_n = UART1_IRQn;
mbed_official 558:0880f51c4036 232 }
mbed_official 558:0880f51c4036 233
mbed_official 558:0880f51c4036 234 if (enable) {
mbed_official 558:0880f51c4036 235 if (irq == RxIrq) {
mbed_official 558:0880f51c4036 236 UART_ITConfig(UART,UART_IT_FLAG_RXI,ENABLE);
mbed_official 558:0880f51c4036 237 } else { // TxIrq
mbed_official 558:0880f51c4036 238 UART_ITConfig(UART,UART_IT_FLAG_TXI,ENABLE);
mbed_official 558:0880f51c4036 239 }
mbed_official 558:0880f51c4036 240
mbed_official 558:0880f51c4036 241 NVIC_ClearPendingIRQ(irq_n);
mbed_official 558:0880f51c4036 242 NVIC_EnableIRQ(irq_n);
mbed_official 558:0880f51c4036 243 } else { // disable
mbed_official 558:0880f51c4036 244 UART_ITConfig(UART,(UART_IT_FLAG_RXI|UART_IT_FLAG_TXI),DISABLE);
mbed_official 558:0880f51c4036 245 NVIC_DisableIRQ(irq_n);
mbed_official 558:0880f51c4036 246 }
mbed_official 558:0880f51c4036 247 }
mbed_official 558:0880f51c4036 248
mbed_official 558:0880f51c4036 249 /******************************************************************************
mbed_official 558:0880f51c4036 250 * READ/WRITE
mbed_official 558:0880f51c4036 251 ******************************************************************************/
mbed_official 558:0880f51c4036 252
mbed_official 558:0880f51c4036 253 int serial_getc(serial_t *obj)
mbed_official 558:0880f51c4036 254 {
mbed_official 558:0880f51c4036 255 UART_TypeDef *uart = (UART_TypeDef *)(obj->uart);
mbed_official 558:0880f51c4036 256 // while(!serial_readable(obj));
mbed_official 558:0880f51c4036 257 while(uart->FR & UART_FR_RXFE);
mbed_official 558:0880f51c4036 258
mbed_official 558:0880f51c4036 259 return (uart->DR & 0xFF);
mbed_official 558:0880f51c4036 260 }
mbed_official 558:0880f51c4036 261
mbed_official 558:0880f51c4036 262 void serial_putc(serial_t *obj, int c)
mbed_official 558:0880f51c4036 263 {
mbed_official 558:0880f51c4036 264 UART_TypeDef *uart = (UART_TypeDef *)(obj->uart);
mbed_official 558:0880f51c4036 265
mbed_official 558:0880f51c4036 266 uart->DR = (uint32_t)(c & (uint16_t)0xFF);
mbed_official 558:0880f51c4036 267 while(uart->FR & UART_FR_BUSY);
mbed_official 558:0880f51c4036 268 }
mbed_official 558:0880f51c4036 269
mbed_official 558:0880f51c4036 270 int serial_readable(serial_t *obj)
mbed_official 558:0880f51c4036 271 {
mbed_official 558:0880f51c4036 272 int status;
mbed_official 558:0880f51c4036 273 UART_TypeDef *uart = (UART_TypeDef *)(obj->uart);
mbed_official 558:0880f51c4036 274 // Check if data is received
mbed_official 558:0880f51c4036 275 status = ((uart->FR & UART_FR_RXFE) ? 0: 1);
mbed_official 558:0880f51c4036 276 return status;
mbed_official 558:0880f51c4036 277 }
mbed_official 558:0880f51c4036 278
mbed_official 558:0880f51c4036 279 int serial_writable(serial_t *obj)
mbed_official 558:0880f51c4036 280 {
mbed_official 558:0880f51c4036 281 int status;
mbed_official 558:0880f51c4036 282 UART_TypeDef *uart = (UART_TypeDef *)(obj->uart);
mbed_official 558:0880f51c4036 283 // Check if data is transmitted
mbed_official 558:0880f51c4036 284 status = ((uart->FR & UART_FR_BUSY) ? 0: 1);
mbed_official 558:0880f51c4036 285 return status;
mbed_official 558:0880f51c4036 286 }
mbed_official 558:0880f51c4036 287
mbed_official 558:0880f51c4036 288 void serial_clear(serial_t *obj)
mbed_official 558:0880f51c4036 289 {
mbed_official 558:0880f51c4036 290 // UartHandle.Instance = (USART_TypeDef *)(obj->uart);
mbed_official 558:0880f51c4036 291 // __HAL_UART_CLEAR_IT(&UartHandle, UART_FLAG_TC);
mbed_official 558:0880f51c4036 292 // __HAL_UART_SEND_REQ(&UartHandle, UART_RXDATA_FLUSH_REQUEST);
mbed_official 558:0880f51c4036 293 }
mbed_official 558:0880f51c4036 294
mbed_official 558:0880f51c4036 295 void serial_pinout_tx(PinName tx)
mbed_official 558:0880f51c4036 296 {
mbed_official 558:0880f51c4036 297 //pinmap_pinout(tx, PinMap_UART_TX);
mbed_official 558:0880f51c4036 298 }
mbed_official 558:0880f51c4036 299
mbed_official 558:0880f51c4036 300 void serial_break_set(serial_t *obj)
mbed_official 558:0880f51c4036 301 {
mbed_official 558:0880f51c4036 302 // [TODO]
mbed_official 558:0880f51c4036 303 }
mbed_official 558:0880f51c4036 304
mbed_official 558:0880f51c4036 305 void serial_break_clear(serial_t *obj)
mbed_official 558:0880f51c4036 306 {
mbed_official 558:0880f51c4036 307 // [TODO]
mbed_official 558:0880f51c4036 308 }
mbed_official 558:0880f51c4036 309
mbed_official 558:0880f51c4036 310 #endif