led_softblink.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**
  2. * Copyright (c) 2015 - 2018, 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. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(LED_SOFTBLINK)
  42. #include <string.h>
  43. #include "led_softblink.h"
  44. #include "nrf_gpio.h"
  45. #include "app_timer.h"
  46. #include "nrf_assert.h"
  47. #include "low_power_pwm.h"
  48. /* Period for LED softblink PWM. */
  49. #define PWM_PERIOD UINT8_MAX
  50. /**@bref Structure to handle timer time-outs
  51. *
  52. */
  53. typedef struct
  54. {
  55. bool leds_is_on; /**< Flag for indicating if LEDs are on. */
  56. bool is_counting_up; /**< Flag for indicating if counter is incrementing or decrementing. */
  57. nrfx_drv_state_t led_sb_state; /**< Indicates current state of instance. */
  58. uint16_t duty_cycle; /**< Current pulse width. */
  59. uint32_t bit_mask; /**< Mask of used pins. */
  60. led_sb_init_params_t params; /**< Structure holding initialization parameters. */
  61. low_power_pwm_config_t pwm_config; /**< Structure holding parameters for initializing low level layer. */
  62. low_power_pwm_t pwm_instance; /**< Structure holding low-power PWM instance parameters. */
  63. }led_sb_context_t;
  64. APP_TIMER_DEF(m_led_softblink_timer);
  65. static led_sb_context_t m_led_sb = {0};
  66. /**@brief Timer event handler for softblink.
  67. *
  68. * @param[in] p_context General purpose pointer. Will be passed to the time-out handler
  69. * when the timer expires.
  70. *
  71. */
  72. static void led_softblink_on_timeout(void * p_context)
  73. {
  74. static int32_t pause_ticks;
  75. ASSERT(m_led_sb.led_sb_state != NRFX_DRV_STATE_UNINITIALIZED);
  76. ret_code_t err_code;
  77. if (pause_ticks <= 0)
  78. {
  79. if (m_led_sb.is_counting_up)
  80. {
  81. if (m_led_sb.duty_cycle >= (m_led_sb.params.duty_cycle_max - m_led_sb.params.duty_cycle_step))
  82. {
  83. // Max PWM duty cycle is reached, start decrementing.
  84. m_led_sb.is_counting_up = false;
  85. m_led_sb.duty_cycle = m_led_sb.params.duty_cycle_max;
  86. pause_ticks = m_led_sb.params.on_time_ticks ? m_led_sb.params.on_time_ticks + APP_TIMER_MIN_TIMEOUT_TICKS : 0;
  87. }
  88. else
  89. {
  90. m_led_sb.duty_cycle += m_led_sb.params.duty_cycle_step;
  91. }
  92. }
  93. else
  94. {
  95. if (m_led_sb.duty_cycle <= (m_led_sb.params.duty_cycle_min + m_led_sb.params.duty_cycle_step))
  96. {
  97. // Min PWM duty cycle is reached, start incrementing.
  98. m_led_sb.is_counting_up = true;
  99. m_led_sb.duty_cycle = m_led_sb.params.duty_cycle_min;
  100. pause_ticks = m_led_sb.params.off_time_ticks ? m_led_sb.params.off_time_ticks + APP_TIMER_MIN_TIMEOUT_TICKS : 0;
  101. }
  102. else
  103. {
  104. m_led_sb.duty_cycle -= m_led_sb.params.duty_cycle_step;
  105. }
  106. }
  107. }
  108. else
  109. {
  110. pause_ticks -= PWM_PERIOD;
  111. }
  112. err_code = low_power_pwm_duty_set(&m_led_sb.pwm_instance, m_led_sb.duty_cycle);
  113. APP_ERROR_CHECK(err_code);
  114. }
  115. ret_code_t led_softblink_init(led_sb_init_params_t const * p_init_params)
  116. {
  117. ret_code_t err_code;
  118. ASSERT(m_led_sb.led_sb_state == NRFX_DRV_STATE_UNINITIALIZED);
  119. ASSERT(p_init_params);
  120. if ( (p_init_params->duty_cycle_max == 0) ||
  121. (p_init_params->duty_cycle_max <= p_init_params->duty_cycle_min) ||
  122. (p_init_params->duty_cycle_step == 0) ||
  123. (p_init_params->leds_pin_bm == 0))
  124. {
  125. return NRF_ERROR_INVALID_PARAM;
  126. }
  127. memset(&m_led_sb, 0, sizeof(led_sb_context_t));
  128. memcpy(&m_led_sb.params, p_init_params, sizeof(led_sb_init_params_t));
  129. m_led_sb.is_counting_up = true;
  130. m_led_sb.duty_cycle = p_init_params->duty_cycle_min + p_init_params->duty_cycle_step;
  131. m_led_sb.leds_is_on = false;
  132. m_led_sb.bit_mask = p_init_params->leds_pin_bm;
  133. m_led_sb.pwm_config.active_high = m_led_sb.params.active_high;
  134. m_led_sb.pwm_config.bit_mask = p_init_params->leds_pin_bm;
  135. m_led_sb.pwm_config.p_port = p_init_params->p_leds_port;
  136. m_led_sb.pwm_config.period = PWM_PERIOD;
  137. m_led_sb.pwm_config.p_timer_id = &m_led_softblink_timer;
  138. err_code = low_power_pwm_init( &m_led_sb.pwm_instance, &m_led_sb.pwm_config, led_softblink_on_timeout);
  139. if (err_code == NRF_SUCCESS)
  140. {
  141. m_led_sb.led_sb_state = NRFX_DRV_STATE_INITIALIZED;
  142. }
  143. else
  144. {
  145. return err_code;
  146. }
  147. err_code = low_power_pwm_duty_set( &m_led_sb.pwm_instance, p_init_params->duty_cycle_min + p_init_params->duty_cycle_step);
  148. return err_code;
  149. }
  150. ret_code_t led_softblink_start(uint32_t leds_pin_bit_mask)
  151. {
  152. ret_code_t err_code;
  153. ASSERT(m_led_sb.led_sb_state == NRFX_DRV_STATE_INITIALIZED);
  154. err_code = low_power_pwm_start(&m_led_sb.pwm_instance, leds_pin_bit_mask);
  155. return err_code;
  156. }
  157. ret_code_t led_softblink_stop(void)
  158. {
  159. ret_code_t err_code;
  160. err_code = low_power_pwm_stop(&m_led_sb.pwm_instance);
  161. return err_code;
  162. }
  163. void led_softblink_off_time_set(uint32_t off_time_ticks)
  164. {
  165. ASSERT(m_led_sb.led_sb_state != NRFX_DRV_STATE_UNINITIALIZED);
  166. m_led_sb.params.off_time_ticks = off_time_ticks;
  167. }
  168. void led_softblink_on_time_set(uint32_t on_time_ticks)
  169. {
  170. ASSERT(m_led_sb.led_sb_state != NRFX_DRV_STATE_UNINITIALIZED);
  171. m_led_sb.params.on_time_ticks = on_time_ticks;
  172. }
  173. ret_code_t led_softblink_uninit(void)
  174. {
  175. ASSERT(m_led_sb.led_sb_state != NRFX_DRV_STATE_UNINITIALIZED);
  176. ret_code_t err_code;
  177. err_code = led_softblink_stop();
  178. if (err_code == NRF_SUCCESS)
  179. {
  180. m_led_sb.led_sb_state = NRFX_DRV_STATE_UNINITIALIZED;
  181. }
  182. else
  183. {
  184. return err_code;
  185. }
  186. memset(&m_led_sb, 0, sizeof(m_led_sb));
  187. return NRF_SUCCESS;
  188. }
  189. #endif //NRF_MODULE_ENABLED(LED_SOFTBLINK)