gpio.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. #include "gpio.h"
  38. /** \name Peripheral Bus Interface
  39. *
  40. * @{
  41. */
  42. /** \brief Enables specific module modes for a set of pins.
  43. *
  44. * \param gpiomap The pin map.
  45. * \param size The number of pins in \a gpiomap.
  46. *
  47. * \return \ref GPIO_SUCCESS or \ref GPIO_INVALID_ARGUMENT.
  48. */
  49. uint32_t gpio_enable_module(const gpio_map_t gpiomap, uint32_t size)
  50. {
  51. uint32_t status = GPIO_SUCCESS;
  52. uint32_t i;
  53. for (i = 0; i < size; i++) {
  54. status |= gpio_enable_module_pin(gpiomap->pin, gpiomap->function);
  55. gpiomap++;
  56. }
  57. return status;
  58. }
  59. /** \brief Enables a specific module mode for a pin.
  60. *
  61. * \note A pin and pin function index can be found in the device part header
  62. * file. The \c AVR32_*_PIN constants map a GPIO number from the device
  63. * datasheet to the appropriate pin function, while the corresponding
  64. * \c AVR32_*_FUNCTION macro contains the appropriate function index.
  65. * \n\n
  66. * For example, the constants \c AVR32_PWM_3_PIN and
  67. * \c AVR32_PWM_3_FUNCTION contain the pin and function index of the PWM
  68. * module, channel 3, for the current device (if available).
  69. *
  70. * \param pin The pin number.
  71. * \param function The pin function.
  72. *
  73. * \return \ref GPIO_SUCCESS or \ref GPIO_INVALID_ARGUMENT.
  74. */
  75. uint32_t gpio_enable_module_pin(uint32_t pin, uint32_t function)
  76. {
  77. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  78. /* Enable the correct function. */
  79. switch (function) {
  80. case 0: /* A function. */
  81. gpio_port->pmr0c = 1 << (pin & 0x1F);
  82. gpio_port->pmr1c = 1 << (pin & 0x1F);
  83. #if (AVR32_GPIO_H_VERSION >= 210)
  84. gpio_port->pmr2c = 1 << (pin & 0x1F);
  85. #endif
  86. break;
  87. case 1: /* B function. */
  88. gpio_port->pmr0s = 1 << (pin & 0x1F);
  89. gpio_port->pmr1c = 1 << (pin & 0x1F);
  90. #if (AVR32_GPIO_H_VERSION >= 210)
  91. gpio_port->pmr2c = 1 << (pin & 0x1F);
  92. #endif
  93. break;
  94. case 2: /* C function. */
  95. gpio_port->pmr0c = 1 << (pin & 0x1F);
  96. gpio_port->pmr1s = 1 << (pin & 0x1F);
  97. #if (AVR32_GPIO_H_VERSION >= 210)
  98. gpio_port->pmr2c = 1 << (pin & 0x1F);
  99. #endif
  100. break;
  101. case 3: /* D function. */
  102. gpio_port->pmr0s = 1 << (pin & 0x1F);
  103. gpio_port->pmr1s = 1 << (pin & 0x1F);
  104. #if (AVR32_GPIO_H_VERSION >= 210)
  105. gpio_port->pmr2c = 1 << (pin & 0x1F);
  106. #endif
  107. break;
  108. #if (AVR32_GPIO_H_VERSION >= 210)
  109. case 4: /* E function. */
  110. gpio_port->pmr0c = 1 << (pin & 0x1F);
  111. gpio_port->pmr1c = 1 << (pin & 0x1F);
  112. gpio_port->pmr2s = 1 << (pin & 0x1F);
  113. break;
  114. case 5: /* F function. */
  115. gpio_port->pmr0s = 1 << (pin & 0x1F);
  116. gpio_port->pmr1c = 1 << (pin & 0x1F);
  117. gpio_port->pmr2s = 1 << (pin & 0x1F);
  118. break;
  119. case 6: /* G function. */
  120. gpio_port->pmr0c = 1 << (pin & 0x1F);
  121. gpio_port->pmr1s = 1 << (pin & 0x1F);
  122. gpio_port->pmr2s = 1 << (pin & 0x1F);
  123. break;
  124. case 7: /* H function. */
  125. gpio_port->pmr0s = 1 << (pin & 0x1F);
  126. gpio_port->pmr1s = 1 << (pin & 0x1F);
  127. gpio_port->pmr2s = 1 << (pin & 0x1F);
  128. break;
  129. #endif
  130. default:
  131. return GPIO_INVALID_ARGUMENT;
  132. }
  133. /* Disable GPIO control. */
  134. gpio_port->gperc = 1 << (pin & 0x1F);
  135. return GPIO_SUCCESS;
  136. }
  137. /** \brief Enables the GPIO mode of a set of pins.
  138. *
  139. * \param gpiomap The pin map.
  140. * \param size The number of pins in \a gpiomap.
  141. */
  142. void gpio_enable_gpio(const gpio_map_t gpiomap, uint32_t size)
  143. {
  144. uint32_t i;
  145. for (i = 0; i < size; i++) {
  146. gpio_enable_gpio_pin(gpiomap->pin);
  147. gpiomap++;
  148. }
  149. }
  150. /** \brief Enables the GPIO mode of a pin.
  151. *
  152. * \param pin The pin number.\n
  153. * Refer to the product header file `uc3x.h' (where x is the part
  154. * number; e.g. x = a0512) for pin definitions. E.g., to enable the
  155. * GPIO mode of PX21, AVR32_PIN_PX21 can be used. Module pins such as
  156. * AVR32_PWM_3_PIN for PWM channel 3 can also be used to release
  157. * module pins for GPIO.
  158. */
  159. void gpio_enable_gpio_pin(uint32_t pin)
  160. {
  161. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  162. gpio_port->oderc = 1 << (pin & 0x1F);
  163. gpio_port->gpers = 1 << (pin & 0x1F);
  164. }
  165. /** \brief Enables the pull-up resistor of a pin.
  166. *
  167. * \param pin The pin number.
  168. */
  169. void gpio_enable_pin_pull_up(uint32_t pin)
  170. {
  171. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  172. gpio_port->puers = 1 << (pin & 0x1F);
  173. #if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || \
  174. defined(AVR32_GPIO_212_H_INCLUDED)
  175. gpio_port->pderc = 1 << (pin & 0x1F);
  176. #endif
  177. }
  178. /** \brief Disables the pull-up resistor of a pin.
  179. *
  180. * \param pin The pin number.
  181. */
  182. void gpio_disable_pin_pull_up(uint32_t pin)
  183. {
  184. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  185. gpio_port->puerc = 1 << (pin & 0x1F);
  186. }
  187. #if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || \
  188. defined(AVR32_GPIO_212_H_INCLUDED)
  189. /* Added support of Pull-up Resistor, Pull-down Resistor and Buskeeper Control. */
  190. /** \brief Enables the pull-down resistor of a pin.
  191. *
  192. * \param pin The pin number.
  193. */
  194. void gpio_enable_pin_pull_down(uint32_t pin)
  195. {
  196. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  197. gpio_port->puerc = 1 << (pin & 0x1F);
  198. gpio_port->pders = 1 << (pin & 0x1F);
  199. }
  200. /** \brief Disables the pull-down resistor of a pin.
  201. *
  202. * \param pin The pin number.
  203. */
  204. void gpio_disable_pin_pull_down(uint32_t pin)
  205. {
  206. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  207. gpio_port->pderc = 1 << (pin & 0x1F);
  208. }
  209. /** \brief Enables the buskeeper functionality on a pin.
  210. *
  211. * \param pin The pin number.
  212. */
  213. void gpio_enable_pin_buskeeper(uint32_t pin)
  214. {
  215. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  216. gpio_port->puers = 1 << (pin & 0x1F);
  217. gpio_port->pders = 1 << (pin & 0x1F);
  218. }
  219. /** \brief Disables the buskeeper functionality on a pin.
  220. *
  221. * \param pin The pin number.
  222. */
  223. void gpio_disable_pin_buskeeper(uint32_t pin)
  224. {
  225. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  226. gpio_port->puerc = 1 << (pin & 0x1F);
  227. gpio_port->pderc = 1 << (pin & 0x1F);
  228. }
  229. #endif
  230. /** \brief Configuration functionality on a pin.
  231. *
  232. * \param pin The pin number.
  233. * \param flags The configuration.
  234. */
  235. void gpio_configure_pin(uint32_t pin, uint32_t flags)
  236. {
  237. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  238. /* Both pull-up and pull-down set means buskeeper */
  239. #if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || \
  240. defined(AVR32_GPIO_212_H_INCLUDED)
  241. if (flags & GPIO_PULL_DOWN) {
  242. gpio_port->pders = 1 << (pin & 0x1F);
  243. } else {
  244. gpio_port->pderc = 1 << (pin & 0x1F);
  245. }
  246. #endif
  247. if (flags & GPIO_PULL_UP) {
  248. gpio_port->puers = 1 << (pin & 0x1F);
  249. } else {
  250. gpio_port->puerc = 1 << (pin & 0x1F);
  251. }
  252. /* Enable open-drain mode if requested */
  253. #if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || \
  254. defined(AVR32_GPIO_212_H_INCLUDED)
  255. if (flags & GPIO_OPEN_DRAIN) {
  256. gpio_port->odmers = 1 << (pin & 0x1F);
  257. } else {
  258. gpio_port->odmerc = 1 << (pin & 0x1F);
  259. }
  260. #endif
  261. #if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || \
  262. defined(AVR32_GPIO_212_H_INCLUDED)
  263. /* Select drive strength */
  264. if (flags & GPIO_DRIVE_LOW) {
  265. gpio_port->odcr0s = 1 << (pin & 0x1F);
  266. } else {
  267. gpio_port->odcr0c = 1 << (pin & 0x1F);
  268. }
  269. if (flags & GPIO_DRIVE_HIGH) {
  270. gpio_port->odcr1s = 1 << (pin & 0x1F);
  271. } else {
  272. gpio_port->odcr1c = 1 << (pin & 0x1F);
  273. }
  274. #endif
  275. /* Select interrupt level for group */
  276. if (flags & GPIO_INTERRUPT) {
  277. if (flags & GPIO_BOTHEDGES) {
  278. gpio_port->imr0c = 1 << (pin & 0x1F);
  279. gpio_port->imr1c = 1 << (pin & 0x1F);
  280. } else if (flags & GPIO_RISING) {
  281. gpio_port->imr0s = 1 << (pin & 0x1F);
  282. gpio_port->imr1c = 1 << (pin & 0x1F);
  283. } else if (flags & GPIO_FALLING) {
  284. gpio_port->imr0c = 1 << (pin & 0x1F);
  285. gpio_port->imr1s = 1 << (pin & 0x1F);
  286. }
  287. }
  288. /* Select direction and initial pin state */
  289. if (flags & GPIO_DIR_OUTPUT) {
  290. if (flags & GPIO_INIT_HIGH) {
  291. gpio_port->ovrs = 1 << (pin & 0x1F);
  292. } else {
  293. gpio_port->ovrc = 1 << (pin & 0x1F);
  294. }
  295. gpio_port->oders = 1 << (pin & 0x1F);
  296. } else {
  297. gpio_port->oderc = 1 << (pin & 0x1F);
  298. }
  299. /* Enable GPIO */
  300. gpio_port->gpers = 1 << (pin & 0x1F);
  301. }
  302. /** \brief Configuration functionality on a port.
  303. *
  304. * \param port The port number.
  305. * \param mask The mask.
  306. * \param flags The configuration.
  307. */
  308. void gpio_configure_group(uint32_t port, uint32_t mask, uint32_t flags)
  309. {
  310. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[port];
  311. /* Both pull-up and pull-down set means buskeeper */
  312. #if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || \
  313. defined(AVR32_GPIO_212_H_INCLUDED)
  314. if (flags & GPIO_PULL_DOWN) {
  315. gpio_port->pders = mask;
  316. } else {
  317. gpio_port->pderc = mask;
  318. }
  319. #endif
  320. if (flags & GPIO_PULL_UP) {
  321. gpio_port->puers = mask;
  322. } else {
  323. gpio_port->puerc = mask;
  324. }
  325. /* Enable open-drain mode if requested */
  326. #if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || \
  327. defined(AVR32_GPIO_212_H_INCLUDED)
  328. if (flags & GPIO_OPEN_DRAIN) {
  329. gpio_port->odmers = mask;
  330. } else {
  331. gpio_port->odmerc = mask;
  332. }
  333. if (flags & GPIO_OPEN_DRAIN) {
  334. gpio_port->pders = mask;
  335. } else {
  336. gpio_port->pderc = mask;
  337. }
  338. #endif
  339. #if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || \
  340. defined(AVR32_GPIO_212_H_INCLUDED)
  341. /* Select drive strength */
  342. if (flags & GPIO_DRIVE_LOW) {
  343. gpio_port->odcr0s = mask;
  344. } else {
  345. gpio_port->odcr0c = mask;
  346. }
  347. if (flags & GPIO_DRIVE_HIGH) {
  348. gpio_port->odcr1s = mask;
  349. } else {
  350. gpio_port->odcr1c = mask;
  351. }
  352. #endif
  353. /* Select interrupt level for group */
  354. if (flags & GPIO_INTERRUPT) {
  355. if (flags & GPIO_BOTHEDGES) {
  356. gpio_port->imr0c = mask;
  357. gpio_port->imr1c = mask;
  358. } else if (flags & GPIO_RISING) {
  359. gpio_port->imr0s = mask;
  360. gpio_port->imr1c = mask;
  361. } else if (flags & GPIO_FALLING) {
  362. gpio_port->imr0c = mask;
  363. gpio_port->imr1s = mask;
  364. }
  365. }
  366. /* Select direction and initial pin state */
  367. if (flags & GPIO_DIR_OUTPUT) {
  368. if (flags & GPIO_INIT_HIGH) {
  369. gpio_port->ovrs = mask;
  370. } else {
  371. gpio_port->ovrc = mask;
  372. }
  373. gpio_port->oders = mask;
  374. } else {
  375. gpio_port->oderc = mask;
  376. }
  377. /* Enable GPIO */
  378. gpio_port->gpers = mask;
  379. }
  380. /** \brief Returns the value of a pin.
  381. *
  382. * \param pin The pin number.
  383. *
  384. * \return The pin value.
  385. */
  386. bool gpio_get_pin_value(uint32_t pin)
  387. {
  388. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  389. return (gpio_port->pvr >> (pin & 0x1F)) & 1;
  390. }
  391. /** \brief Returns the output value set for a GPIO pin.
  392. *
  393. * \param pin The pin number.
  394. *
  395. * \return The pin output value.
  396. *
  397. * \note This function must be used in conjunction with \ref gpio_set_gpio_pin,
  398. * \ref gpio_clr_gpio_pin and \ref gpio_tgl_gpio_pin.
  399. */
  400. bool gpio_get_gpio_pin_output_value(uint32_t pin)
  401. {
  402. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  403. return (gpio_port->ovr >> (pin & 0x1F)) & 1;
  404. }
  405. /** \brief Returns the output value set for a GPIO pin using open drain.
  406. *
  407. * \param pin The pin number.
  408. *
  409. * \return The pin output value.
  410. *
  411. * \note This function must be used in conjunction with
  412. * \ref gpio_set_gpio_open_drain_pin, \ref gpio_clr_gpio_open_drain_pin
  413. * and \ref gpio_tgl_gpio_open_drain_pin.
  414. */
  415. bool gpio_get_gpio_open_drain_pin_output_value(uint32_t pin)
  416. {
  417. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  418. return ((gpio_port->oder >> (pin & 0x1F)) & 1) ^ 1;
  419. }
  420. /** \brief Drives a GPIO pin to 1.
  421. *
  422. * \param pin The pin number.
  423. */
  424. void gpio_set_gpio_pin(uint32_t pin)
  425. {
  426. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  427. /* Value to be driven on the I/O line: 1. */
  428. gpio_port->ovrs = 1 << (pin & 0x1F);
  429. /* The GPIO output driver is enabled for that pin. */
  430. gpio_port->oders = 1 << (pin & 0x1F);
  431. /* The GPIO module controls that pin. */
  432. gpio_port->gpers = 1 << (pin & 0x1F);
  433. }
  434. /** \brief Drives a GPIO pin to 1.
  435. *
  436. * \param pin The pin number.
  437. *
  438. * \note The function \ref gpio_configure_pin must be called before.
  439. */
  440. void gpio_set_pin_high(uint32_t pin)
  441. {
  442. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  443. /* Value to be driven on the I/O line: 1. */
  444. gpio_port->ovrs = 1 << (pin & 0x1F);
  445. }
  446. /** \brief Drives a GPIO port to 1.
  447. *
  448. * \param port The port number.
  449. * \param mask The mask.
  450. */
  451. void gpio_set_group_high(uint32_t port, uint32_t mask)
  452. {
  453. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[port];
  454. /* Value to be driven on the I/O group: 1. */
  455. gpio_port->ovrs = mask;
  456. }
  457. /** \brief Drives a GPIO pin to 0.
  458. *
  459. * \param pin The pin number.
  460. *
  461. * \note The function \ref gpio_configure_pin must be called before.
  462. */
  463. void gpio_set_pin_low(uint32_t pin)
  464. {
  465. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  466. /* Value to be driven on the I/O line: 0. */
  467. gpio_port->ovrc = 1 << (pin & 0x1F);
  468. }
  469. /** \brief Drives a GPIO pin to 0.
  470. *
  471. * \param pin The pin number.
  472. */
  473. void gpio_clr_gpio_pin(uint32_t pin)
  474. {
  475. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  476. /* Value to be driven on the I/O line: 0. */
  477. gpio_port->ovrc = 1 << (pin & 0x1F);
  478. /* The GPIO output driver is enabled for that pin. */
  479. gpio_port->oders = 1 << (pin & 0x1F);
  480. /* The GPIO module controls that pin. */
  481. gpio_port->gpers = 1 << (pin & 0x1F);
  482. }
  483. /** \brief Drives a GPIO port to 0.
  484. *
  485. * \param port The port number.
  486. * \param mask The mask.
  487. */
  488. void gpio_set_group_low(uint32_t port, uint32_t mask)
  489. {
  490. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[port];
  491. /* Value to be driven on the I/O group: 0. */
  492. gpio_port->ovrc = mask;
  493. }
  494. /** \brief Toggles a GPIO pin.
  495. *
  496. * \param pin The pin number.
  497. */
  498. void gpio_tgl_gpio_pin(uint32_t pin)
  499. {
  500. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  501. /* Toggle the I/O line. */
  502. gpio_port->ovrt = 1 << (pin & 0x1F);
  503. /* The GPIO output driver is enabled for that pin. */
  504. gpio_port->oders = 1 << (pin & 0x1F);
  505. /* The GPIO module controls that pin. */
  506. gpio_port->gpers = 1 << (pin & 0x1F);
  507. }
  508. /** \brief Toggles a GPIO pin.
  509. *
  510. * \param pin The pin number.
  511. *
  512. * \note The function \ref gpio_configure_pin must be called before.
  513. */
  514. void gpio_toggle_pin(uint32_t pin)
  515. {
  516. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  517. /* Toggle the I/O line. */
  518. gpio_port->ovrt = 1 << (pin & 0x1F);
  519. }
  520. /** \brief Toggles a GPIO group.
  521. *
  522. * \param port The port number.
  523. * \param mask The mask.
  524. */
  525. void gpio_toggle_group(uint32_t port, uint32_t mask)
  526. {
  527. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[port];
  528. /* Toggle the I/O port. */
  529. gpio_port->ovrt = mask;
  530. }
  531. /** \brief Drives a GPIO pin to 1 using open drain.
  532. *
  533. * \param pin The pin number.
  534. */
  535. void gpio_set_gpio_open_drain_pin(uint32_t pin)
  536. {
  537. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  538. /* The GPIO output driver is disabled for that pin. */
  539. gpio_port->oderc = 1 << (pin & 0x1F);
  540. /* The GPIO module controls that pin. */
  541. gpio_port->gpers = 1 << (pin & 0x1F);
  542. }
  543. /** \brief Drives a GPIO pin to 0 using open drain.
  544. *
  545. * \param pin The pin number.
  546. */
  547. void gpio_clr_gpio_open_drain_pin(uint32_t pin)
  548. {
  549. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  550. /* Value to be driven on the I/O line: 0. */
  551. gpio_port->ovrc = 1 << (pin & 0x1F);
  552. /* The GPIO output driver is enabled for that pin. */
  553. gpio_port->oders = 1 << (pin & 0x1F);
  554. /* The GPIO module controls that pin. */
  555. gpio_port->gpers = 1 << (pin & 0x1F);
  556. }
  557. /** \brief Toggles a GPIO pin using open drain.
  558. *
  559. * \param pin The pin number.
  560. */
  561. void gpio_tgl_gpio_open_drain_pin(uint32_t pin)
  562. {
  563. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  564. /* Value to be driven on the I/O line if the GPIO output driver is
  565. * enabled: 0. */
  566. gpio_port->ovrc = 1 << (pin & 0x1F);
  567. /* The GPIO output driver is toggled for that pin. */
  568. gpio_port->odert = 1 << (pin & 0x1F);
  569. /* The GPIO module controls that pin. */
  570. gpio_port->gpers = 1 << (pin & 0x1F);
  571. }
  572. /** \brief Enables the glitch filter of a pin.
  573. *
  574. * When the glitch filter is enabled, a glitch with duration of less than 1
  575. * clock cycle is automatically rejected, while a pulse with duration of 2 clock
  576. * cycles or more is accepted. For pulse durations between 1 clock cycle and 2
  577. * clock cycles, the pulse may or may not be taken into account, depending on
  578. * the precise timing of its occurrence. Thus for a pulse to be guaranteed
  579. * visible it must exceed 2 clock cycles, whereas for a glitch to be reliably
  580. * filtered out, its duration must not exceed 1 clock cycle. The filter
  581. * introduces 2 clock cycles latency.
  582. *
  583. * \param pin The pin number.
  584. */
  585. void gpio_enable_pin_glitch_filter(uint32_t pin)
  586. {
  587. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  588. gpio_port->gfers = 1 << (pin & 0x1F);
  589. }
  590. /** \brief Disables the glitch filter of a pin.
  591. *
  592. * \param pin The pin number.
  593. */
  594. void gpio_disable_pin_glitch_filter(uint32_t pin)
  595. {
  596. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  597. gpio_port->gferc = 1 << (pin & 0x1F);
  598. }
  599. /** \brief Configure the edge detector of an input pin
  600. *
  601. * \param pin The pin number.
  602. * \param mode The edge detection mode (\ref GPIO_PIN_CHANGE,
  603. * \ref GPIO_RISING_EDGE or \ref GPIO_FALLING_EDGE).
  604. *
  605. * \return \ref GPIO_SUCCESS or \ref GPIO_INVALID_ARGUMENT.
  606. */
  607. static uint32_t gpio_configure_edge_detector(uint32_t pin, uint32_t mode)
  608. {
  609. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  610. /* Configure the edge detector. */
  611. switch (mode) {
  612. case GPIO_PIN_CHANGE:
  613. gpio_port->imr0c = 1 << (pin & 0x1F);
  614. gpio_port->imr1c = 1 << (pin & 0x1F);
  615. break;
  616. case GPIO_RISING_EDGE:
  617. gpio_port->imr0s = 1 << (pin & 0x1F);
  618. gpio_port->imr1c = 1 << (pin & 0x1F);
  619. break;
  620. case GPIO_FALLING_EDGE:
  621. gpio_port->imr0c = 1 << (pin & 0x1F);
  622. gpio_port->imr1s = 1 << (pin & 0x1F);
  623. break;
  624. default:
  625. return GPIO_INVALID_ARGUMENT;
  626. }
  627. return GPIO_SUCCESS;
  628. }
  629. /** \brief Enables the interrupt of a pin with the specified settings.
  630. *
  631. * \param pin The pin number.
  632. * \param mode The trigger mode (\ref GPIO_PIN_CHANGE, \ref GPIO_RISING_EDGE or
  633. * \ref GPIO_FALLING_EDGE).
  634. *
  635. * \return \ref GPIO_SUCCESS or \ref GPIO_INVALID_ARGUMENT.
  636. */
  637. uint32_t gpio_enable_pin_interrupt(uint32_t pin, uint32_t mode)
  638. {
  639. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  640. /* Enable the glitch filter. */
  641. gpio_port->gfers = 1 << (pin & 0x1F);
  642. /* Configure the edge detector. */
  643. if (GPIO_INVALID_ARGUMENT == gpio_configure_edge_detector(pin, mode)) {
  644. return(GPIO_INVALID_ARGUMENT);
  645. }
  646. /* Enable interrupt. */
  647. gpio_port->iers = 1 << (pin & 0x1F);
  648. return GPIO_SUCCESS;
  649. }
  650. /** \brief Disables the interrupt of a pin.
  651. *
  652. * \param pin The pin number.
  653. */
  654. void gpio_disable_pin_interrupt(uint32_t pin)
  655. {
  656. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  657. gpio_port->ierc = 1 << (pin & 0x1F);
  658. }
  659. /** \brief Gets the interrupt flag of a pin.
  660. *
  661. * \param pin The pin number.
  662. *
  663. * \return The pin interrupt flag.
  664. */
  665. bool gpio_get_pin_interrupt_flag(uint32_t pin)
  666. {
  667. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  668. return (gpio_port->ifr >> (pin & 0x1F)) & 1;
  669. }
  670. /** \brief Clears the interrupt flag of a pin.
  671. *
  672. * \param pin The pin number.
  673. */
  674. void gpio_clear_pin_interrupt_flag(uint32_t pin)
  675. {
  676. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  677. #if (AVR32_GPIO_H_VERSION == 211)
  678. /* GPIO erratum - Writing a one to the GPIO.IFRC register */
  679. /* to clear an interrupt will be ignored if interrupt is enabled for the */
  680. /* corresponding port. */
  681. /* Work around for the erratum - Disable the interrupt, clear it by
  682. * writing */
  683. /* a one to GPIO.IFRC, then enable the interrupt. */
  684. /* Save interrupt enable register. */
  685. uint32_t const gpio_ier = gpio_port->ier;
  686. /* Disable interrupt. */
  687. gpio_port->ierc = gpio_ier;
  688. /* Clear pin interrupt. */
  689. gpio_port->ifrc = 1 << (pin & 0x1F);
  690. /* Restore interrupt enable register. */
  691. gpio_port->ier = gpio_ier;
  692. #else
  693. gpio_port->ifrc = 1 << (pin & 0x1F);
  694. #endif
  695. }
  696. #if UC3L
  697. /** \brief Configure the peripheral event trigger mode of a pin
  698. *
  699. * \param pin The pin number.
  700. * \param mode The trigger mode (\ref GPIO_PIN_CHANGE, \ref GPIO_RISING_EDGE or
  701. * \ref GPIO_FALLING_EDGE).
  702. * \param use_igf use the Input Glitch Filter (true) or not (false).
  703. *
  704. * \return \ref GPIO_SUCCESS or \ref GPIO_INVALID_ARGUMENT.
  705. */
  706. uint32_t gpio_configure_pin_periph_event_mode(uint32_t pin, uint32_t mode,
  707. uint32_t use_igf)
  708. {
  709. volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
  710. if (true == use_igf) {
  711. /* Enable the glitch filter. */
  712. gpio_port->gfers = 1 << (pin & 0x1F);
  713. } else {
  714. /* Disable the glitch filter. */
  715. gpio_port->gferc = 1 << (pin & 0x1F);
  716. }
  717. /* Configure the edge detector. */
  718. return gpio_configure_edge_detector(pin, mode);
  719. }
  720. #endif
  721. /** @} */