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 Sep 04 09:30:10 2015 +0100
Revision:
619:034e698bc035
Parent:
558:0880f51c4036
Child:
631:ff681937ffd8
Synchronized with git revision 92d1bfad30082571776c810a56fd471d30514ccf

Full URL: https://github.com/mbedmicro/mbed/commit/92d1bfad30082571776c810a56fd471d30514ccf/

Change directory structure and move files.

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