External oscillator for HSE

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
TravisT
Posts: 72
Joined: Sun Feb 23, 2014 2:31 pm
Location: Portland, OR
Contact:

External oscillator for HSE

Post by TravisT » Wed Nov 08, 2017 4:52 am

Hello,

I am building up some new custom boards based on the STM32F405 and similar pinout to the pyboard 1.0

I have done this in the past with good success, once getting past oscillator load capacitor issues. But partially because of that I decided to be clever and use a external oscillator to and just plug the nice stable 8mhz output directly into the OSC_IN and left OSC_OUT floating.

I was able to use DFU to flash Micropython, but never go the chip to go to REPL after that. What I realized is that there is most likely a configuration for the BYPASS mode for the clock instead of being setup for a Crystal.

Now hoping somebody smarter than myself can help me find out what I need to change to make this work.

I found this nice little SOT23-5 package that is automotive qualified and really want to use it. Small but still very hand solderable.
https://www.digikey.com/product-detail/ ... ND/6677385

Thanks!!
_______________
Travis Travelstead

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: External oscillator for HSE

Post by pythoncoder » Wed Nov 08, 2017 6:29 am

Hi. I'm afraid I have no experience of designing custom boards to run MicroPython or of porting MicroPython to new hardware. So I have no instant answer and my approach would be to trawl through the STM datasheet to figure out the solution, which I'm sure you could do yourself.

Others may have used an external oscillator and are likely to pop up with a solution. I assume you've done a forum search on HSE? I know the topic has come up a number of times.

That oscillator looks like a nice device. I'm never happy with crystal load capacitors and I know Damien had an issue with these in the early days of the Pyboard. That chip's one evident drawback is supply current which would be too high for ultra low power applications where the board goes to sleep with the RTC powered by a backup cell.

Good luck - I'm sure there's a simple firmware fix.

Regards, Pete
Peter Hinch
Index to my micropython libraries.

User avatar
TravisT
Posts: 72
Joined: Sun Feb 23, 2014 2:31 pm
Location: Portland, OR
Contact:

Re: External oscillator for HSE

Post by TravisT » Wed Nov 08, 2017 6:48 am

@pythoncoder Thanks for taking the time to look over my issue. As you said I can hunt, and I have spent a little time looking through the code. I spent a little too much time hunting before realize the STM32 HAL has been moved into a separate repo.

Regarding power usage on the oscillator, I can understand that not being ideal. I am more concerned about simplicity and stability over a wide temperature range. I use it mostly for CAN and USB. But some of the chips have enable/disable so you can go into a lower power mode.

I did manage to hunt down this reference

stm32lib/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc.h
https://github.com/micropython/stm32lib ... _hal_rcc.h

Code: Select all

/** @defgroup RCC_HSE_Config HSE Config
  * @{
  */
#define RCC_HSE_OFF                      0x00000000U
#define RCC_HSE_ON                       RCC_CR_HSEON
#define RCC_HSE_BYPASS                   ((uint32_t)(RCC_CR_HSEBYP | RCC_CR_HSEON))


Which is then used here, I believe to set to standard HSE_ON.
micropython/ports/stm32/system_stm32.c
https://github.com/micropython/micropyt ... em_stm32.c

Code: Select all

    /* Enable HSE Oscillator and activate PLL with HSE as source */
    #if defined(MCU_SERIES_F4) || defined(MCU_SERIES_F7)
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
I think I am on the right track, but not sure how I should implement this.
_______________
Travis Travelstead

chrismas9
Posts: 152
Joined: Wed Jun 25, 2014 10:07 am

Re: External oscillator for HSE

Post by chrismas9 » Wed Nov 08, 2017 12:51 pm

I find the STM32cube utility very useful. It has a GUI clock editor that lets you change all the clock registers and see the effect. It can also generate start up code.

Also look at the Nucleo ports. Nucleo boards don't have an HSE crystal, they get an 8 MHz clock from the ST-LINK chip

User avatar
TravisT
Posts: 72
Joined: Sun Feb 23, 2014 2:31 pm
Location: Portland, OR
Contact:

Re: External oscillator for HSE

Post by TravisT » Wed Nov 08, 2017 1:56 pm

Based on what I have seen so far it would appear I just need to edit two things.

micropython/ports/stm32/modmachine.c
https://github.com/micropython/micropyt ... dmachine.c

Code: Select all

        // re-configure PLL
        // even if we don't use the PLL for the system clock, we still need it for USB, RNG and SDIO
        RCC_OscInitTypeDef RCC_OscInitStruct;
        RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
        RCC_OscInitStruct.HSEState = RCC_HSE_ON;
micropython/ports/stm32/system_stm32.c
https://github.com/micropython/micropyt ... em_stm32.c

Code: Select all

    /* Enable HSE Oscillator and activate PLL with HSE as source */
    #if defined(MCU_SERIES_F4) || defined(MCU_SERIES_F7)
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
I am just not sure if there is anything else I need to change. I am using 8MHz oscillator and the Py1.0 build. I did not notice any changes after I was able to update with DFU.
_______________
Travis Travelstead

User avatar
TravisT
Posts: 72
Joined: Sun Feb 23, 2014 2:31 pm
Location: Portland, OR
Contact:

Re: External oscillator for HSE

Post by TravisT » Wed Nov 08, 2017 1:58 pm

@chrismas9
Thanks for the suggestion. I looked at it a little, but I will see if I can find any more to compare against.
_______________
Travis Travelstead

User avatar
TravisT
Posts: 72
Joined: Sun Feb 23, 2014 2:31 pm
Location: Portland, OR
Contact:

Re: External oscillator for HSE

Post by TravisT » Wed Nov 08, 2017 3:25 pm

Because I am using the same clock frequency (8MHz) it looks like there is not much else to change.

From what I can see all I should need to change is is RCC_HSE_ON to RCC_HSE_BYPASS. What I am having a struggle with, is if this is true why am I able to program with DFU but not be able to use the USB once it is flashed.

Code: Select all

/** @defgroup RCC_HSE_Config HSE Config
  * @{
  */
#define RCC_HSE_OFF                      0x00000000U
#define RCC_HSE_ON                       RCC_CR_HSEON
#define RCC_HSE_BYPASS                   ((uint32_t)(RCC_CR_HSEBYP | RCC_CR_HSEON))
_______________
Travis Travelstead

User avatar
TravisT
Posts: 72
Joined: Sun Feb 23, 2014 2:31 pm
Location: Portland, OR
Contact:

Re: External oscillator for HSE

Post by TravisT » Wed Nov 08, 2017 3:54 pm

Thanks for who has put time in for helping me. Like all fun issues on new hardware there was two variables at work here.

1 - Settings for external clock source and not a crystal
2 - Making sure you remove pin configurations that might affect the system boot. In particular I am thinking the USB VBUS detect pin.

I was able to flash the updated firmware, and the book into a REPL over USB.

Funny how it seems so obvious now. I hope this helps others, in the future.
_______________
Travis Travelstead

Post Reply