Parcourir la source

关闭lora部分代码。RTC按下时间获取

xuxinyi il y a 2 mois
Parent
commit
75eb184eb7
6 fichiers modifiés avec 177 ajouts et 223 suppressions
  1. 24 24
      Core/Inc/RTC_SLEEP.h
  2. 3 3
      Core/Src/E52.c
  3. 79 78
      Core/Src/RTC_SLEEP.c
  4. 37 42
      Core/Src/main.c
  5. 32 76
      Core/Src/stm32f1xx_it.c
  6. 2 0
      lora_bl.ioc

+ 24 - 24
Core/Inc/RTC_SLEEP.h

@@ -13,31 +13,31 @@
 
 
 // 定义结构体一个结构体用于存储读取到的日期时间
-typedef struct {
-    uint8_t hours;
-    uint8_t minutes;
-    uint8_t seconds;
-    uint8_t weekday;
-    uint8_t month;
-    uint8_t date;
-    uint8_t year;
-} RTC_DateTimeTypeDef;
-
-typedef struct {
-    RTC_DateTimeTypeDef startTime;  // 按下时的完整RTC时间
-    uint32_t key_Duration;          // 按压持续时长 (秒),改成32位
-    uint8_t  key_Count;             // 按键次数(可选)
-    uint8_t  key_CheckActive;       // 是否正在检测中
-} RTC_key_struct;
-
-extern RTC_DateTimeTypeDef firstPressTime;
-extern RTC_key_struct SOS_key_struct;
-extern RTC_key_struct JUGE_key_struct;
+// typedef struct {
+//     uint8_t hours;
+//     uint8_t minutes;
+//     uint8_t seconds;
+//     uint8_t weekday;
+//     uint8_t month;
+//     uint8_t date;
+//     uint8_t year;
+// } RTC_DateTimeTypeDef;
+//
+// typedef struct {
+//     RTC_DateTimeTypeDef startTime;  // 按下时的完整RTC时间
+//     uint32_t key_Duration;          // 按压持续时长 (秒),改成32位
+//     uint8_t  key_Count;             // 按键次数(可选)
+//     uint8_t  key_CheckActive;       // 是否正在检测中
+// } RTC_key_struct;
 
-void RTC_GetDateTime(RTC_HandleTypeDef *hrtc, RTC_DateTimeTypeDef *datetime);
+// extern RTC_DateTimeTypeDef firstPressTime;
+extern RTC_TimeTypeDef SOS_key_struct;
+extern RTC_TimeTypeDef JUGE_key_struct;
 
-void RTC_SetDateTime(RTC_HandleTypeDef *hrtc, uint8_t hours, uint8_t minutes, uint8_t seconds,
-                     uint8_t weekday, uint8_t month, uint8_t date, uint8_t year);
+// void RTC_GetDateTime(RTC_HandleTypeDef *hrtc, RTC_DateTimeTypeDef *datetime);
 
-void RTC_Sleep_Enter_Fun(void);
+// void RTC_SetDateTime(RTC_HandleTypeDef *hrtc, uint8_t hours, uint8_t minutes, uint8_t seconds,
+//                      uint8_t weekday, uint8_t month, uint8_t date, uint8_t year);
+//
+// void RTC_Sleep_Enter_Fun(void);
 #endif //RTC_SLEEP_H

+ 3 - 3
Core/Src/E52.c

