fsl_sctimer.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. /*
  2. * Copyright (c) 2016, 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_SCTIMER_H_
  31. #define _FSL_SCTIMER_H_
  32. #include "fsl_common.h"
  33. /*!
  34. * @addtogroup sctimer
  35. * @{
  36. */
  37. /*! @file */
  38. /*******************************************************************************
  39. * Definitions
  40. ******************************************************************************/
  41. /*! @name Driver version */
  42. /*@{*/
  43. #define FSL_SCTIMER_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) /*!< Version 2.0.0 */
  44. /*@}*/
  45. /*! @brief SCTimer PWM operation modes */
  46. typedef enum _sctimer_pwm_mode
  47. {
  48. kSCTIMER_EdgeAlignedPwm = 0U, /*!< Edge-aligned PWM */
  49. kSCTIMER_CenterAlignedPwm /*!< Center-aligned PWM */
  50. } sctimer_pwm_mode_t;
  51. /*! @brief SCTimer counters when working as two independent 16-bit counters */
  52. typedef enum _sctimer_counter
  53. {
  54. kSCTIMER_Counter_L = 0U, /*!< Counter L */
  55. kSCTIMER_Counter_H /*!< Counter H */
  56. } sctimer_counter_t;
  57. /*! @brief List of SCTimer input pins */
  58. typedef enum _sctimer_input
  59. {
  60. kSCTIMER_Input_0 = 0U, /*!< SCTIMER input 0 */
  61. kSCTIMER_Input_1, /*!< SCTIMER input 1 */
  62. kSCTIMER_Input_2, /*!< SCTIMER input 2 */
  63. kSCTIMER_Input_3, /*!< SCTIMER input 3 */
  64. kSCTIMER_Input_4, /*!< SCTIMER input 4 */
  65. kSCTIMER_Input_5, /*!< SCTIMER input 5 */
  66. kSCTIMER_Input_6, /*!< SCTIMER input 6 */
  67. kSCTIMER_Input_7 /*!< SCTIMER input 7 */
  68. } sctimer_input_t;
  69. /*! @brief List of SCTimer output pins */
  70. typedef enum _sctimer_out
  71. {
  72. kSCTIMER_Out_0 = 0U, /*!< SCTIMER output 0*/
  73. kSCTIMER_Out_1, /*!< SCTIMER output 1 */
  74. kSCTIMER_Out_2, /*!< SCTIMER output 2 */
  75. kSCTIMER_Out_3, /*!< SCTIMER output 3 */
  76. kSCTIMER_Out_4, /*!< SCTIMER output 4 */
  77. kSCTIMER_Out_5, /*!< SCTIMER output 5 */
  78. kSCTIMER_Out_6, /*!< SCTIMER output 6 */
  79. kSCTIMER_Out_7 /*!< SCTIMER output 7 */
  80. } sctimer_out_t;
  81. /*! @brief SCTimer PWM output pulse mode: high-true, low-true or no output */
  82. typedef enum _sctimer_pwm_level_select
  83. {
  84. kSCTIMER_LowTrue = 0U, /*!< Low true pulses */
  85. kSCTIMER_HighTrue /*!< High true pulses */
  86. } sctimer_pwm_level_select_t;
  87. /*! @brief Options to configure a SCTimer PWM signal */
  88. typedef struct _sctimer_pwm_signal_param
  89. {
  90. sctimer_out_t output; /*!< The output pin to use to generate the PWM signal */
  91. sctimer_pwm_level_select_t level; /*!< PWM output active level select. */
  92. uint8_t dutyCyclePercent; /*!< PWM pulse width, value should be between 1 to 100
  93. 100 = always active signal (100% duty cycle).*/
  94. } sctimer_pwm_signal_param_t;
  95. /*! @brief SCTimer clock mode options */
  96. typedef enum _sctimer_clock_mode
  97. {
  98. kSCTIMER_System_ClockMode = 0U, /*!< System Clock Mode */
  99. kSCTIMER_Sampled_ClockMode, /*!< Sampled System Clock Mode */
  100. kSCTIMER_Input_ClockMode, /*!< SCT Input Clock Mode */
  101. kSCTIMER_Asynchronous_ClockMode /*!< Asynchronous Mode */
  102. } sctimer_clock_mode_t;
  103. /*! @brief SCTimer clock select options */
  104. typedef enum _sctimer_clock_select
  105. {
  106. kSCTIMER_Clock_On_Rise_Input_0 = 0U, /*!< Rising edges on input 0 */
  107. kSCTIMER_Clock_On_Fall_Input_0, /*!< Falling edges on input 0 */
  108. kSCTIMER_Clock_On_Rise_Input_1, /*!< Rising edges on input 1 */
  109. kSCTIMER_Clock_On_Fall_Input_1, /*!< Falling edges on input 1 */
  110. kSCTIMER_Clock_On_Rise_Input_2, /*!< Rising edges on input 2 */
  111. kSCTIMER_Clock_On_Fall_Input_2, /*!< Falling edges on input 2 */
  112. kSCTIMER_Clock_On_Rise_Input_3, /*!< Rising edges on input 3 */
  113. kSCTIMER_Clock_On_Fall_Input_3, /*!< Falling edges on input 3 */
  114. kSCTIMER_Clock_On_Rise_Input_4, /*!< Rising edges on input 4 */
  115. kSCTIMER_Clock_On_Fall_Input_4, /*!< Falling edges on input 4 */
  116. kSCTIMER_Clock_On_Rise_Input_5, /*!< Rising edges on input 5 */
  117. kSCTIMER_Clock_On_Fall_Input_5, /*!< Falling edges on input 5 */
  118. kSCTIMER_Clock_On_Rise_Input_6, /*!< Rising edges on input 6 */
  119. kSCTIMER_Clock_On_Fall_Input_6, /*!< Falling edges on input 6 */
  120. kSCTIMER_Clock_On_Rise_Input_7, /*!< Rising edges on input 7 */
  121. kSCTIMER_Clock_On_Fall_Input_7 /*!< Falling edges on input 7 */
  122. } sctimer_clock_select_t;
  123. /*!
  124. * @brief SCTimer output conflict resolution options.
  125. *
  126. * Specifies what action should be taken if multiple events dictate that a given output should be
  127. * both set and cleared at the same time
  128. */
  129. typedef enum _sctimer_conflict_resolution
  130. {
  131. kSCTIMER_ResolveNone = 0U, /*!< No change */
  132. kSCTIMER_ResolveSet, /*!< Set output */
  133. kSCTIMER_ResolveClear, /*!< Clear output */
  134. kSCTIMER_ResolveToggle /*!< Toggle output */
  135. } sctimer_conflict_resolution_t;
  136. /*! @brief List of SCTimer event types */
  137. typedef enum _sctimer_event
  138. {
  139. kSCTIMER_InputLowOrMatchEvent =
  140. (0 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (0 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (0 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  141. kSCTIMER_InputRiseOrMatchEvent =
  142. (0 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (1 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (0 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  143. kSCTIMER_InputFallOrMatchEvent =
  144. (0 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (2 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (0 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  145. kSCTIMER_InputHighOrMatchEvent =
  146. (0 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (3 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (0 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  147. kSCTIMER_MatchEventOnly =
  148. (1 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (0 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (0 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  149. kSCTIMER_InputLowEvent =
  150. (2 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (0 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (0 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  151. kSCTIMER_InputRiseEvent =
  152. (2 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (1 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (0 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  153. kSCTIMER_InputFallEvent =
  154. (2 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (2 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (0 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  155. kSCTIMER_InputHighEvent =
  156. (2 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (3 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (0 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  157. kSCTIMER_InputLowAndMatchEvent =
  158. (3 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (0 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (0 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  159. kSCTIMER_InputRiseAndMatchEvent =
  160. (3 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (1 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (0 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  161. kSCTIMER_InputFallAndMatchEvent =
  162. (3 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (2 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (0 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  163. kSCTIMER_InputHighAndMatchEvent =
  164. (3 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (3 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (0 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  165. kSCTIMER_OutputLowOrMatchEvent =
  166. (0 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (0 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (1 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  167. kSCTIMER_OutputRiseOrMatchEvent =
  168. (0 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (1 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (1 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  169. kSCTIMER_OutputFallOrMatchEvent =
  170. (0 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (2 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (1 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  171. kSCTIMER_OutputHighOrMatchEvent =
  172. (0 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (3 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (1 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  173. kSCTIMER_OutputLowEvent =
  174. (2 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (0 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (1 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  175. kSCTIMER_OutputRiseEvent =
  176. (2 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (1 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (1 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  177. kSCTIMER_OutputFallEvent =
  178. (2 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (2 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (1 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  179. kSCTIMER_OutputHighEvent =
  180. (2 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (3 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (1 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  181. kSCTIMER_OutputLowAndMatchEvent =
  182. (3 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (0 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (1 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  183. kSCTIMER_OutputRiseAndMatchEvent =
  184. (3 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (1 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (1 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  185. kSCTIMER_OutputFallAndMatchEvent =
  186. (3 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (2 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (1 << SCT_EVENT_CTRL_OUTSEL_SHIFT),
  187. kSCTIMER_OutputHighAndMatchEvent =
  188. (3 << SCT_EVENT_CTRL_COMBMODE_SHIFT) + (3 << SCT_EVENT_CTRL_IOCOND_SHIFT) + (1 << SCT_EVENT_CTRL_OUTSEL_SHIFT)
  189. } sctimer_event_t;
  190. /*! @brief SCTimer callback typedef. */
  191. typedef void (*sctimer_event_callback_t)(void);
  192. /*! @brief List of SCTimer interrupts */
  193. typedef enum _sctimer_interrupt_enable
  194. {
  195. kSCTIMER_Event0InterruptEnable = (1U << 0), /*!< Event 0 interrupt */
  196. kSCTIMER_Event1InterruptEnable = (1U << 1), /*!< Event 1 interrupt */
  197. kSCTIMER_Event2InterruptEnable = (1U << 2), /*!< Event 2 interrupt */
  198. kSCTIMER_Event3InterruptEnable = (1U << 3), /*!< Event 3 interrupt */
  199. kSCTIMER_Event4InterruptEnable = (1U << 4), /*!< Event 4 interrupt */
  200. kSCTIMER_Event5InterruptEnable = (1U << 5), /*!< Event 5 interrupt */
  201. kSCTIMER_Event6InterruptEnable = (1U << 6), /*!< Event 6 interrupt */
  202. kSCTIMER_Event7InterruptEnable = (1U << 7), /*!< Event 7 interrupt */
  203. kSCTIMER_Event8InterruptEnable = (1U << 8), /*!< Event 8 interrupt */
  204. kSCTIMER_Event9InterruptEnable = (1U << 9), /*!< Event 9 interrupt */
  205. kSCTIMER_Event10InterruptEnable = (1U << 10), /*!< Event 10 interrupt */
  206. kSCTIMER_Event11InterruptEnable = (1U << 11), /*!< Event 11 interrupt */
  207. kSCTIMER_Event12InterruptEnable = (1U << 12), /*!< Event 12 interrupt */
  208. } sctimer_interrupt_enable_t;
  209. /*! @brief List of SCTimer flags */
  210. typedef enum _sctimer_status_flags
  211. {
  212. kSCTIMER_Event0Flag = (1U << 0), /*!< Event 0 Flag */
  213. kSCTIMER_Event1Flag = (1U << 1), /*!< Event 1 Flag */
  214. kSCTIMER_Event2Flag = (1U << 2), /*!< Event 2 Flag */
  215. kSCTIMER_Event3Flag = (1U << 3), /*!< Event 3 Flag */
  216. kSCTIMER_Event4Flag = (1U << 4), /*!< Event 4 Flag */
  217. kSCTIMER_Event5Flag = (1U << 5), /*!< Event 5 Flag */
  218. kSCTIMER_Event6Flag = (1U << 6), /*!< Event 6 Flag */
  219. kSCTIMER_Event7Flag = (1U << 7), /*!< Event 7 Flag */
  220. kSCTIMER_Event8Flag = (1U << 8), /*!< Event 8 Flag */
  221. kSCTIMER_Event9Flag = (1U << 9), /*!< Event 9 Flag */
  222. kSCTIMER_Event10Flag = (1U << 10), /*!< Event 10 Flag */
  223. kSCTIMER_Event11Flag = (1U << 11), /*!< Event 11 Flag */
  224. kSCTIMER_Event12Flag = (1U << 12), /*!< Event 12 Flag */
  225. kSCTIMER_BusErrorLFlag =
  226. (1U << SCT_CONFLAG_BUSERRL_SHIFT), /*!< Bus error due to write when L counter was not halted */
  227. kSCTIMER_BusErrorHFlag =
  228. (1U << SCT_CONFLAG_BUSERRH_SHIFT) /*!< Bus error due to write when H counter was not halted */
  229. } sctimer_status_flags_t;
  230. /*!
  231. * @brief SCTimer configuration structure
  232. *
  233. * This structure holds the configuration settings for the SCTimer peripheral. To initialize this
  234. * structure to reasonable defaults, call the SCTMR_GetDefaultConfig() function and pass a
  235. * pointer to the configuration structure instance.
  236. *
  237. * The configuration structure can be made constant so as to reside in flash.
  238. */
  239. typedef struct _sctimer_config
  240. {
  241. bool enableCounterUnify; /*!< true: SCT operates as a unified 32-bit counter;
  242. false: SCT operates as two 16-bit counters */
  243. sctimer_clock_mode_t clockMode; /*!< SCT clock mode value */
  244. sctimer_clock_select_t clockSelect; /*!< SCT clock select value */
  245. bool enableBidirection_l; /*!< true: Up-down count mode for the L or unified counter
  246. false: Up count mode only for the L or unified counter */
  247. bool enableBidirection_h; /*!< true: Up-down count mode for the H or unified counter
  248. false: Up count mode only for the H or unified counter.
  249. This field is used only if the enableCounterUnify is set
  250. to false */
  251. uint8_t prescale_l; /*!< Prescale value to produce the L or unified counter clock */
  252. uint8_t prescale_h; /*!< Prescale value to produce the H counter clock.
  253. This field is used only if the enableCounterUnify is set
  254. to false */
  255. uint8_t outInitState; /*!< Defines the initial output value */
  256. } sctimer_config_t;
  257. /*******************************************************************************
  258. * API
  259. ******************************************************************************/
  260. #if defined(__cplusplus)
  261. extern "C" {
  262. #endif
  263. /*!
  264. * @name Initialization and deinitialization
  265. * @{
  266. */
  267. /*!
  268. * @brief Ungates the SCTimer clock and configures the peripheral for basic operation.
  269. *
  270. * @note This API should be called at the beginning of the application using the SCTimer driver.
  271. *
  272. * @param base SCTimer peripheral base address
  273. * @param config Pointer to the user configuration structure.
  274. *
  275. * @return kStatus_Success indicates success; Else indicates failure.
  276. */
  277. status_t SCTIMER_Init(SCT_Type *base, const sctimer_config_t *config);
  278. /*!
  279. * @brief Gates the SCTimer clock.
  280. *
  281. * @param base SCTimer peripheral base address
  282. */
  283. void SCTIMER_Deinit(SCT_Type *base);
  284. /*!
  285. * @brief Fills in the SCTimer configuration structure with the default settings.
  286. *
  287. * The default values are:
  288. * @code
  289. * config->enableCounterUnify = true;
  290. * config->clockMode = kSCTIMER_System_ClockMode;
  291. * config->clockSelect = kSCTIMER_Clock_On_Rise_Input_0;
  292. * config->enableBidirection_l = false;
  293. * config->enableBidirection_h = false;
  294. * config->prescale_l = 0;
  295. * config->prescale_h = 0;
  296. * config->outInitState = 0;
  297. * @endcode
  298. * @param config Pointer to the user configuration structure.
  299. */
  300. void SCTIMER_GetDefaultConfig(sctimer_config_t *config);
  301. /*! @}*/
  302. /*!
  303. * @name PWM setup operations
  304. * @{
  305. */
  306. /*!
  307. * @brief Configures the PWM signal parameters.
  308. *
  309. * Call this function to configure the PWM signal period, mode, duty cycle, and edge. This
  310. * function will create 2 events; one of the events will trigger on match with the pulse value
  311. * and the other will trigger when the counter matches the PWM period. The PWM period event is
  312. * also used as a limit event to reset the counter or change direction. Both events are enabled
  313. * for the same state. The state number can be retrieved by calling the function
  314. * SCTIMER_GetCurrentStateNumber().
  315. * The counter is set to operate as one 32-bit counter (unify bit is set to 1).
  316. * The counter operates in bi-directional mode when generating a center-aligned PWM.
  317. *
  318. * @note When setting PWM output from multiple output pins, they all should use the same PWM mode
  319. * i.e all PWM's should be either edge-aligned or center-aligned.
  320. * When using this API, the PWM signal frequency of all the initialized channels must be the same.
  321. * Otherwise all the initialized channels' PWM signal frequency is equal to the last call to the
  322. * API's pwmFreq_Hz.
  323. *
  324. * @param base SCTimer peripheral base address
  325. * @param pwmParams PWM parameters to configure the output
  326. * @param mode PWM operation mode, options available in enumeration ::sctimer_pwm_mode_t
  327. * @param pwmFreq_Hz PWM signal frequency in Hz
  328. * @param srcClock_Hz SCTimer counter clock in Hz
  329. * @param event Pointer to a variable where the PWM period event number is stored
  330. *
  331. * @return kStatus_Success on success
  332. * kStatus_Fail If we have hit the limit in terms of number of events created or if
  333. * an incorrect PWM dutycylce is passed in.
  334. */
  335. status_t SCTIMER_SetupPwm(SCT_Type *base,
  336. const sctimer_pwm_signal_param_t *pwmParams,
  337. sctimer_pwm_mode_t mode,
  338. uint32_t pwmFreq_Hz,
  339. uint32_t srcClock_Hz,
  340. uint32_t *event);
  341. /*!
  342. * @brief Updates the duty cycle of an active PWM signal.
  343. *
  344. * @param base SCTimer peripheral base address
  345. * @param output The output to configure
  346. * @param dutyCyclePercent New PWM pulse width; the value should be between 1 to 100
  347. * @param event Event number associated with this PWM signal. This was returned to the user by the
  348. * function SCTIMER_SetupPwm().
  349. */
  350. void SCTIMER_UpdatePwmDutycycle(SCT_Type *base, sctimer_out_t output, uint8_t dutyCyclePercent, uint32_t event);
  351. /*!
  352. * @name Interrupt Interface
  353. * @{
  354. */
  355. /*!
  356. * @brief Enables the selected SCTimer interrupts.
  357. *
  358. * @param base SCTimer peripheral base address
  359. * @param mask The interrupts to enable. This is a logical OR of members of the
  360. * enumeration ::sctimer_interrupt_enable_t
  361. */
  362. static inline void SCTIMER_EnableInterrupts(SCT_Type *base, uint32_t mask)
  363. {
  364. base->EVEN |= mask;
  365. }
  366. /*!
  367. * @brief Disables the selected SCTimer interrupts.
  368. *
  369. * @param base SCTimer peripheral base address
  370. * @param mask The interrupts to enable. This is a logical OR of members of the
  371. * enumeration ::sctimer_interrupt_enable_t
  372. */
  373. static inline void SCTIMER_DisableInterrupts(SCT_Type *base, uint32_t mask)
  374. {
  375. base->EVEN &= ~mask;
  376. }
  377. /*!
  378. * @brief Gets the enabled SCTimer interrupts.
  379. *
  380. * @param base SCTimer peripheral base address
  381. *
  382. * @return The enabled interrupts. This is the logical OR of members of the
  383. * enumeration ::sctimer_interrupt_enable_t
  384. */
  385. static inline uint32_t SCTIMER_GetEnabledInterrupts(SCT_Type *base)
  386. {
  387. return (base->EVEN & 0xFFFFU);
  388. }
  389. /*! @}*/
  390. /*!
  391. * @name Status Interface
  392. * @{
  393. */
  394. /*!
  395. * @brief Gets the SCTimer status flags.
  396. *
  397. * @param base SCTimer peripheral base address
  398. *
  399. * @return The status flags. This is the logical OR of members of the
  400. * enumeration ::sctimer_status_flags_t
  401. */
  402. static inline uint32_t SCTIMER_GetStatusFlags(SCT_Type *base)
  403. {
  404. uint32_t statusFlags = 0;
  405. /* Add the recorded events */
  406. statusFlags = (base->EVFLAG & 0xFFFFU);
  407. /* Add bus error flags */
  408. statusFlags |= (base->CONFLAG & (SCT_CONFLAG_BUSERRL_MASK | SCT_CONFLAG_BUSERRH_MASK));
  409. return statusFlags;
  410. }
  411. /*!
  412. * @brief Clears the SCTimer status flags.
  413. *
  414. * @param base SCTimer peripheral base address
  415. * @param mask The status flags to clear. This is a logical OR of members of the
  416. * enumeration ::sctimer_status_flags_t
  417. */
  418. static inline void SCTIMER_ClearStatusFlags(SCT_Type *base, uint32_t mask)
  419. {
  420. /* Write to the flag registers */
  421. base->EVFLAG = (mask & 0xFFFFU);
  422. base->CONFLAG = (mask & (SCT_CONFLAG_BUSERRL_MASK | SCT_CONFLAG_BUSERRH_MASK));
  423. }
  424. /*! @}*/
  425. /*!
  426. * @name Counter Start and Stop
  427. * @{
  428. */
  429. /*!
  430. * @brief Starts the SCTimer counter.
  431. *
  432. * @param base SCTimer peripheral base address
  433. * @param countertoStart SCTimer counter to start; if unify mode is set then function always
  434. * writes to HALT_L bit
  435. */
  436. static inline void SCTIMER_StartTimer(SCT_Type *base, sctimer_counter_t countertoStart)
  437. {
  438. /* Clear HALT_L bit if counter is operating in 32-bit mode or user wants to start L counter */
  439. if ((base->CONFIG & SCT_CONFIG_UNIFY_MASK) || (countertoStart == kSCTIMER_Counter_L))
  440. {
  441. base->CTRL &= ~(SCT_CTRL_HALT_L_MASK);
  442. }
  443. else
  444. {
  445. /* Start H counter */
  446. base->CTRL &= ~(SCT_CTRL_HALT_H_MASK);
  447. }
  448. }
  449. /*!
  450. * @brief Halts the SCTimer counter.
  451. *
  452. * @param base SCTimer peripheral base address
  453. * @param countertoStop SCTimer counter to stop; if unify mode is set then function always
  454. * writes to HALT_L bit
  455. */
  456. static inline void SCTIMER_StopTimer(SCT_Type *base, sctimer_counter_t countertoStop)
  457. {
  458. /* Set HALT_L bit if counter is operating in 32-bit mode or user wants to stop L counter */
  459. if ((base->CONFIG & SCT_CONFIG_UNIFY_MASK) || (countertoStop == kSCTIMER_Counter_L))
  460. {
  461. base->CTRL |= (SCT_CTRL_HALT_L_MASK);
  462. }
  463. else
  464. {
  465. /* Stop H counter */
  466. base->CTRL |= (SCT_CTRL_HALT_H_MASK);
  467. }
  468. }
  469. /*! @}*/
  470. /*!
  471. * @name Functions to create a new event and manage the state logic
  472. * @{
  473. */
  474. /*!
  475. * @brief Create an event that is triggered on a match or IO and schedule in current state.
  476. *
  477. * This function will configure an event using the options provided by the user. If the event type uses
  478. * the counter match, then the function will set the user provided match value into a match register
  479. * and put this match register number into the event control register.
  480. * The event is enabled for the current state and the event number is increased by one at the end.
  481. * The function returns the event number; this event number can be used to configure actions to be
  482. * done when this event is triggered.
  483. *
  484. * @param base SCTimer peripheral base address
  485. * @param howToMonitor Event type; options are available in the enumeration ::sctimer_interrupt_enable_t
  486. * @param matchValue The match value that will be programmed to a match register
  487. * @param whichIO The input or output that will be involved in event triggering. This field
  488. * is ignored if the event type is "match only"
  489. * @param whichCounter SCTimer counter to use when operating in 16-bit mode. In 32-bit mode, this
  490. * field has no meaning as we have only 1 unified counter; hence ignored.
  491. * @param event Pointer to a variable where the new event number is stored
  492. *
  493. * @return kStatus_Success on success
  494. * kStatus_Error if we have hit the limit in terms of number of events created or
  495. if we have reached the limit in terms of number of match registers
  496. */
  497. status_t SCTIMER_CreateAndScheduleEvent(SCT_Type *base,
  498. sctimer_event_t howToMonitor,
  499. uint32_t matchValue,
  500. uint32_t whichIO,
  501. sctimer_counter_t whichCounter,
  502. uint32_t *event);
  503. /*!
  504. * @brief Enable an event in the current state.
  505. *
  506. * This function will allow the event passed in to trigger in the current state. The event must
  507. * be created earlier by either calling the function SCTIMER_SetupPwm() or function
  508. * SCTIMER_CreateAndScheduleEvent() .
  509. *
  510. * @param base SCTimer peripheral base address
  511. * @param event Event number to enable in the current state
  512. *
  513. */
  514. void SCTIMER_ScheduleEvent(SCT_Type *base, uint32_t event);
  515. /*!
  516. * @brief Increase the state by 1
  517. *
  518. * All future events created by calling the function SCTIMER_ScheduleEvent() will be enabled in this new
  519. * state.
  520. *
  521. * @param base SCTimer peripheral base address
  522. *
  523. * @return kStatus_Success on success
  524. * kStatus_Error if we have hit the limit in terms of states used
  525. */
  526. status_t SCTIMER_IncreaseState(SCT_Type *base);
  527. /*!
  528. * @brief Provides the current state
  529. *
  530. * User can use this to set the next state by calling the function SCTIMER_SetupNextStateAction().
  531. *
  532. * @param base SCTimer peripheral base address
  533. *
  534. * @return The current state
  535. */
  536. uint32_t SCTIMER_GetCurrentState(SCT_Type *base);
  537. /*! @}*/
  538. /*!
  539. * @name Actions to take in response to an event
  540. * @{
  541. */
  542. /*!
  543. * @brief Setup capture of the counter value on trigger of a selected event
  544. *
  545. * @param base SCTimer peripheral base address
  546. * @param whichCounter SCTimer counter to use when operating in 16-bit mode. In 32-bit mode, this
  547. * field has no meaning as only the Counter_L bits are used.
  548. * @param captureRegister Pointer to a variable where the capture register number will be returned. User
  549. * can read the captured value from this register when the specified event is triggered.
  550. * @param event Event number that will trigger the capture
  551. *
  552. * @return kStatus_Success on success
  553. * kStatus_Error if we have hit the limit in terms of number of match/capture registers available
  554. */
  555. status_t SCTIMER_SetupCaptureAction(SCT_Type *base,
  556. sctimer_counter_t whichCounter,
  557. uint32_t *captureRegister,
  558. uint32_t event);
  559. /*!
  560. * @brief Receive noticification when the event trigger an interrupt.
  561. *
  562. * If the interrupt for the event is enabled by the user, then a callback can be registered
  563. * which will be invoked when the event is triggered
  564. *
  565. * @param base SCTimer peripheral base address
  566. * @param event Event number that will trigger the interrupt
  567. * @param callback Function to invoke when the event is triggered
  568. */
  569. void SCTIMER_SetCallback(SCT_Type *base, sctimer_event_callback_t callback, uint32_t event);
  570. /*!
  571. * @brief Transition to the specified state.
  572. *
  573. * This transition will be triggered by the event number that is passed in by the user.
  574. *
  575. * @param base SCTimer peripheral base address
  576. * @param nextState The next state SCTimer will transition to
  577. * @param event Event number that will trigger the state transition
  578. */
  579. static inline void SCTIMER_SetupNextStateAction(SCT_Type *base, uint32_t nextState, uint32_t event)
  580. {
  581. uint32_t reg = base->EVENT[event].CTRL;
  582. reg &= ~(SCT_EVENT_CTRL_STATEV_MASK);
  583. /* Load the STATEV value when the event occurs to be the next state */
  584. reg |= SCT_EVENT_CTRL_STATEV(nextState) | SCT_EVENT_CTRL_STATELD_MASK;
  585. base->EVENT[event].CTRL = reg;
  586. }
  587. /*!
  588. * @brief Set the Output.
  589. *
  590. * This output will be set when the event number that is passed in by the user is triggered.
  591. *
  592. * @param base SCTimer peripheral base address
  593. * @param whichIO The output to set
  594. * @param event Event number that will trigger the output change
  595. */
  596. static inline void SCTIMER_SetupOutputSetAction(SCT_Type *base, uint32_t whichIO, uint32_t event)
  597. {
  598. base->OUT[whichIO].SET |= (1U << event);
  599. }
  600. /*!
  601. * @brief Clear the Output.
  602. *
  603. * This output will be cleared when the event number that is passed in by the user is triggered.
  604. *
  605. * @param base SCTimer peripheral base address
  606. * @param whichIO The output to clear
  607. * @param event Event number that will trigger the output change
  608. */
  609. static inline void SCTIMER_SetupOutputClearAction(SCT_Type *base, uint32_t whichIO, uint32_t event)
  610. {
  611. base->OUT[whichIO].CLR |= (1U << event);
  612. }
  613. /*!
  614. * @brief Toggle the output level.
  615. *
  616. * This change in the output level is triggered by the event number that is passed in by the user.
  617. *
  618. * @param base SCTimer peripheral base address
  619. * @param whichIO The output to toggle
  620. * @param event Event number that will trigger the output change
  621. */
  622. void SCTIMER_SetupOutputToggleAction(SCT_Type *base, uint32_t whichIO, uint32_t event);
  623. /*!
  624. * @brief Limit the running counter.
  625. *
  626. * The counter is limited when the event number that is passed in by the user is triggered.
  627. *
  628. * @param base SCTimer peripheral base address
  629. * @param whichCounter SCTimer counter to use when operating in 16-bit mode. In 32-bit mode, this
  630. * field has no meaning as only the Counter_L bits are used.
  631. * @param event Event number that will trigger the counter to be limited
  632. */
  633. static inline void SCTIMER_SetupCounterLimitAction(SCT_Type *base, sctimer_counter_t whichCounter, uint32_t event)
  634. {
  635. /* Use Counter_L bits if counter is operating in 32-bit mode or user wants to setup the L counter */
  636. if ((base->CONFIG & SCT_CONFIG_UNIFY_MASK) || (whichCounter == kSCTIMER_Counter_L))
  637. {
  638. base->LIMIT |= SCT_LIMIT_LIMMSK_L(1U << event);
  639. }
  640. else
  641. {
  642. base->LIMIT |= SCT_LIMIT_LIMMSK_H(1U << event);
  643. }
  644. }
  645. /*!
  646. * @brief Stop the running counter.
  647. *
  648. * The counter is stopped when the event number that is passed in by the user is triggered.
  649. *
  650. * @param base SCTimer peripheral base address
  651. * @param whichCounter SCTimer counter to use when operating in 16-bit mode. In 32-bit mode, this
  652. * field has no meaning as only the Counter_L bits are used.
  653. * @param event Event number that will trigger the counter to be stopped
  654. */
  655. static inline void SCTIMER_SetupCounterStopAction(SCT_Type *base, sctimer_counter_t whichCounter, uint32_t event)
  656. {
  657. /* Use Counter_L bits if counter is operating in 32-bit mode or user wants to setup the L counter */
  658. if ((base->CONFIG & SCT_CONFIG_UNIFY_MASK) || (whichCounter == kSCTIMER_Counter_L))
  659. {
  660. base->STOP |= SCT_STOP_STOPMSK_L(1U << event);
  661. }
  662. else
  663. {
  664. base->STOP |= SCT_STOP_STOPMSK_H(1U << event);
  665. }
  666. }
  667. /*!
  668. * @brief Re-start the stopped counter.
  669. *
  670. * The counter will re-start when the event number that is passed in by the user is triggered.
  671. *
  672. * @param base SCTimer peripheral base address
  673. * @param whichCounter SCTimer counter to use when operating in 16-bit mode. In 32-bit mode, this
  674. * field has no meaning as only the Counter_L bits are used.
  675. * @param event Event number that will trigger the counter to re-start
  676. */
  677. static inline void SCTIMER_SetupCounterStartAction(SCT_Type *base, sctimer_counter_t whichCounter, uint32_t event)
  678. {
  679. /* Use Counter_L bits if counter is operating in 32-bit mode or user wants to setup the L counter */
  680. if ((base->CONFIG & SCT_CONFIG_UNIFY_MASK) || (whichCounter == kSCTIMER_Counter_L))
  681. {
  682. base->START |= SCT_START_STARTMSK_L(1U << event);
  683. }
  684. else
  685. {
  686. base->START |= SCT_START_STARTMSK_H(1U << event);
  687. }
  688. }
  689. /*!
  690. * @brief Halt the running counter.
  691. *
  692. * The counter is disabled (halted) when the event number that is passed in by the user is
  693. * triggered. When the counter is halted, all further events are disabled. The HALT condition
  694. * can only be removed by calling the SCTIMER_StartTimer() function.
  695. *
  696. * @param base SCTimer peripheral base address
  697. * @param whichCounter SCTimer counter to use when operating in 16-bit mode. In 32-bit mode, this
  698. * field has no meaning as only the Counter_L bits are used.
  699. * @param event Event number that will trigger the counter to be halted
  700. */
  701. static inline void SCTIMER_SetupCounterHaltAction(SCT_Type *base, sctimer_counter_t whichCounter, uint32_t event)
  702. {
  703. /* Use Counter_L bits if counter is operating in 32-bit mode or user wants to setup the L counter */
  704. if ((base->CONFIG & SCT_CONFIG_UNIFY_MASK) || (whichCounter == kSCTIMER_Counter_L))
  705. {
  706. base->HALT |= SCT_HALT_HALTMSK_L(1U << event);
  707. }
  708. else
  709. {
  710. base->HALT |= SCT_HALT_HALTMSK_H(1U << event);
  711. }
  712. }
  713. /*!
  714. * @brief Generate a DMA request.
  715. *
  716. * DMA request will be triggered by the event number that is passed in by the user.
  717. *
  718. * @param base SCTimer peripheral base address
  719. * @param dmaNumber The DMA request to generate
  720. * @param event Event number that will trigger the DMA request
  721. */
  722. static inline void SCTIMER_SetupDmaTriggerAction(SCT_Type *base, uint32_t dmaNumber, uint32_t event)
  723. {
  724. if (dmaNumber == 0)
  725. {
  726. base->DMA0REQUEST |= (1U << event);
  727. }
  728. else
  729. {
  730. base->DMA1REQUEST |= (1U << event);
  731. }
  732. }
  733. /*!
  734. * @brief SCTimer interrupt handler.
  735. *
  736. * @param base SCTimer peripheral base address.
  737. */
  738. void SCTIMER_EventHandleIRQ(SCT_Type *base);
  739. /*! @}*/
  740. #if defined(__cplusplus)
  741. }
  742. #endif
  743. /*! @}*/
  744. #endif /* _FSL_SCTIMER_H_ */