low_power_pwm.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * Copyright (c) 2015 - 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. *
  42. * @defgroup low_power_pwm Low-power PWM
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief Module for generating a low-power pulse-width modulated output signal.
  47. *
  48. * This module provides a low-power PWM implementation using app_timers and GPIO.
  49. *
  50. * Each low-power PWM instance utilizes one app_timer. This means it runs on RTC
  51. * and does not require HFCLK to be running. There can be any number of output
  52. * channels per instance.
  53. */
  54. #ifndef LOW_POWER_PWM_H__
  55. #define LOW_POWER_PWM_H__
  56. #include <nrfx.h>
  57. #include "app_timer.h"
  58. #include "sdk_errors.h"
  59. #ifdef __cplusplus
  60. extern "C" {
  61. #endif
  62. /**
  63. * @brief Event types.
  64. */
  65. typedef enum
  66. {
  67. LOW_POWER_PWM_EVENT_PERIOD = 0,
  68. LOW_POWER_PWM_EVENT_DUTY_CYCLE
  69. }low_power_pwm_evt_type_t;
  70. /**@brief Application time-out handler type. */
  71. typedef void (*low_power_pwm_timeout_user)(void * p_context, low_power_pwm_evt_type_t evt_type);
  72. /**
  73. * @brief Structure holding the initialization parameters.
  74. */
  75. typedef struct
  76. {
  77. bool active_high; /**< Activate negative polarity. */
  78. uint8_t period; /**< Width of the low_power_pwm period. */
  79. NRF_GPIO_Type * p_port; /**< Port used to work on selected mask. */
  80. uint32_t bit_mask; /**< Pins to be initialized. */
  81. app_timer_id_t const * p_timer_id; /**< Pointer to the timer ID of low_power_pwm. */
  82. } low_power_pwm_config_t;
  83. /**
  84. * @name Default settings
  85. * @{
  86. *
  87. * @brief Default parameters for the @ref low_power_pwm_config_t structure.
  88. *
  89. */
  90. #define LOW_POWER_PWM_CONFIG_ACTIVE_HIGH false
  91. #define LOW_POWER_PWM_CONFIG_PERIOD UINT8_MAX
  92. #define LOW_POWER_PWM_CONFIG_PORT NRF_GPIO
  93. #define LOW_POWER_PWM_CONFIG_BIT_MASK(mask) (mask)
  94. /** @} */
  95. /**
  96. * @brief Low-power PWM default configuration.
  97. */
  98. #define LOW_POWER_PWM_DEFAULT_CONFIG(mask) \
  99. { \
  100. .active_high = LOW_POWER_PWM_CONFIG_ACTIVE_HIGH , \
  101. .period = LOW_POWER_PWM_CONFIG_PERIOD , \
  102. .p_port = LOW_POWER_PWM_CONFIG_PORT, \
  103. .bit_mask = LOW_POWER_PWM_CONFIG_BIT_MASK(mask) \
  104. }
  105. /**
  106. * @cond (NODOX)
  107. * @defgroup low_power_pwm_internal Auxiliary internal types declarations
  108. * @brief Module for internal usage inside the library only.
  109. * @details These definitions are available to the user, but they should not
  110. * be accessed directly. Use @ref low_power_pwm_duty_set instead.
  111. * @{
  112. *
  113. */
  114. /**
  115. * @brief Structure holding parameters of a given low-power PWM instance.
  116. */
  117. struct low_power_pwm_s
  118. {
  119. bool active_high; /**< Activate negative polarity. */
  120. bool pin_is_on; /**< Indicates the current state of the pin. */
  121. uint8_t period; /**< Width of the low_power_pwm period. */
  122. uint8_t duty_cycle; /**< Width of high pulse. */
  123. nrfx_drv_state_t pwm_state; /**< Indicates the current state of the PWM instance. */
  124. uint32_t bit_mask; /**< Pins to be initialized. */
  125. uint32_t bit_mask_toggle; /**< Pins to be toggled. */
  126. uint32_t timeout_ticks; /**< Value to start the next app_timer. */
  127. low_power_pwm_evt_type_t evt_type; /**< Slope that triggered time-out. */
  128. app_timer_timeout_handler_t handler; /**< User handler to be called in the time-out handler. */
  129. app_timer_id_t const * p_timer_id; /**< Pointer to the timer ID of low_power_pwm. */
  130. NRF_GPIO_Type * p_port; /**< Port used with pin bit mask. */
  131. };
  132. /** @}
  133. * @endcond
  134. */
  135. /**
  136. * @brief Internal structure holding parameters of a low-power PWM instance.
  137. */
  138. typedef struct low_power_pwm_s low_power_pwm_t;
  139. /**
  140. * @brief Function for initializing a low-power PWM instance.
  141. *
  142. * @param[in] p_pwm_instance Pointer to the instance to be started.
  143. * @param[in] p_pwm_config Pointer to the configuration structure.
  144. * @param[in] handler User function to be called in case of time-out.
  145. *
  146. * @return Values returned by @ref app_timer_create.
  147. */
  148. ret_code_t low_power_pwm_init(low_power_pwm_t * p_pwm_instance,
  149. low_power_pwm_config_t const * p_pwm_config,
  150. app_timer_timeout_handler_t handler);
  151. /**
  152. * @brief Function for starting a low-power PWM instance.
  153. *
  154. * @param[in] p_pwm_instance Pointer to the instance to be started.
  155. * @param[in] pins_bit_mask Bit mask of pins to be started.
  156. *
  157. * @return Values returned by @ref app_timer_start.
  158. */
  159. ret_code_t low_power_pwm_start(low_power_pwm_t * p_pwm_instance,
  160. uint32_t pins_bit_mask);
  161. /**
  162. * @brief Function for stopping a low-power PWM instance.
  163. *
  164. * @param[in] p_pwm_instance Pointer to the instance to be stopped.
  165. *
  166. * @return Values returned by @ref app_timer_stop.
  167. */
  168. ret_code_t low_power_pwm_stop(low_power_pwm_t * p_pwm_instance);
  169. /**
  170. * @brief Function for setting a new high pulse width for a given instance.
  171. *
  172. * This function can be called from the timer handler.
  173. *
  174. * @param[in] p_pwm_instance Pointer to the instance to be changed.
  175. * @param[in] duty_cycle New high pulse width. 0 means that the pin is always off. 255 means that it is always on.
  176. *
  177. * @retval NRF_SUCCESS If the function completed successfully.
  178. * @retval NRF_ERROR_INVALID_PARAM If the function returned an error because of invalid parameters.
  179. */
  180. ret_code_t low_power_pwm_duty_set(low_power_pwm_t * p_pwm_instance, uint8_t duty_cycle);
  181. #ifdef __cplusplus
  182. }
  183. #endif
  184. #endif // LOW_POWER_PWM_H__
  185. /** @} */