fsl_pwm.h 29 KB

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