Browse Source

iic解码优化

lijinwen 1 month ago
parent
commit
00f30b8a3f

+ 3 - 3
ProtocolIIC/constants.h

@@ -1,9 +1,9 @@
 // // ******************************************************************
-// //       /\ /|       @File         Constants.h
+// //       /\ /|       @File         constants.h
 // //       \ V/        @Brief
 // //       | "")       @Author        lijinwen, ghz005@uni-trend.com.cn
-// //       /  |        @Creation      2024-05-17
-// //      /  \\        @Modified      2024-06-24
+// //       /  |        @Creation      2024-07-19
+// //      /  \\        @Modified      2024-07-25
 // //    *(__\_\
 // // ******************************************************************
 

+ 3 - 3
ProtocolIIC/iic_decode_options.h

@@ -1,9 +1,9 @@
 // // ******************************************************************
-// //       /\ /|       @File         iicDecodeOptions.h
+// //       /\ /|       @File         iic_decode_options.h
 // //       \ V/        @Brief
 // //       | "")       @Author        lijinwen, ghz005@uni-trend.com.cn
-// //       /  |        @Creation      2024-07-16
-// //      /  \\        @Modified      2024-07-16
+// //       /  |        @Creation      2024-07-19
+// //      /  \\        @Modified      2024-07-25
 // //    *(__\_\
 // // ******************************************************************
 

+ 4 - 3
ProtocolIIC/iic_decode_result.h

@@ -2,8 +2,8 @@
 // //       /\ /|       @File         iic_decode_result.h
 // //       \ V/        @Brief
 // //       | "")       @Author        lijinwen, ghz005@uni-trend.com.cn
-// //       /  |        @Creation      2024-05-17
-// //      /  \\        @Modified      2024-06-24
+// //       /  |        @Creation      2024-07-19
+// //      /  \\        @Modified      2024-07-25
 // //    *(__\_\
 // // ******************************************************************
 
