fsl_cmt.h 16 KB

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