gpio.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2011-2012, Freescale Semiconductor, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /*!
  31. * @file gpio.h
  32. * @brief Defines related to the GPIO controller and used by gpio.c
  33. * @ingroup diag_gpio
  34. */
  35. #ifndef __GPIO_H__
  36. #define __GPIO_H__
  37. //! @addtogroup diag_gpio
  38. //! @{
  39. ////////////////////////////////////////////////////////////////////////////////
  40. // Definitions
  41. ////////////////////////////////////////////////////////////////////////////////
  42. //! @brief Available GPIO ports.
  43. typedef enum {
  44. GPIO_NONE = 0,
  45. GPIO_PORT1 = 1,
  46. GPIO_PORT2 = 2,
  47. GPIO_PORT3 = 3,
  48. GPIO_PORT4 = 4,
  49. GPIO_PORT5 = 5,
  50. GPIO_PORT6 = 6,
  51. GPIO_PORT7 = 7,
  52. } GPIO_PORT;
  53. //! @name GPIO bitfield values
  54. //@{
  55. #define GPIO_GDIR_INPUT 0 //!< GPIO pin is input
  56. #define GPIO_GDIR_OUTPUT 1 //!< GPIO pin is output
  57. #define GPIO_LOW_LEVEL 0 //!< GPIO pin is low
  58. #define GPIO_HIGH_LEVEL 1 //!< GPIO pin is high
  59. #define GPIO_ICR_LOW_LEVEL 0 //!< Interrupt is low-level
  60. #define GPIO_ICR_HIGH_LEVEL 1 //!< Interrupt is high-level
  61. #define GPIO_ICR_RISE_EDGE 2 //!< Interrupt is rising edge
  62. #define GPIO_ICR_FALL_EDGE 3 //!< Interrupt is falling edge
  63. #define GPIO_IMR_MASKED 0 //!< Interrupt is masked
  64. #define GPIO_IMR_UNMASKED 1 //!< Interrupt is unmasked
  65. #define GPIO_ISR_NOT_ASSERTED 0 //!< Interrupt is not asserted
  66. #define GPIO_ISR_ASSERTED 1 //!< Interrupt is asserted
  67. #define GPIO_EDGE_SEL_DISABLE 0 //!< Edge select is disabled
  68. #define GPIO_EDGE_SEL_ENABLE 1 //!< Edge select is enabled
  69. //@}
  70. ////////////////////////////////////////////////////////////////////////////////
  71. // API
  72. ////////////////////////////////////////////////////////////////////////////////
  73. #if defined(__cplusplus)
  74. extern "C" {
  75. #endif
  76. /*!
  77. * @brief Returns the number of available GPIO instances.
  78. * @return An integer number of GPIO instances on this hardware.
  79. */
  80. int32_t gpio_get_port_count(void);
  81. /*!
  82. * @brief Sets a GPIO pin to GPIO mode in the IOMUX controller.
  83. *
  84. * @retval SUCCESS
  85. * @retval INVALID_PARAMETER
  86. */
  87. int gpio_set_gpio(int32_t port, int32_t pin);
  88. /*!
  89. * @brief Sets the GPIO direction for the specified pin.
  90. *
  91. * @param port GPIO module instance, GPIO_PORT1, GPIO_PORT2, ... gpio_get_port_count().
  92. * @param pin GPIO pin 0 to 31.
  93. * @param dir Direction for the pin. GPIO_GDIR_INPUT(0) or GPIO_GDIR_OUTPUT(1).
  94. * @return INVALID_PARAMETER(-1)
  95. */
  96. int32_t gpio_set_direction(int32_t port, int32_t pin, int32_t dir);
  97. /*!
  98. * @brief Returns the current direction for the specified pin.
  99. *
  100. * @param port GPIO module instance, GPIO_PORT1, GPIO_PORT2, ... gpio_get_port_count().
  101. * @param pin GPIO pin 0 to 31.
  102. * @retval GPIO_GDIR_INPUT The pin is currently set as an input.
  103. * @retval GPIO_GDIR_OUTPUT The pin is currently set as an output.
  104. */
  105. int32_t gpio_get_direction(int32_t port, int32_t pin);
  106. /*!
  107. * @brief Sets the GPIO level(high or low) for the specified pin.
  108. *
  109. * @warning Fails if pin is not configured as an output.
  110. *
  111. * @param port GPIO module instance, GPIO_PORT1, GPIO_PORT2, ... gpio_get_port_count().
  112. * @param pin GPIO pin 0 to 31.
  113. * @param level GPIO_LOW_LEVEL(0), GPIO_HIGH_LEVEL(1)
  114. * @return INVALID_PARAMETER(-1)
  115. */
  116. int32_t gpio_set_level(int32_t port, int32_t pin, uint32_t level);
  117. /*!
  118. * Gets the GPIO level(high or low) for the specified pin.
  119. *
  120. * @note Returns level for both input and output configured pins.
  121. *
  122. * @param port GPIO module instance, GPIO_PORT1, GPIO_PORT2, ... gpio_get_port_count().
  123. * @param pin GPIO pin 0 to 31.
  124. * @retval INVALID_PARAMETER(-1),
  125. * @retval GPIO_LOW_LEVEL(0),
  126. * @retval GPIO_HIGH_LEVEL(1)
  127. */
  128. int32_t gpio_get_level(int32_t port, int32_t pin);
  129. /*!
  130. * @brief Configures the interrupt condition for the specified GPIO input pin.
  131. *
  132. * @param port GPIO module instance, GPIO_PORT1, GPIO_PORT2, ... gpio_get_port_count().
  133. * @param pin GPIO pin 0 to 31.
  134. * @param config Interrupt condition for the pin. GPIO_ICR_LOW_LEVEL(0), GPIO_ICR_HIGH_LEVEL(1),
  135. * GPIO_ICR_RISE_EDGE(2), GPIO_ICR_FALL_EDGE(3)
  136. * @return INVALID_PARAMETER(-1)
  137. */
  138. int32_t gpio_set_interrupt_config(int32_t port, int32_t pin, int32_t config);
  139. /*!
  140. * @brief Enables/Disables the interrupt for the specified GPIO input pin.
  141. *
  142. * @param port GPIO module instance, GPIO_PORT1, GPIO_PORT2, ... gpio_get_port_count().
  143. * @param pin GPIO pin 0 to 31.
  144. * @param mask interrupt mask for the pin. GPIO_IMR_MASKED(0), GPIO_IMR_UNMASKED(1)
  145. * @return INVALID_PARAMETER(-1)
  146. */
  147. int32_t gpio_set_interrupt_mask(int32_t port, int32_t pin, int32_t mask);
  148. /*!
  149. * @brief Gets the GPIO interrupt status for the specified pin.
  150. *
  151. * @param port GPIO module instance, GPIO_PORT1, GPIO_PORT2, ... gpio_get_port_count().
  152. * @param pin GPIO pin 0 to 31.
  153. * @return INVALID_PARAMETER(-1), GPIO_ISR_NOT_ASSERTED(0), GPIO_ISR_ASSERTED(1)
  154. */
  155. int32_t gpio_get_interrupt_status(int32_t port, int32_t pin);
  156. /*!
  157. * @brief Clears the GPIO interrupt for the specified pin.
  158. *
  159. * @param port GPIO module instance, GPIO_PORT1, GPIO_PORT2, ... gpio_get_port_count().
  160. * @param pin GPIO pin 0 to 31.
  161. * @return INVALID_PARAMETER(-1)
  162. */
  163. int32_t gpio_clear_interrupt(int32_t port, int32_t pin);
  164. #if defined(__cplusplus)
  165. }
  166. #endif
  167. //! @}
  168. #endif //__GPIO_H__
  169. ////////////////////////////////////////////////////////////////////////////////
  170. // EOF
  171. ////////////////////////////////////////////////////////////////////////////////