@@ -31,7 +31,8 @@ namespace Protocol
     {
         bool decode_event_need_update = false;
         uint64_t decode_event_count; //事件数量
-        IicEvent* decode_events_ptr; //解码结果事件指针
+        IicEvent* decode_events_ptr; //解码结事件指针
+        IicDataInfo* decode_data_cell_ptr; //解码结果数据指针
         intptr_t decoder_ptr;
         IicDecodeResult() : decode_event_count(0),
                             decode_events_ptr(nullptr),

+ 117 - 44
ProtocolIIC/iic_decoder.cc

@@ -1,4 +1,13 @@
-#include "iic_decoder.h"
+// // ******************************************************************
+// //       /\ /|       @File         iic_decoder.cc
+// //       \ V/        @Brief
+// //       | "")       @Author        lijinwen, ghz005@uni-trend.com.cn
+// //       /  |        @Creation      2024-07-19
+// //      /  \\        @Modified      2024-07-25
+// //    *(__\_\
+// // ******************************************************************
+
+#include "iic_decoder.h"
 
 #include "constants.h"
 
@@ -21,6 +30,13 @@ namespace Protocol
 		TwoLevelEdgePulse* edge_pulses_data_ptr = edge_pulses_data.GetDataAddrPtr();
 		TwoLevelEdgePulse* edge_pulses_clk_ptr = edge_pulses_clk.GetDataAddrPtr();
 
+		/*auto t1 = offsetof(IicDataInfo, data);
+		auto t2 = offsetof(IicDataInfo, ack);
+		auto t3 = offsetof(IicDataInfo, ack_index);
+		auto t4 = offsetof(IicDataInfo, ack_length);
+		auto t5 = offsetof(IicDataInfo, success_ack);
+		auto t6 = offsetof(IicDataInfo, has_ack);*/
+
 		while (true)
 		{
 			// 检查是否取消
@@ -29,6 +45,10 @@ namespace Protocol
 				WriteLog(LogLevel::Level2, "AsyncFunction canceled");
 				return false;
 			}
+			while (start_index > 0 && edge_pulses_clk_ptr->start_index > start_index)
+			{
+				edge_pulses_clk_ptr--;
+			}
 			// find start index
 			if (!FindStartNode(start_index, edge_pulses_data_ptr, edge_pulses_clk_ptr))
 			{
@@ -168,10 +188,13 @@ namespace Protocol
 			//开始解析数据
 			while (true)
 			{
-				int stop_index = 0;
-				if (FindStopNode(start_index, edge_pulses_data_ptr, edge_pulses_clk_ptr))
+				int32_t stop_index = 0;
+				if (info.end_index > 0) {
+					stop_index = info.end_index;
+				}
+				else if (FindStopNode(start_index, edge_pulses_data_ptr, edge_pulses_clk_ptr, stop_index))
 				{
-					stop_index = edge_pulses_data_ptr->start_index;
+					//stop_index = edge_pulses_data_ptr->start_index;
 					if (stop_index > 0)
 					{
 						info.has_end = true;
@@ -183,7 +206,7 @@ namespace Protocol
 							break;
 						}
 					}
-					break;
+					//break;
 				}
 
 				//下一帧 查找下一段启动信号
@@ -191,12 +214,16 @@ namespace Protocol
 				{
 					start_index = edge_pulses_clk_ptr->start_index;
 				}
-				int32_t next_start_index = -1;
-				if (FindStartNode(start_index, edge_pulses_data_ptr, edge_pulses_clk_ptr) || (is_cancel_ptr_ != nullptr && *is_cancel_ptr_))
+				int32_t next_start_index = start_index;
+				if (FindStartNode(next_start_index, edge_pulses_data_ptr, edge_pulses_clk_ptr) || (is_cancel_ptr_ != nullptr && *is_cancel_ptr_))
 				{
-					next_start_index = edge_pulses_data_ptr->start_index;
+
+				}
+				else
+				{
+					next_start_index = -1;
 				}
-				
+
 				if (start_index >= next_start_index && next_start_index != -1)
 				{
 					start_index = next_start_index - 1;
@@ -213,12 +240,12 @@ namespace Protocol
 						start_index = data_len;
 						break;
 					}
-					if (start_index >= stop_index && stop_index != -1)
+					if (start_index >= stop_index && stop_index > 0)
 					{
 						start_index = stop_index + 1;
 						break;
 					}
-					if (start_index >= next_start_index && next_start_index != -1)
+					if (start_index >= next_start_index && next_start_index > 0)
 					{
 						start_index = next_start_index - 1;
 						break;
@@ -258,7 +285,7 @@ namespace Protocol
 				}
 
 				if (start_index >= data_len) break;
-				if (start_index >= stop_index && stop_index != -1)
+				if (start_index >= stop_index && stop_index > 0)
 				{
 					start_index = stop_index + 1;
 					break;
@@ -269,7 +296,8 @@ namespace Protocol
 					break;
 				}
 				//计算长度
-				data_info.length = stop_index - start_index;
+				//data_info.length = stop_index - start_index;
+				data_info.length = start_index - data_info.index;
 				//解析数据包后的ACK信号
 				data_info.ack_index = start_index;
 				if (!CommonHelper::GetNodeByIndex(start_index, edge_pulses_data_ptr))
@@ -277,7 +305,7 @@ namespace Protocol
 					start_index = data_len;
 					break;
 				}
-				data_info.ack = edge_pulses_data_ptr->current_level == TwoLevelEdgePulseStatusType::High;
+				data_info.ack = edge_pulses_data_ptr->current_level == TwoLevelEdgePulseStatusType::High ? 1 : 0;
 
 				if (!CommonHelper::FindNextNode(start_index, edge_pulses_clk_ptr) || edge_pulses_clk_ptr->start_index <= 0)
 				{
@@ -292,8 +320,8 @@ namespace Protocol
 					break;
 				}
 				data_info.ack_length = start_index - data_info.ack_index;
-				data_info.success_ack = false;
-				if (start_index >= stop_index && stop_index != -1)
+				data_info.success_ack = 0;
+				if (start_index >= stop_index && stop_index > 0)
 				{
 					start_index = stop_index + 1;
 					break;
@@ -322,18 +350,21 @@ namespace Protocol
 			//最后一个数据包的ACK信号为NACK
 			if (!temp_infos.empty())
 			{
-				int32_t old_data_len = static_cast<int32_t>(packet_info_datas_.size());
+				info.data_infos_index = packet_info_datas_.size() + 1;
+				//int32_t old_data_len = static_cast<int32_t>(packet_info_datas_.size());
 				info.data_infos_count = static_cast<int32_t>(temp_infos.size());
 				// 使用std::move将temp_infos的内容移动到packet_info_datas_的末尾
 				packet_info_datas_.insert(packet_info_datas_.end(),
 					std::make_move_iterator(temp_infos.begin()), std::make_move_iterator(temp_infos.end()));
-				info.data_infos_ptr = reinterpret_cast<intptr_t> (packet_info_datas_.data() + old_data_len - 1);
+				//info.data_infos_ptr = reinterpret_cast<intptr_t> (packet_info_datas_.data() + old_data_len - 1);
 
 				info.has_end = true;
-				info.end_index = start_index;
+
+				//info.end_index = start_index;
+
 				info.end_len = 1;
 
-				info.success_addr_ack = true;
+				//info.success_addr_ack = true;
 			}
 
 			packet_infos_.push_back(info);
@@ -344,52 +375,67 @@ namespace Protocol
 		}
 		result.decode_event_count = packet_infos_.size();
 		result.decode_events_ptr = packet_infos_.data();
-
+		result.decode_data_cell_ptr = packet_info_datas_.data();
 		return true;
 	}
