Aditya Mehrotra / mbed-dev

Dependents:   CAN_TEST SPIne_Plus_DYNO_SENSORS SPIne_Plus_v2 SPIne_Plus_Dyno_v2

Committer:
adimmit
Date:
Tue Mar 09 20:33:24 2021 +0000
Revision:
3:993b4d6ff61e
Parent:
0:083111ae2a11
added CAN3

Who changed what in which revision?

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