瀏覽代碼

将堆栈的size扩大到3072,避免写key时堆栈长度不够的问题
增加获取SN和FW Version的功能
写key和读key状态要操作flash,不能放在中断里操作,放在hrtimer中处理
接收完一帧数据的判断条件改严格:要接收到两次AA 55并且累计数据的长度与工具下发的长度相同

mohliy 1 月之前
父節點
當前提交
08f06c8722

+ 1 - 1
application/aem_watch/prj.conf

@@ -204,7 +204,7 @@ CONFIG_APP_STACKSIZE=1536
 CONFIG_MAIN_STACK_SIZE=1536
 CONFIG_UISRV_STACKSIZE=16384
 CONFIG_IDLE_STACK_SIZE=4096
-CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
+CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=3072
 
 # sdfs config
 CONFIG_SD_FS=y

+ 1 - 1
application/aem_watch/src/user_cfg/device_info_cfg.h

@@ -5,7 +5,7 @@
 #define __DEVICE_INFO_CONFIG_H__
 #define MAJOR_VERSION 1
 #define MINOR_VERSION 0
-#define MICRO_VERSION 3
+#define MICRO_VERSION 4
 #define DEVICE_ID "CJ01"//3085
 #define DEVICE_NAME "Catsights"//"3085S WATCH"
 #define DEVICE_NAME_UI "Catsights"//"3085S WATCH"

+ 1 - 1
thirdparty/lib/aem/config/aem_project.h

@@ -4,7 +4,7 @@
 #pragma warning(error : 4013)
 #endif
 #define AEM_VERSION "BL_Develop_NOR"
-#define BUILD_TIME "202502131230"
+#define BUILD_TIME "202502192050"
 #define AEM_GUI_FWK_LIB_VER "2.0.0.0"
 #define APP_DOWNLOAD_URL "https://customer.com"
 #define AEM_APP_TRANS_ANIMATION 1

+ 120 - 39
zephyr/drivers/serial/uart2_rec.c

@@ -10,15 +10,22 @@
 #include <aem_core_if.h>
 #include <logging/log.h>
 //#include <gps/gps.h>
+#include "aem_device.h"
+#include "aem_adapter_dev.h"
+#include <drivers/hrtimer.h>
 
 LOG_MODULE_REGISTER(uart2_rec, LOG_LEVEL_INF);
 #define CONFIG_UART2_REC_DEV_NAME "uart2_rec"
 #define UART2_REC_DEV_NAME CONFIG_UART_2_NAME
 
-#define UART_FIFO_MAX 256
-#define REC_DATA_BUFFER_SIZE 256
-#define PRODUCT_INFO_SIZE 64
+// 定义数据区大小
+#define UART_FIFO_MAX 64
+#define REC_DATA_BUFFER_SIZE 192
+#define DEVICE_ID_SIZE 8
+#define PROJECT_ID_SIZE 4
 #define WRITE_KEY_SIZE 160
+#define DEVICE_SN_SIZE 6
+#define FW_VERSION_SIZE 3
 
 // 定义命令
 #define DEVICE_ID_CMD      0xB1
@@ -26,6 +33,8 @@ LOG_MODULE_REGISTER(uart2_rec, LOG_LEVEL_INF);
 #define WRITE_KEY_CMD      0xB3
 #define IS_KEY_INSIDE_CMD  0xB4
 #define SET_TIME_CMD       0xC1
+#define GET_DEVICE_SN_CMD  0xD1
+#define GET_FW_VERSION_CMD 0xD3
 // 定义帧头和帧尾
 #define FRAME_HEADER1 0xAA
 #define FRAME_HEADER2 0x55
@@ -39,6 +48,7 @@ struct uart2_rec_data {
 };
 
 static struct uart2_rec_data uart2_rec_data;
+static struct hrtimer g_rtc_uart_proc;
 uint8_t bySetTimeStep = 0;
 
 extern uint8_t bySetHour;
@@ -50,6 +60,7 @@ extern uint8_t bySetDay;
 
 extern void uart2_poll_out_ch(int c); // UART2发送一个字节
 extern bool aem_read_aem_key_record(aem_key_w_record_t *record); // 读取AEM KEY记录
+extern bool aem_write_aem_key_record(uint8_t mothod, uint8_t is_pass); // 写入AEM KEY记录
 extern bool aem_defender_factory_write_key(uint8_t * data, uint16_t len); // 写入KEY
 extern uint16_t aem_factory_get_product_info(uint8_t *data, uint16_t in_len); // 获取产品信息
 
