gpio_at91.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*!
  2. * Copyright (C) 2007 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. */
  32. /*!
  33. * $Id: gpio_at91.c 5472 2013-12-06 00:16:28Z olereinhardt $
  34. */
  35. #include <cfg/os.h>
  36. #include <cfg/arch.h>
  37. #include <cfg/arch/gpio.h>
  38. #include <arch/arm.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <dev/gpio.h>
  42. /*!
  43. * \brief Get pin level.
  44. *
  45. * \param bank GPIO bank/port number.
  46. * \param bit Bit number of the specified bank/port.
  47. *
  48. * \return 1 if the pin level is high. 0 is returned if the pin level
  49. * is low or if the pin is undefined.
  50. */
  51. int GpioPinGet(int bank, int bit)
  52. {
  53. int rc = 0;
  54. switch(bank) {
  55. #ifdef PIO_PDSR
  56. case NUTGPIO_PORT:
  57. rc = (inr(PIO_PDSR) & _BV(bit)) != 0;
  58. break;
  59. #endif /* PIO_PDSR */
  60. #ifdef PIOA_PDSR
  61. case NUTGPIO_PORTA:
  62. rc = (inr(PIOA_PDSR) & _BV(bit)) != 0;
  63. break;
  64. #endif /* PIOA_PDSR */
  65. #ifdef PIOB_PDSR
  66. case NUTGPIO_PORTB:
  67. rc = (inr(PIOB_PDSR) & _BV(bit)) != 0;
  68. break;
  69. #endif /* PIOB_PDSR */
  70. #ifdef PIOC_PDSR
  71. case NUTGPIO_PORTC:
  72. rc = (inr(PIOC_PDSR) & _BV(bit)) != 0;
  73. break;
  74. #endif /* PIOC_PDSR */
  75. }
  76. return rc;
  77. }
  78. /*!
  79. * \brief Set pin level to low.
  80. *
  81. * Trying to set undefined pins is silently ignored.
  82. *
  83. * \param bank GPIO bank/port number.
  84. * \param bit Bit number of the specified bank/port.
  85. */
  86. void GpioPinSetLow(int bank, int bit)
  87. {
  88. switch(bank) {
  89. #ifdef PIO_CODR
  90. case NUTGPIO_PORT:
  91. outr(PIO_CODR, _BV(bit));
  92. break;
  93. #endif /* PIO_CODR */
  94. #ifdef PIOA_CODR
  95. case NUTGPIO_PORTA:
  96. outr(PIOA_CODR, _BV(bit));
  97. break;
  98. #endif /* PIOA_CODR */
  99. #ifdef PIOB_CODR
  100. case NUTGPIO_PORTB:
  101. outr(PIOB_CODR, _BV(bit));
  102. break;
  103. #endif /* PIOB_CODR */
  104. #ifdef PIOC_CODR
  105. case NUTGPIO_PORTC:
  106. outr(PIOC_CODR, _BV(bit));
  107. break;
  108. #endif /* PIOC_CODR */
  109. }
  110. }
  111. /*!
  112. * \brief Set pin level to high.
  113. *
  114. * Trying to set undefined pins is silently ignored.
  115. *
  116. * \param bank GPIO bank/port number.
  117. * \param bit Bit number of the specified bank/port.
  118. */
  119. void GpioPinSetHigh(int bank, int bit)
  120. {
  121. switch(bank) {
  122. #ifdef PIO_SODR
  123. case NUTGPIO_PORT:
  124. outr(PIO_SODR, _BV(bit));
  125. break;
  126. #endif /* PIO_SODR */
  127. #ifdef PIOA_SODR
  128. case NUTGPIO_PORTA:
  129. outr(PIOA_SODR, _BV(bit));
  130. break;
  131. #endif /* PIOA_SODR */
  132. #ifdef PIOB_SODR
  133. case NUTGPIO_PORTB:
  134. outr(PIOB_SODR, _BV(bit));
  135. break;
  136. #endif /* PIOB_SODR */
  137. #ifdef PIOC_SODR
  138. case NUTGPIO_PORTC:
  139. outr(PIOC_SODR, _BV(bit));
  140. break;
  141. #endif /* PIOC_SODR */
  142. }
  143. }
  144. /*!
  145. * \brief Set pin level.
  146. *
  147. * Trying to set undefined pins is silently ignored.
  148. *
  149. * \param bank GPIO bank/port number.
  150. * \param bit Bit number of the specified bank/port.
  151. * \param value Level, 0 for low or any other value for high.
  152. */
  153. void GpioPinSet(int bank, int bit, int value)
  154. {
  155. if (value) {
  156. GpioPinSetHigh(bank, bit);
  157. }
  158. else {
  159. GpioPinSetLow(bank, bit);
  160. }
  161. }
  162. /*!
  163. * \brief Get all pin levels of a specified bank/port.
  164. *
  165. * \param bank GPIO bank/port number.
  166. *
  167. * \return Pin levels. 0 is returned for unknown banks and pins.
  168. */
  169. unsigned int GpioPortGet(int bank)
  170. {
  171. unsigned int rc = 0;
  172. switch(bank) {
  173. #ifdef PIO_PDSR
  174. case NUTGPIO_PORT:
  175. rc = inr(PIO_PDSR);
  176. break;
  177. #endif /* PIO_PDSR */
  178. #ifdef PIOA_PDSR
  179. case NUTGPIO_PORTA:
  180. rc = inr(PIOA_PDSR);
  181. break;
  182. #endif /* PIO_PDSR */
  183. #ifdef PIOB_PDSR
  184. case NUTGPIO_PORTB:
  185. rc = inr(PIOB_PDSR);
  186. break;
  187. #endif /* PIOB_PDSR */
  188. #ifdef PIOC_PDSR
  189. case NUTGPIO_PORTC:
  190. rc = inr(PIOC_PDSR);
  191. break;
  192. #endif /* PIOC_PDSR */
  193. }
  194. return rc;
  195. }
  196. /*!
  197. * \brief Set multiple pin levels of a bank/port to low.
  198. *
  199. * \param bank GPIO bank/port number.
  200. * \param mask Pin levels are set to low, if the corresponding
  201. * bit in this mask is 1.
  202. *
  203. * \return Levels.
  204. */
  205. void GpioPortSetLow(int bank, unsigned int mask)
  206. {
  207. switch(bank) {
  208. #ifdef PIO_CODR
  209. case NUTGPIO_PORT:
  210. outr(PIO_CODR, mask);
  211. break;
  212. #endif /* PIO_CODR */
  213. #ifdef PIOA_CODR
  214. case NUTGPIO_PORTA:
  215. outr(PIOA_CODR, mask);
  216. break;
  217. #endif /* PIOA_CODR */
  218. #ifdef PIOB_CODR
  219. case NUTGPIO_PORTB:
  220. outr(PIOB_CODR, mask);
  221. break;
  222. #endif /* PIOB_CODR */
  223. #ifdef PIOC_CODR
  224. case NUTGPIO_PORTC:
  225. outr(PIOC_CODR, mask);
  226. break;
  227. #endif /* PIOC_CODR */
  228. }
  229. }
  230. /*!
  231. * \brief Set multiple pin levels of a bank/port to high.
  232. *
  233. * Trying to set undefined ports is silently ignored.
  234. *
  235. * \param bank GPIO bank/port number.
  236. * \param mask Pin levels are set to high, if the corresponding
  237. * bit in this mask is 1.
  238. */
  239. void GpioPortSetHigh(int bank, unsigned int mask)
  240. {
  241. switch(bank) {
  242. #ifdef PIO_SODR
  243. case NUTGPIO_PORT:
  244. outr(PIO_SODR, mask);
  245. break;
  246. #endif /* PIO_SODR */
  247. #ifdef PIOA_SODR
  248. case NUTGPIO_PORTA:
  249. outr(PIOA_SODR, mask);
  250. break;
  251. #endif /* PIOA_SODR */
  252. #ifdef PIOB_SODR
  253. case NUTGPIO_PORTB:
  254. outr(PIOB_SODR, mask);
  255. break;
  256. #endif /* PIOB_SODR */
  257. #ifdef PIOC_SODR
  258. case NUTGPIO_PORTC:
  259. outr(PIOC_SODR, mask);
  260. break;
  261. #endif /* PIOC_SODR */
  262. }
  263. }
  264. /*!
  265. * \brief Set all pin levels of a bank/port.
  266. *
  267. * This routine can be used to simultaneously set all pins of a specific
  268. * port. However, in some implementations the high bit values will be
  269. * set before the low bit values. If this is a problem, you should use
  270. * GpioPortSetHigh() and GpioPortSetLow().
  271. *
  272. * \param bank GPIO bank/port number.
  273. * \param value Pin levels are set to high, if the corresponding
  274. * bit in this mask is 1. All other pin levels are
  275. * set to low.
  276. */
  277. void GpioPortSet(int bank, unsigned int value)
  278. {
  279. if (value) {
  280. GpioPortSetHigh(bank, value);
  281. }
  282. value = ~value;
  283. if (value) {
  284. GpioPortSetLow(bank, value);
  285. }
  286. }
  287. /*!
  288. * \brief Get pin configuration.
  289. *
  290. * \param bank GPIO bank/port number.
  291. * \param bit Bit number of the specified bank/port.
  292. *
  293. * \return Attribute flags of the pin.
  294. */
  295. uint32_t GpioPinConfigGet(int bank, int bit)
  296. {
  297. uint32_t rc = 0;
  298. switch(bank) {
  299. case NUTGPIO_PORT:
  300. #ifdef PIO_PSR
  301. if ((inr(PIO_PSR) & _BV(bit)) == 0) {
  302. rc |= GPIO_CFG_DISABLED;
  303. }
  304. #endif /* PIO_PSR */
  305. #ifdef PIO_OSR
  306. if (inr(PIO_OSR) & _BV(bit)) {
  307. rc |= GPIO_CFG_OUTPUT;
  308. }
  309. #endif /* PIO_OSR */
  310. #ifdef PIO_IFSR
  311. if (inr(PIO_IFSR) & _BV(bit)) {
  312. rc |= GPIO_CFG_DEBOUNCE;
  313. }
  314. #endif /* PIO_IFSR */
  315. #ifdef PIO_MDSR
  316. if (inr(PIO_MDSR) & _BV(bit)) {
  317. rc |= GPIO_CFG_MULTIDRIVE;
  318. }
  319. #endif /* PIO_MDSR */
  320. #ifdef PIO_PUSR
  321. if ((inr(PIO_PUSR) & _BV(bit)) == 0) {
  322. rc |= GPIO_CFG_PULLUP;
  323. }
  324. #endif /* PIO_PUSR */
  325. break;
  326. case NUTGPIO_PORTA:
  327. #ifdef PIOA_PSR
  328. if ((inr(PIOA_PSR) & _BV(bit)) == 0) {
  329. rc |= GPIO_CFG_DISABLED;
  330. }
  331. #endif /* PIOA_PSR */
  332. #ifdef PIOA_OSR
  333. if (inr(PIOA_OSR) & _BV(bit)) {
  334. rc |= GPIO_CFG_OUTPUT;
  335. }
  336. #endif /* PIOA_OSR */
  337. #ifdef PIOA_IFSR
  338. if (inr(PIOA_IFSR) & _BV(bit)) {
  339. rc |= GPIO_CFG_DEBOUNCE;
  340. }
  341. #endif /* PIOA_IFSR */
  342. #ifdef PIOA_MDSR
  343. if (inr(PIOA_MDSR) & _BV(bit)) {
  344. rc |= GPIO_CFG_MULTIDRIVE;
  345. }
  346. #endif /* PIOA_MDSR */
  347. #ifdef PIOA_PUSR
  348. if ((inr(PIOA_PUSR) & _BV(bit)) == 0) {
  349. rc |= GPIO_CFG_PULLUP;
  350. }
  351. #endif /* PIOA_PUSR */
  352. break;
  353. case NUTGPIO_PORTB:
  354. #ifdef PIOB_PSR
  355. if ((inr(PIOB_PSR) & _BV(bit)) == 0) {
  356. rc |= GPIO_CFG_DISABLED;
  357. }
  358. #endif /* PIOB_PSR */
  359. #ifdef PIOB_OSR
  360. if (inr(PIOB_OSR) & _BV(bit)) {
  361. rc |= GPIO_CFG_OUTPUT;
  362. }
  363. #endif /* PIOB_OSR */
  364. #ifdef PIOB_IFSR
  365. if (inr(PIOB_IFSR) & _BV(bit)) {
  366. rc |= GPIO_CFG_DEBOUNCE;
  367. }
  368. #endif /* PIOB_IFSR */
  369. #ifdef PIOB_MDSR
  370. if (inr(PIOB_MDSR) & _BV(bit)) {
  371. rc |= GPIO_CFG_MULTIDRIVE;
  372. }
  373. #endif /* PIOB_MDSR */
  374. #ifdef PIOB_PUSR
  375. if ((inr(PIOB_PUSR) & _BV(bit)) == 0) {
  376. rc |= GPIO_CFG_PULLUP;
  377. }
  378. #endif /* PIOB_PUSR */
  379. break;
  380. case NUTGPIO_PORTC:
  381. #ifdef PIOC_PSR
  382. if ((inr(PIOC_PSR) & _BV(bit)) == 0) {
  383. rc |= GPIO_CFG_DISABLED;
  384. }
  385. #endif /* PIOC_PSR */
  386. #ifdef PIOC_OSR
  387. if (inr(PIOC_OSR) & _BV(bit)) {
  388. rc |= GPIO_CFG_OUTPUT;
  389. }
  390. #endif /* PIOC_OSR */
  391. #ifdef PIOC_IFSR
  392. if (inr(PIOC_IFSR) & _BV(bit)) {
  393. rc |= GPIO_CFG_DEBOUNCE;
  394. }
  395. #endif /* PIOC_IFSR */
  396. #ifdef PIOC_MDSR
  397. if (inr(PIOC_MDSR) & _BV(bit)) {
  398. rc |= GPIO_CFG_MULTIDRIVE;
  399. }
  400. #endif /* PIOC_MDSR */
  401. #ifdef PIOC_PUSR
  402. if ((inr(PIOC_PUSR) & _BV(bit)) == 0) {
  403. rc |= GPIO_CFG_PULLUP;
  404. }
  405. #endif /* PIOC_PUSR */
  406. break;
  407. }
  408. return rc;
  409. }
  410. /*!
  411. * \brief Set port wide pin configuration.
  412. *
  413. * \note This function does not check for undefined ports and pins or
  414. * invalid attributes. If this is required, use GpioPinConfigSet().
  415. *
  416. * \param bank GPIO bank/port number.
  417. * \param mask The given attributes are set for a specific pin, if the
  418. * corresponding bit in this mask is 1.
  419. * \param flags Attribute flags to set.
  420. *
  421. * \return Always 0.
  422. */
  423. int GpioPortConfigSet(int bank, unsigned int mask, uint32_t flags)
  424. {
  425. switch(bank) {
  426. case NUTGPIO_PORT:
  427. #ifdef PIO_PDR
  428. if (flags & GPIO_CFG_DISABLED) {
  429. outr(PIO_PDR, mask);
  430. }
  431. else {
  432. outr(PIO_PER, mask);
  433. #ifdef PMC_PCER
  434. outr(PMC_PCER, _BV(PIO_ID));
  435. #endif /* PMC_PCER */
  436. }
  437. #endif /* PIO_PDR */
  438. #ifdef PIO_PUER
  439. if (flags & GPIO_CFG_PULLUP) {
  440. outr(PIO_PUER, mask);
  441. }
  442. else {
  443. outr(PIO_PUDR, mask);
  444. }
  445. #endif /* PIO_PUER */
  446. #ifdef PIO_IFER
  447. if (flags & GPIO_CFG_DEBOUNCE) {
  448. outr(PIO_IFER, mask);
  449. }
  450. else {
  451. outr(PIO_IFDR, mask);
  452. }
  453. #endif /* PIO_IFER */
  454. #ifdef PIO_ODR
  455. if ((flags & GPIO_CFG_OUTPUT) == 0) {
  456. outr(PIO_ODR, mask);
  457. }
  458. #endif /* PIO_ODR */
  459. #ifdef PIO_MDER
  460. if (flags & GPIO_CFG_MULTIDRIVE) {
  461. outr(PIO_MDER, mask);
  462. }
  463. else {
  464. outr(PIO_MDDR, mask);
  465. }
  466. #endif /* PIO_MDER */
  467. #ifdef PIO_OER
  468. if (flags & GPIO_CFG_OUTPUT) {
  469. outr(PIO_OER, mask);
  470. }
  471. #endif /* PIO_OER */
  472. break;
  473. case NUTGPIO_PORTA:
  474. #ifdef PIOA_PDR
  475. if (flags & GPIO_CFG_DISABLED) {
  476. outr(PIOA_PDR, mask);
  477. }
  478. else {
  479. outr(PIOA_PER, mask);
  480. #ifdef PMC_PCER
  481. outr(PMC_PCER, _BV(PIOA_ID));
  482. #endif /* PMC_PCER */
  483. }
  484. #endif /* PIOA_PDR */
  485. #ifdef PIOA_PUER
  486. if (flags & GPIO_CFG_PULLUP) {
  487. outr(PIOA_PUER, mask);
  488. }
  489. else {
  490. outr(PIOA_PUDR, mask);
  491. }
  492. #endif /* PIOA_PUER */
  493. #ifdef PIOA_IFER
  494. if (flags & GPIO_CFG_DEBOUNCE) {
  495. outr(PIOA_IFER, mask);
  496. }
  497. else {
  498. outr(PIOA_IFDR, mask);
  499. }
  500. #endif /* PIOA_IFER */
  501. #ifdef PIOA_ODR
  502. if ((flags & GPIO_CFG_OUTPUT) == 0) {
  503. outr(PIOA_ODR, mask);
  504. }
  505. #endif /* PIOA_ODR */
  506. #ifdef PIOA_MDER
  507. if (flags & GPIO_CFG_MULTIDRIVE) {
  508. outr(PIOA_MDER, mask);
  509. }
  510. else {
  511. outr(PIOA_MDDR, mask);
  512. }
  513. #endif /* PIOA_MDER */
  514. #ifdef PIOA_OER
  515. if (flags & GPIO_CFG_OUTPUT) {
  516. outr(PIOA_OER, mask);
  517. }
  518. #endif /* PIOA_OER */
  519. break;
  520. case NUTGPIO_PORTB:
  521. #ifdef PIOB_PDR
  522. if (flags & GPIO_CFG_DISABLED) {
  523. outr(PIOB_PDR, mask);
  524. }
  525. else {
  526. outr(PIOB_PER, mask);
  527. #ifdef PMC_PCER
  528. outr(PMC_PCER, _BV(PIOB_ID));
  529. #endif /* PMC_PCER */
  530. }
  531. #endif /* PIOB_PDR */
  532. #ifdef PIOB_PUER
  533. if (flags & GPIO_CFG_PULLUP) {
  534. outr(PIOB_PUER, mask);
  535. }
  536. else {
  537. outr(PIOB_PUDR, mask);
  538. }
  539. #endif /* PIOB_PUER */
  540. #ifdef PIOB_IFER
  541. if (flags & GPIO_CFG_DEBOUNCE) {
  542. outr(PIOB_IFER, mask);
  543. }
  544. else {
  545. outr(PIOB_IFDR, mask);
  546. }
  547. #endif /* PIOB_IFER */
  548. #ifdef PIOB_ODR
  549. if ((flags & GPIO_CFG_OUTPUT) == 0) {
  550. outr(PIOB_ODR, mask);
  551. }
  552. #endif /* PIOB_ODR */
  553. #ifdef PIOB_MDER
  554. if (flags & GPIO_CFG_MULTIDRIVE) {
  555. outr(PIOB_MDER, mask);
  556. }
  557. else {
  558. outr(PIOB_MDDR, mask);
  559. }
  560. #endif /* PIOB_MDER */
  561. #ifdef PIOB_OER
  562. if (flags & GPIO_CFG_OUTPUT) {
  563. outr(PIOB_OER, mask);
  564. }
  565. #endif /* PIOB_OER */
  566. break;
  567. case NUTGPIO_PORTC:
  568. #ifdef PIOC_PDR
  569. if (flags & GPIO_CFG_DISABLED) {
  570. outr(PIOC_PDR, mask);
  571. }
  572. else {
  573. outr(PIOC_PER, mask);
  574. #ifdef PMC_PCER
  575. outr(PMC_PCER, _BV(PIOC_ID));
  576. #endif /* PMC_PCER */
  577. }
  578. #endif /* PIOC_PDR */
  579. #ifdef PIOC_PUER
  580. if (flags & GPIO_CFG_PULLUP) {
  581. outr(PIOC_PUER, mask);
  582. }
  583. else {
  584. outr(PIOC_PUDR, mask);
  585. }
  586. #endif /* PIOC_PUER */
  587. #ifdef PIOC_IFER
  588. if (flags & GPIO_CFG_DEBOUNCE) {
  589. outr(PIOC_IFER, mask);
  590. }
  591. else {
  592. outr(PIOC_IFDR, mask);
  593. }
  594. #endif /* PIOC_IFER */
  595. #ifdef PIOC_ODR
  596. if ((flags & GPIO_CFG_OUTPUT) == 0) {
  597. outr(PIOC_ODR, mask);
  598. }
  599. #endif /* PIOC_ODR */
  600. #ifdef PIOC_MDER
  601. if (flags & GPIO_CFG_MULTIDRIVE) {
  602. outr(PIOC_MDER, mask);
  603. }
  604. else {
  605. outr(PIOC_MDDR, mask);
  606. }
  607. #endif /* PIOC_MDER */
  608. #ifdef PIOC_OER
  609. if (flags & GPIO_CFG_OUTPUT) {
  610. outr(PIOC_OER, mask);
  611. }
  612. #endif /* PIOC_OER */
  613. break;
  614. }
  615. return 0;
  616. }
  617. /*!
  618. * \brief Set pin configuration.
  619. *
  620. * Applications may also use this function to make sure, that a specific
  621. * attribute is available for a specific pin.
  622. *
  623. * \note GPIO pins are typically initialized to a safe state after power
  624. * up. This routine is not able to determine the consequences of
  625. * changing pin configurations. In the worst case you may permanently
  626. * damage your hardware by bad pin settings.
  627. *
  628. * \param bank GPIO bank/port number.
  629. * \param bit Bit number of the specified bank/port.
  630. * \param flags Attribute flags.
  631. *
  632. * \return 0 if all attributes had been set, -1 otherwise.
  633. */
  634. int GpioPinConfigSet(int bank, int bit, uint32_t flags)
  635. {
  636. GpioPortConfigSet(bank, _BV(bit), flags);
  637. /* Check the result. */
  638. if (GpioPinConfigGet(bank, bit) != flags) {
  639. return -1;
  640. }
  641. return 0;
  642. }
  643. /*!
  644. * \brief Register a GPIO pin interrupt handler.
  645. *
  646. * Generating interrupts on GPIO pin changes is not supported on all
  647. * platforms. In this case dedicated external interrupt pins may
  648. * be used with NutRegisterIrqHandler().
  649. *
  650. * Interrupts are triggered on rising and falling edges. Level triggering
  651. * or triggering on specific edges is not supported.
  652. *
  653. * After registering, interrupts are disabled. Calling GpioIrqEnable()
  654. * is required to activate the interrupt.
  655. *
  656. * The following code fragment registers an interrupt handler which is
  657. * called on each change of bit 4 of the first GPIO port:
  658. * \code
  659. * #include <dev/gpio.h>
  660. *
  661. * static void PinChange(void *arg)
  662. * {
  663. * ...
  664. * }
  665. *
  666. * {
  667. * ...
  668. * GpioPinConfigSet(0, 4, GPIO_CFG_PULLUP);
  669. * GpioRegisterIrqHandler(&sig_GPIO, 4, PinChange, NULL);
  670. * GpioIrqEnable(&sig_GPIO, 4);
  671. * ...
  672. * }
  673. * \endcode
  674. *
  675. * \param sig Bank/port interrupt to be associated with this handler.
  676. * \param bit Bit number of the specified bank/port.
  677. * \param handler This routine will be called by Nut/OS, when the specified
  678. * pin changes its state.
  679. * \param arg Argument to be passed to the interrupt handler routine.
  680. *
  681. * \return 0 on success, -1 otherwise.
  682. */
  683. int GpioRegisterIrqHandler(GPIO_SIGNAL * sig, int bit, void (*handler) (void *), void *arg)
  684. {
  685. int rc = 0;
  686. if (sig->ios_vector == 0) {
  687. /* This is the first call. Allocate the vector table. */
  688. sig->ios_vector = malloc(sizeof(GPIO_VECTOR) * 32);
  689. if (sig->ios_vector) {
  690. memset(sig->ios_vector, 0, sizeof(GPIO_VECTOR) * 32);
  691. /* Register our internal PIO interrupt service. */
  692. rc = NutRegisterIrqHandler(sig->ios_sig, sig->ios_handler, sig->ios_vector);
  693. if (rc == 0) {
  694. rc = NutIrqEnable(sig->ios_sig);
  695. }
  696. }
  697. else {
  698. rc = -1;
  699. }
  700. }
  701. sig->ios_vector[bit].iov_handler = handler;
  702. sig->ios_vector[bit].iov_arg = arg;
  703. return rc;
  704. }
  705. /*!
  706. * \brief Enable a specified GPIO interrupt.
  707. *
  708. * A related interrupt handler must have been registered before calling
  709. * this function. See GpioRegisterIrqHandler().
  710. *
  711. * \param sig Interrupt to enable.
  712. * \param bit Bit number of the specified bank/port.
  713. *
  714. * \return 0 on success, -1 otherwise.
  715. */
  716. int GpioIrqEnable(GPIO_SIGNAL * sig, int bit)
  717. {
  718. return (sig->ios_ctl) (NUT_IRQCTL_ENABLE, NULL, bit);
  719. }
  720. /*!
  721. * \brief Disable a specified GPIO interrupt.
  722. *
  723. * \param sig Interrupt to disable.
  724. * \param bit Bit number of the specified bank/port.
  725. *
  726. * \return 0 on success, -1 otherwise.
  727. */
  728. int GpioIrqDisable(GPIO_SIGNAL * sig, int bit)
  729. {
  730. return (sig->ios_ctl) (NUT_IRQCTL_DISABLE, NULL, bit);
  731. }