Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lypinator 0:bb348c97df44 1 /* mbed Microcontroller Library
lypinator 0:bb348c97df44 2 *******************************************************************************
lypinator 0:bb348c97df44 3 * Copyright (c) 2016, STMicroelectronics
lypinator 0:bb348c97df44 4 * All rights reserved.
lypinator 0:bb348c97df44 5 *
lypinator 0:bb348c97df44 6 * Redistribution and use in source and binary forms, with or without
lypinator 0:bb348c97df44 7 * modification, are permitted provided that the following conditions are met:
lypinator 0:bb348c97df44 8 *
lypinator 0:bb348c97df44 9 * 1. Redistributions of source code must retain the above copyright notice,
lypinator 0:bb348c97df44 10 * this list of conditions and the following disclaimer.
lypinator 0:bb348c97df44 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
lypinator 0:bb348c97df44 12 * this list of conditions and the following disclaimer in the documentation
lypinator 0:bb348c97df44 13 * and/or other materials provided with the distribution.
lypinator 0:bb348c97df44 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
lypinator 0:bb348c97df44 15 * may be used to endorse or promote products derived from this software
lypinator 0:bb348c97df44 16 * without specific prior written permission.
lypinator 0:bb348c97df44 17 *
lypinator 0:bb348c97df44 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
lypinator 0:bb348c97df44 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
lypinator 0:bb348c97df44 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
lypinator 0:bb348c97df44 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
lypinator 0:bb348c97df44 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
lypinator 0:bb348c97df44 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
lypinator 0:bb348c97df44 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
lypinator 0:bb348c97df44 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
lypinator 0:bb348c97df44 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
lypinator 0:bb348c97df44 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
lypinator 0:bb348c97df44 28 *******************************************************************************
lypinator 0:bb348c97df44 29 */
lypinator 0:bb348c97df44 30 #ifndef MBED_PIN_DEVICE_H
lypinator 0:bb348c97df44 31 #define MBED_PIN_DEVICE_H
lypinator 0:bb348c97df44 32
lypinator 0:bb348c97df44 33 #include "cmsis.h"
lypinator 0:bb348c97df44 34
lypinator 0:bb348c97df44 35 // when LL is available, below include can be used
lypinator 0:bb348c97df44 36 // #include "stm32f4xx_ll_gpio.h"
lypinator 0:bb348c97df44 37 // until then let's define locally the required functions
lypinator 0:bb348c97df44 38 #define LL_GPIO_PIN_0 GPIO_BSRR_BS_0 /*!< Select pin 0 */
lypinator 0:bb348c97df44 39 #define LL_GPIO_PIN_1 GPIO_BSRR_BS_1 /*!< Select pin 1 */
lypinator 0:bb348c97df44 40 #define LL_GPIO_PIN_2 GPIO_BSRR_BS_2 /*!< Select pin 2 */
lypinator 0:bb348c97df44 41 #define LL_GPIO_PIN_3 GPIO_BSRR_BS_3 /*!< Select pin 3 */
lypinator 0:bb348c97df44 42 #define LL_GPIO_PIN_4 GPIO_BSRR_BS_4 /*!< Select pin 4 */
lypinator 0:bb348c97df44 43 #define LL_GPIO_PIN_5 GPIO_BSRR_BS_5 /*!< Select pin 5 */
lypinator 0:bb348c97df44 44 #define LL_GPIO_PIN_6 GPIO_BSRR_BS_6 /*!< Select pin 6 */
lypinator 0:bb348c97df44 45 #define LL_GPIO_PIN_7 GPIO_BSRR_BS_7 /*!< Select pin 7 */
lypinator 0:bb348c97df44 46 #define LL_GPIO_PIN_8 GPIO_BSRR_BS_8 /*!< Select pin 8 */
lypinator 0:bb348c97df44 47 #define LL_GPIO_PIN_9 GPIO_BSRR_BS_9 /*!< Select pin 9 */
lypinator 0:bb348c97df44 48 #define LL_GPIO_PIN_10 GPIO_BSRR_BS_10 /*!< Select pin 10 */
lypinator 0:bb348c97df44 49 #define LL_GPIO_PIN_11 GPIO_BSRR_BS_11 /*!< Select pin 11 */
lypinator 0:bb348c97df44 50 #define LL_GPIO_PIN_12 GPIO_BSRR_BS_12 /*!< Select pin 12 */
lypinator 0:bb348c97df44 51 #define LL_GPIO_PIN_13 GPIO_BSRR_BS_13 /*!< Select pin 13 */
lypinator 0:bb348c97df44 52 #define LL_GPIO_PIN_14 GPIO_BSRR_BS_14 /*!< Select pin 14 */
lypinator 0:bb348c97df44 53 #define LL_GPIO_PIN_15 GPIO_BSRR_BS_15 /*!< Select pin 15 */
lypinator 0:bb348c97df44 54
lypinator 0:bb348c97df44 55 #define LL_GPIO_MODE_INPUT ((uint32_t)0x00000000U) /*!< Select input mode */
lypinator 0:bb348c97df44 56 #define LL_GPIO_MODE_OUTPUT GPIO_MODER_MODER0_0 /*!< Select output mode */
lypinator 0:bb348c97df44 57 #define LL_GPIO_MODE_ALTERNATE GPIO_MODER_MODER0_1 /*!< Select alternate function mode */
lypinator 0:bb348c97df44 58 #define LL_GPIO_MODE_ANALOG GPIO_MODER_MODER0 /*!< Select analog mode */
lypinator 0:bb348c97df44 59
lypinator 0:bb348c97df44 60 #define LL_GPIO_OUTPUT_PUSHPULL ((uint32_t)0x00000000U) /*!< Select push-pull as output type */
lypinator 0:bb348c97df44 61 #define LL_GPIO_OUTPUT_OPENDRAIN GPIO_OTYPER_OT_0 /*!< Select open-drain as output type */
lypinator 0:bb348c97df44 62
lypinator 0:bb348c97df44 63 #define LL_GPIO_PULL_NO ((uint32_t)0x00000000U) /*!< Select I/O no pull */
lypinator 0:bb348c97df44 64 #define LL_GPIO_PULL_UP GPIO_PUPDR_PUPDR0_0 /*!< Select I/O pull up */
lypinator 0:bb348c97df44 65 #define LL_GPIO_PULL_DOWN GPIO_PUPDR_PUPDR0_1 /*!< Select I/O pull down */
lypinator 0:bb348c97df44 66
lypinator 0:bb348c97df44 67 #define LL_GPIO_SPEED_FREQ_LOW ((uint32_t)0x00000000U) /*!< Select I/O low output speed */
lypinator 0:bb348c97df44 68 #define LL_GPIO_SPEED_FREQ_MEDIUM GPIO_OSPEEDER_OSPEEDR0_0 /*!< Select I/O medium output speed */
lypinator 0:bb348c97df44 69 #define LL_GPIO_SPEED_FREQ_HIGH GPIO_OSPEEDER_OSPEEDR0_1 /*!< Select I/O fast output speed */
lypinator 0:bb348c97df44 70 #define LL_GPIO_SPEED_FREQ_VERY_HIGH GPIO_OSPEEDER_OSPEEDR0 /*!< Select I/O high output speed */
lypinator 0:bb348c97df44 71
lypinator 0:bb348c97df44 72 __STATIC_INLINE void LL_GPIO_SetAFPin_0_7(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Alternate)
lypinator 0:bb348c97df44 73 {
lypinator 0:bb348c97df44 74 MODIFY_REG(GPIOx->AFR[0], (0xFU << (POSITION_VAL(Pin) * 4U)),
lypinator 0:bb348c97df44 75 (Alternate << (POSITION_VAL(Pin) * 4U)));
lypinator 0:bb348c97df44 76 }
lypinator 0:bb348c97df44 77 __STATIC_INLINE void LL_GPIO_SetAFPin_8_15(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Alternate)
lypinator 0:bb348c97df44 78 {
lypinator 0:bb348c97df44 79 MODIFY_REG(GPIOx->AFR[1], (0xFU << (POSITION_VAL(Pin >> 8U) * 4U)),
lypinator 0:bb348c97df44 80 (Alternate << (POSITION_VAL(Pin >> 8U) * 4U)));
lypinator 0:bb348c97df44 81 }
lypinator 0:bb348c97df44 82 __STATIC_INLINE void LL_GPIO_SetPinMode(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Mode)
lypinator 0:bb348c97df44 83 {
lypinator 0:bb348c97df44 84 MODIFY_REG(GPIOx->MODER, (GPIO_MODER_MODER0 << (POSITION_VAL(Pin) * 2U)), (Mode << (POSITION_VAL(Pin) * 2U)));
lypinator 0:bb348c97df44 85 }
lypinator 0:bb348c97df44 86 __STATIC_INLINE uint32_t LL_GPIO_GetPinMode(GPIO_TypeDef *GPIOx, uint32_t Pin)
lypinator 0:bb348c97df44 87 {
lypinator 0:bb348c97df44 88 return (uint32_t)(READ_BIT(GPIOx->MODER, ((Pin * Pin) * GPIO_MODER_MODER0)) / (Pin * Pin));
lypinator 0:bb348c97df44 89 }
lypinator 0:bb348c97df44 90 __STATIC_INLINE void LL_GPIO_SetPinPull(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Pull)
lypinator 0:bb348c97df44 91 {
lypinator 0:bb348c97df44 92 MODIFY_REG(GPIOx->PUPDR, (GPIO_PUPDR_PUPDR0 << (POSITION_VAL(Pin) * 2U)), (Pull << (POSITION_VAL(Pin) * 2U)));
lypinator 0:bb348c97df44 93 }
lypinator 0:bb348c97df44 94 __STATIC_INLINE void LL_GPIO_SetPinOutputType(GPIO_TypeDef *GPIOx, uint32_t PinMask, uint32_t OutputType)
lypinator 0:bb348c97df44 95 {
lypinator 0:bb348c97df44 96 MODIFY_REG(GPIOx->OTYPER, PinMask, (PinMask * OutputType));
lypinator 0:bb348c97df44 97 }
lypinator 0:bb348c97df44 98 __STATIC_INLINE void LL_GPIO_SetPinSpeed(GPIO_TypeDef *GPIOx, uint32_t Pin, uint32_t Speed)
lypinator 0:bb348c97df44 99 {
lypinator 0:bb348c97df44 100 MODIFY_REG(GPIOx->OSPEEDR, (GPIO_OSPEEDER_OSPEEDR0 << (POSITION_VAL(Pin) * 2U)),
lypinator 0:bb348c97df44 101 (Speed << (POSITION_VAL(Pin) * 2U)));
lypinator 0:bb348c97df44 102 }
lypinator 0:bb348c97df44 103 // Above lines shall be defined in LL when available
lypinator 0:bb348c97df44 104
lypinator 0:bb348c97df44 105 extern const uint32_t ll_pin_defines[16];
lypinator 0:bb348c97df44 106
lypinator 0:bb348c97df44 107 /* Family specific implementations */
lypinator 0:bb348c97df44 108 static inline void stm_pin_DisconnectDebug(PinName pin)
lypinator 0:bb348c97df44 109 {
lypinator 0:bb348c97df44 110 /* empty for now */
lypinator 0:bb348c97df44 111 }
lypinator 0:bb348c97df44 112
lypinator 0:bb348c97df44 113 static inline void stm_pin_PullConfig(GPIO_TypeDef *gpio, uint32_t ll_pin, uint32_t pull_config)
lypinator 0:bb348c97df44 114 {
lypinator 0:bb348c97df44 115 switch (pull_config) {
lypinator 0:bb348c97df44 116 case GPIO_PULLUP:
lypinator 0:bb348c97df44 117 LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_UP);
lypinator 0:bb348c97df44 118 break;
lypinator 0:bb348c97df44 119 case GPIO_PULLDOWN:
lypinator 0:bb348c97df44 120 LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_DOWN);
lypinator 0:bb348c97df44 121 break;
lypinator 0:bb348c97df44 122 default:
lypinator 0:bb348c97df44 123 LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_NO);
lypinator 0:bb348c97df44 124 break;
lypinator 0:bb348c97df44 125 }
lypinator 0:bb348c97df44 126 }
lypinator 0:bb348c97df44 127
lypinator 0:bb348c97df44 128 static inline void stm_pin_SetAFPin(GPIO_TypeDef *gpio, PinName pin, uint32_t afnum)
lypinator 0:bb348c97df44 129 {
lypinator 0:bb348c97df44 130 uint32_t ll_pin = ll_pin_defines[STM_PIN(pin)];
lypinator 0:bb348c97df44 131
lypinator 0:bb348c97df44 132 if (STM_PIN(pin) > 7) {
lypinator 0:bb348c97df44 133 LL_GPIO_SetAFPin_8_15(gpio, ll_pin, afnum);
lypinator 0:bb348c97df44 134 } else {
lypinator 0:bb348c97df44 135 LL_GPIO_SetAFPin_0_7(gpio, ll_pin, afnum);
lypinator 0:bb348c97df44 136 }
lypinator 0:bb348c97df44 137 }
lypinator 0:bb348c97df44 138
lypinator 0:bb348c97df44 139 #endif