[NUCLEO_L152RE] Change system clock to 32MHz + restart PLL after deepsleep

pull/221/head
bcostm 2014-03-20 11:28:30 +01:00
parent a5cb2f420f
commit ad9f894f10
2 changed files with 63 additions and 101 deletions

View File

@ -3,7 +3,7 @@
* @file system_stm32l1xx.c * @file system_stm32l1xx.c
* @author MCD Application Team * @author MCD Application Team
* @version V1.2.0 * @version V1.2.0
* @date 11-January-2014 * @date 14-March-2014
* @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File. * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
* This file contains the system clock configuration for STM32L1xx Ultra * This file contains the system clock configuration for STM32L1xx Ultra
* Low power devices, and is generated by the clock configuration * Low power devices, and is generated by the clock configuration
@ -43,11 +43,11 @@
*============================================================================= *=============================================================================
* System Clock Configuration * System Clock Configuration
*============================================================================= *=============================================================================
* System clock source | HSI * System Clock source | PLL(HSI)
*----------------------------------------------------------------------------- *-----------------------------------------------------------------------------
* SYSCLK | 16000000 Hz * SYSCLK | 32000000 Hz
*----------------------------------------------------------------------------- *-----------------------------------------------------------------------------
* HCLK | 16000000 Hz * HCLK | 32000000 Hz
*----------------------------------------------------------------------------- *-----------------------------------------------------------------------------
* AHB Prescaler | 1 * AHB Prescaler | 1
*----------------------------------------------------------------------------- *-----------------------------------------------------------------------------
@ -55,17 +55,17 @@
*----------------------------------------------------------------------------- *-----------------------------------------------------------------------------
* APB2 Prescaler | 1 * APB2 Prescaler | 1
*----------------------------------------------------------------------------- *-----------------------------------------------------------------------------
* HSE Frequency | 8000000 Hz * HSE Frequency | Not used
*----------------------------------------------------------------------------- *-----------------------------------------------------------------------------
* PLL DIV | Not Used * PLL DIV | 2
*----------------------------------------------------------------------------- *-----------------------------------------------------------------------------
* PLL MUL | Not Used * PLL MUL | 4
*----------------------------------------------------------------------------- *-----------------------------------------------------------------------------
* VDD | 3.3 V * VDD | 3.3 V
*----------------------------------------------------------------------------- *-----------------------------------------------------------------------------
* Vcore | 1.8 V (Range 1) * Vcore | 1.8 V (Range 1)
*----------------------------------------------------------------------------- *-----------------------------------------------------------------------------
* Flash Latency | 0 WS * Flash Latency | 1 WS
*----------------------------------------------------------------------------- *-----------------------------------------------------------------------------
* Require 48MHz for USB clock | Disabled * Require 48MHz for USB clock | Disabled
*----------------------------------------------------------------------------- *-----------------------------------------------------------------------------
@ -149,7 +149,7 @@
/** @addtogroup STM32L1xx_System_Private_Variables /** @addtogroup STM32L1xx_System_Private_Variables
* @{ * @{
*/ */
uint32_t SystemCoreClock = 16000000; uint32_t SystemCoreClock = 32000000;
__I uint8_t PLLMulTable[9] = {3, 4, 6, 8, 12, 16, 24, 32, 48}; __I uint8_t PLLMulTable[9] = {3, 4, 6, 8, 12, 16, 24, 32, 48};
__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}; __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
@ -161,7 +161,7 @@ __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}
* @{ * @{
*/ */
static void SetSysClock(void); void SetSysClock(void);
/** /**
* @} * @}
@ -206,6 +206,23 @@ void SystemInit (void)
#else #else
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */ SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
#endif #endif
/* ADDED FOR MBED DEBUG PURPOSE */
/*
// Enable the GPIOA peripheral
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
// Output the system clock on MCO pin (PA.08)
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Select the clock to output on MCO pin (PA.08)
RCC_MCOConfig(RCC_MCOSource_SYSCLK, RCC_MCODiv_1);
//RCC_MCOConfig(RCC_MCOSource_HSI, RCC_MCODiv_1);
*/
} }
/** /**
@ -305,7 +322,7 @@ void SystemCoreClockUpdate (void)
* @param None * @param None
* @retval None * @retval None
*/ */
static void SetSysClock(void) void SetSysClock(void)
{ {
__IO uint32_t StartUpCounter = 0, HSIStatus = 0; __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
@ -330,15 +347,14 @@ static void SetSysClock(void)
if (HSIStatus == (uint32_t)0x01) if (HSIStatus == (uint32_t)0x01)
{ {
/* Flash 0 wait state */ /* Enable 64-bit access */
FLASH->ACR &= ~FLASH_ACR_LATENCY; FLASH->ACR |= FLASH_ACR_ACC64;
/* Disable Prefetch Buffer */ /* Enable Prefetch Buffer */
FLASH->ACR &= ~FLASH_ACR_PRFTEN; FLASH->ACR |= FLASH_ACR_PRFTEN;
/* Disable 64-bit access */
FLASH->ACR &= ~FLASH_ACR_ACC64;
/* Flash 1 wait state (latency) */
FLASH->ACR |= FLASH_ACR_LATENCY;
/* Power enable */ /* Power enable */
RCC->APB1ENR |= RCC_APB1ENR_PWREN; RCC->APB1ENR |= RCC_APB1ENR_PWREN;
@ -346,26 +362,39 @@ static void SetSysClock(void)
/* Select the Voltage Range 1 (1.8 V) */ /* Select the Voltage Range 1 (1.8 V) */
PWR->CR = PWR_CR_VOS_0; PWR->CR = PWR_CR_VOS_0;
/* Wait Until the Voltage Regulator is ready */ /* Wait Until the Voltage Regulator is ready */
while((PWR->CSR & PWR_CSR_VOSF) != RESET) while((PWR->CSR & PWR_CSR_VOSF) != RESET)
{ {
} }
/* HCLK = SYSCLK /1*/ /* PLL configuration */
/* SYSCLK = (HSI 16 MHz * 4) / 2 = 32 MHz */
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLMUL | RCC_CFGR_PLLDIV));
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSI | RCC_CFGR_PLLMUL4 | RCC_CFGR_PLLDIV2);
/* HCLK = 32 MHz */
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1; RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
/* PCLK2 = HCLK /1*/
/* PCLK2 = 32 MHz */
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1; RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
/* PCLK1 = HCLK /1*/ /* PCLK1 = 32 MHz */
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1; RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
/* Select HSI as system clock source */ /* Enable PLL */
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW)); RCC->CR |= RCC_CR_PLLON;
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSI;
/* Wait till HSI is used as system clock source */ /* Wait till PLL is ready */
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_HSI) while((RCC->CR & RCC_CR_PLLRDY) == 0)
{
}
/* Select PLL as system clock source */
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
/* Wait till PLL is used as system clock source */
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
{ {
} }
} }

