The prosthetic control(MIT)

Committer:
ganlikun
Date:
Thu Jun 23 05:23:34 2022 +0000
Revision:
0:20e0c61e0684
01

Who changed what in which revision?

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