inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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