fsl_gpio.h 10.9 KB
Newer Older
T
tanek liang 已提交
1
/*
2
 * The Clear BSD License
T
tanek liang 已提交
3 4
 * Copyright (c) 2016, Freescale Semiconductor, Inc.
 * Copyright 2016-2017 NXP
5
 * All rights reserved.
T
tanek liang 已提交
6 7
 *
 * Redistribution and use in source and binary forms, with or without modification,
8 9
 * are permitted (subject to the limitations in the disclaimer below) provided
 *  that the following conditions are met:
T
tanek liang 已提交
10 11 12 13 14 15 16 17 18 19 20 21
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
22
 * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
T
tanek liang 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _FSL_GPIO_H_
#define _FSL_GPIO_H_

#include "fsl_common.h"

/*!
 * @addtogroup gpio_driver
 * @{
 */

/*******************************************************************************
 * Definitions
 ******************************************************************************/

/*! @name Driver version */
/*@{*/
/*! @brief GPIO driver version 2.0.1. */
#define FSL_GPIO_DRIVER_VERSION (MAKE_VERSION(2, 0, 1))
/*@}*/

/*! @brief GPIO direction definition. */
typedef enum _gpio_pin_direction
{
58
    kGPIO_DigitalInput = 0U,  /*!< Set current pin as digital input.*/
T
tanek liang 已提交
59 60 61 62 63 64
    kGPIO_DigitalOutput = 1U, /*!< Set current pin as digital output.*/
} gpio_pin_direction_t;

/*! @brief GPIO interrupt mode definition. */
typedef enum _gpio_interrupt_mode
{
65 66 67 68 69
    kGPIO_NoIntmode = 0U,              /*!< Set current pin general IO functionality.*/
    kGPIO_IntLowLevel = 1U,            /*!< Set current pin interrupt is low-level sensitive.*/
    kGPIO_IntHighLevel = 2U,           /*!< Set current pin interrupt is high-level sensitive.*/
    kGPIO_IntRisingEdge = 3U,          /*!< Set current pin interrupt is rising-edge sensitive.*/
    kGPIO_IntFallingEdge = 4U,         /*!< Set current pin interrupt is falling-edge sensitive.*/
T
tanek liang 已提交
70 71 72 73 74 75
    kGPIO_IntRisingOrFallingEdge = 5U, /*!< Enable the edge select bit to override the ICR register's configuration.*/
} gpio_interrupt_mode_t;

/*! @brief GPIO Init structure definition. */
typedef struct _gpio_pin_config
{
76 77 78 79
    gpio_pin_direction_t direction; /*!< Specifies the pin direction. */
    uint8_t outputLogic;            /*!< Set a default output logic, which has no use in input */
    gpio_interrupt_mode_t
        interruptMode; /*!< Specifies the pin interrupt mode, a value of @ref gpio_interrupt_mode_t. */
T
tanek liang 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
} gpio_pin_config_t;

/*******************************************************************************
 * API
 ******************************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif

/*!
 * @name GPIO Initialization and Configuration functions
 * @{
 */

/*!
 * @brief Initializes the GPIO peripheral according to the specified
 *        parameters in the initConfig.
 *
 * @param base GPIO base pointer.
 * @param pin Specifies the pin number
 * @param initConfig pointer to a @ref gpio_pin_config_t structure that
 *        contains the configuration information.
 */
104
void GPIO_PinInit(GPIO_Type *base, uint32_t pin, const gpio_pin_config_t *Config);
T
tanek liang 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
/*@}*/

/*!
 * @name GPIO Reads and Write Functions
 * @{
 */

/*!
 * @brief Sets the output level of the individual GPIO pin to logic 1 or 0.
 *
 * @param base GPIO base pointer.
 * @param pin GPIO port pin number.
 * @param output GPIOpin output logic level.
 *        - 0: corresponding pin output low-logic level.
 *        - 1: corresponding pin output high-logic level.
 */
121
void GPIO_PinWrite(GPIO_Type *base, uint32_t pin, uint8_t output);
T
tanek liang 已提交
122 123 124 125 126

/*!
 * @brief Sets the output level of the individual GPIO pin to logic 1 or 0.
 * @deprecated Do not use this function.  It has been superceded by @ref GPIO_PinWrite.
 */
127
static inline void GPIO_WritePinOutput(GPIO_Type *base, uint32_t pin, uint8_t output)
T
tanek liang 已提交
128 129 130 131 132 133 134 135 136 137
{
    GPIO_PinWrite(base, pin, output);
}

/*!
 * @brief Sets the output level of the multiple GPIO pins to the logic 1.
 *
 * @param base GPIO peripheral base pointer (GPIO1, GPIO2, GPIO3, and so on.)
 * @param mask GPIO pin number macro
 */
