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