PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/boards/arm/stm32/olimex-stm32-h407/src/stm32_buttons.c

https://bitbucket.org/zapparello/nuttx
C | 151 lines | 42 code | 24 blank | 85 comment | 4 complexity | 7b2b127cd17235399c27a5233ba385bf MD5 | raw file
Possible License(s): Apache-2.0
  1. /****************************************************************************
  2. * boards/arm/stm32/olimex-stm32-h407/src/stm32_buttons.c
  3. *
  4. * Copyright (C) 2014-2015, 2017 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 <errno.h>
  41. #include <nuttx/arch.h>
  42. #include <nuttx/board.h>
  43. #include <arch/board/board.h>
  44. #include "olimex-stm32-h407.h"
  45. #ifdef CONFIG_ARCH_BUTTONS
  46. /****************************************************************************
  47. * Private Data
  48. ****************************************************************************/
  49. /* Pin configuration for each Olimex-STM32-H405 button. This array is indexed by
  50. * the BUTTON_* definitions in board.h
  51. */
  52. static const uint32_t g_buttons[NUM_BUTTONS] =
  53. {
  54. GPIO_BTN_BUT
  55. };
  56. /****************************************************************************
  57. * Public Functions
  58. ****************************************************************************/
  59. /****************************************************************************
  60. * Name: board_button_initialize
  61. *
  62. * Description:
  63. * board_button_initialize() must be called to initialize button resources.
  64. * After that, board_buttons() may be called to collect the current state
  65. * of all buttons or board_button_irq() may be called to register button
  66. * interrupt handlers.
  67. *
  68. ****************************************************************************/
  69. void board_button_initialize(void)
  70. {
  71. int i;
  72. /* Configure the GPIO pins as inputs. NOTE that EXTI interrupts are
  73. * configured for all pins.
  74. */
  75. for (i = 0; i < NUM_BUTTONS; i++)
  76. {
  77. stm32_configgpio(g_buttons[i]);
  78. }
  79. }
  80. /****************************************************************************
  81. * Name: board_buttons
  82. ****************************************************************************/
  83. uint32_t board_buttons(void)
  84. {
  85. uint32_t ret = 0;
  86. /* Check that state of each key */
  87. if (!stm32_gpioread(g_buttons[BUTTON_BUT]))
  88. {
  89. ret |= BUTTON_BUT_BIT;
  90. }
  91. return ret;
  92. }
  93. /****************************************************************************
  94. * Button support.
  95. *
  96. * Description:
  97. * board_button_initialize() must be called to initialize button resources.
  98. * After that, board_buttons() may be called to collect the current state
  99. * of all buttons or board_button_irq() may be called to register button
  100. * interrupt handlers.
  101. *
  102. * After board_button_initialize() has been called, board_buttons() may be
  103. * called to collect the state of all buttons. board_buttons() returns an
  104. * 32-bit bit set with each bit associated with a button. See the BUTTON_*_BIT
  105. * definitions in board.h for the meaning of each bit.
  106. *
  107. * board_button_irq() may be called to register an interrupt handler that
  108. * will be called when a button is depressed or released. The ID value is
  109. * a button enumeration value that uniquely identifies a button resource.
  110. * See the BUTTON_* definitions in board.h for the meaning of enumeration
  111. * value.
  112. *
  113. ****************************************************************************/
  114. #ifdef CONFIG_ARCH_IRQBUTTONS
  115. int board_button_irq(int id, xcpt_t irqhandler, FAR void *arg)
  116. {
  117. int ret = -EINVAL;
  118. /* The following should be atomic */
  119. if (id >= MIN_IRQBUTTON && id <= MAX_IRQBUTTON)
  120. {
  121. ret = stm32_gpiosetevent(g_buttons[id], true, true, true, irqhandler,
  122. arg);
  123. }
  124. return ret;
  125. }
  126. #endif
  127. #endif /* CONFIG_ARCH_BUTTONS */