@@ -116,22 +127,73 @@ bool check_receive_frame(uint8_t *data)
     return true; // 接收成功
 }
 
-static uint8_t byAACnt = 2;
+static void uart_proc_timer_acts_handler(struct k_work *work)
+{
+	// 定义发送缓冲区
+	uint8_t tx_buff[2];
+	// 定义AEM密钥记录结构体
+	aem_key_w_record_t key_record;
+
+	// 根据接收到的命令,执行不同的操作
+	switch (uart2_rec_data.rec_data[2])
+	{
+	    case WRITE_KEY_CMD:
+			// 发送缓冲区初始化
+			tx_buff[0] = 0;
+			// 调用写入密钥函数,如果写入成功
+			if (aem_defender_factory_write_key(&uart2_rec_data.rec_data[4], WRITE_KEY_SIZE))
+			{
+				aem_write_aem_key_record(0, 1);  // 写入成功,写入NVRAM记录
+				tx_buff[0] = 0xAA;
+			}
+
+			send_frame(WRITE_KEY_CMD, tx_buff, 1);
+			break;
+
+	    case IS_KEY_INSIDE_CMD:
+			tx_buff[0] = 0;
+			if (aem_read_aem_key_record(&key_record))  // 读取NVRAM记录
+			{
+				//if (&key_record != NULL)
+				{
+					if (key_record.is_pass)
+						tx_buff[0] = 0xAA;
+				}
+			}
+
+			send_frame(IS_KEY_INSIDE_CMD, tx_buff, 1);
+			break;
+
+		default:
+			break;
+	}
+
+	
+}
+K_WORK_DEFINE(uart_proc_timer_acts, uart_proc_timer_acts_handler);
+static void htimer_uart_proc(struct hrtimer *ttimer, void *expiry_fn_arg)
+{
+	k_work_submit(&uart_proc_timer_acts);  //向系统工作队列提交一个工作项,让工作队列的线程将执行该工作
+}
+
+static uint8_t byAACnt = 0;
+static uint8_t by55Cnt = 0;
 static bool bReceiveFrameDone = false;
 static void uart_fifo_callback(const struct device *dev, void *user_data)
 {
 	uint8_t rx_buff[UART_FIFO_MAX];
 	uint8_t tx_buff[UART_FIFO_MAX];
+	char mac_str[7] = { 0 }; // MAC地址字符串
 	int read_size;
 	static uint8_t cnt;
-	aem_key_w_record_t *key_record = NULL;
+	aem_dev_info_t info; // 定义AEM设备信息结构体
 
-	printk("uart_fifo_callback start\n");
+	//printk("uart_fifo_callback start\n");
 
 	uart_irq_update(dev);
 	if (uart_irq_rx_ready(dev)) {
 		read_size = uart_fifo_read(dev, rx_buff, UART_FIFO_MAX);
-		printk("read_size: %d\n", read_size);
+		//printk("read_size: %d\n", read_size);
 		if (read_size == UART_FIFO_MAX)
 			printk("uart fifo buffer overflow\n");
 
@@ -139,20 +201,35 @@ static void uart_fifo_callback(const struct device *dev, void *user_data)
 		{
 			switch (rx_buff[i]) {
 			case FRAME_HEADER1:
-				byAACnt++;
-			    if (byAACnt > 2)
+				if (byAACnt == 0)
+					byAACnt++;
+				else if (byAACnt == 1 && by55Cnt == 2)
+					byAACnt++;
+
+			    if (by55Cnt == 0 && byAACnt == 1)
 				{
-					byAACnt = 1;
 					cnt = 0;
 					bReceiveFrameDone = false;
 				}
-				else if (byAACnt == 2)
+				else if (by55Cnt == 2 && byAACnt == 2)
 				{
 					bReceiveFrameDone = true;
+					by55Cnt = 0;
+					byAACnt = 0;
 				}
 				uart2_rec_data.rec_data[cnt++] = rx_buff[i];
 				break;
-			
+
+			case FRAME_HEADER2:
+				if (by55Cnt == 0 && cnt == 1)
+					by55Cnt++;
+
+				if (cnt > 4 && (cnt - 5 == uart2_rec_data.rec_data[3]))  //长度大于4且与接收数据的长度相等
+					by55Cnt++;
+
+				uart2_rec_data.rec_data[cnt++] = rx_buff[i];
+				break;
+
 			default:
 				uart2_rec_data.rec_data[cnt++] = rx_buff[i];
 
@@ -170,8 +247,8 @@ static void uart_fifo_callback(const struct device *dev, void *user_data)
 			switch (uart2_rec_data.rec_data[2]) 
 			{
 				case DEVICE_ID_CMD:
-					aem_factory_get_product_info(tx_buff, PRODUCT_INFO_SIZE);
-					send_frame(PROJECT_ID_CMD, tx_buff, PRODUCT_INFO_SIZE);
+					aem_factory_get_product_info(tx_buff, 64);
+					send_frame(DEVICE_ID_CMD, tx_buff, DEVICE_ID_SIZE);
 					break;
 
 				case PROJECT_ID_CMD:
@@ -179,44 +256,48 @@ static void uart_fifo_callback(const struct device *dev, void *user_data)
 					tx_buff[1] = 0x6B;
 					tx_buff[2] = 0x3B;
 					tx_buff[3] = 0xD8;
-					send_frame(PROJECT_ID_CMD, tx_buff, 4);
+					send_frame(PROJECT_ID_CMD, tx_buff, PROJECT_ID_SIZE);
 					break;
 
 				case WRITE_KEY_CMD:
-					//uart_fifo_fill(dev,rx_buff,read_size);
+				case IS_KEY_INSIDE_CMD:
 					
-					tx_buff[0] = 0;
-					if (aem_defender_factory_write_key(uart2_rec_data.rec_data, WRITE_KEY_SIZE))
+					hrtimer_init(&g_rtc_uart_proc, htimer_uart_proc, NULL);
+					hrtimer_start(&g_rtc_uart_proc, 1000*10, 0);  //10ms 后执行回调函数
+					break;
+
+				case SET_TIME_CMD:
+					if (uart2_rec_data.rec_data[3] == 6)  //时间数据长度为6
 					{
-						tx_buff[0] = 0xAA;
+						bySetMinute = uart2_rec_data.rec_data[4];
+						bySetHour = uart2_rec_data.rec_data[5];
+						bySetDay = uart2_rec_data.rec_data[6];
+						bySetWeekday = uart2_rec_data.rec_data[7];
+						bySetMonth = uart2_rec_data.rec_data[8];
+						wSetYear = uart2_rec_data.rec_data[9];
+						bySetTimeStep = 1;
 					}
-
-					send_frame(WRITE_KEY_CMD, tx_buff, 1);
+					
+					send_frame(SET_TIME_CMD, tx_buff, 0);
 					break;
 
-				case IS_KEY_INSIDE_CMD:
-				    tx_buff[0] = 0;
-					aem_read_aem_key_record(key_record);
-					if (key_record != NULL)
+				case GET_DEVICE_SN_CMD:
+					aem_get_ble_mac_addr(mac_str, 6);
+					for (int i = 0; i < DEVICE_SN_SIZE; i++)
 					{
-						if (key_record->is_pass)
-							tx_buff[0] = 0xAA;
+						tx_buff[i] = 0xFF - mac_str[i];
 					}
-
-					send_frame(IS_KEY_INSIDE_CMD, tx_buff, 1);
+					send_frame(GET_DEVICE_SN_CMD, tx_buff, DEVICE_SN_SIZE);
 					break;
 
-				case SET_TIME_CMD:
-					bySetMinute = uart2_rec_data.rec_data[4];
-					bySetHour = uart2_rec_data.rec_data[5];
-					bySetDay = uart2_rec_data.rec_data[6];
-					bySetWeekday = uart2_rec_data.rec_data[7];
-					bySetMonth = uart2_rec_data.rec_data[8];
-					wSetYear = uart2_rec_data.rec_data[9];
-					bySetTimeStep = 1;
-					send_frame(SET_TIME_CMD, tx_buff, 0);
+				case GET_FW_VERSION_CMD:
+					info = get_device_info();
+					tx_buff[0] = info.major_v;  //主版本号
+					tx_buff[1] = info.minor_v;  //次版本号
+					tx_buff[2] = info.micro_v;  //补丁版本号
+					send_frame(GET_FW_VERSION_CMD, tx_buff, FW_VERSION_SIZE);
 					break;
-
+					
 				default:
 					break;
 			}