PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/boards/arm/tiva/tm4c1294-launchpad/src/tm4c_buttons.c

https://bitbucket.org/zapparello/nuttx
C | 171 lines | 60 code | 32 blank | 79 comment | 7 complexity | 509115d1d788986c6dda792f223107c5 MD5 | raw file
Possible License(s): Apache-2.0
  1. /****************************************************************************
  2. * boards/arm/tiva/tm4c1294-launchpad/src/tm4c_buttons.c
  3. *
  4. * Copyright (C) 2015, 2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <stdint.h>
  40. #include <nuttx/arch.h>
  41. #include <nuttx/board.h>
  42. #include <arch/irq.h>
  43. #include <arch/board/board.h>
  44. #include "tm4c1294-launchpad.h"
  45. #ifdef CONFIG_ARCH_BUTTONS
  46. /****************************************************************************
  47. * Pre-processor Definitions
  48. ****************************************************************************/
  49. /****************************************************************************
  50. * Private Data
  51. ****************************************************************************/
  52. /* Pin configuration for each STM3210E-EVAL button. This array is indexed by
  53. * the BUTTON_* and JOYSTICK_* definitions in board.h
  54. */
  55. static const uint32_t g_buttons[NUM_BUTTONS] =
  56. {
  57. GPIO_SW1, GPIO_SW2
  58. };
  59. /****************************************************************************
  60. * Private Functions
  61. ****************************************************************************/
  62. /****************************************************************************
  63. * Public Functions
  64. ****************************************************************************/
  65. /****************************************************************************
  66. * Name: board_button_initialize
  67. *
  68. * Description:
  69. * board_button_initialize() must be called to initialize button resources.
  70. * After that, board_buttons() may be called to collect the current state
  71. * of all buttons or board_button_irq() may be called to register button
  72. * interrupt handlers.
  73. *
  74. ****************************************************************************/
  75. void board_button_initialize(void)
  76. {
  77. int i;
  78. /* Configure the GPIO pins as inputs. */
  79. for (i = 0; i < NUM_BUTTONS; i++)
  80. {
  81. tiva_configgpio(g_buttons[i]);
  82. }
  83. #ifdef CONFIG_ARCH_IRQBUTTONS
  84. /* Configure GPIO interrupts */
  85. tiva_gpioirqinitialize();
  86. #endif
  87. }
  88. /****************************************************************************
  89. * Name: board_buttons
  90. ****************************************************************************/
  91. uint32_t board_buttons(void)
  92. {
  93. uint32_t ret = 0;
  94. int i;
  95. /* Check that state of each key */
  96. for (i = 0; i < NUM_BUTTONS; i++)
  97. {
  98. /* A LOW value means that the key is pressed. */
  99. bool released = tiva_gpioread(g_buttons[i]);
  100. /* Accumulate the set of depressed (not released) keys */
  101. if (!released)
  102. {
  103. ret |= (1 << i);
  104. }
  105. }
  106. return ret;
  107. }
  108. /****************************************************************************
  109. * Name: board_button_irq
  110. *
  111. * Description:
  112. * This function may be called to register an interrupt handler that will
  113. * be called when a button is depressed or released.
  114. *
  115. ****************************************************************************/
  116. #ifdef CONFIG_ARCH_IRQBUTTONS
  117. int board_button_irq(int id, xcpt_t irqhandler, FAR void *arg)
  118. {
  119. int ret;
  120. if (id < 0 || id >= NUM_BUTTONS)
  121. {
  122. ret = -EINVAL;
  123. }
  124. else
  125. {
  126. /* Are we attaching or detaching? */
  127. if (irqhandler != NULL)
  128. {
  129. ret = tiva_gpioirqattach(g_buttons[id], irqhandler, arg);
  130. }
  131. else
  132. {
  133. ret = tiva_gpioirqdetach(g_buttons[id]);
  134. }
  135. }
  136. return ret;
  137. }
  138. #endif
  139. #endif /* CONFIG_ARCH_BUTTONS */