138
static inline void GPIO_PortSet(GPIO_Type *base, uint32_t mask)
T
tanek liang 已提交
139
{
140 141 142
#if (defined(FSL_FEATURE_IGPIO_HAS_DR_SET) && (FSL_FEATURE_IGPIO_HAS_DR_SET == 1))
    base->DR_SET = mask;
#else
T
tanek liang 已提交
143
    base->DR |= mask;
144
#endif /* FSL_FEATURE_IGPIO_HAS_DR_SET */
T
tanek liang 已提交
145 146 147 148 149 150
}

/*!
 * @brief Sets the output level of the multiple GPIO pins to the logic 1.
 * @deprecated Do not use this function.  It has been superceded by @ref GPIO_PortSet.
 */
151
static inline void GPIO_SetPinsOutput(GPIO_Type *base, uint32_t mask)
T
tanek liang 已提交
152 153 154 155 156 157 158 159 160 161
{
    GPIO_PortSet(base, mask);
}

/*!
 * @brief Sets the output level of the multiple GPIO pins to the logic 0.
 *
 * @param base GPIO peripheral base pointer (GPIO1, GPIO2, GPIO3, and so on.)
 * @param mask GPIO pin number macro
 */
162
static inline void GPIO_PortClear(GPIO_Type *base, uint32_t mask)
T
tanek liang 已提交
163
{
164 165 166
#if (defined(FSL_FEATURE_IGPIO_HAS_DR_CLEAR) && (FSL_FEATURE_IGPIO_HAS_DR_CLEAR == 1))
    base->DR_CLEAR = mask;
#else
T
tanek liang 已提交
167
    base->DR &= ~mask;
168
#endif /* FSL_FEATURE_IGPIO_HAS_DR_CLEAR */
T
tanek liang 已提交
169 170 171 172 173 174
}

/*!
 * @brief Sets the output level of the multiple GPIO pins to the logic 0.
 * @deprecated Do not use this function.  It has been superceded by @ref GPIO_PortClear.
 */
175
static inline void GPIO_ClearPinsOutput(GPIO_Type *base, uint32_t mask)
T
tanek liang 已提交
176 177 178 179
{
    GPIO_PortClear(base, mask);
}

180 181 182 183 184 185 186 187 188 189 190 191 192
/*!
 * @brief Reverses the current output logic of the multiple GPIO pins.
 *
 * @param base GPIO peripheral base pointer (GPIO1, GPIO2, GPIO3, and so on.)
 * @param mask GPIO pin number macro
 */
static inline void GPIO_PortToggle(GPIO_Type *base, uint32_t mask)
{
#if (defined(FSL_FEATURE_IGPIO_HAS_DR_TOGGLE) && (FSL_FEATURE_IGPIO_HAS_DR_TOGGLE == 1))
    base->DR_TOGGLE = mask;
#endif /* FSL_FEATURE_IGPIO_HAS_DR_TOGGLE */
}

T
tanek liang 已提交
193 194 195 196 197 198 199
/*!
 * @brief Reads the current input value of the GPIO port.
 *
 * @param base GPIO base pointer.
 * @param pin GPIO port pin number.
 * @retval GPIO port input value.
 */
200
static inline uint32_t GPIO_PinRead(GPIO_Type *base, uint32_t pin)
T
tanek liang 已提交
201 202 203 204 205 206 207 208 209 210
{
    assert(pin < 32);

    return (((base->DR) >> pin) & 0x1U);
}

/*!
 * @brief Reads the current input value of the GPIO port.
 * @deprecated Do not use this function.  It has been superceded by @ref GPIO_PinRead.
 */
211
static inline uint32_t GPIO_ReadPinInput(GPIO_Type *base, uint32_t pin)
T
tanek liang 已提交
212 213 214 215 216 217 218 219 220 221
{
    return GPIO_PinRead(base, pin);
}
/*@}*/

/*!
 * @name GPIO Reads Pad Status Functions
 * @{
 */

222 223 224 225 226 227 228 229
/*!
* @brief Reads the current GPIO pin pad status.
*
* @param base GPIO base pointer.
* @param pin GPIO port pin number.
* @retval GPIO pin pad status value.
*/
static inline uint8_t GPIO_PinReadPadStatus(GPIO_Type *base, uint32_t pin)
T
tanek liang 已提交
230 231 232 233 234 235
{
    assert(pin < 32);

    return (uint8_t)(((base->PSR) >> pin) & 0x1U);
}

