fsl_cmt.c 8.6 KB

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