Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: MCGIRCLK Clock

  1. #1

    MCGIRCLK Clock

    So I don't have to wade thru all the details:

    For the default Teensy3.1 setup (48 Mhz) is MCGIRCLK the same frequency as the system clock? Or what frequency is it at?

    I want to use LPTMR Low Power Timer just as an extra simple timer and it is run off this clock. Need to decide on the appropriate prescale.


    Thanks,

    TLB

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,574
    Quote Originally Posted by tlb View Post
    For the default Teensy3.1 setup (48 Mhz) is MCGIRCLK the same frequency as the system clock? Or what frequency is it at?
    I believe it's a copy of the 16 MHz crystal. It's direct from the crystal oscillator, without the small phase noise / jitter of the PLL.

    By default, it's not enabled. You have to set a bit in the oscillator config to enable it.... just a minor little detail, but truly frustrating if you're trying to use LPTMR or another peripheral with that clock, but it's not enabled in the OSC0_CR register (where it's documented with a slightly different name).

    Be careful when writing to OSC0_CR, since you can reconfigure the crystal capacitance if you change the low 4 bits.

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,574
    Also, be aware the documentation on reading LPTMR's CNR register, in section 38.4.5, is incorrect. Reading twice does nothing.

    You have to write, which causes the hardware to sync the actual CNR (which increments in the LPTMR's own clock domain) with the readable CNR register in the system clock domain. Then you can read CNR, and the value you're reading is the count at the moment you did that write.

  4. #4
    There are two clocks, MCGIRCLK, which is off the 4 Mhz internal reference and OSCERCLK which is directly off the 16 Mhz crystal. OSCERCLK is easier to setup (after the default initialization), which is all I care about.

    So for future reference for anyone else who wants to set up LPTMR:

    Code:
    // Initialize LPTMR
    	OSC0_CR |= OSC_ERCLKEN;	    // Enable OSCERCLK - bit 7 16 Mhz XTAL
    	SIM_SCGC5 |= SIM_SCGC5_LPTIMER;	// Enable Software Access to LPTMR
    
    	LPTMR0_CSR = 0;	    // Timer, Reset when Compare,
    	                    //  Interupt Disabled, Timer Disabled
    	LPTMR0_PSR = LPTMR_PSR_PRESCALE(0xD) | LPTMR_PSR_PCS(3);
    	    // 16K Prescale for 1 mS timer clock, select Clock OSCERCLK
    	LPTMR0_CMR = 1000;   // 1000 mSec timer
    
    	NVIC_ENABLE_IRQ(IRQ_LPTMR);	// Connect LPTMR Interrupt in Interrupt Controller
    
    	LPTMR0_CSR = LPTMR_CSR_TIE | LPTMR_CSR_TEN;  
    	                 //  Enable Timer and Interrupt
    Prescale and CMR will change depending on the actual time you want.

    TLB

  5. #5
    Senior Member
    Join Date
    Jan 2015
    Location
    SF Bay Area
    Posts
    255
    FWIW, MCGIRCLK is just as easy to setup.

    Code:
      MCG_C1 |= MCG_C1_IRCLKEN; //enables 4mhz clock
      MCG_C1 |= MCG_C1_IREFS; //set IREFS to switch to 32.768khz clock

    I tested this to work on touch sensor continuous trigger code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •