stm32l: initialize clocks

Run from internal clock at 16Mhz, but enable PLL to get a better
precision.

Signed-off-by: Vincent Palatin <vpalatin@chromium.org>

BUG=None
TEST=run on discovery board and check software is still alive after
clock initialization.

Change-Id: I8425482825015adf96c30e67a9320d0df2f4f2b7
This commit is contained in:
Vincent Palatin
2012-01-27 23:08:56 +00:00
parent 4c98732ce7
commit fb52ad00e4
2 changed files with 43 additions and 0 deletions

View File

@@ -8,6 +8,9 @@
#ifndef __BOARD_H
#define __BOARD_H
/* 16 MHz SYSCLK clock frequency */
#define CPU_CLOCK 16000000
#define USB_CHARGE_PORT_COUNT 0
/* GPIO signal list */

View File

@@ -7,8 +7,11 @@
#include <stdint.h>
#include "board.h"
#include "clock.h"
#include "common.h"
#include "registers.h"
#include "util.h"
/**
* Idle task
@@ -25,5 +28,42 @@ void __idle(void)
int clock_init(void)
{
/*
* The initial state :
* SYSCLK from HSI (=16MHz), no divider on AHB, APB1, APB2
* PLL unlocked, RTC enabled on LSE
*/
/* Ensure that HSI is ON */
if (!(STM32L_RCC_CR & (1 << 1))) {
/* Enable HSI */
STM32L_RCC_CR |= 1 << 0;
/* Wait for HSI to be ready */
while (!(STM32L_RCC_CR & (1 << 1)))
;
}
/*
* stays on HSI, no prescaler, PLLSRC = HSI, PLLMUL = x3, PLLDIV = /3,
* no MCO => PLLVCO = 48 MHz and PLLCLK = 16 Mhz
*/
BUILD_ASSERT(CPU_CLOCK == 16000000);
STM32L_RCC_CFGR = 0x00800001;
/* Enable the PLL */
STM32L_RCC_CR |= 1 << 24;
/* Wait for the PLL to lock */
while (!(STM32L_RCC_CR & (1 << 25)))
;
/* switch to SYSCLK to the PLL */
STM32L_RCC_CFGR = 0x00800003;
/* switch on LSI */
STM32L_RCC_CSR |= 1 << 0;
/* Wait for LSI to be ready */
while (!(STM32L_RCC_CSR & (1 << 1)))
;
/* Enable RTC and use LSI as clock source */
STM32L_RCC_CSR = (STM32L_RCC_CSR & ~0x00430000) | 0x00420000;
return EC_SUCCESS;
}