00

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) 2016, 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 #ifndef MBED_PIN_DEVICE_H
ganlikun 0:13413ea9a877 31 #define MBED_PIN_DEVICE_H
ganlikun 0:13413ea9a877 32
ganlikun 0:13413ea9a877 33 #include "cmsis.h"
ganlikun 0:13413ea9a877 34
ganlikun 0:13413ea9a877 35 // when LL is available, below include can be used
ganlikun 0:13413ea9a877 36 // #include "stm32f4xx_ll_gpio.h"
ganlikun 0:13413ea9a877 37 // until then let's define locally the required functions
ganlikun 0:13413ea9a877 38 #define LL_GPIO_PIN_0 GPIO_BSRR_BS_0 /*!< Select pin 0 */
ganlikun 0:13413ea9a877 39 #define LL_GPIO_PIN_1 GPIO_BSRR_BS_1 /*!< Select pin 1 */
ganlikun 0:13413ea9a877 40 #define LL_GPIO_PIN_2 GPIO_BSRR_BS_2 /*!< Select pin 2 */
ganlikun 0:13413ea9a877 41 #define LL_GPIO_PIN_3 GPIO_BSRR_BS_3 /*!< Select pin 3 */
ganlikun 0:13413ea9a877 42 #define LL_GPIO_PIN_4 GPIO_BSRR_BS_4 /*!< Select pin 4 */
ganlikun 0:13413ea9a877 43 #define LL_GPIO_PIN_5 GPIO_BSRR_BS_5 /*!< Select pin 5 */
ganlikun 0:13413ea9a877 44 #define LL_GPIO_PIN_6 GPIO_BSRR_BS_6 /*!< Select pin 6 */
ganlikun 0:13413ea9a877 45 #define LL_GPIO_PIN_7 GPIO_BSRR_BS_7 /*!< Select pin 7 */
ganlikun 0:13413ea9a877 46 #define LL_GPIO_PIN_8 GPIO_BSRR_BS_8 /*!< Select pin 8 */
ganlikun 0:13413ea9a877 47 #define LL_GPIO_PIN_9 GPIO_BSRR_BS_9 /*!< Select pin 9 */
ganlikun 0:13413ea9a877 48 #define LL_GPIO_PIN_10 GPIO_BSRR_BS_10 /*!< Select pin 10 */
ganlikun 0:13413ea9a877 49 #define LL_GPIO_PIN_11 GPIO_BSRR_BS_11 /*!< Select pin 11 */
ganlikun 0:13413ea9a877 50 #define LL_GPIO_PIN_12 GPIO_BSRR_BS_12 /*!< Select pin 12 */
ganlikun 0:13413ea9a877 51 #define LL_GPIO_PIN_13 GPIO_BSRR_BS_13 /*!< Select pin 13 */
ganlikun 0:13413ea9a877 52 #define LL_GPIO_PIN_14 GPIO_BSRR_BS_14 /*!< Select pin 14 */
ganlikun 0:13413ea9a877 53 #define LL_GPIO_PIN_15 GPIO_BSRR_BS_15 /*!< Select pin 15 */
ganlikun 0:13413ea9a877 54
ganlikun 0:13413ea9a877 55 #define LL_GPIO_MODE_INPUT ((uint32_t)0x00000000U) /*!< Select input mode */
ganlikun 0:13413ea9a877 56 #define LL_GPIO_MODE_OUTPUT GPIO_MODER_MODER0_0 /*!< Select output mode */
ganlikun 0:13413ea9a877 57 #define LL_GPIO_MODE_ALTERNATE GPIO_MODER_MODER0_1 /*!< Select alternate function mode */
ganlikun 0:13413ea9a877 58 #define LL_GPIO_MODE_ANALOG GPIO_MODER_MODER0 /*!< Select analog mode */
ganlikun 0:13413ea9a877 59
ganlikun 0:13413ea9a877 60 #define LL_GPIO_OUTPUT_PUSHPULL ((uint32_t)0x00000000U) /*!< Select push-pull as output type */
ganlikun 0:13413ea9a877 61 #define LL_GPIO_OUTPUT_OPENDRAIN GPIO_OTYPER_OT_0 /*!< Select open-drain as output type */
ganlikun 0:13413ea9a877 62
ganlikun 0:13413ea9a877 63 #define LL_GPIO_PULL_NO ((uint32_t)0x00000000U) /*!< Select I/O no pull */
ganlikun 0:13413ea9a877 64 #define LL_GPIO_PULL_UP GPIO_PUPDR_PUPDR0_0 /*!< Select I/O pull up */
ganlikun 0:13413ea9a877 65 #define LL_GPIO_PULL_DOWN GPIO_PUPDR_PUPDR0_1 /*!< Select I/O pull down */
ganlikun 0:13413ea9a877 66
ganlikun 0:13413ea9a877 67 #define LL_GPIO_SPEED_FREQ_LOW ((uint32_t)0x00000000U) /*!< Select I/O low output speed */
ganlikun 0:13413ea9a877 68 #define LL_GPIO_SPEED_FREQ_MEDIUM GPIO_OSPEEDER_OSPEEDR0_0 /*!< Select I/O medium output speed */
ganlikun 0:13413ea9a877 69 #define LL_GPIO_SPEED_FREQ_HIGH GPIO_OSPEEDER_OSPEEDR0_1 /*!< Select I/O fast output speed */
ganlikun 0:13413ea9a877 70 #define LL_GPIO_SPEED_FREQ_VERY_HIGH GPIO_OSPEEDER_OSPEEDR0 /*!< Select I/O high output speed */
ganlikun 0:13413ea9a877 71
ganlikun 0:13413ea9a877 72 __STATIC_INLINE void LL_GPIO_SetAFPin_0_7(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Alternate)
ganlikun 0:13413ea9a877 73 {
ganlikun 0:13413ea9a877 74 MODIFY_REG(GPIOx->AFR[0], (0xFU << (POSITION_VAL(Pin) * 4U)),
ganlikun 0:13413ea9a877 75 (Alternate << (POSITION_VAL(Pin) * 4U)));
ganlikun 0:13413ea9a877 76 }
ganlikun 0:13413ea9a877 77 __STATIC_INLINE void LL_GPIO_SetAFPin_8_15(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Alternate)
ganlikun 0:13413ea9a877 78 {
ganlikun 0:13413ea9a877 79 MODIFY_REG(GPIOx->AFR[1], (0xFU << (POSITION_VAL(Pin >> 8U) * 4U)),
ganlikun 0:13413ea9a877 80 (Alternate << (POSITION_VAL(Pin >> 8U) * 4U)));
ganlikun 0:13413ea9a877 81 }
ganlikun 0:13413ea9a877 82 __STATIC_INLINE void LL_GPIO_SetPinMode(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Mode)
ganlikun 0:13413ea9a877 83 {
ganlikun 0:13413ea9a877 84 MODIFY_REG(GPIOx->MODER, (GPIO_MODER_MODER0 << (POSITION_VAL(Pin) * 2U)), (Mode << (POSITION_VAL(Pin) * 2U)));
ganlikun 0:13413ea9a877 85 }
ganlikun 0:13413ea9a877 86 __STATIC_INLINE uint32_t LL_GPIO_GetPinMode(GPIO_TypeDef *GPIOx, uint32_t Pin)
ganlikun 0:13413ea9a877 87 {
ganlikun 0:13413ea9a877 88 return (uint32_t)(READ_BIT(GPIOx->MODER, ((Pin * Pin) * GPIO_MODER_MODER0)) / (Pin * Pin));
ganlikun 0:13413ea9a877 89 }
ganlikun 0:13413ea9a877 90 __STATIC_INLINE void LL_GPIO_SetPinPull(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Pull)
ganlikun 0:13413ea9a877 91 {
ganlikun 0:13413ea9a877 92 MODIFY_REG(GPIOx->PUPDR, (GPIO_PUPDR_PUPDR0 << (POSITION_VAL(Pin) * 2U)), (Pull << (POSITION_VAL(Pin) * 2U)));
ganlikun 0:13413ea9a877 93 }
ganlikun 0:13413ea9a877 94 __STATIC_INLINE void LL_GPIO_SetPinOutputType(GPIO_TypeDef *GPIOx, uint32_t PinMask, uint32_t OutputType)
ganlikun 0:13413ea9a877 95 {
ganlikun 0:13413ea9a877 96 MODIFY_REG(GPIOx->OTYPER, PinMask, (PinMask * OutputType));
ganlikun 0:13413ea9a877 97 }
ganlikun 0:13413ea9a877 98 __STATIC_INLINE void LL_GPIO_SetPinSpeed(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Speed)
ganlikun 0:13413ea9a877 99 {
ganlikun 0:13413ea9a877 100 MODIFY_REG(GPIOx->OSPEEDR, (GPIO_OSPEEDER_OSPEEDR0 << (POSITION_VAL(Pin) * 2U)),
ganlikun 0:13413ea9a877 101 (Speed << (POSITION_VAL(Pin) * 2U)));
ganlikun 0:13413ea9a877 102 }
ganlikun 0:13413ea9a877 103 // Above lines shall be defined in LL when available
ganlikun 0:13413ea9a877 104
ganlikun 0:13413ea9a877 105 extern const uint32_t ll_pin_defines[16];
ganlikun 0:13413ea9a877 106
ganlikun 0:13413ea9a877 107 /* Family specific implementations */
ganlikun 0:13413ea9a877 108 static inline void stm_pin_DisconnectDebug(PinName pin)
ganlikun 0:13413ea9a877 109 {
ganlikun 0:13413ea9a877 110 /* empty for now */
ganlikun 0:13413ea9a877 111 }
ganlikun 0:13413ea9a877 112
ganlikun 0:13413ea9a877 113 static inline void stm_pin_PullConfig(GPIO_TypeDef *gpio, uint32_t ll_pin, uint32_t pull_config)
ganlikun 0:13413ea9a877 114 {
ganlikun 0:13413ea9a877 115 switch (pull_config) {
ganlikun 0:13413ea9a877 116 case GPIO_PULLUP:
ganlikun 0:13413ea9a877 117 LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_UP);
ganlikun 0:13413ea9a877 118 break;
ganlikun 0:13413ea9a877 119 case GPIO_PULLDOWN:
ganlikun 0:13413ea9a877 120 LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_DOWN);
ganlikun 0:13413ea9a877 121 break;
ganlikun 0:13413ea9a877 122 default:
ganlikun 0:13413ea9a877 123 LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_NO);
ganlikun 0:13413ea9a877 124 break;
ganlikun 0:13413ea9a877 125 }
ganlikun 0:13413ea9a877 126 }
ganlikun 0:13413ea9a877 127
ganlikun 0:13413ea9a877 128 static inline void stm_pin_SetAFPin( GPIO_TypeDef *gpio, PinName pin, uint32_t afnum)
ganlikun 0:13413ea9a877 129 {
ganlikun 0:13413ea9a877 130 uint32_t ll_pin = ll_pin_defines[STM_PIN(pin)];
ganlikun 0:13413ea9a877 131
ganlikun 0:13413ea9a877 132 if (STM_PIN(pin) > 7)
ganlikun 0:13413ea9a877 133 LL_GPIO_SetAFPin_8_15(gpio, ll_pin, afnum);
ganlikun 0:13413ea9a877 134 else
ganlikun 0:13413ea9a877 135 LL_GPIO_SetAFPin_0_7(gpio, ll_pin, afnum);
ganlikun 0:13413ea9a877 136 }
ganlikun 0:13413ea9a877 137
ganlikun 0:13413ea9a877 138 #endif
ganlikun 0:13413ea9a877 139