View File

@ -30,74 +30,8 @@
#include "sleep_api.h" #include "sleep_api.h"
#include "cmsis.h" #include "cmsis.h"
static void SetSysClock_HSI(void) // This function is in the system_stm32l1xx.c file
{ extern void SetSysClock(void);
__IO uint32_t StartUpCounter = 0, HSIStatus = 0;
/* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
/* Enable HSI */
RCC->CR |= ((uint32_t)RCC_CR_HSION);
/* Wait till HSI is ready and if Time out is reached exit */
do
{
HSIStatus = RCC->CR & RCC_CR_HSIRDY;
} while((HSIStatus == 0) && (StartUpCounter != HSI_STARTUP_TIMEOUT));
if ((RCC->CR & RCC_CR_HSIRDY) != RESET)
{
HSIStatus = (uint32_t)0x01;
}
else
{
HSIStatus = (uint32_t)0x00;
}
if (HSIStatus == (uint32_t)0x01)
{
/* Flash 0 wait state */
FLASH->ACR &= ~FLASH_ACR_LATENCY;
/* Disable Prefetch Buffer */
FLASH->ACR &= ~FLASH_ACR_PRFTEN;
/* Disable 64-bit access */
FLASH->ACR &= ~FLASH_ACR_ACC64;
/* Power enable */
RCC->APB1ENR |= RCC_APB1ENR_PWREN;
/* Select the Voltage Range 1 (1.8 V) */
PWR->CR = PWR_CR_VOS_0;
/* Wait Until the Voltage Regulator is ready */
while((PWR->CSR & PWR_CSR_VOSF) != RESET)
{
}
/* HCLK = SYSCLK /1*/
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
/* PCLK2 = HCLK /1*/
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
/* PCLK1 = HCLK /1*/
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
/* Select HSI as system clock source */
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSI;
/* Wait till HSI is used as system clock source */
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_HSI)
{
}
}
else
{
/* If HSI fails to start-up, the application will have wrong clock
configuration. User can add here some code to deal with this error */
}
}
// MCU SLEEP mode // MCU SLEEP mode
void sleep(void) void sleep(void)
@ -121,7 +55,6 @@ void deepsleep(void)
// Enter Stop Mode // Enter Stop Mode
PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI); PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
// After wake-up from STOP reconfigure the system clock (HSI) // After wake-up from STOP reconfigure the PLL
SetSysClock_HSI(); SetSysClock();
} }