gpio.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**
  2. * @file gpio.h
  3. * @brief General-Purpose Input/Output (GPIO) function prototypes and data types.
  4. */
  5. /* ****************************************************************************
  6. * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
  22. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. * OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. * Except as contained in this notice, the name of Maxim Integrated
  27. * Products, Inc. shall not be used except as stated in the Maxim Integrated
  28. * Products, Inc. Branding Policy.
  29. *
  30. * The mere transfer of this software does not imply any licenses
  31. * of trade secrets, proprietary technology, copyrights, patents,
  32. * trademarks, maskwork rights, or any other form of intellectual
  33. * property whatsoever. Maxim Integrated Products, Inc. retains all
  34. * ownership rights.
  35. *
  36. * $Date: 2018-12-18 15:37:22 -0600 (Tue, 18 Dec 2018) $
  37. * $Revision: 40072 $
  38. *
  39. *************************************************************************** */
  40. /* Define to prevent redundant inclusion */
  41. #ifndef _GPIO_H_
  42. #define _GPIO_H_
  43. /* **** Includes **** */
  44. #include "gpio_regs.h"
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * @defgroup gpio General-Purpose Input/Output (GPIO)
  50. * @ingroup periphlibs
  51. * @{
  52. */
  53. /* **** Definitions **** */
  54. /**
  55. * @defgroup gpio_port_pin Port and Pin Definitions
  56. * @ingroup gpio
  57. * @{
  58. * @defgroup gpio_port Port Definitions
  59. * @ingroup gpio_port_pin
  60. * @{
  61. */
  62. #define PORT_0 ((uint32_t)(0UL)) /**< Port 0 Define*/
  63. #define PORT_1 ((uint32_t)(1UL)) /**< Port 1 Define*/
  64. #define PORT_2 ((uint32_t)(2UL)) /**< Port 2 Define*/
  65. #define PORT_3 ((uint32_t)(3UL)) /**< Port 3 Define*/
  66. #define PORT_4 ((uint32_t)(4UL)) /**< Port 4 Define*/
  67. /**@} end of gpio_port group*/
  68. /**
  69. * @defgroup gpio_pin Pin Definitions
  70. * @ingroup gpio_port_pin
  71. * @{
  72. */
  73. #define PIN_0 ((uint32_t)(1UL << 0)) /**< Pin 0 Define */
  74. #define PIN_1 ((uint32_t)(1UL << 1)) /**< Pin 1 Define */
  75. #define PIN_2 ((uint32_t)(1UL << 2)) /**< Pin 2 Define */
  76. #define PIN_3 ((uint32_t)(1UL << 3)) /**< Pin 3 Define */
  77. #define PIN_4 ((uint32_t)(1UL << 4)) /**< Pin 4 Define */
  78. #define PIN_5 ((uint32_t)(1UL << 5)) /**< Pin 5 Define */
  79. #define PIN_6 ((uint32_t)(1UL << 6)) /**< Pin 6 Define */
  80. #define PIN_7 ((uint32_t)(1UL << 7)) /**< Pin 7 Define */
  81. #define PIN_8 ((uint32_t)(1UL << 8)) /**< Pin 8 Define */
  82. #define PIN_9 ((uint32_t)(1UL << 9)) /**< Pin 9 Define */
  83. #define PIN_10 ((uint32_t)(1UL << 10)) /**< Pin 10 Define */
  84. #define PIN_11 ((uint32_t)(1UL << 11)) /**< Pin 11 Define */
  85. #define PIN_12 ((uint32_t)(1UL << 12)) /**< Pin 12 Define */
  86. #define PIN_13 ((uint32_t)(1UL << 13)) /**< Pin 13 Define */
  87. #define PIN_14 ((uint32_t)(1UL << 14)) /**< Pin 14 Define */
  88. #define PIN_15 ((uint32_t)(1UL << 15)) /**< Pin 15 Define */
  89. #define PIN_16 ((uint32_t)(1UL << 16)) /**< Pin 16 Define */
  90. #define PIN_17 ((uint32_t)(1UL << 17)) /**< Pin 17 Define */
  91. #define PIN_18 ((uint32_t)(1UL << 18)) /**< Pin 18 Define */
  92. #define PIN_19 ((uint32_t)(1UL << 19)) /**< Pin 19 Define */
  93. #define PIN_20 ((uint32_t)(1UL << 20)) /**< Pin 20 Define */
  94. #define PIN_21 ((uint32_t)(1UL << 21)) /**< Pin 21 Define */
  95. #define PIN_22 ((uint32_t)(1UL << 22)) /**< Pin 22 Define */
  96. #define PIN_23 ((uint32_t)(1UL << 23)) /**< Pin 23 Define */
  97. #define PIN_24 ((uint32_t)(1UL << 24)) /**< Pin 24 Define */
  98. #define PIN_25 ((uint32_t)(1UL << 25)) /**< Pin 25 Define */
  99. #define PIN_26 ((uint32_t)(1UL << 26)) /**< Pin 26 Define */
  100. #define PIN_27 ((uint32_t)(1UL << 27)) /**< Pin 27 Define */
  101. #define PIN_28 ((uint32_t)(1UL << 28)) /**< Pin 28 Define */
  102. #define PIN_29 ((uint32_t)(1UL << 29)) /**< Pin 29 Define */
  103. #define PIN_30 ((uint32_t)(1UL << 30)) /**< Pin 30 Define */
  104. #define PIN_31 ((uint32_t)(1UL << 31)) /**< Pin 31 Define */
  105. /**@} end of gpio_pin group */
  106. /**@} end of gpio_port_pin group */
  107. /**
  108. * Enumeration type for the GPIO Function Type
  109. */
  110. typedef enum {
  111. GPIO_FUNC_IN, /**< GPIO Input */
  112. GPIO_FUNC_OUT, /**< GPIO Output */
  113. GPIO_FUNC_ALT1, /**< Alternate Function Selection */
  114. GPIO_FUNC_ALT2, /**< Alternate Function Selection */
  115. GPIO_FUNC_ALT3, /**< Alternate Function Selection */
  116. GPIO_FUNC_ALT4, /**< Alternate Function Selection */
  117. } gpio_func_t;
  118. /**
  119. * Enumeration type for the type of GPIO pad on a given pin.
  120. */
  121. typedef enum {
  122. GPIO_PAD_NONE, /**< No pull-up or pull-down */
  123. GPIO_PAD_PULL_UP, /**< Set pad to weak pull-up */
  124. GPIO_PAD_PULL_DOWN, /**< Set pad to weak pull-down */
  125. } gpio_pad_t;
  126. /**
  127. * Structure type for configuring a GPIO port.
  128. */
  129. typedef struct {
  130. uint32_t port; /**< Index of GPIO port */
  131. uint32_t mask; /**< Pin mask (multiple pins may be set) */
  132. gpio_func_t func; /**< Function type */
  133. gpio_pad_t pad; /**< Pad type */
  134. } gpio_cfg_t;
  135. /**
  136. * Enumeration type for the interrupt modes.
  137. */
  138. typedef enum {
  139. GPIO_INT_LEVEL = 0, /**< Interrupt is level sensitive */
  140. GPIO_INT_EDGE = 1 /**< Interrupt is edge sensitive */
  141. } gpio_int_mode_t;
  142. /**
  143. * Enumeration type for the interrupt polarity.
  144. */
  145. typedef enum {
  146. GPIO_INT_FALLING = 0, /**< Interrupt triggers on falling edge */
  147. GPIO_INT_HIGH = GPIO_INT_FALLING, /**< Interrupt triggers when level is high */
  148. GPIO_INT_RISING, /**< Interrupt triggers on rising edge */
  149. GPIO_INT_LOW = GPIO_INT_RISING, /**< Interrupt triggers when level is low */
  150. GPIO_INT_BOTH /**< Interrupt triggers on either edge */
  151. } gpio_int_pol_t;
  152. /* **** Function Prototypes **** */
  153. /**
  154. * @brief Initialize GPIO.
  155. * @return #E_NO_ERROR if everything is successful.
  156. */
  157. int GPIO_Init(void);
  158. /**
  159. * @brief Configure GPIO pin(s).
  160. * @param cfg Pointer to configuration structure describing the pin.
  161. * @return #E_NO_ERROR if everything is successful.
  162. */
  163. int GPIO_Config(const gpio_cfg_t *cfg);
  164. /**
  165. * @brief Gets the pin(s) input state.
  166. * @param cfg Pointer to configuration structure describing the pin.
  167. * @return The requested pin state.
  168. */
  169. uint32_t GPIO_InGet(const gpio_cfg_t *cfg);
  170. /**
  171. * @brief Sets the pin(s) to a high level output.
  172. * @param cfg Pointer to configuration structure describing the pin.
  173. *
  174. */
  175. void GPIO_OutSet(const gpio_cfg_t *cfg);
  176. /**
  177. * @brief Clears the pin(s) to a low level output.
  178. * @param cfg Pointer to configuration structure describing the pin.
  179. *
  180. */
  181. void GPIO_OutClr(const gpio_cfg_t *cfg);
  182. /**
  183. * @brief Gets the pin(s) output state.
  184. * @param cfg Pointer to configuration structure describing the pin.
  185. *
  186. * @return The state of the requested pin.
  187. *
  188. */
  189. uint32_t GPIO_OutGet(const gpio_cfg_t *cfg);
  190. /**
  191. * @brief Write the pin(s) to a desired output level.
  192. * @param cfg Pointer to configuration structure describing the pin.
  193. * @param val Desired output level of the pin(s). This will be masked
  194. * with the configuration mask.
  195. */
  196. void GPIO_OutPut(const gpio_cfg_t *cfg, uint32_t val);
  197. /**
  198. * @brief Toggles the the pin(s) output level.
  199. * @param cfg Pointer to configuration structure describing the pin.
  200. *
  201. */
  202. void GPIO_OutToggle(const gpio_cfg_t *cfg);
  203. /**
  204. * @brief Configure GPIO interrupt(s)
  205. * @param cfg Pointer to configuration structure describing the pin.
  206. * @param mode Requested interrupt mode.
  207. * @param pol Requested interrupt polarity.
  208. * @return #E_NO_ERROR if everything is successful.
  209. */
  210. int GPIO_IntConfig(const gpio_cfg_t *cfg, gpio_int_mode_t mode, gpio_int_pol_t pol);
  211. /**
  212. * @brief Enables the specified GPIO interrupt
  213. * @param cfg Pointer to configuration structure describing the pin.
  214. *
  215. */
  216. void GPIO_IntEnable(const gpio_cfg_t *cfg);
  217. /**
  218. * @brief Disables the specified GPIO interrupt.
  219. * @param cfg Pointer to configuration structure describing the pin.
  220. */
  221. void GPIO_IntDisable(const gpio_cfg_t *cfg);
  222. /**
  223. * @brief Gets the interrupt(s) status on a GPIO pin.
  224. * @param cfg Pointer to configuration structure describing the pin
  225. * for which the status is being requested.
  226. * @return The requested interrupt status.
  227. */
  228. uint32_t GPIO_IntStatus(const gpio_cfg_t *cfg);
  229. /**
  230. * @brief Clears the interrupt(s) status on a GPIO pin.
  231. * @param cfg Pointer to configuration structure describing the pin
  232. * to clear the interrupt state of.
  233. */
  234. void GPIO_IntClr(const gpio_cfg_t *cfg);
  235. /**
  236. * @brief Type alias for a GPIO callback function with prototype:
  237. * @code
  238. void callback_fn(void *cbdata);
  239. * @endcode
  240. * @param cbdata A void pointer to the data type as registered when
  241. * GPIO_RegisterCallback() was called.
  242. */
  243. typedef void (*gpio_callback_fn)(void *cbdata);
  244. /**
  245. * @brief Registers a callback for the interrupt on a given port and pin.
  246. * @param cfg Pointer to configuration structure describing the pin
  247. * @param callback A pointer to a function of type \c #gpio_callback_fn.
  248. * @param cbdata The parameter to be passed to the callback function, #gpio_callback_fn, when an interrupt occurs.
  249. *
  250. */
  251. void GPIO_RegisterCallback(const gpio_cfg_t *cfg, gpio_callback_fn callback, void *cbdata);
  252. /**
  253. * @brief GPIO IRQ Handler. @note If a callback is registered for a given
  254. * interrupt, the callback function will be called.
  255. *
  256. * @param port number of the port that generated the interrupt service routine.
  257. *
  258. */
  259. void GPIO_Handler(unsigned int port);
  260. /**@} end of group gpio */
  261. #ifdef __cplusplus
  262. }
  263. #endif
  264. #endif /* _GPIO_H_ */