Parcourir la source

CRC Check 기능 추가 CheckSum 기능 추가 / MBIC Header Check 기능추가

PYJ il y a 5 ans
Parent
commit
58e0cdd665

BIN
Debug/STM32F103_ATTEN_PLL_Zig.elf


Fichier diff supprimé car celui-ci est trop grand
+ 610 - 475
Debug/STM32F103_ATTEN_PLL_Zig.hex


Fichier diff supprimé car celui-ci est trop grand
+ 3427 - 2811
Debug/STM32F103_ATTEN_PLL_Zig.list


Fichier diff supprimé car celui-ci est trop grand
+ 557 - 516
Debug/STM32F103_ATTEN_PLL_Zig.map


BIN
Debug/Src/CRC16.o


+ 6 - 4
Debug/Src/CRC16.su

@@ -1,4 +1,6 @@
1
-CRC16.c:50:10:CRC16_Generate	8	static
2
-CRC16.c:84:9:CRC16_Check	12	static
3
-CRC16.c:119:9:STH30_CreateCrc	8	static
4
-CRC16.c:137:9:STH30_CheckCrc	12	static
1
+CRC16.c:51:6:Chksum_Check	8	static
2
+CRC16.c:64:9:Chksum_Create	0	static
3
+CRC16.c:76:10:CRC16_Generate	8	static
4
+CRC16.c:110:9:CRC16_Check	12	static
5
+CRC16.c:145:9:STH30_CreateCrc	8	static
6
+CRC16.c:163:9:STH30_CheckCrc	12	static

+ 3 - 3
Debug/Src/main.su

@@ -1,5 +1,5 @@
1 1
 main.c:74:6:HAL_TIM_PeriodElapsedCallback	0	static
2 2
 main.c:83:5:_write	8	static
3
-main.c:157:6:SystemClock_Config	72	static
4
-main.c:95:5:main	40	static
5
-main.c:358:6:Error_Handler	0	static
3
+main.c:161:6:SystemClock_Config	72	static
4
+main.c:99:5:main	40	static
5
+main.c:362:6:Error_Handler	0	static

+ 2 - 0
Inc/CRC16.h

@@ -15,6 +15,8 @@ typedef enum{
15 15
     NO_ERROR
16 16
 }etError;
17 17
 
18
+bool Chksum_Check(uint8_t *data, uint32_t leng,uint8_t chkdata);
19
+uint8_t Chksum_Create(uint8_t *data);
18 20
 
19 21
 uint16_t CRC16_Generate(uint8_t *buf_ptr, int32_t len);
20 22
 etError CRC16_Check(uint8_t *buf_ptr, int32_t len,uint16_t checksum);

+ 18 - 0
Inc/MBIC_Bootloader.h

@@ -23,6 +23,24 @@ typedef enum{
23 23
   MBIC_Reboot_Notice_RSP,
24 24
 }MBIC_Download_Rsp_L;
25 25
 
26
+#define MBIC_HEADER_SIZE 22
27
+#define MBIC_PREAMBLE0 0x16
28
+#define MBIC_PREAMBLE1 0x16
29
+#define MBIC_PREAMBLE2 0x16
30
+#define MBIC_PREAMBLE3 0x16
31
+#define MBIC_SUBUID0 0x00
32
+#define MBIC_SUBUID1 0xF1
33
+#define MBIC_RCODE
34
+#define MBIC_TRID
35
+#define MBIC_SEQNUM
36
+#define MBIC_TTL
37
+#define MBIC_TIME
38
+#define MBIC_ERRRESPONSE 0x00
39
+#define MBIC_CMD
40
+#define MBIC_LENGTH
41
+#define MBIC_CHECKSHUM_INDEX MBIC_HEADER_SIZE - 1 //CheckSUM REMOVE INDEX
42
+#define MBIC_ETX 0x03
43
+#define MBIC_NODE_MU 0x80
26 44
 
