AnsweredAssumed Answered

Why do I need a delay after PIT clock enable?

Question asked by Charles Zimnicki on Feb 21, 2015
Latest reply on Feb 21, 2015 by Mark Butcher

I tried to write an application that uses PIT interrupt and I ran into trouble. I noticed that the timers do not advance. So I started to investigate what is wrong, printing register values. It turns out that if there is no delay after the line "SIM_SCGC6 |= SIM_SCGC6_PIT_MASK;" the PIT_MCR somehow ends up with the value 2 and the timer does not advance. This happens even when commenting a couple of the "__asm("nop"); " lines. However, with this 8-line delay everything works OK, with the PIT_MCR having value 0 and timer advancing normally.

My setup is the same as with "Writing my first KSDK Application in KDS - Hello World and Toggle LED with GPIO Interrupt", with exception that I work with FRDM-K22F, KDS 2.0.0, KSDK 1.1.0. I flash the firmware using PEMicro/OpenSDA. My plan is to investigate assembly code next but I thought that there is a good chance someone will provide an answer here.


#include "fsl_device_registers.h"

#include "board.h"

#include <stdio.h>


int main(void)




  SIM_SCGC6 |= SIM_SCGC6_PIT_MASK;     // Enable the clock to the PIT module


  __asm("nop");     // I have no idea why I need this delay :-(

  __asm("nop");     // but without these lines timer does not advance.

  __asm("nop");     // I had a printf here before and everything started to work

  __asm("nop");     // so I put these lines to investigate how long of a delay is needed.






  PIT_MCR = 0x00; //Enable PIT module

  PIT_LDVAL1 = 8000000; //PIT Load value

  PIT_TCTRL1 |= PIT_TCTRL_TEN_MASK; // start Timer 1


  printf("Register   address  value\n\r");

  printf("SIM_SCGC6  %08x %08X\n\r",&SIM_SCGC6, SIM_SCGC6);

  printf("PIT_MCR    %08x %08X\n\r",&PIT_MCR, PIT_MCR);

  printf("PIT_LDVAL1 %08x %08X\n\r",&PIT_LDVAL1, PIT_LDVAL1);

  printf("PIT_TCTRL1 %08x %08X\n\r",&PIT_TCTRL1, PIT_TCTRL1);

  printf("PIT_CVAL1  %08x %08X\n\r",&PIT_CVAL1, PIT_CVAL1);



  return 0;