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