nrf_drv_pwm.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /**@file
  13. * @addtogroup nrf_pwm PWM HAL and driver
  14. * @ingroup nrf_drivers
  15. * @brief @tagAPI52 Pulse Width Modulation (PWM) module APIs.
  16. *
  17. * @defgroup nrf_drv_pwm PWM driver
  18. * @{
  19. * @ingroup nrf_pwm
  20. * @brief @tagAPI52 Pulse Width Modulation (PWM) module driver.
  21. */
  22. #ifndef NRF_DRV_PWM_H__
  23. #define NRF_DRV_PWM_H__
  24. #include "nordic_common.h"
  25. #include "nrf_drv_config.h"
  26. #include "nrf_pwm.h"
  27. #include "sdk_errors.h"
  28. /**
  29. * @brief PWM driver instance data structure.
  30. */
  31. typedef struct
  32. {
  33. NRF_PWM_Type * p_registers; ///< Pointer to the structure with PWM peripheral instance registers.
  34. uint8_t drv_inst_idx; ///< Driver instance index.
  35. } nrf_drv_pwm_t;
  36. /**
  37. * @brief Macro for creating a PWM driver instance.
  38. */
  39. #define NRF_DRV_PWM_INSTANCE(id) \
  40. { \
  41. .p_registers = CONCAT_2(NRF_PWM, id), \
  42. .drv_inst_idx = CONCAT_3(PWM, id, _INSTANCE_INDEX), \
  43. }
  44. /**
  45. * @brief This value can be provided instead of a pin number for any channel
  46. * to specify that its output is not used and therefore does not need
  47. * to be connected to a pin.
  48. */
  49. #define NRF_DRV_PWM_PIN_NOT_USED 0xFF
  50. /**
  51. * @brief This value can be added to a pin number to inverse its polarity
  52. * (set idle state = 1).
  53. */
  54. #define NRF_DRV_PWM_PIN_INVERTED 0x80
  55. /**
  56. * @brief PWM driver configuration structure.
  57. */
  58. typedef struct
  59. {
  60. uint8_t output_pins[NRF_PWM_CHANNEL_COUNT]; ///< Pin numbers for individual output channels (optional).
  61. /**< Use @ref NRF_DRV_PWM_PIN_NOT_USED
  62. * if a given output channel is not needed. */
  63. uint8_t irq_priority; ///< Interrupt priority.
  64. nrf_pwm_clk_t base_clock; ///< Base clock frequency.
  65. nrf_pwm_mode_t count_mode; ///< Operating mode of the pulse generator counter.
  66. uint16_t top_value; ///< Value up to which the pulse generator counter counts.
  67. nrf_pwm_dec_load_t load_mode; ///< Mode of loading sequence data from RAM.
  68. nrf_pwm_dec_step_t step_mode; ///< Mode of advancing the active sequence.
  69. } nrf_drv_pwm_config_t;
  70. /**
  71. * @brief PWM driver default configuration.
  72. */
  73. #define NRF_DRV_PWM_DEFAULT_CONFIG(id) \
  74. { \
  75. .output_pins = { CONCAT_3(PWM, id, _CONFIG_OUT0_PIN), \
  76. CONCAT_3(PWM, id, _CONFIG_OUT1_PIN), \
  77. CONCAT_3(PWM, id, _CONFIG_OUT2_PIN), \
  78. CONCAT_3(PWM, id, _CONFIG_OUT3_PIN) }, \
  79. .irq_priority = CONCAT_3(PWM, id, _CONFIG_IRQ_PRIORITY), \
  80. .base_clock = CONCAT_3(PWM, id, _CONFIG_BASE_CLOCK), \
  81. .count_mode = CONCAT_3(PWM, id, _CONFIG_COUNT_MODE), \
  82. .top_value = CONCAT_3(PWM, id, _CONFIG_TOP_VALUE), \
  83. .load_mode = CONCAT_3(PWM, id, _CONFIG_LOAD_MODE), \
  84. .step_mode = CONCAT_3(PWM, id, _CONFIG_STEP_MODE), \
  85. }
  86. /**
  87. * @brief PWM flags providing additional playback options.
  88. */
  89. typedef enum
  90. {
  91. NRF_DRV_PWM_FLAG_STOP = 0x01, /**< When the requested playback is finished,
  92. the peripheral should be stopped.
  93. @note The STOP task is triggered when
  94. the last value of the final sequence is
  95. loaded from RAM, and the peripheral stops
  96. at the end of the current PWM period.
  97. For sequences with configured repeating
  98. of duty cycle values, this might result in
  99. less than the requested number of repeats
  100. of the last value. */
  101. NRF_DRV_PWM_FLAG_LOOP = 0x02, /**< When the requested playback is finished,
  102. it should be started from the beginning.
  103. This flag is ignored if used together
  104. with @ref NRF_DRV_PWM_FLAG_STOP. */
  105. NRF_DRV_PWM_FLAG_SIGNAL_END_SEQ0 = 0x04, /**< The event handler should be
  106. called when the last value
  107. from sequence 0 is loaded. */
  108. NRF_DRV_PWM_FLAG_SIGNAL_END_SEQ1 = 0x08, /**< The event handler should be
  109. called when the last value
  110. from sequence 1 is loaded. */
  111. NRF_DRV_PWM_FLAG_NO_EVT_FINISHED = 0x10, /**< The playback finished event
  112. (enabled by default) should be
  113. suppressed. */
  114. } nrf_drv_pwm_flag_t;
  115. /**
  116. * @brief PWM driver event type.
  117. */
  118. typedef enum
  119. {
  120. NRF_DRV_PWM_EVT_FINISHED, ///< Sequence playback finished.
  121. NRF_DRV_PWM_EVT_END_SEQ0, /**< End of sequence 0 reached. Its data can be
  122. safely modified now. */
  123. NRF_DRV_PWM_EVT_END_SEQ1, /**< End of sequence 1 reached. Its data can be
  124. safely modified now. */
  125. NRF_DRV_PWM_EVT_STOPPED, ///< The PWM peripheral has been stopped.
  126. } nrf_drv_pwm_evt_type_t;
  127. /**
  128. * @brief PWM driver event handler type.
  129. */
  130. typedef void (* nrf_drv_pwm_handler_t)(nrf_drv_pwm_evt_type_t event_type);
  131. /**
  132. * @brief Function for initializing the PWM driver.
  133. *
  134. * @param[in] p_instance Pointer to the driver instance structure.
  135. * @param[in] p_config Pointer to the structure with initial configuration.
  136. * If NULL, the default configuration is used.
  137. * @param[in] handler Event handler provided by the user. If NULL is passed
  138. * instead, event notifications are not done and PWM
  139. * interrupts are disabled.
  140. *
  141. * @retval NRF_SUCCESS If initialization was successful.
  142. * @retval NRF_ERROR_INVALID_STATE If the driver was already initialized.
  143. */
  144. ret_code_t nrf_drv_pwm_init(nrf_drv_pwm_t const * const p_instance,
  145. nrf_drv_pwm_config_t const * p_config,
  146. nrf_drv_pwm_handler_t handler);
  147. /**
  148. * @brief Function for uninitializing the PWM driver.
  149. *
  150. * If any sequence playback is in progress, it is stopped immediately.
  151. *
  152. * @param[in] p_instance Pointer to the driver instance structure.
  153. */
  154. void nrf_drv_pwm_uninit(nrf_drv_pwm_t const * const p_instance);
  155. /**
  156. * @brief Function for starting a single sequence playback.
  157. *
  158. * To take advantage of the looping mechanism in the PWM peripheral, both
  159. * sequences must be used (single sequence can be played back only once by
  160. * the peripheral). Therefore, the provided sequence is internally set and
  161. * played back as both sequence 0 and sequence 1. Consequently, if end of
  162. * sequence notifications are required, events for both sequences should be
  163. * used (that means that both the @ref NRF_DRV_PWM_FLAG_SIGNAL_END_SEQ0 flag
  164. * and the @ref NRF_DRV_PWM_FLAG_SIGNAL_END_SEQ1 flag should be specified and
  165. * the @ref NRF_DRV_PWM_EVT_END_SEQ0 event and the @ref NRF_DRV_PWM_EVT_END_SEQ1
  166. * event should be handled in the same way).
  167. *
  168. * @note The array containing the duty cycle values for the specified sequence
  169. * must be in RAM and cannot be allocated on stack.
  170. * For detailed information, see @ref nrf_pwm_sequence_t.
  171. *
  172. * @param[in] p_instance Pointer to the driver instance structure.
  173. * @param[in] p_sequence Sequence to be played back.
  174. * @param[in] playback_count Number of playbacks to be performed (must not be 0).
  175. * @param[in] flags Additional options. Pass any combination of
  176. * @ref nrf_drv_pwm_flag_t "playback flags", or 0
  177. * for default settings.
  178. */
  179. void nrf_drv_pwm_simple_playback(nrf_drv_pwm_t const * const p_instance,
  180. nrf_pwm_sequence_t const * p_sequence,
  181. uint16_t playback_count,
  182. uint32_t flags);
  183. /**
  184. * @brief Function for starting a two-sequence playback.
  185. *
  186. * @note The array containing the duty cycle values for the specified sequence
  187. * must be in RAM and cannot be allocated on stack.
  188. * For detailed information, see @ref nrf_pwm_sequence_t.
  189. *
  190. * @param[in] p_instance Pointer to the driver instance structure.
  191. * @param[in] p_sequence_0 First sequence to be played back.
  192. * @param[in] p_sequence_1 Second sequence to be played back.
  193. * @param[in] playback_count Number of playbacks to be performed (must not be 0).
  194. * @param[in] flags Additional options. Pass any combination of
  195. * @ref nrf_drv_pwm_flag_t "playback flags", or 0
  196. * for default settings.
  197. */
  198. void nrf_drv_pwm_complex_playback(nrf_drv_pwm_t const * const p_instance,
  199. nrf_pwm_sequence_t const * p_sequence_0,
  200. nrf_pwm_sequence_t const * p_sequence_1,
  201. uint16_t playback_count,
  202. uint32_t flags);
  203. /**
  204. * @brief Function for advancing the active sequence.
  205. *
  206. * This function only applies to @ref NRF_PWM_STEP_TRIGGERED mode.
  207. *
  208. * @param[in] p_instance Pointer to the driver instance structure.
  209. */
  210. __STATIC_INLINE void nrf_drv_pwm_step(nrf_drv_pwm_t const * const p_instance);
  211. /**
  212. * @brief Function for stopping the sequence playback.
  213. *
  214. * The playback is stopped at the end of the current PWM period.
  215. * This means that if the active sequence is configured to repeat each duty
  216. * cycle value for a certain number of PWM periods, the last played value
  217. * might appear on the output less times than requested.
  218. *
  219. * @note This function can be instructed to wait until the playback is stopped
  220. * (by setting @p wait_until_stopped to true). Note that, depending on
  221. * the length of the PMW period, this might take a significant amount of
  222. * time. Alternatively, the @ref nrf_drv_pwm_is_stopped function can be
  223. * used to poll the status, or the @ref NRF_DRV_PWM_EVT_STOPPED event can
  224. * be used to get the notification when the playback is stopped, provided
  225. * the event handler is defined.
  226. *
  227. * @param[in] p_instance Pointer to the driver instance structure.
  228. * @param[in] wait_until_stopped If true, the function will not return until
  229. * the playback is stopped.
  230. *
  231. * @retval true If the PWM peripheral is stopped.
  232. * @retval false If the PWM peripheral is not stopped.
  233. */
  234. bool nrf_drv_pwm_stop(nrf_drv_pwm_t const * const p_instance,
  235. bool wait_until_stopped);
  236. /**
  237. * @brief Function for checking the status of the PWM peripheral.
  238. *
  239. * @param[in] p_instance Pointer to the driver instance structure.
  240. *
  241. * @retval true If the PWM peripheral is stopped.
  242. * @retval false If the PWM peripheral is not stopped.
  243. */
  244. bool nrf_drv_pwm_is_stopped(nrf_drv_pwm_t const * const p_instance);
  245. /**
  246. * @brief Function for updating the sequence data during playback.
  247. *
  248. * @param[in] p_instance Pointer to the driver instance structure.
  249. * @param[in] seq_id Identifier of the sequence (0 or 1).
  250. * @param[in] p_sequence Pointer to the new sequence definition.
  251. */
  252. __STATIC_INLINE void nrf_drv_pwm_sequence_update(
  253. nrf_drv_pwm_t const * const p_instance,
  254. uint8_t seq_id,
  255. nrf_pwm_sequence_t const * p_sequence);
  256. /**
  257. * @brief Function for updating the pointer to the duty cycle values
  258. * in the specified sequence during playback.
  259. *
  260. * @param[in] p_instance Pointer to the driver instance structure.
  261. * @param[in] seq_id Identifier of the sequence (0 or 1).
  262. * @param[in] values New pointer to the duty cycle values.
  263. */
  264. __STATIC_INLINE void nrf_drv_pwm_sequence_values_update(
  265. nrf_drv_pwm_t const * const p_instance,
  266. uint8_t seq_id,
  267. nrf_pwm_values_t values);
  268. /**
  269. * @brief Function for updating the number of duty cycle values
  270. * in the specified sequence during playback.
  271. *
  272. * @param[in] p_instance Pointer to the driver instance structure.
  273. * @param[in] seq_id Identifier of the sequence (0 or 1).
  274. * @param[in] length New number of the duty cycle values.
  275. */
  276. __STATIC_INLINE void nrf_drv_pwm_sequence_length_update(
  277. nrf_drv_pwm_t const * const p_instance,
  278. uint8_t seq_id,
  279. uint16_t length);
  280. /**
  281. * @brief Function for updating the number of repeats for duty cycle values
  282. * in specified sequence during playback.
  283. *
  284. * @param[in] p_instance Pointer to the driver instance structure.
  285. * @param[in] seq_id Identifier of the sequence (0 or 1).
  286. * @param[in] repeats New number of repeats.
  287. */
  288. __STATIC_INLINE void nrf_drv_pwm_sequence_repeats_update(
  289. nrf_drv_pwm_t const * const p_instance,
  290. uint8_t seq_id,
  291. uint32_t repeats);
  292. /**
  293. * @brief Function for updating the additional delay after the specified
  294. * sequence during playback.
  295. *
  296. * @param[in] p_instance Pointer to the driver instance structure.
  297. * @param[in] seq_id Identifier of the sequence (0 or 1).
  298. * @param[in] end_delay New end delay value (in PWM periods).
  299. */
  300. __STATIC_INLINE void nrf_drv_pwm_sequence_end_delay_update(
  301. nrf_drv_pwm_t const * const p_instance,
  302. uint8_t seq_id,
  303. uint32_t end_delay);
  304. /**
  305. * @brief Function for returning the address of a specified PWM task that can
  306. * be used in PPI module.
  307. *
  308. * @param[in] p_instance Pointer to the driver instance structure.
  309. * @param[in] task Requested task.
  310. *
  311. * @return Task address.
  312. */
  313. __STATIC_INLINE uint32_t nrf_drv_pwm_task_address_get(
  314. nrf_drv_pwm_t const * const p_instance,
  315. nrf_pwm_task_t task);
  316. /**@brief Function for returning the address of a specified PWM event that can
  317. * be used in PPI module.
  318. *
  319. * @param[in] p_instance Pointer to the driver instance structure.
  320. * @param[in] event Requested event.
  321. *
  322. * @return Event address.
  323. */
  324. __STATIC_INLINE uint32_t nrf_drv_pwm_event_address_get(
  325. nrf_drv_pwm_t const * const p_instance,
  326. nrf_pwm_event_t event);
  327. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  328. __STATIC_INLINE void nrf_drv_pwm_step(nrf_drv_pwm_t const * const p_instance)
  329. {
  330. nrf_pwm_task_trigger(p_instance->p_registers, NRF_PWM_TASK_NEXTSTEP);
  331. }
  332. __STATIC_INLINE void nrf_drv_pwm_sequence_update(
  333. nrf_drv_pwm_t const * const p_instance,
  334. uint8_t seq_id,
  335. nrf_pwm_sequence_t const * p_sequence)
  336. {
  337. nrf_pwm_sequence_set(p_instance->p_registers, seq_id, p_sequence);
  338. }
  339. __STATIC_INLINE void nrf_drv_pwm_sequence_values_update(
  340. nrf_drv_pwm_t const * const p_instance,
  341. uint8_t seq_id,
  342. nrf_pwm_values_t values)
  343. {
  344. nrf_pwm_seq_ptr_set(p_instance->p_registers, seq_id, values.p_raw);
  345. }
  346. __STATIC_INLINE void nrf_drv_pwm_sequence_length_update(
  347. nrf_drv_pwm_t const * const p_instance,
  348. uint8_t seq_id,
  349. uint16_t length)
  350. {
  351. nrf_pwm_seq_cnt_set(p_instance->p_registers, seq_id, length);
  352. }
  353. __STATIC_INLINE void nrf_drv_pwm_sequence_repeats_update(
  354. nrf_drv_pwm_t const * const p_instance,
  355. uint8_t seq_id,
  356. uint32_t repeats)
  357. {
  358. nrf_pwm_seq_refresh_set(p_instance->p_registers, seq_id, repeats);
  359. }
  360. __STATIC_INLINE void nrf_drv_pwm_sequence_end_delay_update(
  361. nrf_drv_pwm_t const * const p_instance,
  362. uint8_t seq_id,
  363. uint32_t end_delay)
  364. {
  365. nrf_pwm_seq_end_delay_set(p_instance->p_registers, seq_id, end_delay);
  366. }
  367. __STATIC_INLINE uint32_t nrf_drv_pwm_task_address_get(
  368. nrf_drv_pwm_t const * const p_instance,
  369. nrf_pwm_task_t task)
  370. {
  371. return nrf_pwm_task_address_get(p_instance->p_registers, task);
  372. }
  373. __STATIC_INLINE uint32_t nrf_drv_pwm_event_address_get(
  374. nrf_drv_pwm_t const * const p_instance,
  375. nrf_pwm_event_t event)
  376. {
  377. return nrf_pwm_event_address_get(p_instance->p_registers, event);
  378. }
  379. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  380. #endif // NRF_DRV_PWM_H__
  381. /** @} */