test

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Wed Jun 21 17:46:44 2017 +0100
Revision:
168:e84263d55307
This updates the lib to the mbed lib v 145

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 168:e84263d55307 1 /* mbed Microcontroller Library
AnnaBridge 168:e84263d55307 2 *******************************************************************************
AnnaBridge 168:e84263d55307 3 * Copyright (c) 2015, STMicroelectronics
AnnaBridge 168:e84263d55307 4 * All rights reserved.
AnnaBridge 168:e84263d55307 5 *
AnnaBridge 168:e84263d55307 6 * Redistribution and use in source and binary forms, with or without
AnnaBridge 168:e84263d55307 7 * modification, are permitted provided that the following conditions are met:
AnnaBridge 168:e84263d55307 8 *
AnnaBridge 168:e84263d55307 9 * 1. Redistributions of source code must retain the above copyright notice,
AnnaBridge 168:e84263d55307 10 * this list of conditions and the following disclaimer.
AnnaBridge 168:e84263d55307 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
AnnaBridge 168:e84263d55307 12 * this list of conditions and the following disclaimer in the documentation
AnnaBridge 168:e84263d55307 13 * and/or other materials provided with the distribution.
AnnaBridge 168:e84263d55307 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
AnnaBridge 168:e84263d55307 15 * may be used to endorse or promote products derived from this software
AnnaBridge 168:e84263d55307 16 * without specific prior written permission.
AnnaBridge 168:e84263d55307 17 *
AnnaBridge 168:e84263d55307 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AnnaBridge 168:e84263d55307 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
AnnaBridge 168:e84263d55307 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
AnnaBridge 168:e84263d55307 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
AnnaBridge 168:e84263d55307 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
AnnaBridge 168:e84263d55307 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
AnnaBridge 168:e84263d55307 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
AnnaBridge 168:e84263d55307 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
AnnaBridge 168:e84263d55307 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
AnnaBridge 168:e84263d55307 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
AnnaBridge 168:e84263d55307 28 *******************************************************************************
AnnaBridge 168:e84263d55307 29 */
AnnaBridge 168:e84263d55307 30 #include "mbed_assert.h"
AnnaBridge 168:e84263d55307 31 #include "mbed_error.h"
AnnaBridge 168:e84263d55307 32 #include "serial_api.h"
AnnaBridge 168:e84263d55307 33 #include "serial_api_hal.h"
AnnaBridge 168:e84263d55307 34 #include "PeripheralPins.h"
AnnaBridge 168:e84263d55307 35
AnnaBridge 168:e84263d55307 36 #if DEVICE_SERIAL
AnnaBridge 168:e84263d55307 37
AnnaBridge 168:e84263d55307 38 void init_uart(serial_t *obj)
AnnaBridge 168:e84263d55307 39 {
AnnaBridge 168:e84263d55307 40 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 168:e84263d55307 41 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
AnnaBridge 168:e84263d55307 42 huart->Instance = (USART_TypeDef *)(obj_s->uart);
AnnaBridge 168:e84263d55307 43
AnnaBridge 168:e84263d55307 44 huart->Init.BaudRate = obj_s->baudrate;
AnnaBridge 168:e84263d55307 45 huart->Init.WordLength = obj_s->databits;
AnnaBridge 168:e84263d55307 46 huart->Init.StopBits = obj_s->stopbits;
AnnaBridge 168:e84263d55307 47 huart->Init.Parity = obj_s->parity;
AnnaBridge 168:e84263d55307 48 #if DEVICE_SERIAL_FC
AnnaBridge 168:e84263d55307 49 huart->Init.HwFlowCtl = obj_s->hw_flow_ctl;
AnnaBridge 168:e84263d55307 50 #else
AnnaBridge 168:e84263d55307 51 huart->Init.HwFlowCtl = UART_HWCONTROL_NONE;
AnnaBridge 168:e84263d55307 52 #endif
AnnaBridge 168:e84263d55307 53 huart->Init.OverSampling = UART_OVERSAMPLING_16;
AnnaBridge 168:e84263d55307 54 huart->TxXferCount = 0;
AnnaBridge 168:e84263d55307 55 huart->TxXferSize = 0;
AnnaBridge 168:e84263d55307 56 huart->RxXferCount = 0;
AnnaBridge 168:e84263d55307 57 huart->RxXferSize = 0;
AnnaBridge 168:e84263d55307 58
AnnaBridge 168:e84263d55307 59 if (obj_s->pin_rx == NC) {
AnnaBridge 168:e84263d55307 60 huart->Init.Mode = UART_MODE_TX;
AnnaBridge 168:e84263d55307 61 } else if (obj_s->pin_tx == NC) {
AnnaBridge 168:e84263d55307 62 huart->Init.Mode = UART_MODE_RX;
AnnaBridge 168:e84263d55307 63 } else {
AnnaBridge 168:e84263d55307 64 huart->Init.Mode = UART_MODE_TX_RX;
AnnaBridge 168:e84263d55307 65 }
AnnaBridge 168:e84263d55307 66
AnnaBridge 168:e84263d55307 67 if (HAL_UART_Init(huart) != HAL_OK) {
AnnaBridge 168:e84263d55307 68 error("Cannot initialize UART\n");
AnnaBridge 168:e84263d55307 69 }
AnnaBridge 168:e84263d55307 70 }
AnnaBridge 168:e84263d55307 71
AnnaBridge 168:e84263d55307 72 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
AnnaBridge 168:e84263d55307 73 {
AnnaBridge 168:e84263d55307 74 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 168:e84263d55307 75
AnnaBridge 168:e84263d55307 76 switch (parity) {
AnnaBridge 168:e84263d55307 77 case ParityOdd:
AnnaBridge 168:e84263d55307 78 obj_s->parity = UART_PARITY_ODD;
AnnaBridge 168:e84263d55307 79 break;
AnnaBridge 168:e84263d55307 80 case ParityEven:
AnnaBridge 168:e84263d55307 81 obj_s->parity = UART_PARITY_EVEN;
AnnaBridge 168:e84263d55307 82 break;
AnnaBridge 168:e84263d55307 83 default: // ParityNone
AnnaBridge 168:e84263d55307 84 case ParityForced0: // unsupported!
AnnaBridge 168:e84263d55307 85 case ParityForced1: // unsupported!
AnnaBridge 168:e84263d55307 86 obj_s->parity = UART_PARITY_NONE;
AnnaBridge 168:e84263d55307 87 break;
AnnaBridge 168:e84263d55307 88 }
AnnaBridge 168:e84263d55307 89
AnnaBridge 168:e84263d55307 90 switch (data_bits) {
AnnaBridge 168:e84263d55307 91 case 9:
AnnaBridge 168:e84263d55307 92 MBED_ASSERT(parity == UART_PARITY_NONE);
AnnaBridge 168:e84263d55307 93 obj_s->databits = UART_WORDLENGTH_9B;
AnnaBridge 168:e84263d55307 94 break;
AnnaBridge 168:e84263d55307 95 default:
AnnaBridge 168:e84263d55307 96 case 8:
AnnaBridge 168:e84263d55307 97 if (parity != UART_PARITY_NONE) {
AnnaBridge 168:e84263d55307 98 obj_s->databits = UART_WORDLENGTH_9B;
AnnaBridge 168:e84263d55307 99 } else {
AnnaBridge 168:e84263d55307 100 obj_s->databits = UART_WORDLENGTH_8B;
AnnaBridge 168:e84263d55307 101 }
AnnaBridge 168:e84263d55307 102 break;
AnnaBridge 168:e84263d55307 103 #if defined UART_WORDLENGTH_7B
AnnaBridge 168:e84263d55307 104 case 7:
AnnaBridge 168:e84263d55307 105 if (parity != UART_PARITY_NONE) {
AnnaBridge 168:e84263d55307 106 obj_s->databits = UART_WORDLENGTH_8B;
AnnaBridge 168:e84263d55307 107 } else {
AnnaBridge 168:e84263d55307 108 obj_s->databits = UART_WORDLENGTH_7B;
AnnaBridge 168:e84263d55307 109 }
AnnaBridge 168:e84263d55307 110 break;
AnnaBridge 168:e84263d55307 111 #endif
AnnaBridge 168:e84263d55307 112 }
AnnaBridge 168:e84263d55307 113
AnnaBridge 168:e84263d55307 114 if (stop_bits == 2) {
AnnaBridge 168:e84263d55307 115 obj_s->stopbits = UART_STOPBITS_2;
AnnaBridge 168:e84263d55307 116 } else {
AnnaBridge 168:e84263d55307 117 obj_s->stopbits = UART_STOPBITS_1;
AnnaBridge 168:e84263d55307 118 }
AnnaBridge 168:e84263d55307 119
AnnaBridge 168:e84263d55307 120 init_uart(obj);
AnnaBridge 168:e84263d55307 121 }
AnnaBridge 168:e84263d55307 122
AnnaBridge 168:e84263d55307 123 /******************************************************************************
AnnaBridge 168:e84263d55307 124 * READ/WRITE
AnnaBridge 168:e84263d55307 125 ******************************************************************************/
AnnaBridge 168:e84263d55307 126
AnnaBridge 168:e84263d55307 127 int serial_readable(serial_t *obj)
AnnaBridge 168:e84263d55307 128 {
AnnaBridge 168:e84263d55307 129 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 168:e84263d55307 130 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
AnnaBridge 168:e84263d55307 131 /* To avoid a target blocking case, let's check for
AnnaBridge 168:e84263d55307 132 * possible OVERRUN error and discard it
AnnaBridge 168:e84263d55307 133 */
AnnaBridge 168:e84263d55307 134 if(__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE)) {
AnnaBridge 168:e84263d55307 135 __HAL_UART_CLEAR_OREFLAG(huart);
AnnaBridge 168:e84263d55307 136 }
AnnaBridge 168:e84263d55307 137 // Check if data is received
AnnaBridge 168:e84263d55307 138 return (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) ? 1 : 0;
AnnaBridge 168:e84263d55307 139 }
AnnaBridge 168:e84263d55307 140
AnnaBridge 168:e84263d55307 141 int serial_writable(serial_t *obj)
AnnaBridge 168:e84263d55307 142 {
AnnaBridge 168:e84263d55307 143 struct serial_s *obj_s = SERIAL_S(obj);
AnnaBridge 168:e84263d55307 144 UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
AnnaBridge 168:e84263d55307 145
AnnaBridge 168:e84263d55307 146 // Check if data is transmitted
AnnaBridge 168:e84263d55307 147 return (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) ? 1 : 0;
AnnaBridge 168:e84263d55307 148 }
AnnaBridge 168:e84263d55307 149
AnnaBridge 168:e84263d55307 150 void serial_pinout_tx(PinName tx)
AnnaBridge 168:e84263d55307 151 {
AnnaBridge 168:e84263d55307 152 pinmap_pinout(tx, PinMap_UART_TX);
AnnaBridge 168:e84263d55307 153 }
AnnaBridge 168:e84263d55307 154
AnnaBridge 168:e84263d55307 155 void serial_break_clear(serial_t *obj)
AnnaBridge 168:e84263d55307 156 {
AnnaBridge 168:e84263d55307 157 (void)obj;
AnnaBridge 168:e84263d55307 158 }
AnnaBridge 168:e84263d55307 159
AnnaBridge 168:e84263d55307 160 #endif /* DEVICE_SERIAL */