Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

988 rader
40 KiB

  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2020 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef _FSL_PWM_H_
  9. #define _FSL_PWM_H_
  10. #include "fsl_common.h"
  11. /*!
  12. * @addtogroup pwm_driver
  13. * @{
  14. */
  15. /*******************************************************************************
  16. * Definitions
  17. ******************************************************************************/
  18. /*! @name Driver version */
  19. /*@{*/
  20. #define FSL_PWM_DRIVER_VERSION (MAKE_VERSION(2, 2, 1)) /*!< Version 2.2.1 */
  21. /*@}*/
  22. /*! Number of bits per submodule for software output control */
  23. #define PWM_SUBMODULE_SWCONTROL_WIDTH 2
  24. /*! @brief List of PWM submodules */
  25. typedef enum _pwm_submodule
  26. {
  27. kPWM_Module_0 = 0U, /*!< Submodule 0 */
  28. kPWM_Module_1, /*!< Submodule 1 */
  29. kPWM_Module_2, /*!< Submodule 2 */
  30. kPWM_Module_3 /*!< Submodule 3 */
  31. } pwm_submodule_t;
  32. /*! @brief List of PWM channels in each module */
  33. typedef enum _pwm_channels
  34. {
  35. kPWM_PwmB = 0U,
  36. kPWM_PwmA,
  37. kPWM_PwmX
  38. } pwm_channels_t;
  39. /*! @brief List of PWM value registers */
  40. typedef enum _pwm_value_register
  41. {
  42. kPWM_ValueRegister_0 = 0U, /*!< PWM Value0 register */
  43. kPWM_ValueRegister_1, /*!< PWM Value1 register */
  44. kPWM_ValueRegister_2, /*!< PWM Value2 register */
  45. kPWM_ValueRegister_3, /*!< PWM Value3 register */
  46. kPWM_ValueRegister_4, /*!< PWM Value4 register */
  47. kPWM_ValueRegister_5 /*!< PWM Value5 register */
  48. } pwm_value_register_t;
  49. /*! @brief List of PWM value registers mask */
  50. enum _pwm_value_register_mask
  51. {
  52. kPWM_ValueRegisterMask_0 = (1U << 0), /*!< PWM Value0 register mask */
  53. kPWM_ValueRegisterMask_1 = (1U << 1), /*!< PWM Value1 register mask */
  54. kPWM_ValueRegisterMask_2 = (1U << 2), /*!< PWM Value2 register mask */
  55. kPWM_ValueRegisterMask_3 = (1U << 3), /*!< PWM Value3 register mask */
  56. kPWM_ValueRegisterMask_4 = (1U << 4), /*!< PWM Value4 register mask */
  57. kPWM_ValueRegisterMask_5 = (1U << 5) /*!< PWM Value5 register mask */
  58. };
  59. /*! @brief PWM clock source selection.*/
  60. typedef enum _pwm_clock_source
  61. {
  62. kPWM_BusClock = 0U, /*!< The IPBus clock is used as the clock */
  63. kPWM_ExternalClock, /*!< EXT_CLK is used as the clock */
  64. kPWM_Submodule0Clock /*!< Clock of the submodule 0 (AUX_CLK) is used as the source clock */
  65. } pwm_clock_source_t;
  66. /*! @brief PWM prescaler factor selection for clock source*/
  67. typedef enum _pwm_clock_prescale
  68. {
  69. kPWM_Prescale_Divide_1 = 0U, /*!< PWM clock frequency = fclk/1 */
  70. kPWM_Prescale_Divide_2, /*!< PWM clock frequency = fclk/2 */
  71. kPWM_Prescale_Divide_4, /*!< PWM clock frequency = fclk/4 */
  72. kPWM_Prescale_Divide_8, /*!< PWM clock frequency = fclk/8 */
  73. kPWM_Prescale_Divide_16, /*!< PWM clock frequency = fclk/16 */
  74. kPWM_Prescale_Divide_32, /*!< PWM clock frequency = fclk/32 */
  75. kPWM_Prescale_Divide_64, /*!< PWM clock frequency = fclk/64 */
  76. kPWM_Prescale_Divide_128 /*!< PWM clock frequency = fclk/128 */
  77. } pwm_clock_prescale_t;
  78. /*! @brief Options that can trigger a PWM FORCE_OUT */
  79. typedef enum _pwm_force_output_trigger
  80. {
  81. kPWM_Force_Local = 0U, /*!< The local force signal, CTRL2[FORCE], from the submodule is used to force updates */
  82. kPWM_Force_Master, /*!< The master force signal from submodule 0 is used to force updates */
  83. kPWM_Force_LocalReload, /*!< The local reload signal from this submodule is used to force updates without regard to
  84. the state of LDOK */
  85. kPWM_Force_MasterReload, /*!< The master reload signal from submodule 0 is used to force updates if LDOK is set */
  86. kPWM_Force_LocalSync, /*!< The local sync signal from this submodule is used to force updates */
  87. kPWM_Force_MasterSync, /*!< The master sync signal from submodule0 is used to force updates */
  88. kPWM_Force_External, /*!< The external force signal, EXT_FORCE, from outside the PWM module causes updates */
  89. kPWM_Force_ExternalSync /*!< The external sync signal, EXT_SYNC, from outside the PWM module causes updates */
  90. } pwm_force_output_trigger_t;
  91. /*! @brief PWM counter initialization options */
  92. typedef enum _pwm_init_source
  93. {
  94. kPWM_Initialize_LocalSync = 0U, /*!< Local sync causes initialization */
  95. kPWM_Initialize_MasterReload, /*!< Master reload from submodule 0 causes initialization */
  96. kPWM_Initialize_MasterSync, /*!< Master sync from submodule 0 causes initialization */
  97. kPWM_Initialize_ExtSync /*!< EXT_SYNC causes initialization */
  98. } pwm_init_source_t;
  99. /*! @brief PWM load frequency selection */
  100. typedef enum _pwm_load_frequency
  101. {
  102. kPWM_LoadEveryOportunity = 0U, /*!< Every PWM opportunity */
  103. kPWM_LoadEvery2Oportunity, /*!< Every 2 PWM opportunities */
  104. kPWM_LoadEvery3Oportunity, /*!< Every 3 PWM opportunities */
  105. kPWM_LoadEvery4Oportunity, /*!< Every 4 PWM opportunities */
  106. kPWM_LoadEvery5Oportunity, /*!< Every 5 PWM opportunities */
  107. kPWM_LoadEvery6Oportunity, /*!< Every 6 PWM opportunities */
  108. kPWM_LoadEvery7Oportunity, /*!< Every 7 PWM opportunities */
  109. kPWM_LoadEvery8Oportunity, /*!< Every 8 PWM opportunities */
  110. kPWM_LoadEvery9Oportunity, /*!< Every 9 PWM opportunities */
  111. kPWM_LoadEvery10Oportunity, /*!< Every 10 PWM opportunities */
  112. kPWM_LoadEvery11Oportunity, /*!< Every 11 PWM opportunities */
  113. kPWM_LoadEvery12Oportunity, /*!< Every 12 PWM opportunities */
  114. kPWM_LoadEvery13Oportunity, /*!< Every 13 PWM opportunities */
  115. kPWM_LoadEvery14Oportunity, /*!< Every 14 PWM opportunities */
  116. kPWM_LoadEvery15Oportunity, /*!< Every 15 PWM opportunities */
  117. kPWM_LoadEvery16Oportunity /*!< Every 16 PWM opportunities */
  118. } pwm_load_frequency_t;
  119. /*! @brief List of PWM fault selections */
  120. typedef enum _pwm_fault_input
  121. {
  122. kPWM_Fault_0 = 0U, /*!< Fault 0 input pin */
  123. kPWM_Fault_1, /*!< Fault 1 input pin */
  124. kPWM_Fault_2, /*!< Fault 2 input pin */
  125. kPWM_Fault_3 /*!< Fault 3 input pin */
  126. } pwm_fault_input_t;
  127. /*! @brief List of PWM fault disable mapping selections */
  128. typedef enum _pwm_fault_disable
  129. {
  130. kPWM_FaultDisable_0 = (1U << 0), /*!< Fault 0 disable mapping */
  131. kPWM_FaultDisable_1 = (1U << 1), /*!< Fault 1 disable mapping */
  132. kPWM_FaultDisable_2 = (1U << 2), /*!< Fault 2 disable mapping */
  133. kPWM_FaultDisable_3 = (1U << 3) /*!< Fault 3 disable mapping */
  134. } pwm_fault_disable_t;
  135. /*! @brief List of PWM fault channels */
  136. typedef enum _pwm_fault_channels
  137. {
  138. kPWM_faultchannel_0 = 0U,
  139. kPWM_faultchannel_1
  140. } pwm_fault_channels_t;
  141. /*! @brief PWM capture edge select */
  142. typedef enum _pwm_input_capture_edge
  143. {
  144. kPWM_Disable = 0U, /*!< Disabled */
  145. kPWM_FallingEdge, /*!< Capture on falling edge only */
  146. kPWM_RisingEdge, /*!< Capture on rising edge only */
  147. kPWM_RiseAndFallEdge /*!< Capture on rising or falling edge */
  148. } pwm_input_capture_edge_t;
  149. /*! @brief PWM output options when a FORCE_OUT signal is asserted */
  150. typedef enum _pwm_force_signal
  151. {
  152. kPWM_UsePwm = 0U, /*!< Generated PWM signal is used by the deadtime logic.*/
  153. kPWM_InvertedPwm, /*!< Inverted PWM signal is used by the deadtime logic.*/
  154. kPWM_SoftwareControl, /*!< Software controlled value is used by the deadtime logic. */
  155. kPWM_UseExternal /*!< PWM_EXTA signal is used by the deadtime logic. */
  156. } pwm_force_signal_t;
  157. /*! @brief Options available for the PWM A & B pair operation */
  158. typedef enum _pwm_chnl_pair_operation
  159. {
  160. kPWM_Independent = 0U, /*!< PWM A & PWM B operate as 2 independent channels */
  161. kPWM_ComplementaryPwmA, /*!< PWM A & PWM B are complementary channels, PWM A generates the signal */
  162. kPWM_ComplementaryPwmB /*!< PWM A & PWM B are complementary channels, PWM B generates the signal */
  163. } pwm_chnl_pair_operation_t;
  164. /*! @brief Options available on how to load the buffered-registers with new values */
  165. typedef enum _pwm_register_reload
  166. {
  167. kPWM_ReloadImmediate = 0U, /*!< Buffered-registers get loaded with new values as soon as LDOK bit is set */
  168. kPWM_ReloadPwmHalfCycle, /*!< Registers loaded on a PWM half cycle */
  169. kPWM_ReloadPwmFullCycle, /*!< Registers loaded on a PWM full cycle */
  170. kPWM_ReloadPwmHalfAndFullCycle /*!< Registers loaded on a PWM half & full cycle */
  171. } pwm_register_reload_t;
  172. /*! @brief Options available on how to re-enable the PWM output when recovering from a fault */
  173. typedef enum _pwm_fault_recovery_mode
  174. {
  175. kPWM_NoRecovery = 0U, /*!< PWM output will stay inactive */
  176. kPWM_RecoverHalfCycle, /*!< PWM output re-enabled at the first half cycle */
  177. kPWM_RecoverFullCycle, /*!< PWM output re-enabled at the first full cycle */
  178. kPWM_RecoverHalfAndFullCycle /*!< PWM output re-enabled at the first half or full cycle */
  179. } pwm_fault_recovery_mode_t;
  180. /*! @brief List of PWM interrupt options */
  181. typedef enum _pwm_interrupt_enable
  182. {
  183. kPWM_CompareVal0InterruptEnable = (1U << 0), /*!< PWM VAL0 compare interrupt */
  184. kPWM_CompareVal1InterruptEnable = (1U << 1), /*!< PWM VAL1 compare interrupt */
  185. kPWM_CompareVal2InterruptEnable = (1U << 2), /*!< PWM VAL2 compare interrupt */
  186. kPWM_CompareVal3InterruptEnable = (1U << 3), /*!< PWM VAL3 compare interrupt */
  187. kPWM_CompareVal4InterruptEnable = (1U << 4), /*!< PWM VAL4 compare interrupt */
  188. kPWM_CompareVal5InterruptEnable = (1U << 5), /*!< PWM VAL5 compare interrupt */
  189. kPWM_CaptureX0InterruptEnable = (1U << 6), /*!< PWM capture X0 interrupt */
  190. kPWM_CaptureX1InterruptEnable = (1U << 7), /*!< PWM capture X1 interrupt */
  191. kPWM_CaptureB0InterruptEnable = (1U << 8), /*!< PWM capture B0 interrupt */
  192. kPWM_CaptureB1InterruptEnable = (1U << 9), /*!< PWM capture B1 interrupt */
  193. kPWM_CaptureA0InterruptEnable = (1U << 10), /*!< PWM capture A0 interrupt */
  194. kPWM_CaptureA1InterruptEnable = (1U << 11), /*!< PWM capture A1 interrupt */
  195. kPWM_ReloadInterruptEnable = (1U << 12), /*!< PWM reload interrupt */
  196. kPWM_ReloadErrorInterruptEnable = (1U << 13), /*!< PWM reload error interrupt */
  197. kPWM_Fault0InterruptEnable = (1U << 16), /*!< PWM fault 0 interrupt */
  198. kPWM_Fault1InterruptEnable = (1U << 17), /*!< PWM fault 1 interrupt */
  199. kPWM_Fault2InterruptEnable = (1U << 18), /*!< PWM fault 2 interrupt */
  200. kPWM_Fault3InterruptEnable = (1U << 19) /*!< PWM fault 3 interrupt */
  201. } pwm_interrupt_enable_t;
  202. /*! @brief List of PWM status flags */
  203. typedef enum _pwm_status_flags
  204. {
  205. kPWM_CompareVal0Flag = (1U << 0), /*!< PWM VAL0 compare flag */
  206. kPWM_CompareVal1Flag = (1U << 1), /*!< PWM VAL1 compare flag */
  207. kPWM_CompareVal2Flag = (1U << 2), /*!< PWM VAL2 compare flag */
  208. kPWM_CompareVal3Flag = (1U << 3), /*!< PWM VAL3 compare flag */
  209. kPWM_CompareVal4Flag = (1U << 4), /*!< PWM VAL4 compare flag */
  210. kPWM_CompareVal5Flag = (1U << 5), /*!< PWM VAL5 compare flag */
  211. kPWM_CaptureX0Flag = (1U << 6), /*!< PWM capture X0 flag */
  212. kPWM_CaptureX1Flag = (1U << 7), /*!< PWM capture X1 flag */
  213. kPWM_CaptureB0Flag = (1U << 8), /*!< PWM capture B0 flag */
  214. kPWM_CaptureB1Flag = (1U << 9), /*!< PWM capture B1 flag */
  215. kPWM_CaptureA0Flag = (1U << 10), /*!< PWM capture A0 flag */
  216. kPWM_CaptureA1Flag = (1U << 11), /*!< PWM capture A1 flag */
  217. kPWM_ReloadFlag = (1U << 12), /*!< PWM reload flag */
  218. kPWM_ReloadErrorFlag = (1U << 13), /*!< PWM reload error flag */
  219. kPWM_RegUpdatedFlag = (1U << 14), /*!< PWM registers updated flag */
  220. kPWM_Fault0Flag = (1U << 16), /*!< PWM fault 0 flag */
  221. kPWM_Fault1Flag = (1U << 17), /*!< PWM fault 1 flag */
  222. kPWM_Fault2Flag = (1U << 18), /*!< PWM fault 2 flag */
  223. kPWM_Fault3Flag = (1U << 19) /*!< PWM fault 3 flag */
  224. } pwm_status_flags_t;
  225. /*! @brief List of PWM DMA options */
  226. typedef enum _pwm_dma_enable
  227. {
  228. kPWM_CaptureX0DMAEnable = (1U << 0), /*!< PWM capture X0 DMA */
  229. kPWM_CaptureX1DMAEnable = (1U << 1), /*!< PWM capture X1 DMA */
  230. kPWM_CaptureB0DMAEnable = (1U << 2), /*!< PWM capture B0 DMA */
  231. kPWM_CaptureB1DMAEnable = (1U << 3), /*!< PWM capture B1 DMA */
  232. kPWM_CaptureA0DMAEnable = (1U << 4), /*!< PWM capture A0 DMA */
  233. kPWM_CaptureA1DMAEnable = (1U << 5) /*!< PWM capture A1 DMA */
  234. } pwm_dma_enable_t;
  235. /*! @brief List of PWM capture DMA enable source select */
  236. typedef enum _pwm_dma_source_select
  237. {
  238. kPWM_DMARequestDisable = 0U, /*!< Read DMA requests disabled */
  239. kPWM_DMAWatermarksEnable, /*!< Exceeding a FIFO watermark sets the DMA read request */
  240. kPWM_DMALocalSync, /*!< A local sync (VAL1 matches counter) sets the read DMA request */
  241. kPWM_DMALocalReload /*!< A local reload (STS[RF] being set) sets the read DMA request */
  242. } pwm_dma_source_select_t;
  243. /*! @brief PWM FIFO Watermark AND Control */
  244. typedef enum _pwm_watermark_control
  245. {
  246. kPWM_FIFOWatermarksOR = 0U, /*!< Selected FIFO watermarks are OR'ed together */
  247. kPWM_FIFOWatermarksAND /*!< Selected FIFO watermarks are AND'ed together */
  248. } pwm_watermark_control_t;
  249. /*! @brief PWM operation mode */
  250. typedef enum _pwm_mode
  251. {
  252. kPWM_SignedCenterAligned = 0U, /*!< Signed center-aligned */
  253. kPWM_CenterAligned, /*!< Unsigned cente-aligned */
  254. kPWM_SignedEdgeAligned, /*!< Signed edge-aligned */
  255. kPWM_EdgeAligned /*!< Unsigned edge-aligned */
  256. } pwm_mode_t;
  257. /*! @brief PWM output pulse mode, high-true or low-true */
  258. typedef enum _pwm_level_select
  259. {
  260. kPWM_HighTrue = 0U, /*!< High level represents "on" or "active" state */
  261. kPWM_LowTrue /*!< Low level represents "on" or "active" state */
  262. } pwm_level_select_t;
  263. /*! @brief PWM output fault status */
  264. typedef enum _pwm_fault_state
  265. {
  266. kPWM_PwmFaultState0 =
  267. 0U, /*!< Output is forced to logic 0 state prior to consideration of output polarity control. */
  268. kPWM_PwmFaultState1, /*!< Output is forced to logic 1 state prior to consideration of output polarity control. */
  269. kPWM_PwmFaultState2, /*!< Output is tristated. */
  270. kPWM_PwmFaultState3 /*!< Output is tristated. */
  271. } pwm_fault_state_t;
  272. /*! @brief PWM reload source select */
  273. typedef enum _pwm_reload_source_select
  274. {
  275. kPWM_LocalReload = 0U, /*!< The local reload signal is used to reload registers */
  276. kPWM_MasterReload /*!< The master reload signal (from submodule 0) is used to reload */
  277. } pwm_reload_source_select_t;
  278. /*! @brief PWM fault clearing options */
  279. typedef enum _pwm_fault_clear
  280. {
  281. kPWM_Automatic = 0U, /*!< Automatic fault clearing */
  282. kPWM_ManualNormal, /*!< Manual fault clearing with no fault safety mode */
  283. kPWM_ManualSafety /*!< Manual fault clearing with fault safety mode */
  284. } pwm_fault_clear_t;
  285. /*! @brief Options for submodule master control operation */
  286. typedef enum _pwm_module_control
  287. {
  288. kPWM_Control_Module_0 = (1U << 0), /*!< Control submodule 0's start/stop,buffer reload operation */
  289. kPWM_Control_Module_1 = (1U << 1), /*!< Control submodule 1's start/stop,buffer reload operation */
  290. kPWM_Control_Module_2 = (1U << 2), /*!< Control submodule 2's start/stop,buffer reload operation */
  291. kPWM_Control_Module_3 = (1U << 3) /*!< Control submodule 3's start/stop,buffer reload operation */
  292. } pwm_module_control_t;
  293. /*! @brief Structure for the user to define the PWM signal characteristics */
  294. typedef struct _pwm_signal_param
  295. {
  296. pwm_channels_t pwmChannel; /*!< PWM channel being configured; PWM A or PWM B */
  297. uint8_t dutyCyclePercent; /*!< PWM pulse width, value should be between 0 to 100
  298. 0=inactive signal(0% duty cycle)...
  299. 100=always active signal (100% duty cycle)*/
  300. pwm_level_select_t level; /*!< PWM output active level select */
  301. uint16_t deadtimeValue; /*!< The deadtime value; only used if channel pair is operating in complementary mode */
  302. pwm_fault_state_t faultState; /*!< PWM output fault status */
  303. } pwm_signal_param_t;
  304. /*!
  305. * @brief PWM config structure
  306. *
  307. * This structure holds the configuration settings for the PWM peripheral. To initialize this
  308. * structure to reasonable defaults, call the PWM_GetDefaultConfig() function and pass a
  309. * pointer to your config structure instance.
  310. *
  311. * The config struct can be made const so it resides in flash
  312. */
  313. typedef struct _pwm_config
  314. {
  315. bool enableDebugMode; /*!< true: PWM continues to run in debug mode;
  316. false: PWM is paused in debug mode */
  317. bool enableWait; /*!< true: PWM continues to run in WAIT mode;
  318. false: PWM is paused in WAIT mode */
  319. pwm_init_source_t initializationControl; /*!< Option to initialize the counter */
  320. pwm_clock_source_t clockSource; /*!< Clock source for the counter */
  321. pwm_clock_prescale_t prescale; /*!< Pre-scaler to divide down the clock */
  322. pwm_chnl_pair_operation_t pairOperation; /*!< Channel pair in indepedent or complementary mode */
  323. pwm_register_reload_t reloadLogic; /*!< PWM Reload logic setup */
  324. pwm_reload_source_select_t reloadSelect; /*!< Reload source select */
  325. pwm_load_frequency_t reloadFrequency; /*!< Specifies when to reload, used when user's choice
  326. is not immediate reload */
  327. pwm_force_output_trigger_t forceTrigger; /*!< Specify which signal will trigger a FORCE_OUT */
  328. } pwm_config_t;
  329. /*! @brief Structure for the user to configure the fault input filter. */
  330. typedef struct _pwm_fault_input_filter_param
  331. {
  332. uint8_t faultFilterCount; /*!< Fault filter count */
  333. uint8_t faultFilterPeriod; /*!< Fault filter period;value of 0 will bypass the filter */
  334. bool faultGlitchStretch; /*!< Fault Glitch Stretch Enable: A logic 1 means that input
  335. fault signals will be stretched to at least 2 IPBus clock cycles */
  336. } pwm_fault_input_filter_param_t;
  337. /*! @brief Structure is used to hold the parameters to configure a PWM fault */
  338. typedef struct _pwm_fault_param
  339. {
  340. pwm_fault_clear_t faultClearingMode; /*!< Fault clearing mode to use */
  341. bool faultLevel; /*!< true: Logic 1 indicates fault;
  342. false: Logic 0 indicates fault */
  343. bool enableCombinationalPath; /*!< true: Combinational Path from fault input is enabled;
  344. false: No combination path is available */
  345. pwm_fault_recovery_mode_t recoverMode; /*!< Specify when to re-enable the PWM output */
  346. } pwm_fault_param_t;
  347. /*!
  348. * @brief Structure is used to hold parameters to configure the capture capability of a signal pin
  349. */
  350. typedef struct _pwm_input_capture_param
  351. {
  352. bool captureInputSel; /*!< true: Use the edge counter signal as source
  353. false: Use the raw input signal from the pin as source */
  354. uint8_t edgeCompareValue; /*!< Compare value, used only if edge counter is used as source */
  355. pwm_input_capture_edge_t edge0; /*!< Specify which edge causes a capture for input circuitry 0 */
  356. pwm_input_capture_edge_t edge1; /*!< Specify which edge causes a capture for input circuitry 1 */
  357. bool enableOneShotCapture; /*!< true: Use one-shot capture mode;
  358. false: Use free-running capture mode */
  359. uint8_t fifoWatermark; /*!< Watermark level for capture FIFO. The capture flags in
  360. the status register will set if the word count in the FIFO
  361. is greater than this watermark level */
  362. } pwm_input_capture_param_t;
  363. /*******************************************************************************
  364. * API
  365. ******************************************************************************/
  366. #if defined(__cplusplus)
  367. extern "C" {
  368. #endif
  369. /*!
  370. * @name Initialization and deinitialization
  371. * @{
  372. */
  373. /*!
  374. * @brief Ungates the PWM submodule clock and configures the peripheral for basic operation.
  375. *
  376. * @note This API should be called at the beginning of the application using the PWM driver.
  377. *
  378. * @param base PWM peripheral base address
  379. * @param subModule PWM submodule to configure
  380. * @param config Pointer to user's PWM config structure.
  381. *
  382. * @return kStatus_Success means success; else failed.
  383. */
  384. status_t PWM_Init(PWM_Type *base, pwm_submodule_t subModule, const pwm_config_t *config);
  385. /*!
  386. * @brief Gate the PWM submodule clock
  387. *
  388. * @param base PWM peripheral base address
  389. * @param subModule PWM submodule to deinitialize
  390. */
  391. void PWM_Deinit(PWM_Type *base, pwm_submodule_t subModule);
  392. /*!
  393. * @brief Fill in the PWM config struct with the default settings
  394. *
  395. * The default values are:
  396. * @code
  397. * config->enableDebugMode = false;
  398. * config->enableWait = false;
  399. * config->reloadSelect = kPWM_LocalReload;
  400. * config->clockSource = kPWM_BusClock;
  401. * config->prescale = kPWM_Prescale_Divide_1;
  402. * config->initializationControl = kPWM_Initialize_LocalSync;
  403. * config->forceTrigger = kPWM_Force_Local;
  404. * config->reloadFrequency = kPWM_LoadEveryOportunity;
  405. * config->reloadLogic = kPWM_ReloadImmediate;
  406. * config->pairOperation = kPWM_Independent;
  407. * @endcode
  408. * @param config Pointer to user's PWM config structure.
  409. */
  410. void PWM_GetDefaultConfig(pwm_config_t *config);
  411. /*! @}*/
  412. /*!
  413. * @name Module PWM output
  414. * @{
  415. */
  416. /*!
  417. * @brief Sets up the PWM signals for a PWM submodule.
  418. *
  419. * The function initializes the submodule according to the parameters passed in by the user. The function
  420. * also sets up the value compare registers to match the PWM signal requirements.
  421. * If the dead time insertion logic is enabled, the pulse period is reduced by the
  422. * dead time period specified by the user.
  423. *
  424. * @param base PWM peripheral base address
  425. * @param subModule PWM submodule to configure
  426. * @param chnlParams Array of PWM channel parameters to configure the channel(s)
  427. * @param numOfChnls Number of channels to configure, this should be the size of the array passed in.
  428. * Array size should not be more than 2 as each submodule has 2 pins to output PWM
  429. * @param mode PWM operation mode, options available in enumeration ::pwm_mode_t
  430. * @param pwmFreq_Hz PWM signal frequency in Hz
  431. * @param srcClock_Hz PWM main counter clock in Hz.
  432. *
  433. * @return Returns kStatusFail if there was error setting up the signal; kStatusSuccess otherwise
  434. */
  435. status_t PWM_SetupPwm(PWM_Type *base,
  436. pwm_submodule_t subModule,
  437. const pwm_signal_param_t *chnlParams,
  438. uint8_t numOfChnls,
  439. pwm_mode_t mode,
  440. uint32_t pwmFreq_Hz,
  441. uint32_t srcClock_Hz);
  442. /*!
  443. * @brief Updates the PWM signal's dutycycle.
  444. *
  445. * The function updates the PWM dutycyle to the new value that is passed in.
  446. * If the dead time insertion logic is enabled then the pulse period is reduced by the
  447. * dead time period specified by the user.
  448. *
  449. * @param base PWM peripheral base address
  450. * @param subModule PWM submodule to configure
  451. * @param pwmSignal Signal (PWM A or PWM B) to update
  452. * @param currPwmMode The current PWM mode set during PWM setup
  453. * @param dutyCyclePercent New PWM pulse width, value should be between 0 to 100
  454. * 0=inactive signal(0% duty cycle)...
  455. * 100=active signal (100% duty cycle)
  456. */
  457. void PWM_UpdatePwmDutycycle(PWM_Type *base,
  458. pwm_submodule_t subModule,
  459. pwm_channels_t pwmSignal,
  460. pwm_mode_t currPwmMode,
  461. uint8_t dutyCyclePercent);
  462. /*!
  463. * @brief Updates the PWM signal's dutycycle with 16-bit accuracy.
  464. *
  465. * The function updates the PWM dutycyle to the new value that is passed in.
  466. * If the dead time insertion logic is enabled then the pulse period is reduced by the
  467. * dead time period specified by the user.
  468. *
  469. * @param base PWM peripheral base address
  470. * @param subModule PWM submodule to configure
  471. * @param pwmSignal Signal (PWM A or PWM B) to update
  472. * @param currPwmMode The current PWM mode set during PWM setup
  473. * @param dutyCycle New PWM pulse width, value should be between 0 to 65535
  474. * 0=inactive signal(0% duty cycle)...
  475. * 65535=active signal (100% duty cycle)
  476. */
  477. void PWM_UpdatePwmDutycycleHighAccuracy(
  478. PWM_Type *base, pwm_submodule_t subModule, pwm_channels_t pwmSignal, pwm_mode_t currPwmMode, uint16_t dutyCycle);
  479. /*! @}*/
  480. /*!
  481. * @brief Sets up the PWM input capture
  482. *
  483. * Each PWM submodule has 3 pins that can be configured for use as input capture pins. This function
  484. * sets up the capture parameters for each pin and enables the pin for input capture operation.
  485. *
  486. * @param base PWM peripheral base address
  487. * @param subModule PWM submodule to configure
  488. * @param pwmChannel Channel in the submodule to setup
  489. * @param inputCaptureParams Parameters passed in to set up the input pin
  490. */
  491. void PWM_SetupInputCapture(PWM_Type *base,
  492. pwm_submodule_t subModule,
  493. pwm_channels_t pwmChannel,
  494. const pwm_input_capture_param_t *inputCaptureParams);
  495. /*!
  496. * @brief Sets up the PWM fault input filter.
  497. *
  498. * @param base PWM peripheral base address
  499. * @param faultInputFilterParams Parameters passed in to set up the fault input filter.
  500. */
  501. void PWM_SetupFaultInputFilter(PWM_Type *base, const pwm_fault_input_filter_param_t *faultInputFilterParams);
  502. /*!
  503. * @brief Sets up the PWM fault protection.
  504. *
  505. * PWM has 4 fault inputs.
  506. *
  507. * @param base PWM peripheral base address
  508. * @param faultNum PWM fault to configure.
  509. * @param faultParams Pointer to the PWM fault config structure
  510. */
  511. void PWM_SetupFaults(PWM_Type *base, pwm_fault_input_t faultNum, const pwm_fault_param_t *faultParams);
  512. /*!
  513. * @brief Fill in the PWM fault config struct with the default settings
  514. *
  515. * The default values are:
  516. * @code
  517. * config->faultClearingMode = kPWM_Automatic;
  518. * config->faultLevel = false;
  519. * config->enableCombinationalPath = true;
  520. * config->recoverMode = kPWM_NoRecovery;
  521. * @endcode
  522. * @param config Pointer to user's PWM fault config structure.
  523. */
  524. void PWM_FaultDefaultConfig(pwm_fault_param_t *config);
  525. /*!
  526. * @brief Selects the signal to output on a PWM pin when a FORCE_OUT signal is asserted.
  527. *
  528. * The user specifies which channel to configure by supplying the submodule number and whether
  529. * to modify PWM A or PWM B within that submodule.
  530. *
  531. * @param base PWM peripheral base address
  532. * @param subModule PWM submodule to configure
  533. * @param pwmChannel Channel to configure
  534. * @param mode Signal to output when a FORCE_OUT is triggered
  535. */
  536. void PWM_SetupForceSignal(PWM_Type *base,
  537. pwm_submodule_t subModule,
  538. pwm_channels_t pwmChannel,
  539. pwm_force_signal_t mode);
  540. /*!
  541. * @name Interrupts Interface
  542. * @{
  543. */
  544. /*!
  545. * @brief Enables the selected PWM interrupts
  546. *
  547. * @param base PWM peripheral base address
  548. * @param subModule PWM submodule to configure
  549. * @param mask The interrupts to enable. This is a logical OR of members of the
  550. * enumeration ::pwm_interrupt_enable_t
  551. */
  552. void PWM_EnableInterrupts(PWM_Type *base, pwm_submodule_t subModule, uint32_t mask);
  553. /*!
  554. * @brief Disables the selected PWM interrupts
  555. *
  556. * @param base PWM peripheral base address
  557. * @param subModule PWM submodule to configure
  558. * @param mask The interrupts to enable. This is a logical OR of members of the
  559. * enumeration ::pwm_interrupt_enable_t
  560. */
  561. void PWM_DisableInterrupts(PWM_Type *base, pwm_submodule_t subModule, uint32_t mask);
  562. /*!
  563. * @brief Gets the enabled PWM interrupts
  564. *
  565. * @param base PWM peripheral base address
  566. * @param subModule PWM submodule to configure
  567. *
  568. * @return The enabled interrupts. This is the logical OR of members of the
  569. * enumeration ::pwm_interrupt_enable_t
  570. */
  571. uint32_t PWM_GetEnabledInterrupts(PWM_Type *base, pwm_submodule_t subModule);
  572. /*! @}*/
  573. /*!
  574. * @name DMA Interface
  575. * @{
  576. */
  577. /*!
  578. * @brief Capture DMA Enable Source Select.
  579. *
  580. * @param base PWM peripheral base address
  581. * @param subModule PWM submodule to configure
  582. * @param pwm_watermark_control PWM FIFO watermark and control
  583. */
  584. static inline void PWM_DMAFIFOWatermarkControl(PWM_Type *base,
  585. pwm_submodule_t subModule,
  586. pwm_watermark_control_t pwm_watermark_control)
  587. {
  588. uint16_t reg = base->SM[subModule].DMAEN;
  589. if (pwm_watermark_control == kPWM_FIFOWatermarksOR)
  590. {
  591. reg &= ~((uint16_t)PWM_DMAEN_FAND_MASK);
  592. }
  593. else
  594. {
  595. reg |= ((uint16_t)PWM_DMAEN_FAND_MASK);
  596. }
  597. base->SM[subModule].DMAEN = reg;
  598. }
  599. /*!
  600. * @brief Capture DMA Enable Source Select.
  601. *
  602. * @param base PWM peripheral base address
  603. * @param subModule PWM submodule to configure
  604. * @param pwm_dma_source_select PWM capture DMA enable source select
  605. */
  606. static inline void PWM_DMACaptureSourceSelect(PWM_Type *base,
  607. pwm_submodule_t subModule,
  608. pwm_dma_source_select_t pwm_dma_source_select)
  609. {
  610. uint16_t reg = base->SM[subModule].DMAEN;
  611. reg &= ~((uint16_t)PWM_DMAEN_CAPTDE_MASK);
  612. reg |= (((uint16_t)pwm_dma_source_select << (uint16_t)PWM_DMAEN_CAPTDE_SHIFT) & (uint16_t)PWM_DMAEN_CAPTDE_MASK);
  613. base->SM[subModule].DMAEN = reg;
  614. }
  615. /*!
  616. * @brief Enables or disables the selected PWM DMA Capture read request.
  617. *
  618. * @param base PWM peripheral base address
  619. * @param subModule PWM submodule to configure
  620. * @param mask The DMA to enable or disable. This is a logical OR of members of the
  621. * enumeration ::pwm_dma_enable_t
  622. * @param activate true: Enable DMA read request; false: Disable DMA read request
  623. */
  624. static inline void PWM_EnableDMACapture(PWM_Type *base, pwm_submodule_t subModule, uint16_t mask, bool activate)
  625. {
  626. uint16_t reg = base->SM[subModule].DMAEN;
  627. if (activate)
  628. {
  629. reg |= (uint16_t)(mask);
  630. }
  631. else
  632. {
  633. reg &= ~((uint16_t)(mask));
  634. }
  635. base->SM[subModule].DMAEN = reg;
  636. }
  637. /*!
  638. * @brief Enables or disables the PWM DMA write request.
  639. *
  640. * @param base PWM peripheral base address
  641. * @param subModule PWM submodule to configure
  642. * @param activate true: Enable DMA write request; false: Disable DMA write request
  643. */
  644. static inline void PWM_EnableDMAWrite(PWM_Type *base, pwm_submodule_t subModule, bool activate)
  645. {
  646. uint16_t reg = base->SM[subModule].DMAEN;
  647. if (activate)
  648. {
  649. reg |= ((uint16_t)PWM_DMAEN_VALDE_MASK);
  650. }
  651. else
  652. {
  653. reg &= ~((uint16_t)PWM_DMAEN_VALDE_MASK);
  654. }
  655. base->SM[subModule].DMAEN = reg;
  656. }
  657. /*! @}*/
  658. /*!
  659. * @name Status Interface
  660. * @{
  661. */
  662. /*!
  663. * @brief Gets the PWM status flags
  664. *
  665. * @param base PWM peripheral base address
  666. * @param subModule PWM submodule to configure
  667. *
  668. * @return The status flags. This is the logical OR of members of the
  669. * enumeration ::pwm_status_flags_t
  670. */
  671. uint32_t PWM_GetStatusFlags(PWM_Type *base, pwm_submodule_t subModule);
  672. /*!
  673. * @brief Clears the PWM status flags
  674. *
  675. * @param base PWM peripheral base address
  676. * @param subModule PWM submodule to configure
  677. * @param mask The status flags to clear. This is a logical OR of members of the
  678. * enumeration ::pwm_status_flags_t
  679. */
  680. void PWM_ClearStatusFlags(PWM_Type *base, pwm_submodule_t subModule, uint32_t mask);
  681. /*! @}*/
  682. /*!
  683. * @name Timer Start and Stop
  684. * @{
  685. */
  686. /*!
  687. * @brief Starts the PWM counter for a single or multiple submodules.
  688. *
  689. * Sets the Run bit which enables the clocks to the PWM submodule. This function can start multiple
  690. * submodules at the same time.
  691. *
  692. * @param base PWM peripheral base address
  693. * @param subModulesToStart PWM submodules to start. This is a logical OR of members of the
  694. * enumeration ::pwm_module_control_t
  695. */
  696. static inline void PWM_StartTimer(PWM_Type *base, uint8_t subModulesToStart)
  697. {
  698. base->MCTRL |= PWM_MCTRL_RUN(subModulesToStart);
  699. }
  700. /*!
  701. * @brief Stops the PWM counter for a single or multiple submodules.
  702. *
  703. * Clears the Run bit which resets the submodule's counter. This function can stop multiple
  704. * submodules at the same time.
  705. *
  706. * @param base PWM peripheral base address
  707. * @param subModulesToStop PWM submodules to stop. This is a logical OR of members of the
  708. * enumeration ::pwm_module_control_t
  709. */
  710. static inline void PWM_StopTimer(PWM_Type *base, uint8_t subModulesToStop)
  711. {
  712. base->MCTRL &= ~(PWM_MCTRL_RUN(subModulesToStop));
  713. }
  714. /*! @}*/
  715. /*!
  716. * @brief Enables or disables the PWM output trigger.
  717. *
  718. * This function allows the user to enable or disable the PWM trigger. The PWM has 2 triggers. Trigger 0
  719. * is activated when the counter matches VAL 0, VAL 2, or VAL 4 register. Trigger 1 is activated
  720. * when the counter matches VAL 1, VAL 3, or VAL 5 register.
  721. *
  722. * @param base PWM peripheral base address
  723. * @param subModule PWM submodule to configure
  724. * @param valueRegister Value register that will activate the trigger
  725. * @param activate true: Enable the trigger; false: Disable the trigger
  726. */
  727. static inline void PWM_OutputTriggerEnable(PWM_Type *base,
  728. pwm_submodule_t subModule,
  729. pwm_value_register_t valueRegister,
  730. bool activate)
  731. {
  732. if (activate)
  733. {
  734. base->SM[subModule].TCTRL |= ((uint16_t)1U << (uint16_t)valueRegister);
  735. }
  736. else
  737. {
  738. base->SM[subModule].TCTRL &= ~((uint16_t)1U << (uint16_t)valueRegister);
  739. }
  740. }
  741. /*!
  742. * @brief Enables the PWM output trigger.
  743. *
  744. * This function allows the user to enable one or more (VAL0-5) PWM trigger.
  745. *
  746. * @param base PWM peripheral base address
  747. * @param subModule PWM submodule to configure
  748. * @param valueRegisterMask Value register mask that will activate one or more (VAL0-5) trigger
  749. * enumeration ::_pwm_value_register_mask
  750. */
  751. static inline void PWM_ActivateOutputTrigger(PWM_Type *base, pwm_submodule_t subModule, uint16_t valueRegisterMask)
  752. {
  753. base->SM[subModule].TCTRL |= (PWM_TCTRL_OUT_TRIG_EN_MASK & (valueRegisterMask));
  754. }
  755. /*!
  756. * @brief Disables the PWM output trigger.
  757. *
  758. * This function allows the user to disables one or more (VAL0-5) PWM trigger.
  759. *
  760. * @param base PWM peripheral base address
  761. * @param subModule PWM submodule to configure
  762. * @param valueRegisterMask Value register mask that will Deactivate one or more (VAL0-5) trigger
  763. * enumeration ::_pwm_value_register_mask
  764. */
  765. static inline void PWM_DeactivateOutputTrigger(PWM_Type *base, pwm_submodule_t subModule, uint16_t valueRegisterMask)
  766. {
  767. base->SM[subModule].TCTRL &= ~(PWM_TCTRL_OUT_TRIG_EN_MASK & (valueRegisterMask));
  768. }
  769. /*!
  770. * @brief Sets the software control output for a pin to high or low.
  771. *
  772. * The user specifies which channel to modify by supplying the submodule number and whether
  773. * to modify PWM A or PWM B within that submodule.
  774. *
  775. * @param base PWM peripheral base address
  776. * @param subModule PWM submodule to configure
  777. * @param pwmChannel Channel to configure
  778. * @param value true: Supply a logic 1, false: Supply a logic 0.
  779. */
  780. static inline void PWM_SetupSwCtrlOut(PWM_Type *base, pwm_submodule_t subModule, pwm_channels_t pwmChannel, bool value)
  781. {
  782. if (value)
  783. {
  784. base->SWCOUT |=
  785. ((uint16_t)1U << (((uint16_t)subModule * (uint16_t)PWM_SUBMODULE_SWCONTROL_WIDTH) + (uint16_t)pwmChannel));
  786. }
  787. else
  788. {
  789. base->SWCOUT &=
  790. ~((uint16_t)1U << (((uint16_t)subModule * (uint16_t)PWM_SUBMODULE_SWCONTROL_WIDTH) + (uint16_t)pwmChannel));
  791. }
  792. }
  793. /*!
  794. * @brief Sets or clears the PWM LDOK bit on a single or multiple submodules
  795. *
  796. * Set LDOK bit to load buffered values into CTRL[PRSC] and the INIT, FRACVAL and VAL registers. The
  797. * values are loaded immediately if kPWM_ReloadImmediate option was choosen during config. Else the
  798. * values are loaded at the next PWM reload point.
  799. * This function can issue the load command to multiple submodules at the same time.
  800. *
  801. * @param base PWM peripheral base address
  802. * @param subModulesToUpdate PWM submodules to update with buffered values. This is a logical OR of
  803. * members of the enumeration ::pwm_module_control_t
  804. * @param value true: Set LDOK bit for the submodule list; false: Clear LDOK bit
  805. */
  806. static inline void PWM_SetPwmLdok(PWM_Type *base, uint8_t subModulesToUpdate, bool value)
  807. {
  808. if (value)
  809. {
  810. base->MCTRL |= PWM_MCTRL_LDOK(subModulesToUpdate);
  811. }
  812. else
  813. {
  814. base->MCTRL |= PWM_MCTRL_CLDOK(subModulesToUpdate);
  815. }
  816. }
  817. /*!
  818. * @brief Set PWM output fault status
  819. *
  820. * These bits determine the fault state for the PWM_A output in fault conditions
  821. * and STOP mode. It may also define the output state in WAIT and DEBUG modes
  822. * depending on the settings of CTRL2[WAITEN] and CTRL2[DBGEN].
  823. * This function can update PWM output fault status.
  824. *
  825. * @param base PWM peripheral base address
  826. * @param subModule PWM submodule to configure
  827. * @param pwmChannel Channel to configure
  828. * @param faultState PWM output fault status
  829. */
  830. static inline void PWM_SetPwmFaultState(PWM_Type *base,
  831. pwm_submodule_t subModule,
  832. pwm_channels_t pwmChannel,
  833. pwm_fault_state_t faultState)
  834. {
  835. uint16_t reg = base->SM[subModule].OCTRL;
  836. switch (pwmChannel)
  837. {
  838. case kPWM_PwmA:
  839. reg &= ~((uint16_t)PWM_OCTRL_PWMAFS_MASK);
  840. reg |= (((uint16_t)faultState << (uint16_t)PWM_OCTRL_PWMAFS_SHIFT) & (uint16_t)PWM_OCTRL_PWMAFS_MASK);
  841. break;
  842. case kPWM_PwmB:
  843. reg &= ~((uint16_t)PWM_OCTRL_PWMBFS_MASK);
  844. reg |= (((uint16_t)faultState << (uint16_t)PWM_OCTRL_PWMBFS_SHIFT) & (uint16_t)PWM_OCTRL_PWMBFS_MASK);
  845. break;
  846. case kPWM_PwmX:
  847. reg &= ~((uint16_t)PWM_OCTRL_PWMXFS_MASK);
  848. reg |= (((uint16_t)faultState << (uint16_t)PWM_OCTRL_PWMXFS_SHIFT) & (uint16_t)PWM_OCTRL_PWMXFS_MASK);
  849. break;
  850. default:
  851. assert(false);
  852. break;
  853. }
  854. base->SM[subModule].OCTRL = reg;
  855. }
  856. /*!
  857. * @brief Set PWM fault disable mapping
  858. *
  859. * Each of the four bits of this read/write field is one-to-one associated
  860. * with the four FAULTx inputs of fault channel 0/1. The PWM output will be turned
  861. * off if there is a logic 1 on an FAULTx input and a 1 in the corresponding
  862. * bit of this field. A reset sets all bits in this field.
  863. *
  864. * @param base PWM peripheral base address
  865. * @param subModule PWM submodule to configure
  866. * @param pwmChannel PWM channel to configure
  867. * @param pwm_fault_channels PWM fault channel to configure
  868. * @param value Fault disable mapping mask value
  869. * enumeration ::pwm_fault_disable_t
  870. */
  871. static inline void PWM_SetupFaultDisableMap(PWM_Type *base,
  872. pwm_submodule_t subModule,
  873. pwm_channels_t pwmChannel,
  874. pwm_fault_channels_t pwm_fault_channels,
  875. uint16_t value)
  876. {
  877. uint16_t reg = base->SM[subModule].DISMAP[pwm_fault_channels];
  878. switch (pwmChannel)
  879. {
  880. case kPWM_PwmA:
  881. reg &= ~((uint16_t)PWM_DISMAP_DIS0A_MASK);
  882. reg |= (((uint16_t)(value) << (uint16_t)PWM_DISMAP_DIS0A_SHIFT) & (uint16_t)PWM_DISMAP_DIS0A_MASK);
  883. break;
  884. case kPWM_PwmB:
  885. reg &= ~((uint16_t)PWM_DISMAP_DIS0B_MASK);
  886. reg |= (((uint16_t)(value) << (uint16_t)PWM_DISMAP_DIS0B_SHIFT) & (uint16_t)PWM_DISMAP_DIS0B_MASK);
  887. break;
  888. case kPWM_PwmX:
  889. reg &= ~((uint16_t)PWM_DISMAP_DIS0X_MASK);
  890. reg |= (((uint16_t)(value) << (uint16_t)PWM_DISMAP_DIS0X_SHIFT) & (uint16_t)PWM_DISMAP_DIS0X_MASK);
  891. break;
  892. default:
  893. assert(false);
  894. break;
  895. }
  896. base->SM[subModule].DISMAP[pwm_fault_channels] = reg;
  897. }
  898. #if defined(__cplusplus)
  899. }
  900. #endif
  901. /*! @}*/
  902. #endif /* _FSL_PWM_H_ */