nrf_pwm.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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 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. /**
  351. * @brief Function for enabling the PWM peripheral.
  352. *
  353. * @param[in] p_reg Pointer to the peripheral registers structure.
  354. */
  355. __STATIC_INLINE void nrf_pwm_enable(NRF_PWM_Type * p_reg);
  356. /**
  357. * @brief Function for disabling the PWM peripheral.
  358. *
  359. * @param[in] p_reg Pointer to the peripheral registers structure.
  360. */
  361. __STATIC_INLINE void nrf_pwm_disable(NRF_PWM_Type * p_reg);
  362. /**
  363. * @brief Function for assigning pins to PWM output channels.
  364. *
  365. * Usage of all PWM output channels is optional. If a given channel is not
  366. * needed, pass the @ref NRF_PWM_PIN_NOT_CONNECTED value instead of its pin
  367. * number.
  368. *
  369. * @param[in] p_reg Pointer to the peripheral registers structure.
  370. * @param[in] out_pins Array with pin numbers for individual PWM output channels.
  371. */
  372. __STATIC_INLINE void nrf_pwm_pins_set(NRF_PWM_Type * p_reg,
  373. uint32_t out_pins[NRF_PWM_CHANNEL_COUNT]);
  374. /**
  375. * @brief Function for configuring the PWM peripheral.
  376. *
  377. * @param[in] p_reg Pointer to the peripheral registers structure.
  378. * @param[in] base_clock Base clock frequency.
  379. * @param[in] mode Operating mode of the pulse generator counter.
  380. * @param[in] top_value Value up to which the pulse generator counter counts.
  381. */
  382. __STATIC_INLINE void nrf_pwm_configure(NRF_PWM_Type * p_reg,
  383. nrf_pwm_clk_t base_clock,
  384. nrf_pwm_mode_t mode,
  385. uint16_t top_value);
  386. /**
  387. * @brief Function for defining a sequence of PWM duty cycles.
  388. *
  389. * @param[in] p_reg Pointer to the peripheral registers structure.
  390. * @param[in] seq_id Identifier of the sequence (0 or 1).
  391. * @param[in] p_seq Pointer to the sequence definition.
  392. */
  393. __STATIC_INLINE void nrf_pwm_sequence_set(NRF_PWM_Type * p_reg,
  394. uint8_t seq_id,
  395. nrf_pwm_sequence_t const * p_seq);
  396. /**
  397. * @brief Function for modifying the pointer to the duty cycle values
  398. * in the specified sequence.
  399. *
  400. * @param[in] p_reg Pointer to the peripheral registers structure.
  401. * @param[in] seq_id Identifier of the sequence (0 or 1).
  402. * @param[in] p_values Pointer to an array with duty cycle values.
  403. */
  404. __STATIC_INLINE void nrf_pwm_seq_ptr_set(NRF_PWM_Type * p_reg,
  405. uint8_t seq_id,
  406. uint16_t const * p_values);
  407. /**
  408. * @brief Function for modifying the total number of duty cycle values
  409. * in the specified sequence.
  410. *
  411. * @param[in] p_reg Pointer to the peripheral registers structure.
  412. * @param[in] seq_id Identifier of the sequence (0 or 1).
  413. * @param[in] length Number of duty cycle values.
  414. */
  415. __STATIC_INLINE void nrf_pwm_seq_cnt_set(NRF_PWM_Type * p_reg,
  416. uint8_t seq_id,
  417. uint16_t length);
  418. /**
  419. * @brief Function for modifying the additional number of PWM periods spent
  420. * on each duty cycle value in the specified sequence.
  421. *
  422. * @param[in] p_reg Pointer to the peripheral registers structure.
  423. * @param[in] seq_id Identifier of the sequence (0 or 1).
  424. * @param[in] refresh Number of additional PWM periods for each duty cycle value.
  425. */
  426. __STATIC_INLINE void nrf_pwm_seq_refresh_set(NRF_PWM_Type * p_reg,
  427. uint8_t seq_id,
  428. uint32_t refresh);
  429. /**
  430. * @brief Function for modifying the additional time added after the sequence
  431. * is played.
  432. *
  433. * @param[in] p_reg Pointer to the peripheral registers structure.
  434. * @param[in] seq_id Identifier of the sequence (0 or 1).
  435. * @param[in] end_delay Number of PWM periods added at the end of the sequence.
  436. */
  437. __STATIC_INLINE void nrf_pwm_seq_end_delay_set(NRF_PWM_Type * p_reg,
  438. uint8_t seq_id,
  439. uint32_t end_delay);
  440. /**
  441. * @brief Function for setting the mode of loading sequence data from RAM
  442. * and advancing the sequence.
  443. *
  444. * @param[in] p_reg Pointer to the peripheral registers structure.
  445. * @param[in] dec_load Mode of loading sequence data from RAM.
  446. * @param[in] dec_step Mode of advancing the active sequence.
  447. */
  448. __STATIC_INLINE void nrf_pwm_decoder_set(NRF_PWM_Type * p_reg,
  449. nrf_pwm_dec_load_t dec_load,
  450. nrf_pwm_dec_step_t dec_step);
  451. /**
  452. * @brief Function for setting the number of times the sequence playback
  453. * should be performed.
  454. *
  455. * This function applies to two-sequence playback (concatenated sequence 0 and 1).
  456. * A single sequence can be played back only once.
  457. *
  458. * @param[in] p_reg Pointer to the peripheral registers structure.
  459. * @param[in] loop_count Number of times to perform the sequence playback.
  460. */
  461. __STATIC_INLINE void nrf_pwm_loop_set(NRF_PWM_Type * p_reg,
  462. uint16_t loop_count);
  463. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  464. __STATIC_INLINE void nrf_pwm_task_trigger(NRF_PWM_Type * p_reg,
  465. nrf_pwm_task_t task)
  466. {
  467. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
  468. }
  469. __STATIC_INLINE uint32_t nrf_pwm_task_address_get(NRF_PWM_Type const * p_reg,
  470. nrf_pwm_task_t task)
  471. {
  472. return ((uint32_t)p_reg + (uint32_t)task);
  473. }
  474. __STATIC_INLINE void nrf_pwm_event_clear(NRF_PWM_Type * p_reg,
  475. nrf_pwm_event_t event)
  476. {
  477. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
  478. #if __CORTEX_M == 0x04
  479. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
  480. (void)dummy;
  481. #endif
  482. }
  483. __STATIC_INLINE bool nrf_pwm_event_check(NRF_PWM_Type const * p_reg,
  484. nrf_pwm_event_t event)
  485. {
  486. return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  487. }
  488. __STATIC_INLINE uint32_t nrf_pwm_event_address_get(NRF_PWM_Type const * p_reg,
  489. nrf_pwm_event_t event)
  490. {
  491. return ((uint32_t)p_reg + (uint32_t)event);
  492. }
  493. __STATIC_INLINE void nrf_pwm_shorts_enable(NRF_PWM_Type * p_reg,
  494. uint32_t pwm_shorts_mask)
  495. {
  496. p_reg->SHORTS |= pwm_shorts_mask;
  497. }
  498. __STATIC_INLINE void nrf_pwm_shorts_disable(NRF_PWM_Type * p_reg,
  499. uint32_t pwm_shorts_mask)
  500. {
  501. p_reg->SHORTS &= ~(pwm_shorts_mask);
  502. }
  503. __STATIC_INLINE void nrf_pwm_shorts_set(NRF_PWM_Type * p_reg,
  504. uint32_t pwm_shorts_mask)
  505. {
  506. p_reg->SHORTS = pwm_shorts_mask;
  507. }
  508. __STATIC_INLINE void nrf_pwm_int_enable(NRF_PWM_Type * p_reg,
  509. uint32_t pwm_int_mask)
  510. {
  511. p_reg->INTENSET = pwm_int_mask;
  512. }
  513. __STATIC_INLINE void nrf_pwm_int_disable(NRF_PWM_Type * p_reg,
  514. uint32_t pwm_int_mask)
  515. {
  516. p_reg->INTENCLR = pwm_int_mask;
  517. }
  518. __STATIC_INLINE void nrf_pwm_int_set(NRF_PWM_Type * p_reg,
  519. uint32_t pwm_int_mask)
  520. {
  521. p_reg->INTEN = pwm_int_mask;
  522. }
  523. __STATIC_INLINE bool nrf_pwm_int_enable_check(NRF_PWM_Type const * p_reg,
  524. nrf_pwm_int_mask_t pwm_int)
  525. {
  526. return (bool)(p_reg->INTENSET & pwm_int);
  527. }
  528. __STATIC_INLINE void nrf_pwm_enable(NRF_PWM_Type * p_reg)
  529. {
  530. p_reg->ENABLE = (PWM_ENABLE_ENABLE_Enabled << PWM_ENABLE_ENABLE_Pos);
  531. }
  532. __STATIC_INLINE void nrf_pwm_disable(NRF_PWM_Type * p_reg)
  533. {
  534. p_reg->ENABLE = (PWM_ENABLE_ENABLE_Disabled << PWM_ENABLE_ENABLE_Pos);
  535. }
  536. __STATIC_INLINE void nrf_pwm_pins_set(NRF_PWM_Type * p_reg,
  537. uint32_t out_pins[NRF_PWM_CHANNEL_COUNT])
  538. {
  539. uint8_t i;
  540. for (i = 0; i < NRF_PWM_CHANNEL_COUNT; ++i)
  541. {
  542. p_reg->PSEL.OUT[i] = out_pins[i];
  543. }
  544. }
  545. __STATIC_INLINE void nrf_pwm_configure(NRF_PWM_Type * p_reg,
  546. nrf_pwm_clk_t base_clock,
  547. nrf_pwm_mode_t mode,
  548. uint16_t top_value)
  549. {
  550. NRFX_ASSERT(top_value <= PWM_COUNTERTOP_COUNTERTOP_Msk);
  551. p_reg->PRESCALER = base_clock;
  552. p_reg->MODE = mode;
  553. p_reg->COUNTERTOP = top_value;
  554. }
  555. __STATIC_INLINE void nrf_pwm_sequence_set(NRF_PWM_Type * p_reg,
  556. uint8_t seq_id,
  557. nrf_pwm_sequence_t const * p_seq)
  558. {
  559. NRFX_ASSERT(p_seq != NULL);
  560. nrf_pwm_seq_ptr_set( p_reg, seq_id, p_seq->values.p_raw);
  561. nrf_pwm_seq_cnt_set( p_reg, seq_id, p_seq->length);
  562. nrf_pwm_seq_refresh_set( p_reg, seq_id, p_seq->repeats);
  563. nrf_pwm_seq_end_delay_set(p_reg, seq_id, p_seq->end_delay);
  564. }
  565. __STATIC_INLINE void nrf_pwm_seq_ptr_set(NRF_PWM_Type * p_reg,
  566. uint8_t seq_id,
  567. uint16_t const * p_values)
  568. {
  569. NRFX_ASSERT(seq_id <= 1);
  570. NRFX_ASSERT(p_values != NULL);
  571. p_reg->SEQ[seq_id].PTR = (uint32_t)p_values;
  572. }
  573. __STATIC_INLINE void nrf_pwm_seq_cnt_set(NRF_PWM_Type * p_reg,
  574. uint8_t seq_id,
  575. uint16_t length)
  576. {
  577. NRFX_ASSERT(seq_id <= 1);
  578. NRFX_ASSERT(length != 0);
  579. NRFX_ASSERT(length <= PWM_SEQ_CNT_CNT_Msk);
  580. p_reg->SEQ[seq_id].CNT = length;
  581. }
  582. __STATIC_INLINE void nrf_pwm_seq_refresh_set(NRF_PWM_Type * p_reg,
  583. uint8_t seq_id,
  584. uint32_t refresh)
  585. {
  586. NRFX_ASSERT(seq_id <= 1);
  587. NRFX_ASSERT(refresh <= PWM_SEQ_REFRESH_CNT_Msk);
  588. p_reg->SEQ[seq_id].REFRESH = refresh;
  589. }
  590. __STATIC_INLINE void nrf_pwm_seq_end_delay_set(NRF_PWM_Type * p_reg,
  591. uint8_t seq_id,
  592. uint32_t end_delay)
  593. {
  594. NRFX_ASSERT(seq_id <= 1);
  595. NRFX_ASSERT(end_delay <= PWM_SEQ_ENDDELAY_CNT_Msk);
  596. p_reg->SEQ[seq_id].ENDDELAY = end_delay;
  597. }
  598. __STATIC_INLINE void nrf_pwm_decoder_set(NRF_PWM_Type * p_reg,
  599. nrf_pwm_dec_load_t dec_load,
  600. nrf_pwm_dec_step_t dec_step)
  601. {
  602. p_reg->DECODER = ((uint32_t)dec_load << PWM_DECODER_LOAD_Pos) |
  603. ((uint32_t)dec_step << PWM_DECODER_MODE_Pos);
  604. }
  605. __STATIC_INLINE void nrf_pwm_loop_set(NRF_PWM_Type * p_reg,
  606. uint16_t loop_count)
  607. {
  608. p_reg->LOOP = loop_count;
  609. }
  610. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  611. /** @} */
  612. #ifdef __cplusplus
  613. }
  614. #endif
  615. #endif // NRF_PWM_H__