Fahad Mirza
/
Nucleo_HXC900
A demo application for HXC900 LoRaWAN module using Nucleo-L053R8.
Driver/hw_gpio.c@39:cb0e5a76ab15, 2019-01-24 (annotated)
- Committer:
- fahadmirza
- Date:
- Thu Jan 24 23:45:12 2019 +0000
- Revision:
- 39:cb0e5a76ab15
- Parent:
- 5:53302861bfea
Doxygen style header
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fahadmirza | 0:a0c5877bd360 | 1 | /* |
fahadmirza | 0:a0c5877bd360 | 2 | / _____) _ | | |
fahadmirza | 0:a0c5877bd360 | 3 | ( (____ _____ ____ _| |_ _____ ____| |__ |
fahadmirza | 0:a0c5877bd360 | 4 | \____ \| ___ | (_ _) ___ |/ ___) _ \ |
fahadmirza | 0:a0c5877bd360 | 5 | _____) ) ____| | | || |_| ____( (___| | | | |
fahadmirza | 0:a0c5877bd360 | 6 | (______/|_____)_|_|_| \__)_____)\____)_| |_| |
fahadmirza | 0:a0c5877bd360 | 7 | (C)2013 Semtech |
fahadmirza | 0:a0c5877bd360 | 8 | |
fahadmirza | 0:a0c5877bd360 | 9 | Description: Bleeper board GPIO driver implementation |
fahadmirza | 0:a0c5877bd360 | 10 | |
fahadmirza | 0:a0c5877bd360 | 11 | License: Revised BSD License, see LICENSE.TXT file include in the project |
fahadmirza | 0:a0c5877bd360 | 12 | |
fahadmirza | 0:a0c5877bd360 | 13 | Maintainer: Miguel Luis and Gregory Cristian |
fahadmirza | 0:a0c5877bd360 | 14 | */ |
fahadmirza | 0:a0c5877bd360 | 15 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 16 | * @file hw_gpio.c |
fahadmirza | 0:a0c5877bd360 | 17 | * @author MCD Application Team |
fahadmirza | 0:a0c5877bd360 | 18 | * @version V1.1.4 |
fahadmirza | 0:a0c5877bd360 | 19 | * @date 08-January-2018 |
fahadmirza | 0:a0c5877bd360 | 20 | * @brief driver for GPIO |
fahadmirza | 0:a0c5877bd360 | 21 | ****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 22 | * @attention |
fahadmirza | 0:a0c5877bd360 | 23 | * |
fahadmirza | 0:a0c5877bd360 | 24 | * <h2><center>© Copyright (c) 2017 STMicroelectronics International N.V. |
fahadmirza | 0:a0c5877bd360 | 25 | * All rights reserved.</center></h2> |
fahadmirza | 0:a0c5877bd360 | 26 | * |
fahadmirza | 0:a0c5877bd360 | 27 | * Redistribution and use in source and binary forms, with or without |
fahadmirza | 0:a0c5877bd360 | 28 | * modification, are permitted, provided that the following conditions are met: |
fahadmirza | 0:a0c5877bd360 | 29 | * |
fahadmirza | 0:a0c5877bd360 | 30 | * 1. Redistribution of source code must retain the above copyright notice, |
fahadmirza | 0:a0c5877bd360 | 31 | * this list of conditions and the following disclaimer. |
fahadmirza | 0:a0c5877bd360 | 32 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
fahadmirza | 0:a0c5877bd360 | 33 | * this list of conditions and the following disclaimer in the documentation |
fahadmirza | 0:a0c5877bd360 | 34 | * and/or other materials provided with the distribution. |
fahadmirza | 0:a0c5877bd360 | 35 | * 3. Neither the name of STMicroelectronics nor the names of other |
fahadmirza | 0:a0c5877bd360 | 36 | * contributors to this software may be used to endorse or promote products |
fahadmirza | 0:a0c5877bd360 | 37 | * derived from this software without specific written permission. |
fahadmirza | 0:a0c5877bd360 | 38 | * 4. This software, including modifications and/or derivative works of this |
fahadmirza | 0:a0c5877bd360 | 39 | * software, must execute solely and exclusively on microcontroller or |
fahadmirza | 0:a0c5877bd360 | 40 | * microprocessor devices manufactured by or for STMicroelectronics. |
fahadmirza | 0:a0c5877bd360 | 41 | * 5. Redistribution and use of this software other than as permitted under |
fahadmirza | 0:a0c5877bd360 | 42 | * this license is void and will automatically terminate your rights under |
fahadmirza | 0:a0c5877bd360 | 43 | * this license. |
fahadmirza | 0:a0c5877bd360 | 44 | * |
fahadmirza | 0:a0c5877bd360 | 45 | * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" |
fahadmirza | 0:a0c5877bd360 | 46 | * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT |
fahadmirza | 0:a0c5877bd360 | 47 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
fahadmirza | 0:a0c5877bd360 | 48 | * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY |
fahadmirza | 0:a0c5877bd360 | 49 | * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT |
fahadmirza | 0:a0c5877bd360 | 50 | * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
fahadmirza | 0:a0c5877bd360 | 51 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
fahadmirza | 0:a0c5877bd360 | 52 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
fahadmirza | 0:a0c5877bd360 | 53 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
fahadmirza | 0:a0c5877bd360 | 54 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
fahadmirza | 0:a0c5877bd360 | 55 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
fahadmirza | 0:a0c5877bd360 | 56 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
fahadmirza | 0:a0c5877bd360 | 57 | * |
fahadmirza | 0:a0c5877bd360 | 58 | ****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 59 | */ |
fahadmirza | 0:a0c5877bd360 | 60 | |
fahadmirza | 0:a0c5877bd360 | 61 | /* Includes ------------------------------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 62 | #include "hw.h" |
fahadmirza | 0:a0c5877bd360 | 63 | |
fahadmirza | 0:a0c5877bd360 | 64 | /* Private typedef -----------------------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 65 | /* Private define ------------------------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 66 | /* Private macro -------------------------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 67 | /* Private variables ---------------------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 68 | static GpioIrqHandler *GpioIrq[16] = { NULL }; |
fahadmirza | 0:a0c5877bd360 | 69 | |
fahadmirza | 0:a0c5877bd360 | 70 | /* Private function prototypes -----------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 71 | |
fahadmirza | 0:a0c5877bd360 | 72 | static uint8_t HW_GPIO_GetBitPos(uint16_t GPIO_Pin); |
fahadmirza | 0:a0c5877bd360 | 73 | static IRQn_Type MSP_GetIRQn( uint16_t gpioPin); |
fahadmirza | 0:a0c5877bd360 | 74 | |
fahadmirza | 0:a0c5877bd360 | 75 | /* Exported functions ---------------------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 76 | /*! |
fahadmirza | 0:a0c5877bd360 | 77 | * @brief Initializes the given GPIO object |
fahadmirza | 0:a0c5877bd360 | 78 | * |
fahadmirza | 0:a0c5877bd360 | 79 | * @param GPIOx: where x can be (A..E and H) |
fahadmirza | 0:a0c5877bd360 | 80 | * @param GPIO_Pin: specifies the port bit to be written. |
fahadmirza | 0:a0c5877bd360 | 81 | * This parameter can be one of GPIO_PIN_x where x can be (0..15). |
fahadmirza | 0:a0c5877bd360 | 82 | * All port bits are not necessarily available on all GPIOs. |
fahadmirza | 0:a0c5877bd360 | 83 | * @param [IN] initStruct GPIO_InitTypeDef intit structure |
fahadmirza | 0:a0c5877bd360 | 84 | * @retval none |
fahadmirza | 0:a0c5877bd360 | 85 | */ |
fahadmirza | 0:a0c5877bd360 | 86 | void HW_GPIO_Init( GPIO_TypeDef* port, uint16_t GPIO_Pin, GPIO_InitTypeDef* initStruct) |
fahadmirza | 0:a0c5877bd360 | 87 | { |
fahadmirza | 0:a0c5877bd360 | 88 | |
fahadmirza | 0:a0c5877bd360 | 89 | RCC_GPIO_CLK_ENABLE( (uint32_t) port); |
fahadmirza | 0:a0c5877bd360 | 90 | |
fahadmirza | 0:a0c5877bd360 | 91 | initStruct->Pin = GPIO_Pin ; |
fahadmirza | 0:a0c5877bd360 | 92 | |
fahadmirza | 0:a0c5877bd360 | 93 | HAL_GPIO_Init( port, initStruct ); |
fahadmirza | 0:a0c5877bd360 | 94 | } |
fahadmirza | 0:a0c5877bd360 | 95 | |
fahadmirza | 0:a0c5877bd360 | 96 | /*! |
fahadmirza | 0:a0c5877bd360 | 97 | * @brief Records the interrupt handler for the GPIO object |
fahadmirza | 0:a0c5877bd360 | 98 | * |
fahadmirza | 0:a0c5877bd360 | 99 | * @param GPIOx: where x can be (A..E and H) |
fahadmirza | 0:a0c5877bd360 | 100 | * @param GPIO_Pin: specifies the port bit to be written. |
fahadmirza | 0:a0c5877bd360 | 101 | * This parameter can be one of GPIO_PIN_x where x can be (0..15). |
fahadmirza | 0:a0c5877bd360 | 102 | * All port bits are not necessarily available on all GPIOs. |
fahadmirza | 0:a0c5877bd360 | 103 | * @param [IN] prio NVIC priority (0 is highest) |
fahadmirza | 0:a0c5877bd360 | 104 | * @param [IN] irqHandler points to the function to execute |
fahadmirza | 0:a0c5877bd360 | 105 | * @retval none |
fahadmirza | 0:a0c5877bd360 | 106 | */ |
fahadmirza | 0:a0c5877bd360 | 107 | void HW_GPIO_SetIrq( GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, uint32_t prio, GpioIrqHandler *irqHandler ) |
fahadmirza | 0:a0c5877bd360 | 108 | { |
fahadmirza | 0:a0c5877bd360 | 109 | IRQn_Type IRQnb; |
fahadmirza | 0:a0c5877bd360 | 110 | |
fahadmirza | 0:a0c5877bd360 | 111 | uint32_t BitPos = HW_GPIO_GetBitPos( GPIO_Pin ) ; |
fahadmirza | 0:a0c5877bd360 | 112 | |
fahadmirza | 0:a0c5877bd360 | 113 | if ( irqHandler != NULL) |
fahadmirza | 0:a0c5877bd360 | 114 | { |
fahadmirza | 0:a0c5877bd360 | 115 | GpioIrq[ BitPos ] = irqHandler; |
fahadmirza | 0:a0c5877bd360 | 116 | |
fahadmirza | 0:a0c5877bd360 | 117 | IRQnb = MSP_GetIRQn( GPIO_Pin ); |
fahadmirza | 0:a0c5877bd360 | 118 | |
fahadmirza | 0:a0c5877bd360 | 119 | HAL_NVIC_SetPriority( IRQnb , prio, 0); |
fahadmirza | 0:a0c5877bd360 | 120 | |
fahadmirza | 0:a0c5877bd360 | 121 | HAL_NVIC_EnableIRQ( IRQnb ); |
fahadmirza | 0:a0c5877bd360 | 122 | } |
fahadmirza | 0:a0c5877bd360 | 123 | else |
fahadmirza | 0:a0c5877bd360 | 124 | { |
fahadmirza | 0:a0c5877bd360 | 125 | GpioIrq[ BitPos ] = NULL; |
fahadmirza | 0:a0c5877bd360 | 126 | } |
fahadmirza | 0:a0c5877bd360 | 127 | } |
fahadmirza | 0:a0c5877bd360 | 128 | |
fahadmirza | 0:a0c5877bd360 | 129 | /*! |
fahadmirza | 0:a0c5877bd360 | 130 | * @brief Execute the interrupt from the object |
fahadmirza | 0:a0c5877bd360 | 131 | * |
fahadmirza | 0:a0c5877bd360 | 132 | * @param GPIO_Pin: specifies the port bit to be written. |
fahadmirza | 0:a0c5877bd360 | 133 | * This parameter can be one of GPIO_PIN_x where x can be (0..15). |
fahadmirza | 0:a0c5877bd360 | 134 | * All port bits are not necessarily available on all GPIOs. |
fahadmirza | 0:a0c5877bd360 | 135 | * @retval none |
fahadmirza | 0:a0c5877bd360 | 136 | */ |
fahadmirza | 0:a0c5877bd360 | 137 | void HW_GPIO_IrqHandler( uint16_t GPIO_Pin ) |
fahadmirza | 0:a0c5877bd360 | 138 | { |
fahadmirza | 0:a0c5877bd360 | 139 | uint32_t BitPos = HW_GPIO_GetBitPos( GPIO_Pin ); |
fahadmirza | 0:a0c5877bd360 | 140 | |
fahadmirza | 0:a0c5877bd360 | 141 | if ( GpioIrq[ BitPos ] != NULL) |
fahadmirza | 0:a0c5877bd360 | 142 | { |
fahadmirza | 0:a0c5877bd360 | 143 | GpioIrq[ BitPos ] ( ); |
fahadmirza | 0:a0c5877bd360 | 144 | } |
fahadmirza | 0:a0c5877bd360 | 145 | } |
fahadmirza | 0:a0c5877bd360 | 146 | |
fahadmirza | 0:a0c5877bd360 | 147 | /*! |
fahadmirza | 0:a0c5877bd360 | 148 | * @brief Writes the given value to the GPIO output |
fahadmirza | 0:a0c5877bd360 | 149 | * |
fahadmirza | 0:a0c5877bd360 | 150 | * @param GPIO_Pin: specifies the port bit to be written. |
fahadmirza | 0:a0c5877bd360 | 151 | * This parameter can be one of GPIO_PIN_x where x can be (0..15). |
fahadmirza | 0:a0c5877bd360 | 152 | * All port bits are not necessarily available on all GPIOs. |
fahadmirza | 0:a0c5877bd360 | 153 | * @param [IN] value New GPIO output value |
fahadmirza | 0:a0c5877bd360 | 154 | * @retval none |
fahadmirza | 0:a0c5877bd360 | 155 | */ |
fahadmirza | 0:a0c5877bd360 | 156 | void HW_GPIO_Write( GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, uint32_t value ) |
fahadmirza | 0:a0c5877bd360 | 157 | { |
fahadmirza | 0:a0c5877bd360 | 158 | HAL_GPIO_WritePin( GPIOx, GPIO_Pin , (GPIO_PinState) value ); |
fahadmirza | 0:a0c5877bd360 | 159 | } |
fahadmirza | 0:a0c5877bd360 | 160 | |
fahadmirza | 0:a0c5877bd360 | 161 | /*! |
fahadmirza | 0:a0c5877bd360 | 162 | * @brief Reads the current GPIO input value |
fahadmirza | 0:a0c5877bd360 | 163 | * |
fahadmirza | 0:a0c5877bd360 | 164 | * @param GPIOx: where x can be (A..E and H) |
fahadmirza | 0:a0c5877bd360 | 165 | * @param GPIO_Pin: specifies the port bit to be written. |
fahadmirza | 0:a0c5877bd360 | 166 | * This parameter can be one of GPIO_PIN_x where x can be (0..15). |
fahadmirza | 0:a0c5877bd360 | 167 | * All port bits are not necessarily available on all GPIOs. |
fahadmirza | 0:a0c5877bd360 | 168 | * @retval value Current GPIO input value |
fahadmirza | 0:a0c5877bd360 | 169 | */ |
fahadmirza | 0:a0c5877bd360 | 170 | uint32_t HW_GPIO_Read( GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin ) |
fahadmirza | 0:a0c5877bd360 | 171 | { |
fahadmirza | 0:a0c5877bd360 | 172 | return HAL_GPIO_ReadPin( GPIOx, GPIO_Pin); |
fahadmirza | 0:a0c5877bd360 | 173 | } |
fahadmirza | 0:a0c5877bd360 | 174 | |
fahadmirza | 0:a0c5877bd360 | 175 | /* Private functions ---------------------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 176 | |
fahadmirza | 0:a0c5877bd360 | 177 | /*! |
fahadmirza | 0:a0c5877bd360 | 178 | * @brief Get the position of the bit set in the GPIO_Pin |
fahadmirza | 0:a0c5877bd360 | 179 | * @param GPIO_Pin: specifies the port bit to be written. |
fahadmirza | 0:a0c5877bd360 | 180 | * This parameter can be one of GPIO_PIN_x where x can be (0..15). |
fahadmirza | 0:a0c5877bd360 | 181 | * All port bits are not necessarily available on all GPIOs. |
fahadmirza | 0:a0c5877bd360 | 182 | * @retval the position of the bit |
fahadmirza | 0:a0c5877bd360 | 183 | */ |
fahadmirza | 0:a0c5877bd360 | 184 | static uint8_t HW_GPIO_GetBitPos(uint16_t GPIO_Pin) |
fahadmirza | 0:a0c5877bd360 | 185 | { |
fahadmirza | 0:a0c5877bd360 | 186 | uint8_t PinPos=0; |
fahadmirza | 0:a0c5877bd360 | 187 | |
fahadmirza | 0:a0c5877bd360 | 188 | if ( ( GPIO_Pin & 0xFF00 ) != 0) { PinPos |= 0x8; } |
fahadmirza | 0:a0c5877bd360 | 189 | if ( ( GPIO_Pin & 0xF0F0 ) != 0) { PinPos |= 0x4; } |
fahadmirza | 0:a0c5877bd360 | 190 | if ( ( GPIO_Pin & 0xCCCC ) != 0) { PinPos |= 0x2; } |
fahadmirza | 0:a0c5877bd360 | 191 | if ( ( GPIO_Pin & 0xAAAA ) != 0) { PinPos |= 0x1; } |
fahadmirza | 0:a0c5877bd360 | 192 | |
fahadmirza | 0:a0c5877bd360 | 193 | return PinPos; |
fahadmirza | 0:a0c5877bd360 | 194 | } |
fahadmirza | 0:a0c5877bd360 | 195 | |
fahadmirza | 0:a0c5877bd360 | 196 | |
fahadmirza | 0:a0c5877bd360 | 197 | /*! |
fahadmirza | 0:a0c5877bd360 | 198 | * GPIO IRQ handler function prototype |
fahadmirza | 0:a0c5877bd360 | 199 | */ |
fahadmirza | 0:a0c5877bd360 | 200 | static IRQn_Type MSP_GetIRQn(uint16_t GPIO_Pin) |
fahadmirza | 0:a0c5877bd360 | 201 | { |
fahadmirza | 0:a0c5877bd360 | 202 | switch (GPIO_Pin) |
fahadmirza | 0:a0c5877bd360 | 203 | { |
fahadmirza | 0:a0c5877bd360 | 204 | case GPIO_PIN_0: |
fahadmirza | 0:a0c5877bd360 | 205 | case GPIO_PIN_1: return EXTI0_1_IRQn; |
fahadmirza | 0:a0c5877bd360 | 206 | case GPIO_PIN_2: |
fahadmirza | 0:a0c5877bd360 | 207 | case GPIO_PIN_3: return EXTI2_3_IRQn; |
fahadmirza | 0:a0c5877bd360 | 208 | case GPIO_PIN_4: |
fahadmirza | 0:a0c5877bd360 | 209 | case GPIO_PIN_5: |
fahadmirza | 0:a0c5877bd360 | 210 | case GPIO_PIN_6: |
fahadmirza | 0:a0c5877bd360 | 211 | case GPIO_PIN_7: |
fahadmirza | 0:a0c5877bd360 | 212 | case GPIO_PIN_8: |
fahadmirza | 0:a0c5877bd360 | 213 | case GPIO_PIN_9: |
fahadmirza | 0:a0c5877bd360 | 214 | case GPIO_PIN_10: |
fahadmirza | 0:a0c5877bd360 | 215 | case GPIO_PIN_11: |
fahadmirza | 0:a0c5877bd360 | 216 | case GPIO_PIN_12: |
fahadmirza | 0:a0c5877bd360 | 217 | case GPIO_PIN_13: |
fahadmirza | 0:a0c5877bd360 | 218 | case GPIO_PIN_14: |
fahadmirza | 0:a0c5877bd360 | 219 | case GPIO_PIN_15: |
fahadmirza | 0:a0c5877bd360 | 220 | default: return EXTI4_15_IRQn; |
fahadmirza | 0:a0c5877bd360 | 221 | } |
fahadmirza | 0:a0c5877bd360 | 222 | } |
fahadmirza | 0:a0c5877bd360 | 223 | |
fahadmirza | 0:a0c5877bd360 | 224 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
fahadmirza | 0:a0c5877bd360 | 225 |