fsl_cmt.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef _FSL_CMT_H_
  31. #define _FSL_CMT_H_
  32. #include "fsl_common.h"
  33. /*!
  34. * @addtogroup cmt
  35. * @{
  36. */
  37. /*******************************************************************************
  38. * Definitions
  39. ******************************************************************************/
  40. /*! @name Driver version */
  41. /*@{*/
  42. /*! @brief CMT driver version 2.0.1. */
  43. #define FSL_CMT_DRIVER_VERSION (MAKE_VERSION(2, 0, 1))
  44. /*@}*/
  45. /*!
  46. * @brief The modes of CMT.
  47. */
  48. typedef enum _cmt_mode
  49. {
  50. kCMT_DirectIROCtl = 0x00U, /*!< Carrier modulator is disabled and the IRO signal is directly in software control */
  51. kCMT_TimeMode = 0x01U, /*!< Carrier modulator is enabled in time mode. */
  52. kCMT_FSKMode = 0x05U, /*!< Carrier modulator is enabled in FSK mode. */
  53. kCMT_BasebandMode = 0x09U /*!< Carrier modulator is enabled in baseband mode. */
  54. } cmt_mode_t;
  55. /*!
  56. * @brief The CMT clock divide primary prescaler.
  57. * The primary clock divider is used to divider the bus clock to
  58. * get the intermediate frequency to approximately equal to 8 MHZ.
  59. * When the bus clock is 8 MHZ, set primary prescaler to "kCMT_PrimaryClkDiv1".
  60. */
  61. typedef enum _cmt_primary_clkdiv
  62. {
  63. kCMT_PrimaryClkDiv1 = 0U, /*!< The intermediate frequency is the bus clock divided by 1. */
  64. kCMT_PrimaryClkDiv2 = 1U, /*!< The intermediate frequency is the bus clock divided by 2. */
  65. kCMT_PrimaryClkDiv3 = 2U, /*!< The intermediate frequency is the bus clock divided by 3. */
  66. kCMT_PrimaryClkDiv4 = 3U, /*!< The intermediate frequency is the bus clock divided by 4. */
  67. kCMT_PrimaryClkDiv5 = 4U, /*!< The intermediate frequency is the bus clock divided by 5. */
  68. kCMT_PrimaryClkDiv6 = 5U, /*!< The intermediate frequency is the bus clock divided by 6. */
  69. kCMT_PrimaryClkDiv7 = 6U, /*!< The intermediate frequency is the bus clock divided by 7. */
  70. kCMT_PrimaryClkDiv8 = 7U, /*!< The intermediate frequency is the bus clock divided by 8. */
  71. kCMT_PrimaryClkDiv9 = 8U, /*!< The intermediate frequency is the bus clock divided by 9. */
  72. kCMT_PrimaryClkDiv10 = 9U, /*!< The intermediate frequency is the bus clock divided by 10. */
  73. kCMT_PrimaryClkDiv11 = 10U, /*!< The intermediate frequency is the bus clock divided by 11. */
  74. kCMT_PrimaryClkDiv12 = 11U, /*!< The intermediate frequency is the bus clock divided by 12. */
  75. kCMT_PrimaryClkDiv13 = 12U, /*!< The intermediate frequency is the bus clock divided by 13. */
  76. kCMT_PrimaryClkDiv14 = 13U, /*!< The intermediate frequency is the bus clock divided by 14. */
  77. kCMT_PrimaryClkDiv15 = 14U, /*!< The intermediate frequency is the bus clock divided by 15. */
  78. kCMT_PrimaryClkDiv16 = 15U /*!< The intermediate frequency is the bus clock divided by 16. */
  79. } cmt_primary_clkdiv_t;
  80. /*!
  81. * @brief The CMT clock divide secondary prescaler.
  82. * The second prescaler can be used to divide the 8 MHZ CMT clock
  83. * by 1, 2, 4, or 8 according to the specification.
  84. */
  85. typedef enum _cmt_second_clkdiv
  86. {
  87. kCMT_SecondClkDiv1 = 0U, /*!< The CMT clock is the intermediate frequency frequency divided by 1. */
  88. kCMT_SecondClkDiv2 = 1U, /*!< The CMT clock is the intermediate frequency frequency divided by 2. */
  89. kCMT_SecondClkDiv4 = 2U, /*!< The CMT clock is the intermediate frequency frequency divided by 4. */
  90. kCMT_SecondClkDiv8 = 3U /*!< The CMT clock is the intermediate frequency frequency divided by 8. */
  91. } cmt_second_clkdiv_t;
  92. /*!
  93. * @brief The CMT infrared output polarity.
  94. */
  95. typedef enum _cmt_infrared_output_polarity
  96. {
  97. kCMT_IROActiveLow = 0U, /*!< The CMT infrared output signal polarity is active-low. */
  98. kCMT_IROActiveHigh = 1U /*!< The CMT infrared output signal polarity is active-high. */
  99. } cmt_infrared_output_polarity_t;
  100. /*!
  101. * @brief The CMT infrared output signal state control.
  102. */
  103. typedef enum _cmt_infrared_output_state
  104. {
  105. kCMT_IROCtlLow = 0U, /*!< The CMT Infrared output signal state is controlled to low. */
  106. kCMT_IROCtlHigh = 1U /*!< The CMT Infrared output signal state is controlled to high. */
  107. } cmt_infrared_output_state_t;
  108. /*!
  109. * @brief CMT interrupt configuration structure, default settings all disabled.
  110. *
  111. * This structure contains the settings for all of the CMT interrupt configurations.
  112. */
  113. enum _cmt_interrupt_enable
  114. {
  115. kCMT_EndOfCycleInterruptEnable = CMT_MSC_EOCIE_MASK, /*!< CMT end of cycle interrupt. */
  116. };
  117. /*!
  118. * @brief CMT carrier generator and modulator configuration structure
  119. *
  120. */
  121. typedef struct _cmt_modulate_config
  122. {
  123. uint8_t highCount1; /*!< The high-time for carrier generator first register. */
  124. uint8_t lowCount1; /*!< The low-time for carrier generator first register. */
  125. uint8_t highCount2; /*!< The high-time for carrier generator second register for FSK mode. */
  126. uint8_t lowCount2; /*!< The low-time for carrier generator second register for FSK mode. */
  127. uint16_t markCount; /*!< The mark time for the modulator gate. */
  128. uint16_t spaceCount; /*!< The space time for the modulator gate. */
  129. } cmt_modulate_config_t;
  130. /*! @brief CMT basic configuration structure. */
  131. typedef struct _cmt_config
  132. {
  133. bool isInterruptEnabled; /*!< Timer interrupt 0-disable, 1-enable. */
  134. bool isIroEnabled; /*!< The IRO output 0-disabled, 1-enabled. */
  135. cmt_infrared_output_polarity_t iroPolarity; /*!< The IRO polarity. */
  136. cmt_second_clkdiv_t divider; /*!< The CMT clock divide prescaler. */
  137. } cmt_config_t;
  138. /*******************************************************************************
  139. * API
  140. ******************************************************************************/
  141. #if defined(__cplusplus)
  142. extern "C" {
  143. #endif
  144. /*!
  145. * @name Initialization and deinitialization
  146. * @{
  147. */
  148. /*!
  149. * @brief Gets the CMT default configuration structure. This API
  150. * gets the default configuration structure for the CMT_Init().
  151. * Use the initialized structure unchanged in CMT_Init() or modify
  152. * fields of the structure before calling the CMT_Init().
  153. *
  154. * @param config The CMT configuration structure pointer.
  155. */
  156. void CMT_GetDefaultConfig(cmt_config_t *config);
  157. /*!
  158. * @brief Initializes the CMT module.
  159. *
  160. * This function ungates the module clock and sets the CMT internal clock,
  161. * interrupt, and infrared output signal for the CMT module.
  162. *
  163. * @param base CMT peripheral base address.
  164. * @param config The CMT basic configuration structure.
  165. * @param busClock_Hz The CMT module input clock - bus clock frequency.
  166. */
  167. void CMT_Init(CMT_Type *base, const cmt_config_t *config, uint32_t busClock_Hz);
  168. /*!
  169. * @brief Disables the CMT module and gate control.
  170. *
  171. * This function disables CMT modulator, interrupts, and gates the
  172. * CMT clock control. CMT_Init must be called to use the CMT again.
  173. *
  174. * @param base CMT peripheral base address.
  175. */
  176. void CMT_Deinit(CMT_Type *base);
  177. /*! @}*/
  178. /*!
  179. * @name Basic Control Operations
  180. * @{
  181. */
  182. /*!
  183. * @brief Selects the mode for CMT.
  184. *
  185. * @param base CMT peripheral base address.
  186. * @param mode The CMT feature mode enumeration. See "cmt_mode_t".
  187. * @param modulateConfig The carrier generation and modulator configuration.
  188. */
  189. void CMT_SetMode(CMT_Type *base, cmt_mode_t mode, cmt_modulate_config_t *modulateConfig);
  190. /*!
  191. * @brief Gets the mode of the CMT module.
  192. *
  193. * @param base CMT peripheral base address.
  194. * @return The CMT mode.
  195. * kCMT_DirectIROCtl Carrier modulator is disabled; the IRO signal is directly in software control.
  196. * kCMT_TimeMode Carrier modulator is enabled in time mode.
  197. * kCMT_FSKMode Carrier modulator is enabled in FSK mode.
  198. * kCMT_BasebandMode Carrier modulator is enabled in baseband mode.
  199. */
  200. cmt_mode_t CMT_GetMode(CMT_Type *base);
  201. /*!
  202. * @brief Gets the actual CMT clock frequency.
  203. *
  204. * @param base CMT peripheral base address.
  205. * @param busClock_Hz CMT module input clock - bus clock frequency.
  206. * @return The CMT clock frequency.
  207. */
  208. uint32_t CMT_GetCMTFrequency(CMT_Type *base, uint32_t busClock_Hz);
  209. /*!
  210. * @brief Sets the primary data set for the CMT carrier generator counter.
  211. *
  212. * This function sets the high-time and low-time of the primary data set for the
  213. * CMT carrier generator counter to control the period and the duty cycle of the
  214. * output carrier signal.
  215. * If the CMT clock period is Tcmt, the period of the carrier generator signal equals
  216. * (highCount + lowCount) * Tcmt. The duty cycle equals to highCount / (highCount + lowCount).
  217. *
  218. * @param base CMT peripheral base address.
  219. * @param highCount The number of CMT clocks for carrier generator signal high time,
  220. * integer in the range of 1 ~ 0xFF.
  221. * @param lowCount The number of CMT clocks for carrier generator signal low time,
  222. * integer in the range of 1 ~ 0xFF.
  223. */
  224. static inline void CMT_SetCarrirGenerateCountOne(CMT_Type *base, uint32_t highCount, uint32_t lowCount)
  225. {
  226. assert(highCount <= CMT_CGH1_PH_MASK);
  227. assert(highCount);
  228. assert(lowCount <= CMT_CGL1_PL_MASK);
  229. assert(lowCount);
  230. base->CGH1 = highCount;
  231. base->CGL1 = lowCount;
  232. }
  233. /*!
  234. * @brief Sets the secondary data set for the CMT carrier generator counter.
  235. *
  236. * This function is used for FSK mode setting the high-time and low-time of the secondary
  237. * data set CMT carrier generator counter to control the period and the duty cycle
  238. * of the output carrier signal.
  239. * If the CMT clock period is Tcmt, the period of the carrier generator signal equals
  240. * (highCount + lowCount) * Tcmt. The duty cycle equals highCount / (highCount + lowCount).
  241. *
  242. * @param base CMT peripheral base address.
  243. * @param highCount The number of CMT clocks for carrier generator signal high time,
  244. * integer in the range of 1 ~ 0xFF.
  245. * @param lowCount The number of CMT clocks for carrier generator signal low time,
  246. * integer in the range of 1 ~ 0xFF.
  247. */
  248. static inline void CMT_SetCarrirGenerateCountTwo(CMT_Type *base, uint32_t highCount, uint32_t lowCount)
  249. {
  250. assert(highCount <= CMT_CGH2_SH_MASK);
  251. assert(highCount);
  252. assert(lowCount <= CMT_CGL2_SL_MASK);
  253. assert(lowCount);
  254. base->CGH2 = highCount;
  255. base->CGL2 = lowCount;
  256. }
  257. /*!
  258. * @brief Sets the modulation mark and space time period for the CMT modulator.
  259. *
  260. * This function sets the mark time period of the CMT modulator counter
  261. * to control the mark time of the output modulated signal from the carrier generator output signal.
  262. * If the CMT clock frequency is Fcmt and the carrier out signal frequency is fcg:
  263. * - In Time and Baseband mode: The mark period of the generated signal equals (markCount + 1) / (Fcmt/8).
  264. * The space period of the generated signal equals spaceCount / (Fcmt/8).
  265. * - In FSK mode: The mark period of the generated signal equals (markCount + 1)/fcg.
  266. * The space period of the generated signal equals spaceCount / fcg.
  267. *
  268. * @param base Base address for current CMT instance.
  269. * @param markCount The number of clock period for CMT modulator signal mark period,
  270. * in the range of 0 ~ 0xFFFF.
  271. * @param spaceCount The number of clock period for CMT modulator signal space period,
  272. * in the range of the 0 ~ 0xFFFF.
  273. */
  274. void CMT_SetModulateMarkSpace(CMT_Type *base, uint32_t markCount, uint32_t spaceCount);
  275. /*!
  276. * @brief Enables or disables the extended space operation.
  277. *
  278. * This function is used to make the space period longer
  279. * for time, baseband, and FSK modes.
  280. *
  281. * @param base CMT peripheral base address.
  282. * @param enable True enable the extended space, false disable the extended space.
  283. */
  284. static inline void CMT_EnableExtendedSpace(CMT_Type *base, bool enable)
  285. {
  286. if (enable)
  287. {
  288. base->MSC |= CMT_MSC_EXSPC_MASK;
  289. }
  290. else
  291. {
  292. base->MSC &= ~CMT_MSC_EXSPC_MASK;
  293. }
  294. }
  295. /*!
  296. * @brief Sets the IRO (infrared output) signal state.
  297. *
  298. * Changes the states of the IRO signal when the kCMT_DirectIROMode mode is set
  299. * and the IRO signal is enabled.
  300. *
  301. * @param base CMT peripheral base address.
  302. * @param state The control of the IRO signal. See "cmt_infrared_output_state_t"
  303. */
  304. void CMT_SetIroState(CMT_Type *base, cmt_infrared_output_state_t state);
  305. /*!
  306. * @brief Enables the CMT interrupt.
  307. *
  308. * This function enables the CMT interrupts according to the provided mask if enabled.
  309. * The CMT only has the end of the cycle interrupt - an interrupt occurs at the end
  310. * of the modulator cycle. This interrupt provides a means for the user
  311. * to reload the new mark/space values into the CMT modulator data registers
  312. * and verify the modulator mark and space.
  313. * For example, to enable the end of cycle, do the following.
  314. * @code
  315. * CMT_EnableInterrupts(CMT, kCMT_EndOfCycleInterruptEnable);
  316. * @endcode
  317. * @param base CMT peripheral base address.
  318. * @param mask The interrupts to enable. Logical OR of @ref _cmt_interrupt_enable.
  319. */
  320. static inline void CMT_EnableInterrupts(CMT_Type *base, uint32_t mask)
  321. {
  322. base->MSC |= mask;
  323. }
  324. /*!
  325. * @brief Disables the CMT interrupt.
  326. *
  327. * This function disables the CMT interrupts according to the provided maskIf enabled.
  328. * The CMT only has the end of the cycle interrupt.
  329. * For example, to disable the end of cycle, do the following.
  330. * @code
  331. * CMT_DisableInterrupts(CMT, kCMT_EndOfCycleInterruptEnable);
  332. * @endcode
  333. *
  334. * @param base CMT peripheral base address.
  335. * @param mask The interrupts to enable. Logical OR of @ref _cmt_interrupt_enable.
  336. */
  337. static inline void CMT_DisableInterrupts(CMT_Type *base, uint32_t mask)
  338. {
  339. base->MSC &= ~mask;
  340. }
  341. /*!
  342. * @brief Gets the end of the cycle status flag.
  343. *
  344. * The flag is set:
  345. * - When the modulator is not currently active and carrier and modulator
  346. * are set to start the initial CMT transmission.
  347. * - At the end of each modulation cycle when the counter is reloaded and
  348. * the carrier and modulator are enabled.
  349. * @param base CMT peripheral base address.
  350. * @return Current status of the end of cycle status flag
  351. * @arg non-zero: End-of-cycle has occurred.
  352. * @arg zero: End-of-cycle has not yet occurred since the flag last cleared.
  353. */
  354. static inline uint32_t CMT_GetStatusFlags(CMT_Type *base)
  355. {
  356. return base->MSC & CMT_MSC_EOCF_MASK;
  357. }
  358. /*! @}*/
  359. #if defined(__cplusplus)
  360. }
  361. #endif
  362. /*! @}*/
  363. #endif /* _FSL_CMT_H_*/