5 years, 11 months ago.

How to set up stm32L4 in Run mode 2 (26 Mhz)?

Hello,

Can someone please guide me how to set up stm32L4 in Run mode 2 (26 Mhz)? If there is a source code and libraries for that it would be great. I could only find codes for the lowpower run mode (around 8 Mhz I think) and other sleep and stop modes .

1 Answer

5 years, 11 months ago.

I'm not sure what you mean by run mode 2. If you work from the mbed-os source code, you can make changes to oscillator configuration deep in the target folder in the file: system_stm32l4xx.c. You should be able to just tweak some of the PLL values and bring down the clock frequency. You can see in the function SetSysClock(void), it tries to start the micro using different oscillators based on #define settings. If the first oscillator fails to start it tries the next one. Figure out which oscillator you want to use and update that initialization routine. It's pretty straightforward. All of the clocks should already be using the PLL so to change main oscillator you can just tweak PLLN and PLLR and get a wide range of frequencies.

Also, you can setup a project in ST's CubeMx with that micro and pull up the clock configurator which gives you a nice graphic of how all the PLL settings work.

Accepted Answer

Thank you so much. This is what I will do, please correct me if I'm wrong: I plan to use MSI as input. In order to change the clock to 26 Mhz, I will change the parameter PLL_M and leave the rest of the parameters as they are in the method uint8_t SetSysClock_PLL_MSI(void) defined in [https://github.com/ARMmbed/mbed-os/blob/master/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L432xC/TARGET_NUCLEO_L432KC/system_clock.c#L80]. To calculate PLL_m , I solve for it in the equation: SystemClock = ((MSI / PLL_M) * PLL_N) / PLL_R and set the SystemClock to 26 Mhz. Then I will call the method from my main program to change the clock speed.

posted by maryam magdy 05 Jun 2018

I don't have one of these boards so can't try this, so am flying a bit blind. And it is slightly different than the file for the L4 board I have used. First thing, they are using a macro called CLOCK_SOURCE to determine which oscillator startup function to call. Using MSI by calling SetSysClock_PLL_MSI() is just fine, you just have to be sure it gets down that far in SetSysClock(void), as it first tries to start every other clock. CLOCK_SOURCE by default is probably set to use 8MHz external clock from the onboard stlink micro, so if you just change MSI settings it will never actually get there.

If you're using the devboard you can just use the 8MHz external clock signal as well which it's already using by default. This is the call to SetSysClock_PLL_HSE(1). I will assume you are using this because it should be called by default without having to worry about where CLOCK_SOURCE macro is defined. Using 8MHz external clock on HSE input through Trial and Error in ST Cube Mx I get the following values to get the SystemClock to 26MHz. Certain combinations are not allowed and CubeMx warns you about those. These settings work without any errors though.

PLLM = 2 PLLN = 26 PLLR = 4

SYSCLK = 26MHz AHB prescaler = 1

or 8MHz /2 * 26 / 4 = 26MHz

Relevant code then, starting on line 191:

    RCC_OscInitStruct.HSIState              = RCC_HSI_OFF;
    RCC_OscInitStruct.PLL.PLLSource         = RCC_PLLSOURCE_HSE; // 8 MHz
    RCC_OscInitStruct.PLL.PLLState          = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLM              = 2 // VCO input clock = 8 MHz (8 MHz / 2)
    RCC_OscInitStruct.PLL.PLLN              = 26; // VCO output clock = 104 MHz (4 MHz * 26)
    RCC_OscInitStruct.PLL.PLLP              = 7; // PLLSAI3 clock = 22 MHz (160 MHz / 7)
    RCC_OscInitStruct.PLL.PLLQ              = 2;
    RCC_OscInitStruct.PLL.PLLR              = 4; // PLL clock = 26 MHz (104 MHz / 4)

And no, you do Not call this from your main program. This function is already called automatically at startup to set the clock. You are modifying how it starts up.

You can use the MCO output pin on the micro to send the clock to PA8 and verify the frequency is what you expect. Line 63, just set DEBUG_MCO to 1.

#define DEBUG_MCO        (0) // Output the MCO on PA8 for debugging (0=OFF, 1=SYSCLK, 2=HSE, 3=HSI, 4=MSI)
posted by Graham S. 05 Jun 2018

Thank you a lot this really helped! Another question please: if you ever used mbed which mbed library do you use and with which compiler? for now, I'm using the online mbed compiler and the official mbed library but it doesn't allow me to change the source code of the mbed library. Either I'll export the library to another compiler or use another library like the mbed-src (but the problem with this one it's not updated like the official mbed)

posted by maryam magdy 05 Jun 2018

I'm working on a big project using mbed 5.4.2. Mostly I use it offline in System Workbench so I can do proper debugging. I use online compiler frequently to check some quick ideas or try libraries.

In the online compiler, you can import the source and make these changes in mbed-os. Ctrl-C copy the link to mbed-os github:

https://github.com/ARMmbed/mbed-os.git

In the online compiler, in your project hit import, then on the top of the page there is a hyperlink to import from URL. Put that link in and import as a library and you'll have the latest mbed-os source in your project. Delete the precompiled version. Compilation is a bit slower but it works just fine.

posted by Graham S. 05 Jun 2018