Kinetis SDK v.1.3 API Reference Manual  Rev. 0
Freescale Semiconductor, Inc.
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages

Overview

This section describes the programming interface of the CMT Peripheral driver. The CMT driver configures and uses CMT hardware. It supports module initializations and CMT module configurations.

CMT Clock formulas

CMT clock frequency = bus_clock_Hz / (bus_clock_Hz / 8 MHz) / cmtDivider

CMT carrier and generator frequency = CMT Clock frequency / (highCount1 + lowCount1) (In FSK mode, the second frequency = CMT Clock frequency / (highCount2 + lowCount2))

In Time and Baseband mode CMT IRO signal mark time = (markCount + 1) / (CMT Clock frequency / 8)

CMT IRO signal space time = spaceCount / (CMT Clock frequency / 8)

In FSK mode CMT IRO signal mark time = (markCount + 1) / CMT carrier and generator frequency

CMT IRO signal space time = spaceCount / CMT carrier and generator frequency

CMT Initialization

To initialize the CMT driver, define an cmt_base_config_t type variable and pass it in the CMT_DRV_Init() function. The initialize setting the CMT clock, interrupt and the IRO signal for the CMT module.
This is an example code to initialize and configure the driver:

// Define device configuration.
const cmt_base_config_t cmtInit = {
isInterruptEnabled = enable, // Enable end of modulation cycle interrupt. //
isIroEnabled = true, // Enable the IRO signal. //
iroPolarity = kCMTIROActiveHigh, // Set the IRO polarity active high. //
cmtDivider = kCMTSecClkDiv1 // Set the secondary CMT clock divider to 1. //
};
// Initialize CMT instance 0.
CMT_DRV_Init(0, &cmtInit);

CMT Carrier generator and Modulator Start

To Start the CMT carrier and modulator, define an cmt_modulate_config_t type variable and pass it in the CMT_DRV_StartCarrierModulate() function. The carrier and modulation start configuring the CMT mode, generating protocol timing, or carrier signal data.

