gpio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* This source 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 driver for AVR32 UC3.
  6. *
  7. * This file defines a useful set of functions for the GPIO.
  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. #include "gpio.h"
  48. //! GPIO module instance.
  49. #define GPIO AVR32_GPIO
  50. /*! \name Peripheral Bus Interface
  51. */
  52. //! @{
  53. int gpio_enable_module(const gpio_map_t gpiomap, unsigned int size)
  54. {
  55. int status = GPIO_SUCCESS;
  56. unsigned int i;
  57. for (i = 0; i < size; i++)
  58. {
  59. status |= gpio_enable_module_pin(gpiomap->pin, gpiomap->function);
  60. gpiomap++;
  61. }
  62. return status;
  63. }
  64. int gpio_enable_module_pin(unsigned int pin, unsigned int function)
  65. {
  66. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  67. // Enable the correct function.
  68. switch (function)
  69. {
  70. case 0: // A function.
  71. gpio_port->pmr0c = 1 << (pin & 0x1F);
  72. gpio_port->pmr1c = 1 << (pin & 0x1F);
  73. #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
  74. gpio_port->pmr2c = 1 << (pin & 0x1F);
  75. #endif
  76. break;
  77. case 1: // B function.
  78. gpio_port->pmr0s = 1 << (pin & 0x1F);
  79. gpio_port->pmr1c = 1 << (pin & 0x1F);
  80. #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
  81. gpio_port->pmr2c = 1 << (pin & 0x1F);
  82. #endif
  83. break;
  84. case 2: // C function.
  85. gpio_port->pmr0c = 1 << (pin & 0x1F);
  86. gpio_port->pmr1s = 1 << (pin & 0x1F);
  87. #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
  88. gpio_port->pmr2c = 1 << (pin & 0x1F);
  89. #endif
  90. break;
  91. case 3: // D function.
  92. gpio_port->pmr0s = 1 << (pin & 0x1F);
  93. gpio_port->pmr1s = 1 << (pin & 0x1F);
  94. #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
  95. gpio_port->pmr2c = 1 << (pin & 0x1F);
  96. #endif
  97. break;
  98. #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
  99. case 4: // E function.
  100. gpio_port->pmr0c = 1 << (pin & 0x1F);
  101. gpio_port->pmr1c = 1 << (pin & 0x1F);
  102. gpio_port->pmr2s = 1 << (pin & 0x1F);
  103. break;
  104. case 5: // F function.
  105. gpio_port->pmr0s = 1 << (pin & 0x1F);
  106. gpio_port->pmr1c = 1 << (pin & 0x1F);
  107. gpio_port->pmr2s = 1 << (pin & 0x1F);
  108. break;
  109. case 6: // G function.
  110. gpio_port->pmr0c = 1 << (pin & 0x1F);
  111. gpio_port->pmr1s = 1 << (pin & 0x1F);
  112. gpio_port->pmr2s = 1 << (pin & 0x1F);
  113. break;
  114. case 7: // H function.
  115. gpio_port->pmr0s = 1 << (pin & 0x1F);
  116. gpio_port->pmr1s = 1 << (pin & 0x1F);
  117. gpio_port->pmr2s = 1 << (pin & 0x1F);
  118. break;
  119. #endif
  120. default:
  121. return GPIO_INVALID_ARGUMENT;
  122. }
  123. // Disable GPIO control.
  124. gpio_port->gperc = 1 << (pin & 0x1F);
  125. return GPIO_SUCCESS;
  126. }
  127. void gpio_enable_gpio(const gpio_map_t gpiomap, unsigned int size)
  128. {
  129. unsigned int i;
  130. for (i = 0; i < size; i++)
  131. {
  132. gpio_enable_gpio_pin(gpiomap->pin);
  133. gpiomap++;
  134. }
  135. }
  136. void gpio_enable_gpio_pin(unsigned int pin)
  137. {
  138. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  139. gpio_port->oderc = 1 << (pin & 0x1F);
  140. gpio_port->gpers = 1 << (pin & 0x1F);
  141. }
  142. // The open-drain mode is not synthesized on the current AVR32 products.
  143. // If one day some AVR32 products have this feature, the corresponding part
  144. // numbers should be listed in the #if below.
  145. // Note that other functions are available in this driver to use pins with open
  146. // drain in GPIO mode. The advantage of the open-drain mode functions over these
  147. // other functions is that they can be used not only in GPIO mode but also in
  148. // module mode.
  149. #if 0
  150. void gpio_enable_pin_open_drain(unsigned int pin)
  151. {
  152. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  153. gpio_port->odmers = 1 << (pin & 0x1F);
  154. }
  155. void gpio_disable_pin_open_drain(unsigned int pin)
  156. {
  157. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  158. gpio_port->odmerc = 1 << (pin & 0x1F);
  159. }
  160. #endif
  161. void gpio_enable_pin_pull_up(unsigned int pin)
  162. {
  163. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  164. gpio_port->puers = 1 << (pin & 0x1F);
  165. #if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
  166. gpio_port->pderc = 1 << (pin & 0x1F);
  167. #endif
  168. }
  169. void gpio_disable_pin_pull_up(unsigned int pin)
  170. {
  171. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  172. gpio_port->puerc = 1 << (pin & 0x1F);
  173. }
  174. #if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
  175. // Added support of Pull-up Resistor, Pull-down Resistor and Buskeeper Control.
  176. /*! \brief Enables the pull-down resistor of a pin.
  177. *
  178. * \param pin The pin number.
  179. */
  180. void gpio_enable_pin_pull_down(unsigned int pin)
  181. {
  182. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  183. gpio_port->puerc = 1 << (pin & 0x1F);
  184. gpio_port->pders = 1 << (pin & 0x1F);
  185. }
  186. /*! \brief Disables the pull-down resistor of a pin.
  187. *
  188. * \param pin The pin number.
  189. */
  190. void gpio_disable_pin_pull_down(unsigned int pin)
  191. {
  192. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  193. gpio_port->pderc = 1 << (pin & 0x1F);
  194. }
  195. /*! \brief Enables the buskeeper functionality on a pin.
  196. *
  197. * \param pin The pin number.
  198. */
  199. void gpio_enable_pin_buskeeper(unsigned int pin)
  200. {
  201. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  202. gpio_port->puers = 1 << (pin & 0x1F);
  203. gpio_port->pders = 1 << (pin & 0x1F);
  204. }
  205. /*! \brief Disables the buskeeper functionality on a pin.
  206. *
  207. * \param pin The pin number.
  208. */
  209. void gpio_disable_pin_buskeeper(unsigned int pin)
  210. {
  211. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  212. gpio_port->puerc = 1 << (pin & 0x1F);
  213. gpio_port->pderc = 1 << (pin & 0x1F);
  214. }
  215. #endif
  216. int gpio_get_pin_value(unsigned int pin)
  217. {
  218. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  219. return (gpio_port->pvr >> (pin & 0x1F)) & 1;
  220. }
  221. int gpio_get_gpio_pin_output_value(unsigned int pin)
  222. {
  223. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  224. return (gpio_port->ovr >> (pin & 0x1F)) & 1;
  225. }
  226. int gpio_get_gpio_open_drain_pin_output_value(unsigned int pin)
  227. {
  228. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  229. return ((gpio_port->oder >> (pin & 0x1F)) & 1) ^ 1;
  230. }
  231. void gpio_set_gpio_pin(unsigned int pin)
  232. {
  233. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  234. gpio_port->ovrs = 1 << (pin & 0x1F); // Value to be driven on the I/O line: 1.
  235. gpio_port->oders = 1 << (pin & 0x1F); // The GPIO output driver is enabled for that pin.
  236. gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin.
  237. }
  238. void gpio_clr_gpio_pin(unsigned int pin)
  239. {
  240. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  241. gpio_port->ovrc = 1 << (pin & 0x1F); // Value to be driven on the I/O line: 0.
  242. gpio_port->oders = 1 << (pin & 0x1F); // The GPIO output driver is enabled for that pin.
  243. gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin.
  244. }
  245. void gpio_tgl_gpio_pin(unsigned int pin)
  246. {
  247. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  248. gpio_port->ovrt = 1 << (pin & 0x1F); // Toggle the I/O line.
  249. gpio_port->oders = 1 << (pin & 0x1F); // The GPIO output driver is enabled for that pin.
  250. gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin.
  251. }
  252. void gpio_set_gpio_open_drain_pin(unsigned int pin)
  253. {
  254. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  255. gpio_port->oderc = 1 << (pin & 0x1F); // The GPIO output driver is disabled for that pin.
  256. gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin.
  257. }
  258. void gpio_clr_gpio_open_drain_pin(unsigned int pin)
  259. {
  260. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  261. gpio_port->ovrc = 1 << (pin & 0x1F); // Value to be driven on the I/O line: 0.
  262. gpio_port->oders = 1 << (pin & 0x1F); // The GPIO output driver is enabled for that pin.
  263. gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin.
  264. }
  265. void gpio_tgl_gpio_open_drain_pin(unsigned int pin)
  266. {
  267. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  268. gpio_port->ovrc = 1 << (pin & 0x1F); // Value to be driven on the I/O line if the GPIO output driver is enabled: 0.
  269. gpio_port->odert = 1 << (pin & 0x1F); // The GPIO output driver is toggled for that pin.
  270. gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin.
  271. }
  272. void gpio_enable_pin_glitch_filter(unsigned int pin)
  273. {
  274. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  275. gpio_port->gfers = 1 << (pin & 0x1F);
  276. }
  277. void gpio_disable_pin_glitch_filter(unsigned int pin)
  278. {
  279. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  280. gpio_port->gferc = 1 << (pin & 0x1F);
  281. }
  282. /*! \brief Configure the edge detector of an input pin
  283. *
  284. * \param pin The pin number.
  285. * \param mode The edge detection mode (\ref GPIO_PIN_CHANGE, \ref GPIO_RISING_EDGE
  286. * or \ref GPIO_FALLING_EDGE).
  287. *
  288. * \return \ref GPIO_SUCCESS or \ref GPIO_INVALID_ARGUMENT.
  289. */
  290. static int gpio_configure_edge_detector(unsigned int pin, unsigned int mode)
  291. {
  292. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  293. // Configure the edge detector.
  294. switch (mode)
  295. {
  296. case GPIO_PIN_CHANGE:
  297. gpio_port->imr0c = 1 << (pin & 0x1F);
  298. gpio_port->imr1c = 1 << (pin & 0x1F);
  299. break;
  300. case GPIO_RISING_EDGE:
  301. gpio_port->imr0s = 1 << (pin & 0x1F);
  302. gpio_port->imr1c = 1 << (pin & 0x1F);
  303. break;
  304. case GPIO_FALLING_EDGE:
  305. gpio_port->imr0c = 1 << (pin & 0x1F);
  306. gpio_port->imr1s = 1 << (pin & 0x1F);
  307. break;
  308. default:
  309. return GPIO_INVALID_ARGUMENT;
  310. }
  311. return GPIO_SUCCESS;
  312. }
  313. int gpio_enable_pin_interrupt(unsigned int pin, unsigned int mode)
  314. {
  315. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  316. // Enable the glitch filter.
  317. gpio_port->gfers = 1 << (pin & 0x1F);
  318. // Configure the edge detector.
  319. if(GPIO_INVALID_ARGUMENT == gpio_configure_edge_detector(pin, mode))
  320. return(GPIO_INVALID_ARGUMENT);
  321. // Enable interrupt.
  322. gpio_port->iers = 1 << (pin & 0x1F);
  323. return GPIO_SUCCESS;
  324. }
  325. void gpio_disable_pin_interrupt(unsigned int pin)
  326. {
  327. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  328. gpio_port->ierc = 1 << (pin & 0x1F);
  329. }
  330. int gpio_get_pin_interrupt_flag(unsigned int pin)
  331. {
  332. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  333. return (gpio_port->ifr >> (pin & 0x1F)) & 1;
  334. }
  335. void gpio_clear_pin_interrupt_flag(unsigned int pin)
  336. {
  337. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  338. gpio_port->ifrc = 1 << (pin & 0x1F);
  339. }
  340. //#
  341. //# Peripheral Event System Support.
  342. //#
  343. #if UC3L
  344. int gpio_configure_pin_periph_event_mode(unsigned int pin, unsigned int mode, unsigned int use_igf)
  345. {
  346. volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
  347. if(TRUE == use_igf)
  348. {
  349. // Enable the glitch filter.
  350. gpio_port->gfers = 1 << (pin & 0x1F);
  351. }
  352. else
  353. {
  354. // Disable the glitch filter.
  355. gpio_port->gferc = 1 << (pin & 0x1F);
  356. }
  357. // Configure the edge detector.
  358. return(gpio_configure_edge_detector(pin, mode));
  359. }
  360. #endif
  361. //! @}