nrfx_pwm.h 21 KB

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