app_pwm.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 app_pwm Pulse-width modulation (PWM)
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief Module for generating a pulse-width modulated output signal.
  47. *
  48. * @details This module provides a PWM implementation using timers, GPIOTE, and PPI.
  49. *
  50. * Resource usage:
  51. * - 2 PPI channels per instance + 2 PPI channels per PWM channel.
  52. * - 1 PPI group per instance.
  53. * - 1 GPIOTE channel per PWM channel.
  54. *
  55. * For example, a PWM instance with two channels will consume 2 + 4 PPI channels, 1 PPI group, and 2 GPIOTE channels.
  56. *
  57. * The maximum number of PWM channels per instance is 2.
  58. */
  59. #ifndef APP_PWM_H__
  60. #define APP_PWM_H__
  61. #include <stdint.h>
  62. #include "sdk_errors.h"
  63. #include "nrf_drv_timer.h"
  64. #include "nrf_drv_ppi.h"
  65. #include "nrf_peripherals.h"
  66. #ifdef __cplusplus
  67. extern "C" {
  68. #endif
  69. #if defined(GPIOTE_FEATURE_SET_PRESENT) && defined(GPIOTE_FEATURE_CLR_PRESENT)
  70. #define GPIOTE_SET_CLEAR_TASKS
  71. #endif
  72. #define APP_PWM_NOPIN 0xFFFFFFFF
  73. /** @brief Number of channels for one timer instance (fixed to 2 due to timer properties).*/
  74. #define APP_PWM_CHANNELS_PER_INSTANCE 2
  75. /**@brief Macro for creating a PWM instance. */
  76. #define APP_PWM_INSTANCE(name, num) \
  77. const nrf_drv_timer_t m_pwm_##name##_timer = NRF_DRV_TIMER_INSTANCE(num); \
  78. app_pwm_cb_t m_pwm_##name##_cb; \
  79. /*lint -e{545}*/ \
  80. const app_pwm_t name = { \
  81. .p_cb = &m_pwm_##name##_cb, \
  82. .p_timer = &m_pwm_##name##_timer, \
  83. }
  84. /**@brief PWM instance default configuration (1 channel). */
  85. #define APP_PWM_DEFAULT_CONFIG_1CH(period_in_us, pin) \
  86. { \
  87. .pins = {pin, APP_PWM_NOPIN}, \
  88. .pin_polarity = {APP_PWM_POLARITY_ACTIVE_LOW, APP_PWM_POLARITY_ACTIVE_LOW}, \
  89. .num_of_channels = 1, \
  90. .period_us = period_in_us \
  91. }
  92. /**@brief PWM instance default configuration (2 channels). */
  93. #define APP_PWM_DEFAULT_CONFIG_2CH(period_in_us, pin0, pin1) \
  94. { \
  95. .pins = {pin0, pin1}, \
  96. .pin_polarity = {APP_PWM_POLARITY_ACTIVE_LOW, APP_PWM_POLARITY_ACTIVE_LOW}, \
  97. .num_of_channels = 2, \
  98. .period_us = period_in_us \
  99. }
  100. typedef uint16_t app_pwm_duty_t;
  101. /**
  102. * @brief PWM callback that is executed when a PWM duty change has been completed.
  103. *
  104. * @param[in] pwm_id PWM instance ID.
  105. */
  106. typedef void (* app_pwm_callback_t)(uint32_t);
  107. /**
  108. * @brief Channel polarity.
  109. */
  110. typedef enum
  111. {
  112. APP_PWM_POLARITY_ACTIVE_LOW = 0,
  113. APP_PWM_POLARITY_ACTIVE_HIGH = 1
  114. } app_pwm_polarity_t;
  115. /**@brief PWM configuration structure used for initialization. */
  116. typedef struct
  117. {
  118. uint32_t pins[APP_PWM_CHANNELS_PER_INSTANCE]; //!< Pins configured as PWM output.
  119. app_pwm_polarity_t pin_polarity[APP_PWM_CHANNELS_PER_INSTANCE]; //!< Polarity of active state on pin.
  120. uint32_t num_of_channels; //!< Number of channels that can be used.
  121. uint32_t period_us; //!< PWM signal output period to configure (in microseconds).
  122. } app_pwm_config_t;
  123. /**
  124. * @cond (NODOX)
  125. * @defgroup app_pwm_internal Auxiliary internal types declarations
  126. * @{
  127. * @internal
  128. *
  129. * @brief Module for internal usage inside the library only
  130. *
  131. * There are some definitions that must be included in the header file because
  132. * of the way the library is set up. In this way, the are accessible to the user.
  133. * However, any functions and variables defined here may change at any time
  134. * without a warning, so you should not access them directly.
  135. */
  136. /**
  137. * @brief PWM channel instance
  138. *
  139. * This structure holds all data needed by a single PWM channel.
  140. */
  141. typedef struct
  142. {
  143. uint32_t gpio_pin; //!< Pin that is used by this PWM channel.
  144. uint32_t pulsewidth; //!< The copy of duty currently set (in ticks).
  145. nrf_ppi_channel_t ppi_channels[2]; //!< PPI channels used by the PWM channel to clear and set the output.
  146. app_pwm_polarity_t polarity; //!< The active state of the pin.
  147. uint8_t initialized; //!< The internal information if the selected channel was initialized.
  148. } app_pwm_channel_cb_t;
  149. /**
  150. * @brief Variable part of PWM instance
  151. *
  152. * This structure holds instance data that may change.
  153. */
  154. typedef struct
  155. {
  156. app_pwm_channel_cb_t channels_cb[APP_PWM_CHANNELS_PER_INSTANCE]; //!< Channels data
  157. uint32_t period; //!< Selected period in ticks
  158. app_pwm_callback_t p_ready_callback; //!< Callback function called on PWM readiness
  159. #ifdef GPIOTE_SET_CLEAR_TASKS
  160. nrf_ppi_channel_t ppi_channel; //!< PPI channel used temporary while changing duty
  161. #else
  162. nrf_ppi_channel_t ppi_channels[2]; //!< PPI channels used temporary while changing duty
  163. nrf_ppi_channel_group_t ppi_group; //!< PPI group used to synchronize changes on channels
  164. #endif
  165. nrfx_drv_state_t state; //!< Current driver status
  166. } app_pwm_cb_t;
  167. /** @}
  168. * @endcond
  169. */
  170. /**@brief PWM instance structure. */
  171. typedef struct
  172. {
  173. app_pwm_cb_t *p_cb; //!< Pointer to control block internals.
  174. nrf_drv_timer_t const * const p_timer; //!< Timer used by this PWM instance.
  175. } app_pwm_t;
  176. /**
  177. * @brief Function for checking if the PWM instance is busy updating the duty cycle.
  178. *
  179. * @param[in] p_instance PWM instance.
  180. *
  181. * @retval True If the PWM instance is ready for duty cycle changes.
  182. * @retval False If a change operation is in progress.
  183. */
  184. bool app_pwm_busy_check(app_pwm_t const * const p_instance);
  185. /**
  186. * @brief Function for initializing a PWM instance.
  187. *
  188. * @param[in] p_instance PWM instance.
  189. * @param[in] p_config Initial configuration.
  190. * @param[in] p_ready_callback Pointer to ready callback function (or NULL to disable).
  191. *
  192. * @retval NRF_SUCCESS If initialization was successful.
  193. * @retval NRF_ERROR_NO_MEM If there were not enough free resources.
  194. * @retval NRF_ERROR_INVALID_PARAM If an invalid configuration structure was passed.
  195. * @retval NRF_ERROR_INVALID_STATE If the timer/PWM is already in use or if initialization failed.
  196. */
  197. ret_code_t app_pwm_init(app_pwm_t const * const p_instance, app_pwm_config_t const * const p_config,
  198. app_pwm_callback_t p_ready_callback);
  199. /**
  200. * @brief Function for uninitializing a PWM instance and releasing the allocated resources.
  201. *
  202. * @param[in] p_instance PWM instance.
  203. *
  204. * @retval NRF_SUCCESS If uninitialization was successful.
  205. * @retval NRF_ERROR_INVALID_STATE If the given instance was not initialized.
  206. */
  207. ret_code_t app_pwm_uninit(app_pwm_t const * const p_instance);
  208. /**
  209. * @brief Function for enabling a PWM instance after initialization.
  210. *
  211. * @param[in] p_instance PWM instance.
  212. */
  213. void app_pwm_enable(app_pwm_t const * const p_instance);
  214. /**
  215. * @brief Function for disabling a PWM instance after initialization.
  216. *
  217. * @param[in] p_instance PWM instance.
  218. */
  219. void app_pwm_disable(app_pwm_t const * const p_instance);
  220. /**
  221. * @brief Function for setting the PWM channel duty cycle in percents.
  222. *
  223. * A duty cycle change requires one full PWM clock period to finish.
  224. * If another change is attempted for any channel of given instance before
  225. * the current change is complete, the new attempt will result in the error
  226. * NRF_ERROR_BUSY.
  227. *
  228. * @param[in] p_instance PWM instance.
  229. * @param[in] channel Channel number.
  230. * @param[in] duty Duty cycle (0 - 100).
  231. *
  232. * @retval NRF_SUCCESS If the operation was successful.
  233. * @retval NRF_ERROR_BUSY If the PWM is not ready yet.
  234. * @retval NRF_ERROR_INVALID_STATE If the given instance was not initialized.
  235. *
  236. */
  237. ret_code_t app_pwm_channel_duty_set(app_pwm_t const * const p_instance,
  238. uint8_t channel, app_pwm_duty_t duty);
  239. /**
  240. * @brief Function for retrieving the PWM channel duty cycle in percents.
  241. *
  242. * @param[in] p_instance PWM instance.
  243. * @param[in] channel Channel number.
  244. *
  245. * @return Duty cycle value.
  246. */
  247. app_pwm_duty_t app_pwm_channel_duty_get(app_pwm_t const * const p_instance, uint8_t channel);
  248. /**
  249. * @name Functions accessing values in ticks
  250. *
  251. * Auxiliary functions that allow to get values in actual timer ticks.
  252. * @{
  253. */
  254. /**
  255. * @brief Function for setting PWM channel duty cycle in clock ticks.
  256. *
  257. * @note Duty cycle changes require one full PWM clock period to finish.
  258. * Until that, the next change attempt (for any channel of given instance)
  259. * will result in an NRF_ERROR_BUSY error.
  260. *
  261. * @param[in] p_instance PWM instance.
  262. * @param[in] channel Channel number.
  263. * @param[in] ticks Number of PWM clock ticks.
  264. *
  265. * @retval NRF_SUCCESS If the operation was successful.
  266. * @retval NRF_ERROR_BUSY If PWM is not ready yet.
  267. * @retval NRF_ERROR_INVALID_STATE If the given instance was not initialized.
  268. */
  269. ret_code_t app_pwm_channel_duty_ticks_set(app_pwm_t const * const p_instance,
  270. uint8_t channel,
  271. uint16_t ticks);
  272. /**
  273. * @brief Function for retrieving the PWM channel duty cycle in ticks.
  274. *
  275. * This function retrieves the real, currently set duty cycle in ticks.
  276. * For one full PWM cycle the value might be different than the value set by the last
  277. * @ref app_pwm_channel_duty_ticks_set function call.
  278. *
  279. * @param[in] p_instance PWM instance.
  280. * @param[in] channel Channel number.
  281. *
  282. * @return Number of ticks set for selected channel.
  283. *
  284. */
  285. uint16_t app_pwm_channel_duty_ticks_get(app_pwm_t const * const p_instance, uint8_t channel);
  286. /**
  287. * @brief Function for returning the number of ticks in a whole cycle.
  288. *
  289. * @param[in] p_instance PWM instance.
  290. *
  291. * @return Number of ticks that corresponds to 100% of the duty cycle.
  292. */
  293. uint16_t app_pwm_cycle_ticks_get(app_pwm_t const * const p_instance);
  294. /** @} */
  295. #ifdef __cplusplus
  296. }
  297. #endif
  298. #endif
  299. /** @} */