main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * Copyright (c) 2014 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /**@file
  41. * @defgroup nrf_dev_simple_timer_example_main.c
  42. * @{
  43. * @ingroup nrf_dev_simple_timer_example
  44. * @brief Timer example application main file.
  45. *
  46. * This file contains the source code for a sample application using timer library.
  47. * For a more detailed description of the functionality, see the SDK documentation.
  48. */
  49. #include "app_simple_timer.h"
  50. #include <stdio.h>
  51. #include "boards.h"
  52. #include "app_error.h"
  53. #include "nrf_delay.h"
  54. #define TIMEOUT_VALUE 50000 /**< 50 mseconds timer time-out value. */
  55. #define TOGGLE_LED_COUNTER (500 / (TIMEOUT_VALUE / 1000)) /**< Interval for toggling a LED. Yields to 500 mseconds. */
  56. #define STATE_TRANSIT_COUNTER_INIT_VALUE (4 * TOGGLE_LED_COUNTER) /**< Initial value for the state transition counter. */
  57. #define GENERIC_DELAY_TIME 1000 /**< Generic delay time used by application. */
  58. /**@brief Application states. */
  59. typedef enum
  60. {
  61. APP_STATE_SINGLE_SHOT, /**< Application state where single shot timer mode is tested. */
  62. APP_STATE_REPEATED /**< Application state where repeated timer mode is tested. */
  63. } state_t;
  64. static volatile uint32_t m_state_transit_counter = 0; /**< State transition counter variable. */
  65. static volatile uint32_t m_toggle_led_counter = 0; /**< Led toggling counter variable. */
  66. static volatile state_t m_state; /**< Current application state. */
  67. void timeout_handler(void * p_context);
  68. void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
  69. {
  70. bsp_board_leds_off();
  71. for (;;)
  72. {
  73. nrf_delay_ms(GENERIC_DELAY_TIME);
  74. bsp_board_led_invert(BSP_BOARD_LED_0);
  75. bsp_board_led_invert(BSP_BOARD_LED_1);
  76. }
  77. }
  78. /**@brief Function for toggling a LED and starting a timer.
  79. *
  80. * @param[in] led_id ID of the LED to toggle.
  81. * @param[in] timer_mode Timer mode @ref timer_mode_t.
  82. */
  83. static void led_and_timer_control(uint32_t led_id, app_simple_timer_mode_t timer_mode)
  84. {
  85. uint32_t err_code;
  86. bsp_board_led_invert(led_id);
  87. m_state_transit_counter = STATE_TRANSIT_COUNTER_INIT_VALUE;
  88. m_toggle_led_counter = TOGGLE_LED_COUNTER;
  89. err_code = app_simple_timer_start(timer_mode, timeout_handler, TIMEOUT_VALUE, NULL);
  90. APP_ERROR_CHECK(err_code);
  91. }
  92. /**@brief Function for executing the state entry action.
  93. */
  94. static __INLINE void state_entry_action_execute(void)
  95. {
  96. switch (m_state)
  97. {
  98. case APP_STATE_SINGLE_SHOT:
  99. led_and_timer_control(BSP_BOARD_LED_0, APP_SIMPLE_TIMER_MODE_SINGLE_SHOT);
  100. break;
  101. case APP_STATE_REPEATED:
  102. led_and_timer_control(BSP_BOARD_LED_1, APP_SIMPLE_TIMER_MODE_REPEATED);
  103. break;
  104. default:
  105. APP_ERROR_HANDLER(m_state);
  106. break;
  107. }
  108. }
  109. /**@brief Function for changing the state of the state machine.
  110. *
  111. * @param[in] new_state State to which the state machine transitions.
  112. */
  113. static void state_machine_state_change(state_t new_state)
  114. {
  115. m_state = new_state;
  116. state_entry_action_execute();
  117. }
  118. void timeout_handler(void * p_context)
  119. {
  120. switch (m_state)
  121. {
  122. uint32_t err_code;
  123. case APP_STATE_SINGLE_SHOT:
  124. if (--m_state_transit_counter != 0)
  125. {
  126. if (--m_toggle_led_counter == 0)
  127. {
  128. m_toggle_led_counter = TOGGLE_LED_COUNTER;
  129. bsp_board_led_invert(BSP_BOARD_LED_0);
  130. }
  131. err_code = app_simple_timer_start(APP_SIMPLE_TIMER_MODE_SINGLE_SHOT,
  132. timeout_handler,
  133. TIMEOUT_VALUE,
  134. NULL);
  135. APP_ERROR_CHECK(err_code);
  136. }
  137. else
  138. {
  139. state_machine_state_change(APP_STATE_REPEATED);
  140. }
  141. break;
  142. case APP_STATE_REPEATED:
  143. if (--m_state_transit_counter != 0)
  144. {
  145. if (--m_toggle_led_counter == 0)
  146. {
  147. m_toggle_led_counter = TOGGLE_LED_COUNTER;
  148. bsp_board_led_invert(BSP_BOARD_LED_1);
  149. }
  150. }
  151. else
  152. {
  153. bsp_board_led_on(BSP_BOARD_LED_0);
  154. bsp_board_led_on(BSP_BOARD_LED_1);
  155. err_code = app_simple_timer_stop();
  156. APP_ERROR_CHECK(err_code);
  157. nrf_delay_ms(GENERIC_DELAY_TIME);
  158. state_machine_state_change(APP_STATE_SINGLE_SHOT);
  159. }
  160. break;
  161. default:
  162. APP_ERROR_HANDLER(m_state);
  163. break;
  164. }
  165. }
  166. /**@brief Function for the Power Management.
  167. */
  168. static void power_manage(void)
  169. {
  170. // Use directly __WFE and __SEV macros since the SoftDevice is not available.
  171. // Wait for event.
  172. __WFE();
  173. // Clear Event Register.
  174. __SEV();
  175. __WFE();
  176. }
  177. int main(void)
  178. {
  179. uint32_t err_code = app_simple_timer_init();
  180. APP_ERROR_CHECK(err_code);
  181. bsp_board_init(BSP_INIT_LEDS);
  182. bsp_board_led_on(BSP_BOARD_LED_0);
  183. bsp_board_led_on(BSP_BOARD_LED_1);
  184. nrf_delay_ms(GENERIC_DELAY_TIME);
  185. state_machine_state_change(APP_STATE_SINGLE_SHOT);
  186. for (;;)
  187. {
  188. power_manage();
  189. }
  190. }