Bläddra i källkod

整体框架已确定。完成蓝牙扫描和发送。

xuxinyi 2 månader sedan
förälder
incheckning
d8800f8ad4
4 ändrade filer med 111 tillägg och 45 borttagningar
  1. 41 0
      Core/Inc/E52.h
  2. 11 13
      Core/Src/E52.c
  3. 52 31
      Core/Src/main.c
  4. 7 1
      Core/Src/stm32f1xx_it.c

+ 41 - 0
Core/Inc/E52.h

@@ -10,6 +10,15 @@
 #include "string.h"
 #include "stdlib.h"
 
+// 组合命令和步骤的宏
+#define COMBINE_CMD_STEP(cmd, step) ((cmd) | (step))
+
+// 从组合值中提取命令
+#define GET_CMD(value) (value & 0x0F)
+
+// 从组合值中提取步骤
+#define GET_STEP(value) ((value & 0xF0))
+
 // 定义云端命令枚举,使命令含义更清晰
 typedef enum {
     IDLE = 0x00,
@@ -19,6 +28,38 @@ typedef enum {
     E52_HEARTBEAT = 0x08            // E52 心跳包命令
 } CloudCommand;
 
+
+
+// 定义步骤枚举,高4位用于存储步骤(0x00~0x50,间隔0x10) 通用
+typedef enum {
+    STEP_INIT = 0x00 << 4,  // 0x00 - 原始命令
+    STEP_VERIFY = 0x01 << 4, // 0x10 - 中断已处理(while中防止一个命令被多次分析数据,导致内存崩溃)(有新数据过来)
+    STEP_COMPLETE = 0x0F << 4 // 0x40 - 完成步骤
+} CommandStep;
+
+//独立step范围(0x02~0x0E
+typedef enum {
+    REQUEST_BLUETOOTH_DATA_SCAN = 0x02 << 4,  // 请求蓝牙数据第1步
+    REQUEST_BLUETOOTH_DATA_SEND = 0x03 << 4   // 发送蓝牙数据第2步
+} REQUEST_BLUETOOTH_DATA_STEP;
+
+typedef enum {
+    CONFIGURE_E52_STEP1 = 0x01 << 4,  //还没想好
+    CONFIGURE_E52_STEP2 = 0x02 << 4   // 还没想好
+} CONFIGURE_E52_STEP;
+
+
+
+
+// // 发送 "请求蓝牙数据" 命令的第2步
+// uint8_t cmd = COMBINE_CMD_STEP(REQUEST_BLUETOOTH_DATA, STEP_2);
+// // cmd 的值为 0x23 (0x03 | 0x20)
+//
+// // 解析接收到的命令
+// uint8_t received_cmd = 0x23;
+// CloudCommand command = GET_CMD(received_cmd);       // 得到 0x03 (REQUEST_BLUETOOTH_DATA)
+// uint8_t step = GET_STEP(received_cmd);              // 得到 2
+
 typedef enum {
     TIMER_IDLE,
     TIMER_ENABLE,

+ 11 - 13
Core/Src/E52.c

@@ -80,10 +80,9 @@ void E52_Request_Command_Fun() {
     if ((((uint16_t)rx_buf_uart2[5] << 8) | rx_buf_uart2[6]) != 0) {
         // 组合两个字节为16位的持续时间值
         deviceInfo.forwardBLAndLoraDataDuration = ((uint16_t)rx_buf_uart2[5] << 8) | rx_buf_uart2[6];
-        deviceInfo.commandFromCloud = REQUEST_BLUETOOTH_DATA;
     } else {
-        printf("扫描时间为0,不执行扫描\r\n");
-        deviceInfo.commandFromCloud = IDLE;
+        printf("扫描时间为0,默认500ms扫描时间\r\n");
+        deviceInfo.forwardBLAndLoraDataDuration = 500;
     }
     // TODO: command:03 - 此处添加0x03命令的其他处理逻辑
 }
@@ -105,35 +104,34 @@ void E52_Heartbeat_Fun1() {
 }
 
 void E52_Analyze_Data() {
-    // uint16_t received_len = sizeof(rx_buf_uart2) - __HAL_DMA_GET_COUNTER(huart2.hdmarx);
 
-    if (rx_buf_uart2[0] == 0x55 && rx_buf_uart2[1] == 0xBB && deviceInfo.commandFromCloud == IDLE) {
+    if (rx_buf_uart2[0] == 0x55 && rx_buf_uart2[1] == 0xBB) {
         // 根据接收到的第三个字节(命令字节)进行分支处理
         switch (rx_buf_uart2[2]) {
             case REQUEST_BLUETOOTH_DATA:
+                deviceInfo.commandFromCloud = COMBINE_CMD_STEP(REQUEST_BLUETOOTH_DATA, REQUEST_BLUETOOTH_DATA_SCAN);
                 E52_Request_Command_Fun();
                 break;
             case CONFIGURE_E52:
+                // deviceInfo.commandFromCloud = COMBINE_CMD_STEP(CONFIGURE_E52, STEP_EXECUTE);
                 E52_Configuration_Fun();
                 break;
             case E52_ENTER_SLEEP:
+                // deviceInfo.commandFromCloud = COMBINE_CMD_STEP(E52_ENTER_SLEEP, STEP_EXECUTE);
                 E52_Send_Sleep_Fun();
                 break;
             case E52_HEARTBEAT:
+                // deviceInfo.commandFromCloud = COMBINE_CMD_STEP(E52_HEARTBEAT, STEP_EXECUTE);
                 E52_Heartbeat_Fun1();
                 break;
             default:
-                // 处理未知命令的情况,比如打印日志或进行错误处理
-                // printf("Unknown command: 0x%02X\r\n", rx_buf_uart2[2]);
+                deviceInfo.commandFromCloud = COMBINE_CMD_STEP(IDLE, STEP_INIT);
                 break;
         }
+    }else {
+        //有效数据。
+        deviceInfo.commandFromCloud = COMBINE_CMD_STEP(IDLE, STEP_INIT);
     }
-    // memset(rx_buf_uart2, 0, sizeof(rx_buf_uart2));
-    // // 清空缓冲区(可选)
-    // memset(rx_buf_uart2, 0, sizeof(rx_buf_uart2));
-    // // 重新开启 DMA 接收
-    // HAL_UART_Receive_DMA(&huart2, rx_buf_uart2, sizeof(rx_buf_uart2));
-    // deviceInfo.newLoraDataFlag = 0;//重置状态
 }
 
 

+ 52 - 31
Core/Src/main.c

@@ -138,8 +138,8 @@ void Device_Info_Init_Fun() {
   deviceInfo.BlDataFlag = BL_IDLE;
   deviceInfo.newLoraDataFlag = 0;
   deviceInfo.isReady = 0;
-  deviceInfo.commandFromCloud = IDLE;
   deviceInfo.loraSendSuccessFlag = 0;
+  deviceInfo.commandFromCloud = COMBINE_CMD_STEP(IDLE, STEP_INIT);  //初始化
   //TODO: 大循环,获取设备地址信息,否则重新发送。发送上线信息,等待反馈,否则重新发送上线信息。一切准备就绪后,开启isready为1,进入工作模式。
   // while (1){}
 }
@@ -287,42 +287,63 @@ int main(void)
     /* USER CODE BEGIN 3 */
     //检测是否有新的lora 数据判断
     //TODO 应该防止频繁调用此函数。如果command 是IDLE,则调用此函数。
-    if (deviceInfo.newLoraDataFlag == 1) {
-      // printf("loraDeviceAddress: 0x%02X 0x%02X\r\n", deviceInfo.loraDeviceAddress_H,deviceInfo.loraDeviceAddress_L);
+    // if (deviceInfo.newLoraDataFlag == 1) {
+    //   // printf("loraDeviceAddress: 0x%02X 0x%02X\r\n", deviceInfo.loraDeviceAddress_H,deviceInfo.loraDeviceAddress_L);
+    //
+    // }
+    // 命令为IDLE,且步骤为STEP_INIT 说明有新数据。
+    if (GET_CMD( deviceInfo.commandFromCloud) == IDLE && GET_STEP(deviceInfo.commandFromCloud) == STEP_VERIFY) {
       E52_Analyze_Data();
     }
 
     // 检查是否接收到"请求蓝牙数据"命令,且定时器处于就绪状态
-    if (deviceInfo.commandFromCloud == REQUEST_BLUETOOTH_DATA) {
-
-      if (deviceInfo.timeCount >= deviceInfo.forwardBLAndLoraDataDuration) {
-        Timer_Managment_Fun(&htim4, TIMER_OP_STOP);
-        deviceInfo.BlDataFlag = BL_STOP;
-        while (receiveBlDataCount >= i) {
-
-          printf("发送第%d个蓝牙数据给Lora\r\n", i);
-          if (deviceInfo.loraSendSuccessFlag == 0) {
-            E52_Send_Bl_Data_Fun(i);
-          }else {
-            continue;
+    if (GET_CMD( deviceInfo.commandFromCloud)  == REQUEST_BLUETOOTH_DATA) {
+
+      switch (GET_STEP(deviceInfo.commandFromCloud)) {
+        case REQUEST_BLUETOOTH_DATA_SCAN:
+          Timer_Managment_Fun(&htim4, TIMER_OP_START);
+          deviceInfo.BlDataFlag = BL_START;
+          printf("开启蓝牙扫描\r\n");
+          break;    //扫描蓝牙阶段
+
+        case REQUEST_BLUETOOTH_DATA_SEND:
+          Timer_Managment_Fun(&htim4, TIMER_OP_STOP);
+          deviceInfo.BlDataFlag = BL_STOP;
+          i = 0;
+          while (receiveBlDataCount >= i) {
+
+            printf("发送第%d个蓝牙数据给Lora\r\n", i);
+            if (deviceInfo.loraSendSuccessFlag == 0) {
+              E52_Send_Bl_Data_Fun(i);
+            }else {
+              continue;
+            }
+            memset(totalData[i], 0, sizeof(totalData[i]));
+            i++;
           }
-          memset(totalData[i], 0, sizeof(totalData[i]));
-          i++;
-        }
-        receiveBlDataCount = 0;
-        deviceInfo.timeCount = 0;
-        deviceInfo.forwardBLAndLoraDataDuration = 0;
-        i = 0;
-
-        deviceInfo.commandFromCloud = IDLE;
-        // 清空缓冲区(可选)
-        //TODO 停止计时器后,将数据发送出去。
-        printf("停止蓝牙扫描,开始发送蓝牙数据给Lora\r\n");
-      }else {
-        Timer_Managment_Fun(&htim4, TIMER_OP_START);
-        deviceInfo.BlDataFlag = BL_START;
-        printf("开启蓝牙扫描\r\n");
+          deviceInfo.commandFromCloud = COMBINE_CMD_STEP(REQUEST_BLUETOOTH_DATA, STEP_COMPLETE);
+          printf("停止蓝牙扫描,开始发送蓝牙数据给Lora\r\n");
+          break;          //发送数据阶段
+        case STEP_COMPLETE:
+          receiveBlDataCount = 0;
+          deviceInfo.timeCount = 0;
+          deviceInfo.forwardBLAndLoraDataDuration = 0;
+          i = 0;
+          deviceInfo.commandFromCloud = COMBINE_CMD_STEP(IDLE, STEP_INIT);
+          printf("发送结束\r\n");
+          break;
+
+        default:
+          // 处理未知步骤
+          printf("收到未知步骤,忽略处理\r\n");
+          break;
       }
+
+      // if (deviceInfo.timeCount >= deviceInfo.forwardBLAndLoraDataDuration) {
+      //
+      // }else {
+      //
+      // }
     }
   }
   /* USER CODE END 3 */

+ 7 - 1
Core/Src/stm32f1xx_it.c

@@ -263,6 +263,11 @@ void TIM4_IRQHandler(void)
   HAL_TIM_IRQHandler(&htim4);
   // printf("定时器测试\r\n");
   deviceInfo.timeCount++;
+  if (GET_CMD(deviceInfo.commandFromCloud) == REQUEST_BLUETOOTH_DATA) {
+    if (deviceInfo.timeCount >= deviceInfo.forwardBLAndLoraDataDuration) {
+      deviceInfo.commandFromCloud = COMBINE_CMD_STEP(REQUEST_BLUETOOTH_DATA, REQUEST_BLUETOOTH_DATA_SEND);
+    }
+  }
   /* USER CODE BEGIN TIM4_IRQn 1 */
 
   /* USER CODE END TIM4_IRQn 1 */
@@ -310,7 +315,8 @@ void USART2_IRQHandler(void)
       deviceInfo.loraSendSuccessFlag = 0;
       memset(rx_buf_uart2, 0, sizeof(rx_buf_uart2));
     }else if (rx_buf_uart2[3] == deviceInfo.loraDeviceAddress_H && rx_buf_uart2[4] == deviceInfo.loraDeviceAddress_L){
-      deviceInfo.newLoraDataFlag = 1;
+      // deviceInfo.newLoraDataFlag = 1;
+      deviceInfo.commandFromCloud = COMBINE_CMD_STEP(IDLE, STEP_VERIFY);  //初始化  命令和步骤。说明有新数据过来
     }else {
       memset(rx_buf_uart2, 0, sizeof(rx_buf_uart2));
     }