gpio_sam3u.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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_sam3u.c 4590 2012-09-10 10:26:01Z olereinhardt $
  34. */
  35. #include <arch/cm3.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <dev/gpio.h>
  39. /*!
  40. * \brief Get pin level.
  41. *
  42. * \param bank GPIO bank/port number.
  43. * \param bit Bit number of the specified bank/port.
  44. *
  45. * \return 1 if the pin level is high. 0 is returned if the pin level
  46. * is low or if the pin is undefined.
  47. */
  48. int GpioPinGet(int bank, int bit)
  49. {
  50. int rc = 0;
  51. switch(bank) {
  52. case NUTGPIO_PORTA:
  53. rc = (inr(AT91C_PIOA_PDSR) & _BV(bit)) != 0;
  54. break;
  55. case NUTGPIO_PORTB:
  56. rc = (inr(AT91C_PIOB_PDSR) & _BV(bit)) != 0;
  57. break;
  58. case NUTGPIO_PORTC:
  59. rc = (inr(AT91C_PIOC_PDSR) & _BV(bit)) != 0;
  60. break;
  61. }
  62. return rc;
  63. }
  64. /*!
  65. * \brief Set pin level to low.
  66. *
  67. * Trying to set undefined pins is silently ignored.
  68. *
  69. * \param bank GPIO bank/port number.
  70. * \param bit Bit number of the specified bank/port.
  71. */
  72. void GpioPinSetLow(int bank, int bit)
  73. {
  74. switch(bank) {
  75. case NUTGPIO_PORTA:
  76. outr(AT91C_PIOA_CODR, _BV(bit));
  77. break;
  78. case NUTGPIO_PORTB:
  79. outr(AT91C_PIOB_CODR, _BV(bit));
  80. break;
  81. case NUTGPIO_PORTC:
  82. outr(AT91C_PIOC_CODR, _BV(bit));
  83. break;
  84. }
  85. }
  86. /*!
  87. * \brief Set pin level to high.
  88. *
  89. * Trying to set undefined pins is silently ignored.
  90. *
  91. * \param bank GPIO bank/port number.
  92. * \param bit Bit number of the specified bank/port.
  93. */
  94. void GpioPinSetHigh(int bank, int bit)
  95. {
  96. switch(bank) {
  97. case NUTGPIO_PORTA:
  98. outr(AT91C_PIOA_SODR, _BV(bit));
  99. break;
  100. case NUTGPIO_PORTB:
  101. outr(AT91C_PIOB_SODR, _BV(bit));
  102. break;
  103. case NUTGPIO_PORTC:
  104. outr(AT91C_PIOC_SODR, _BV(bit));
  105. break;
  106. }
  107. }
  108. /*!
  109. * \brief Set pin level.
  110. *
  111. * Trying to set undefined pins is silently ignored.
  112. *
  113. * \param bank GPIO bank/port number.
  114. * \param bit Bit number of the specified bank/port.
  115. * \param value Level, 0 for low or any other value for high.
  116. */
  117. void GpioPinSet(int bank, int bit, int value)
  118. {
  119. if (value) {
  120. GpioPinSetHigh(bank, bit);
  121. }
  122. else {
  123. GpioPinSetLow(bank, bit);
  124. }
  125. }
  126. /*!
  127. * \brief Get all pin levels of a specified bank/port.
  128. *
  129. * \param bank GPIO bank/port number.
  130. *
  131. * \return Pin levels. 0 is returned for unknown banks and pins.
  132. */
  133. unsigned int GpioPortGet(int bank)
  134. {
  135. unsigned int rc = 0;
  136. switch(bank) {
  137. case NUTGPIO_PORTA:
  138. rc = inr(AT91C_PIOA_PDSR);
  139. break;
  140. case NUTGPIO_PORTB:
  141. rc = inr(AT91C_PIOB_PDSR);
  142. break;
  143. case NUTGPIO_PORTC:
  144. rc = inr(AT91C_PIOC_PDSR);
  145. break;
  146. }
  147. return rc;
  148. }
  149. /*!
  150. * \brief Set multiple pin levels of a bank/port to low.
  151. *
  152. * \param bank GPIO bank/port number.
  153. * \param mask Pin levels are set to low, if the corresponding
  154. * bit in this mask is 1.
  155. *
  156. * \return Levels.
  157. */
  158. void GpioPortSetLow(int bank, unsigned int mask)
  159. {
  160. switch(bank) {
  161. case NUTGPIO_PORTA:
  162. outr(AT91C_PIOA_CODR, mask);
  163. break;
  164. case NUTGPIO_PORTB:
  165. outr(AT91C_PIOB_CODR, mask);
  166. break;
  167. case NUTGPIO_PORTC:
  168. outr(AT91C_PIOC_CODR, mask);
  169. break;
  170. }
  171. }
  172. /*!
  173. * \brief Set multiple pin levels of a bank/port to high.
  174. *
  175. * Trying to set undefined ports is silently ignored.
  176. *
  177. * \param bank GPIO bank/port number.
  178. * \param mask Pin levels are set to high, if the corresponding
  179. * bit in this mask is 1.
  180. */
  181. void GpioPortSetHigh(int bank, unsigned int mask)
  182. {
  183. switch(bank) {
  184. case NUTGPIO_PORTA:
  185. outr(AT91C_PIOA_SODR, mask);
  186. break;
  187. case NUTGPIO_PORTB:
  188. outr(AT91C_PIOB_SODR, mask);
  189. break;
  190. case NUTGPIO_PORTC:
  191. outr(AT91C_PIOC_SODR, mask);
  192. break;
  193. }
  194. }
  195. /*!
  196. * \brief Set all pin levels of a bank/port.
  197. *
  198. * This routine can be used to simultaneously set all pins of a specific
  199. * port. However, in some implementations the high bit values will be
  200. * set before the low bit values. If this is a problem, you should use
  201. * GpioPortSetHigh() and GpioPortSetLow().
  202. *
  203. * \param bank GPIO bank/port number.
  204. * \param value Pin levels are set to high, if the corresponding
  205. * bit in this mask is 1. All other pin levels are
  206. * set to low.
  207. */
  208. void GpioPortSet(int bank, unsigned int value)
  209. {
  210. if (value) {
  211. GpioPortSetHigh(bank, value);
  212. }
  213. value = ~value;
  214. if (value) {
  215. GpioPortSetLow(bank, value);
  216. }
  217. }
  218. /*!
  219. * \brief Get pin configuration.
  220. *
  221. * \param bank GPIO bank/port number.
  222. * \param bit Bit number of the specified bank/port.
  223. *
  224. * \return Attribute flags of the pin.
  225. */
  226. uint32_t GpioPinConfigGet(int bank, int bit)
  227. {
  228. uint32_t rc = 0;
  229. switch(bank) {
  230. case NUTGPIO_PORTA:
  231. if ((inr(AT91C_PIOA_PSR) & _BV(bit)) == 0) {
  232. rc |= GPIO_CFG_DISABLED;
  233. }
  234. if (inr(AT91C_PIOA_OSR) & _BV(bit)) {
  235. rc |= GPIO_CFG_OUTPUT;
  236. }
  237. if (inr(AT91C_PIOA_IFSR) & _BV(bit)) {
  238. rc |= GPIO_CFG_DEBOUNCE;
  239. }
  240. if (inr(AT91C_PIOA_MDSR) & _BV(bit)) {
  241. rc |= GPIO_CFG_MULTIDRIVE;
  242. }
  243. if ((inr(AT91C_PIOA_PPUSR) & _BV(bit)) == 0) {
  244. rc |= GPIO_CFG_PULLUP;
  245. }
  246. break;
  247. case NUTGPIO_PORTB:
  248. if ((inr(AT91C_PIOB_PSR) & _BV(bit)) == 0) {
  249. rc |= GPIO_CFG_DISABLED;
  250. }
  251. if (inr(AT91C_PIOB_OSR) & _BV(bit)) {
  252. rc |= GPIO_CFG_OUTPUT;
  253. }
  254. if (inr(AT91C_PIOB_IFSR) & _BV(bit)) {
  255. rc |= GPIO_CFG_DEBOUNCE;
  256. }
  257. if (inr(AT91C_PIOB_MDSR) & _BV(bit)) {
  258. rc |= GPIO_CFG_MULTIDRIVE;
  259. }
  260. if ((inr(AT91C_PIOB_PPUSR) & _BV(bit)) == 0) {
  261. rc |= GPIO_CFG_PULLUP;
  262. }
  263. break;
  264. case NUTGPIO_PORTC:
  265. if ((inr(AT91C_PIOC_PSR) & _BV(bit)) == 0) {
  266. rc |= GPIO_CFG_DISABLED;
  267. }
  268. if (inr(AT91C_PIOC_OSR) & _BV(bit)) {
  269. rc |= GPIO_CFG_OUTPUT;
  270. }
  271. if (inr(AT91C_PIOC_IFSR) & _BV(bit)) {
  272. rc |= GPIO_CFG_DEBOUNCE;
  273. }
  274. if (inr(AT91C_PIOC_MDSR) & _BV(bit)) {
  275. rc |= GPIO_CFG_MULTIDRIVE;
  276. }
  277. if ((inr(AT91C_PIOC_PPUSR) & _BV(bit)) == 0) {
  278. rc |= GPIO_CFG_PULLUP;
  279. }
  280. break;
  281. }
  282. return rc;
  283. }
  284. /*!
  285. * \brief Set port wide pin configuration.
  286. *
  287. * \note This function does not check for undefined ports and pins or
  288. * invalid attributes. If this is required, use GpioPinConfigSet().
  289. *
  290. * \param bank GPIO bank/port number.
  291. * \param mask The given attributes are set for a specific pin, if the
  292. * corresponding bit in this mask is 1.
  293. * \param flags Attribute flags to set.
  294. *
  295. * \return Always 0.
  296. */
  297. int GpioPortConfigSet(int bank, unsigned int mask, uint32_t flags)
  298. {
  299. switch(bank) {
  300. case NUTGPIO_PORTA:
  301. if (flags & GPIO_CFG_DISABLED) {
  302. outr(AT91C_PIOA_PDR, mask);
  303. }
  304. else {
  305. outr(AT91C_PIOA_PER, mask);
  306. outr(AT91C_PMC_PCER, _BV(AT91C_ID_PIOA));
  307. }
  308. if (flags & GPIO_CFG_PULLUP) {
  309. outr(AT91C_PIOA_PPUER, mask);
  310. }
  311. else {
  312. outr(AT91C_PIOA_PPUDR, mask);
  313. }
  314. if (flags & GPIO_CFG_DEBOUNCE) {
  315. outr(AT91C_PIOA_IFER, mask);
  316. }
  317. else {
  318. outr(AT91C_PIOA_IFDR, mask);
  319. }
  320. if ((flags & GPIO_CFG_OUTPUT) == 0) {
  321. outr(AT91C_PIOA_ODR, mask);
  322. }
  323. if (flags & GPIO_CFG_MULTIDRIVE) {
  324. outr(AT91C_PIOA_MDER, mask);
  325. }
  326. else {
  327. outr(AT91C_PIOA_MDDR, mask);
  328. }
  329. if (flags & GPIO_CFG_OUTPUT) {
  330. outr(AT91C_PIOA_OER, mask);
  331. }
  332. break;
  333. case NUTGPIO_PORTB:
  334. if (flags & GPIO_CFG_DISABLED) {
  335. outr(AT91C_PIOB_PDR, mask);
  336. }
  337. else {
  338. outr(AT91C_PIOB_PER, mask);
  339. outr(AT91C_PMC_PCER, _BV(AT91C_ID_PIOB));
  340. }
  341. if (flags & GPIO_CFG_PULLUP) {
  342. outr(AT91C_PIOB_PPUER, mask);
  343. }
  344. else {
  345. outr(AT91C_PIOB_PPUDR, mask);
  346. }
  347. if (flags & GPIO_CFG_DEBOUNCE) {
  348. outr(AT91C_PIOB_IFER, mask);
  349. }
  350. else {
  351. outr(AT91C_PIOB_IFDR, mask);
  352. }
  353. if ((flags & GPIO_CFG_OUTPUT) == 0) {
  354. outr(AT91C_PIOB_ODR, mask);
  355. }
  356. if (flags & GPIO_CFG_MULTIDRIVE) {
  357. outr(AT91C_PIOB_MDER, mask);
  358. }
  359. else {
  360. outr(AT91C_PIOB_MDDR, mask);
  361. }
  362. if (flags & GPIO_CFG_OUTPUT) {
  363. outr(AT91C_PIOB_OER, mask);
  364. }
  365. break;
  366. case NUTGPIO_PORTC:
  367. if (flags & GPIO_CFG_DISABLED) {
  368. outr(AT91C_PIOC_PDR, mask);
  369. }
  370. else {
  371. outr(AT91C_PIOC_PER, mask);
  372. outr(AT91C_PMC_PCER, _BV(AT91C_ID_PIOC));
  373. }
  374. if (flags & GPIO_CFG_PULLUP) {
  375. outr(AT91C_PIOC_PPUER, mask);
  376. }
  377. else {
  378. outr(AT91C_PIOC_PPUDR, mask);
  379. }
  380. if (flags & GPIO_CFG_DEBOUNCE) {
  381. outr(AT91C_PIOC_IFER, mask);
  382. }
  383. else {
  384. outr(AT91C_PIOC_IFDR, mask);
  385. }
  386. if ((flags & GPIO_CFG_OUTPUT) == 0) {
  387. outr(AT91C_PIOC_ODR, mask);
  388. }
  389. if (flags & GPIO_CFG_MULTIDRIVE) {
  390. outr(AT91C_PIOC_MDER, mask);
  391. }
  392. else {
  393. outr(AT91C_PIOC_MDDR, mask);
  394. }
  395. if (flags & GPIO_CFG_OUTPUT) {
  396. outr(AT91C_PIOC_OER, mask);
  397. }
  398. break;
  399. }
  400. return 0;
  401. }
  402. /*!
  403. * \brief Set pin configuration.
  404. *
  405. * Applications may also use this function to make sure, that a specific
  406. * attribute is available for a specific pin.
  407. *
  408. * \note GPIO pins are typically initialized to a safe state after power
  409. * up. This routine is not able to determine the consequences of
  410. * changing pin configurations. In the worst case you may permanently
  411. * damage your hardware by bad pin settings.
  412. *
  413. * \param bank GPIO bank/port number.
  414. * \param bit Bit number of the specified bank/port.
  415. * \param flags Attribute flags.
  416. *
  417. * \return 0 if all attributes had been set, -1 otherwise.
  418. */
  419. int GpioPinConfigSet(int bank, int bit, uint32_t flags)
  420. {
  421. GpioPortConfigSet(bank, _BV(bit), flags);
  422. /* Check the result. */
  423. if (GpioPinConfigGet(bank, bit) != flags) {
  424. return -1;
  425. }
  426. return 0;
  427. }
  428. /*!
  429. * \brief Register a GPIO pin interrupt handler.
  430. *
  431. * Generating interrupts on GPIO pin changes is not supported on all
  432. * platforms. In this case dedicated external interrupt pins may
  433. * be used with NutRegisterIrqHandler().
  434. *
  435. * Interrupts are triggered on rising and falling edges. Level triggering
  436. * or triggering on specific edges is not supported.
  437. *
  438. * After registering, interrupts are disabled. Calling GpioIrqEnable()
  439. * is required to activate the interrupt.
  440. *
  441. * The following code fragment registers an interrupt handler which is
  442. * called on each change of bit 4 of the first GPIO port:
  443. * \code
  444. * #include <dev/gpio.h>
  445. *
  446. * static void PinChange(void *arg)
  447. * {
  448. * ...
  449. * }
  450. *
  451. * {
  452. * ...
  453. * GpioPinConfigSet(0, 4, GPIO_CFG_PULLUP);
  454. * GpioRegisterIrqHandler(&sig_GPIO, 4, PinChange, NULL);
  455. * GpioIrqEnable(&sig_GPIO, 4);
  456. * ...
  457. * }
  458. * \endcode
  459. *
  460. * \param sig Bank/port interrupt to be associated with this handler.
  461. * \param bit Bit number of the specified bank/port.
  462. * \param handler This routine will be called by Nut/OS, when the specified
  463. * pin changes its state.
  464. * \param arg Argument to be passed to the interrupt handler routine.
  465. *
  466. * \return 0 on success, -1 otherwise.
  467. */
  468. int GpioRegisterIrqHandler(GPIO_SIGNAL * sig, int bit, void (*handler) (void *), void *arg)
  469. {
  470. int rc = 0;
  471. if (sig->ios_vector == 0) {
  472. /* This is the first call. Allocate the vector table. */
  473. sig->ios_vector = malloc(sizeof(GPIO_VECTOR) * 32);
  474. if (sig->ios_vector) {
  475. memset(sig->ios_vector, 0, sizeof(GPIO_VECTOR) * 32);
  476. /* Register our internal PIO interrupt service. */
  477. rc = NutRegisterIrqHandler(sig->ios_sig, sig->ios_handler, sig->ios_vector);
  478. if (rc == 0) {
  479. rc = NutIrqEnable(sig->ios_sig);
  480. }
  481. }
  482. else {
  483. rc = -1;
  484. }
  485. }
  486. sig->ios_vector[bit].iov_handler = handler;
  487. sig->ios_vector[bit].iov_arg = arg;
  488. return rc;
  489. }
  490. /*!
  491. * \brief Enable a specified GPIO interrupt.
  492. *
  493. * A related interrupt handler must have been registered before calling
  494. * this function. See GpioRegisterIrqHandler().
  495. *
  496. * \param sig Interrupt to enable.
  497. * \param bit Bit number of the specified bank/port.
  498. *
  499. * \return 0 on success, -1 otherwise.
  500. */
  501. int GpioIrqEnable(GPIO_SIGNAL * sig, int bit)
  502. {
  503. return (sig->ios_ctl) (NUT_IRQCTL_ENABLE, NULL, bit);
  504. }
  505. /*!
  506. * \brief Disable a specified GPIO interrupt.
  507. *
  508. * \param sig Interrupt to disable.
  509. * \param bit Bit number of the specified bank/port.
  510. *
  511. * \return 0 on success, -1 otherwise.
  512. */
  513. int GpioIrqDisable(GPIO_SIGNAL * sig, int bit)
  514. {
  515. return (sig->ios_ctl) (NUT_IRQCTL_DISABLE, NULL, bit);
  516. }