cancel
Showing results for 
Search instead for 
Did you mean: 

Proper way to switch to HSE

shotaro
Associate II
Posted on April 05, 2012 at 18:43

My application calls for using an external oscillator (not a crystal) as a clock source. To test this, I have an arbitrary function generator outputting a 50% duty cycle square wave of the expected clock rate. The MCU is supposed to go to sleep, then upon wake up, switch to the HSE as fast as possible.

However, in tests, I find that switching to the HSE is always taking somewhere around 2.5ms. The function generator is always on, so I was expecting sub millisecond switching. Here's what I'm doing:

  1. On wake up, set the HSEON bit in the RCC_CR register (using the RCC_HSEConfig(ENABLE); function from the standard periph library)
  2. Do a spin-lock while loop until HSE is ready.
    1. The reason why I do a spin-lock is because the system clock is going to drive a PWM immediately afterwards, so the MCU cannot go ahead and drive it, otherwise the timing will be off.

Is there something I'm doing wrong that's causing up to 2.5ms delay? I recall when I had a regular crystal oscillator, it was adding about 1ms or so.

EDIT: I found the args for RCC_HSEConfig() are supposed to be RCC_HSE_ON or RCC_HSE_Bypass, and I'm not sure what ENABLE is defined as.
3 REPLIES 3
Posted on April 05, 2012 at 19:42

I'm hard pressed to see why you would need RCC_HSE_ON, it's not like you need the oscillator circuit running for an external TCXO or whatever.

The system will come up with HSI, whatever time it takes that to start/stabilize. But you should just be able to grind the gears and jam the SYSCLK directly to HSE, thus :

      RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE); /* Select HSE as system clock source */

      /* Wait till HSE is used as system clock source */

      while(RCC_GetSYSCLKSource() != 0x04); // HSI 00, HSE 04, PLL 08

Should only take a couple of minutes coding to confirm if it is viable, or not.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
shotaro
Associate II
Posted on April 09, 2012 at 18:41

Thanks for your response.

It appears I misunderstood what was wanted out of the board, so I'm not using the HSE as the system clock. In fact, there was no intent on using the HSE.

But a guy I spoke to about this thinks the hardware for the HSE is shut off when I go into STOP mode, and when I exit out of STOP mode, the HSE hardware needs to turn on and figure out what's the clock speed. And he thinks the wakeup time is dependent upon the speed of the clock.

Posted on April 09, 2012 at 19:04

Wakeup Latency from STOP - ''HSI RC wakeup time + regulator wakeup time from Low-power mode''

HSI is spec'd to come up in 1-2 us

LSI is up to 85 us

Wakeup from STOP (reg in run mode) typical 3.6 us

Wakeup from STOP (reg in low power mode) typical 5.4 us

If you use the PLL, it's lock time is typically 200 us

HSE has a typical startup time of  2 ms after VDD stabilizes, but is hugely dependent on the properties of the crystal.

I can't find a number quickly for switching clocks, but the circuit probably uses some simple synchronization techniques to avoid glitching as it switches over, which might equate to a couple of clocks at the slowest rate. At a guess, less than a micro-second. The main purpose of waiting in the spin loop is probably to assure the thing is actually clocking.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..