typedef struct CmtModulateConfig
{
cmt_mode_t mode; // The modes for carrier and modulator enabled. kCMTDirectIROCtl should not be used here. //
uint8_t highCount1; // The high time period for the first counter of the CMT carrier generator. //
uint8_t lowCount1; // The low time period for the first counter of the CMT carrier generator. //
uint8_t highCount2; // The high time period for the second counter of the CMT carrier generator. //
uint8_t lowCount2; // The low time period for the second counter of the CMT carrier generator. //
uint16_t markCount; // The mark time period for the CMT modulator. //
uint16_t spaceCount; // The space time period for the CMT modulator. //
typedef struct CmtModulateData {
uint8_t *data; // The data for modulation, the LSB was transmitted first. //
uint32_t len; // The length of data bit for modulation. //
uint32_t lenDone; // The length of data bit has been modulated. //
uint16_t markOne; // The mark time for the data bit "1". //
uint16_t spaceOne; // The space time for the data bit "1". //
uint16_t markZero; // The mark time for the data bit "0". //
uint16_t spaceZero; // The space time for the data bit "0". //

This is an example code to start the driver:

// Defines the CMT carrier and modulator configuration.
cmt_modulate_config_t modulateConfig = {
mode = kCMTTimeMode, // Set the CMT Time mode. //
highCount1 = 1, // Set the high time for the first count of CMT carrier generator. //
lowCount1 = 1, // Set the low time for the first count of CMT carrier generator. //
markCount = 9, // Set the count of modulator mark period. //
spaceCount = 0, // Set the count of modulator space period. //
};
// Defines data to be transmitted
uint8_t newData[2] = {0x85, 0x14};
cmt_modulate_data_t modData = {
.data = newData,
.len = 15,
.markOne = 8,
.spaceOne = 0,
.markZero = 0,
.spaceZero = 8
};
// Start CMT instance 0 with LSB mode byte transmit.
CMT_DRV_StartCarrierModulate(0, &modulateConfig, &modData, false);

For the settings above, the IRO output signal is a 4 MHz signal with 50% duty cycle when the Bus clock is 48 MHz.

CMT DATA Modulate

The CMT_DRV_ModulateDataByBit() is provided to modulate data and should be called by the ISR handler.

When handling CMT initialization, define a global cmt_modulate_data_t type variable modData, define a cmt_modulate_config_t type variable, and define the MSB/LSB mode then pass it in the CMT_DRV_StartCarrierModulate() function.

After the data modulation is complete, call the void CMT_DRV_StopCarrierModulate(uint32_t instance) function to stop it at any time.
To close the CMT module entirely, call the cmt_status_t CMT_DRV_Deinit(uint32_t instance) function. This disables both the CMT carrier modulator and the clock gate.

Data Structures

struct  cmt_base_config_t
 CMT configuration structure. More...
 
struct  cmt_modulate_data_t
 CMT modulate data structure. More...
 
struct  cmt_modulate_config_t
 CMT modulate configure structure. More...
 

Macros

#define CMT_INTERMEDIATEFREQUENCY_8MHZ   8000000U
 Define the standard intermediate frequency (IF)
 
#define CMT_MODULATEUNIT8BITS   8U
 Define the standard intermediate frequency (IF)
 

Variables

CMT_Type *const g_cmtBase []
 Table of base addresses for CMT instances. More...
 
const IRQn_Type g_cmtIrqId []
 Table to save CMT IRQ enumeration numbers defined in the CMSIS header file.
 

Initialization and Shutdown

cmt_status_t CMT_DRV_Init (uint32_t instance, const cmt_base_config_t *config)
 Initializes the CMT module. More...
 
cmt_status_t CMT_DRV_Deinit (uint32_t instance)
 Disables the CMT module and gate control. More...
 
void CMT_DRV_StartCarrierModulate (uint32_t instance, cmt_modulate_config_t *modulateConfig, cmt_modulate_data_t *cmtData, bool isMsbMode)
 Starts the CMT carrier generator and modulator. More...
 
void CMT_DRV_StopCarrierModulate (uint32_t instance)
 Stops the CMT carrier generator and modulator. More...
 
cmt_status_t CMT_DRV_ModulateDataByBit (uint32_t instance, cmt_modulate_data_t *cmtData, bool isMsbMode)
 Modulates the date by one bit. More...
 
void CMT_DRV_SetMode (uint32_t instance, cmt_mode_t mode)
 Selects the mode for CMT. More...
 
cmt_mode_t CMT_DRV_GetMode (uint32_t instance)
 Gets the mode of CMT module. More...
 

Clock divider

void CMT_DRV_SetClockDiv (uint32_t instance, cmt_second_clkdiv_t spsDiv)
 Sets CMT clock divider. More...
 

Interrupt

bool CMT_DRV_IsEndofCycle (uint32_t instance)
 Reads Reads the current end of cycle flag. More...
 
void CMT_DRV_ClearEndofCycleFlag (uint32_t instance, uint32_t markCount)
 Clears the end of cycle status flag. More...
 

Counters

void CMT_DRV_SetModulateMarkCount (uint32_t instance, uint32_t markCount)
 Sets the modulation mark time period for the CMT modulator counter. More...
 
void CMT_DRV_SetModulateSpaceCount (uint32_t instance, uint32_t spaceCount)
 Sets the modulation space time period for the CMT modulator counter. More...
 

Output signal Control

void CMT_DRV_SetIroCtl (uint32_t instance, cmt_iro_ctl_t ctlVal)
 IRO Latch Control. More...
 
bool CMT_DRV_GetIrolatchState (uint32_t instance)
 Read the states of IRO latch. More...
 

Data Structure Documentation

struct cmt_base_config_t

Defines a basic configure structure CmtBaseConfig and uses the CMT_DRV_Init() function to make necessary initializations.

Data Fields

bool isInterruptEnabled
 Timer interrupt 0-disable/1-enable.
 
bool isIroEnabled
 The IRO output 0-disabled/1-enabled.
 
cmt_iro_polarity_t iroPolarity
 The IRO polarity.
 
cmt_second_clkdiv_t cmtDivider
 The CMT clock divide prescaler.
 
struct cmt_modulate_data_t

Defines a data structure for CMT data modulation.

struct cmt_modulate_config_t

Defines a specific configuration structure CmtModulateConfig and uses the CMT_DRV_StartCarrierModulate() function to initialize and start carrier generator and modulator. Stores the transmitted data information to the global variable for data modulation.

Data Fields

cmt_mode_t mode
 < Choose these mode for carrier and modulator enabled, kCMTDirectIROCtl should not used here More...
 

Field Documentation

cmt_mode_t cmt_modulate_config_t::mode

The high and low time for the carrier signal

Function Documentation

cmt_status_t CMT_DRV_Init ( uint32_t  instance,
const cmt_base_config_t config 
)

This function must be called before calling all the other CMT driver functions. This function ungates the CMT clock and sets the CMT clock, interrupt, and output signal for CMT module. Pass in its basic configuration structure.

Parameters
instanceCMT module instance number.
configThe CMT basic configuration structure.
Returns
Error or success status returned by API.
cmt_status_t CMT_DRV_Deinit ( uint32_t  instance)

This function disables all CMT interrupts and CMT clock. It, then, gates the CMT clock control. CMT_DRV_Init must be called if you want to use CMT again.

Parameters
instanceCMT module instance number.
Returns
Error or success status returned by API.
void CMT_DRV_StartCarrierModulate ( uint32_t  instance,
cmt_modulate_config_t modulateConfig,
cmt_modulate_data_t cmtData,
bool  isMsbMode 
)

This function un-gates the CMT carrier generator and modulator clock, initializes, and starts the modulator and carrier generator. Pass in the modulation specific configuration structure. CMT_DRV_Init should be called before using this function.

Parameters
instanceCMT module instance number.
modulateConfigThe CMT modulator specific configuration structure.
cmtDataThe data to be modulated.
isMsbModeThe MSB or LSB mode for the byte transmit
  • true, The MSB mode.
  • false, The LSB mode.
void CMT_DRV_StopCarrierModulate ( uint32_t  instance)

This function stops and disables the CMT carrier generator and modulator. When the CMT carrier generator and modulator is disabled the IRO signal can be controlled directly by calling the CMT_DRV_SetIroCtl.

Parameters
instanceCMT module instance number.
cmt_status_t CMT_DRV_ModulateDataByBit ( uint32_t  instance,
cmt_modulate_data_t cmtData,
bool  isMsbMode 
)

This function can be called by the CMT ISR handler to handle data modulation by using ISR handler to change the modulation mark and space time period according to the data bit "1" or bit "0" each time. Note: the modulation updates the mark and space time period at the end of every modulation cycle, irrespective of interrupt handling. To update the new mark and space time period in time, this function should be processed in one modulation cycle. When tested for Bare Metal, the function called by the ISR handler takes about 4 µs and the maximum bit rate is about 250000.

Parameters
instanceCMT module instance number.
cmtDataThe data to be modulated.
isMsbModeThe MSB or LSB mode for the byte transmit.
  • true, The MSB mode.
  • false, The LSB mode.
void CMT_DRV_SetMode ( uint32_t  instance,
cmt_mode_t  mode 
)

This function selects the mode for CMT.

Parameters
baseBase address for current CMT instance.
modeSelection of CMT feature mode enumeration. See to "cmt_mode_t"
cmt_mode_t CMT_DRV_GetMode ( uint32_t  instance)

This function gets the mode of CMT module.

Parameters
baseBase address for current CMT instance.
Returns
The mode of CMT module. See to "cmt_mode_t".
void CMT_DRV_SetClockDiv ( uint32_t  instance,
cmt_second_clkdiv_t  spsDiv 
)

This function sets the CMT clock divider.

Parameters
instanceCMT module instance number.
spsDivThe CMT clock divider prescaler. The CMT carrier generator resolution is 8 Mhz/(2 ^ spsDiv) . spsDiv should be a integer range between 0 ~ 3 representing the divisor 1,2,4 and 8 (See "cmt_second_clkdiv_t").
bool CMT_DRV_IsEndofCycle ( uint32_t  instance)

This flag is set at the end of each cycle.

Parameters
instanceCMT module instance number.
channelTimer channel number
Returns
The status of the end of cycle flag
  • true: The end of cycle has occurred.
  • false: The end of cycle has not yet occurred.
void CMT_DRV_ClearEndofCycleFlag ( uint32_t  instance,
uint32_t  markCount 
)

Clears the end of cycle status flag by reading the MSC followed by and access of the lower eight bit of the modulator mark or space counter. Note that this is not for clearing the end of cycle status flag when the CMT DMA is enabled.

Parameters
instanceCMT module instance number.
markCountThe number of clock period for CMT modulator signal mark period, in the range of 0 ~ 0xFFFF.
void CMT_DRV_SetModulateMarkCount ( uint32_t  instance,
uint32_t  markCount 
)

This function sets the mark time period of the CMT modulator counter to control the mark time of the output modulated signal from the carrier generator output signal. If the CMT clock frequency is Fcmt and the carrier out signal frequency is fcg:

  • In Time and Baseband mode: The mark period of the generated signal equals (markCount + 1) / (Fcmt/8).
  • In FSK mode:The mark period of the generated signal equals (markCount + 1) / fcg.
Parameters
instanceCMT module instance number.
markCountThe number of clock period for CMT modulator signal mark period, in the range of 0 ~ 0xFFFF.
void CMT_DRV_SetModulateSpaceCount ( uint32_t  instance,
uint32_t  spaceCount 
)

This function sets the mark time period of the CMT modulator counter to control the space time of the output modulated signal from the carrier generator output signal. If the CMT clock frequency is Fcmt and the carrier out signal frequency is fcg:

  • In Time and Baseband mode: The space period of the generated signal equals spaceCount / (Fcmt/8).
  • In FSK mode:The space period of the generated signal equals spaceCount / fcg.
Parameters
instanceCMT module instance number.
spaceCountThe number of clock period for CMT modulator signal space period, in the range of 0 ~ 0xFFFF.
void CMT_DRV_SetIroCtl ( uint32_t  instance,
cmt_iro_ctl_t  ctlVal 
)

Changes the states of the IRO signal when the kCMTDirectIROMode mode is set and the IRO signal is enabled.

Parameters
baseBase address for current CMT instance.
ctlValThe control of the IRO signal. See "_cmt_iro_ctl_t"
bool CMT_DRV_GetIrolatchState ( uint32_t  instance)
Parameters
baseBase address for current CMT instance.
Returns
The states of the IRO latch

Variable Documentation

CMT_Type* const g_cmtBase[]