27 45
 typedef enum{
28 46
 	MBIC_PREAMBLE_0 = 0,

+ 1 - 1
Inc/uart.h

@@ -11,7 +11,7 @@
11 11
 #include "main.h"
12 12
 #include "Bootloader.h"
13 13
 #define hTerminal    huart1
14
-#define hWifi        huart2
14
+#define hTest        huart2
15 15
 
16 16
 #define QUEUE_BUFFER_LENGTH 2048
17 17
 

+ 26 - 0
Src/CRC16.c

@@ -5,6 +5,7 @@
5 5
  *      Author: parkyj
6 6
  */
7 7
 #include "CRC16.h"
8
+#include "MBIC_Bootloader.h"
8 9
 /*---------------------------------------------------------------------------------------*/
9 10
 /*									CRC16	TABLE						    			 */
10 11
 /*---------------------------------------------------------------------------------------*/
@@ -47,6 +48,31 @@ uint16_t Table_CRC16[]  = {
47 48
 //-----------------------------------------------
48 49
 //UART CRC üũ �Լ�
49 50
 //-----------------------------------------------
51
+bool Chksum_Check(uint8_t *data, uint32_t leng,uint8_t chkdata)
52
+{
53
+   uint8_t dataret = 0;
54
+   bool ret = false;
55
+   for(int i = MBIC_SUBUID_0; i < MBIC_HEADERCHECKSUM_0; i++){
56
+        dataret += data[i];
57
+   }
58
+   if(dataret == chkdata){
59
+        ret = true;
60
+   }
61
+//    printf("dataret : %x   chkdata : %x \r\n",dataret,chkdata);
62
+    return ret;
63
+}
64
+uint8_t Chksum_Create(uint8_t *data)
65
+{
66
+   uint8_t dataret = 0;
67
+
68
+   for(int i = MBIC_SUBUID_0; i < MBIC_HEADERCHECKSUM_0; i++){
69
+        dataret += data[i];
70
+//        printf("dataret : %x data[%d] : %x \r\n",dataret,i,data[i]);
71
+   }
72
+//    printf("dataret : %x \r\n",dataret);
73
+    return dataret;
74
+}
75
+
50 76
 uint16_t CRC16_Generate(uint8_t *buf_ptr, int32_t len)
51 77
 {
52 78
 	uint8_t dt = 0U;

+ 138 - 1
Src/MBIC_Bootloader.c

@@ -7,13 +7,150 @@
7 7
 
8 8
 
9 9
 #include "main.h"
10
+#include <stdbool.h>
10 11
 #include "MBIC_Bootloader.h"
11 12
 
13
+#define MARK_START_POSITION						0
14
+#define TYPE_START_POSITION						9	
15
+#define FILE_TYPE_START_POSITION				10
16
+#define VERSION_START_POSITION					11
17
+#define FILENAME_START_POSITION					14
18
+#define CREATION_TIME_START_POSITION			55
19
+#define LENGTH_START_POSITION					61
20
+#define CRC_START_POSITION						65
21
+#define RESERVED_START_POSITION					69
22
+typedef struct{
23
+    uint16_t Length;
24
+    uint16_t Crcchk;
25
+    bool FileChk;
26
+}BootLoaderDataCheck_st;
12 27
 
13
-void MBIC_Bootloader_FirmwareUpdate(uint8_t* data){
14 28
 
29
+BootLoaderDataCheck_st MBIC_FileDataCheck(uint8_t* data){
30
+  BootLoaderDataCheck_st ret = {0,0,false};
31
+  int8_t MBIC_Mark[9] = "JT-NRDAS ";
32
+  int8_t MBIC_FileName1[] = "jhfs-mbic-nrdas-v";
33
+  int8_t MBIC_FileName2[] = "v00.00.01.mbc";
34
+  uint8_t MBIC_type = 0x20;
35
+  uint8_t MBIC_FileType = 0x00;
36
+  int i = 0;
37
+  /***
38
+  MARK Check
39
+  ***/
40
+  for(i = MARK_START_POSITION; i < TYPE_START_POSITION; i++){
41
+    if(MBIC_Mark[i] != data[i]){
42
+      printf("Data Diff \r\n");
43
+      return ret;
44
+    }else{
45
+      printf("MARK Data Success \r\n");
46
+    }
47
+  }
48
+    /***
49
+  TYPE Check
50
+  ***/
51
+  for(i = TYPE_START_POSITION; i < FILE_TYPE_START_POSITION; i++){
52
+    if(MBIC_type != data[i]){
53
+      printf("Data Diff \r\n");
54
+      return ret;
55
+    }
56
+    else
57
+    printf("Type Data Success \r\n");
58
+  }  
59
+    /***
60
+  File Type Check
61
+  ***/
62
+  for(i = FILE_TYPE_START_POSITION; i < VERSION_START_POSITION; i++){
63
+    if(MBIC_FileType != data[i]){
64
+      printf("Data Diff \r\n");
65
+      return ret;
66
+    }
67
+    else
68
+      printf("File Type Data Success \r\n");
69
+  }  
70
+    /***
71
+  Version Check 
72
+  ***/
73
+  for(i = VERSION_START_POSITION; i < FILENAME_START_POSITION; i++){
74
+    printf("Version Data Success Version %x\r\n",data[i]);    
75
+  }
76
+  /***
77
+  File Name Check
78
+  ***/
79
+  for(i = FILENAME_START_POSITION; i < 30; i++){
80
+    if(MBIC_FileName1[i-14] != data[i]){
81
+      printf("Data Diff \r\n");
82
+      printf("MBIC_FileName1[%d] : %x, data[%d] :  %x\r\n",i - 14,MBIC_FileName1[i - 14],i,data[i]);
83
+      return ret;
84
+    }
85
+    else
86
+      printf("File Name Data Success %c\r\n",data[i]);
87
+  }  
88
+  for(i = i; i < 43; i++){
89
+    if(MBIC_FileName2[i-30] != data[i]){
90
+      printf("Data Diff %c\r\n",data[i]);
91
+      printf("MBIC_FileName2[%d] : %x, data[%d] :  %x\r\n",i - 30,MBIC_FileName2[i - 30],i,data[i]);
92
+      return ret;
93
+    }
94
+    else
95
+      printf("File Name Data Success %c\r\n",data[i]);
96
+  }   
97
+
98
+  for(i = i; i < 49; i++){
99
+      printf("Creation Success %x\r\n",data[i]);
100
+  }
101
+  for(i = i; i < 55; i++){
102
+    printf("Creation Success data[%d] : %x\r\n",i,data[i]);
103
+  }
104
+  printf(" %d",data[i++]);
105
+  printf(" -%02d",data[i++]);
106
+  printf(" -%02d",data[i++]);
107
+  printf(" -%02d",data[i++]);
108
+  printf(" -%02d",data[i++]);
109
+  printf(" -%02d\r\n",data[i++]);
110
+  
111
+  
112
+  
113
+  ret.Length = ((data[i++] << 8) & 0xFF00);
114
+  ret.Length += (data[i++]);
115
+  printf("data[%d] : %d\r\n",i - 1,ret.Length);
116
+  printf("data[%d] : %d\r\n",i - 1,ret.Length);
117
+
118
+  ret.Crcchk = ((data[i++] << 8) & 0xFF00);
119
+  ret.Crcchk += (data[i++]);
120
+  
121
+  printf("CRC_H[%d] : %x\r\n",i,ret.Crcchk);
122
+  printf("CRC_L[%d] : %x\r\n",i,ret.Crcchk);
123
+
124
+  ret.FileChk = true;
125
+  return ret;
126
+}
127
+
128
+
129
+
130
+void MBIC_Bootloader_FirmwareUpdate(uint8_t* data){
15 131
     uint8_t datatype = data[MBIC_PAYLOADSTART];
16 132
 
133
+#if 0 // PYJ.2020.06.04_BEGIN -- 
134
+    uint8_t dataTest[1024] = {
135
+      0x4A,0x54,0x2D,0x4E,0x52,0x44,0x41,0x53,0x20,0x20,0x00,0x00,
136
+        0x00,0x01,0x6A,0x68,0x66,0x73,0x2D,0x6D,0x62,0x69,0x63,0x2D,
137
+        0x6E,0x72,0x64,0x61,0x73,0x2D,0x76,0x30,0x30,0x2E,0x30,0x30,0x2E,
138
+        0x30,0x31,0x2E,0x6D,0x62,0x63,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
139
+        0x20,0x20,0x20,0x20,0x20,0x14,0x06,0x03,0x10,0x1F,0xC4,0x3C,0x49,
140
+        0x42,0x98,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
141
+        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
142
+        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
143
+        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
144
+        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
145
+    };
146
+    MBIC_FileDataCheck(dataTest);
147
+    
148
+#endif // PYJ.2020.06.04_END -- 
149
+
150
+
151
+//    printf("RX");
152
+//    for(int i = 0; i < 128; i++)
153
+//        printf("%c",*data++);
17 154
     switch(datatype){
18 155
         case MBIC_Notice_REQ:
19 156
             

+ 5 - 1
Src/main.c

@@ -86,6 +86,10 @@ int _write (int file, uint8_t *ptr, uint16_t len)
86 86
     return len;
87 87
 }
88 88
 extern UARTQUEUE TerminalQueue;
89
+
90
+
91
+
92
+
89 93
 /* USER CODE END 0 */
90 94
 
91 95
 /**
@@ -140,7 +144,7 @@ int main(void)
140 144
 	 // printf("Uart Start \r\n");
141 145
 	  if(LedTimerCnt > 500){HAL_GPIO_TogglePin(BOOT_LED_GPIO_Port,BOOT_LED_Pin);LedTimerCnt = 0;}
142 146
 	  while (TerminalQueue.data > 0 && UartTimerCnt > 30) GetDataFromUartQueue(&hTerminal);
143
-      while(FirmwareTimerCnt > 3000)  Jump_App();
147
+//	  while(FirmwareTimerCnt > 3000)  Jump_App();
144 148
 
145 149
 	  //HAL_Delay(500);
146 150
     /* USER CODE END WHILE */

+ 31 - 11
Src/uart.c

@@ -6,7 +6,8 @@
6 6
  */
7 7
 
8 8
 #include "uart.h"
9
-
9
+#include "CRC16.h"
10
+#include "MBIC_Bootloader.h"
10 11
 UARTQUEUE TerminalQueue;
11 12
 UARTQUEUE WifiQueue;
12 13
 
@@ -18,6 +19,8 @@ void InitUartQueue(pUARTQUEUE pQueue)
18 19
     {
19 20
       //_Error_Handler(__FILE__, __LINE__);
20 21
     }
22
+
23
+
21 24
     //HAL_UART_Receive_DMA(&hTerminal,  TerminalQueue.Buffer, 1);
22 25
     //HAL_UART_Receive_IT(hTerminal, pQueue->Buffer + pQueue->head, 1);
23 26
 }
@@ -51,10 +54,10 @@ void GetDataFromUartQueue(UART_HandleTypeDef *huart)
51 54
 {
52 55
     volatile static uint8_t update_data_buf[QUEUE_BUFFER_LENGTH];
53 56
     volatile static int cnt;
54
-    uint8_t temp_buf[11];
55
-    
56
-//    UART_HandleTypeDef *dst = (huart->Instance == USART2 ? &hWifi:&hTerminal);
57
-    UART_HandleTypeDef *dst = &hTerminal;
57
+    uint16_t Length = 0;
58
+    uint16_t CrcChk = 0;
59
+    UART_HandleTypeDef *dst = (huart->Instance == USART2 ? &hTest:&hTerminal);
60
+//    UART_HandleTypeDef *dst = &hTerminal;
58 61
     pUARTQUEUE pQueue = &TerminalQueue;
59 62
 //    if (HAL_UART_Transmit(dst, pQueue->Buffer + pQueue->tail, 1, 3000) != HAL_OK)
60 63
 //    {
@@ -69,15 +72,32 @@ void GetDataFromUartQueue(UART_HandleTypeDef *huart)
69 72
     if(pQueue->data == 0){
70 73
         //HAL_UART_Transmit_DMA(dst, &temp_buf[BLUECELL_HEADER00], 11);
71 74
 #if 0 // PYJ.2019.07.15_BEGIN -- 
72
-//            for(int i = 0; i < cnt; i++){
73
-//                printf("%02x",update_data_buf[i]);
74
-//            }
75
+            for(int i = 0; i < 128; i++){
76
+                printf("%02x",update_data_buf[i]);
77
+            }
75 78
 #endif // PYJ.2019.07.15_END -- 
76 79
         cnt = 0;
77
-        if(update_data_buf[0] = 0xbe)
80
+        if(update_data_buf[0] == 0xbe){
78 81
             FirmwareUpdateStart(&update_data_buf[0]);
79
-        else
80
-            MBIC_Bootloader_FirmwareUpdate(&update_data_buf[0]);
82
+        }
83
+        else if(update_data_buf[0] == MBIC_PREAMBLE0
84
+               &&update_data_buf[1] == MBIC_PREAMBLE1
85
+               &&update_data_buf[2] == MBIC_PREAMBLE2
86
+               &&update_data_buf[3] == MBIC_PREAMBLE3){
87
+                   
88
+               if(Chksum_Check(update_data_buf,MBIC_HEADER_SIZE - 4,update_data_buf[MBIC_CHECKSHUM_INDEX])){
89
+                Length = ((update_data_buf[MBIC_LENGTH_0] << 8) | update_data_buf[MBIC_LENGTH_1]);
90
+                CrcChk = ((update_data_buf[MBIC_PAYLOADSTART + Length + 1] << 8) | (update_data_buf[MBIC_PAYLOADSTART + Length + 2]));
91
+                    if(CRC16_Check(&update_data_buf[MBIC_PAYLOADSTART], Length,CrcChk)){
92
+                       MBIC_Bootloader_FirmwareUpdate(&update_data_buf[0]);
93
+                    }else{
94
+                        printf("CRC ERR %x \r\n",CrcChk);
95
+                    }
96
+               }
97
+               else{
98
+                    printf("CHECK SUM ERR %x \r\n",update_data_buf[MBIC_CHECKSHUM_INDEX]);
99
+               }
100
+            }
81 101
         for(int i  = 0; i < QUEUE_BUFFER_LENGTH; i++)
82 102
             update_data_buf[i] = 0;
83 103