nrf_pwm.h 25 KB

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