001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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