fsl_cmt.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. #include "fsl_cmt.h"
  35. /*******************************************************************************
  36. * Definitions
  37. ******************************************************************************/
  38. /* Component ID definition, used by tools. */
  39. #ifndef FSL_COMPONENT_ID
  40. #define FSL_COMPONENT_ID "platform.drivers.cmt"
  41. #endif
  42. /* The standard intermediate frequency (IF). */
  43. #define CMT_INTERMEDIATEFREQUENCY_8MHZ (8000000U)
  44. /* CMT data modulate mask. */
  45. #define CMT_MODULATE_COUNT_WIDTH (8U)
  46. /* CMT diver 1. */
  47. #define CMT_CMTDIV_ONE (1)
  48. /* CMT diver 2. */
  49. #define CMT_CMTDIV_TWO (2)
  50. /* CMT diver 4. */
  51. #define CMT_CMTDIV_FOUR (4)
  52. /* CMT diver 8. */
  53. #define CMT_CMTDIV_EIGHT (8)
  54. /*******************************************************************************
  55. * Prototypes
  56. ******************************************************************************/
  57. /*!
  58. * @brief Get instance number for CMT module.
  59. *
  60. * @param base CMT peripheral base address.
  61. */
  62. static uint32_t CMT_GetInstance(CMT_Type *base);
  63. /*******************************************************************************
  64. * Variables
  65. ******************************************************************************/
  66. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  67. /*! @brief Pointers to cmt clocks for each instance. */
  68. static const clock_ip_name_t s_cmtClock[FSL_FEATURE_SOC_CMT_COUNT] = CMT_CLOCKS;
  69. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  70. /*! @brief Pointers to cmt bases for each instance. */
  71. static CMT_Type *const s_cmtBases[] = CMT_BASE_PTRS;
  72. /*! @brief Pointers to cmt IRQ number for each instance. */
  73. static const IRQn_Type s_cmtIrqs[] = CMT_IRQS;
  74. /*******************************************************************************
  75. * Codes
  76. ******************************************************************************/
  77. static uint32_t CMT_GetInstance(CMT_Type *base)
  78. {
  79. uint32_t instance;
  80. /* Find the instance index from base address mappings. */
  81. for (instance = 0; instance < ARRAY_SIZE(s_cmtBases); instance++)
  82. {
  83. if (s_cmtBases[instance] == base)
  84. {
  85. break;
  86. }
  87. }
  88. assert(instance < ARRAY_SIZE(s_cmtBases));
  89. return instance;
  90. }
  91. void CMT_GetDefaultConfig(cmt_config_t *config)
  92. {
  93. assert(config);
  94. /* Default infrared output is enabled and set with high active, the divider is set to 1. */
  95. config->isInterruptEnabled = false;
  96. config->isIroEnabled = true;
  97. config->iroPolarity = kCMT_IROActiveHigh;
  98. config->divider = kCMT_SecondClkDiv1;
  99. }
  100. void CMT_Init(CMT_Type *base, const cmt_config_t *config, uint32_t busClock_Hz)
  101. {
  102. assert(config);
  103. assert(busClock_Hz >= CMT_INTERMEDIATEFREQUENCY_8MHZ);
  104. uint8_t divider;
  105. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  106. /* Ungate clock. */
  107. CLOCK_EnableClock(s_cmtClock[CMT_GetInstance(base)]);
  108. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  109. /* Sets clock divider. The divider set in pps should be set
  110. to make sycClock_Hz/divder = 8MHz */
  111. base->PPS = CMT_PPS_PPSDIV(busClock_Hz / CMT_INTERMEDIATEFREQUENCY_8MHZ - 1);
  112. divider = base->MSC;
  113. divider &= ~CMT_MSC_CMTDIV_MASK;
  114. divider |= CMT_MSC_CMTDIV(config->divider);
  115. base->MSC = divider;
  116. /* Set the IRO signal. */
  117. base->OC = CMT_OC_CMTPOL(config->iroPolarity) | CMT_OC_IROPEN(config->isIroEnabled);
  118. /* Set interrupt. */
  119. if (config->isInterruptEnabled)
  120. {
  121. CMT_EnableInterrupts(base, kCMT_EndOfCycleInterruptEnable);
  122. EnableIRQ(s_cmtIrqs[CMT_GetInstance(base)]);
  123. }
  124. }
  125. void CMT_Deinit(CMT_Type *base)
  126. {
  127. /*Disable the CMT modulator. */
  128. base->MSC = 0;
  129. /* Disable the interrupt. */
  130. CMT_DisableInterrupts(base, kCMT_EndOfCycleInterruptEnable);
  131. DisableIRQ(s_cmtIrqs[CMT_GetInstance(base)]);
  132. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  133. /* Gate the clock. */
  134. CLOCK_DisableClock(s_cmtClock[CMT_GetInstance(base)]);
  135. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  136. }
  137. void CMT_SetMode(CMT_Type *base, cmt_mode_t mode, cmt_modulate_config_t *modulateConfig)
  138. {
  139. uint8_t mscReg = base->MSC;
  140. /* Judge the mode. */
  141. if (mode != kCMT_DirectIROCtl)
  142. {
  143. assert(modulateConfig);
  144. /* Set carrier generator. */
  145. CMT_SetCarrirGenerateCountOne(base, modulateConfig->highCount1, modulateConfig->lowCount1);
  146. if (mode == kCMT_FSKMode)
  147. {
  148. CMT_SetCarrirGenerateCountTwo(base, modulateConfig->highCount2, modulateConfig->lowCount2);
  149. }
  150. /* Set carrier modulator. */
  151. CMT_SetModulateMarkSpace(base, modulateConfig->markCount, modulateConfig->spaceCount);
  152. mscReg &= ~ (CMT_MSC_FSK_MASK | CMT_MSC_BASE_MASK);
  153. mscReg |= mode;
  154. }
  155. else
  156. {
  157. mscReg &= ~CMT_MSC_MCGEN_MASK;
  158. }
  159. /* Set the CMT mode. */
  160. base->MSC = mscReg;
  161. }
  162. cmt_mode_t CMT_GetMode(CMT_Type *base)
  163. {
  164. uint8_t mode = base->MSC;
  165. if (!(mode & CMT_MSC_MCGEN_MASK))
  166. { /* Carrier modulator disabled and the IRO signal is in direct software control. */
  167. return kCMT_DirectIROCtl;
  168. }
  169. else
  170. {
  171. /* Carrier modulator is enabled. */
  172. if (mode & CMT_MSC_BASE_MASK)
  173. {
  174. /* Base band mode. */
  175. return kCMT_BasebandMode;
  176. }
  177. else if (mode & CMT_MSC_FSK_MASK)
  178. {
  179. /* FSK mode. */
  180. return kCMT_FSKMode;
  181. }
  182. else
  183. {
  184. /* Time mode. */
  185. return kCMT_TimeMode;
  186. }
  187. }
  188. }
  189. uint32_t CMT_GetCMTFrequency(CMT_Type *base, uint32_t busClock_Hz)
  190. {
  191. uint32_t frequency;
  192. uint32_t divider;
  193. /* Get intermediate frequency. */
  194. frequency = busClock_Hz / ((base->PPS & CMT_PPS_PPSDIV_MASK) + 1);
  195. /* Get the second divider. */
  196. divider = ((base->MSC & CMT_MSC_CMTDIV_MASK) >> CMT_MSC_CMTDIV_SHIFT);
  197. /* Get CMT frequency. */
  198. switch ((cmt_second_clkdiv_t)divider)
  199. {
  200. case kCMT_SecondClkDiv1:
  201. frequency = frequency / CMT_CMTDIV_ONE;
  202. break;
  203. case kCMT_SecondClkDiv2:
  204. frequency = frequency / CMT_CMTDIV_TWO;
  205. break;
  206. case kCMT_SecondClkDiv4:
  207. frequency = frequency / CMT_CMTDIV_FOUR;
  208. break;
  209. case kCMT_SecondClkDiv8:
  210. frequency = frequency / CMT_CMTDIV_EIGHT;
  211. break;
  212. default:
  213. frequency = frequency / CMT_CMTDIV_ONE;
  214. break;
  215. }
  216. return frequency;
  217. }
  218. void CMT_SetModulateMarkSpace(CMT_Type *base, uint32_t markCount, uint32_t spaceCount)
  219. {
  220. /* Set modulate mark. */
  221. base->CMD1 = (markCount >> CMT_MODULATE_COUNT_WIDTH) & CMT_CMD1_MB_MASK;
  222. base->CMD2 = (markCount & CMT_CMD2_MB_MASK);
  223. /* Set modulate space. */
  224. base->CMD3 = (spaceCount >> CMT_MODULATE_COUNT_WIDTH) & CMT_CMD3_SB_MASK;
  225. base->CMD4 = spaceCount & CMT_CMD4_SB_MASK;
  226. }
  227. void CMT_SetIroState(CMT_Type *base, cmt_infrared_output_state_t state)
  228. {
  229. uint8_t ocReg = base->OC;
  230. ocReg &= ~CMT_OC_IROL_MASK;
  231. ocReg |= CMT_OC_IROL(state);
  232. /* Set the infrared output signal control. */
  233. base->OC = ocReg;
  234. }