Hi,
I am trying to generate a simple clock of 16khz using a FTM2 CH0 and below is my initialization code
void startClock() //FTM2 CH0 16khz
{
/**********************************************************************************
* PORT SETUP
* ********************************************************************************
*/
// System Clock Gating Control Register 5 (SIM_SCGC5), switch on clock for port B
*(volatile unsigned long *)(SIM_BLOCK + SIM_PORT_CLK_GATE_OFFSET) |= 0x400;
// FTM2_CH0 on PTB18 (alt. function 3) and high drive strength
*(volatile unsigned long *)(PORTB_BLOCK + 0x48) |= (0x00000300 | 0x00000040);
/**********************************************************************************
* Flex Timer 2 Channel 0 Setup
* ********************************************************************************
*/
// FTM2. Ensure that the FlexTimer/TPM module is powered up
*(volatile unsigned long *)(SIM_BLOCK + 0x103c) |= 0x4000000;
// FTM Status And Control (FTM0_SC). Disable FTM2 operation (system clock)
*(volatile unsigned long *)(FTM2_BASE_ADDR + 0x000) = 0x00000000;
// Configuration (FTMx_CONF) pp 1020
// pin
// 6 - 7 - BDM mode 3 - 0xc0 (allow timer to continue operating when debugging)
// 9 - Enable use of global timebase from another FTM module 0x200 (Unused here)
// 10 - Enable output of global timebase to other FTMs 0x400 (unused here)
*(unsigned long *)(FTM2_BASE_ADDR + 0x084) = (0x000000c0);
// Disable Write protection on the FTMx_MODE.
*(unsigned long *)(FTM2_BASE_ADDR + 0x054) = 0x00000004;
// Features Mode Selection (FTMx_MODE)
// Switch off the enhanced FTM features to avoid needing to do an explicit software load of the CnV regs.
// Keep write protection disabled - 0x04
*(unsigned long *)(FTM2_BASE_ADDR + 0x054) = (0x00000004 | 0x0000000);
// Synchronisation (FTMx_SYNC).
// Enable loading at maximum (i.e. CV == MOD) - 0x00000002
// Software triggering is enabled - 0x80
*(unsigned long *)(FTM2_BASE_ADDR + 0x58) = (0x00000002 | 0x80);
// Set counter MOD register to the value that provides the frequency of interest.
// 0x3C6 - 1 kHz with a pre-scaler of 128
// 0x0BB; - 320 kHz
*(unsigned long *)(FTM2_BASE_ADDR + FTM2_MOD) = 3750-1;//0x0BB;// - 320 kHz;
// Synchronisation Configuration (FTMx_SYNCONF)
// set SYNCMODE (Enhanced PWM synchronisation is selected.) - 0x80
// set SWRSTCNT = 1 (FTM counter synchronisation is activated by the software trigger) - 0x100
// set SWWRBUF = 1 (MOD, CNTIN, and CV registers synchronisation is activated by the software trigger.) - 0x200
*(unsigned long *)(FTM2_BASE_ADDR + 0x8C) = (0x200);
// Channel (n) Status And Control (FTMx_CnSC)
// ELSnB:ELSnA = 1:0 Edge-Aligned PWM High-true pulses (clear Output on match) - 0x08
// MSnB:MSnA = 1:0 (See ELSnB:ELSnA) - 0x20
*(volatile unsigned long *)(FTM2_BASE_ADDR + FTM2_C0SC_OFFSET) = (0x00000020 | 0x00000008);
// Set counter initial value to zero
*(volatile unsigned long *)(FTM2_BASE_ADDR + 0x04c) = 0;
*(volatile unsigned long *)(FTM2_BASE_ADDR + 0x060) = 0x01;
// prepare first PWM value (place it in the CnV register)
*(volatile unsigned long *)(FTM2_BASE_ADDR + FTM2_C0V_OFFSET) = 3750/2;//0x5D;//0x1E3;
}
I am generating an SPWM on FTM0 CH6 and 7 too, so I am enabling both the FTM modules together after initializing the FTM0 module as below.
/**********************************************************************************
* Enable Clock to FTM module.
* ********************************************************************************
*/
// FTM Status And Control (FTM0_SC). Enable FTM0 operation (system clock)
*(volatile unsigned long *)(FTM0_BASE_ADDR + 0x000) = 0x00000008;
// FTM Status And Control (FTM2_SC). Enable FTM2 operation (system clock)
*(volatile unsigned long *)(FTM2_BASE_ADDR + 0x000) = 0x00000008;
I am getting the SPWM output on PTA 1/2 but the FTM2 clock output on PTB18 isn't getting generated. The output is turned ON and OFF using GPIOB Pin 19.
Is there some settings that are missing in the above code? I have enabled the clock to PORTB & PORTA in board init.
Regards,
Rashmitha