Skip to content

Commit ad9f894

Browse files
committed
[NUCLEO_L152RE] Change system clock to 32MHz + restart PLL after deepsleep
1 parent a5cb2f4 commit ad9f894

File tree

2 files changed

+62
-100
lines changed

2 files changed

+62
-100
lines changed

libraries/mbed/targets/cmsis/TARGET_STM/TARGET_NUCLEO_L152RE/system_stm32l1xx.c

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @file system_stm32l1xx.c
44
* @author MCD Application Team
55
* @version V1.2.0
6-
* @date 11-January-2014
6+
* @date 14-March-2014
77
* @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
88
* This file contains the system clock configuration for STM32L1xx Ultra
99
* Low power devices, and is generated by the clock configuration
@@ -43,29 +43,29 @@
4343
*=============================================================================
4444
* System Clock Configuration
4545
*=============================================================================
46-
* System clock source | HSI
46+
* System Clock source | PLL(HSI)
4747
*-----------------------------------------------------------------------------
48-
* SYSCLK | 16000000 Hz
48+
* SYSCLK | 32000000 Hz
4949
*-----------------------------------------------------------------------------
50-
* HCLK | 16000000 Hz
50+
* HCLK | 32000000 Hz
5151
*-----------------------------------------------------------------------------
5252
* AHB Prescaler | 1
5353
*-----------------------------------------------------------------------------
5454
* APB1 Prescaler | 1
5555
*-----------------------------------------------------------------------------
5656
* APB2 Prescaler | 1
5757
*-----------------------------------------------------------------------------
58-
* HSE Frequency | 8000000 Hz
58+
* HSE Frequency | Not used
5959
*-----------------------------------------------------------------------------
60-
* PLL DIV | Not Used
60+
* PLL DIV | 2
6161
*-----------------------------------------------------------------------------
62-
* PLL MUL | Not Used
62+
* PLL MUL | 4
6363
*-----------------------------------------------------------------------------
6464
* VDD | 3.3 V
6565
*-----------------------------------------------------------------------------
6666
* Vcore | 1.8 V (Range 1)
6767
*-----------------------------------------------------------------------------
68-
* Flash Latency | 0 WS
68+
* Flash Latency | 1 WS
6969
*-----------------------------------------------------------------------------
7070
* Require 48MHz for USB clock | Disabled
7171
*-----------------------------------------------------------------------------
@@ -149,7 +149,7 @@
149149
/** @addtogroup STM32L1xx_System_Private_Variables
150150
* @{
151151
*/
152-
uint32_t SystemCoreClock = 16000000;
152+
uint32_t SystemCoreClock = 32000000;
153153
__I uint8_t PLLMulTable[9] = {3, 4, 6, 8, 12, 16, 24, 32, 48};
154154
__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
155155

@@ -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}
161161
* @{
162162
*/
163163

164-
static void SetSysClock(void);
164+
void SetSysClock(void);
165165

166166
/**
167167
* @}
@@ -206,6 +206,23 @@ void SystemInit (void)
206206
#else
207207
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
208208
#endif
209+
210+
/* ADDED FOR MBED DEBUG PURPOSE */
211+
/*
212+
// Enable the GPIOA peripheral
213+
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
214+
// Output the system clock on MCO pin (PA.08)
215+
GPIO_InitTypeDef GPIO_InitStructure;
216+
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
217+
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
218+
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
219+
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
220+
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
221+
GPIO_Init(GPIOA, &GPIO_InitStructure);
222+
// Select the clock to output on MCO pin (PA.08)
223+
RCC_MCOConfig(RCC_MCOSource_SYSCLK, RCC_MCODiv_1);
224+
//RCC_MCOConfig(RCC_MCOSource_HSI, RCC_MCODiv_1);
225+
*/
209226
}
210227

