sleep example
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 00003 bool sleepyTime = false; 00004 00005 void interrupter() 00006 { 00007 sleepyTime = true; 00008 }; 00009 00010 int main() 00011 { 00012 /* (#)Initialize the LPTIM low level resources by implementing the */ 00013 DigitalOut led( PB_3 ); 00014 InterruptIn inter( PA_12 ); 00015 inter.fall( &interrupter ); 00016 led = 0; 00017 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; 00018 __HAL_RCC_MSI_RANGE_CONFIG( RCC_MSIRANGE_5 ); 00019 wait_ms( 2000 ); 00020 RCC_ClkInitStruct.ClockType = ( RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 ); 00021 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI; // 80 MHz 00022 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 80 MHz 00023 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; // 80 MHz 00024 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 80 MHz 00025 HAL_RCC_ClockConfig( &RCC_ClkInitStruct, FLASH_LATENCY_4 ); 00026 HAL_PWREx_ControlVoltageScaling( PWR_REGULATOR_VOLTAGE_SCALE2 ); 00027 HAL_PWREx_EnableLowPowerRunMode(); 00028 SystemCoreClockUpdate(); 00029 HAL_PWREx_EnterSTOP0Mode( PWR_STOPENTRY_WFI ); 00030 00031 if( HAL_RCC_GetSysClockFreq() < 3000000 ) { led = 1; } 00032 00033 wait_ms( 1 ); 00034 led = 0; 00035 wait_ms( 1000 ); 00036 led = 1; 00037 wait_ms( 10 ); 00038 led = 0; 00039 /* HAL_MPU_Disable(); */ 00040 /* __HAL_RCC_MSI_STANDBY_RANGE_CONFIG(RCC_MSIRANGE_5); */ 00041 /* __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_MSI); */ 00042 /* GPIO_InitTypeDef GPIO_InitStructure = {0}; */ 00043 /* GPIO_InitStructure.Pin = GPIO_PIN_All; */ 00044 /* GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; */ 00045 /* GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW; */ 00046 /* GPIO_InitStructure.Pull = GPIO_NOPULL; */ 00047 /* HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); */ 00048 /* HAL_GPIO_Init(GPIOH, &GPIO_InitStructure); */ 00049 /* __HAL_RCC_GPIOC_CLK_DISABLE(); */ 00050 /* __HAL_RCC_GPIOH_CLK_DISABLE(); */ 00051 /* __HAL_RCC_USB_CLK_DISABLE(); */ 00052 /* __HAL_RCC_ADC_CLK_DISABLE(); */ 00053 /* __HAL_RCC_USART1_CLK_DISABLE(); */ 00054 /* __HAL_RCC_USART2_CLK_DISABLE(); */ 00055 /* __HAL_RCC_LPUART1_CLK_DISABLE(); */ 00056 wait_ms( 2000 ); 00057 /* wait_ms(3000); */ 00058 /* HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN5); */ 00059 /* //pc.printf("1\r\n"); */ 00060 00061 /* /1* Enable the fast wake up from Ultra low power mode *1/ */ 00062 /* /1* HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); *1/ */ 00063 /* /1* HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); *1/ */ 00064 /* /1* __HAL_RCC_GPIOA_CLK_DISABLE(); *1/ */ 00065 /* /1* __HAL_RCC_GPIOB_CLK_DISABLE(); *1/ */ 00066 /* for(int i = 0; i < 13; ++i){ */ 00067 /* Thread::wait(1); */ 00068 /* HAL_GPIO_DeInit(GPIOA,i); */ 00069 /* HAL_GPIO_DeInit(GPIOB,i); */ 00070 /* HAL_GPIO_DeInit(GPIOC,i); */ 00071 /* HAL_GPIO_DeInit(GPIOH,i); */ 00072 /* } */ 00073 /* int j = 0; */ 00074 /* HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); */ 00075 /* HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); */ 00076 /* Thread::wait(5000); */ 00077 /* GPIO_InitStructure.Pin = GPIO_PIN_All; */ 00078 /* GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; */ 00079 /* GPIO_InitStructure.Pull = GPIO_NOPULL; */ 00080 /* HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); */ 00081 /* HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); */ 00082 00083 /* Timer t; */ 00084 while( 1 ) { 00085 if( sleepyTime ) { 00086 led = 0; 00087 /* HAL_PWR_EnterSTANDBYMode(); */ 00088 HAL_PWREx_EnterSTOP0Mode( PWR_STOPENTRY_WFI ); 00089 HAL_RCC_GetSysClockFreq(); 00090 wait_ms( 50 ); 00091 sleepyTime = false; 00092 }; 00093 00094 wait_ms( 1000 ); 00095 00096 /* event.rise(&pressed); */ 00097 /* if ((go_to_sleep == 0) || (go_to_sleep == 2)) { */ 00098 /* printf("%d: Running\n", i); */ 00099 /* myled = !myled; */ 00100 /* wait(1.0); */ 00101 /* } */ 00102 led = 1; 00103 00104 wait_ms( 100 ); 00105 00106 led = 0; 00107 00108 /* deepsleep(); */ 00109 /* if (go_to_sleep == 1) { */ 00110 /* myled = 0; */ 00111 /* printf("%d: Entering sleep (press user button to resume)\n", i); */ 00112 /* sleep(); */ 00113 /* } */ 00114 /* if (go_to_sleep == 3) { */ 00115 /* myled = 0; */ 00116 /* printf("%d: Entering deepsleep (press user button to resume)\n", i); */ 00117 /* deepsleep(); */ 00118 /* } */ 00119 /* t.reset(); */ 00120 /* t.start(); */ 00121 /* led = !led; */ 00122 /* j=0; */ 00123 /* while(j < 100000) { */ 00124 /* j = j + 1; */ 00125 /* } */ 00126 /* t.stop(); */ 00127 /* led = !led; */ 00128 /* led = !led; */ 00129 /* HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); */ 00130 /* HAL_GPIO_Init(GPIOD, &GPIO_InitStructure); */ 00131 /* HAL_GPIO_Init(GPIOH, &GPIO_InitStructure); */ 00132 /* Thread::wait(500); */ 00133 } 00134 }
Generated on Wed Jul 20 2022 07:23:55 by
1.7.2