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) 2017, 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 "pinmap.h"
ganlikun 0:20e0c61e0684 32 #include "PortNames.h"
ganlikun 0:20e0c61e0684 33 #include "mbed_error.h"
ganlikun 0:20e0c61e0684 34 #include "pin_device.h"
ganlikun 0:20e0c61e0684 35
ganlikun 0:20e0c61e0684 36 extern GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx);
ganlikun 0:20e0c61e0684 37
ganlikun 0:20e0c61e0684 38 const uint32_t ll_pin_defines[16] = {
ganlikun 0:20e0c61e0684 39 LL_GPIO_PIN_0,
ganlikun 0:20e0c61e0684 40 LL_GPIO_PIN_1,
ganlikun 0:20e0c61e0684 41 LL_GPIO_PIN_2,
ganlikun 0:20e0c61e0684 42 LL_GPIO_PIN_3,
ganlikun 0:20e0c61e0684 43 LL_GPIO_PIN_4,
ganlikun 0:20e0c61e0684 44 LL_GPIO_PIN_5,
ganlikun 0:20e0c61e0684 45 LL_GPIO_PIN_6,
ganlikun 0:20e0c61e0684 46 LL_GPIO_PIN_7,
ganlikun 0:20e0c61e0684 47 LL_GPIO_PIN_8,
ganlikun 0:20e0c61e0684 48 LL_GPIO_PIN_9,
ganlikun 0:20e0c61e0684 49 LL_GPIO_PIN_10,
ganlikun 0:20e0c61e0684 50 LL_GPIO_PIN_11,
ganlikun 0:20e0c61e0684 51 LL_GPIO_PIN_12,
ganlikun 0:20e0c61e0684 52 LL_GPIO_PIN_13,
ganlikun 0:20e0c61e0684 53 LL_GPIO_PIN_14,
ganlikun 0:20e0c61e0684 54 LL_GPIO_PIN_15
ganlikun 0:20e0c61e0684 55 };
ganlikun 0:20e0c61e0684 56
ganlikun 0:20e0c61e0684 57 /**
ganlikun 0:20e0c61e0684 58 * Configure pin (mode, speed, output type and pull-up/pull-down)
ganlikun 0:20e0c61e0684 59 */
ganlikun 0:20e0c61e0684 60 void pin_function(PinName pin, int data)
ganlikun 0:20e0c61e0684 61 {
ganlikun 0:20e0c61e0684 62 MBED_ASSERT(pin != (PinName)NC);
ganlikun 0:20e0c61e0684 63
ganlikun 0:20e0c61e0684 64 // Get the pin informations
ganlikun 0:20e0c61e0684 65 uint32_t mode = STM_PIN_FUNCTION(data);
ganlikun 0:20e0c61e0684 66 uint32_t afnum = STM_PIN_AFNUM(data);
ganlikun 0:20e0c61e0684 67 uint32_t port = STM_PORT(pin);
ganlikun 0:20e0c61e0684 68 uint32_t ll_pin = ll_pin_defines[STM_PIN(pin)];
ganlikun 0:20e0c61e0684 69 uint32_t ll_mode = 0;
ganlikun 0:20e0c61e0684 70
ganlikun 0:20e0c61e0684 71 // Enable GPIO clock
ganlikun 0:20e0c61e0684 72 GPIO_TypeDef *gpio = Set_GPIO_Clock(port);
ganlikun 0:20e0c61e0684 73
ganlikun 0:20e0c61e0684 74 /* Set default speed to high.
ganlikun 0:20e0c61e0684 75 * For most families there are dedicated registers so it is
ganlikun 0:20e0c61e0684 76 * not so important, register can be set at any time.
ganlikun 0:20e0c61e0684 77 * But for families like F1, speed only applies to output.
ganlikun 0:20e0c61e0684 78 */
ganlikun 0:20e0c61e0684 79 #if defined (TARGET_STM32F1)
ganlikun 0:20e0c61e0684 80 if (mode == STM_PIN_OUTPUT) {
ganlikun 0:20e0c61e0684 81 #endif
ganlikun 0:20e0c61e0684 82 LL_GPIO_SetPinSpeed(gpio, ll_pin, LL_GPIO_SPEED_FREQ_HIGH);
ganlikun 0:20e0c61e0684 83 #if defined (TARGET_STM32F1)
ganlikun 0:20e0c61e0684 84 }
ganlikun 0:20e0c61e0684 85 #endif
ganlikun 0:20e0c61e0684 86
ganlikun 0:20e0c61e0684 87 switch (mode) {
ganlikun 0:20e0c61e0684 88 case STM_PIN_INPUT:
ganlikun 0:20e0c61e0684 89 ll_mode = LL_GPIO_MODE_INPUT;
ganlikun 0:20e0c61e0684 90 break;
ganlikun 0:20e0c61e0684 91 case STM_PIN_OUTPUT:
ganlikun 0:20e0c61e0684 92 ll_mode = LL_GPIO_MODE_OUTPUT;
ganlikun 0:20e0c61e0684 93 break;
ganlikun 0:20e0c61e0684 94 case STM_PIN_ALTERNATE:
ganlikun 0:20e0c61e0684 95 ll_mode = LL_GPIO_MODE_ALTERNATE;
ganlikun 0:20e0c61e0684 96 // In case of ALT function, also set he afnum
ganlikun 0:20e0c61e0684 97 stm_pin_SetAFPin(gpio, pin, afnum);
ganlikun 0:20e0c61e0684 98 break;
ganlikun 0:20e0c61e0684 99 case STM_PIN_ANALOG:
ganlikun 0:20e0c61e0684 100 ll_mode = LL_GPIO_MODE_ANALOG;
ganlikun 0:20e0c61e0684 101 break;
ganlikun 0:20e0c61e0684 102 default:
ganlikun 0:20e0c61e0684 103 MBED_ASSERT(0);
ganlikun 0:20e0c61e0684 104 break;
ganlikun 0:20e0c61e0684 105 }
ganlikun 0:20e0c61e0684 106 LL_GPIO_SetPinMode(gpio, ll_pin, ll_mode);
ganlikun 0:20e0c61e0684 107
ganlikun 0:20e0c61e0684 108 #if defined(GPIO_ASCR_ASC0)
ganlikun 0:20e0c61e0684 109 /* For families where Analog Control ASC0 register is present */
ganlikun 0:20e0c61e0684 110 if (STM_PIN_ANALOG_CONTROL(data)) {
ganlikun 0:20e0c61e0684 111 LL_GPIO_EnablePinAnalogControl(gpio, ll_pin);
ganlikun 0:20e0c61e0684 112 } else {
ganlikun 0:20e0c61e0684 113 LL_GPIO_DisablePinAnalogControl(gpio, ll_pin);
ganlikun 0:20e0c61e0684 114 }
ganlikun 0:20e0c61e0684 115 #endif
ganlikun 0:20e0c61e0684 116
ganlikun 0:20e0c61e0684 117 /* For now by default use Speed HIGH for output or alt modes */
ganlikun 0:20e0c61e0684 118 if ((mode == STM_PIN_OUTPUT) ||(mode == STM_PIN_ALTERNATE)) {
ganlikun 0:20e0c61e0684 119 if (STM_PIN_OD(data)) {
ganlikun 0:20e0c61e0684 120 LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_OPENDRAIN);
ganlikun 0:20e0c61e0684 121 } else {
ganlikun 0:20e0c61e0684 122 LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_PUSHPULL);
ganlikun 0:20e0c61e0684 123 }
ganlikun 0:20e0c61e0684 124 }
ganlikun 0:20e0c61e0684 125
ganlikun 0:20e0c61e0684 126 stm_pin_PullConfig(gpio, ll_pin, STM_PIN_PUPD(data));
ganlikun 0:20e0c61e0684 127
ganlikun 0:20e0c61e0684 128 stm_pin_DisconnectDebug(pin);
ganlikun 0:20e0c61e0684 129 }
ganlikun 0:20e0c61e0684 130
ganlikun 0:20e0c61e0684 131 /**
ganlikun 0:20e0c61e0684 132 * Configure pin pull-up/pull-down
ganlikun 0:20e0c61e0684 133 */
ganlikun 0:20e0c61e0684 134 void pin_mode(PinName pin, PinMode mode)
ganlikun 0:20e0c61e0684 135 {
ganlikun 0:20e0c61e0684 136 MBED_ASSERT(pin != (PinName)NC);
ganlikun 0:20e0c61e0684 137
ganlikun 0:20e0c61e0684 138 uint32_t port_index = STM_PORT(pin);
ganlikun 0:20e0c61e0684 139 uint32_t ll_pin = ll_pin_defines[STM_PIN(pin)];
ganlikun 0:20e0c61e0684 140 // Enable GPIO clock
ganlikun 0:20e0c61e0684 141 GPIO_TypeDef *gpio = Set_GPIO_Clock(port_index);
ganlikun 0:20e0c61e0684 142 uint32_t function = LL_GPIO_GetPinMode(gpio, ll_pin);
ganlikun 0:20e0c61e0684 143
ganlikun 0:20e0c61e0684 144 if ((function == LL_GPIO_MODE_OUTPUT) || (function == LL_GPIO_MODE_ALTERNATE))
ganlikun 0:20e0c61e0684 145 {
ganlikun 0:20e0c61e0684 146 if ((mode == OpenDrainNoPull) || (mode == OpenDrainPullUp) || (mode == OpenDrainPullDown)) {
ganlikun 0:20e0c61e0684 147 LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_OPENDRAIN);
ganlikun 0:20e0c61e0684 148 } else {
ganlikun 0:20e0c61e0684 149 LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_PUSHPULL);
ganlikun 0:20e0c61e0684 150 }
ganlikun 0:20e0c61e0684 151 }
ganlikun 0:20e0c61e0684 152
ganlikun 0:20e0c61e0684 153 if ((mode == OpenDrainPullUp) || (mode == PullUp)) {
ganlikun 0:20e0c61e0684 154 stm_pin_PullConfig(gpio, ll_pin, GPIO_PULLUP);
ganlikun 0:20e0c61e0684 155 } else if ((mode == OpenDrainPullDown) || (mode == PullDown)) {
ganlikun 0:20e0c61e0684 156 stm_pin_PullConfig(gpio, ll_pin, GPIO_PULLDOWN);
ganlikun 0:20e0c61e0684 157 } else {
ganlikun 0:20e0c61e0684 158 stm_pin_PullConfig(gpio, ll_pin, GPIO_NOPULL);
ganlikun 0:20e0c61e0684 159 }
ganlikun 0:20e0c61e0684 160 }
ganlikun 0:20e0c61e0684 161