Explorar o código

timeout na UART

Juraj Ďuďák hai 1 ano
pai
achega
d9465e6c0d
Modificáronse 3 ficheiros con 38 adicións e 15 borrados
  1. 23 7
      Core/Src/main.c
  2. 8 4
      test/app.py
  3. 7 4
      test/comm.py

+ 23 - 7
Core/Src/main.c

@@ -91,6 +91,7 @@ volatile uint8_t dataI;
 volatile uint32_t uart_timeout;
 
 volatile uint8_t icm_data_ready = 0;
+//volatile uint8_t uart_data_ready = 0;
 
 inline void uart_send(uint8_t *data, int n)
 {
@@ -129,10 +130,16 @@ static inline uint8_t loop_callback() {
 		return 1;  // interrupt from external sensor: data ready
 	}
 
-	if(uart_timeout + 2 > HAL_GetTick()) {
+	uint32_t ht = HAL_GetTick();
+	//TODO toto opravit, ten timeout hned skoci do obsluhy
+	if(uart_timeout + 2 > ht) {
 		dataL = 0;
 		dataI = 0;
 		data[0] = 0;
+		uart_timeout = 0;
+//		HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
+		led_off();
+		HAL_UARTEx_ReceiveToIdle_DMA(&huart2, data, 64);
 		return 2;  // UART receive timeout
 	}
 
@@ -141,30 +148,37 @@ static inline uint8_t loop_callback() {
 
 //  Application callbacks
 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){
+	if (dataL == 0) {
 		uart_timeout = HAL_GetTick();
 		dataL = data[0];
 		dataI = 0;
-		HAL_GPIO_TogglePin(LD3_GPIO_Port, LD3_Pin);
-		if(Size == 1){
+//		HAL_GPIO_TogglePin(LD3_GPIO_Port, LD3_Pin);
+		led_on();
+		if (Size == 1) {
 			return;
 		}
-		dataI = 1;
+
+		//dataI = 1;
 		Size--;
 		copy_offset = 1;
 	}
 
 	memcpy(&dataUART[dataI], &data[copy_offset], Size);
+
 	dataI += Size;
 	if(dataI >= dataL){
-		HAL_GPIO_TogglePin(LD3_GPIO_Port, LD3_Pin);
+//		HAL_GPIO_TogglePin(LD3_GPIO_Port, LD3_Pin);
+		led_off();
 		nbus_cb_UART_RX(dataI);
 		dataL=0;
 		dataI=0;
 		data[0]=0;
+		uart_timeout = 0;
 	}
+
 }
 
 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
@@ -211,6 +225,8 @@ int main(void)
   dataI=0;
   dataL=0;
 
+  HAL_UART_RegisterRxEventCallback(&huart2, HAL_UARTEx_RxEventCallback);
+
 #ifdef MODULE_INIT_IP_ADC
   MX_ADC_Init();
 #endif
@@ -265,7 +281,7 @@ int main(void)
 
 	 nbus_init(getImuDriver(), &hw_platform);
 	 nbus_init_app(&hspi1, &config);
-	 //nbus_set_app_callback(loop_callback);
+
 #endif
 
   nBus_MemoryDriver memory_ec20 = {

+ 8 - 4
test/app.py

@@ -18,11 +18,15 @@ class AppTest:
         print("Version: "+version)
 
     def cmd_echo(self, msg):        
-        resp = self.serial_port.request(self.module, 0, CMD_ECHO,msg)
+        resp = self.serial_port.request(self.module, 0, CMD_ECHO,msg)        
         echo = ""
+        if len(resp) == 0:
+            print("No ECHO")
+            return 0
         for r in range(len(msg)):
             echo = echo + chr(resp[3+r])
         print("Echo:"+echo)
+        return len(resp)
 
     def cmd_set_param(self, sensor, param, value):                
         print("SET param:", PARAM_NAME[param], "[", param, "]=>", value)
@@ -137,9 +141,9 @@ if __name__ == "__main__":
     #app.cmd_version()
     #app.cmd_version()
     # app.cmd_module_stop()
-    app.cmd_echo([65])    
-
-    app.cmd_echo([97,98,99,100])    
+    
+    if app.cmd_echo([97,98,99,100]) == 0:
+        sys.exit()
 
     app.cmd_set_param(1, PARAM_SAMPLERATE, 10)
     app.cmd_set_param(1, PARAM_RANGE, 1)

+ 7 - 4
test/comm.py

@@ -130,7 +130,7 @@ class SerialComm:
             self.open()
         self.port.flush()
 
-        start[0] = len(start)
+        start[0] = len(start)+5
         crc_sum = crc8(start[1:])
         
         start.append(crc_sum)
@@ -177,15 +177,18 @@ class SerialComm:
         has_response = False
         counter = 0
         while has_response == False:
-            self.port.write([m[0]]) # send legth of message
-            # usleep(5)
-            self.port.write(m[1:]) # send body of message
+            # self.port.write([m[0]]) # send legth of message
+            # self.port.write(m[1:]) # send body of message
+
+            self.port.write(m) # send body of message
+
             data = self.receive_response()
             if len(data)>0:
                 has_response = True
             counter = counter + 1
             if counter > 5:
                 return []
+            
         if data[2] & 0x80 != 0:
             print("Chyba", hex(data[3]))
             return [data[3]]