|
30 | 30 | #include "sleep_api.h"
|
31 | 31 | #include "cmsis.h"
|
32 | 32 |
|
| 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 | +} |
| 101 | + |
| 102 | +// MCU SLEEP mode |
33 | 103 | void sleep(void)
|
34 | 104 | {
|
35 |
| - SCB->SCR = 0; // Normal sleep mode for ARM core |
36 |
| - __WFI(); |
| 105 | + // Enable PWR clock |
| 106 | + RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); |
| 107 | + // Request to enter SLEEP mode with regulator ON |
| 108 | + PWR_EnterSleepMode(PWR_Regulator_ON, PWR_SLEEPEntry_WFI); |
37 | 109 | }
|
38 | 110 |
|
| 111 | +// MCU STOP mode (Regulator in LP mode, LSI, HSI and HSE OFF) |
39 | 112 | void deepsleep(void)
|
40 | 113 | {
|
41 | 114 | // Enable PWR clock
|
42 | 115 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
|
43 | 116 |
|
44 |
| - // Request to enter STOP mode with regulator in low power mode |
| 117 | + // Enable Ultra low power mode |
| 118 | + PWR_UltraLowPowerCmd(ENABLE); |
| 119 | + |
| 120 | + // Enter Stop Mode |
45 | 121 | PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
|
| 122 | + |
| 123 | + // After wake-up from STOP reconfigure the system clock (HSI) |
| 124 | + SetSysClock_HSI(); |
| 125 | + |
46 | 126 | }
|
0 commit comments