nrfx_pwm.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. #ifndef NRFX_PWM_H__
  41. #define NRFX_PWM_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_pwm.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @defgroup nrfx_pwm PWM driver
  49. * @{
  50. * @ingroup nrf_pwm
  51. * @brief Pulse Width Modulation (PWM) peripheral driver.
  52. */
  53. /** @brief PWM driver instance data structure. */
  54. typedef struct
  55. {
  56. NRF_PWM_Type * p_registers; ///< Pointer to the structure with PWM peripheral instance registers.
  57. uint8_t drv_inst_idx; ///< Index of the driver instance. For internal use only.
  58. } nrfx_pwm_t;
  59. /** @brief Macro for creating a PWM driver instance. */
  60. #define NRFX_PWM_INSTANCE(id) \
  61. { \
  62. .p_registers = NRFX_CONCAT_2(NRF_PWM, id), \
  63. .drv_inst_idx = NRFX_CONCAT_3(NRFX_PWM, id, _INST_IDX), \
  64. }
  65. #ifndef __NRFX_DOXYGEN__
  66. enum {
  67. #if NRFX_CHECK(NRFX_PWM0_ENABLED)
  68. NRFX_PWM0_INST_IDX,
  69. #endif
  70. #if NRFX_CHECK(NRFX_PWM1_ENABLED)
  71. NRFX_PWM1_INST_IDX,
  72. #endif
  73. #if NRFX_CHECK(NRFX_PWM2_ENABLED)
  74. NRFX_PWM2_INST_IDX,
  75. #endif
  76. #if NRFX_CHECK(NRFX_PWM3_ENABLED)
  77. NRFX_PWM3_INST_IDX,
  78. #endif
  79. NRFX_PWM_ENABLED_COUNT
  80. };
  81. #endif
  82. /**
  83. * @brief This value can be provided instead of a pin number for any channel
  84. * to specify that its output is not used and therefore does not need
  85. * to be connected to a pin.
  86. */
  87. #define NRFX_PWM_PIN_NOT_USED 0xFF
  88. /** @brief This value can be added to a pin number to invert its polarity (set idle state = 1). */
  89. #define NRFX_PWM_PIN_INVERTED 0x80
  90. /** @brief PWM driver configuration structure. */
  91. typedef struct
  92. {
  93. uint8_t output_pins[NRF_PWM_CHANNEL_COUNT]; ///< Pin numbers for individual output channels (optional).
  94. /**< Use @ref NRFX_PWM_PIN_NOT_USED
  95. * if a given output channel is not needed. */
  96. uint8_t irq_priority; ///< Interrupt priority.
  97. nrf_pwm_clk_t base_clock; ///< Base clock frequency.
  98. nrf_pwm_mode_t count_mode; ///< Operating mode of the pulse generator counter.
  99. uint16_t top_value; ///< Value up to which the pulse generator counter counts.
  100. nrf_pwm_dec_load_t load_mode; ///< Mode of loading sequence data from RAM.
  101. nrf_pwm_dec_step_t step_mode; ///< Mode of advancing the active sequence.
  102. } nrfx_pwm_config_t;
  103. /** @brief PWM driver default configuration. */
  104. #define NRFX_PWM_DEFAULT_CONFIG \
  105. { \
  106. .output_pins = { NRFX_PWM_DEFAULT_CONFIG_OUT0_PIN, \
  107. NRFX_PWM_DEFAULT_CONFIG_OUT1_PIN, \
  108. NRFX_PWM_DEFAULT_CONFIG_OUT2_PIN, \
  109. NRFX_PWM_DEFAULT_CONFIG_OUT3_PIN }, \
  110. .irq_priority = NRFX_PWM_DEFAULT_CONFIG_IRQ_PRIORITY, \
  111. .base_clock = (nrf_pwm_clk_t)NRFX_PWM_DEFAULT_CONFIG_BASE_CLOCK, \
  112. .count_mode = (nrf_pwm_mode_t)NRFX_PWM_DEFAULT_CONFIG_COUNT_MODE, \
  113. .top_value = NRFX_PWM_DEFAULT_CONFIG_TOP_VALUE, \
  114. .load_mode = (nrf_pwm_dec_load_t)NRFX_PWM_DEFAULT_CONFIG_LOAD_MODE, \
  115. .step_mode = (nrf_pwm_dec_step_t)NRFX_PWM_DEFAULT_CONFIG_STEP_MODE, \
  116. }
  117. /** @brief PWM flags that provide additional playback options. */
  118. typedef enum
  119. {
  120. NRFX_PWM_FLAG_STOP = 0x01, /**< When the requested playback is finished,
  121. the peripheral will be stopped.
  122. @note The STOP task is triggered when
  123. the last value of the final sequence is
  124. loaded from RAM, and the peripheral stops
  125. at the end of the current PWM period.
  126. For sequences with configured repeating
  127. of duty cycle values, this might result in
  128. less than the requested number of repeats
  129. of the last value. */
  130. NRFX_PWM_FLAG_LOOP = 0x02, /**< When the requested playback is finished,
  131. it will be started from the beginning.
  132. This flag is ignored if used together
  133. with @ref NRFX_PWM_FLAG_STOP.
  134. @note The playback restart is done via a
  135. shortcut configured in the PWM peripheral.
  136. This shortcut triggers the proper starting
  137. task when the final value of previous
  138. playback is read from RAM and applied to
  139. the pulse generator counter.
  140. When this mechanism is used together with
  141. the @ref NRF_PWM_STEP_TRIGGERED mode,
  142. the playback restart will occur right
  143. after switching to the final value (this
  144. final value will be played only once). */
  145. NRFX_PWM_FLAG_SIGNAL_END_SEQ0 = 0x04, /**< The event handler is to be
  146. called when the last value
  147. from sequence 0 is loaded. */
  148. NRFX_PWM_FLAG_SIGNAL_END_SEQ1 = 0x08, /**< The event handler is to be
  149. called when the last value
  150. from sequence 1 is loaded. */
  151. NRFX_PWM_FLAG_NO_EVT_FINISHED = 0x10, /**< The playback finished event
  152. (enabled by default) is to be
  153. suppressed. */
  154. NRFX_PWM_FLAG_START_VIA_TASK = 0x80, /**< The playback must not be
  155. started directly by the called
  156. function. Instead, the function
  157. must only prepare it and
  158. return the address of the task
  159. to be triggered to start the
  160. playback. */
  161. } nrfx_pwm_flag_t;
  162. /** @brief PWM driver event type. */
  163. typedef enum
  164. {
  165. NRFX_PWM_EVT_FINISHED, ///< Sequence playback finished.
  166. NRFX_PWM_EVT_END_SEQ0, /**< End of sequence 0 reached. Its data can be
  167. safely modified now. */
  168. NRFX_PWM_EVT_END_SEQ1, /**< End of sequence 1 reached. Its data can be
  169. safely modified now. */
  170. NRFX_PWM_EVT_STOPPED, ///< The PWM peripheral has been stopped.
  171. } nrfx_pwm_evt_type_t;
  172. /** @brief PWM driver event handler type. */
  173. typedef void (* nrfx_pwm_handler_t)(nrfx_pwm_evt_type_t event_type);
  174. /**
  175. * @brief Function for initializing the PWM driver.
  176. *
  177. * @param[in] p_instance Pointer to the driver instance structure.
  178. * @param[in] p_config Pointer to the structure with the initial configuration.
  179. * @param[in] handler Event handler provided by the user. If NULL is passed
  180. * instead, event notifications are not done and PWM
  181. * interrupts are disabled.
  182. *
  183. * @retval NRFX_SUCCESS Initialization was successful.
  184. * @retval NRFX_ERROR_INVALID_STATE The driver was already initialized.
  185. */
  186. nrfx_err_t nrfx_pwm_init(nrfx_pwm_t const * const p_instance,
  187. nrfx_pwm_config_t const * p_config,
  188. nrfx_pwm_handler_t handler);
  189. /**
  190. * @brief Function for uninitializing the PWM driver.
  191. *
  192. * If any sequence playback is in progress, it is stopped immediately.
  193. *
  194. * @param[in] p_instance Pointer to the driver instance structure.
  195. */
  196. void nrfx_pwm_uninit(nrfx_pwm_t const * const p_instance);
  197. /**
  198. * @brief Function for starting a single sequence playback.
  199. *
  200. * To take advantage of the looping mechanism in the PWM peripheral, both
  201. * sequences must be used (single sequence can be played back only once by
  202. * the peripheral). Therefore, the provided sequence is internally set and
  203. * played back as both sequence 0 and sequence 1. Consequently, if the end of
  204. * sequence notifications are required, events for both sequences must be
  205. * used (that is, both the @ref NRFX_PWM_FLAG_SIGNAL_END_SEQ0 flag
  206. * and the @ref NRFX_PWM_FLAG_SIGNAL_END_SEQ1 flag must be specified, and
  207. * the @ref NRFX_PWM_EVT_END_SEQ0 event and the @ref NRFX_PWM_EVT_END_SEQ1
  208. * event must be handled in the same way).
  209. *
  210. * Use the @ref NRFX_PWM_FLAG_START_VIA_TASK flag if you want the playback
  211. * to be only prepared by this function, and you want to start it later by
  212. * triggering a task (for example, by using PPI). The function will then return
  213. * the address of the task to be triggered.
  214. *
  215. * @note The array containing the duty cycle values for the specified sequence
  216. * must be in RAM and cannot be allocated on the stack.
  217. * For detailed information, see @ref nrf_pwm_sequence_t.
  218. *
  219. * @param[in] p_instance Pointer to the driver instance structure.
  220. * @param[in] p_sequence Sequence to be played back.
  221. * @param[in] playback_count Number of playbacks to be performed (must not be 0).
  222. * @param[in] flags Additional options. Pass any combination of
  223. * @ref nrfx_pwm_flag_t "playback flags", or 0
  224. * for default settings.
  225. *
  226. * @return Address of the task to be triggered to start the playback if the @ref
  227. * NRFX_PWM_FLAG_START_VIA_TASK flag was used, 0 otherwise.
  228. */
  229. uint32_t nrfx_pwm_simple_playback(nrfx_pwm_t const * const p_instance,
  230. nrf_pwm_sequence_t const * p_sequence,
  231. uint16_t playback_count,
  232. uint32_t flags);
  233. /**
  234. * @brief Function for starting a two-sequence playback.
  235. *
  236. * Use the @ref NRFX_PWM_FLAG_START_VIA_TASK flag if you want the playback
  237. * to be only prepared by this function, and you want to start it later by
  238. * triggering a task (using PPI for instance). The function will then return
  239. * the address of the task to be triggered.
  240. *
  241. * @note The array containing the duty cycle values for the specified sequence
  242. * must be in RAM and cannot be allocated on the stack.
  243. * For detailed information, see @ref nrf_pwm_sequence_t.
  244. *
  245. * @param[in] p_instance Pointer to the driver instance structure.
  246. * @param[in] p_sequence_0 First sequence to be played back.
  247. * @param[in] p_sequence_1 Second sequence to be played back.
  248. * @param[in] playback_count Number of playbacks to be performed (must not be 0).
  249. * @param[in] flags Additional options. Pass any combination of
  250. * @ref nrfx_pwm_flag_t "playback flags", or 0
  251. * for default settings.
  252. *
  253. * @return Address of the task to be triggered to start the playback if the @ref
  254. * NRFX_PWM_FLAG_START_VIA_TASK flag was used, 0 otherwise.
  255. */
  256. uint32_t nrfx_pwm_complex_playback(nrfx_pwm_t const * const p_instance,
  257. nrf_pwm_sequence_t const * p_sequence_0,
  258. nrf_pwm_sequence_t const * p_sequence_1,
  259. uint16_t playback_count,
  260. uint32_t flags);
  261. /**
  262. * @brief Function for advancing the active sequence.
  263. *
  264. * This function only applies to @ref NRF_PWM_STEP_TRIGGERED mode.
  265. *
  266. * @param[in] p_instance Pointer to the driver instance structure.
  267. */
  268. __STATIC_INLINE void nrfx_pwm_step(nrfx_pwm_t const * const p_instance);
  269. /**
  270. * @brief Function for stopping the sequence playback.
  271. *
  272. * The playback is stopped at the end of the current PWM period.
  273. * This means that if the active sequence is configured to repeat each duty
  274. * cycle value for a certain number of PWM periods, the last played value
  275. * might appear on the output less times than requested.
  276. *
  277. * @note This function can be instructed to wait until the playback is stopped
  278. * (by setting @p wait_until_stopped to true). Depending on
  279. * the length of the PMW period, this might take a significant amount of
  280. * time. Alternatively, the @ref nrfx_pwm_is_stopped function can be
  281. * used to poll the status, or the @ref NRFX_PWM_EVT_STOPPED event can
  282. * be used to get the notification when the playback is stopped, provided
  283. * the event handler is defined.
  284. *
  285. * @param[in] p_instance Pointer to the driver instance structure.
  286. * @param[in] wait_until_stopped If true, the function will not return until
  287. * the playback is stopped.
  288. *
  289. * @retval true The PWM peripheral is stopped.
  290. * @retval false The PWM peripheral is not stopped.
  291. */
  292. bool nrfx_pwm_stop(nrfx_pwm_t const * const p_instance, bool wait_until_stopped);
  293. /**
  294. * @brief Function for checking the status of the PWM peripheral.
  295. *
  296. * @param[in] p_instance Pointer to the driver instance structure.
  297. *
  298. * @retval true The PWM peripheral is stopped.
  299. * @retval false The PWM peripheral is not stopped.
  300. */
  301. bool nrfx_pwm_is_stopped(nrfx_pwm_t const * const p_instance);
  302. /**
  303. * @brief Function for updating the sequence data during playback.
  304. *
  305. * @param[in] p_instance Pointer to the driver instance structure.
  306. * @param[in] seq_id Identifier of the sequence (0 or 1).
  307. * @param[in] p_sequence Pointer to the new sequence definition.
  308. */
  309. __STATIC_INLINE void nrfx_pwm_sequence_update(nrfx_pwm_t const * const p_instance,
  310. uint8_t seq_id,
  311. nrf_pwm_sequence_t const * p_sequence);
  312. /**
  313. * @brief Function for updating the pointer to the duty cycle values
  314. * in the specified sequence during playback.
  315. *
  316. * @param[in] p_instance Pointer to the driver instance structure.
  317. * @param[in] seq_id Identifier of the sequence (0 or 1).
  318. * @param[in] values New pointer to the duty cycle values.
  319. */
  320. __STATIC_INLINE void nrfx_pwm_sequence_values_update(nrfx_pwm_t const * const p_instance,
  321. uint8_t seq_id,
  322. nrf_pwm_values_t values);
  323. /**
  324. * @brief Function for updating the number of duty cycle values
  325. * in the specified sequence during playback.
  326. *
  327. * @param[in] p_instance Pointer to the driver instance structure.
  328. * @param[in] seq_id Identifier of the sequence (0 or 1).
  329. * @param[in] length New number of the duty cycle values.
  330. */
  331. __STATIC_INLINE void nrfx_pwm_sequence_length_update(nrfx_pwm_t const * const p_instance,
  332. uint8_t seq_id,
  333. uint16_t length);
  334. /**
  335. * @brief Function for updating the number of repeats for duty cycle values
  336. * in the specified sequence during playback.
  337. *
  338. * @param[in] p_instance Pointer to the driver instance structure.
  339. * @param[in] seq_id Identifier of the sequence (0 or 1).
  340. * @param[in] repeats New number of repeats.
  341. */
  342. __STATIC_INLINE void nrfx_pwm_sequence_repeats_update(nrfx_pwm_t const * const p_instance,
  343. uint8_t seq_id,
  344. uint32_t repeats);
  345. /**
  346. * @brief Function for updating the additional delay after the specified
  347. * sequence during playback.
  348. *
  349. * @param[in] p_instance Pointer to the driver instance structure.
  350. * @param[in] seq_id Identifier of the sequence (0 or 1).
  351. * @param[in] end_delay New end delay value (in PWM periods).
  352. */
  353. __STATIC_INLINE void nrfx_pwm_sequence_end_delay_update(nrfx_pwm_t const * const p_instance,
  354. uint8_t seq_id,
  355. uint32_t end_delay);
  356. /**
  357. * @brief Function for returning the address of a specified PWM task that can
  358. * be used in PPI module.
  359. *
  360. * @param[in] p_instance Pointer to the driver instance structure.
  361. * @param[in] task Requested task.
  362. *
  363. * @return Task address.
  364. */
  365. __STATIC_INLINE uint32_t nrfx_pwm_task_address_get(nrfx_pwm_t const * const p_instance,
  366. nrf_pwm_task_t task);
  367. /**
  368. * @brief Function for returning the address of a specified PWM event that can
  369. * be used in PPI module.
  370. *
  371. * @param[in] p_instance Pointer to the driver instance structure.
  372. * @param[in] event Requested event.
  373. *
  374. * @return Event address.
  375. */
  376. __STATIC_INLINE uint32_t nrfx_pwm_event_address_get(nrfx_pwm_t const * const p_instance,
  377. nrf_pwm_event_t event);
  378. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  379. __STATIC_INLINE void nrfx_pwm_step(nrfx_pwm_t const * const p_instance)
  380. {
  381. nrf_pwm_task_trigger(p_instance->p_registers, NRF_PWM_TASK_NEXTSTEP);
  382. }
  383. __STATIC_INLINE void nrfx_pwm_sequence_update(nrfx_pwm_t const * const p_instance,
  384. uint8_t seq_id,
  385. nrf_pwm_sequence_t const * p_sequence)
  386. {
  387. nrf_pwm_sequence_set(p_instance->p_registers, seq_id, p_sequence);
  388. }
  389. __STATIC_INLINE void nrfx_pwm_sequence_values_update(nrfx_pwm_t const * const p_instance,
  390. uint8_t seq_id,
  391. nrf_pwm_values_t values)
  392. {
  393. nrf_pwm_seq_ptr_set(p_instance->p_registers, seq_id, values.p_raw);
  394. }
  395. __STATIC_INLINE void nrfx_pwm_sequence_length_update(nrfx_pwm_t const * const p_instance,
  396. uint8_t seq_id,
  397. uint16_t length)
  398. {
  399. nrf_pwm_seq_cnt_set(p_instance->p_registers, seq_id, length);
  400. }
  401. __STATIC_INLINE void nrfx_pwm_sequence_repeats_update(nrfx_pwm_t const * const p_instance,
  402. uint8_t seq_id,
  403. uint32_t repeats)
  404. {
  405. nrf_pwm_seq_refresh_set(p_instance->p_registers, seq_id, repeats);
  406. }
  407. __STATIC_INLINE void nrfx_pwm_sequence_end_delay_update(nrfx_pwm_t const * const p_instance,
  408. uint8_t seq_id,
  409. uint32_t end_delay)
  410. {
  411. nrf_pwm_seq_end_delay_set(p_instance->p_registers, seq_id, end_delay);
  412. }
  413. __STATIC_INLINE uint32_t nrfx_pwm_task_address_get(nrfx_pwm_t const * const p_instance,
  414. nrf_pwm_task_t task)
  415. {
  416. return nrf_pwm_task_address_get(p_instance->p_registers, task);
  417. }
  418. __STATIC_INLINE uint32_t nrfx_pwm_event_address_get(nrfx_pwm_t const * const p_instance,
  419. nrf_pwm_event_t event)
  420. {
  421. return nrf_pwm_event_address_get(p_instance->p_registers, event);
  422. }
  423. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  424. /** @} */
  425. void nrfx_pwm_0_irq_handler(void);
  426. void nrfx_pwm_1_irq_handler(void);
  427. void nrfx_pwm_2_irq_handler(void);
  428. void nrfx_pwm_3_irq_handler(void);
  429. #ifdef __cplusplus
  430. }
  431. #endif
  432. #endif // NRFX_PWM_H__