-	bool IicDecoder::FindStopNode(int32_t& start_index, TwoLevelEdgePulse* node_sda,
-		TwoLevelEdgePulse* node_scl)
+	bool IicDecoder::FindStopNode(int32_t start_index, TwoLevelEdgePulse* node_sda,
+		TwoLevelEdgePulse* node_scl, int32_t& stop_index)
 	{
 		if (start_index < 0)
 		{
 			return false;
 		}
+		TwoLevelEdgePulse* node_sda_tmp = node_sda;
+		TwoLevelEdgePulse* node_scl_tmp = node_scl;
 		const auto start_time = std::chrono::steady_clock::now();
-		while (CommonHelper::CheckTwoLevelEdgePulseValid(node_sda))
+		while (CommonHelper::CheckTwoLevelEdgePulseValid(node_sda_tmp))
 		{
 			// 找到SDA上升沿
 			// 寻找SCL高
-			if (node_sda->start_index > start_index || node_sda->current_level != TwoLevelEdgePulseStatusType::High)
+			/*if (node_sda_tmp->start_index > start_index || node_sda_tmp->current_level != TwoLevelEdgePulseStatusType::High)
 			{
-				node_sda++;
+				node_sda_tmp++;
 				continue;
+			}*/
+			while (node_sda_tmp->start_index < start_index || node_sda_tmp->current_level != TwoLevelEdgePulseStatusType::High)
+			{
+				node_sda_tmp++;
+				if (!CommonHelper::CheckTwoLevelEdgePulseValid(node_sda_tmp))
+				{
+					return false;
+				}
 			}
-
-			if (!CommonHelper::FindNextNode(node_sda->start_index, node_scl))
+			if (!CommonHelper::FindNextNode(node_sda_tmp->start_index, node_scl_tmp))
 			{
 				return false;
 			}
 
-			if (node_sda->start_index > node_scl->start_index)
-			{
-				node_scl++;
-			}
-			if (!CommonHelper::CheckTwoLevelEdgePulseValid(node_scl))
+			while (node_sda_tmp->start_index > node_scl_tmp->start_index)
 			{
-				return false;
+				node_scl_tmp++;
+				if (!CommonHelper::CheckTwoLevelEdgePulseValid(node_scl))
+				{
+					return false;
+				}
 			}
-
+			//sda高
 			// 确认下一个SCL为低电平
-			if (node_scl->current_level == TwoLevelEdgePulseStatusType::Low)
+			if (node_scl_tmp->current_level == TwoLevelEdgePulseStatusType::Low)
 			{
-				if (node_sda->start_index < node_scl->start_index)
+				if (node_sda_tmp->start_index < node_scl_tmp->start_index)
 				{
 					// 找到目标停止条件
+					stop_index = node_sda_tmp->start_index;
 					return true;
 				}
 			}
-			start_index = node_scl->end_index + 1;
-			if (auto end_time = std::chrono::steady_clock::now(); std::chrono::duration_cast<std::chrono::nanoseconds>(
+			node_scl_tmp++;
+			start_index = node_scl_tmp->end_index;
+			//start_index = node_scl_tmp->end_index + 1;
+		/*	auto end_time = std::chrono::steady_clock::now();
+			auto tmp = std::chrono::duration_cast<std::chrono::seconds>(
+				end_time - start_time).count();*/
+			if (auto end_time = std::chrono::steady_clock::now(); std::chrono::duration_cast<std::chrono::seconds>(
 				end_time - start_time).count() >
 				DECODE_OUT_TIME_BY_SEC)
 			{
@@ -397,7 +443,7 @@ namespace Protocol
 				return false;
 			}
 		}
-		return CommonHelper::CheckTwoLevelEdgePulseValid(node_sda);
+		return CommonHelper::CheckTwoLevelEdgePulseValid(node_sda_tmp);
 	}
 
 	bool IicDecoder::FindStartNode(int32_t& start_index, TwoLevelEdgePulse* node_sda,
@@ -412,16 +458,36 @@ namespace Protocol
 		{
 			// 找到SDA下降沿
 			// 寻找SCL高
-			if (node_sda->start_index < start_index || node_sda->current_level != TwoLevelEdgePulseStatusType::Low)
+		/*	if (node_sda->start_index < start_index)
 			{
 				node_sda += 1;
-				continue;
+			}
+			if (!CommonHelper::CheckTwoLevelEdgePulseValid(node_sda))
+			{
+				return false;
+			}
+			if (node_sda->current_level != TwoLevelEdgePulseStatusType::Low)
+			{
+				node_sda += 1;
+			}*/
+			while (node_sda->start_index < start_index || node_sda->current_level != TwoLevelEdgePulseStatusType::Low)
+			{
+				node_sda++;
+				if (!CommonHelper::CheckTwoLevelEdgePulseValid(node_sda))
+				{
+					return false;
+				}
 			}
 
 			while (node_scl->start_index < start_index)
 			{
 				node_scl++;
+				if (!CommonHelper::CheckTwoLevelEdgePulseValid(node_scl))
+				{
+					return false;
+				}
 			}
+			//sda 低 
 			if (node_sda->start_index > node_scl->start_index)
 			{
 				node_scl += 1;
@@ -441,8 +507,15 @@ namespace Protocol
 					return true;
 				}
 			}
-			start_index = node_scl->end_index + 1;
-			if (auto end_time = std::chrono::steady_clock::now(); std::chrono::duration_cast<std::chrono::nanoseconds>(
+			if (node_sda->end_index < start_index)
+			{
+				node_sda++;
+			}
+			start_index = node_sda->end_index + 1;
+			/*auto end_time = std::chrono::steady_clock::now();
+			auto tmp = std::chrono::duration_cast<std::chrono::seconds>(
+				end_time - start_time).count();*/
+			if (auto end_time = std::chrono::steady_clock::now(); std::chrono::duration_cast<std::chrono::seconds>(
 				end_time - start_time).count() >
 				DECODE_OUT_TIME_BY_SEC)
 			{

+ 5 - 5
ProtocolIIC/iic_decoder.h

@@ -1,9 +1,9 @@
 // // ******************************************************************
-// //       /\ /|       @File         iicDecoder.h
+// //       /\ /|       @File         iic_decoder.h
 // //       \ V/        @Brief
 // //       | "")       @Author        lijinwen, ghz005@uni-trend.com.cn
-// //       /  |        @Creation      2024-07-16
-// //      /  \\        @Modified      2024-07-16
+// //       /  |        @Creation      2024-07-19
+// //      /  \\        @Modified      2024-07-25
 // //    *(__\_\
 // // ******************************************************************
 
@@ -43,7 +43,7 @@ namespace Protocol
 
         static bool FindStartNode(int32_t& start_index, TwoLevelEdgePulse* node_sda,
                                   TwoLevelEdgePulse* node_scl);
-        static bool FindStopNode(int32_t& start_index, TwoLevelEdgePulse* node_sda,
-                                 TwoLevelEdgePulse* node_scl);
+        static bool FindStopNode(int32_t start_index, TwoLevelEdgePulse* node_sda,
+                                 TwoLevelEdgePulse* node_scl, int32_t& stop_index);
     };
 }

+ 6 - 6
ProtocolIIC/iic_packet.h

@@ -2,8 +2,8 @@
 // //       /\ /|       @File         iic_packet.h
 // //       \ V/        @Brief
 // //       | "")       @Author        lijinwen, ghz005@uni-trend.com.cn
-// //       /  |        @Creation      2024-05-16
-// //      /  \\        @Modified      2024-06-24
+// //       /  |        @Creation      2024-07-19
+// //      /  \\        @Modified      2024-07-25
 // //    *(__\_\
 // // ******************************************************************
 
@@ -21,11 +21,11 @@ namespace Protocol
         int32_t index;
         int32_t length;
         uint8_t data;
-        bool ack;
+        uint8_t ack;
         int32_t ack_index;
         int32_t ack_length;
-        bool success_ack;
-        bool has_ack;
+        uint8_t success_ack;
+        uint8_t has_ack;
     };
 
     //内部使用数据结构 数据帧
@@ -69,7 +69,7 @@ namespace Protocol
         int32_t addr_ack_index;
         int32_t addr_ack_len;
         bool has_addr_ack;
-        intptr_t data_infos_ptr; // ptr to std::vector<IicDataInfo> packet_infos_
+        int32_t data_infos_index; // ptr to std::vector<IicDataInfo> packet_infos_
         int32_t data_infos_count;
         bool has_end;
         int32_t end_index;

+ 3 - 3
ProtocolIIC/protocol_iic_enums.h

@@ -1,9 +1,9 @@
 // // ******************************************************************
-// //       /\ /|       @File         protocol_Usb_enums.h
+// //       /\ /|       @File         protocol_iic_enums.h
 // //       \ V/        @Brief
 // //       | "")       @Author        lijinwen, ghz005@uni-trend.com.cn
-// //       /  |        @Creation      2024-05-17
-// //      /  \\        @Modified      2024-06-24
+// //       /  |        @Creation      2024-07-19
+// //      /  \\        @Modified      2024-07-25
 // //    *(__\_\
 // // ******************************************************************
 

BIN
x64/Debug/ProtocolDecoder.dll


BIN
x64/Debug/ProtocolDecoder.pdb