fsl_pwm.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder 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. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef _FSL_PWM_H_
  35. #define _FSL_PWM_H_
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup pwm_driver
  39. * @{
  40. */
  41. /*******************************************************************************
  42. * Definitions
  43. ******************************************************************************/
  44. /*! @name Driver version */
  45. /*@{*/
  46. #define FSL_PWM_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) /*!< Version 2.0.0 */
  47. /*@}*/
  48. /*! Number of bits per submodule for software output control */
  49. #define PWM_SUBMODULE_SWCONTROL_WIDTH 2
  50. /*! @brief List of PWM submodules */
  51. typedef enum _pwm_submodule
  52. {
  53. kPWM_Module_0 = 0U, /*!< Submodule 0 */
  54. kPWM_Module_1, /*!< Submodule 1 */
  55. kPWM_Module_2, /*!< Submodule 2 */
  56. kPWM_Module_3 /*!< Submodule 3 */
  57. } pwm_submodule_t;
  58. /*! @brief List of PWM channels in each module */
  59. typedef enum _pwm_channels
  60. {
  61. kPWM_PwmB = 0U,
  62. kPWM_PwmA,
  63. kPWM_PwmX
  64. } pwm_channels_t;
  65. /*! @brief List of PWM value registers */
  66. typedef enum _pwm_value_register
  67. {
  68. kPWM_ValueRegister_0 = 0U, /*!< PWM Value0 register */
  69. kPWM_ValueRegister_1, /*!< PWM Value1 register */
  70. kPWM_ValueRegister_2, /*!< PWM Value2 register */
  71. kPWM_ValueRegister_3, /*!< PWM Value3 register */
  72. kPWM_ValueRegister_4, /*!< PWM Value4 register */
  73. kPWM_ValueRegister_5 /*!< PWM Value5 register */
  74. } pwm_value_register_t;
  75. /*! @brief PWM clock source selection.*/
  76. typedef enum _pwm_clock_source
  77. {
  78. kPWM_BusClock = 0U, /*!< The IPBus clock is used as the clock */
  79. kPWM_ExternalClock, /*!< EXT_CLK is used as the clock */
  80. kPWM_Submodule0Clock /*!< Clock of the submodule 0 (AUX_CLK) is used as the source clock */
  81. } pwm_clock_source_t;
  82. /*! @brief PWM prescaler factor selection for clock source*/
  83. typedef enum _pwm_clock_prescale
  84. {
  85. kPWM_Prescale_Divide_1 = 0U, /*!< PWM clock frequency = fclk/1 */
  86. kPWM_Prescale_Divide_2, /*!< PWM clock frequency = fclk/2 */
  87. kPWM_Prescale_Divide_4, /*!< PWM clock frequency = fclk/4 */
  88. kPWM_Prescale_Divide_8, /*!< PWM clock frequency = fclk/8 */
  89. kPWM_Prescale_Divide_16, /*!< PWM clock frequency = fclk/16 */
  90. kPWM_Prescale_Divide_32, /*!< PWM clock frequency = fclk/32 */
  91. kPWM_Prescale_Divide_64, /*!< PWM clock frequency = fclk/64 */
  92. kPWM_Prescale_Divide_128 /*!< PWM clock frequency = fclk/128 */
  93. } pwm_clock_prescale_t;
  94. /*! @brief Options that can trigger a PWM FORCE_OUT */
  95. typedef enum _pwm_force_output_trigger
  96. {
  97. kPWM_Force_Local = 0U, /*!< The local force signal, CTRL2[FORCE], from the submodule is used to force updates */
  98. kPWM_Force_Master, /*!< The master force signal from submodule 0 is used to force updates */
  99. kPWM_Force_LocalReload, /*!< The local reload signal from this submodule is used to force updates without regard to
  100. the state of LDOK */
  101. kPWM_Force_MasterReload, /*!< The master reload signal from submodule 0 is used to force updates if LDOK is set */
  102. kPWM_Force_LocalSync, /*!< The local sync signal from this submodule is used to force updates */
  103. kPWM_Force_MasterSync, /*!< The master sync signal from submodule0 is used to force updates */
  104. kPWM_Force_External, /*!< The external force signal, EXT_FORCE, from outside the PWM module causes updates */
  105. kPWM_Force_ExternalSync /*!< The external sync signal, EXT_SYNC, from outside the PWM module causes updates */
  106. } pwm_force_output_trigger_t;
  107. /*! @brief PWM counter initialization options */
  108. typedef enum _pwm_init_source
  109. {
  110. kPWM_Initialize_LocalSync = 0U, /*!< Local sync causes initialization */
  111. kPWM_Initialize_MasterReload, /*!< Master reload from submodule 0 causes initialization */
  112. kPWM_Initialize_MasterSync, /*!< Master sync from submodule 0 causes initialization */
  113. kPWM_Initialize_ExtSync /*!< EXT_SYNC causes initialization */
  114. } pwm_init_source_t;
  115. /*! @brief PWM load frequency selection */
  116. typedef enum _pwm_load_frequency
  117. {
  118. kPWM_LoadEveryOportunity = 0U, /*!< Every PWM opportunity */
  119. kPWM_LoadEvery2Oportunity, /*!< Every 2 PWM opportunities */
  120. kPWM_LoadEvery3Oportunity, /*!< Every 3 PWM opportunities */
  121. kPWM_LoadEvery4Oportunity, /*!< Every 4 PWM opportunities */
  122. kPWM_LoadEvery5Oportunity, /*!< Every 5 PWM opportunities */
  123. kPWM_LoadEvery6Oportunity, /*!< Every 6 PWM opportunities */
  124. kPWM_LoadEvery7Oportunity, /*!< Every 7 PWM opportunities */
  125. kPWM_LoadEvery8Oportunity, /*!< Every 8 PWM opportunities */
  126. kPWM_LoadEvery9Oportunity, /*!< Every 9 PWM opportunities */
  127. kPWM_LoadEvery10Oportunity, /*!< Every 10 PWM opportunities */
  128. kPWM_LoadEvery11Oportunity, /*!< Every 11 PWM opportunities */
  129. kPWM_LoadEvery12Oportunity, /*!< Every 12 PWM opportunities */
  130. kPWM_LoadEvery13Oportunity, /*!< Every 13 PWM opportunities */
  131. kPWM_LoadEvery14Oportunity, /*!< Every 14 PWM opportunities */
  132. kPWM_LoadEvery15Oportunity, /*!< Every 15 PWM opportunities */
  133. kPWM_LoadEvery16Oportunity /*!< Every 16 PWM opportunities */
  134. } pwm_load_frequency_t;
  135. /*! @brief List of PWM fault selections */
  136. typedef enum _pwm_fault_input
  137. {
  138. kPWM_Fault_0 = 0U, /*!< Fault 0 input pin */
  139. kPWM_Fault_1, /*!< Fault 1 input pin */
  140. kPWM_Fault_2, /*!< Fault 2 input pin */
  141. kPWM_Fault_3 /*!< Fault 3 input pin */
  142. } pwm_fault_input_t;
  143. /*! @brief PWM capture edge select */
  144. typedef enum _pwm_input_capture_edge
  145. {
  146. kPWM_Disable = 0U, /*!< Disabled */
  147. kPWM_FallingEdge, /*!< Capture on falling edge only */
  148. kPWM_RisingEdge, /*!< Capture on rising edge only */
  149. kPWM_RiseAndFallEdge /*!< Capture on rising or falling edge */
  150. } pwm_input_capture_edge_t;
  151. /*! @brief PWM output options when a FORCE_OUT signal is asserted */
  152. typedef enum _pwm_force_signal
  153. {
  154. kPWM_UsePwm = 0U, /*!< Generated PWM signal is used by the deadtime logic.*/
  155. kPWM_InvertedPwm, /*!< Inverted PWM signal is used by the deadtime logic.*/
  156. kPWM_SoftwareControl, /*!< Software controlled value is used by the deadtime logic. */
  157. kPWM_UseExternal /*!< PWM_EXTA signal is used by the deadtime logic. */
  158. } pwm_force_signal_t;
  159. /*! @brief Options available for the PWM A & B pair operation */
  160. typedef enum _pwm_chnl_pair_operation
  161. {
  162. kPWM_Independent = 0U, /*!< PWM A & PWM B operate as 2 independent channels */
  163. kPWM_ComplementaryPwmA, /*!< PWM A & PWM B are complementary channels, PWM A generates the signal */
  164. kPWM_ComplementaryPwmB /*!< PWM A & PWM B are complementary channels, PWM B generates the signal */
  165. } pwm_chnl_pair_operation_t;
  166. /*! @brief Options available on how to load the buffered-registers with new values */
  167. typedef enum _pwm_register_reload
  168. {
  169. kPWM_ReloadImmediate = 0U, /*!< Buffered-registers get loaded with new values as soon as LDOK bit is set */
  170. kPWM_ReloadPwmHalfCycle, /*!< Registers loaded on a PWM half cycle */
  171. kPWM_ReloadPwmFullCycle, /*!< Registers loaded on a PWM full cycle */
  172. kPWM_ReloadPwmHalfAndFullCycle /*!< Registers loaded on a PWM half & full cycle */
  173. } pwm_register_reload_t;
  174. /*! @brief Options available on how to re-enable the PWM output when recovering from a fault */
  175. typedef enum _pwm_fault_recovery_mode
  176. {
  177. kPWM_NoRecovery = 0U, /*!< PWM output will stay inactive */
  178. kPWM_RecoverHalfCycle, /*!< PWM output re-enabled at the first half cycle */
  179. kPWM_RecoverFullCycle, /*!< PWM output re-enabled at the first full cycle */
  180. kPWM_RecoverHalfAndFullCycle /*!< PWM output re-enabled at the first half or full cycle */
  181. } pwm_fault_recovery_mode_t;
  182. /*! @brief List of PWM interrupt options */
  183. typedef enum _pwm_interrupt_enable
  184. {
  185. kPWM_CompareVal0InterruptEnable = (1U << 0), /*!< PWM VAL0 compare interrupt */
  186. kPWM_CompareVal1InterruptEnable = (1U << 1), /*!< PWM VAL1 compare interrupt */
  187. kPWM_CompareVal2InterruptEnable = (1U << 2), /*!< PWM VAL2 compare interrupt */
  188. kPWM_CompareVal3InterruptEnable = (1U << 3), /*!< PWM VAL3 compare interrupt */
  189. kPWM_CompareVal4InterruptEnable = (1U << 4), /*!< PWM VAL4 compare interrupt */
  190. kPWM_CompareVal5InterruptEnable = (1U << 5), /*!< PWM VAL5 compare interrupt */
  191. kPWM_CaptureX0InterruptEnable = (1U << 6), /*!< PWM capture X0 interrupt */
  192. kPWM_CaptureX1InterruptEnable = (1U << 7), /*!< PWM capture X1 interrupt */
  193. kPWM_CaptureB0InterruptEnable = (1U << 8), /*!< PWM capture B0 interrupt */
  194. kPWM_CaptureB1InterruptEnable = (1U << 9), /*!< PWM capture B1 interrupt */
  195. kPWM_CaptureA0InterruptEnable = (1U << 10), /*!< PWM capture A0 interrupt */
  196. kPWM_CaptureA1InterruptEnable = (1U << 11), /*!< PWM capture A1 interrupt */
  197. kPWM_ReloadInterruptEnable = (1U << 12), /*!< PWM reload interrupt */
  198. kPWM_ReloadErrorInterruptEnable = (1U << 13), /*!< PWM reload error interrupt */
  199. kPWM_Fault0InterruptEnable = (1U << 16), /*!< PWM fault 0 interrupt */
  200. kPWM_Fault1InterruptEnable = (1U << 17), /*!< PWM fault 1 interrupt */
  201. kPWM_Fault2InterruptEnable = (1U << 18), /*!< PWM fault 2 interrupt */
  202. kPWM_Fault3InterruptEnable = (1U << 19) /*!< PWM fault 3 interrupt */
  203. } pwm_interrupt_enable_t;
  204. /*! @brief List of PWM status flags */
  205. typedef enum _pwm_status_flags
  206. {
  207. kPWM_CompareVal0Flag = (1U << 0), /*!< PWM VAL0 compare flag */
  208. kPWM_CompareVal1Flag = (1U << 1), /*!< PWM VAL1 compare flag */
  209. kPWM_CompareVal2Flag = (1U << 2), /*!< PWM VAL2 compare flag */
  210. kPWM_CompareVal3Flag = (1U << 3), /*!< PWM VAL3 compare flag */
  211. kPWM_CompareVal4Flag = (1U << 4), /*!< PWM VAL4 compare flag */
  212. kPWM_CompareVal5Flag = (1U << 5), /*!< PWM VAL5 compare flag */
  213. kPWM_CaptureX0Flag = (1U << 6), /*!< PWM capture X0 flag */
  214. kPWM_CaptureX1Flag = (1U << 7), /*!< PWM capture X1 flag */
  215. kPWM_CaptureB0Flag = (1U << 8), /*!< PWM capture B0 flag */
  216. kPWM_CaptureB1Flag = (1U << 9), /*!< PWM capture B1 flag */
  217. kPWM_CaptureA0Flag = (1U << 10), /*!< PWM capture A0 flag */
  218. kPWM_CaptureA1Flag = (1U << 11), /*!< PWM capture A1 flag */
  219. kPWM_ReloadFlag = (1U << 12), /*!< PWM reload flag */
  220. kPWM_ReloadErrorFlag = (1U << 13), /*!< PWM reload error flag */
  221. kPWM_RegUpdatedFlag = (1U << 14), /*!< PWM registers updated flag */
  222. kPWM_Fault0Flag = (1U << 16), /*!< PWM fault 0 flag */
  223. kPWM_Fault1Flag = (1U << 17), /*!< PWM fault 1 flag */
  224. kPWM_Fault2Flag = (1U << 18), /*!< PWM fault 2 flag */
  225. kPWM_Fault3Flag = (1U << 19) /*!< PWM fault 3 flag */
  226. } pwm_status_flags_t;
  227. /*! @brief PWM operation mode */
  228. typedef enum _pwm_mode
  229. {
  230. kPWM_SignedCenterAligned = 0U, /*!< Signed center-aligned */
  231. kPWM_CenterAligned, /*!< Unsigned cente-aligned */
  232. kPWM_SignedEdgeAligned, /*!< Signed edge-aligned */
  233. kPWM_EdgeAligned /*!< Unsigned edge-aligned */
  234. } pwm_mode_t;
  235. /*! @brief PWM output pulse mode, high-true or low-true */
  236. typedef enum _pwm_level_select
  237. {
  238. kPWM_HighTrue = 0U, /*!< High level represents "on" or "active" state */
  239. kPWM_LowTrue /*!< Low level represents "on" or "active" state */
  240. } pwm_level_select_t;
  241. /*! @brief PWM reload source select */
  242. typedef enum _pwm_reload_source_select
  243. {
  244. kPWM_LocalReload = 0U, /*!< The local reload signal is used to reload registers */
  245. kPWM_MasterReload /*!< The master reload signal (from submodule 0) is used to reload */
  246. } pwm_reload_source_select_t;
  247. /*! @brief PWM fault clearing options */
  248. typedef enum _pwm_fault_clear
  249. {
  250. kPWM_Automatic = 0U, /*!< Automatic fault clearing */
  251. kPWM_ManualNormal, /*!< Manual fault clearing with no fault safety mode */
  252. kPWM_ManualSafety /*!< Manual fault clearing with fault safety mode */
  253. } pwm_fault_clear_t;
  254. /*! @brief Options for submodule master control operation */
  255. typedef enum _pwm_module_control
  256. {
  257. kPWM_Control_Module_0 = (1U << 0), /*!< Control submodule 0's start/stop,buffer reload operation */
  258. kPWM_Control_Module_1 = (1U << 1), /*!< Control submodule 1's start/stop,buffer reload operation */
  259. kPWM_Control_Module_2 = (1U << 2), /*!< Control submodule 2's start/stop,buffer reload operation */
  260. kPWM_Control_Module_3 = (1U << 3) /*!< Control submodule 3's start/stop,buffer reload operation */
  261. } pwm_module_control_t;
  262. /*! @brief Structure for the user to define the PWM signal characteristics */
  263. typedef struct _pwm_signal_param
  264. {
  265. pwm_channels_t pwmChannel; /*!< PWM channel being configured; PWM A or PWM B */
  266. uint8_t dutyCyclePercent; /*!< PWM pulse width, value should be between 0 to 100
  267. 0=inactive signal(0% duty cycle)...
  268. 100=always active signal (100% duty cycle)*/
  269. pwm_level_select_t level; /*!< PWM output active level select */
  270. uint16_t deadtimeValue; /*!< The deadtime value; only used if channel pair is operating in complementary mode */
  271. } pwm_signal_param_t;
  272. /*!
  273. * @brief PWM config structure
  274. *
  275. * This structure holds the configuration settings for the PWM peripheral. To initialize this
  276. * structure to reasonable defaults, call the PWM_GetDefaultConfig() function and pass a
  277. * pointer to your config structure instance.
  278. *
  279. * The config struct can be made const so it resides in flash
  280. */
  281. typedef struct _pwm_config
  282. {
  283. bool enableDebugMode; /*!< true: PWM continues to run in debug mode;
  284. false: PWM is paused in debug mode */
  285. bool enableWait; /*!< true: PWM continues to run in WAIT mode;
  286. false: PWM is paused in WAIT mode */
  287. uint8_t faultFilterCount; /*!< Fault filter count */
  288. uint8_t faultFilterPeriod; /*!< Fault filter period;value of 0 will bypass the filter */
  289. pwm_init_source_t initializationControl; /*!< Option to initialize the counter */
  290. pwm_clock_source_t clockSource; /*!< Clock source for the counter */
  291. pwm_clock_prescale_t prescale; /*!< Pre-scaler to divide down the clock */
  292. pwm_chnl_pair_operation_t pairOperation; /*!< Channel pair in indepedent or complementary mode */
  293. pwm_register_reload_t reloadLogic; /*!< PWM Reload logic setup */
  294. pwm_reload_source_select_t reloadSelect; /*!< Reload source select */
  295. pwm_load_frequency_t reloadFrequency; /*!< Specifies when to reload, used when user's choice
  296. is not immediate reload */
  297. pwm_force_output_trigger_t forceTrigger; /*!< Specify which signal will trigger a FORCE_OUT */
  298. } pwm_config_t;
  299. /*! @brief Structure is used to hold the parameters to configure a PWM fault */
  300. typedef struct _pwm_fault_param
  301. {
  302. pwm_fault_clear_t faultClearingMode; /*!< Fault clearing mode to use */
  303. bool faultLevel; /*!< true: Logic 1 indicates fault;
  304. false: Logic 0 indicates fault */
  305. bool enableCombinationalPath; /*!< true: Combinational Path from fault input is enabled;
  306. false: No combination path is available */
  307. pwm_fault_recovery_mode_t recoverMode; /*!< Specify when to re-enable the PWM output */
  308. } pwm_fault_param_t;
  309. /*!
  310. * @brief Structure is used to hold parameters to configure the capture capability of a signal pin
  311. */
  312. typedef struct _pwm_input_capture_param
  313. {
  314. bool captureInputSel; /*!< true: Use the edge counter signal as source
  315. false: Use the raw input signal from the pin as source */
  316. uint8_t edgeCompareValue; /*!< Compare value, used only if edge counter is used as source */
  317. pwm_input_capture_edge_t edge0; /*!< Specify which edge causes a capture for input circuitry 0 */
  318. pwm_input_capture_edge_t edge1; /*!< Specify which edge causes a capture for input circuitry 1 */
  319. bool enableOneShotCapture; /*!< true: Use one-shot capture mode;
  320. false: Use free-running capture mode */
  321. uint8_t fifoWatermark; /*!< Watermark level for capture FIFO. The capture flags in
  322. the status register will set if the word count in the FIFO
  323. is greater than this watermark level */
  324. } pwm_input_capture_param_t;
  325. /*******************************************************************************
  326. * API
  327. ******************************************************************************/
  328. #if defined(__cplusplus)
  329. extern "C" {
  330. #endif
  331. /*!
  332. * @name Initialization and deinitialization
  333. * @{
  334. */
  335. /*!
  336. * @brief Ungates the PWM submodule clock and configures the peripheral for basic operation.
  337. *
  338. * @note This API should be called at the beginning of the application using the PWM driver.
  339. *
  340. * @param base PWM peripheral base address
  341. * @param subModule PWM submodule to configure
  342. * @param config Pointer to user's PWM config structure.
  343. *
  344. * @return kStatus_Success means success; else failed.
  345. */
  346. status_t PWM_Init(PWM_Type *base, pwm_submodule_t subModule, const pwm_config_t *config);
  347. /*!
  348. * @brief Gate the PWM submodule clock
  349. *
  350. * @param base PWM peripheral base address
  351. * @param subModule PWM submodule to deinitialize
  352. */
  353. void PWM_Deinit(PWM_Type *base, pwm_submodule_t subModule);
  354. /*!
  355. * @brief Fill in the PWM config struct with the default settings
  356. *
  357. * The default values are:
  358. * @code
  359. * config->enableDebugMode = false;
  360. * config->enableWait = false;
  361. * config->reloadSelect = kPWM_LocalReload;
  362. * config->faultFilterCount = 0;
  363. * config->faultFilterPeriod = 0;
  364. * config->clockSource = kPWM_BusClock;
  365. * config->prescale = kPWM_Prescale_Divide_1;
  366. * config->initializationControl = kPWM_Initialize_LocalSync;
  367. * config->forceTrigger = kPWM_Force_Local;
  368. * config->reloadFrequency = kPWM_LoadEveryOportunity;
  369. * config->reloadLogic = kPWM_ReloadImmediate;
  370. * config->pairOperation = kPWM_Independent;
  371. * @endcode
  372. * @param config Pointer to user's PWM config structure.
  373. */
  374. void PWM_GetDefaultConfig(pwm_config_t *config);
  375. /*! @}*/
  376. /*!
  377. * @name Module PWM output
  378. * @{
  379. */
  380. /*!
  381. * @brief Sets up the PWM signals for a PWM submodule.
  382. *
  383. * The function initializes the submodule according to the parameters passed in by the user. The function
  384. * also sets up the value compare registers to match the PWM signal requirements.
  385. * If the dead time insertion logic is enabled, the pulse period is reduced by the
  386. * dead time period specified by the user.
  387. *
  388. * @param base PWM peripheral base address
  389. * @param subModule PWM submodule to configure
  390. * @param chnlParams Array of PWM channel parameters to configure the channel(s)
  391. * @param numOfChnls Number of channels to configure, this should be the size of the array passed in.
  392. * Array size should not be more than 2 as each submodule has 2 pins to output PWM
  393. * @param mode PWM operation mode, options available in enumeration ::pwm_mode_t
  394. * @param pwmFreq_Hz PWM signal frequency in Hz
  395. * @param srcClock_Hz PWM main counter clock in Hz.
  396. *
  397. * @return Returns kStatusFail if there was error setting up the signal; kStatusSuccess otherwise
  398. */
  399. status_t PWM_SetupPwm(PWM_Type *base,
  400. pwm_submodule_t subModule,
  401. const pwm_signal_param_t *chnlParams,
  402. uint8_t numOfChnls,
  403. pwm_mode_t mode,
  404. uint32_t pwmFreq_Hz,
  405. uint32_t srcClock_Hz);
  406. /*!
  407. * @brief Updates the PWM signal's dutycycle.
  408. *
  409. * The function updates the PWM dutycyle to the new value that is passed in.
  410. * If the dead time insertion logic is enabled then the pulse period is reduced by the
  411. * dead time period specified by the user.
  412. *
  413. * @param base PWM peripheral base address
  414. * @param subModule PWM submodule to configure
  415. * @param pwmSignal Signal (PWM A or PWM B) to update
  416. * @param currPwmMode The current PWM mode set during PWM setup
  417. * @param dutyCyclePercent New PWM pulse width, value should be between 0 to 100
  418. * 0=inactive signal(0% duty cycle)...
  419. * 100=active signal (100% duty cycle)
  420. */
  421. void PWM_UpdatePwmDutycycle(PWM_Type *base,
  422. pwm_submodule_t subModule,
  423. pwm_channels_t pwmSignal,
  424. pwm_mode_t currPwmMode,
  425. uint8_t dutyCyclePercent);
  426. /*! @}*/
  427. /*!
  428. * @brief Sets up the PWM input capture
  429. *
  430. * Each PWM submodule has 3 pins that can be configured for use as input capture pins. This function
  431. * sets up the capture parameters for each pin and enables the pin for input capture operation.
  432. *
  433. * @param base PWM peripheral base address
  434. * @param subModule PWM submodule to configure
  435. * @param pwmChannel Channel in the submodule to setup
  436. * @param inputCaptureParams Parameters passed in to set up the input pin
  437. */
  438. void PWM_SetupInputCapture(PWM_Type *base,
  439. pwm_submodule_t subModule,
  440. pwm_channels_t pwmChannel,
  441. const pwm_input_capture_param_t *inputCaptureParams);
  442. /*!
  443. * @brief Sets up the PWM fault protection.
  444. *
  445. * PWM has 4 fault inputs.
  446. *
  447. * @param base PWM peripheral base address
  448. * @param faultNum PWM fault to configure.
  449. * @param faultParams Pointer to the PWM fault config structure
  450. */
  451. void PWM_SetupFaults(PWM_Type *base, pwm_fault_input_t faultNum, const pwm_fault_param_t *faultParams);
  452. /*!
  453. * @brief Selects the signal to output on a PWM pin when a FORCE_OUT signal is asserted.
  454. *
  455. * The user specifies which channel to configure by supplying the submodule number and whether
  456. * to modify PWM A or PWM B within that submodule.
  457. *
  458. * @param base PWM peripheral base address
  459. * @param subModule PWM submodule to configure
  460. * @param pwmChannel Channel to configure
  461. * @param mode Signal to output when a FORCE_OUT is triggered
  462. */
  463. void PWM_SetupForceSignal(PWM_Type *base,
  464. pwm_submodule_t subModule,
  465. pwm_channels_t pwmChannel,
  466. pwm_force_signal_t mode);
  467. /*!
  468. * @name Interrupts Interface
  469. * @{
  470. */
  471. /*!
  472. * @brief Enables the selected PWM interrupts
  473. *
  474. * @param base PWM peripheral base address
  475. * @param subModule PWM submodule to configure
  476. * @param mask The interrupts to enable. This is a logical OR of members of the
  477. * enumeration ::pwm_interrupt_enable_t
  478. */
  479. void PWM_EnableInterrupts(PWM_Type *base, pwm_submodule_t subModule, uint32_t mask);
  480. /*!
  481. * @brief Disables the selected PWM interrupts
  482. *
  483. * @param base PWM peripheral base address
  484. * @param subModule PWM submodule to configure
  485. * @param mask The interrupts to enable. This is a logical OR of members of the
  486. * enumeration ::pwm_interrupt_enable_t
  487. */
  488. void PWM_DisableInterrupts(PWM_Type *base, pwm_submodule_t subModule, uint32_t mask);
  489. /*!
  490. * @brief Gets the enabled PWM interrupts
  491. *
  492. * @param base PWM peripheral base address
  493. * @param subModule PWM submodule to configure
  494. *
  495. * @return The enabled interrupts. This is the logical OR of members of the
  496. * enumeration ::pwm_interrupt_enable_t
  497. */
  498. uint32_t PWM_GetEnabledInterrupts(PWM_Type *base, pwm_submodule_t subModule);
  499. /*! @}*/
  500. /*!
  501. * @name Status Interface
  502. * @{
  503. */
  504. /*!
  505. * @brief Gets the PWM status flags
  506. *
  507. * @param base PWM peripheral base address
  508. * @param subModule PWM submodule to configure
  509. *
  510. * @return The status flags. This is the logical OR of members of the
  511. * enumeration ::pwm_status_flags_t
  512. */
  513. uint32_t PWM_GetStatusFlags(PWM_Type *base, pwm_submodule_t subModule);
  514. /*!
  515. * @brief Clears the PWM status flags
  516. *
  517. * @param base PWM peripheral base address
  518. * @param subModule PWM submodule to configure
  519. * @param mask The status flags to clear. This is a logical OR of members of the
  520. * enumeration ::pwm_status_flags_t
  521. */
  522. void PWM_ClearStatusFlags(PWM_Type *base, pwm_submodule_t subModule, uint32_t mask);
  523. /*! @}*/
  524. /*!
  525. * @name Timer Start and Stop
  526. * @{
  527. */
  528. /*!
  529. * @brief Starts the PWM counter for a single or multiple submodules.
  530. *
  531. * Sets the Run bit which enables the clocks to the PWM submodule. This function can start multiple
  532. * submodules at the same time.
  533. *
  534. * @param base PWM peripheral base address
  535. * @param subModulesToStart PWM submodules to start. This is a logical OR of members of the
  536. * enumeration ::pwm_module_control_t
  537. */
  538. static inline void PWM_StartTimer(PWM_Type *base, uint8_t subModulesToStart)
  539. {
  540. base->MCTRL |= PWM_MCTRL_RUN(subModulesToStart);
  541. }
  542. /*!
  543. * @brief Stops the PWM counter for a single or multiple submodules.
  544. *
  545. * Clears the Run bit which resets the submodule's counter. This function can stop multiple
  546. * submodules at the same time.
  547. *
  548. * @param base PWM peripheral base address
  549. * @param subModulesToStop PWM submodules to stop. This is a logical OR of members of the
  550. * enumeration ::pwm_module_control_t
  551. */
  552. static inline void PWM_StopTimer(PWM_Type *base, uint8_t subModulesToStop)
  553. {
  554. base->MCTRL &= ~(PWM_MCTRL_RUN(subModulesToStop));
  555. }
  556. /*! @}*/
  557. /*!
  558. * @brief Enables or disables the PWM output trigger.
  559. *
  560. * This function allows the user to enable or disable the PWM trigger. The PWM has 2 triggers. Trigger 0
  561. * is activated when the counter matches VAL 0, VAL 2, or VAL 4 register. Trigger 1 is activated
  562. * when the counter matches VAL 1, VAL 3, or VAL 5 register.
  563. *
  564. * @param base PWM peripheral base address
  565. * @param subModule PWM submodule to configure
  566. * @param valueRegister Value register that will activate the trigger
  567. * @param activate true: Enable the trigger; false: Disable the trigger
  568. */
  569. static inline void PWM_OutputTriggerEnable(PWM_Type *base,
  570. pwm_submodule_t subModule,
  571. pwm_value_register_t valueRegister,
  572. bool activate)
  573. {
  574. if (activate)
  575. {
  576. base->SM[subModule].TCTRL |= (1U << valueRegister);
  577. }
  578. else
  579. {
  580. base->SM[subModule].TCTRL &= ~(1U << valueRegister);
  581. }
  582. }
  583. /*!
  584. * @brief Sets the software control output for a pin to high or low.
  585. *
  586. * The user specifies which channel to modify by supplying the submodule number and whether
  587. * to modify PWM A or PWM B within that submodule.
  588. *
  589. * @param base PWM peripheral base address
  590. * @param subModule PWM submodule to configure
  591. * @param pwmChannel Channel to configure
  592. * @param value true: Supply a logic 1, false: Supply a logic 0.
  593. */
  594. static inline void PWM_SetupSwCtrlOut(PWM_Type *base, pwm_submodule_t subModule, pwm_channels_t pwmChannel, bool value)
  595. {
  596. if (value)
  597. {
  598. base->SWCOUT |= (1U << ((subModule * PWM_SUBMODULE_SWCONTROL_WIDTH) + pwmChannel));
  599. }
  600. else
  601. {
  602. base->SWCOUT &= ~(1U << ((subModule * PWM_SUBMODULE_SWCONTROL_WIDTH) + pwmChannel));
  603. }
  604. }
  605. /*!
  606. * @brief Sets or clears the PWM LDOK bit on a single or multiple submodules
  607. *
  608. * Set LDOK bit to load buffered values into CTRL[PRSC] and the INIT, FRACVAL and VAL registers. The
  609. * values are loaded immediately if kPWM_ReloadImmediate option was choosen during config. Else the
  610. * values are loaded at the next PWM reload point.
  611. * This function can issue the load command to multiple submodules at the same time.
  612. *
  613. * @param base PWM peripheral base address
  614. * @param subModulesToUpdate PWM submodules to update with buffered values. This is a logical OR of
  615. * members of the enumeration ::pwm_module_control_t
  616. * @param value true: Set LDOK bit for the submodule list; false: Clear LDOK bit
  617. */
  618. static inline void PWM_SetPwmLdok(PWM_Type *base, uint8_t subModulesToUpdate, bool value)
  619. {
  620. if (value)
  621. {
  622. base->MCTRL |= PWM_MCTRL_LDOK(subModulesToUpdate);
  623. }
  624. else
  625. {
  626. base->MCTRL |= PWM_MCTRL_CLDOK(subModulesToUpdate);
  627. }
  628. }
  629. #if defined(__cplusplus)
  630. }
  631. #endif
  632. /*! @}*/
  633. #endif /* _FSL_PWM_H_ */