211228
/**
@@ -305,7 +322,7 @@ void SystemCoreClockUpdate (void)
305322
* @param None
306323
* @retval None
307324
*/
308-
static void SetSysClock(void)
325+
void SetSysClock(void)
309326
{
310327
__IO uint32_t StartUpCounter = 0, HSIStatus = 0;
311328

@@ -330,42 +347,54 @@ static void SetSysClock(void)
330347

331348
if (HSIStatus == (uint32_t)0x01)
332349
{
333-
/* Flash 0 wait state */
334-
FLASH->ACR &= ~FLASH_ACR_LATENCY;
350+
/* Enable 64-bit access */
351+
FLASH->ACR |= FLASH_ACR_ACC64;
335352

336-
/* Disable Prefetch Buffer */
337-
FLASH->ACR &= ~FLASH_ACR_PRFTEN;
353+
/* Enable Prefetch Buffer */
354+
FLASH->ACR |= FLASH_ACR_PRFTEN;
338355

339-
/* Disable 64-bit access */
340-
FLASH->ACR &= ~FLASH_ACR_ACC64;
356+
/* Flash 1 wait state (latency) */
357+
FLASH->ACR |= FLASH_ACR_LATENCY;
341358

342-
343359
/* Power enable */
344360
RCC->APB1ENR |= RCC_APB1ENR_PWREN;
345361

346362
/* Select the Voltage Range 1 (1.8 V) */
347363
PWR->CR = PWR_CR_VOS_0;
348-
349-
364+
350365
/* Wait Until the Voltage Regulator is ready */
351366
while((PWR->CSR & PWR_CSR_VOSF) != RESET)
352367
{
353368
}
354-
355-
/* HCLK = SYSCLK /1*/
369+
370+
/* PLL configuration */
371+
/* SYSCLK = (HSI 16 MHz * 4) / 2 = 32 MHz */
372+
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLMUL | RCC_CFGR_PLLDIV));
373+
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSI | RCC_CFGR_PLLMUL4 | RCC_CFGR_PLLDIV2);
374+
375+
/* HCLK = 32 MHz */
356376
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
357-
/* PCLK2 = HCLK /1*/
377+
378+
/* PCLK2 = 32 MHz */
358379
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
359380

360-
/* PCLK1 = HCLK /1*/
381+
/* PCLK1 = 32 MHz */
361382
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
362-
363-
/* Select HSI as system clock source */
383+
384+
/* Enable PLL */
385+
RCC->CR |= RCC_CR_PLLON;
386+
387+
/* Wait till PLL is ready */
388+
while((RCC->CR & RCC_CR_PLLRDY) == 0)
389+
{
390+
}
391+
392+
/* Select PLL as system clock source */
364393
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
365-
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSI;
394+
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
366395

367-
/* Wait till HSI is used as system clock source */
368-
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_HSI)
396+
/* Wait till PLL is used as system clock source */
397+
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
369398
{
370399
}
371400
}

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/sleep.c

Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -30,74 +30,8 @@
3030
#include "sleep_api.h"
3131
#include "cmsis.h"
3232

33-
static void SetSysClock_HSI(void)
34-
{
35-
__IO uint32_t StartUpCounter = 0, HSIStatus = 0;
36-
37-
/* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
38-
/* Enable HSI */
39-
RCC->CR |= ((uint32_t)RCC_CR_HSION);
40-
41-
/* Wait till HSI is ready and if Time out is reached exit */
42-
do
43-
{
44-
HSIStatus = RCC->CR & RCC_CR_HSIRDY;
45-
} while((HSIStatus == 0) && (StartUpCounter != HSI_STARTUP_TIMEOUT));
46-
47-
if ((RCC->CR & RCC_CR_HSIRDY) != RESET)
48-
{
49-
HSIStatus = (uint32_t)0x01;
50-
}
51-
else
52-
{
53-
HSIStatus = (uint32_t)0x00;
54-
}
55-
56-
if (HSIStatus == (uint32_t)0x01)
57-
{
58-
/* Flash 0 wait state */
59-
FLASH->ACR &= ~FLASH_ACR_LATENCY;
60-
61-
/* Disable Prefetch Buffer */
62-
FLASH->ACR &= ~FLASH_ACR_PRFTEN;
63-
64-
/* Disable 64-bit access */
65-
FLASH->ACR &= ~FLASH_ACR_ACC64;
66-
67-
/* Power enable */
68-
RCC->APB1ENR |= RCC_APB1ENR_PWREN;
69-
70-
/* Select the Voltage Range 1 (1.8 V) */
71-
PWR->CR = PWR_CR_VOS_0;
72-
73-
/* Wait Until the Voltage Regulator is ready */
74-
while((PWR->CSR & PWR_CSR_VOSF) != RESET)
75-
{
76-
}
77-
78-
/* HCLK = SYSCLK /1*/
79-
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
80-
/* PCLK2 = HCLK /1*/
81-
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
82-
83-
/* PCLK1 = HCLK /1*/
84-
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
85-
86-
/* Select HSI as system clock source */
87-
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
88-
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSI;
89-
90-
/* Wait till HSI is used as system clock source */
91-
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_HSI)
92-
{
93-
}
94-
}
95-
else
96-
{
97-
/* If HSI fails to start-up, the application will have wrong clock
98-
configuration. User can add here some code to deal with this error */
99-
}
100-
}
33+
// This function is in the system_stm32l1xx.c file
34+
extern void SetSysClock(void);
10135

10236
// MCU SLEEP mode
10337
void sleep(void)
@@ -121,7 +55,6 @@ void deepsleep(void)
12155
// Enter Stop Mode
12256
PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
12357

124-
// After wake-up from STOP reconfigure the system clock (HSI)
125-
SetSysClock_HSI();
126-
58+
// After wake-up from STOP reconfigure the PLL
59+
SetSysClock();
12760
}

0 commit comments

Comments
 (0)