Hi everyone, I was hoping you could tell me where I'm going wrong. I'm trying to set up a simple counter using the FTM in a MK64FX512VLQ12. I'm using the KSDK 2.3.0. Here's the code of interest:
void vConfigureTimer( void )
{
CLOCK_EnableClock(kCLOCK_Ftm0);
ftm_config_t ftmInfo = {
.prescale = kFTM_Prescale_Divide_16,
.bdmMode = kFTM_BdmMode_3,
.pwmSyncMode = kFTM_SoftwareTrigger,
.reloadPoints = 0,
.faultMode = kFTM_Fault_Disable,
.faultFilterValue = 0,
.deadTimePrescale = kFTM_Deadtime_Prescale_1,
.deadTimeValue = 0,
.extTriggers = 0,
.chnlInitState = 0,
.chnlPolarity = 0,
.useGlobalTimeBase = false
};
FTM_Init(FTM0, &ftmInfo);
FTM_StartTimer(FTM0, kFTM_FixedClock);
}
My understanding was that if the BDM mode was set to 3, the timer would run even during debugging. However, if I set up a simple routine to retrieve the counter value:
void PrintTimer( void )
{
uint32_t timer = FTM0->CNT;
PRINTF("%i\n", timer);
}
It just prints 0. Does anyone see where I'm going wrong? Thanks in advance for your help!
Hi Noah
BDM mode 3 does allow the timer to continue running even when the debugger pauses the CPU.
If the counter doesn't count after enabling its clock source it will (with high probability) mean that clock source that has been selected is not running. Try using the bus clock rather than fixed clock (MCGFFCLK in K64) for your first trials since that will always be running.
For more performance, increased development efficiency and K64 simulation check out the uTasker project below.
Regards
Mark
Kinetis: http://www.utasker.com/kinetis.html
Kinetis K64:
- http://www.utasker.com/kinetis/FRDM-K64F.html
- http://www.utasker.com/kinetis/TWR-K64F120M.html
- http://www.utasker.com/kinetis/TEENSY_3.5.html
- http://www.utasker.com/kinetis/Hexiwear-K64F.html
HW Timers: http://www.utasker.com/docs/uTasker/uTaskerHWTimers.PDF
Free Open Source solution: https://github.com/uTasker/uTasker-Kinetis
Working project in 15 minutes video: https://youtu.be/K8ScSgpgQ6M
For better, faster, cheaper product developments consider the uTasker developer's version, professional Kinetis support, one-on-one training and complete fast-track project solutions to set you apart from the herd : http://www.utasker.com/support.html
Hi Mark, thanks for your reply.
I made the change you suggested:
FTM_StartTimer(FTM0, kFTM_SystemClock);
Unfortunately this did not make a difference, the counter remained at 0.
Something else I did not mention in the OP - I'm using freeRTOS. Would it somehow affect the FTM somehow?
Thanks
Noah
Noah
Unless you have a FreeRTOS task that is disabling the FlexTimer it will have no effect (FreeRTOS only uses SYSTICK for its Tick interrupt).
I have generated a binary for your board (it runs from the HIRC48 so is K64 HW independent) with FreeRTOS as OS and USB-CDC (assuming you have USB you can connect to it and use the command line interface) and UART (the same UART connections as on the FRDM-K64F board at 115'200 Baud) for the same menu.
FlexTimer 0 is operating as 150ms periodic interrupt (from bus clock at 24MHz). The interrupt toggles PTE26 at this rate.
The command line has a memory debugger (I/O memory see example below) so you can check register setup and operation.
The FlexTimer is setup with (FreeRTOS using uTasker peripheral library):
static TIMER_INTERRUPT_SETUP timer_setup = {0}; // interrupt configuration parameters
timer_setup.int_type = TIMER_INTERRUPT;
timer_setup.int_priority = PRIORITY_TIMERS;
timer_setup.int_handler = timer_int;
timer_setup.timer_reference = 0; // timer 0 (flex timer on Kinetis parts)
timer_setup.timer_mode = (TIMER_PERIODIC); // period timer interrupt
timer_setup.timer_value = TIMER_MS_DELAY(150); // 150ms periodic interrupt
fnConfigureInterrupt((void *)&timer_setup); // enter interrupt for timer test
static void timer_int(void) // FlexTimer 0 interrupt handler
{
TOGGLE_TEST_OUTPUT();
}
CLI on USB-CDC showing how the FlexTimer 0 registers are set and showing CNT counting. (up arrow returns the previous command fir easy repeating to check the values changing):
Serial number: 00
Software version V1.4.012
Device identification: KINETIS
Main menu
===================
1 Configure LAN interface
2 Configure serial interface
3 Go to I/O menu
4 Go to administration menu
5 Go to overview/statistics menu
6 Go to USB menu
7 Go to I2C menu
8 Go to utFAT disk interface
9 FTP/TELNET commands
a CAN commands
help Display menu specific help
quit Leave command mode
3
Input/Output menu
===================
up go to main menu
md Memory Display [address] [<l>|<w>|<b>] [num]
mm Memory Modify [address] [<l>|<w>|<b>] [val]
mf Memory Fill [address] [<l>|<w>|<b>] [val] [num]
sd Storage Display {as md}
sm Storage Modify {as mm}
sf Storage Fill {as mf}
se Storage Erase [address] [len-hex]
set_ddr Set port type [1..4] [<i>|<o>
get_ddr Get data direction [1..4]
read_port Read port input [1..4]
write_port Set port output [1..4] [0/1]
lp_cnt Read LPTMR CNT
save Save port setting as default
help Display menu specific help
quit Leave command mode
#md 40038000 l 10
Memory Display
0x40038000 0000004f 00000535 0000dbba 00000000 ...O...5........
0x40038010 00000000 00000000 00000000 00000000 ................
0x40038020 00000000 00000000 ........
md 40038000 l 10
Memory Display
0x40038000 0000004f 000066bb 0000dbba 00000000 ...O..f.........
0x40038010 00000000 00000000 00000000 00000000 ................
0x40038020 00000000 00000000 ........
md 40038000 l 10
Memory Display
0x40038000 0000004f 000047e6 0000dbba 00000000 ...O..G.........
0x40038010 00000000 00000000 00000000 00000000 ................
0x40038020 00000000 00000000 ........
In fact the only thing needed is to write the match value to FTM0_MOD, install an interrupt handler and set 0x4f to FTM0_SC (If you just want to see the count incrementing no interrupt is needed).
I needed 7 minutes to create and verify this application for your board with the FlexTimer reference. Basic FlexTimer operation is very simple (as seen from this) so I would expect you to be able to do it with the libraries you are using, even if it may take rather longer to get a complete project together.
Regards
Mark