lpc176x_gpio.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * Copyright (C) 2012 by Ole Reinhardt (ole.reinhardt@embedded-it.de)
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*
  35. * \verbatim
  36. * $Id: lpc176x_gpio.c $
  37. * \endverbatim
  38. */
  39. #include <cfg/os.h>
  40. #include <cfg/arch.h>
  41. #include <cfg/arch/gpio.h>
  42. #include <sys/nutdebug.h>
  43. #include <arch/cm3.h>
  44. #include <arch/cm3/nxp/lpc176x_gpio.h>
  45. #include <dev/gpio.h>
  46. #include <string.h>
  47. #include <stdlib.h>
  48. /*!
  49. * \addtogroup xgNutArchCm3Lpc176xGpio
  50. */
  51. /*@{*/
  52. #define NUTGPIOPORT_MAX NUTGPIO_PORT4+1
  53. /*!
  54. * \brief Get pin configuration.
  55. *
  56. * Trying to set undefined ports must be avoided.
  57. * If NUTDEBUG is enabled an assertion will be rised.
  58. *
  59. * \param bank GPIO bank/port number.
  60. * \param bit Bit number of the specified bank/port.
  61. *
  62. * \return Attribute flags of the pin.
  63. */
  64. uint32_t GpioPinConfigGet(int bank, int bit)
  65. {
  66. uint32_t rc = 0;
  67. int pin_offset;
  68. __IO uint32_t *PINSEL;
  69. __IO uint32_t *PINMODE;
  70. __IO uint32_t *PINMODE_OD;
  71. NUTASSERT(bank < NUTGPIOPORT_MAX);
  72. NUTASSERT(bit < 32);
  73. /*
  74. * PINSEL
  75. *
  76. * 00 GPIO
  77. * 01 Peripheral 1 AF
  78. * 10 Peripheral 2 AF
  79. * 11 Peripheral 3 AF
  80. *
  81. * PINMODE
  82. *
  83. * 00 Pullup enabled
  84. * 01 Repeater mode
  85. * 10 Neither pullup nor pulldown enabled
  86. * 11 Pulldown enabled
  87. */
  88. if (bit < 16) {
  89. PINSEL = (uint32_t *)&LPC_PINCON->PINSEL0 + bank * 2;
  90. PINMODE = (uint32_t *)&LPC_PINCON->PINMODE0 + bank * 2;
  91. pin_offset = bit * 2;
  92. } else {
  93. PINSEL = (uint32_t *)&LPC_PINCON->PINSEL0 + bank * 2 + 1;
  94. PINMODE = (uint32_t *)&LPC_PINCON->PINMODE0 + bank * 2 + 1;
  95. pin_offset = (bit - 16) * 2;
  96. }
  97. PINMODE_OD = (uint32_t *)&LPC_PINCON->PINMODE_OD0 + bank;
  98. if (CM3BBGET(GPIO_BANKID2BASE(bank), LPC_GPIO_TypeDef, FIODIR, bit)) {
  99. rc |= GPIO_CFG_OUTPUT;
  100. }
  101. switch ((*PINSEL >> pin_offset) & 0x03) {
  102. case PINCON_PINSEL_AF0:
  103. /* GPIO mode selected */
  104. rc |= GPIO_CFG_PERIPHERAL0;
  105. break;
  106. case PINCON_PINSEL_AF1:
  107. /* AF1 selected */
  108. rc |= GPIO_CFG_PERIPHERAL1 | GPIO_CFG_DISABLED;
  109. break;
  110. case PINCON_PINSEL_AF2:
  111. /* AF1 selected */
  112. rc |= GPIO_CFG_PERIPHERAL2 | GPIO_CFG_DISABLED;
  113. break;
  114. case PINCON_PINSEL_AF3:
  115. /* AF1 selected */
  116. rc |= GPIO_CFG_PERIPHERAL3 | GPIO_CFG_DISABLED;
  117. break;
  118. }
  119. switch ((*PINMODE >> pin_offset) & 0x03) {
  120. case PINCON_PINMODE_PULLUP:
  121. /* Pullup mode selected */
  122. rc |= GPIO_CFG_PULLUP;
  123. break;
  124. case PINCON_PINMODE_REPEATER:
  125. /* Repeater mode selected */
  126. rc |= GPIO_CFG_REPEATER;
  127. break;
  128. case PINCON_PINMODE_NORMAL:
  129. /* Neither pullup not pulldown */
  130. break;
  131. case PINCON_PINMODE_PULLDOWN:
  132. /* AF1 selected */
  133. rc |= GPIO_CFG_PULLDOWN;
  134. break;
  135. }
  136. if (*PINMODE_OD & bit) {
  137. rc |= GPIO_CFG_MULTIDRIVE;
  138. }
  139. return rc;
  140. }
  141. /*!
  142. * \brief Set port wide pin configuration.
  143. *
  144. * \note This function does not check for undefined ports and pins or
  145. * invalid attributes. If this is required, use GpioPinConfigSet().
  146. * If NUTDEBUG is enabled accessing an undefined port will rise
  147. * an assertion.
  148. *
  149. * \param bank GPIO bank/port number.
  150. * \param mask The given attributes are set for a specific pin, if the
  151. * corresponding bit in this mask is 1.
  152. * \param flags Attribute flags to set.
  153. *
  154. * \return Always 0.
  155. */
  156. int GpioPortConfigSet(int bank, uint32_t mask, uint32_t flags)
  157. {
  158. int i, j;
  159. uint32_t mode_l, mode_h;
  160. uint32_t sel_l, sel_h;
  161. __IO uint32_t *PINSEL_L;
  162. __IO uint32_t *PINSEL_H;
  163. __IO uint32_t *PINMODE_L;
  164. __IO uint32_t *PINMODE_H;
  165. __IO uint32_t *PINMODE_OD;
  166. NUTASSERT(bank < NUTGPIOPORT_MAX);
  167. /*
  168. * PINSEL
  169. *
  170. * 00 GPIO
  171. * 01 Peripheral 1 AF
  172. * 10 Peripheral 2 AF
  173. * 11 Peripheral 3 AF
  174. *
  175. * PINMODE
  176. *
  177. * 00 Pullup enabled
  178. * 01 Repeater mode
  179. * 10 Neither pullup nor pulldown enabled
  180. * 11 Pulldown enabled
  181. */
  182. PINSEL_L = (uint32_t *)&LPC_PINCON->PINSEL0 + bank * 2;
  183. PINSEL_H = PINSEL_L + 1;
  184. PINMODE_L = (uint32_t *)&LPC_PINCON->PINMODE0 + bank * 2;
  185. PINMODE_H = PINMODE_L + 1;
  186. PINMODE_OD = (uint32_t *)&LPC_PINCON->PINMODE_OD0 + bank;
  187. /* Read current settings */
  188. sel_l = *PINSEL_L;
  189. sel_h = *PINSEL_H;
  190. mode_l = *PINMODE_L;
  191. mode_h = *PINMODE_H;
  192. for (i = 0, j = 16; i < 16; i ++, j++) {
  193. if (mask & _BV(i)) {
  194. sel_l &= ~(PINCON_PINSEL_MASK << (i * 2));
  195. mode_l &= ~(PINCON_PINMODE_MASK << (i * 2));
  196. /* skip flag GPIO_CFG_PERIPHERAL0 ==> PINCON_PINSEL_AF0 */
  197. if ((flags & GPIO_CFG_PERIPHERAL_MASK) == GPIO_CFG_PERIPHERAL1) {
  198. sel_l |= PINCON_PINSEL_AF1 << (i * 2);
  199. } else
  200. if ((flags & GPIO_CFG_PERIPHERAL_MASK) == GPIO_CFG_PERIPHERAL2) {
  201. sel_l |= PINCON_PINSEL_AF2 << (i * 2);
  202. } else
  203. if ((flags & GPIO_CFG_PERIPHERAL_MASK) == GPIO_CFG_PERIPHERAL3) {
  204. sel_l |= PINCON_PINSEL_AF3 << (i * 2);
  205. }
  206. if (flags & GPIO_CFG_PULLUP) {
  207. mode_l |= PINCON_PINMODE_PULLUP << (i * 2);
  208. } else
  209. if (flags & GPIO_CFG_REPEATER) {
  210. mode_l |= PINCON_PINMODE_REPEATER << (i * 2);
  211. } else
  212. if (flags & GPIO_CFG_PULLDOWN) {
  213. mode_l |= PINCON_PINMODE_PULLDOWN << (i * 2);
  214. } else {
  215. mode_l |= PINCON_PINMODE_NORMAL << (i * 2);
  216. }
  217. }
  218. if (mask & _BV(j)) {
  219. sel_h &= ~(PINCON_PINSEL_MASK << (i * 2));
  220. mode_h &= ~(PINCON_PINMODE_MASK << (i * 2));
  221. /* skip flag GPIO_CFG_PERIPHERAL0 ==> PINCON_PINSEL_AF0 */
  222. if ((flags & GPIO_CFG_PERIPHERAL_MASK) == GPIO_CFG_PERIPHERAL1) {
  223. sel_h |= PINCON_PINSEL_AF1 << (i * 2);
  224. } else
  225. if ((flags & GPIO_CFG_PERIPHERAL_MASK) == GPIO_CFG_PERIPHERAL2) {
  226. sel_h |= PINCON_PINSEL_AF2 << (i * 2);
  227. } else
  228. if ((flags & GPIO_CFG_PERIPHERAL_MASK) == GPIO_CFG_PERIPHERAL3) {
  229. sel_h |= PINCON_PINSEL_AF3 << (i * 2);
  230. }
  231. if (flags & GPIO_CFG_PULLUP) {
  232. mode_h |= PINCON_PINMODE_PULLUP << (i * 2);
  233. } else
  234. if (flags & GPIO_CFG_REPEATER) {
  235. mode_h |= PINCON_PINMODE_REPEATER << (i * 2);
  236. } else
  237. if (flags & GPIO_CFG_PULLDOWN) {
  238. mode_h |= PINCON_PINMODE_PULLDOWN << (i * 2);
  239. } else {
  240. mode_h |= PINCON_PINMODE_NORMAL << (i * 2);
  241. }
  242. }
  243. }
  244. /* Write back the modified register values */
  245. *PINSEL_L = sel_l;
  246. *PINSEL_H = sel_h;
  247. *PINMODE_L = mode_l;
  248. *PINMODE_H = mode_h;
  249. if (flags & GPIO_CFG_OUTPUT) {
  250. CM3REG(GPIO_BANKID2BASE(bank), LPC_GPIO_TypeDef, FIODIR) |= mask;
  251. } else {
  252. CM3REG(GPIO_BANKID2BASE(bank), LPC_GPIO_TypeDef, FIODIR) &= ~mask;
  253. }
  254. if (flags & GPIO_CFG_MULTIDRIVE) {
  255. *PINMODE_OD |= mask;
  256. } else {
  257. *PINMODE_OD &= ~mask;
  258. }
  259. return 0;
  260. }
  261. /*!
  262. * \brief Set pin configuration.
  263. *
  264. * Applications may also use this function to make sure, that a specific
  265. * attribute is available for a specific pin.
  266. *
  267. * \note GPIO pins are typically initialized to a safe state after power
  268. * up. This routine is not able to determine the consequences of
  269. * changing pin configurations. In the worst case you may permanently
  270. * damage your hardware by bad pin settings.
  271. *
  272. * \param bank GPIO bank/port number.
  273. * \param bit Bit number of the specified bank/port.
  274. * \param flags Attribute flags.
  275. *
  276. * \return 0 if all attributes had been set, -1 otherwise.
  277. */
  278. int GpioPinConfigSet(int bank, int bit, uint32_t flags)
  279. {
  280. uint32_t mode;
  281. uint32_t sel;
  282. int pin_offset;
  283. __IO uint32_t *PINSEL_L;
  284. __IO uint32_t *PINSEL_H;
  285. __IO uint32_t *PINMODE_L;
  286. __IO uint32_t *PINMODE_H;
  287. __IO uint32_t *PINMODE_OD;
  288. NUTASSERT(bank < NUTGPIOPORT_MAX);
  289. NUTASSERT(bit < 32);
  290. /*
  291. * PINSEL
  292. *
  293. * 00 GPIO
  294. * 01 Peripheral 1 AF
  295. * 10 Peripheral 2 AF
  296. * 11 Peripheral 3 AF
  297. *
  298. * PINMODE
  299. *
  300. * 00 Pullup enabled
  301. * 01 Repeater mode
  302. * 10 Neither pullup nor pulldown enabled
  303. * 11 Pulldown enabled
  304. */
  305. PINSEL_L = (uint32_t *)&LPC_PINCON->PINSEL0 + bank * 2;
  306. PINSEL_H = PINSEL_L + 1;
  307. PINMODE_L = (uint32_t *)&LPC_PINCON->PINMODE0 + bank * 2;
  308. PINMODE_H = PINMODE_L + 1;
  309. PINMODE_OD = (uint32_t *)&LPC_PINCON->PINMODE_OD0 + bank;
  310. /* Read current settings */
  311. if (bit < 16) {
  312. sel = *PINSEL_L;
  313. mode = *PINMODE_L;
  314. pin_offset = bit * 2;
  315. } else {
  316. sel = *PINSEL_H;
  317. mode = *PINMODE_H;
  318. pin_offset = (bit - 16) * 2;
  319. }
  320. sel &= ~(PINCON_PINSEL_MASK << pin_offset);
  321. mode &= ~(PINCON_PINMODE_MASK << pin_offset);
  322. /* skip flag GPIO_CFG_PERIPHERAL0 */
  323. if ((flags & GPIO_CFG_PERIPHERAL_MASK) == GPIO_CFG_PERIPHERAL1) {
  324. sel |= PINCON_PINSEL_AF1 << pin_offset;
  325. } else
  326. if ((flags & GPIO_CFG_PERIPHERAL_MASK) == GPIO_CFG_PERIPHERAL2) {
  327. sel |= PINCON_PINSEL_AF2 << pin_offset;
  328. } else
  329. if ((flags & GPIO_CFG_PERIPHERAL_MASK) == GPIO_CFG_PERIPHERAL3) {
  330. sel |= PINCON_PINSEL_AF3 << pin_offset;
  331. }
  332. if (flags & GPIO_CFG_PULLUP) {
  333. mode |= PINCON_PINMODE_PULLUP << pin_offset;
  334. } else
  335. if (flags & GPIO_CFG_REPEATER) {
  336. mode |= PINCON_PINMODE_REPEATER << pin_offset;
  337. } else
  338. if (flags & GPIO_CFG_PULLDOWN) {
  339. mode |= PINCON_PINMODE_PULLDOWN << pin_offset;
  340. } else {
  341. mode |= PINCON_PINMODE_NORMAL << pin_offset;
  342. }
  343. /* Write back the modified register values */
  344. if (bit < 16) {
  345. *PINSEL_L = sel;
  346. *PINMODE_L = mode;
  347. } else {
  348. *PINSEL_H = sel;
  349. *PINMODE_H = mode;
  350. }
  351. if (flags & GPIO_CFG_OUTPUT) {
  352. CM3BBSET(GPIO_BANKID2BASE(bank), LPC_GPIO_TypeDef, FIODIR, bit);
  353. } else {
  354. CM3BBCLR(GPIO_BANKID2BASE(bank), LPC_GPIO_TypeDef, FIODIR, bit);
  355. }
  356. if (flags & GPIO_CFG_MULTIDRIVE) {
  357. *PINMODE_OD |= _BV(bit);
  358. } else {
  359. *PINMODE_OD &= ~_BV(bit);
  360. }
  361. return 0;
  362. }
  363. /*!
  364. * \brief Register a GPIO pin interrupt handler.
  365. *
  366. * Generating interrupts on GPIO pin changes is not supported on all
  367. * platforms. In this case dedicated external interrupt pins may
  368. * be used with NutRegisterIrqHandler().
  369. *
  370. * On the LPC17xx interrupts are triggered on rising, falling or both
  371. * edges. Level triggering is not supported.
  372. *
  373. * After registering, interrupts are disabled. Calling GpioIrqEnable()
  374. * is required to activate the interrupt.
  375. *
  376. * The following code fragment registers an interrupt handler which is
  377. * called on a rising edge of bit 4 of the first GPIO port:
  378. * \code
  379. * #include <dev/gpio.h>
  380. *
  381. * static void PinChange(void *arg)
  382. * {
  383. * ...
  384. * }
  385. *
  386. * {
  387. * ...
  388. * GpioPinConfigSet(0, 4, GPIO_CFG_PULLUP);
  389. * GpioRegisterIrqHandler(&sig_GPIO0, 4, PinChange, NULL);
  390. * GpioIrqSetMode(&sig_GPIO0, 4, NUT_IRQMODE_RISINGEDGE);
  391. * GpioIrqEnable(&sig_GPIO0, 4);
  392. * ...
  393. * }
  394. * \endcode
  395. *
  396. * \param sig Bank/port interrupt to be associated with this handler.
  397. * \param bit Bit number of the specified bank/port.
  398. * \param handler This routine will be called by Nut/OS, when the specified
  399. * pin changes its state.
  400. * \param arg Argument to be passed to the interrupt handler routine.
  401. *
  402. * \return 0 on success, -1 otherwise.
  403. */
  404. int GpioRegisterIrqHandler(GPIO_SIGNAL * sig, int bit, void (*handler) (void *), void *arg)
  405. {
  406. int rc = 0;
  407. if (sig->ios_vector == 0) {
  408. /* This is the first call. Allocate the vector table. */
  409. sig->ios_vector = malloc(sizeof(GPIO_VECTOR) * 32);
  410. if (sig->ios_vector) {
  411. memset(sig->ios_vector, 0, sizeof(GPIO_VECTOR) * 32);
  412. /* Register our internal PIO interrupt service. */
  413. if (sig_PIO.ir_handler == NULL) {
  414. rc = NutRegisterIrqHandler(&sig_PIO, sig->ios_handler, NULL);
  415. if (rc == 0) {
  416. /* Clear any pending interrupts */
  417. LPC_GPIOINT->IO0IntClr = 0xFFFFFFFF;
  418. LPC_GPIOINT->IO2IntClr = 0xFFFFFFFF;
  419. rc = NutIrqEnable(&sig_PIO);
  420. }
  421. }
  422. }
  423. else {
  424. return -1;
  425. }
  426. }
  427. sig->ios_vector[bit].iov_handler = handler;
  428. sig->ios_vector[bit].iov_arg = arg;
  429. return rc;
  430. }
  431. /*!
  432. * \brief Enable a specified GPIO interrupt.
  433. *
  434. * A related interrupt handler must have been registered before calling
  435. * this function. See GpioRegisterIrqHandler().
  436. *
  437. * \param sig Interrupt to enable.
  438. * \param bit Bit number of the specified bank/port.
  439. *
  440. * \return 0 on success, -1 otherwise.
  441. */
  442. int GpioIrqEnable(GPIO_SIGNAL * sig, int bit)
  443. {
  444. return (sig->ios_ctl) (sig, NUT_IRQCTL_ENABLE, NULL, bit);
  445. }
  446. /*!
  447. * \brief Disable a specified GPIO interrupt.
  448. *
  449. * \param sig Interrupt to disable.
  450. * \param bit Bit number of the specified bank/port.
  451. *
  452. * \return 0 on success, -1 otherwise.
  453. */
  454. int GpioIrqDisable(GPIO_SIGNAL * sig, int bit)
  455. {
  456. return (sig->ios_ctl) (sig, NUT_IRQCTL_DISABLE, NULL, bit);
  457. }
  458. /*!
  459. * \brief Query the status of a specified GPIO interrupt.
  460. *
  461. * A related interrupt handler must have been registered before calling
  462. * this function. See GpioRegisterIrqHandler().
  463. *
  464. * \param sig Interrupt to query
  465. * \param bit Bit number of the specified bank/port.
  466. *
  467. * \return 0 if interrupt is disabled, 1 of enabled
  468. */
  469. int GpioIrqStatus(GPIO_SIGNAL * sig, int bit)
  470. {
  471. uint32_t status;
  472. (sig->ios_ctl) (sig, NUT_IRQCTL_STATUS, &status, bit);
  473. return status;
  474. }
  475. /*!
  476. * \brief Set the GPIO interrupt mode for a pin
  477. *
  478. * \param sig Interrupt to configure.
  479. * \param bit Bit number of the specified bank/port.
  480. * \param mode one of the following modes:
  481. * NUT_IRQMODE_RISINGEDGE,
  482. * NUT_IRQMODE_FALLINGEDGE,
  483. * NUT_IRQMODE_BOTHEDGE,
  484. * NUT_IRQMODE_NONE,
  485. *
  486. * \return 0 on success, -1 otherwise.
  487. */
  488. int GpioIrqSetMode(GPIO_SIGNAL * sig, int bit, int mode)
  489. {
  490. return (sig->ios_ctl) (sig, NUT_IRQCTL_SETMODE, &mode, bit);
  491. }
  492. /*@}*/