nrf_pwm.h 30 KB

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