Przeglądaj źródła

完成RTC定时唤醒命令

xuxinyi 1 miesiąc temu
rodzic
commit
1b066c81b6
4 zmienionych plików z 79 dodań i 46 usunięć
  1. 7 4
      Core/Inc/E52.h
  2. 34 4
      Core/Src/E52.c
  3. 22 21
      Core/Src/main.c
  4. 16 17
      README.md

+ 7 - 4
Core/Inc/E52.h

@@ -61,6 +61,9 @@ typedef enum {
     HEARTBEAT_SEND = 0x02 << 4,  //发送心跳包
 } HEARTBEAT_E52_STEP;
 
+typedef enum {
+    RTC_SLEEP_STEP = 0x02 << 4,  //睡眠模式
+} SLEEP_STEP;
 
 
 
@@ -118,9 +121,8 @@ typedef struct {
     //lora发生成功标志
     // uint8_t loraSendSuccessFlag;
 
-    //定时器相关
-    // uint8_t  timerEnable;           //定时器使能
-    // uint16_t timeCount;             //定时器计数
+    //rtc定时唤醒时间
+    uint32_t rtc_wake_up_time;
 
     uint16_t forwardBLAndLoraDataDuration;   //发送蓝牙间隔时间
     uint8_t forwardBLAndLoraDataCount;   //发送蓝牙数量
@@ -128,7 +130,6 @@ typedef struct {
 
     RingBuffer queueBLData;                 //蓝牙数据队列
     RingBuffer queueLoraCommandData;        //命令数据队列
-    // RingBuffer queueSenderData;
 
 } Device_structure;
 
@@ -201,4 +202,6 @@ void E52_CtlPowerOn_Fun();
 void E52_CtlPowerOff_Fun();
 
 void schedule_Fun(void);
+
+void Enter_Sleep_ACK_Fun(void);
 #endif //E52_H

+ 34 - 4
Core/Src/E52.c

@@ -173,7 +173,19 @@ void E52_Configuration_Fun() {
 
 void E52_Send_Sleep_Fun() {
     printf("这是让设备睡眠的命令\r\n");
-    // RTC_Sleep_Enter_Fun();
+
+    // 解析睡眠时间 24小时秒数
+    uint32_t sleep_seconds = ((uint32_t)rx_buf_uart2[5] << 16) |
+                             ((uint32_t)rx_buf_uart2[6] << 8)  |
+                             ((uint32_t)rx_buf_uart2[7]);
+
+    if (sleep_seconds == 0 || sleep_seconds > 86399) {
+        printf("睡眠时间无效, 使用默认3000ms\r\n");
+        deviceInfo.rtc_wake_up_time = 3000;
+    } else {
+        deviceInfo.rtc_wake_up_time = sleep_seconds;
+        printf("接收到睡眠时间: %lu 秒\r\n", sleep_seconds);
+    }
     // TODO: command:05
 }
 
@@ -195,6 +207,25 @@ void E52_Heartbeat_Fun1() {
     // TODO: command:08
 }
 
+void Enter_Sleep_ACK_Fun() {
+    printf("这是心跳包\r\n");
+    Heartbeat_structure heartbeat_structure;
+
+    heartbeat_structure.frame_header_H = 0x55;
+    heartbeat_structure.frame_header_L = 0xAA;
+    heartbeat_structure.function_code = 0x05;
+    heartbeat_structure.badge_addr_high = deviceInfo.loraDeviceAddress_H;
+    heartbeat_structure.badge_addr_low = deviceInfo.loraDeviceAddress_L;
+    heartbeat_structure.broadcast_type = deviceInfo.broadcast_type;
+    heartbeat_structure.target_addr_H = deviceInfo.target_addr_H;
+    heartbeat_structure.target_addr_L = deviceInfo.target_addr_L;
+    heartbeat_structure.frame_tail_H = 0xFF;
+    heartbeat_structure.frame_tail_L = 0xEE;
+
+    HAL_UART_Transmit(&huart2, (uint8_t *)&heartbeat_structure, sizeof(heartbeat_structure), HAL_MAX_DELAY);
+    // TODO: command:01
+}
+
 void E52_Analyze_Data() {
     // 根据接收到的第三个字节(命令字节)进行分支处理
     switch (rx_buf_uart2[2]) {
@@ -207,9 +238,8 @@ void E52_Analyze_Data() {
             deviceInfo.commandFromCloud = COMBINE_CMD_STEP(CONFIGURE_E52, CONFIGURE_E52_SET_BROADCAST_TYPE);
             break;
         case E52_ENTER_SLEEP:
-            // deviceInfo.commandFromCloud = COMBINE_CMD_STEP(E52_ENTER_SLEEP, STEP_EXECUTE);
-            // E52_Send_Sleep_Fun();
-            deviceInfo.commandFromCloud = COMBINE_CMD_STEP(IDLE, STEP_INIT);
+            E52_Send_Sleep_Fun();
+            deviceInfo.commandFromCloud = COMBINE_CMD_STEP(E52_ENTER_SLEEP, RTC_SLEEP_STEP);
             break;
         case E52_HEARTBEAT:// 完成
             E52_Heartbeat_Fun1();

+ 22 - 21
Core/Src/main.c

@@ -284,28 +284,23 @@ int main(void)
   __HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
   Flash_Read_LoraAddr(&deviceInfo.loraDeviceAddress_H, &deviceInfo.loraDeviceAddress_L);
 
-  printf("开始\r\n");
-  Set_Alarm_After(80);   // 50 秒后触发
+  // printf("开始\r\n");
+  // Set_Alarm_After(80);   // 50 秒后触发
+  //
+  // while (TRUE) {
+  //
+  //
+  //   RTC_TimeTypeDef now;
+  //
+  //   // 读当前时间
+  //   HAL_RTC_GetTime(&hrtc, &now, RTC_FORMAT_BIN);
+  //
+  //   printf("Now      : %02d:%02d:%02d\r\n", now.Hours, now.Minutes, now.Seconds);
+  //   // printf("NextAlarm: %02d:%02d:%02d\r\n", hr, min, sec);
+  //
+  //   // HAL_Delay(1000);
+  // }
 
-  while (TRUE) {
-
-
-    RTC_TimeTypeDef now;
-
-    // 读当前时间
-    HAL_RTC_GetTime(&hrtc, &now, RTC_FORMAT_BIN);
-
-    printf("Now      : %02d:%02d:%02d\r\n", now.Hours, now.Minutes, now.Seconds);
-    // printf("NextAlarm: %02d:%02d:%02d\r\n", hr, min, sec);
-
-    // HAL_Delay(1000);
-  }
-
-
-
-  while (TRUE) {
-    printf("123\r\n");
-  }
   if (deviceInfo.loraDeviceAddress_H == 0xFF || deviceInfo.loraDeviceAddress_L == 0xFF || deviceInfo.loraDeviceAddress_H == 0x00 || deviceInfo.loraDeviceAddress_L == 0x00) { // 未设置Lora地址,应该是初次启动,配置Lora地址蓝牙等
     HAL_Delay(4000);
     printf("开始初始化\r\n");
@@ -485,6 +480,12 @@ int main(void)
       }
     }
 
+    if (GET_CMD( deviceInfo.commandFromCloud)  == E52_ENTER_SLEEP && deviceInfo.isOnline) {
+      Enter_Sleep_ACK_Fun();
+      HAL_Delay(4000);
+      Set_Alarm_After(deviceInfo.rtc_wake_up_time);
+    }
+
   }
   /* USER CODE END 3 */
 }

+ 16 - 17
README.md

@@ -1,20 +1,3 @@
-1、后台命令。 通用部分  帧头:55 BB  命令:08 设备地址:E1 16   测试设备地址:55 BB 03 FC A5 01 C8
-2、设备上报。 通用部分  帧头:55 AA  命令:02 设备地址:E1 16
-
-命令:
-    后台:03 请求蓝牙  07 配置lora  05 设备睡眠 04 上报点卯信息  09 ACK服务端响应
-    设备:01 主动上报点卯  02 上报蓝牙数据  04 SOS信息
-
-
-1.1、启动设备:55 BB 08 E1 16  注释:后台主动问询设备的在线状态
-返回:无
-
-
-1.2、请求蓝牙:55 BB 03 E1 16 00 C8  注释:请求蓝牙数据。搜索蓝牙时间 00 C8 。两个16进制数表示毫秒。
-返回:55 AA 02 E1 16 25 AD 0B 1D 60 C8 F0 A9 01 FF EE   注释:
-
-
-
 目前测试后台地址是:3460
 
 1.停止上线通知
@@ -70,4 +53,20 @@
     }
 
 5.中继休眠
+55 BB 05 BA 8F 00 00 00 34 60 FF EE  ->  55 AA 05 AC 95 03 34 60 FF EE(返回值:确认睡眠)
+
+    {
+        55 BB       : 帧头
+        05          : 命令(睡眠)
+        BA 8F       : 设备地址
+        00 00 00    : 睡眠时间 24小时(1 ~ 86399)秒
+        34 60       : 目标地址
+        FF EE       : 帧尾
+    }
+
+
+
+----------------------------------------------------------------------------------------------------------
+向后台发送的命令
+