nrf_pwm.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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 NRF_PWM_H__
  41. #define NRF_PWM_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_pwm_hal PWM HAL
  48. * @{
  49. * @ingroup nrf_pwm
  50. * @brief Hardware access layer for managing the Pulse Width Modulation (PWM) peripheral.
  51. */
  52. /**
  53. * @brief This value can be provided as a parameter for the @ref nrf_pwm_pins_set
  54. * function call to specify that a given output channel shall not be
  55. * connected to a physical pin.
  56. */
  57. #define NRF_PWM_PIN_NOT_CONNECTED 0xFFFFFFFF
  58. /** @brief Number of channels in each PWM instance. */
  59. #define NRF_PWM_CHANNEL_COUNT 4
  60. /**
  61. * @brief Helper macro for calculating the number of 16-bit values in the specified
  62. * array of duty cycle values.
  63. */
  64. #define NRF_PWM_VALUES_LENGTH(array) (sizeof(array) / sizeof(uint16_t))
  65. /** @brief PWM tasks. */
  66. typedef enum
  67. {
  68. NRF_PWM_TASK_STOP = offsetof(NRF_PWM_Type, TASKS_STOP), ///< Stops PWM pulse generation on all channels at the end of the current PWM period, and stops the sequence playback.
  69. NRF_PWM_TASK_SEQSTART0 = offsetof(NRF_PWM_Type, TASKS_SEQSTART[0]), ///< Starts playback of sequence 0.
  70. NRF_PWM_TASK_SEQSTART1 = offsetof(NRF_PWM_Type, TASKS_SEQSTART[1]), ///< Starts playback of sequence 1.
  71. NRF_PWM_TASK_NEXTSTEP = offsetof(NRF_PWM_Type, TASKS_NEXTSTEP) ///< Steps by one value in the current sequence if the decoder is set to @ref NRF_PWM_STEP_TRIGGERED mode.
  72. } nrf_pwm_task_t;
  73. /** @brief PWM events. */
  74. typedef enum
  75. {
  76. NRF_PWM_EVENT_STOPPED = offsetof(NRF_PWM_Type, EVENTS_STOPPED), ///< Response to STOP task, emitted when PWM pulses are no longer generated.
  77. NRF_PWM_EVENT_SEQSTARTED0 = offsetof(NRF_PWM_Type, EVENTS_SEQSTARTED[0]), ///< First PWM period started on sequence 0.
  78. NRF_PWM_EVENT_SEQSTARTED1 = offsetof(NRF_PWM_Type, EVENTS_SEQSTARTED[1]), ///< First PWM period started on sequence 1.
  79. NRF_PWM_EVENT_SEQEND0 = offsetof(NRF_PWM_Type, EVENTS_SEQEND[0]), ///< Emitted at the end of every sequence 0 when its last value has been read from RAM.
  80. NRF_PWM_EVENT_SEQEND1 = offsetof(NRF_PWM_Type, EVENTS_SEQEND[1]), ///< Emitted at the end of every sequence 1 when its last value has been read from RAM.
  81. NRF_PWM_EVENT_PWMPERIODEND = offsetof(NRF_PWM_Type, EVENTS_PWMPERIODEND), ///< Emitted at the end of each PWM period.
  82. NRF_PWM_EVENT_LOOPSDONE = offsetof(NRF_PWM_Type, EVENTS_LOOPSDONE) ///< Concatenated sequences have been played the specified number of times.
  83. } nrf_pwm_event_t;
  84. /** @brief PWM interrupts. */
  85. typedef enum
  86. {
  87. NRF_PWM_INT_STOPPED_MASK = PWM_INTENSET_STOPPED_Msk, ///< Interrupt on STOPPED event.
  88. NRF_PWM_INT_SEQSTARTED0_MASK = PWM_INTENSET_SEQSTARTED0_Msk, ///< Interrupt on SEQSTARTED[0] event.
  89. NRF_PWM_INT_SEQSTARTED1_MASK = PWM_INTENSET_SEQSTARTED1_Msk, ///< Interrupt on SEQSTARTED[1] event.
  90. NRF_PWM_INT_SEQEND0_MASK = PWM_INTENSET_SEQEND0_Msk, ///< Interrupt on SEQEND[0] event.
  91. NRF_PWM_INT_SEQEND1_MASK = PWM_INTENSET_SEQEND1_Msk, ///< Interrupt on SEQEND[1] event.
  92. NRF_PWM_INT_PWMPERIODEND_MASK = PWM_INTENSET_PWMPERIODEND_Msk, ///< Interrupt on PWMPERIODEND event.
  93. NRF_PWM_INT_LOOPSDONE_MASK = PWM_INTENSET_LOOPSDONE_Msk ///< Interrupt on LOOPSDONE event.
  94. } nrf_pwm_int_mask_t;
  95. /** @brief PWM shortcuts. */
  96. typedef enum
  97. {
  98. NRF_PWM_SHORT_SEQEND0_STOP_MASK = PWM_SHORTS_SEQEND0_STOP_Msk, ///< Shortcut between SEQEND[0] event and STOP task.
  99. NRF_PWM_SHORT_SEQEND1_STOP_MASK = PWM_SHORTS_SEQEND1_STOP_Msk, ///< Shortcut between SEQEND[1] event and STOP task.
  100. NRF_PWM_SHORT_LOOPSDONE_SEQSTART0_MASK = PWM_SHORTS_LOOPSDONE_SEQSTART0_Msk, ///< Shortcut between LOOPSDONE event and SEQSTART[0] task.
  101. NRF_PWM_SHORT_LOOPSDONE_SEQSTART1_MASK = PWM_SHORTS_LOOPSDONE_SEQSTART1_Msk, ///< Shortcut between LOOPSDONE event and SEQSTART[1] task.
  102. NRF_PWM_SHORT_LOOPSDONE_STOP_MASK = PWM_SHORTS_LOOPSDONE_STOP_Msk ///< Shortcut between LOOPSDONE event and STOP task.
  103. } nrf_pwm_short_mask_t;
  104. /** @brief PWM modes of operation. */
  105. typedef enum
  106. {
  107. NRF_PWM_MODE_UP = PWM_MODE_UPDOWN_Up, ///< Up counter (edge-aligned PWM duty cycle).
  108. NRF_PWM_MODE_UP_AND_DOWN = PWM_MODE_UPDOWN_UpAndDown, ///< Up and down counter (center-aligned PWM duty cycle).
  109. } nrf_pwm_mode_t;
  110. /** @brief PWM base clock frequencies. */
  111. typedef enum
  112. {
  113. NRF_PWM_CLK_16MHz = PWM_PRESCALER_PRESCALER_DIV_1, ///< 16 MHz / 1 = 16 MHz.
  114. NRF_PWM_CLK_8MHz = PWM_PRESCALER_PRESCALER_DIV_2, ///< 16 MHz / 2 = 8 MHz.
  115. NRF_PWM_CLK_4MHz = PWM_PRESCALER_PRESCALER_DIV_4, ///< 16 MHz / 4 = 4 MHz.
  116. NRF_PWM_CLK_2MHz = PWM_PRESCALER_PRESCALER_DIV_8, ///< 16 MHz / 8 = 2 MHz.
  117. NRF_PWM_CLK_1MHz = PWM_PRESCALER_PRESCALER_DIV_16, ///< 16 MHz / 16 = 1 MHz.
  118. NRF_PWM_CLK_500kHz = PWM_PRESCALER_PRESCALER_DIV_32, ///< 16 MHz / 32 = 500 kHz.
  119. NRF_PWM_CLK_250kHz = PWM_PRESCALER_PRESCALER_DIV_64, ///< 16 MHz / 64 = 250 kHz.
  120. NRF_PWM_CLK_125kHz = PWM_PRESCALER_PRESCALER_DIV_128 ///< 16 MHz / 128 = 125 kHz.
  121. } nrf_pwm_clk_t;
  122. /**
  123. * @brief PWM decoder load modes.
  124. *
  125. * The selected mode determines how the sequence data is read from RAM and
  126. * spread to the compare registers.
  127. */
  128. typedef enum
  129. {
  130. NRF_PWM_LOAD_COMMON = PWM_DECODER_LOAD_Common, ///< 1st half word (16-bit) used in all PWM channels (0-3).
  131. NRF_PWM_LOAD_GROUPED = PWM_DECODER_LOAD_Grouped, ///< 1st half word (16-bit) used in channels 0 and 1; 2nd word in channels 2 and 3.
  132. NRF_PWM_LOAD_INDIVIDUAL = PWM_DECODER_LOAD_Individual, ///< 1st half word (16-bit) used in channel 0; 2nd in channel 1; 3rd in channel 2; 4th in channel 3.
  133. NRF_PWM_LOAD_WAVE_FORM = PWM_DECODER_LOAD_WaveForm ///< 1st half word (16-bit) used in channel 0; 2nd in channel 1; ... ; 4th as the top value for the pulse generator counter.
  134. } nrf_pwm_dec_load_t;
  135. /**
  136. * @brief PWM decoder next step modes.
  137. *
  138. * The selected mode determines when the next value from the active sequence
  139. * is loaded.
  140. */
  141. typedef enum
  142. {
  143. NRF_PWM_STEP_AUTO = PWM_DECODER_MODE_RefreshCount, ///< Automatically after the current value is played and repeated the requested number of times.
  144. NRF_PWM_STEP_TRIGGERED = PWM_DECODER_MODE_NextStep ///< When the @ref NRF_PWM_TASK_NEXTSTEP task is triggered.
  145. } nrf_pwm_dec_step_t;
  146. /**
  147. * @brief Type used for defining duty cycle values for a sequence
  148. * loaded in @ref NRF_PWM_LOAD_COMMON mode.
  149. */
  150. typedef uint16_t nrf_pwm_values_common_t;
  151. /**
  152. * @brief Structure for defining duty cycle values for a sequence
  153. * loaded in @ref NRF_PWM_LOAD_GROUPED mode.
  154. */
  155. typedef struct {
  156. uint16_t group_0; ///< Duty cycle value for group 0 (channels 0 and 1).
  157. uint16_t group_1; ///< Duty cycle value for group 1 (channels 2 and 3).
  158. } nrf_pwm_values_grouped_t;
  159. /**
  160. * @brief Structure for defining duty cycle values for a sequence
  161. * loaded in @ref NRF_PWM_LOAD_INDIVIDUAL mode.
  162. */
  163. typedef struct
  164. {
  165. uint16_t channel_0; ///< Duty cycle value for channel 0.
  166. uint16_t channel_1; ///< Duty cycle value for channel 1.
  167. uint16_t channel_2; ///< Duty cycle value for channel 2.
  168. uint16_t channel_3; ///< Duty cycle value for channel 3.
  169. } nrf_pwm_values_individual_t;
  170. /**
  171. * @brief Structure for defining duty cycle values for a sequence
  172. * loaded in @ref NRF_PWM_LOAD_WAVE_FORM mode.
  173. */
  174. typedef struct {
  175. uint16_t channel_0; ///< Duty cycle value for channel 0.
  176. uint16_t channel_1; ///< Duty cycle value for channel 1.
  177. uint16_t channel_2; ///< Duty cycle value for channel 2.
  178. uint16_t counter_top; ///< Top value for the pulse generator counter.
  179. } nrf_pwm_values_wave_form_t;
  180. /**
  181. * @brief Union grouping pointers to arrays of duty cycle values applicable to
  182. * various loading modes.
  183. */
  184. typedef union {
  185. nrf_pwm_values_common_t const * p_common; ///< Pointer to be used in @ref NRF_PWM_LOAD_COMMON mode.
  186. nrf_pwm_values_grouped_t const * p_grouped; ///< Pointer to be used in @ref NRF_PWM_LOAD_GROUPED mode.
  187. nrf_pwm_values_individual_t const * p_individual; ///< Pointer to be used in @ref NRF_PWM_LOAD_INDIVIDUAL mode.
  188. nrf_pwm_values_wave_form_t const * p_wave_form; ///< Pointer to be used in @ref NRF_PWM_LOAD_WAVE_FORM mode.
  189. uint16_t const * p_raw; ///< Pointer providing raw access to the values.
  190. } nrf_pwm_values_t;
  191. /**
  192. * @brief Structure for defining a sequence of PWM duty cycles.
  193. *
  194. * When the sequence is set (by a call to @ref nrf_pwm_sequence_set), the
  195. * provided duty cycle values are not copied. The @p values pointer is stored
  196. * in the internal register of the peripheral, and the values are loaded from RAM
  197. * during the sequence playback. Therefore, you must ensure that the values
  198. * do not change before and during the sequence playback (for example,
  199. * the values cannot be placed in a local variable that is allocated on stack).
  200. * If the sequence is played in a loop and the values are to be updated
  201. * before the next iteration, it is safe to modify them when the corresponding
  202. * event signaling the end of sequence occurs (@ref NRF_PWM_EVENT_SEQEND0
  203. * or @ref NRF_PWM_EVENT_SEQEND1, respectively).
  204. *
  205. * @note The @p repeats and @p end_delay values (which are written to the
  206. * SEQ[n].REFRESH and SEQ[n].ENDDELAY registers in the peripheral,
  207. * respectively) are ignored at the end of a complex sequence
  208. * playback, indicated by the LOOPSDONE event.
  209. * See the @linkProductSpecification52 for more information.
  210. */
  211. typedef struct
  212. {
  213. nrf_pwm_values_t values; ///< Pointer to an array with duty cycle values. This array must be in Data RAM.
  214. /**< This field is defined as an union of pointers
  215. * to provide a convenient way to define duty
  216. * cycle values in various loading modes
  217. * (see @ref nrf_pwm_dec_load_t).
  218. * In each value, the most significant bit (15)
  219. * determines the polarity of the output and the
  220. * others (14-0) compose the 15-bit value to be
  221. * compared with the pulse generator counter. */
  222. uint16_t length; ///< Number of 16-bit values in the array pointed by @p values.
  223. uint32_t repeats; ///< Number of times that each duty cycle is to be repeated (after being played once). Ignored in @ref NRF_PWM_STEP_TRIGGERED mode.
  224. uint32_t end_delay; ///< Additional time (in PWM periods) that the last duty cycle is to be kept after the sequence is played. Ignored in @ref NRF_PWM_STEP_TRIGGERED mode.
  225. } nrf_pwm_sequence_t;
  226. /**
  227. * @brief Function for activating the specified PWM task.
  228. *
  229. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  230. * @param[in] task Task to be activated.
  231. */
  232. __STATIC_INLINE void nrf_pwm_task_trigger(NRF_PWM_Type * p_reg,
  233. nrf_pwm_task_t task);
  234. /**
  235. * @brief Function for getting the address of the specified PWM task register.
  236. *
  237. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  238. * @param[in] task PWM task.
  239. *
  240. * @return Address of the specified task register.
  241. */
  242. __STATIC_INLINE uint32_t nrf_pwm_task_address_get(NRF_PWM_Type const * p_reg,
  243. nrf_pwm_task_t task);
  244. /**
  245. * @brief Function for clearing the specified PWM event.
  246. *
  247. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  248. * @param[in] event Event to clear.
  249. */
  250. __STATIC_INLINE void nrf_pwm_event_clear(NRF_PWM_Type * p_reg,
  251. nrf_pwm_event_t event);
  252. /**
  253. * @brief Function for retrieving the state of the PWM event.
  254. *
  255. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  256. * @param[in] event Event to be checked.
  257. *
  258. * @retval true The event has been generated.
  259. * @retval false The event has not been generated.
  260. */
  261. __STATIC_INLINE bool nrf_pwm_event_check(NRF_PWM_Type const * p_reg,
  262. nrf_pwm_event_t event);
  263. /**
  264. * @brief Function for getting the address of the specified PWM event register.
  265. *
  266. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  267. * @param[in] event PWM event.
  268. *
  269. * @return Address of the specified event register.
  270. */
  271. __STATIC_INLINE uint32_t nrf_pwm_event_address_get(NRF_PWM_Type const * p_reg,
  272. nrf_pwm_event_t event);
  273. /**
  274. * @brief Function for enabling the specified shortcuts.
  275. *
  276. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  277. * @param[in] mask Mask of shortcuts to be enabled.
  278. */
  279. __STATIC_INLINE void nrf_pwm_shorts_enable(NRF_PWM_Type * p_reg,
  280. uint32_t mask);
  281. /**
  282. * @brief Function for disabling the specified shortcuts.
  283. *
  284. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  285. * @param[in] mask Mask of shortcuts to be disabled.
  286. */
  287. __STATIC_INLINE void nrf_pwm_shorts_disable(NRF_PWM_Type * p_reg,
  288. uint32_t mask);
  289. /**
  290. * @brief Function for setting the configuration of PWM shortcuts.
  291. *
  292. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  293. * @param[in] mask Shortcuts configuration to be set.
  294. */
  295. __STATIC_INLINE void nrf_pwm_shorts_set(NRF_PWM_Type * p_reg,
  296. uint32_t mask);
  297. /**
  298. * @brief Function for enabling specified interrupts.
  299. *
  300. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  301. * @param[in] mask Mask of interrupts to be enabled.
  302. */
  303. __STATIC_INLINE void nrf_pwm_int_enable(NRF_PWM_Type * p_reg,
  304. uint32_t mask);
  305. /**
  306. * @brief Function for disabling specified interrupts.
  307. *
  308. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  309. * @param[in] mask Mask of interrupts to be disabled.
  310. */
  311. __STATIC_INLINE void nrf_pwm_int_disable(NRF_PWM_Type * p_reg,
  312. uint32_t mask);
  313. /**
  314. * @brief Function for setting the configuration of PWM interrupts.
  315. *
  316. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  317. * @param[in] mask Mask of interrupts to be set.
  318. */
  319. __STATIC_INLINE void nrf_pwm_int_set(NRF_PWM_Type * p_reg,
  320. uint32_t mask);
  321. /**
  322. * @brief Function for retrieving the state of a given interrupt.
  323. *
  324. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  325. * @param[in] pwm_int Interrupt to be checked.
  326. *
  327. * @retval true The interrupt is enabled.
  328. * @retval false The interrupt is not enabled.
  329. */
  330. __STATIC_INLINE bool nrf_pwm_int_enable_check(NRF_PWM_Type const * p_reg,
  331. nrf_pwm_int_mask_t pwm_int);
  332. #if defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  333. /**
  334. * @brief Function for setting the subscribe configuration for a given
  335. * PWM task.
  336. *
  337. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  338. * @param[in] task Task for which to set the configuration.
  339. * @param[in] channel Channel through which to subscribe events.
  340. */
  341. __STATIC_INLINE void nrf_pwm_subscribe_set(NRF_PWM_Type * p_reg,
  342. nrf_pwm_task_t task,
  343. uint8_t channel);
  344. /**
  345. * @brief Function for clearing the subscribe configuration for a given
  346. * PWM task.
  347. *
  348. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  349. * @param[in] task Task for which to clear the configuration.
  350. */
  351. __STATIC_INLINE void nrf_pwm_subscribe_clear(NRF_PWM_Type * p_reg,
  352. nrf_pwm_task_t task);
  353. /**
  354. * @brief Function for setting the publish configuration for a given
  355. * PWM event.
  356. *
  357. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  358. * @param[in] event Event for which to set the configuration.
  359. * @param[in] channel Channel through which to publish the event.
  360. */
  361. __STATIC_INLINE void nrf_pwm_publish_set(NRF_PWM_Type * p_reg,
  362. nrf_pwm_event_t event,
  363. uint8_t channel);
  364. /**
  365. * @brief Function for clearing the publish configuration for a given
  366. * PWM event.
  367. *
  368. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  369. * @param[in] event Event for which to clear the configuration.
  370. */
  371. __STATIC_INLINE void nrf_pwm_publish_clear(NRF_PWM_Type * p_reg,
  372. nrf_pwm_event_t event);
  373. #endif // defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  374. /**
  375. * @brief Function for enabling the PWM peripheral.
  376. *
  377. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  378. */
  379. __STATIC_INLINE void nrf_pwm_enable(NRF_PWM_Type * p_reg);
  380. /**
  381. * @brief Function for disabling the PWM peripheral.
  382. *
  383. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  384. */
  385. __STATIC_INLINE void nrf_pwm_disable(NRF_PWM_Type * p_reg);
  386. /**
  387. * @brief Function for assigning pins to PWM output channels.
  388. *
  389. * Usage of all PWM output channels is optional. If a given channel is not
  390. * needed, pass the @ref NRF_PWM_PIN_NOT_CONNECTED value instead of its pin
  391. * number.
  392. *
  393. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  394. * @param[in] out_pins Array with pin numbers for individual PWM output channels.
  395. */
  396. __STATIC_INLINE void nrf_pwm_pins_set(NRF_PWM_Type * p_reg,
  397. uint32_t out_pins[NRF_PWM_CHANNEL_COUNT]);
  398. /**
  399. * @brief Function for configuring the PWM peripheral.
  400. *
  401. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  402. * @param[in] base_clock Base clock frequency.
  403. * @param[in] mode Operating mode of the pulse generator counter.
  404. * @param[in] top_value Value up to which the pulse generator counter counts.
  405. */
  406. __STATIC_INLINE void nrf_pwm_configure(NRF_PWM_Type * p_reg,
  407. nrf_pwm_clk_t base_clock,
  408. nrf_pwm_mode_t mode,
  409. uint16_t top_value);
  410. /**
  411. * @brief Function for defining a sequence of PWM duty cycles.
  412. *
  413. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  414. * @param[in] seq_id Identifier of the sequence (0 or 1).
  415. * @param[in] p_seq Pointer to the sequence definition.
  416. */
  417. __STATIC_INLINE void nrf_pwm_sequence_set(NRF_PWM_Type * p_reg,
  418. uint8_t seq_id,
  419. nrf_pwm_sequence_t const * p_seq);
  420. /**
  421. * @brief Function for modifying the pointer to the duty cycle values
  422. * in the specified sequence.
  423. *
  424. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  425. * @param[in] seq_id Identifier of the sequence (0 or 1).
  426. * @param[in] p_values Pointer to an array with duty cycle values.
  427. */
  428. __STATIC_INLINE void nrf_pwm_seq_ptr_set(NRF_PWM_Type * p_reg,
  429. uint8_t seq_id,
  430. uint16_t const * p_values);
  431. /**
  432. * @brief Function for modifying the total number of duty cycle values
  433. * in the specified sequence.
  434. *
  435. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  436. * @param[in] seq_id Identifier of the sequence (0 or 1).
  437. * @param[in] length Number of duty cycle values.
  438. */
  439. __STATIC_INLINE void nrf_pwm_seq_cnt_set(NRF_PWM_Type * p_reg,
  440. uint8_t seq_id,
  441. uint16_t length);
  442. /**
  443. * @brief Function for modifying the additional number of PWM periods spent
  444. * on each duty cycle value in the specified sequence.
  445. *
  446. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  447. * @param[in] seq_id Identifier of the sequence (0 or 1).
  448. * @param[in] refresh Number of additional PWM periods for each duty cycle value.
  449. */
  450. __STATIC_INLINE void nrf_pwm_seq_refresh_set(NRF_PWM_Type * p_reg,
  451. uint8_t seq_id,
  452. uint32_t refresh);
  453. /**
  454. * @brief Function for modifying the additional time added after the sequence
  455. * is played.
  456. *
  457. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  458. * @param[in] seq_id Identifier of the sequence (0 or 1).
  459. * @param[in] end_delay Number of PWM periods added at the end of the sequence.
  460. */
  461. __STATIC_INLINE void nrf_pwm_seq_end_delay_set(NRF_PWM_Type * p_reg,
  462. uint8_t seq_id,
  463. uint32_t end_delay);
  464. /**
  465. * @brief Function for setting the mode of loading sequence data from RAM
  466. * and advancing the sequence.
  467. *
  468. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  469. * @param[in] dec_load Mode of loading sequence data from RAM.
  470. * @param[in] dec_step Mode of advancing the active sequence.
  471. */
  472. __STATIC_INLINE void nrf_pwm_decoder_set(NRF_PWM_Type * p_reg,
  473. nrf_pwm_dec_load_t dec_load,
  474. nrf_pwm_dec_step_t dec_step);
  475. /**
  476. * @brief Function for setting the number of times the sequence playback
  477. * should be performed.
  478. *
  479. * This function applies to two-sequence playback (concatenated sequence 0 and 1).
  480. * A single sequence can be played back only once.
  481. *
  482. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  483. * @param[in] loop_count Number of times to perform the sequence playback.
  484. */
  485. __STATIC_INLINE void nrf_pwm_loop_set(NRF_PWM_Type * p_reg,
  486. uint16_t loop_count);
  487. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  488. __STATIC_INLINE void nrf_pwm_task_trigger(NRF_PWM_Type * p_reg,
  489. nrf_pwm_task_t task)
  490. {
  491. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
  492. }
  493. __STATIC_INLINE uint32_t nrf_pwm_task_address_get(NRF_PWM_Type const * p_reg,
  494. nrf_pwm_task_t task)
  495. {
  496. return ((uint32_t)p_reg + (uint32_t)task);
  497. }
  498. __STATIC_INLINE void nrf_pwm_event_clear(NRF_PWM_Type * p_reg,
  499. nrf_pwm_event_t event)
  500. {
  501. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
  502. #if __CORTEX_M == 0x04
  503. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
  504. (void)dummy;
  505. #endif
  506. }
  507. __STATIC_INLINE bool nrf_pwm_event_check(NRF_PWM_Type const * p_reg,
  508. nrf_pwm_event_t event)
  509. {
  510. return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  511. }
  512. __STATIC_INLINE uint32_t nrf_pwm_event_address_get(NRF_PWM_Type const * p_reg,
  513. nrf_pwm_event_t event)
  514. {
  515. return ((uint32_t)p_reg + (uint32_t)event);
  516. }
  517. __STATIC_INLINE void nrf_pwm_shorts_enable(NRF_PWM_Type * p_reg,
  518. uint32_t mask)
  519. {
  520. p_reg->SHORTS |= mask;
  521. }
  522. __STATIC_INLINE void nrf_pwm_shorts_disable(NRF_PWM_Type * p_reg,
  523. uint32_t mask)
  524. {
  525. p_reg->SHORTS &= ~(mask);
  526. }
  527. __STATIC_INLINE void nrf_pwm_shorts_set(NRF_PWM_Type * p_reg,
  528. uint32_t mask)
  529. {
  530. p_reg->SHORTS = mask;
  531. }
  532. __STATIC_INLINE void nrf_pwm_int_enable(NRF_PWM_Type * p_reg,
  533. uint32_t mask)
  534. {
  535. p_reg->INTENSET = mask;
  536. }
  537. __STATIC_INLINE void nrf_pwm_int_disable(NRF_PWM_Type * p_reg,
  538. uint32_t mask)
  539. {
  540. p_reg->INTENCLR = mask;
  541. }
  542. __STATIC_INLINE void nrf_pwm_int_set(NRF_PWM_Type * p_reg,
  543. uint32_t mask)
  544. {
  545. p_reg->INTEN = mask;
  546. }
  547. __STATIC_INLINE bool nrf_pwm_int_enable_check(NRF_PWM_Type const * p_reg,
  548. nrf_pwm_int_mask_t pwm_int)
  549. {
  550. return (bool)(p_reg->INTENSET & pwm_int);
  551. }
  552. #if defined(DPPI_PRESENT)
  553. __STATIC_INLINE void nrf_pwm_subscribe_set(NRF_PWM_Type * p_reg,
  554. nrf_pwm_task_t task,
  555. uint8_t channel)
  556. {
  557. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) =
  558. ((uint32_t)channel | PWM_SUBSCRIBE_STOP_EN_Msk);
  559. }
  560. __STATIC_INLINE void nrf_pwm_subscribe_clear(NRF_PWM_Type * p_reg,
  561. nrf_pwm_task_t task)
  562. {
  563. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) = 0;
  564. }
  565. __STATIC_INLINE void nrf_pwm_publish_set(NRF_PWM_Type * p_reg,
  566. nrf_pwm_event_t event,
  567. uint8_t channel)
  568. {
  569. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) =
  570. ((uint32_t)channel | PWM_PUBLISH_STOPPED_EN_Msk);
  571. }
  572. __STATIC_INLINE void nrf_pwm_publish_clear(NRF_PWM_Type * p_reg,
  573. nrf_pwm_event_t event)
  574. {
  575. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) = 0;
  576. }
  577. #endif // defined(DPPI_PRESENT)
  578. __STATIC_INLINE void nrf_pwm_enable(NRF_PWM_Type * p_reg)
  579. {
  580. p_reg->ENABLE = (PWM_ENABLE_ENABLE_Enabled << PWM_ENABLE_ENABLE_Pos);
  581. }
  582. __STATIC_INLINE void nrf_pwm_disable(NRF_PWM_Type * p_reg)
  583. {
  584. p_reg->ENABLE = (PWM_ENABLE_ENABLE_Disabled << PWM_ENABLE_ENABLE_Pos);
  585. }
  586. __STATIC_INLINE void nrf_pwm_pins_set(NRF_PWM_Type * p_reg,
  587. uint32_t out_pins[NRF_PWM_CHANNEL_COUNT])
  588. {
  589. uint8_t i;
  590. for (i = 0; i < NRF_PWM_CHANNEL_COUNT; ++i)
  591. {
  592. p_reg->PSEL.OUT[i] = out_pins[i];
  593. }
  594. }
  595. __STATIC_INLINE void nrf_pwm_configure(NRF_PWM_Type * p_reg,
  596. nrf_pwm_clk_t base_clock,
  597. nrf_pwm_mode_t mode,
  598. uint16_t top_value)
  599. {
  600. NRFX_ASSERT(top_value <= PWM_COUNTERTOP_COUNTERTOP_Msk);
  601. p_reg->PRESCALER = base_clock;
  602. p_reg->MODE = mode;
  603. p_reg->COUNTERTOP = top_value;
  604. }
  605. __STATIC_INLINE void nrf_pwm_sequence_set(NRF_PWM_Type * p_reg,
  606. uint8_t seq_id,
  607. nrf_pwm_sequence_t const * p_seq)
  608. {
  609. NRFX_ASSERT(p_seq != NULL);
  610. nrf_pwm_seq_ptr_set( p_reg, seq_id, p_seq->values.p_raw);
  611. nrf_pwm_seq_cnt_set( p_reg, seq_id, p_seq->length);
  612. nrf_pwm_seq_refresh_set( p_reg, seq_id, p_seq->repeats);
  613. nrf_pwm_seq_end_delay_set(p_reg, seq_id, p_seq->end_delay);
  614. }
  615. __STATIC_INLINE void nrf_pwm_seq_ptr_set(NRF_PWM_Type * p_reg,
  616. uint8_t seq_id,
  617. uint16_t const * p_values)
  618. {
  619. NRFX_ASSERT(seq_id <= 1);
  620. NRFX_ASSERT(p_values != NULL);
  621. p_reg->SEQ[seq_id].PTR = (uint32_t)p_values;
  622. }
  623. __STATIC_INLINE void nrf_pwm_seq_cnt_set(NRF_PWM_Type * p_reg,
  624. uint8_t seq_id,
  625. uint16_t length)
  626. {
  627. NRFX_ASSERT(seq_id <= 1);
  628. NRFX_ASSERT(length != 0);
  629. NRFX_ASSERT(length <= PWM_SEQ_CNT_CNT_Msk);
  630. p_reg->SEQ[seq_id].CNT = length;
  631. }
  632. __STATIC_INLINE void nrf_pwm_seq_refresh_set(NRF_PWM_Type * p_reg,
  633. uint8_t seq_id,
  634. uint32_t refresh)
  635. {
  636. NRFX_ASSERT(seq_id <= 1);
  637. NRFX_ASSERT(refresh <= PWM_SEQ_REFRESH_CNT_Msk);
  638. p_reg->SEQ[seq_id].REFRESH = refresh;
  639. }
  640. __STATIC_INLINE void nrf_pwm_seq_end_delay_set(NRF_PWM_Type * p_reg,
  641. uint8_t seq_id,
  642. uint32_t end_delay)
  643. {
  644. NRFX_ASSERT(seq_id <= 1);
  645. NRFX_ASSERT(end_delay <= PWM_SEQ_ENDDELAY_CNT_Msk);
  646. p_reg->SEQ[seq_id].ENDDELAY = end_delay;
  647. }
  648. __STATIC_INLINE void nrf_pwm_decoder_set(NRF_PWM_Type * p_reg,
  649. nrf_pwm_dec_load_t dec_load,
  650. nrf_pwm_dec_step_t dec_step)
  651. {
  652. p_reg->DECODER = ((uint32_t)dec_load << PWM_DECODER_LOAD_Pos) |
  653. ((uint32_t)dec_step << PWM_DECODER_MODE_Pos);
  654. }
  655. __STATIC_INLINE void nrf_pwm_loop_set(NRF_PWM_Type * p_reg,
  656. uint16_t loop_count)
  657. {
  658. p_reg->LOOP = loop_count;
  659. }
  660. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  661. /** @} */
  662. #ifdef __cplusplus
  663. }
  664. #endif
  665. #endif // NRF_PWM_H__