Lib to change the clock speed of the ST Nucleo L152RE board to 32 MHz.

The ST Nucleo L152 board is running on 16 MHz out of the box. To speed up the cpu to the maximum 32MHz speed we have to change the clock setting. Simply add the lib and :

         #include "ST_L152_32MHZ.h"
        L152_init32 myinit(0);   // use the internal oscillator 

in front of your program. This should be the first line in main to ensure the frequency is changed before other objects are initialised.

This frequency is generated out of the internal RC oscillator. The frequency is not so stable like a crystal. The PLL is switched to 96MHz to enable the use of the USB interface.

If you need a more precise timing source, you have to add a external crystal.

/media/uploads/dreschpe/oszillator.jpg

You need : X3 8MHz crystal , C33 and C34 18pF 0603 , R35 and R37 have to be short with a small piece of wire.

         #include "ST_L152_32MHZ.h"
        L152_init32 myinit(1);   // use external crystal oscillator 

Files at this revision

API Documentation at this revision

Comitter:
dreschpe
Date:
Tue Mar 11 20:34:57 2014 +0000
Child:
1:bdeac50afe1a
Commit message:
Lib for the ST Nucleo L152RE board to switch the clock to 32MHz.; Out of the box the board is running on 16MHz.

Changed in this revision

ST_L152_32MHZ.cpp Show annotated file Show diff for this revision Revisions of this file
ST_L152_32MHZ.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ST_L152_32MHZ.cpp	Tue Mar 11 20:34:57 2014 +0000
@@ -0,0 +1,46 @@
+
+
+#include "stm32l1xx.h"
+#include "stm32l1xx_flash.h"
+#include "stm32l1xx_rcc.h"
+#include "ST_L152_32MHZ.h"
+
+// set cpu clock to 32MHz
+
+
+
+L152_init32::L152_init32(unsigned int external){
+    clk_err = setup_clock_32MHZ(external);
+    }
+
+#define PLL_STARTUP_TIMEOUT 0x5000
+
+int L152_init32::setup_clock_32MHZ(int external)
+{
+    uint32_t PLLStartUpCounter = 0,PLLStatus = 0,error;
+
+    if(external == 0) {                                               // internal Oscillator
+        RCC_PLLConfig(RCC_PLLSource_HSI,RCC_PLLMul_6,RCC_PLLDiv_3);   // setup pll to 96MHz to use USB
+    } else {
+        RCC_HSEConfig(RCC_HSE_ON);                                    // start external crystal osc.
+        error = RCC_WaitForHSEStartUp();
+        if(error == ERROR ) { // no external crystal
+            return(0);
+        }
+        RCC_PLLConfig(RCC_PLLSource_HSE,RCC_PLLMul_12,RCC_PLLDiv_3);   // setup pll to 96MHz to use USB
+    }
+    RCC_PLLCmd(ENABLE);                                                // switch on pll
+    do {
+        PLLStatus = RCC->CR & RCC_CR_PLLRDY;
+    } while((PLLStatus == 0) && (PLLStartUpCounter < PLL_STARTUP_TIMEOUT)); // wait for pll
+    if(PLLStatus == 0) {
+        return(0);
+    }
+    FLASH_SetLatency(FLASH_Latency_1);
+    FLASH_PrefetchBufferCmd(ENABLE);
+    FLASH_ReadAccess64Cmd(ENABLE);
+    RCC_HCLKConfig(RCC_SYSCLK_Div2);
+    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);  // switch to 32 MHz clock
+    SystemCoreClockUpdate();                    // update SystemCoreClock var
+    return(1);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ST_L152_32MHZ.h	Tue Mar 11 20:34:57 2014 +0000
@@ -0,0 +1,49 @@
+/* mbed library for the ST NUCLEO board L152RE 
+ * to change the CPU clock to 32 MHz
+ * A pll clock of 96 MHz is used to enable USB  
+ *
+ * Copyright (c) 2014 Peter Drescher - DC2PD
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+ 
+ 
+#ifndef MBED_ST_L152_32MHZ_H
+#define MBED_ST_L152_32MHZ_H
+
+/** Setup cpu speed to 32 MHz
+ *
+ * @code
+ * #include "mbed.h"
+ * #include "ST_L152_32MHZ.h"
+ *
+ * // place the init before other code to ensure right timing of other objects !
+ * L152_init32 myinit(0);   // use the internal oscillator 
+ * 
+ */
+
+class L152_init32
+{
+public:
+ /** Create a L152_init32 object to change the clock
+   * @param external = 0 use internal oscillator
+   * @param external = 1 use external 8 MHz crystal - you have to add some comonents to the pcb !
+*/
+L152_init32(unsigned int external);
+int clk_err;
+
+protected:
+// do the magic ;-)
+int setup_clock_32MHZ(int external);
+};
+
+#endif
+
+