Browse Source

实现stm32进入死机状态,并定时唤醒。目前死机状态功耗是30多。

xuxinyi 2 months ago
parent
commit
de10d70669
7 changed files with 61 additions and 55 deletions
  1. 3 1
      CMakeLists.txt
  2. 1 0
      Core/Inc/E52.h
  3. 15 0
      Core/Inc/RTC_SLEEP.h
  4. 1 0
      Core/Inc/main.h
  5. 1 0
      Core/Src/E52.c
  6. 39 0
      Core/Src/RTC_SLEEP.c
  7. 1 54
      Core/Src/main.c

+ 3 - 1
CMakeLists.txt

@@ -36,7 +36,9 @@ add_executable(${CMAKE_PROJECT_NAME}
         Core/Src/E104-BT5005A.c
         Core/Inc/E104-BT5005A.h
         Core/Src/E52.c
-        Core/Inc/E52.h)
+        Core/Inc/E52.h
+        Core/Src/RTC_SLEEP.c
+        Core/Inc/RTC_SLEEP.h)
 
 # Add STM32CubeMX generated sources
 add_subdirectory(cmake/stm32cubemx)

+ 1 - 0
Core/Inc/E52.h

@@ -9,6 +9,7 @@
 #include <sys/types.h>
 #include "string.h"
 #include "stdlib.h"
+#include "RTC_SLEEP.h"
 
 // 组合命令和步骤的宏
 #define COMBINE_CMD_STEP(cmd, step) ((cmd) | (step))

+ 15 - 0
Core/Inc/RTC_SLEEP.h

@@ -0,0 +1,15 @@
+//
+// Created by EDZ on 25-9-16.
+//
+
+#ifndef RTC_SLEEP_H
+#define RTC_SLEEP_H
+#include <time.h>
+#include <stdlib.h>
+
+#include "string.h"
+#include "E52.h"
+#include "E104-BT5005A.h"
+
+void RTC_Sleep_Enter_Fun(void);
+#endif //RTC_SLEEP_H

+ 1 - 0
Core/Inc/main.h

@@ -64,6 +64,7 @@ extern uint8_t rx_buf_uart2[100];
 extern  uint8_t loraSendNextDataFlag;
 // extern uint8_t loraDataErrorCount;
 extern uint8_t totalData[120][50];
+extern  RTC_HandleTypeDef hrtc;
 extern uint8_t workMode;  // 0: 接收蓝牙数据 1: 使用lora发生数据。
 extern uint8_t receiveBlDataCount;
 extern UART_HandleTypeDef huart1;

+ 1 - 0
Core/Src/E52.c

@@ -154,6 +154,7 @@ void E52_Configuration_Fun() {
 
 void E52_Send_Sleep_Fun() {
     printf("这是让设备睡眠的命令\r\n");
+    RTC_Sleep_Enter_Fun();
     // TODO: command:05
 }
 

+ 39 - 0
Core/Src/RTC_SLEEP.c

@@ -0,0 +1,39 @@
+//
+// Created by EDZ on 25-9-16.
+//
+
+#include "../Inc/RTC_SLEEP.h"
+
+
+
+void RTC_Sleep_Enter_Fun() {
+    /* 设置初始时间 */
+    RTC_TimeTypeDef sTime = {0};
+    RTC_DateTypeDef sDate = {0};
+
+    sTime.Hours = 0x12;
+    sTime.Minutes = 0x00;
+    sTime.Seconds = 0x00;
+    HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD);
+
+    sDate.WeekDay = RTC_WEEKDAY_MONDAY;
+    sDate.Month = RTC_MONTH_SEPTEMBER;
+    sDate.Date = 0x16;
+    sDate.Year = 0x25;
+    HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD);
+
+    /* 设置闹钟:20 秒后触发 */
+    RTC_AlarmTypeDef sAlarm = {0};
+    sAlarm.AlarmTime.Hours = sTime.Hours;
+    sAlarm.AlarmTime.Minutes = sTime.Minutes;
+    sAlarm.AlarmTime.Seconds = (sTime.Seconds + 50) % 60;
+    sAlarm.Alarm = RTC_ALARM_A;
+
+    HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD);
+
+    /* 进入 STANDBY 模式 */
+    __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);               // 清除唤醒标志
+    HAL_PWR_EnterSTANDBYMode();                      // MCU 进入 STANDBY
+
+    // MCU 唤醒后会从 main() 重新开始运行
+}

+ 1 - 54
Core/Src/main.c

@@ -103,20 +103,6 @@ uint16_t randomFun() {
   return rand() % RandomTimer + baseRandomTimer; //随机时间1分钟到3分钟之间随机数
 }
 
-// 进入睡眠模式
-void EnterSleepMode(void)
-{
-  // 确保所有外设操作完成
-  HAL_Delay(10);
-
-  // 清除所有挂起的中断,防止立即唤醒
-  // __HAL_IRQ_CLEAR_PENDING(EXTI0_IRQn);
-
-  // 进入睡眠模式,任何中断都能唤醒
-  HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
-  // 或者使用WFE指令:HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFE);
-}
-
 void Device_Info_Init_Fun() {
   deviceInfo.isOnline = FALSE;
   deviceInfo.broadcast_type = BROADCAST_ALL;
@@ -219,45 +205,6 @@ int main(void)
   MX_USART3_UART_Init();
   MX_TIM4_Init();
   MX_RTC_Init();
-
-  /* 设置初始时间 */
-  RTC_TimeTypeDef sTime = {0};
-  RTC_DateTypeDef sDate = {0};
-
-  sTime.Hours = 0x12;
-  sTime.Minutes = 0x00;
-  sTime.Seconds = 0x00;
-  HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD);
-
-  sDate.WeekDay = RTC_WEEKDAY_MONDAY;
-  sDate.Month = RTC_MONTH_SEPTEMBER;
-  sDate.Date = 0x16;
-  sDate.Year = 0x25;
-  HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD);
-
-  /* 设置第一次闹钟:20 秒后触发 */
-  RTC_AlarmTypeDef sAlarm = {0};
-  sAlarm.AlarmTime.Hours = sTime.Hours;
-  sAlarm.AlarmTime.Minutes = sTime.Minutes;
-  sAlarm.AlarmTime.Seconds = (sTime.Seconds + 20) % 60;
-  // sAlarm.AlarmMask = 0;
-  sAlarm.Alarm = RTC_ALARM_A;
-
-  HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD);
-
-
-  while (TRUE) {
-    // HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
-    HAL_SuspendTick();   // 停止 SysTick 中断
-    __WFI();             // 真正进入 Sleep
-    HAL_ResumeTick();    // 唤醒后恢复 SysTick
-    printf("wwww122\r\n");
-    break;
-  }
-  while (TRUE) {
-    printf("111\r\n");
-    HAL_Delay(1000);
-  }
   /* USER CODE BEGIN 2 */
   // 启动 USART2 DMA 接收
   HAL_UART_Receive_DMA(&huart3, rx_buf, sizeof(rx_buf));
@@ -302,7 +249,7 @@ int main(void)
   srand(HAL_GetTick());
   // HAL_TIM_Base_Start(&htim4);
   /* USER CODE END 2 */
-
+  RTC_Sleep_Enter_Fun();
   /* Infinite loop */
   /* USER CODE BEGIN WHILE */
   printf("Device is ready\r\n");