gpio.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /* This header file is part of the ATMEL AVR-UC3-SoftwareFramework-1.7.0 Release */
  2. /*This file has been prepared for Doxygen automatic documentation generation.*/
  3. /*! \file *********************************************************************
  4. *
  5. * \brief GPIO header for AVR32 UC3.
  6. *
  7. * This file contains basic GPIO driver functions.
  8. *
  9. * - Compiler: IAR EWAVR32 and GNU GCC for AVR32
  10. * - Supported devices: All AVR32 devices with a GPIO module can be used.
  11. * - AppNote:
  12. *
  13. * \author Atmel Corporation: http://www.atmel.com \n
  14. * Support and FAQ: http://support.atmel.no/
  15. *
  16. *****************************************************************************/
  17. /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions are met:
  21. *
  22. * 1. Redistributions of source code must retain the above copyright notice, this
  23. * list of conditions and the following disclaimer.
  24. *
  25. * 2. Redistributions in binary form must reproduce the above copyright notice,
  26. * this list of conditions and the following disclaimer in the documentation
  27. * and/or other materials provided with the distribution.
  28. *
  29. * 3. The name of Atmel may not be used to endorse or promote products derived
  30. * from this software without specific prior written permission.
  31. *
  32. * 4. This software may only be redistributed and used in connection with an Atmel
  33. * AVR product.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  37. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  38. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  39. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  40. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  41. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  42. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  44. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  45. *
  46. */
  47. #ifndef _GPIO_H_
  48. #define _GPIO_H_
  49. #include <avr32/io.h>
  50. #include "compiler.h"
  51. /*! \name Return Values of the GPIO API
  52. */
  53. //! @{
  54. #define GPIO_SUCCESS 0 //!< Function successfully completed.
  55. #define GPIO_INVALID_ARGUMENT 1 //!< Input parameters are out of range.
  56. //! @}
  57. /*! \name Interrupt Trigger Modes
  58. */
  59. //! @{
  60. #define GPIO_PIN_CHANGE 0 //!< Interrupt triggered upon pin change.
  61. #define GPIO_RISING_EDGE 1 //!< Interrupt triggered upon rising edge.
  62. #define GPIO_FALLING_EDGE 2 //!< Interrupt triggered upon falling edge.
  63. //! @}
  64. //! A type definition of pins and modules connectivity.
  65. typedef struct
  66. {
  67. unsigned char pin; //!< Module pin.
  68. unsigned char function; //!< Module function.
  69. } gpio_map_t[];
  70. /*! \name Peripheral Bus Interface
  71. *
  72. * Low-speed interface with a non-deterministic number of clock cycles per
  73. * access.
  74. *
  75. * This interface operates with lower clock frequencies (fPB <= fCPU), and its
  76. * timing is not deterministic since it needs to access a shared bus which may
  77. * be heavily loaded.
  78. *
  79. * \note This interface is immediately available without initialization.
  80. */
  81. //! @{
  82. /*! \brief Enables specific module modes for a set of pins.
  83. *
  84. * \param gpiomap The pin map.
  85. * \param size The number of pins in \a gpiomap.
  86. *
  87. * \return \ref GPIO_SUCCESS or \ref GPIO_INVALID_ARGUMENT.
  88. */
  89. extern int gpio_enable_module(const gpio_map_t gpiomap, unsigned int size);
  90. /*! \brief Enables a specific module mode for a pin.
  91. *
  92. * \param pin The pin number.\n
  93. * Refer to the product header file `uc3x.h' (where x is the part
  94. * number; e.g. x = a0512) for module pins. E.g., to enable a PWM
  95. * channel output, the pin number can be AVR32_PWM_3_PIN for PWM
  96. * channel 3.
  97. * \param function The pin function.\n
  98. * Refer to the product header file `uc3x.h' (where x is the
  99. * part number; e.g. x = a0512) for module pin functions. E.g.,
  100. * to enable a PWM channel output, the pin function can be
  101. * AVR32_PWM_3_FUNCTION for PWM channel 3.
  102. *
  103. * \return \ref GPIO_SUCCESS or \ref GPIO_INVALID_ARGUMENT.
  104. */
  105. extern int gpio_enable_module_pin(unsigned int pin, unsigned int function);
  106. /*! \brief Enables the GPIO mode of a set of pins.
  107. *
  108. * \param gpiomap The pin map.
  109. * \param size The number of pins in \a gpiomap.
  110. */
  111. extern void gpio_enable_gpio(const gpio_map_t gpiomap, unsigned int size);
  112. /*! \brief Enables the GPIO mode of a pin.
  113. *
  114. * \param pin The pin number.\n
  115. * Refer to the product header file `uc3x.h' (where x is the part
  116. * number; e.g. x = a0512) for pin definitions. E.g., to enable the
  117. * GPIO mode of PX21, AVR32_PIN_PX21 can be used. Module pins such as
  118. * AVR32_PWM_3_PIN for PWM channel 3 can also be used to release
  119. * module pins for GPIO.
  120. */
  121. extern void gpio_enable_gpio_pin(unsigned int pin);
  122. // The open-drain mode is not synthesized on the current AVR32 products.
  123. // If one day some AVR32 products have this feature, the corresponding part
  124. // numbers should be listed in the #if below.
  125. // Note that other functions are available in this driver to use pins with open
  126. // drain in GPIO mode. The advantage of the open-drain mode functions over these
  127. // other functions is that they can be used not only in GPIO mode but also in
  128. // module mode.
  129. #if 0
  130. /*! \brief Enables the open-drain mode of a pin.
  131. *
  132. * \param pin The pin number.
  133. */
  134. extern void gpio_enable_pin_open_drain(unsigned int pin);
  135. /*! \brief Disables the open-drain mode of a pin.
  136. *
  137. * \param pin The pin number.
  138. */
  139. extern void gpio_disable_pin_open_drain(unsigned int pin);
  140. #endif
  141. /*! \brief Enables the pull-up resistor of a pin.
  142. *
  143. * \param pin The pin number.
  144. */
  145. extern void gpio_enable_pin_pull_up(unsigned int pin);
  146. /*! \brief Disables the pull-up resistor of a pin.
  147. *
  148. * \param pin The pin number.
  149. */
  150. extern void gpio_disable_pin_pull_up(unsigned int pin);
  151. #if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
  152. // Added support of Pull-up Resistor, Pull-down Resistor and Buskeeper Control.
  153. /*! \brief Enables the pull-down resistor of a pin.
  154. *
  155. * \param pin The pin number.
  156. */
  157. extern void gpio_enable_pin_pull_down(unsigned int pin);
  158. /*! \brief Disables the pull-down resistor of a pin.
  159. *
  160. * \param pin The pin number.
  161. */
  162. extern void gpio_disable_pin_pull_down(unsigned int pin);
  163. /*! \brief Enables the buskeeper functionality on a pin.
  164. *
  165. * \param pin The pin number.
  166. */
  167. extern void gpio_enable_pin_buskeeper(unsigned int pin);
  168. /*! \brief Disables the buskeeper functionality on a pin.
  169. *
  170. * \param pin The pin number.
  171. */
  172. extern void gpio_disable_pin_buskeeper(unsigned int pin);
  173. #endif
  174. /*! \brief Returns the value of a pin.
  175. *
  176. * \param pin The pin number.
  177. *
  178. * \return The pin value.
  179. */
  180. extern int gpio_get_pin_value(unsigned int pin);
  181. /*! \brief Returns the output value set for a GPIO pin.
  182. *
  183. * \param pin The pin number.
  184. *
  185. * \return The pin output value.
  186. *
  187. * \note This function must be used in conjunction with \ref gpio_set_gpio_pin,
  188. * \ref gpio_clr_gpio_pin and \ref gpio_tgl_gpio_pin.
  189. */
  190. extern int gpio_get_gpio_pin_output_value(unsigned int pin);
  191. /*! \brief Returns the output value set for a GPIO pin using open drain.
  192. *
  193. * \param pin The pin number.
  194. *
  195. * \return The pin output value.
  196. *
  197. * \note This function must be used in conjunction with
  198. * \ref gpio_set_gpio_open_drain_pin, \ref gpio_clr_gpio_open_drain_pin
  199. * and \ref gpio_tgl_gpio_open_drain_pin.
  200. */
  201. extern int gpio_get_gpio_open_drain_pin_output_value(unsigned int pin);
  202. /*! \brief Drives a GPIO pin to 1.
  203. *
  204. * \param pin The pin number.
  205. */
  206. extern void gpio_set_gpio_pin(unsigned int pin);
  207. /*! \brief Drives a GPIO pin to 0.
  208. *
  209. * \param pin The pin number.
  210. */
  211. extern void gpio_clr_gpio_pin(unsigned int pin);
  212. /*! \brief Toggles a GPIO pin.
  213. *
  214. * \param pin The pin number.
  215. */
  216. extern void gpio_tgl_gpio_pin(unsigned int pin);
  217. /*! \brief Drives a GPIO pin to 1 using open drain.
  218. *
  219. * \param pin The pin number.
  220. */
  221. extern void gpio_set_gpio_open_drain_pin(unsigned int pin);
  222. /*! \brief Drives a GPIO pin to 0 using open drain.
  223. *
  224. * \param pin The pin number.
  225. */
  226. extern void gpio_clr_gpio_open_drain_pin(unsigned int pin);
  227. /*! \brief Toggles a GPIO pin using open drain.
  228. *
  229. * \param pin The pin number.
  230. */
  231. extern void gpio_tgl_gpio_open_drain_pin(unsigned int pin);
  232. /*! \brief Enables the glitch filter of a pin.
  233. *
  234. * When the glitch filter is enabled, a glitch with duration of less than 1
  235. * clock cycle is automatically rejected, while a pulse with duration of 2 clock
  236. * cycles or more is accepted. For pulse durations between 1 clock cycle and 2
  237. * clock cycles, the pulse may or may not be taken into account, depending on
  238. * the precise timing of its occurrence. Thus for a pulse to be guaranteed
  239. * visible it must exceed 2 clock cycles, whereas for a glitch to be reliably
  240. * filtered out, its duration must not exceed 1 clock cycle. The filter
  241. * introduces 2 clock cycles latency.
  242. *
  243. * \param pin The pin number.
  244. */
  245. extern void gpio_enable_pin_glitch_filter(unsigned int pin);
  246. /*! \brief Disables the glitch filter of a pin.
  247. *
  248. * \param pin The pin number.
  249. */
  250. extern void gpio_disable_pin_glitch_filter(unsigned int pin);
  251. /*! \brief Enables the interrupt of a pin with the specified settings.
  252. *
  253. * \param pin The pin number.
  254. * \param mode The trigger mode (\ref GPIO_PIN_CHANGE, \ref GPIO_RISING_EDGE or
  255. * \ref GPIO_FALLING_EDGE).
  256. *
  257. * \return \ref GPIO_SUCCESS or \ref GPIO_INVALID_ARGUMENT.
  258. */
  259. extern int gpio_enable_pin_interrupt(unsigned int pin, unsigned int mode);
  260. /*! \brief Disables the interrupt of a pin.
  261. *
  262. * \param pin The pin number.
  263. */
  264. extern void gpio_disable_pin_interrupt(unsigned int pin);
  265. /*! \brief Gets the interrupt flag of a pin.
  266. *
  267. * \param pin The pin number.
  268. *
  269. * \return The pin interrupt flag.
  270. */
  271. extern int gpio_get_pin_interrupt_flag(unsigned int pin);
  272. /*! \brief Clears the interrupt flag of a pin.
  273. *
  274. * \param pin The pin number.
  275. */
  276. extern void gpio_clear_pin_interrupt_flag(unsigned int pin);
  277. //! @}
  278. #if (defined AVR32_GPIO_LOCAL_ADDRESS)
  279. /*! \name Local Bus Interface
  280. *
  281. * High-speed interface with only one clock cycle per access.
  282. *
  283. * This interface operates with high clock frequency (fCPU), and its timing is
  284. * deterministic since it does not need to access a shared bus which may be
  285. * heavily loaded.
  286. *
  287. * \warning To use this interface, the clock frequency of the peripheral bus on
  288. * which the GPIO peripheral is connected must be set to the CPU clock
  289. * frequency (fPB = fCPU).
  290. *
  291. * \note This interface has to be initialized in order to be available.
  292. */
  293. //! @{
  294. /*! \brief Enables the local bus interface for GPIO.
  295. *
  296. * \note This function must have been called at least once before using other
  297. * functions in this interface.
  298. */
  299. #if (defined __GNUC__)
  300. __attribute__((__always_inline__))
  301. #endif
  302. extern __inline__ void gpio_local_init(void)
  303. {
  304. Set_system_register(AVR32_CPUCR,
  305. Get_system_register(AVR32_CPUCR) | AVR32_CPUCR_LOCEN_MASK);
  306. }
  307. /*! \brief Enables the output driver of a pin.
  308. *
  309. * \param pin The pin number.
  310. *
  311. * \note \ref gpio_local_init must have been called beforehand.
  312. *
  313. * \note This function does not enable the GPIO mode of the pin.
  314. * \ref gpio_enable_gpio_pin can be called for this purpose.
  315. */
  316. #if (defined __GNUC__)
  317. __attribute__((__always_inline__))
  318. #endif
  319. extern __inline__ void gpio_local_enable_pin_output_driver(unsigned int pin)
  320. {
  321. AVR32_GPIO_LOCAL.port[pin >> 5].oders = 1 << (pin & 0x1F);
  322. }
  323. /*! \brief Disables the output driver of a pin.
  324. *
  325. * \param pin The pin number.
  326. *
  327. * \note \ref gpio_local_init must have been called beforehand.
  328. */
  329. #if (defined __GNUC__)
  330. __attribute__((__always_inline__))
  331. #endif
  332. extern __inline__ void gpio_local_disable_pin_output_driver(unsigned int pin)
  333. {
  334. AVR32_GPIO_LOCAL.port[pin >> 5].oderc = 1 << (pin & 0x1F);
  335. }
  336. /*! \brief Returns the value of a pin.
  337. *
  338. * \param pin The pin number.
  339. *
  340. * \return The pin value.
  341. *
  342. * \note \ref gpio_local_init must have been called beforehand.
  343. */
  344. #if (defined __GNUC__)
  345. __attribute__((__always_inline__))
  346. #endif
  347. extern __inline__ int gpio_local_get_pin_value(unsigned int pin)
  348. {
  349. return (AVR32_GPIO_LOCAL.port[pin >> 5].pvr >> (pin & 0x1F)) & 1;
  350. }
  351. /*! \brief Drives a GPIO pin to 1.
  352. *
  353. * \param pin The pin number.
  354. *
  355. * \note \ref gpio_local_init must have been called beforehand.
  356. *
  357. * \note This function does not enable the GPIO mode of the pin nor its output
  358. * driver. \ref gpio_enable_gpio_pin and
  359. * \ref gpio_local_enable_pin_output_driver can be called for this
  360. * purpose.
  361. */
  362. #if (defined __GNUC__)
  363. __attribute__((__always_inline__))
  364. #endif
  365. extern __inline__ void gpio_local_set_gpio_pin(unsigned int pin)
  366. {
  367. AVR32_GPIO_LOCAL.port[pin >> 5].ovrs = 1 << (pin & 0x1F);
  368. }
  369. /*! \brief Drives a GPIO pin to 0.
  370. *
  371. * \param pin The pin number.
  372. *
  373. * \note \ref gpio_local_init must have been called beforehand.
  374. *
  375. * \note This function does not enable the GPIO mode of the pin nor its output
  376. * driver. \ref gpio_enable_gpio_pin and
  377. * \ref gpio_local_enable_pin_output_driver can be called for this
  378. * purpose.
  379. */
  380. #if (defined __GNUC__)
  381. __attribute__((__always_inline__))
  382. #endif
  383. extern __inline__ void gpio_local_clr_gpio_pin(unsigned int pin)
  384. {
  385. AVR32_GPIO_LOCAL.port[pin >> 5].ovrc = 1 << (pin & 0x1F);
  386. }
  387. /*! \brief Toggles a GPIO pin.
  388. *
  389. * \param pin The pin number.
  390. *
  391. * \note \ref gpio_local_init must have been called beforehand.
  392. *
  393. * \note This function does not enable the GPIO mode of the pin nor its output
  394. * driver. \ref gpio_enable_gpio_pin and
  395. * \ref gpio_local_enable_pin_output_driver can be called for this
  396. * purpose.
  397. */
  398. #if (defined __GNUC__)
  399. __attribute__((__always_inline__))
  400. #endif
  401. extern __inline__ void gpio_local_tgl_gpio_pin(unsigned int pin)
  402. {
  403. AVR32_GPIO_LOCAL.port[pin >> 5].ovrt = 1 << (pin & 0x1F);
  404. }
  405. /*! \brief Initializes the configuration of a GPIO pin so that it can be used
  406. * with GPIO open-drain functions.
  407. *
  408. * \note This function must have been called at least once before using
  409. * \ref gpio_local_set_gpio_open_drain_pin,
  410. * \ref gpio_local_clr_gpio_open_drain_pin or
  411. * \ref gpio_local_tgl_gpio_open_drain_pin.
  412. */
  413. #if (defined __GNUC__)
  414. __attribute__((__always_inline__))
  415. #endif
  416. extern __inline__ void gpio_local_init_gpio_open_drain_pin(unsigned int pin)
  417. {
  418. AVR32_GPIO_LOCAL.port[pin >> 5].ovrc = 1 << (pin & 0x1F);
  419. }
  420. /*! \brief Drives a GPIO pin to 1 using open drain.
  421. *
  422. * \param pin The pin number.
  423. *
  424. * \note \ref gpio_local_init and \ref gpio_local_init_gpio_open_drain_pin must
  425. * have been called beforehand.
  426. *
  427. * \note This function does not enable the GPIO mode of the pin.
  428. * \ref gpio_enable_gpio_pin can be called for this purpose.
  429. */
  430. #if (defined __GNUC__)
  431. __attribute__((__always_inline__))
  432. #endif
  433. extern __inline__ void gpio_local_set_gpio_open_drain_pin(unsigned int pin)
  434. {
  435. AVR32_GPIO_LOCAL.port[pin >> 5].oderc = 1 << (pin & 0x1F);
  436. }
  437. /*! \brief Drives a GPIO pin to 0 using open drain.
  438. *
  439. * \param pin The pin number.
  440. *
  441. * \note \ref gpio_local_init and \ref gpio_local_init_gpio_open_drain_pin must
  442. * have been called beforehand.
  443. *
  444. * \note This function does not enable the GPIO mode of the pin.
  445. * \ref gpio_enable_gpio_pin can be called for this purpose.
  446. */
  447. #if (defined __GNUC__)
  448. __attribute__((__always_inline__))
  449. #endif
  450. extern __inline__ void gpio_local_clr_gpio_open_drain_pin(unsigned int pin)
  451. {
  452. AVR32_GPIO_LOCAL.port[pin >> 5].oders = 1 << (pin & 0x1F);
  453. }
  454. /*! \brief Toggles a GPIO pin using open drain.
  455. *
  456. * \param pin The pin number.
  457. *
  458. * \note \ref gpio_local_init and \ref gpio_local_init_gpio_open_drain_pin must
  459. * have been called beforehand.
  460. *
  461. * \note This function does not enable the GPIO mode of the pin.
  462. * \ref gpio_enable_gpio_pin can be called for this purpose.
  463. */
  464. #if (defined __GNUC__)
  465. __attribute__((__always_inline__))
  466. #endif
  467. extern __inline__ void gpio_local_tgl_gpio_open_drain_pin(unsigned int pin)
  468. {
  469. AVR32_GPIO_LOCAL.port[pin >> 5].odert = 1 << (pin & 0x1F);
  470. }
  471. //! @}
  472. #endif // AVR32_GPIO_LOCAL_ADDRESS
  473. #if UC3L
  474. //! @{
  475. /*! \name Peripheral Event System support
  476. *
  477. * The GPIO can be programmed to output peripheral events whenever an interrupt
  478. * condition is detected, such as pin value change, or only when a rising or
  479. * falling edge is detected.
  480. *
  481. */
  482. /*! \brief Enables the peripheral event generation of a pin.
  483. *
  484. * \param pin The pin number.
  485. *
  486. */
  487. #if (defined __GNUC__)
  488. __attribute__((__always_inline__))
  489. #endif
  490. extern __inline__ void gpio_enable_pin_periph_event(unsigned int pin)
  491. {
  492. AVR32_GPIO.port[pin >> 5].oderc = 1 << (pin & 0x1F); // The GPIO output driver is disabled for that pin.
  493. AVR32_GPIO.port[pin >> 5].evers = 1 << (pin & 0x1F);
  494. }
  495. /*! \brief Disables the peripheral event generation of a pin.
  496. *
  497. * \param pin The pin number.
  498. *
  499. */
  500. #if (defined __GNUC__)
  501. __attribute__((__always_inline__))
  502. #endif
  503. extern __inline__ void gpio_disable_pin_periph_event(unsigned int pin)
  504. {
  505. AVR32_GPIO.port[pin >> 5].everc = 1 << (pin & 0x1F);
  506. }
  507. /*! \brief Configure the peripheral event trigger mode of a pin
  508. *
  509. * \param pin The pin number.
  510. * \param mode The trigger mode (\ref GPIO_PIN_CHANGE, \ref GPIO_RISING_EDGE or
  511. * \ref GPIO_FALLING_EDGE).
  512. * \param use_igf use the Input Glitch Filter (TRUE) or not (FALSE).
  513. *
  514. * \return \ref GPIO_SUCCESS or \ref GPIO_INVALID_ARGUMENT.
  515. */
  516. extern int gpio_configure_pin_periph_event_mode(unsigned int pin, unsigned int mode, unsigned int use_igf);
  517. //! @}
  518. #endif
  519. #endif // _GPIO_H_