PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/arm/src/stm32l4/stm32l4_exti_wakeup.c

https://bitbucket.org/hg42/nuttx
C | 153 lines | 51 code | 26 blank | 76 comment | 3 complexity | db34cf3f96b9d3f0b120c4d7e3094b80 MD5 | raw file
Possible License(s): 0BSD
  1. /****************************************************************************
  2. * arch/arm/src/stm32l4/stm32l4_exti_wakeup.c
  3. *
  4. * Copyright (C) 2009, 2012, 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. * Juha Niskanen <juha.niskanen@haltian.com>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <nuttx/irq.h>
  41. #include <nuttx/arch.h>
  42. #include <stdint.h>
  43. #include <stdbool.h>
  44. #include <errno.h>
  45. #include <arch/irq.h>
  46. #include "up_arch.h"
  47. #include "chip.h"
  48. #include "stm32l4_gpio.h"
  49. #include "stm32l4_exti.h"
  50. /****************************************************************************
  51. * Private Data
  52. ****************************************************************************/
  53. /* Interrupt handlers attached to the RTC WAKEUP EXTI */
  54. static xcpt_t g_wakeup_callback;
  55. static void *g_callback_arg;
  56. /****************************************************************************
  57. * Private Functions
  58. ****************************************************************************/
  59. /****************************************************************************
  60. * Name: stm32l4_exti_wakeup_isr
  61. *
  62. * Description:
  63. * EXTI periodic WAKEUP interrupt service routine/dispatcher
  64. *
  65. ****************************************************************************/
  66. static int stm32l4_exti_wakeup_isr(int irq, void *context, FAR void *arg)
  67. {
  68. int ret = OK;
  69. /* Dispatch the interrupt to the handler */
  70. if (g_wakeup_callback != NULL)
  71. {
  72. ret = g_wakeup_callback(irq, context, g_callback_arg);
  73. }
  74. /* Clear the pending EXTI interrupt */
  75. putreg32(EXTI1_RTC_WAKEUP, STM32L4_EXTI1_PR);
  76. return ret;
  77. }
  78. /****************************************************************************
  79. * Public Functions
  80. ****************************************************************************/
  81. /****************************************************************************
  82. * Name: stm32l4_exti_wakeup
  83. *
  84. * Description:
  85. * Sets/clears EXTI wakeup interrupt.
  86. *
  87. * Parameters:
  88. * - rising/falling edge: enables interrupt on rising/falling edges
  89. * - event: generate event when set
  90. * - func: when non-NULL, generate interrupt
  91. *
  92. * Returned Value:
  93. * Zero (OK) on success; a negated errno value on failure indicating the
  94. * nature of the failure.
  95. *
  96. ****************************************************************************/
  97. int stm32l4_exti_wakeup(bool risingedge, bool fallingedge, bool event,
  98. xcpt_t func, void *arg)
  99. {
  100. g_wakeup_callback = func;
  101. g_callback_arg = arg;
  102. /* Install external interrupt handlers (if not already attached) */
  103. if (func)
  104. {
  105. irq_attach(STM32L4_IRQ_RTC_WKUP, stm32l4_exti_wakeup_isr, NULL);
  106. up_enable_irq(STM32L4_IRQ_RTC_WKUP);
  107. }
  108. else
  109. {
  110. up_disable_irq(STM32L4_IRQ_RTC_WKUP);
  111. }
  112. /* Configure rising/falling edges */
  113. modifyreg32(STM32L4_EXTI1_RTSR,
  114. risingedge ? 0 : EXTI1_RTC_WAKEUP,
  115. risingedge ? EXTI1_RTC_WAKEUP : 0);
  116. modifyreg32(STM32L4_EXTI1_FTSR,
  117. fallingedge ? 0 : EXTI1_RTC_WAKEUP,
  118. fallingedge ? EXTI1_RTC_WAKEUP : 0);
  119. /* Enable Events and Interrupts */
  120. modifyreg32(STM32L4_EXTI1_EMR,
  121. event ? 0 : EXTI1_RTC_WAKEUP,
  122. event ? EXTI1_RTC_WAKEUP : 0);
  123. modifyreg32(STM32L4_EXTI1_IMR,
  124. func ? 0 : EXTI1_RTC_WAKEUP,
  125. func ? EXTI1_RTC_WAKEUP : 0);
  126. return OK;
  127. }