fsl_sctimer.h 33 KB

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