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