gpio.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*****************************************************************************
  2. *
  3. * \file
  4. *
  5. * \brief GPIO software driver interface for AVR UC3.
  6. *
  7. * Copyright (c) 2010-2018 Microchip Technology Inc. and its subsidiaries.
  8. *
  9. * \asf_license_start
  10. *
  11. * \page License
  12. *
  13. * Subject to your compliance with these terms, you may use Microchip
  14. * software and any derivatives exclusively with Microchip products.
  15. * It is your responsibility to comply with third party license terms applicable
  16. * to your use of third party software (including open source software) that
  17. * may accompany Microchip software.
  18. *
  19. * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
  20. * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
  21. * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
  22. * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
  23. * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
  24. * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
  25. * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
  26. * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
  27. * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
  28. * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
  29. * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
  30. *
  31. * \asf_license_stop
  32. *
  33. *****************************************************************************/
  34. /*
  35. * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
  36. */
  37. #ifndef _GPIO_H_
  38. #define _GPIO_H_
  39. /**
  40. * \defgroup group_avr32_drivers_gpio GPIO - General-Purpose Input/Output
  41. *
  42. * GPIO gives access to the MCU pins.
  43. *
  44. * @{
  45. */
  46. #include <avr32/io.h>
  47. #include "compiler.h"
  48. /** \name Return Values of the GPIO API
  49. * @{ */
  50. #define GPIO_SUCCESS 0 /**< Function successfully completed. */
  51. #define GPIO_INVALID_ARGUMENT 1 /**< Input parameters are out of range. */
  52. /** @} */
  53. /** \name Interrupt Trigger Modes
  54. * @{ */
  55. #define GPIO_PIN_CHANGE 0 /**< Interrupt triggered upon pin change. */
  56. #define GPIO_RISING_EDGE 1 /**< Interrupt triggered upon rising edge. */
  57. #define GPIO_FALLING_EDGE 2 /**< Interrupt triggered upon falling edge. */
  58. /** @} */
  59. /** \name Common defines for GPIO_FLAGS parameter
  60. * @{ */
  61. #define GPIO_DIR_INPUT (0 << 0) /**< Pin is Input */
  62. #define GPIO_DIR_OUTPUT (1 << 0) /**< Pin is Output */
  63. #define GPIO_INIT_LOW (0 << 1) /**< Initial Output State is Low */
  64. #define GPIO_INIT_HIGH (1 << 1) /**< Initial Output State is High */
  65. #define GPIO_PULL_UP (1 << 2) /**< Pull-Up (when input) */
  66. #define GPIO_PULL_DOWN (2 << 2) /**< Pull-Down (when input) */
  67. #define GPIO_BUSKEEPER (3 << 2) /**< Bus Keeper */
  68. #define GPIO_DRIVE_MIN (0 << 4) /**< Drive Min Configuration */
  69. #define GPIO_DRIVE_LOW (1 << 4) /**< Drive Low Configuration */
  70. #define GPIO_DRIVE_HIGH (2 << 4) /**< Drive High Configuration */
  71. #define GPIO_DRIVE_MAX (3 << 4) /**< Drive Max Configuration */
  72. #define GPIO_OPEN_DRAIN (1 << 6) /**< Open-Drain (when output) */
  73. #define GPIO_INTERRUPT (1 << 7) /**< Enable Pin/Group Interrupt */
  74. #define GPIO_BOTHEDGES (3 << 7) /**< Sense Both Edges */
  75. #define GPIO_RISING (5 << 7) /**< Sense Rising Edge */
  76. #define GPIO_FALLING (7 << 7) /**< Sense Falling Edge */
  77. /** @} */
  78. /** A type definition of pins and modules connectivity. */
  79. typedef struct {
  80. uint32_t pin; /**< Module pin. */
  81. uint32_t function; /**< Module function. */
  82. } gpio_map_t[];
  83. /** \name Peripheral Bus Interface
  84. *
  85. * Low-speed interface with a non-deterministic number of clock cycles per
  86. * access.
  87. *
  88. * This interface operates with lower clock frequencies (fPB <= fCPU), and its
  89. * timing is not deterministic since it needs to access a shared bus which may
  90. * be heavily loaded.
  91. *
  92. * \note This interface is immediately available without initialization.
  93. *
  94. * @{
  95. */
  96. uint32_t gpio_enable_module(const gpio_map_t gpiomap, uint32_t size);
  97. uint32_t gpio_enable_module_pin(uint32_t pin, uint32_t function);
  98. void gpio_enable_gpio(const gpio_map_t gpiomap, uint32_t size);
  99. void gpio_enable_gpio_pin(uint32_t pin);
  100. void gpio_enable_pin_pull_up(uint32_t pin);
  101. void gpio_disable_pin_pull_up(uint32_t pin);
  102. #if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || \
  103. defined(AVR32_GPIO_212_H_INCLUDED)
  104. void gpio_enable_pin_pull_down(uint32_t pin);
  105. void gpio_disable_pin_pull_down(uint32_t pin);
  106. void gpio_enable_pin_buskeeper(uint32_t pin);
  107. void gpio_disable_pin_buskeeper(uint32_t pin);
  108. #endif
  109. void gpio_configure_pin(uint32_t pin, uint32_t flags);
  110. void gpio_configure_group(uint32_t port, uint32_t mask, uint32_t flags);
  111. bool gpio_get_pin_value(uint32_t pin);
  112. /**
  113. * \brief Check if the pin is in low logical level.
  114. *
  115. * \param pin The pin number.
  116. * \return bool \c true if the pin is in low logical level
  117. * \c false if the pin is not in low logical level
  118. */
  119. __always_inline static bool gpio_pin_is_low(uint32_t pin)
  120. {
  121. return (gpio_get_pin_value(pin) == 0);
  122. }
  123. /**
  124. * \brief Check if the pin is in high logical level.
  125. *
  126. * \param pin The pin number.
  127. * \return bool \c true if the pin is in high logical level
  128. * \c false if the pin is not in high logical level
  129. */
  130. __always_inline static bool gpio_pin_is_high(uint32_t pin)
  131. {
  132. return (gpio_get_pin_value(pin) != 0);
  133. }
  134. bool gpio_get_gpio_pin_output_value(uint32_t pin);
  135. bool gpio_get_gpio_open_drain_pin_output_value(uint32_t pin);
  136. void gpio_set_gpio_pin(uint32_t pin);
  137. void gpio_set_pin_high(uint32_t pin);
  138. void gpio_set_group_high(uint32_t port, uint32_t mask);
  139. void gpio_clr_gpio_pin(uint32_t pin);
  140. void gpio_set_pin_low(uint32_t pin);
  141. void gpio_set_group_low(uint32_t port, uint32_t mask);
  142. void gpio_tgl_gpio_pin(uint32_t pin);
  143. void gpio_toggle_pin(uint32_t pin);
  144. void gpio_toggle_group(uint32_t port, uint32_t mask);
  145. void gpio_set_gpio_open_drain_pin(uint32_t pin);
  146. void gpio_clr_gpio_open_drain_pin(uint32_t pin);
  147. void gpio_tgl_gpio_open_drain_pin(uint32_t pin);
  148. void gpio_enable_pin_glitch_filter(uint32_t pin);
  149. void gpio_disable_pin_glitch_filter(uint32_t pin);
  150. uint32_t gpio_enable_pin_interrupt(uint32_t pin, uint32_t mode);
  151. void gpio_disable_pin_interrupt(uint32_t pin);
  152. bool gpio_get_pin_interrupt_flag(uint32_t pin);
  153. void gpio_clear_pin_interrupt_flag(uint32_t pin);
  154. /** @} */
  155. #if (defined AVR32_GPIO_LOCAL_ADDRESS) || defined(__DOXYGEN__)
  156. /** \name Local Bus Interface
  157. *
  158. * High-speed interface with only one clock cycle per access.
  159. *
  160. * This interface operates with high clock frequency (fCPU), and its timing is
  161. * deterministic since it does not need to access a shared bus which may be
  162. * heavily loaded.
  163. *
  164. * \warning To use this interface, the clock frequency of the peripheral bus on
  165. * which the GPIO peripheral is connected must be set to the CPU clock
  166. * frequency (fPB = fCPU).
  167. *
  168. * \note This interface has to be initialized in order to be available.
  169. *
  170. * @{
  171. */
  172. /** \brief Enables the local bus interface for GPIO.
  173. *
  174. * \note This function must have been called at least once before using other
  175. * functions in this interface.
  176. */
  177. __always_inline static void gpio_local_init(void)
  178. {
  179. Set_system_register(AVR32_CPUCR,
  180. Get_system_register(AVR32_CPUCR) | AVR32_CPUCR_LOCEN_MASK);
  181. }
  182. /** \brief Enables the output driver of a pin.
  183. *
  184. * \param pin The pin number.
  185. *
  186. * \note \ref gpio_local_init must have been called beforehand.
  187. *
  188. * \note This function does not enable the GPIO mode of the pin.
  189. * \ref gpio_enable_gpio_pin can be called for this purpose.
  190. */
  191. __always_inline static void gpio_local_enable_pin_output_driver(uint32_t pin)
  192. {
  193. AVR32_GPIO_LOCAL.port[pin >> 5].oders = 1 << (pin & 0x1F);
  194. }
  195. /** \brief Disables the output driver of a pin.
  196. *
  197. * \param pin The pin number.
  198. *
  199. * \note \ref gpio_local_init must have been called beforehand.
  200. */
  201. __always_inline static void gpio_local_disable_pin_output_driver(uint32_t pin)
  202. {
  203. AVR32_GPIO_LOCAL.port[pin >> 5].oderc = 1 << (pin & 0x1F);
  204. }
  205. /** \brief Returns the value of a pin.
  206. *
  207. * \param pin The pin number.
  208. *
  209. * \return The pin value.
  210. *
  211. * \note \ref gpio_local_init must have been called beforehand.
  212. */
  213. __always_inline static bool gpio_local_get_pin_value(uint32_t pin)
  214. {
  215. return (AVR32_GPIO_LOCAL.port[pin >> 5].pvr >> (pin & 0x1F)) & 1;
  216. }
  217. /** \brief Drives a GPIO pin to 1.
  218. *
  219. * \param pin The pin number.
  220. *
  221. * \note \ref gpio_local_init must have been called beforehand.
  222. *
  223. * \note This function does not enable the GPIO mode of the pin nor its output
  224. * driver. \ref gpio_enable_gpio_pin and
  225. * \ref gpio_local_enable_pin_output_driver can be called for this
  226. * purpose.
  227. */
  228. __always_inline static void gpio_local_set_gpio_pin(uint32_t pin)
  229. {
  230. AVR32_GPIO_LOCAL.port[pin >> 5].ovrs = 1 << (pin & 0x1F);
  231. }
  232. /** \brief Drives a GPIO pin to 0.
  233. *
  234. * \param pin The pin number.
  235. *
  236. * \note \ref gpio_local_init must have been called beforehand.
  237. *
  238. * \note This function does not enable the GPIO mode of the pin nor its output
  239. * driver. \ref gpio_enable_gpio_pin and
  240. * \ref gpio_local_enable_pin_output_driver can be called for this
  241. * purpose.
  242. */
  243. __always_inline static void gpio_local_clr_gpio_pin(uint32_t pin)
  244. {
  245. AVR32_GPIO_LOCAL.port[pin >> 5].ovrc = 1 << (pin & 0x1F);
  246. }
  247. /** \brief Toggles a GPIO pin.
  248. *
  249. * \param pin The pin number.
  250. *
  251. * \note \ref gpio_local_init must have been called beforehand.
  252. *
  253. * \note This function does not enable the GPIO mode of the pin nor its output
  254. * driver. \ref gpio_enable_gpio_pin and
  255. * \ref gpio_local_enable_pin_output_driver can be called for this
  256. * purpose.
  257. */
  258. __always_inline static void gpio_local_tgl_gpio_pin(uint32_t pin)
  259. {
  260. AVR32_GPIO_LOCAL.port[pin >> 5].ovrt = 1 << (pin & 0x1F);
  261. }
  262. /** \brief Initializes the configuration of a GPIO pin so that it can be used
  263. * with GPIO open-drain functions.
  264. *
  265. * \note This function must have been called at least once before using
  266. * \ref gpio_local_set_gpio_open_drain_pin,
  267. * \ref gpio_local_clr_gpio_open_drain_pin or
  268. * \ref gpio_local_tgl_gpio_open_drain_pin.
  269. */
  270. __always_inline static void gpio_local_init_gpio_open_drain_pin(uint32_t pin)
  271. {
  272. AVR32_GPIO_LOCAL.port[pin >> 5].ovrc = 1 << (pin & 0x1F);
  273. }
  274. /** \brief Drives a GPIO pin to 1 using open drain.
  275. *
  276. * \param pin The pin number.
  277. *
  278. * \note \ref gpio_local_init and \ref gpio_local_init_gpio_open_drain_pin must
  279. * have been called beforehand.
  280. *
  281. * \note This function does not enable the GPIO mode of the pin.
  282. * \ref gpio_enable_gpio_pin can be called for this purpose.
  283. */
  284. __always_inline static void gpio_local_set_gpio_open_drain_pin(uint32_t pin)
  285. {
  286. AVR32_GPIO_LOCAL.port[pin >> 5].oderc = 1 << (pin & 0x1F);
  287. }
  288. /** \brief Drives a GPIO pin to 0 using open drain.
  289. *
  290. * \param pin The pin number.
  291. *
  292. * \note \ref gpio_local_init and \ref gpio_local_init_gpio_open_drain_pin must
  293. * have been called beforehand.
  294. *
  295. * \note This function does not enable the GPIO mode of the pin.
  296. * \ref gpio_enable_gpio_pin can be called for this purpose.
  297. */
  298. __always_inline static void gpio_local_clr_gpio_open_drain_pin(uint32_t pin)
  299. {
  300. AVR32_GPIO_LOCAL.port[pin >> 5].oders = 1 << (pin & 0x1F);
  301. }
  302. /** \brief Toggles a GPIO pin using open drain.
  303. *
  304. * \param pin The pin number.
  305. *
  306. * \note \ref gpio_local_init and \ref gpio_local_init_gpio_open_drain_pin must
  307. * have been called beforehand.
  308. *
  309. * \note This function does not enable the GPIO mode of the pin.
  310. * \ref gpio_enable_gpio_pin can be called for this purpose.
  311. */
  312. __always_inline static void gpio_local_tgl_gpio_open_drain_pin(uint32_t pin)
  313. {
  314. AVR32_GPIO_LOCAL.port[pin >> 5].odert = 1 << (pin & 0x1F);
  315. }
  316. /** @} */
  317. #endif /* AVR32_GPIO_LOCAL_ADDRESS */
  318. #if UC3L
  319. /** \name Peripheral Event System support
  320. *
  321. * The GPIO can be programmed to output peripheral events whenever an interrupt
  322. * condition is detected, such as pin value change, or only when a rising or
  323. * falling edge is detected.
  324. *
  325. * @{
  326. */
  327. /** \brief Enables the peripheral event generation of a pin.
  328. *
  329. * \param pin The pin number.
  330. *
  331. */
  332. __always_inline static void gpio_enable_pin_periph_event(uint32_t pin)
  333. {
  334. AVR32_GPIO.port[pin >> 5].oderc = 1 << (pin & 0x1F); /* The GPIO output
  335. * driver is
  336. * disabled for
  337. * that pin. */
  338. AVR32_GPIO.port[pin >> 5].evers = 1 << (pin & 0x1F);
  339. }
  340. /** \brief Disables the peripheral event generation of a pin.
  341. *
  342. * \param pin The pin number.
  343. *
  344. */
  345. __always_inline static void gpio_disable_pin_periph_event(uint32_t pin)
  346. {
  347. AVR32_GPIO.port[pin >> 5].everc = 1 << (pin & 0x1F);
  348. }
  349. uint32_t gpio_configure_pin_periph_event_mode(uint32_t pin, uint32_t mode,
  350. uint32_t use_igf);
  351. /** @} */
  352. #endif
  353. /** @} */
  354. #endif /* _GPIO_H_ */