fsl_cmt.c 8.5 KB

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