Quellcode durchsuchen

lora通讯异常,关闭设备。优化代码,修改bug

xuxinyi vor 1 Monat
Ursprung
Commit
8dbf324984
5 geänderte Dateien mit 451 neuen und 585 gelöschten Zeilen
  1. 1 5
      Core/Inc/main.h
  2. 0 12
      Core/Src/E52.c
  3. 0 46
      Core/Src/RTC_SLEEP.c
  4. 416 495
      Core/Src/main.c
  5. 34 27
      Core/Src/stm32f1xx_it.c

+ 1 - 5
Core/Inc/main.h

@@ -74,14 +74,10 @@ void Error_Handler(void);
 
 #define FLASH_USER_ADDR   0x0800FC00  // 用户存储页(最后 1KB)
 
-extern uint8_t rx_buf[25];
+extern uint8_t rx_buf_uart3[25];
 extern uint8_t rx_buf_uart2[25];
 extern uint8_t rx_buf_uart2_DMA[25];
-extern  uint8_t loraSendNextDataFlag;
-// extern uint8_t totalData[50][25];
 extern  RTC_HandleTypeDef hrtc;
-// extern uint8_t workMode;  // 0: 接收蓝牙数据 1: 使用lora发生数据。
-extern uint8_t receiveBlDataCount;
 extern UART_HandleTypeDef huart1;
 extern UART_HandleTypeDef huart2;
 extern UART_HandleTypeDef huart3;

+ 0 - 12
Core/Src/E52.c

@@ -15,18 +15,6 @@ void E52_CtlPowerOff_Fun() {
     HAL_GPIO_WritePin(POWER_ON_GPIO_Port, CTL_LORA_POWER_Pin, GPIO_PIN_SET);
 }
 
-
-void E52_AT_TYPE_Fun(uint8_t type) {
-    HAL_Delay(1000);
-    char lora_msg[20];
-    // 拼接字符串 AT+TYPE=x
-    sprintf(lora_msg, "AT+TYPE=%d", type);
-
-    HAL_UART_Transmit(&huart2, (uint8_t *)lora_msg, strlen(lora_msg), HAL_MAX_DELAY);
-    HAL_Delay(1000);
-}
-
-
 //AT COMMAND
 /**
  *配置lora发送方式

+ 0 - 46
Core/Src/RTC_SLEEP.c

@@ -29,52 +29,6 @@ void RTC_SetDateTime(RTC_HandleTypeDef *hrtc, uint8_t hours, uint8_t minutes, ui
     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() 重新开始运行
-// }
-
-
 /**
  * @brief  设置指定秒数后的闹钟
  * @param  delay_seconds 延迟秒数(最大支持 24*3600-1 秒,即一天内)

Datei-Diff unterdrückt, da er zu groß ist
+ 416 - 495
Core/Src/main.c


+ 34 - 27
Core/Src/stm32f1xx_it.c

@@ -277,7 +277,7 @@ void USART2_IRQHandler(void)
     if (rx_len > 0)
     {
       // ====== 协议帧校验 ======
-      if (rx_len >= 10 &&
+      if (rx_len == 12 &&
           rx_buf_uart2_DMA[0] == 0x55 &&
           rx_buf_uart2_DMA[1] == 0xBB &&
           rx_buf_uart2_DMA[3] == deviceInfo.loraDeviceAddress_H &&
@@ -341,7 +341,7 @@ void USART2_IRQHandler(void)
 void USART3_IRQHandler(void)
 {
   /* USER CODE BEGIN USART3_IRQn 0 */
-
+  // 可以在这里加断点调试或者统计中断次数
   /* USER CODE END USART3_IRQn 0 */
   HAL_UART_IRQHandler(&huart3);
   /* USER CODE BEGIN USART3_IRQn 1 */
@@ -350,33 +350,40 @@ void USART3_IRQHandler(void)
     __HAL_UART_CLEAR_IDLEFLAG(&huart3);
     HAL_UART_DMAStop(&huart3);
 