@@ -55,7 +55,7 @@ void E104_BT5005A_DST_ADDR_Fun(void) {
  * 帧头:55 AA
  */
 void E52_Heartbeat_Fun() {
-    printf("这是心跳包\r\n");
+    // printf("这是心跳包\r\n");
     Heartbeat_structure heartbeat_structure;
 
     heartbeat_structure.frame_header_H = 0x55;
@@ -69,7 +69,7 @@ void E52_Heartbeat_Fun() {
     heartbeat_structure.frame_tail_H = 0xFF;
     heartbeat_structure.frame_tail_L = 0xEE;
 
-    printf("heartbeat size = %d\n", sizeof(heartbeat_structure));
+    // printf("heartbeat size = %d\n", sizeof(heartbeat_structure));
 
     HAL_UART_Transmit(&huart2, (uint8_t *)&heartbeat_structure, sizeof(heartbeat_structure), HAL_MAX_DELAY);
     // TODO: command:01
@@ -163,7 +163,7 @@ void E52_Configuration_Fun() {
 
 void E52_Send_Sleep_Fun() {
     printf("这是让设备睡眠的命令\r\n");
-    RTC_Sleep_Enter_Fun();
+    // RTC_Sleep_Enter_Fun();
     // TODO: command:05
 }
 

+ 79 - 78
Core/Src/RTC_SLEEP.c

@@ -3,82 +3,83 @@
 //
 
 #include "../Inc/RTC_SLEEP.h"
-
-RTC_key_struct SOS_key_struct = {
-    .key_Count = 0,
-    .key_Duration = 0,
-    .key_CheckActive = FALSE
-};
-
-RTC_key_struct JUGE_key_struct = {
-    .key_Count = 0,
-    .key_Duration = 0,
-    .key_CheckActive = FALSE
-};
-
-
-// 单独的RTC时间和日期设置函数
-void RTC_SetDateTime(RTC_HandleTypeDef *hrtc, uint8_t hours, uint8_t minutes, uint8_t seconds,
-                     uint8_t weekday, uint8_t month, uint8_t date, uint8_t year) {
-    RTC_TimeTypeDef sTime = {0};
-    RTC_DateTypeDef sDate = {0};
-
-    // 设置时间
-    sTime.Hours = hours;
-    sTime.Minutes = minutes;
-    sTime.Seconds = seconds;
-    HAL_RTC_SetTime(hrtc, &sTime, RTC_FORMAT_BCD);
-
-    // 设置日期
-    sDate.WeekDay = weekday;
-    sDate.Month = month;
-    sDate.Date = date;
-    sDate.Year = year;
-    HAL_RTC_SetDate(hrtc, &sDate, RTC_FORMAT_BCD);
-}
-
-// 读取RTC当前时间和日期
-void RTC_GetDateTime(RTC_HandleTypeDef *hrtc, RTC_DateTimeTypeDef *datetime) {
-    RTC_TimeTypeDef sTime = {0};
-    RTC_DateTypeDef sDate = {0};
-
-    // 读取当前时间
-    HAL_RTC_GetTime(hrtc, &sTime, RTC_FORMAT_BCD);
-    // 读取当前日期
-    HAL_RTC_GetDate(hrtc, &sDate, RTC_FORMAT_BCD);
-
-    // 存储到自定义结构体中
-    datetime->hours = sTime.Hours;
-    datetime->minutes = sTime.Minutes;
-    datetime->seconds = sTime.Seconds;
-    datetime->weekday = sDate.WeekDay;
-    datetime->month = sDate.Month;
-    datetime->date = sDate.Date;
-    datetime->year = sDate.Year;
-}
-
-void RTC_Sleep_Enter_Fun() {
-    /* 调用独立的函数设置初始时间 */
-    RTC_SetDateTime(&hrtc, 0x12, 0x00, 0x00,
-                   RTC_WEEKDAY_MONDAY, RTC_MONTH_SEPTEMBER, 0x16, 0x25);
-
-    /* 获取当前时间用于设置闹钟 */
-    RTC_TimeTypeDef currentTime = {0};
-    HAL_RTC_GetTime(&hrtc, &currentTime, RTC_FORMAT_BCD);
-
-    /* 设置闹钟:50秒后触发 */
-    RTC_AlarmTypeDef sAlarm = {0};
-    sAlarm.AlarmTime.Hours = currentTime.Hours;
-    sAlarm.AlarmTime.Minutes = currentTime.Minutes;
-    sAlarm.AlarmTime.Seconds = (currentTime.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() 重新开始运行
-}
+RTC_TimeTypeDef SOS_key_struct =  {0};
+RTC_TimeTypeDef JUGE_key_struct = {0};
+// RTC_key_struct SOS_key_struct = {
+//     .key_Count = 0,
+//     .key_Duration = 0,
+//     .key_CheckActive = FALSE
+// };
+//
+// RTC_key_struct JUGE_key_struct = {
+//     .key_Count = 0,
+//     .key_Duration = 0,
+//     .key_CheckActive = FALSE
+// };
+
+
+// // 单独的RTC时间和日期设置函数
+// void RTC_SetDateTime(RTC_HandleTypeDef *hrtc, uint8_t hours, uint8_t minutes, uint8_t seconds,
+//                      uint8_t weekday, uint8_t month, uint8_t date, uint8_t year) {
+//     RTC_TimeTypeDef sTime = {0};
+//     RTC_DateTypeDef sDate = {0};
+//
+//     // 设置时间
+//     sTime.Hours = hours;
+//     sTime.Minutes = minutes;
+//     sTime.Seconds = seconds;
+//     HAL_RTC_SetTime(hrtc, &sTime, RTC_FORMAT_BCD);
+//
+//     // 设置日期
+//     sDate.WeekDay = weekday;
+//     sDate.Month = month;
+//     sDate.Date = date;
+//     sDate.Year = year;
+//     HAL_RTC_SetDate(hrtc, &sDate, RTC_FORMAT_BCD);
+// }
+
+// // 读取RTC当前时间和日期
+// void RTC_GetDateTime(RTC_HandleTypeDef *hrtc, RTC_DateTimeTypeDef *datetime) {
+//     RTC_TimeTypeDef sTime = {0};
+//     RTC_DateTypeDef sDate = {0};
+//
+//     // 读取当前时间
+//     HAL_RTC_GetTime(hrtc, &sTime, RTC_FORMAT_BCD);
+//     // 读取当前日期
+//     HAL_RTC_GetDate(hrtc, &sDate, RTC_FORMAT_BCD);
+//
+//     // 存储到自定义结构体中
+//     datetime->hours = sTime.Hours;
+//     datetime->minutes = sTime.Minutes;
+//     datetime->seconds = sTime.Seconds;
+//     datetime->weekday = sDate.WeekDay;
+//     datetime->month = sDate.Month;
+//     datetime->date = sDate.Date;
+//     datetime->year = sDate.Year;
+// }
+//
+// void RTC_Sleep_Enter_Fun() {
+//     /* 调用独立的函数设置初始时间 */
+//     RTC_SetDateTime(&hrtc, 0x12, 0x00, 0x00,
+//                    RTC_WEEKDAY_MONDAY, RTC_MONTH_SEPTEMBER, 0x16, 0x25);
+//
+//     /* 获取当前时间用于设置闹钟 */
+//     RTC_TimeTypeDef currentTime = {0};
+//     HAL_RTC_GetTime(&hrtc, &currentTime, RTC_FORMAT_BCD);
+//
+//     /* 设置闹钟:50秒后触发 */
+//     RTC_AlarmTypeDef sAlarm = {0};
+//     sAlarm.AlarmTime.Hours = currentTime.Hours;
+//     sAlarm.AlarmTime.Minutes = currentTime.Minutes;
+//     sAlarm.AlarmTime.Seconds = (currentTime.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() 重新开始运行
+// }
 

+ 37 - 42
Core/Src/main.c

@@ -179,16 +179,16 @@ void powerOff() {
   HAL_GPIO_WritePin(POWER_ON_GPIO_Port, POWER_ON_Pin, GPIO_PIN_RESET);
 }
 
-uint32_t RTC_CalcDiffSeconds(RTC_DateTimeTypeDef *t1, RTC_DateTimeTypeDef *t2)
-{
-  uint32_t s1 = t1->hours * 3600 + t1->minutes * 60 + t1->seconds;
-  uint32_t s2 = t2->hours * 3600 + t2->minutes * 60 + t2->seconds;
-
-  if (s2 >= s1)
-    return s2 - s1;
-  else
-    return 24*3600 - (s1 - s2);  // 跨零点的情况
-}
+// uint32_t RTC_CalcDiffSeconds(RTC_DateTimeTypeDef *t1, RTC_DateTimeTypeDef *t2)
+// {
+//   uint32_t s1 = t1->hours * 3600 + t1->minutes * 60 + t1->seconds;
+//   uint32_t s2 = t2->hours * 3600 + t2->minutes * 60 + t2->seconds;
+//
+//   if (s2 >= s1)
+//     return s2 - s1;
+//   else
+//     return 24*3600 - (s1 - s2);  // 跨零点的情况
+// }
 
 void Flash_Write_LoraAddr(uint8_t addrH, uint8_t addrL)
 {
@@ -217,9 +217,6 @@ void Flash_Read_LoraAddr(uint8_t *addrH, uint8_t *addrL)
   *addrH = (halfword >> 8) & 0xFF;
   *addrL = halfword & 0xFF;
 }
-
-
-
 /* USER CODE END 0 */
 
 /**
@@ -273,14 +270,14 @@ int main(void)
   // }
   // 在需要设置RTC时间的地方直接调用
   // 参数依次为:小时、分钟、秒、星期、月份、日期、年份(均为BCD格式)
-  RTC_SetDateTime(&hrtc,
-                 0x12,  // 12时
-                 0x30,  // 30分
-                 0x00,  // 00秒
-                 RTC_WEEKDAY_WEDNESDAY,  // 星期三
-                 RTC_MONTH_OCTOBER,      // 10月
-                 0x17,  // 17日
-                 0x25); // 25年(2025年)
+  // RTC_SetDateTime(&hrtc,
+  //                0x12,  // 12时
+  //                0x30,  // 30分
+  //                0x00,  // 00秒
+  //                RTC_WEEKDAY_WEDNESDAY,  // 星期三
+  //                RTC_MONTH_OCTOBER,      // 10月
+  //                0x17,  // 17日
+  //                0x25); // 25年(2025年)
 
   // 启动 USART2 DMA 接收
   HAL_UART_Receive_DMA(&huart3, rx_buf, sizeof(rx_buf));
@@ -337,13 +334,11 @@ int main(void)
 
   /* Infinite loop */
   /* USER CODE BEGIN WHILE */
-  RTC_DateTimeTypeDef now;
-  RTC_GetDateTime(&hrtc, &now);
 
   printf("Device is ready\r\n");
   printf("设备地址:0x%02X%02X\n", deviceInfo.loraDeviceAddress_H, deviceInfo.loraDeviceAddress_L);
   uint8_t i = 0;
-  uint8_t ledShanshuo = 0;
+  // uint8_t ledShanshuo = 0;
   while (1)
   {
     /* USER CODE END WHILE */
@@ -419,19 +414,19 @@ int main(void)
     // }
 
     // 检查是否接收到"心跳"命令,且设备在线
-    if (!deviceInfo.isOnline){
-      printf("The device is not online.\r\n");
-      deviceInfo.commandFromCloud = COMBINE_CMD_STEP(IDLE, STEP_INIT);
-      if (!deviceInfo.loraSendSuccessFlag) {
-        E52_Heartbeat_Fun();
-      }
-      HAL_Delay(3000);
-    }
+    // if (!deviceInfo.isOnline){
+    //   printf("The device is not online.\r\n");
+    //   deviceInfo.commandFromCloud = COMBINE_CMD_STEP(IDLE, STEP_INIT);
+    //   if (!deviceInfo.loraSendSuccessFlag) {
+    //     E52_Heartbeat_Fun();
+    //   }
+    //   HAL_Delay(3000);
+    // }
 
     // 解析后台命令
-    if (GET_CMD( deviceInfo.commandFromCloud) == IDLE && GET_STEP(deviceInfo.commandFromCloud) == STEP_VERIFY) {
-      E52_Analyze_Data();
-    }
+    // if (GET_CMD( deviceInfo.commandFromCloud) == IDLE && GET_STEP(deviceInfo.commandFromCloud) == STEP_VERIFY) {
+    //   E52_Analyze_Data();
+    // }
 
     // 03:请求蓝牙数据
     if (GET_CMD( deviceInfo.commandFromCloud)  == REQUEST_BLUETOOTH_DATA && deviceInfo.isOnline) {
@@ -622,20 +617,20 @@ static void MX_RTC_Init(void)
 
   /** Initialize RTC and set the Time and Date
   */
-  sTime.Hours = 0x0;
-  sTime.Minutes = 0x0;
-  sTime.Seconds = 0x0;
+  sTime.Hours = 0;
+  sTime.Minutes = 0;
+  sTime.Seconds = 0;
 
-  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
+  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
   {
     Error_Handler();
   }
   DateToUpdate.WeekDay = RTC_WEEKDAY_MONDAY;
   DateToUpdate.Month = RTC_MONTH_JANUARY;
-  DateToUpdate.Date = 0x1;
-  DateToUpdate.Year = 0x0;
+  DateToUpdate.Date = 1;
+  DateToUpdate.Year = 0;
 
-  if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BCD) != HAL_OK)
+  if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BIN) != HAL_OK)
   {
     Error_Handler();
   }

+ 32 - 76
Core/Src/stm32f1xx_it.c

@@ -286,7 +286,6 @@ void USART2_IRQHandler(void)
   /* USER CODE BEGIN USART2_IRQn 0 */
   /* USER CODE END USART2_IRQn 0 */
   HAL_UART_IRQHandler(&huart2);
-
   /* USER CODE BEGIN USART2_IRQn 1 */
   if (__HAL_UART_GET_FLAG(&huart2, UART_FLAG_IDLE))
   {
@@ -413,91 +412,48 @@ void RTC_Alarm_IRQHandler(void)
 }
 
 /* USER CODE BEGIN 1 */
-void Print_RTC_DateTime(RTC_DateTimeTypeDef *t)
-{
-  printf("%02u-%02u-%02u %02u:%02u:%02u\r\n",
-         t->year + 2000,  // 如果 year 是两位数
-         t->month,
-         t->date,
-         t->hours,
-         t->minutes,
-         t->seconds);
-}
-/* 把 RTC_DateTimeTypeDef 转成当天秒数 */
-uint32_t RTC_ToSeconds(RTC_DateTimeTypeDef *t)
-{
-  return t->hours * 3600 + t->minutes * 60 + t->seconds;
-}
-
-/* 计算两个时间点的秒差,支持跨天(不跨月/跨年) */
-uint32_t RTC_CalcDuration(RTC_DateTimeTypeDef *start, RTC_DateTimeTypeDef *end)
-{
-  uint32_t startSec = RTC_ToSeconds(start);
-  uint32_t endSec   = RTC_ToSeconds(end);
-
-  if (start->year == end->year && start->month == end->month && start->date == end->date) {
-    // 同一天
-    return (endSec >= startSec) ? (endSec - startSec) : 0;
-  } else {
-    // 跨天
-    uint32_t daysDiff = end->date - start->date;
-    uint32_t duration = (24*3600 - startSec) + endSec;
-    if (daysDiff > 1) {
-      duration += (daysDiff - 1) * 86400;
-    }
-    return duration;
-  }
-}
-
-
-void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
-{
-  RTC_DateTimeTypeDef now;
-  RTC_GetDateTime(&hrtc, &now);  // 获取当前RTC时间到你自定义结构体
-
+void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
+  RTC_TimeTypeDef currentTime = {0};
+  HAL_RTC_GetTime(&hrtc, &currentTime, RTC_FORMAT_BIN);
   // ===== SOS_KEY =====
-  if (GPIO_Pin == SOS_KEY_Pin)
-  {
-    if (HAL_GPIO_ReadPin(SOS_KEY_GPIO_Port, SOS_KEY_Pin) == GPIO_PIN_SET)
-    {
-      // 松开
+  if (GPIO_Pin == SOS_KEY_Pin) {
+    if (HAL_GPIO_ReadPin(SOS_KEY_GPIO_Port, SOS_KEY_Pin) == GPIO_PIN_SET) {
+      // 松开(上升沿)
       printf("SOS_KEY 松开\r\n");
-      SOS_key_struct.key_Duration = RTC_CalcDuration(&SOS_key_struct.startTime, &now);
-      printf("SOS_KEY 按压了:%lu 秒\r\n", SOS_key_struct.key_Duration);
-      Print_RTC_DateTime(&now);
-      SOS_key_struct.key_CheckActive = 0;
-      SOS_key_struct.key_Count = 0;
-    }
-    else
-    {
-      // 按下
+
+      printf("Current time: %02d:%02d:%02d\n",
+             currentTime.Hours,
+             currentTime.Minutes,
+             currentTime.Seconds);
+    } else {
+      // 按下(下降沿)
       printf("SOS_KEY 按下\r\n");
-      SOS_key_struct.startTime = now;
-      Print_RTC_DateTime(&now);
-      SOS_key_struct.key_CheckActive = 1;
+      // SOS_key_struct = currentTime;
+      printf("Current time: %02d:%02d:%02d\n",
+             currentTime.Hours,
+             currentTime.Minutes,
+             currentTime.Seconds);
     }
   }
   // ===== JUGE_KEY =====
-  else if (GPIO_Pin == JUGE_PIN_Pin)
-  {
-    if (HAL_GPIO_ReadPin(JUGE_PIN_GPIO_Port, JUGE_PIN_Pin) == GPIO_PIN_SET)
-    {
-      // 松开
+  else if (GPIO_Pin == JUGE_PIN_Pin) {
+    if (HAL_GPIO_ReadPin(JUGE_PIN_GPIO_Port, JUGE_PIN_Pin) == GPIO_PIN_SET) {
+      // 松开(上升沿)
       printf("JUGE_KEY 松开\r\n");
-      JUGE_key_struct.key_Duration = RTC_CalcDuration(&JUGE_key_struct.startTime, &now);
-      printf("JUGE_KEY 按压了:%lu 秒\r\n", JUGE_key_struct.key_Duration);
-
-      JUGE_key_struct.key_CheckActive = 0;
-      JUGE_key_struct.key_Count = 0;
-    }
-    else
-    {
-      // 按下
+      printf("Current time: %02d:%02d:%02d\n",
+             currentTime.Hours,
+             currentTime.Minutes,
+             currentTime.Seconds);
+    } else {
+      // 按下(下降沿)
       printf("JUGE_KEY 按下\r\n");
-      JUGE_key_struct.startTime = now;
-      JUGE_key_struct.key_CheckActive = 1;
+      printf("Current time: %02d:%02d:%02d\n",
+             currentTime.Hours,
+             currentTime.Minutes,
+             currentTime.Seconds);
     }
   }
 }
 
+
 /* USER CODE END 1 */

+ 2 - 0
lora_bl.ioc

@@ -196,6 +196,8 @@ RCC.SYSCLKSource=RCC_SYSCLKSOURCE_PLLCLK
 RCC.TimSysFreq_Value=72000000
 RCC.USBFreq_Value=72000000
 RCC.VCOOutput2Freq_Value=8000000
+RTC.Format=RTC_FORMAT_BIN
+RTC.IPParameters=Format
 SH.GPXTI14.0=GPIO_EXTI14
 SH.GPXTI14.ConfNb=1
 SH.GPXTI15.0=GPIO_EXTI15