Forked.

Fork of mbed-dev by mbed official

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

Who changed what in which revision?

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