-    // uint32_t received_len = sizeof(rx_buf) - __HAL_DMA_GET_COUNTER(huart3.hdmarx);
-
-    if (strncmp((char *)rx_buf, "AT", 2) != 0) {
-      if (deviceInfo.BlDataFlag == BL_START) {
-        // 检查队列是否已满
-        if (RingBuffer_IsFull(&deviceInfo.queueBLData)) {
-          uint8_t temp[DATA_SIZE];
-          // 先出队一个数据腾出空间
-          if (RingBuffer_Dequeue(&deviceInfo.queueBLData, temp) == 0) {
-            // 可选:打印或处理被删除的数据
-            // printf("队列满,出队一个数据腾出空间\n");
+    uint16_t rx_len = UART3_DMA_BUF_LEN - __HAL_DMA_GET_COUNTER(huart3.hdmarx);
+
+    if (rx_len > 0 && rx_len <= UART3_DMA_BUF_LEN)
+    {
+      // 避免 strcmp 越界,先判断长度再判断开头
+      if (!(rx_len >= 2 && rx_buf_uart3[0] == 'A' && rx_buf_uart3[1] == 'T'))
+      {
+        if (deviceInfo.BlDataFlag == BL_START)
+        {
+          // 队列已满则先出队一个,保证能入队
+          if (RingBuffer_IsFull(&deviceInfo.queueBLData)) {
+            uint8_t drop[DATA_SIZE];
+            RingBuffer_Dequeue(&deviceInfo.queueBLData, drop);
           }
-        }
 
-        // 再入队新数据
-        if (rx_buf[6] == 0xf0 && RingBuffer_Enqueue(&deviceInfo.queueBLData, rx_buf) == 0) {
-          // 入队成功
-          // printf("入队成功\n");
-        } else {
-          // 理论上不会再失败,因为已经腾出一个位置
-          // printf("入队失败\n");
+          // 确保至少有7字节再判断 rx_buf[6]
+          if (rx_len >= 7 && rx_buf_uart3[6] == 0xF0)
+          {
+            // 临时缓存,避免 DMA 缓冲被清空后数据丢失
+            uint8_t temp_buf[UART3_DMA_BUF_LEN];
+            memcpy(temp_buf, rx_buf_uart3, rx_len);
+
+            // 这里建议 RingBuffer_Enqueue 支持传长度
+            if (RingBuffer_Enqueue(&deviceInfo.queueBLData, temp_buf) == 0) {
+              // 入队成功
+            }
+          }
         }
       }
     }
 
-    memset(rx_buf, 0, UART3_DMA_BUF_LEN);
-    HAL_UART_Receive_DMA(&huart3, rx_buf, UART3_DMA_BUF_LEN); // 重新启动
+    // 清空 DMA 缓冲 & 重新启动 DMA 接收
+    memset(rx_buf_uart3, 0, UART3_DMA_BUF_LEN);
+    HAL_UART_Receive_DMA(&huart3, rx_buf_uart3, UART3_DMA_BUF_LEN);
   }
   /* USER CODE END USART3_IRQn 1 */
 }
@@ -419,11 +426,11 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
   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");
+      // printf("SOS_KEY 松开\r\n");
       // deviceInfo.isSOS = FALSE;
     } else {
       // 按下(下降沿)
-      printf("SOS_KEY 按下\r\n");
+      // printf("SOS_KEY 按下\r\n");
       SOS_struct = currentTime;  // 记录上报时间
       // deviceInfo.isSOS = TRUE;
     }
@@ -432,10 +439,10 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
   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");
+      // printf("JUGE_KEY 松开\r\n");
     } else {
       // 按下(下降沿)
-      printf("JUGE_KEY 按下\r\n");
+      // printf("JUGE_KEY 按下\r\n");
       HAL_GPIO_WritePin(POWER_ON_GPIO_Port, POWER_ON_Pin, GPIO_PIN_RESET);
     }
   }

Einige Dateien werden nicht angezeigt, da zu viele Dateien in diesem Diff geändert wurden.