浏览代码

UART timeout detection with 2ms systick

Juraj Ďuďák 1 年之前
父节点
当前提交
145b86a27a
共有 3 个文件被更改,包括 13 次插入3 次删除
  1. 11 1
      Core/Src/main.c
  2. 1 1
      Modules/icm20948
  3. 1 1
      Modules/nbus

+ 11 - 1
Core/Src/main.c

@@ -88,6 +88,7 @@ uint8_t data[64];	// tmp read buffer
 uint8_t *dataUART;  // pointee for rx_buffer
 volatile uint8_t dataL;
 volatile uint8_t dataI;
+volatile uint32_t uart_timeout;
 
 volatile uint8_t icm_data_ready = 0;
 
@@ -125,8 +126,16 @@ inline void app_delay(uint8_t ms){
 static inline uint8_t loop_callback() {
 	if(icm_data_ready == 1){
 		icm_data_ready = 0;
-		return 1;
+		return 1;  // interrupt from external sensor: data ready
 	}
+
+	if(uart_timeout + 2 > HAL_GetTick()) {
+		dataL = 0;
+		dataI = 0;
+		data[0] = 0;
+		return 2;  // UART receive timeout
+	}
+
 	return 0;
 }
 
@@ -135,6 +144,7 @@ void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
 	HAL_UARTEx_ReceiveToIdle_DMA(huart, data, 64);
 	uint8_t copy_offset = 0;
 	if(dataL==0){
+		uart_timeout = HAL_GetTick();
 		dataL = data[0];
 		dataI = 0;
 		HAL_GPIO_TogglePin(LD3_GPIO_Port, LD3_Pin);

+ 1 - 1
Modules/icm20948

@@ -1 +1 @@
-Subproject commit afabaea4a749ab11a4d11d0c4fde1ea4df257af5
+Subproject commit 9fa1b82af75c7e11acfae01da92b422799f131a4

+ 1 - 1
Modules/nbus

@@ -1 +1 @@
-Subproject commit 170cf868c9780fe27d56095c607042d38ec40b22
+Subproject commit 2902994f3f8000b42c6cb25da0fd142f8c44287a