236 237 238 239 240
/*!
* @brief Reads the current GPIO pin pad status.
* @deprecated Do not use this function.  It has been superceded by @ref GPIO_PinReadPadStatus.
*/
static inline uint8_t GPIO_ReadPadStatus(GPIO_Type *base, uint32_t pin)
T
tanek liang 已提交
241 242 243
{
    return GPIO_PinReadPadStatus(base, pin);
}
244

T
tanek liang 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
/*@}*/

/*!
 * @name Interrupts and flags management functions
 * @{
 */

/*!
 * @brief Sets the current pin interrupt mode.
 *
 * @param base GPIO base pointer.
 * @param pin GPIO port pin number.
 * @param pininterruptMode pointer to a @ref gpio_interrupt_mode_t structure
 *        that contains the interrupt mode information.
 */
260
void GPIO_PinSetInterruptConfig(GPIO_Type *base, uint32_t pin, gpio_interrupt_mode_t pinInterruptMode);
T
tanek liang 已提交
261 262 263 264 265

/*!
 * @brief Sets the current pin interrupt mode.
 * @deprecated Do not use this function.  It has been superceded by @ref GPIO_PinSetInterruptConfig.
 */
266
static inline void GPIO_SetPinInterruptConfig(GPIO_Type *base, uint32_t pin, gpio_interrupt_mode_t pinInterruptMode)
T
tanek liang 已提交
267 268 269 270 271 272 273 274 275 276
{
    GPIO_PinSetInterruptConfig(base, pin, pinInterruptMode);
}

/*!
 * @brief Enables the specific pin interrupt.
 *
 * @param base GPIO base pointer.
 * @param mask GPIO pin number macro.
 */
277
static inline void GPIO_PortEnableInterrupts(GPIO_Type *base, uint32_t mask)
T
tanek liang 已提交
278 279 280 281 282 283 284 285 286 287
{
    base->IMR |= mask;
}

/*!
 * @brief Enables the specific pin interrupt.
 *
 * @param base GPIO base pointer.
 * @param mask GPIO pin number macro.
 */
288
static inline void GPIO_EnableInterrupts(GPIO_Type *base, uint32_t mask)
T
tanek liang 已提交
289 290 291 292 293 294 295 296 297 298
{
    GPIO_PortEnableInterrupts(base, mask);
}

/*!
 * @brief Disables the specific pin interrupt.
 *
 * @param base GPIO base pointer.
 * @param mask GPIO pin number macro.
 */
299
static inline void GPIO_PortDisableInterrupts(GPIO_Type *base, uint32_t mask)
T
tanek liang 已提交
300 301 302 303 304 305 306 307
{
    base->IMR &= ~mask;
}

/*!
 * @brief Disables the specific pin interrupt.
 * @deprecated Do not use this function.  It has been superceded by @ref GPIO_PortDisableInterrupts.
 */
308
static inline void GPIO_DisableInterrupts(GPIO_Type *base, uint32_t mask)
T
tanek liang 已提交
309 310 311 312 313 314 315 316 317 318
{
    GPIO_PortDisableInterrupts(base, mask);
}

/*!
 * @brief Reads individual pin interrupt status.
 *
 * @param base GPIO base pointer.
 * @retval current pin interrupt status flag.
 */
319
static inline uint32_t GPIO_PortGetInterruptFlags(GPIO_Type *base)
T
tanek liang 已提交
320 321 322 323 324 325 326 327 328 329
{
    return base->ISR;
}

/*!
 * @brief Reads individual pin interrupt status.
 *
 * @param base GPIO base pointer.
 * @retval current pin interrupt status flag.
 */
330
static inline uint32_t GPIO_GetPinsInterruptFlags(GPIO_Type *base)
T
tanek liang 已提交
331 332 333 334 335 336 337 338 339 340 341
{
    return GPIO_PortGetInterruptFlags(base);
}

/*!
 * @brief Clears pin interrupt flag. Status flags are cleared by
 *        writing a 1 to the corresponding bit position.
 *
 * @param base GPIO base pointer.
 * @param mask GPIO pin number macro.
 */
342
static inline void GPIO_PortClearInterruptFlags(GPIO_Type *base, uint32_t mask)
T
tanek liang 已提交
343 344 345 346 347 348 349 350 351 352 353
{
    base->ISR = mask;
}

/*!
 * @brief Clears pin interrupt flag. Status flags are cleared by
 *        writing a 1 to the corresponding bit position.
 *
 * @param base GPIO base pointer.
 * @param mask GPIO pin number macro.
 */
354
static inline void GPIO_ClearPinsInterruptFlags(GPIO_Type *base, uint32_t mask)
T
tanek liang 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368
{
    GPIO_PortClearInterruptFlags(base, mask);
}
/*@}*/

#if defined(__cplusplus)
}
#endif

/*!
 * @}
 */

#endif /* _FSL_GPIO_H_*/