Author Topic: Generate output clock with KL03Z  (Read 7273 times)

Offline enrico

  • Newbie
  • *
  • Posts: 22
    • View Profile
Generate output clock with KL03Z
« on: August 25, 2016, 10:04:52 PM »
Hi all,

I'd like to generate an internal clock from the KL03 and output the signal through a GPIO pin. Specifically I need a clock between 6 and 12MHz, which I'd like to use to drive an FPGA. I tried with the PWM modules, but it seems I can't go above 4MHz, and I'm not even sure this is the best approach. Is there a way to output directly to a pin one of the internal KL03 clocks?

Below is the code I'm currently using for the PWM approach that gives 4MHz output on PB11.

Code: [Select]

    PWM_INTERRUPT_SETUP pwm_setup;
    pwm_setup.int_type = PWM_INTERRUPT;
    pwm_setup.pwm_mode = (PWM_SYS_CLK | PWM_PRESCALER_1);             // clock PWM timer from the IRC48M clock with /1 pre-scaler
    pwm_setup.int_handler = 0;                                           // no user interrupt call-back or DMA on PWM cycle
    pwm_setup.pwm_frequency = PWM_TIMER_US_DELAY(TIMER_FREQUENCY_VALUE(500000), 8);                   
    pwm_setup.pwm_value   = _PWM_PERCENT(50, pwm_setup.pwm_frequency);   // 50% PWM (high/low)
    pwm_setup.pwm_reference = (_TIMER_0 | 0);                            // timer module 0, channel 0
    fnConfigureInterrupt((void *)&pwm_setup);                            // enter configuration


Thank you,
Enrico

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Generate output clock with KL03Z
« Reply #1 on: August 26, 2016, 02:28:27 AM »
Hi Enrico

Try
pwm_setup.pwm_frequency = PWM_FREQUENCY(8000000, 1);
to set the frequency. This should give you 8MHz on the PWM output.

In case you don't have the macro in your version it is
#define PWM_FREQUENCY(frequency, prescaler)  (PWM_CLOCK/prescaler/frequency)


You can also use the CLKOUT pins to connect the bus clock to, however this will only be useful if you have the bus clock set to the desired frequency. Therefore I think that the PWM output method is the best for you - it is more flexible.

Regards

Mark

Offline enrico

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Generate output clock with KL03Z
« Reply #2 on: August 26, 2016, 03:27:19 PM »
Hi Mark,

Thanks for your reply. I tried with the new macro, but still I can't get more than 4MHz. There is no output at all when
I'm running the MCU at 48MHz at the moment (from HIRC and clock divide=1), and using the version Kinetis_17-6-2015. Any idea? Should I try to update the kinetis_PWM to the latest version?

    #define RUN_FROM_HIRC                                                // clock from internal 48MHz RC clock
    #define SYSTEM_CLOCK_DIVIDE  1                                       // 1 to 16 - usually 1


Offline enrico

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Generate output clock with KL03Z
« Reply #3 on: August 26, 2016, 03:48:15 PM »
I think I might have found the reason why PWM is limited to 4MHz.

In kinetis.h
Code: [Select]
#if defined KINETIS_KL
    #if defined TPM_CLOCKED_FROM_OSCERCLK                                // {55}
        #define TIMER_CLOCK       (OSCERCLK)
    #elif defined TPM_CLOCKED_FROM_MCGIRCLK
        #define TIMER_CLOCK       (MCGIRCLK)
    #else
        #if defined FLL_FACTOR
            #define TIMER_CLOCK   (MCGFLLCLK)
        #else
            #define TIMER_CLOCK   (MCGPLLCLK/2)
        #endif
    #endif
    #define PWM_CLOCK             TIMER_CLOCK
#elif defined KINETIS_KE
    #define TIMER_CLOCK           (BUS_CLOCK)
    #define PWM_CLOCK             (BUS_CLOCK)
#else
    #define TIMER_CLOCK           (BUS_CLOCK)
    #define PWM_CLOCK             (SYSTEM_CLOCK/2)
#endif

and then in app_hw_kinetis.h:
Code: [Select]
    #define TPM_CLOCKED_FROM_MCGIRCLK                                    // TPM clock is connected to MCGIRCLK (either 32kHz or 4MHz)
    #define USE_FAST_INTERNAL_CLOCK

And finally in kinetis.h
Code: [Select]
#if defined USE_FAST_INTERNAL_CLOCK                                  // if not selected the slow internal clock is used (when needed)
        #define MCGIRCLK       FAST_ICR                                  // 4MHz, or 8MHz in mcg lite
    #else
        #define MCGIRCLK       SLOW_ICR

With the KL03 FAST_ICR should be 8MHz, thus the 4MHz limitation.


Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Generate output clock with KL03Z
« Reply #4 on: August 26, 2016, 11:15:23 PM »
Enrico

To clock the TMP from the 48MHz clock you need to disabled
TPM_CLOCKED_FROM_OSCERCLK and
TPM_CLOCKED_FROM_MCGIRCLK

Regards

Mark

Offline enrico

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Generate output clock with KL03Z
« Reply #5 on: August 28, 2016, 06:55:19 PM »
Thank you, Mark! I really appreciate your help.