Quellcode durchsuchen

add iap upgrade support and watchdog support

guoqiang vor 1 Jahr
Ursprung
Commit
8f8717a75a
16 geänderte Dateien mit 3943 neuen und 1646 gelöschten Zeilen
  1. 61 0
      Device/watchdog.c
  2. 57 0
      Device/watchdog.h
  3. 370 0
      Lib/md5c.c
  4. 27 0
      Lib/md5c.h
  5. 77 60
      Project/AirControlValve.uvoptx
  6. 26 21
      Project/AirControlValve.uvprojx
  7. 1852 387
      Project/JLinkLog.txt
  8. 871 1154
      Project/Listings/AirControlValve.map
  9. 470 3
      User/IAP.c
  10. 35 2
      User/IAP.h
  11. 11 3
      User/cfg.c
  12. 28 9
      User/cfg.h
  13. 18 0
      User/main.c
  14. 28 3
      User/main_task.c
  15. 10 4
      User/protocol.c
  16. 2 0
      User/protocol.h

+ 61 - 0
Device/watchdog.c

@@ -0,0 +1,61 @@
+#include "watchdog.h"
+#include "string.h"
+#include "ac780x.h"
+#include "ac780x_wdg.h"
+#include "ac780x_wdg_reg.h"
+#include "ac780x_gpio.h"
+//#include "wdg_sample.h"
+
+#define WDG_Timeout(n)   (n * APB_BUS_FREQ)
+#define WDG_CLK(n)     (APB_BUS_FREQ * n -1)
+ 
+
+/**
+ * WDGCallback
+ *
+ * @param[in]  wparam: reserved
+ * @param[in]  lparam: reserved
+ * @return 0: success, other: error
+ *
+ * @brief Watchdog IRQ callback function
+ * 中断服务仅有128个总线时钟(时间极短),然后MCU会被复位
+ */
+void WDGCallback(void *device, uint32_t wpara, uint32_t lpara)
+{
+    
+	return;
+}
+
+void Watchdog_Init(void)
+{
+	#ifdef WATCHDOG_ENABLE
+		uint32_t tmpTimeoutValue = WDG_CLK(WATCHDOG_TIMEOUT);            // (119999999+1)/24000000 = 5s(时钟为48M)
+		WDG_ConfigType WDGConfig;
+		
+		memset(&WDGConfig,0,sizeof(WDGConfig));
+		
+		WDGConfig.clockSource = WDG_CLOCK_APB;                //0 is 24MHz clk source
+    WDGConfig.WDGEn = ENABLE;
+    WDGConfig.interruptEn = ENABLE;           //使能中断,延迟128个总线时钟后复位,可在128总线时钟内执行中断例程
+	                                                 //若需要超时立即复位,禁能中断即可
+    WDGConfig.prescalerEn = DISABLE;          //0:disable 256 pre, 1:enable 256 pre
+    WDGConfig.updateEn = ENABLE;       		 //if update is 0,can't do any setting
+    WDGConfig.windowEn = DISABLE;          //window mode watchdog
+    WDGConfig.timeoutValue = tmpTimeoutValue;            //time = pre*timeout/clk source
+    WDGConfig.windowValue = NULL;
+    WDGConfig.callBack = WDGCallback;              //设置中断回调
+    WDG_Init(&WDGConfig);
+	
+		WDG_Feed(); 
+	#endif 
+}
+
+
+void Watchdog_Feed(void)
+{
+	
+	#ifdef WATCHDOG_ENABLE
+		WDG_Feed();                                  //刷新看门狗	
+	#endif 
+}
+

+ 57 - 0
Device/watchdog.h

@@ -0,0 +1,57 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("AutoChips Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
+ * the prior written permission of AutoChips inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of AutoChips Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * AutoChips Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
+ * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
+ * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
+ * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
+ */
+ 
+
+#ifndef WATCHDOG_H
+#define WATCHDOG_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+#include "ac780x.h"
+
+
+#define WATCHDOG_ENABLE   (1)  //
+#define WATCHDOG_TIMEOUT  2  // 2s 
+
+
+void Watchdog_Init(void);
+void Watchdog_Feed(void);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* WATCHDOG_H */
+
+

+ 370 - 0
Lib/md5c.c

@@ -0,0 +1,370 @@
+/* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm 
+ */ 
+ 
+/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All 
+rights reserved. 
+ 
+License to copy and use this software is granted provided that it 
+is identified as the "RSA Data Security, Inc. MD5 Message-Digest 
+Algorithm" in all material mentioning or referencing this software 
+or this function. 
+ 
+License is also granted to make and use derivative works provided 
+that such works are identified as "derived from the RSA Data 
+Security, Inc. MD5 Message-Digest Algorithm" in all material 
+mentioning or referencing the derived work. 
+ 
+RSA Data Security, Inc. makes no representations concerning either 
+the merchantability of this software or the suitability of this 
+software for any particular purpose. It is provided "as is" 
+without express or implied warranty of any kind. 
+ 
+These notices must be retained in any copies of any part of this 
+documentation and/or software. 
+ */ 
+#include "md5c.h" 
+#include <string.h> 
+#include <stdio.h> 
+ 
+/* Constants for MD5Transform routine. 
+*/ 
+ 
+ 
+#define S11 7 
+#define S12 12 
+#define S13 17 
+#define S14 22 
+#define S21 5 
+#define S22 9 
+#define S23 14 
+#define S24 20 
+#define S31 4 
+#define S32 11 
+#define S33 16 
+#define S34 23 
+#define S41 6 
+#define S42 10 
+#define S43 15 
+#define S44 21 
+ 
+static void MD5_memcpy (POINTER output, POINTER input, unsigned int len); 
+static void MD5Transform (UINT4 state[4], unsigned char block[64]); 
+static void Encode (unsigned char *output, UINT4 *input, unsigned int len); 
+static void MD5_memset (POINTER output, int value, unsigned int len); 
+static void Decode (UINT4 *output, unsigned char *input, unsigned int len); 
+ 
+static unsigned char PADDING[64] = { 
+ 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 
+}; 
+ 
+/* F, G, H and I are basic MD5 functions. 
+*/ 
+#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) 
+#define G(x, y, z) (((x) & (z)) | ((y) & (~z))) 
+#define H(x, y, z) ((x) ^ (y) ^ (z)) 
+#define I(x, y, z) ((y) ^ ((x) | (~z))) 
+ 
+/* ROTATE_LEFT rotates x left n bits. 
+*/ 
+#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) 
+ 
+/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. 
+Rotation is separate from addition to prevent recomputation. 
+*/ 
+#define FF(a, b, c, d, x, s, ac) { \
+	(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+ } 
+#define GG(a, b, c, d, x, s, ac) { \
+ (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+ } 
+#define HH(a, b, c, d, x, s, ac) { \
+ (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+ } 
+#define II(a, b, c, d, x, s, ac) { \
+ (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+ } 
+ 
+/* MD5 initialization. Begins an MD5 operation, writing a new context. 
+ */ 
+void MD5Init (MD5_CTX *context)          /* context */ 
+{ 
+ context->count[0] = context->count[1] = 0; 
+ /* Load magic initialization constants. 
+ */ 
+ context->state[0] = 0x67452301; 
+ context->state[1] = 0xefcdab89; 
+ context->state[2] = 0x98badcfe; 
+ context->state[3] = 0x10325476; 
+} 
+ 
+/* MD5 block update operation. Continues an MD5 message-digest 
+ operation, processing another message block, and updating the 
+ context. 
+ */ 
+void MD5Update (MD5_CTX *context, unsigned char *input, unsigned int inputLen) 
+ 
+{ 
+ unsigned int i, index, partLen; 
+ 
+ /* Compute number of bytes mod 64 */ 
+ index = (unsigned int)((context->count[0] >> 3) & 0x3F); 
+ 
+ /* Update number of bits */ 
+ if ((context->count[0] += ((UINT4)inputLen << 3)) 
+  < ((UINT4)inputLen << 3)) 
+  context->count[1]++; 
+ context->count[1] += ((UINT4)inputLen >> 29); 
+ 
+ partLen = 64 - index; 
+ 
+ /* Transform as many times as possible. 
+ */ 
+ if (inputLen >= partLen) { 
+  MD5_memcpy((POINTER)&context->buffer[index], (POINTER)input, partLen); 
+  MD5Transform (context->state, context->buffer); 
+ 
+  for (i = partLen; i + 63 < inputLen; i += 64) 
+   MD5Transform (context->state, &input[i]); 
+ 
+  index = 0; 
+ } 
+ else 
+  i = 0; 
+ 
+ /* Buffer remaining input */ 
+ MD5_memcpy((POINTER)&context->buffer[index], (POINTER)&input[i],inputLen-i); 
+} 
+ 
+/* MD5 finalization. Ends an MD5 message-digest operation, writing the 
+ the message digest and zeroizing the context. 
+ */ 
+void MD5Final (unsigned char digest[16], MD5_CTX *context)         
+{ 
+ unsigned char bits[8]; 
+ unsigned int index, padLen; 
+ 
+ /* Save number of bits */ 
+ Encode (bits, context->count, 8); 
+ 
+ /* Pad out to 56 mod 64. 
+ */ 
+ index = (unsigned int)((context->count[0] >> 3) & 0x3f); 
+ padLen = (index < 56) ? (56 - index) : (120 - index); 
+ MD5Update (context, PADDING, padLen); 
+ 
+ /* Append length (before padding) */ 
+ MD5Update (context, bits, 8); 
+ 
+ /* Store state in digest */ 
+ Encode (digest, context->state, 16); 
+ 
+ /* Zeroize sensitive information. 
+ */ 
+ MD5_memset ((POINTER)context, 0, sizeof (*context)); 
+} 
+ 
+/* MD5 basic transformation. Transforms state based on block. 
+ */ 
+static void MD5Transform (UINT4 state[4], unsigned char block[64]) 
+{ 
+ UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; 
+ 
+ Decode (x, block, 64); 
+ 
+ /* Round 1 */ 
+ FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ 
+ FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ 
+ FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ 
+ FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ 
+ FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ 
+ FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ 
+ FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ 
+ FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ 
+ FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ 
+ FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ 
+ FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ 
+ FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ 
+ FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ 
+ FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ 
+ FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ 
+ FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ 
+ 
+ /* Round 2 */ 
+ GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ 
+ GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ 
+ GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ 
+ GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ 
+ GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ 
+ GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */ 
+ GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ 
+ GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ 
+ GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ 
+ GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ 
+ GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ 
+ GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ 
+ GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ 
+ GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ 
+ GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ 
+ GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ 
+ 
+ /* Round 3 */ 
+ HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ 
+ HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ 
+ HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ 
+ HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ 
+ HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ 
+ HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ 
+ HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ 
+ HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ 
+ HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ 
+ HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ 
+ HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ 
+ HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ 
+ HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ 
+ HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ 
+ HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ 
+ HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ 
+ 
+ /* Round 4 */ 
+ II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ 
+ II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ 
+ II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ 
+ II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ 
+ II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ 
+ II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ 
+ II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ 
+ II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ 
+ II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ 
+ II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ 
+ II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ 
+ II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ 
+ II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ 
+ II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ 
+ II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ 
+ II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ 
+ 
+ state[0] += a; 
+ state[1] += b; 
+ state[2] += c; 
+ state[3] += d; 
+ 
+ /* Zeroize sensitive information. 
+ */ 
+ MD5_memset ((POINTER)x, 0, sizeof (x)); 
+} 
+ 
+/* Encodes input (UINT4) into output (unsigned char). Assumes len is 
+ a multiple of 4. 
+ */ 
+static void Encode (unsigned char *output, UINT4 *input, unsigned int len) 
+{ 
+ unsigned int i, j; 
+ 
+ for (i = 0, j = 0; j < len; i++, j += 4) { 
+  output[j] = (unsigned char)(input[i] & 0xff); 
+  output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); 
+  output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); 
+  output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); 
+ } 
+} 
+ 
+/* Decodes input (unsigned char) into output (UINT4). Assumes len is 
+ a multiple of 4. 
+ */ 
+static void Decode (UINT4 *output, unsigned char *input, unsigned int len) 
+{ 
+ unsigned int i, j; 
+ 
+ for (i = 0, j = 0; j < len; i++, j += 4) 
+  output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | 
+  (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24); 
+} 
+ 
+/* Note: Replace "for loop" with standard memcpy if possible. 
+ */ 
+ 
+static void MD5_memcpy (POINTER output, POINTER input, unsigned int len) 
+{ 
+ unsigned int i; 
+ 
+ for (i = 0; i < len; i++) 
+  output[i] = input[i]; 
+} 
+ 
+/* Note: Replace "for loop" with standard memset if possible. 
+ */ 
+static void MD5_memset (POINTER output, int value, unsigned int len) 
+{ 
+ unsigned int i; 
+ 
+ for (i = 0; i < len; i++) 
+  ((char *)output)[i] = (char)value; 
+} 
+
+#if 0
+/* Digests a string and prints the result. 
+ */ 
+void MDString (char *string,unsigned char digest[16]) 
+{ 
+ MD5_CTX context; 
+ unsigned int len = strlen (string); 
+ 
+ MD5Init (&context); 
+ MD5Update (&context, (unsigned char *)string, len); 
+ MD5Final (digest, &context); 
+} 
+/* Digests a file and prints the result. 
+ */ 
+int MD5File (char *filename,unsigned char digest[16]) 
+{ 
+ FILE *file; 
+ MD5_CTX context; 
+ int len; 
+ unsigned char buffer[1024]; 
+ 
+ if ((file = fopen (filename, "rb")) == NULL) 
+  return -1; 
+ else { 
+  MD5Init (&context); 
+  while (len = fread (buffer, 1, 1024, file)) 
+   MD5Update (&context, buffer, len); 
+  MD5Final (digest, &context); 
+ 
+  fclose (file); 
+ } 
+ return 0; 
+} 
+
+
+void MD5UpdaterString(MD5_CTX *context,const char *string) 
+{ 
+ unsigned int len = strlen (string); 
+ MD5Update (context, (unsigned char *)string, len); 
+} 
+int MD5FileUpdateFile (MD5_CTX *context,char *filename) 
+{ 
+ FILE *file; 
+ int len; 
+ unsigned char buffer[1024]; 
+ 
+ if ((file = fopen (filename, "rb")) == NULL) 
+  return -1; 
+ else { 
+  while (len = fread (buffer, 1, 1024, file)) 
+   MD5Update (context, buffer, len); 
+  fclose (file); 
+ } 
+ return 0; 
+} 
+#endif 
+

+ 27 - 0
Lib/md5c.h

@@ -0,0 +1,27 @@
+#ifndef __MD5C_H__
+#define __MD5C_H__
+/* POINTER defines a generic pointer type */ 
+typedef unsigned char * POINTER; 
+ 
+/* UINT2 defines a two byte word */ 
+//typedef unsigned short int UINT2; 
+ 
+/* UINT4 defines a four byte word */ 
+typedef unsigned long int UINT4; 
+ 
+ 
+/* MD5 context. */ 
+typedef struct { 
+ UINT4 state[4];         /* state (ABCD) */ 
+ UINT4 count[2];  /* number of bits, modulo 2^64 (lsb first) */ 
+ unsigned char buffer[64];       /* input buffer */ 
+} MD5_CTX; 
+ 
+void MD5Init (MD5_CTX *context); 
+void MD5Update (MD5_CTX *context, unsigned char *input, unsigned int inputLen); 
+void MD5UpdaterString(MD5_CTX *context,const char *string); 
+int MD5FileUpdateFile (MD5_CTX *context,char *filename); 
+void MD5Final (unsigned char digest[16], MD5_CTX *context); 
+//void MDString (char *string,unsigned char digest[16]); 
+//int MD5File (char *filename,unsigned char digest[16]); 
+#endif

+ 77 - 60
Project/AirControlValve.uvoptx

@@ -117,6 +117,11 @@
         <pMon>Segger\JL2CM3.dll</pMon>
       </DebugOpt>
       <TargetDriverDllRegistry>
+        <SetRegEntry>
+          <Number>0</Number>
+          <Key>DLGUARM</Key>
+          <Name></Name>
+        </SetRegEntry>
         <SetRegEntry>
           <Number>0</Number>
           <Key>ARMRTXEVENTFLAGS</Key>
@@ -191,7 +196,7 @@
 
   <Group>
     <GroupName>User</GroupName>
-    <tvExp>0</tvExp>
+    <tvExp>1</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>0</RteFlg>
@@ -214,30 +219,6 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\User\Comm.c</PathWithFileName>
-      <FilenameWithoutPath>Comm.c</FilenameWithoutPath>
-      <RteFlg>0</RteFlg>
-      <bShared>0</bShared>
-    </File>
-    <File>
-      <GroupNumber>1</GroupNumber>
-      <FileNumber>3</FileNumber>
-      <FileType>5</FileType>
-      <tvExp>0</tvExp>
-      <tvExpOptDlg>0</tvExpOptDlg>
-      <bDave2>0</bDave2>
-      <PathWithFileName>..\User\Comm.h</PathWithFileName>
-      <FilenameWithoutPath>Comm.h</FilenameWithoutPath>
-      <RteFlg>0</RteFlg>
-      <bShared>0</bShared>
-    </File>
-    <File>
-      <GroupNumber>1</GroupNumber>
-      <FileNumber>4</FileNumber>
-      <FileType>1</FileType>
-      <tvExp>0</tvExp>
-      <tvExpOptDlg>0</tvExpOptDlg>
-      <bDave2>0</bDave2>
       <PathWithFileName>..\User\IAP.c</PathWithFileName>
       <FilenameWithoutPath>IAP.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
@@ -245,7 +226,7 @@
     </File>
     <File>
       <GroupNumber>1</GroupNumber>
-      <FileNumber>5</FileNumber>
+      <FileNumber>3</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -257,19 +238,7 @@
     </File>
     <File>
       <GroupNumber>1</GroupNumber>
-      <FileNumber>6</FileNumber>
-      <FileType>5</FileType>
-      <tvExp>0</tvExp>
-      <tvExpOptDlg>0</tvExpOptDlg>
-      <bDave2>0</bDave2>
-      <PathWithFileName>..\User\IAP_cfg.h</PathWithFileName>
-      <FilenameWithoutPath>IAP_cfg.h</FilenameWithoutPath>
-      <RteFlg>0</RteFlg>
-      <bShared>0</bShared>
-    </File>
-    <File>
-      <GroupNumber>1</GroupNumber>
-      <FileNumber>7</FileNumber>
+      <FileNumber>4</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -281,7 +250,7 @@
     </File>
     <File>
       <GroupNumber>1</GroupNumber>
-      <FileNumber>8</FileNumber>
+      <FileNumber>5</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -293,7 +262,7 @@
     </File>
     <File>
       <GroupNumber>1</GroupNumber>
-      <FileNumber>9</FileNumber>
+      <FileNumber>6</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -305,7 +274,7 @@
     </File>
     <File>
       <GroupNumber>1</GroupNumber>
-      <FileNumber>10</FileNumber>
+      <FileNumber>7</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -317,7 +286,7 @@
     </File>
     <File>
       <GroupNumber>1</GroupNumber>
-      <FileNumber>11</FileNumber>
+      <FileNumber>8</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -329,7 +298,7 @@
     </File>
     <File>
       <GroupNumber>1</GroupNumber>
-      <FileNumber>12</FileNumber>
+      <FileNumber>9</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -341,7 +310,7 @@
     </File>
     <File>
       <GroupNumber>1</GroupNumber>
-      <FileNumber>13</FileNumber>
+      <FileNumber>10</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -353,7 +322,7 @@
     </File>
     <File>
       <GroupNumber>1</GroupNumber>
-      <FileNumber>14</FileNumber>
+      <FileNumber>11</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -373,7 +342,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>2</GroupNumber>
-      <FileNumber>15</FileNumber>
+      <FileNumber>12</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -385,7 +354,7 @@
     </File>
     <File>
       <GroupNumber>2</GroupNumber>
-      <FileNumber>16</FileNumber>
+      <FileNumber>13</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -397,7 +366,7 @@
     </File>
     <File>
       <GroupNumber>2</GroupNumber>
-      <FileNumber>17</FileNumber>
+      <FileNumber>14</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -409,7 +378,7 @@
     </File>
     <File>
       <GroupNumber>2</GroupNumber>
-      <FileNumber>18</FileNumber>
+      <FileNumber>15</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -421,7 +390,7 @@
     </File>
     <File>
       <GroupNumber>2</GroupNumber>
-      <FileNumber>19</FileNumber>
+      <FileNumber>16</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -433,7 +402,7 @@
     </File>
     <File>
       <GroupNumber>2</GroupNumber>
-      <FileNumber>20</FileNumber>
+      <FileNumber>17</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -443,17 +412,41 @@
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
+    <File>
+      <GroupNumber>2</GroupNumber>
+      <FileNumber>18</FileNumber>
+      <FileType>1</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\Lib\md5c.c</PathWithFileName>
+      <FilenameWithoutPath>md5c.c</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
+    <File>
+      <GroupNumber>2</GroupNumber>
+      <FileNumber>19</FileNumber>
+      <FileType>5</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\Lib\md5c.h</PathWithFileName>
+      <FilenameWithoutPath>md5c.h</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
   </Group>
 
   <Group>
     <GroupName>Device</GroupName>
-    <tvExp>0</tvExp>
+    <tvExp>1</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>21</FileNumber>
+      <FileNumber>20</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -465,7 +458,7 @@
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>22</FileNumber>
+      <FileNumber>21</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -477,7 +470,7 @@
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>23</FileNumber>
+      <FileNumber>22</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -489,7 +482,7 @@
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>24</FileNumber>
+      <FileNumber>23</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -501,7 +494,7 @@
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>25</FileNumber>
+      <FileNumber>24</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -513,7 +506,7 @@
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>26</FileNumber>
+      <FileNumber>25</FileNumber>
       <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -523,6 +516,30 @@
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
+    <File>
+      <GroupNumber>3</GroupNumber>
+      <FileNumber>26</FileNumber>
+      <FileType>1</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\Device\watchdog.c</PathWithFileName>
+      <FilenameWithoutPath>watchdog.c</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
+    <File>
+      <GroupNumber>3</GroupNumber>
+      <FileNumber>27</FileNumber>
+      <FileType>5</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\Device\watchdog.h</PathWithFileName>
+      <FilenameWithoutPath>watchdog.h</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
   </Group>
 
   <Group>
@@ -535,7 +552,7 @@
 
   <Group>
     <GroupName>::Device</GroupName>
-    <tvExp>0</tvExp>
+    <tvExp>1</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>1</RteFlg>

+ 26 - 21
Project/AirControlValve.uvprojx

@@ -49,7 +49,7 @@
             <InvalidFlash>1</InvalidFlash>
           </TargetStatus>
           <OutputDirectory>.\Objects\</OutputDirectory>
-          <OutputName>AirControlValve</OutputName>
+          <OutputName>AirControlValve_20240108</OutputName>
           <CreateExecutable>1</CreateExecutable>
           <CreateLib>0</CreateLib>
           <CreateHexFile>1</CreateHexFile>
@@ -80,9 +80,9 @@
             <nStopB2X>0</nStopB2X>
           </BeforeMake>
           <AfterMake>
-            <RunUserProg1>0</RunUserProg1>
+            <RunUserProg1>1</RunUserProg1>
             <RunUserProg2>0</RunUserProg2>
-            <UserProg1Name></UserProg1Name>
+            <UserProg1Name>fromelf --bin -o "$L@L.bin" "#L</UserProg1Name>
             <UserProg2Name></UserProg2Name>
             <UserProg1Dos16Mode>0</UserProg1Dos16Mode>
             <UserProg2Dos16Mode>0</UserProg2Dos16Mode>
@@ -134,11 +134,11 @@
             <RunIndependent>0</RunIndependent>
             <UpdateFlashBeforeDebugging>1</UpdateFlashBeforeDebugging>
             <Capability>1</Capability>
-            <DriverSelection>-1</DriverSelection>
+            <DriverSelection>4096</DriverSelection>
           </Flash1>
           <bUseTDR>1</bUseTDR>
           <Flash2>BIN\UL2CM3.DLL</Flash2>
-          <Flash3></Flash3>
+          <Flash3>"" ()</Flash3>
           <Flash4></Flash4>
           <pFcarmOut></pFcarmOut>
           <pFcarmGrp></pFcarmGrp>
@@ -276,7 +276,7 @@
               <OCR_RVCT4>
                 <Type>1</Type>
                 <StartAddress>0x8000000</StartAddress>
-                <Size>0x20000</Size>
+                <Size>0xa800</Size>
               </OCR_RVCT4>
               <OCR_RVCT5>
                 <Type>1</Type>
@@ -388,16 +388,6 @@
               <FileType>1</FileType>
               <FilePath>..\User\main.c</FilePath>
             </File>
-            <File>
-              <FileName>Comm.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>..\User\Comm.c</FilePath>
-            </File>
-            <File>
-              <FileName>Comm.h</FileName>
-              <FileType>5</FileType>
-              <FilePath>..\User\Comm.h</FilePath>
-            </File>
             <File>
               <FileName>IAP.c</FileName>
               <FileType>1</FileType>
@@ -408,11 +398,6 @@
               <FileType>5</FileType>
               <FilePath>..\User\IAP.h</FilePath>
             </File>
-            <File>
-              <FileName>IAP_cfg.h</FileName>
-              <FileType>5</FileType>
-              <FilePath>..\User\IAP_cfg.h</FilePath>
-            </File>
             <File>
               <FileName>main_task.c</FileName>
               <FileType>1</FileType>
@@ -488,6 +473,16 @@
               <FileType>5</FileType>
               <FilePath>..\Lib\fifo.h</FilePath>
             </File>
+            <File>
+              <FileName>md5c.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\Lib\md5c.c</FilePath>
+            </File>
+            <File>
+              <FileName>md5c.h</FileName>
+              <FileType>5</FileType>
+              <FilePath>..\Lib\md5c.h</FilePath>
+            </File>
           </Files>
         </Group>
         <Group>
@@ -523,6 +518,16 @@
               <FileType>5</FileType>
               <FilePath>..\Device\uart.h</FilePath>
             </File>
+            <File>
+              <FileName>watchdog.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\Device\watchdog.c</FilePath>
+            </File>
+            <File>
+              <FileName>watchdog.h</FileName>
+              <FileType>5</FileType>
+              <FilePath>..\Device\watchdog.h</FilePath>
+            </File>
           </Files>
         </Group>
         <Group>

Datei-Diff unterdrückt, da er zu groß ist
+ 1852 - 387
Project/JLinkLog.txt


Datei-Diff unterdrückt, da er zu groß ist
+ 871 - 1154
Project/Listings/AirControlValve.map


+ 470 - 3
User/IAP.c

@@ -32,7 +32,10 @@
  */
  
 #include <string.h>
+//#include "crc16.h"
 #include "IAP.h" 
+#include "md5c.h"
+#include "watchdog.h"
 
 
 static __align(4)uint8_t WriteDataBuff[2048];
@@ -43,6 +46,158 @@ static uint8_t s_iapStep;
 
 static uint32_t s_writeDataBuffSeek;
 
+//升级完成后, 重启标志
+uint8_t iap_reboot = 0;
+
+
+///< 升级包信息
+static uint8_t s_flashType;
+static uint32_t s_version[4];
+static uint32_t s_flashMemSize;
+static uint32_t s_recvByte=0;
+static uint16_t s_sequence=0; 
+
+//回传数据buffer 指针、 及数据长度
+static uint8_t* s_wData = NULL;
+static uint16_t s_wLen = 0;
+
+
+typedef void (*pFunction)(void);
+static pFunction s_jumpToApplication;
+/**
+* JumpTOApp
+*
+* @param[in] none
+* @return    none
+*
+* @brief  Jump to APP Code
+*/
+void JumpTOApp1(void)
+{
+    uint32_t JumpAddress;
+
+    if(((*(__IO uint32_t *)APP1_ADDRESS) & 0x2FFE0000) == 0x20000000)
+    {
+        __ASM("CPSID I");//关全局中断
+        JumpAddress = *(__IO uint32_t *)(APP1_ADDRESS + 4); // Jump to user application
+        s_jumpToApplication = (pFunction)JumpAddress;   // Initialize user application's Stack Pointer
+        __set_MSP(*(__IO uint32_t*)APP1_ADDRESS);
+        s_jumpToApplication();  /* jump to app */
+    }
+    else
+    {
+        //printf("top of stack pointer is unvalid! enter APP update!\r\n");
+        return;
+    }
+    while (1);
+}
+
+int App_MD5_Check(uint32_t addr,unsigned int all_len)
+{
+	unsigned char digest[16];
+//	unsigned char *md5_ptr=(unsigned char *)(StartMode_Addr+72);
+	unsigned int i,update_len;
+	MD5_CTX md5c;
+	all_len -= 16;
+	MD5Init(&md5c);
+	for(i=0;i<all_len;)
+	{
+		if(all_len>(i+512))
+			update_len = 512;
+		else
+			update_len = all_len-i;
+		memcpy(WriteDataBuff,(const void *)(addr+i),update_len);
+		MD5Update (&md5c, WriteDataBuff, update_len);
+		i += update_len;
+		
+		Watchdog_Feed();
+	}
+	MD5Final(digest,&md5c);
+	//memcpy(iapbuf,(const void *)(addr+all_len),16);
+	for(i=0;i<16;++i)
+	{
+		if(digest[i]!=*(unsigned char *)(addr+all_len+i))
+			break;
+	}
+	if(i>=16)
+		return 1;
+	else
+		return 0;
+}
+
+/*
+// appxaddr:应用程序的起始地址
+// appbuf:应用程序CODE.
+// appsize:应用程序大小(字节).
+void IAP_write_appbin(uint32_t appxaddr, uint8_t *appbuf, uint32_t appsize)
+{
+	uint32_t t;
+	uint16_t i = 0;
+	uint16_t temp;
+	uint32_t fwaddr = appxaddr; //当前写入的地址
+	uint32_t *dfu = appbuf;
+	for (t = 0; t < appsize; t += 2)
+	{
+		temp = (uint16_t)dfu[1] << 8;
+		temp += (uint16_t)dfu[0];
+		dfu += 2; //偏移2个字节
+		iapbuf[i++] = temp;
+		if (i == 1024)
+		{
+			i = 0;
+			HAL_GPIO_TogglePin(WDI_sp706_kanmemgou_GPIO_Port, WDI_sp706_kanmemgou_Pin);
+			STMFLASH_Write(fwaddr, iapbuf, 1024);
+			fwaddr += 2048; //偏移2048  16=2*8.所以要乘以2.
+		}
+		
+	}
+	if (i)
+		STMFLASH_Write(fwaddr, iapbuf, i); //将最后的一些内容字节写进去.
+}
+
+*/
+
+//
+int IAP_Init(void)
+{
+	/*
+    EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;
+    memcpy(reflectionBuff, (void *)CONFIG_ADDRESS, sizeof(Config_type));
+    if (config->flag0 != Flag0Value || config->flag1 != flag1Value)
+    {
+				// initiliaze flash 
+        memset(reflectionBuff, 0, sizeof(Config_type)); 
+        config->flag0 = Flag0Value;
+        config->flag1 = flag1Value;
+        
+        EFLASH_UnlockCtrl();
+        ret = EFLASH_PageErase(CONFIG_ADDRESS);     ///<erase page
+        if (ret != EFLASH_STATUS_SUCCESS) return -1;
+        ret = EFLASH_PageEraseVerify(CONFIG_ADDRESS);   ///< verify erase state
+        if (ret != EFLASH_STATUS_SUCCESS) return -1;
+        ret = EFLASH_PageProgram(CONFIG_ADDRESS, (uint32_t *)reflectionBuff, (sizeof(Config_type)>>2)+1);   ///< 
+        if (ret != EFLASH_STATUS_SUCCESS) return -1;
+        EFLASH_LockCtrl();
+    }
+		
+		*/
+		
+		s_recvByte=0;
+		s_sequence=0;
+		
+		s_version[0] = 0;
+		s_version[1] = 0;
+		s_version[2] = 0;
+		s_version[3] = 0;
+		
+		s_flashType = 0;
+		s_flashMemSize = 0;
+		
+		iap_reboot = 0;
+		
+    return 0;
+}
+
 
 void IAP_Start(void)
 {
@@ -50,9 +205,11 @@ void IAP_Start(void)
     {
         return;     ///<repeat start
     }
-    s_writeAddress = APP_ADDRESS;
+    s_writeAddress = APP2_ADDRESS;
     s_iapStep = 1;
 }
+
+
 int IAP_Write(uint8_t *pdata, uint16_t length)
 {
     EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;
@@ -61,7 +218,7 @@ int IAP_Write(uint8_t *pdata, uint16_t length)
         return -1;
     while (length > 0)
     {
-        if ((s_writeAddress < APP_ADDRESS) || (s_writeAddress >= APP_ADDRESS_END))  ///<IAP不能超出APP_BKP地址范围
+        if ((s_writeAddress < APP2_ADDRESS) || (s_writeAddress >= APP2_ADDRESS_END))  ///<IAP不能超出APP_BKP地址范围
             return -1;
         if (s_writeDataBuffSeek + length > 2048)    ///得到单次写入的长度
         {
@@ -96,7 +253,7 @@ int IAP_Stop(void)
 {
     EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;
     s_iapStep = 0;  ///<stop后重置step
-    if ((s_writeAddress < APP_ADDRESS) || (s_writeAddress >= APP_ADDRESS_END))  ///<IAP不能超出APP_BKP地址范围
+    if ((s_writeAddress < APP2_ADDRESS) || (s_writeAddress >= APP2_ADDRESS_END))  ///<IAP不能超出APP_BKP地址范围
         return -1;
     EFLASH_UnlockCtrl();
     ret = EFLASH_PageErase(s_writeAddress);     ///<erase page
@@ -110,3 +267,313 @@ int IAP_Stop(void)
 }
 
 
+
+/**
+* DataTransferRequestHandle
+*
+* @param[in] pdata:data point
+* @param[in] length:data length
+*                   
+* @return    none
+*
+* @brief  上位机通信 数据传输请求
+*/
+static void DataTransferRequestHandle(uint8_t *pdata, uint16_t length)
+{
+		uint32_t _version[4];
+    s_flashType = pdata[2];
+	
+		_version[0] = ((uint32_t)pdata[3]<<24) | ((uint32_t)pdata[4]<<16)| ((uint32_t)pdata[5]<<8)| pdata[6]; 
+		_version[1] = ((uint32_t)pdata[7]<<24) | ((uint32_t)pdata[8]<<16)| ((uint32_t)pdata[9]<<8)| pdata[10]; 
+		_version[2] = ((uint32_t)pdata[11]<<24) | ((uint32_t)pdata[12]<<16)| ((uint32_t)pdata[13]<<8)| pdata[14]; 
+		_version[3] = ((uint32_t)pdata[15]<<24) | ((uint32_t)pdata[16]<<16)| ((uint32_t)pdata[17]<<8)| pdata[18]; 
+	
+    s_flashMemSize = pdata[19];
+    s_flashMemSize <<= 8;
+    s_flashMemSize += pdata[20];
+    s_flashMemSize <<= 8;
+    s_flashMemSize += pdata[21];
+    s_flashMemSize <<= 8;
+    s_flashMemSize += pdata[22];
+	
+    
+    s_wData[s_wLen++] = DataTransferCmdHost;
+    s_wData[s_wLen++] = DataTransferRequest;
+		
+    if (s_flashType != IAP_BIN_FILE)    ///<目前只支持传输IAP bin 文件
+    {
+        s_wData[s_wLen++] = ResultParamErr;
+    }
+    else
+    {
+        if (s_flashMemSize > APP_SIZE)
+        {
+            s_wData[s_wLen++] = ResultExceedLimit;  ///<exceed limit 
+        }
+        else 
+        {
+            IAP_Start();
+            s_wData[s_wLen++] = ResultSuccess;
+        }
+    }
+    
+    s_wData[s_wLen++] = (uint8_t)((MaxBlockSize >> 24) & 0xff);
+    s_wData[s_wLen++] = (uint8_t)((MaxBlockSize >> 16) & 0xff);
+    s_wData[s_wLen++] = (uint8_t)((MaxBlockSize >> 8) & 0xff);
+    s_wData[s_wLen++] = (uint8_t)(MaxBlockSize & 0xff);
+		
+		//版本不一致
+		if(0 != memcmp(s_version, _version, 16)){
+			
+			s_version[0] = _version[0];
+			s_version[1] = _version[1];
+			s_version[2] = _version[2];
+			s_version[3] = _version[3];
+			
+			s_recvByte=0;
+			s_sequence=0;
+
+		}
+		
+		s_wData[s_wLen++] = (s_sequence>>8)&0xFF;
+		s_wData[s_wLen++] =  s_sequence&0xFF;
+		
+}
+/**
+* TransferDataHandle
+*
+* @param[in] pdata:data point
+* @param[in] length:data length
+*                   
+* @return    none
+*
+* @brief  上位机通信 传输数据处理
+*/
+static void TransferDataHandle(uint8_t *pdata, uint16_t length)
+{
+		uint16_t _sequence = pdata[2];
+		_sequence <<= 8;
+		_sequence += pdata[3];
+		
+		s_wData[s_wLen++] = DataTransferCmdHost;
+    s_wData[s_wLen++] = DataTransferRequest;
+	
+    if (length-4 > MaxBlockSize)
+    {
+        s_wData[s_wLen++] = ResultExceedLimit;    ///<长度超出限制
+        IAP_Stop();
+    }
+    else if (_sequence != s_sequence)
+    {
+        s_wData[s_wLen++] = ResultSequenceErr;    ///<序列号不正确
+        IAP_Stop();
+    }
+    else 
+    {
+        
+        if (0 != IAP_Write(&pdata[4], length - 4))
+        {
+            s_wData[s_wLen++] = ResultExecErr;    ///<执行错误
+            IAP_Stop();
+        }else{
+						s_wData[s_wLen++] = ResultSuccess;    ///<succeed
+						s_recvByte += length - 4;
+						s_sequence++;
+				}
+        
+    }
+		
+		s_wData[s_wLen++] = (s_sequence>>8)&0xFF;
+		s_wData[s_wLen++] =  s_sequence&0xFF;
+		
+}
+/**
+* TransferExitHandle
+*
+* @param[in] pdata:data point
+* @param[in] length:data length
+*                   
+* @return    none
+*
+* @brief  上位机通信 传输数据结束
+*/
+static void TransferExitHandle(uint8_t *pdata, uint16_t length)
+{
+    s_wData[s_wLen++] = DataTransferCmdHost;
+    s_wData[s_wLen++] = DataTransferRequest;
+	
+    //crc = ((uint16_t)pdata[2] << 8) | pdata[3]; 
+    
+    if (0 != IAP_Stop())
+    {
+        s_wData[s_wLen++] = ResultExecErr;    ///<执行错误
+    }
+    else
+    {
+        if (s_recvByte != s_flashMemSize)
+        {
+            s_wData[s_wLen++] = ResultTotalLengthErr;    ///<接收数据长度不正确
+        }
+        else
+        {
+
+						s_wData[s_wLen++] = ResultSuccess;
+					
+						config->App2Size = s_flashMemSize;
+						config->IapFlag = Startup_Update;
+						SaveConfig();
+					
+						//if (0 != SaveConfig())
+						//{
+						//   data[index] = ResultExecErr;
+						//}
+					
+						iap_reboot = 1;
+ 
+        }
+    }
+    
+}
+
+
+/**
+* IAP_CmdHandle
+*
+* @param[in] pdata:recv data point
+* @param[in] length:data length
+* @param[in] pout:send data point                 
+* @return    none
+*
+* @brief  上位机通信 数据接收处理函数
+*/
+uint16_t IAP_CmdHandle(uint8_t* pdata, uint16_t length, uint8_t* pout)
+{
+		s_wData = pout;
+		s_wLen = 0;
+	
+		if(DataTransferCmd == pdata[0]){
+			
+				switch (pdata[1])
+				{
+						case DataTransferRequest: //0x00
+								DataTransferRequestHandle(pdata, length);
+								break;
+						case TransferData: //0x01
+								TransferDataHandle(pdata, length);
+								break;
+						case TransferExit: //0x02
+								TransferExitHandle(pdata, length);
+								break;
+						default :
+							s_wData[0] = ResultCmdErr;
+							s_wLen =  1;
+								break;
+				}
+				
+		
+		}else{
+			
+			s_wData[0] = ResultCmdErr;
+			s_wLen =  1;
+		}
+		 
+		return s_wLen;
+    
+}
+
+
+
+/**
+ * @bieaf 进行BootLoader的启动
+ *
+ * @param none
+ * @return none
+ */
+void Start_BootLoader(void)
+{
+	//uint16_t crcCheck;
+	uint32_t writed_len;
+	uint32_t will_write_len;
+	EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;
+
+	switch (config->IapFlag) ///< 读取是否启动应用程序 */
+	{
+	case Startup_Normal: ///< 正常启动 */   //在APP2中可以设定此标志位   使得下次重启之后进入APP1
+	{
+		; // printf("> Normal start......\r\n");
+		break;
+	}
+	case Startup_Update: /*启动最新的程序 */
+
+			//crcCheck = crc16((const uint8_t *)APP2_ADDRESS, config->App2Size);
+			if ((config->App2Size <= APP_SIZE) && App_MD5_Check(APP2_ADDRESS,config->App2Size))
+			{
+					writed_len =0;
+					will_write_len = config->App2Size;
+				
+					do{
+						
+						Watchdog_Feed();
+						
+						if(will_write_len >= 2048){
+							memcpy(WriteDataBuff, (void *)(APP2_ADDRESS+writed_len), 2048);
+							
+							EFLASH_UnlockCtrl();
+							ret = EFLASH_PageErase(APP1_ADDRESS+writed_len);     ///<erase page
+							if (ret != EFLASH_STATUS_SUCCESS) break;
+							ret = EFLASH_PageEraseVerify(APP1_ADDRESS+writed_len);   ///< verify erase state
+							if (ret != EFLASH_STATUS_SUCCESS) break;
+							ret = EFLASH_PageProgram(APP1_ADDRESS+writed_len, (uint32_t *)WriteDataBuff, 512); 
+							if (ret != EFLASH_STATUS_SUCCESS) break;
+							EFLASH_LockCtrl();
+							
+							writed_len += 2048;
+							will_write_len -= 2048;
+							
+						}else{
+							
+							memcpy(WriteDataBuff, (void *)(APP2_ADDRESS+writed_len), will_write_len);
+							
+							EFLASH_UnlockCtrl();
+							ret = EFLASH_PageErase(APP1_ADDRESS+writed_len);     ///<erase page
+							if (ret != EFLASH_STATUS_SUCCESS) break;
+							ret = EFLASH_PageEraseVerify(APP1_ADDRESS+writed_len);   ///< verify erase state
+							if (ret != EFLASH_STATUS_SUCCESS) break;
+							ret = EFLASH_PageProgram(APP1_ADDRESS+writed_len, (uint32_t *)WriteDataBuff, (will_write_len+3)/4); 
+							if (ret != EFLASH_STATUS_SUCCESS) break;
+							EFLASH_LockCtrl();
+							
+							writed_len += will_write_len;
+							will_write_len = 0;
+						
+						}
+						
+					}while(will_write_len > 0);
+					
+					if(EFLASH_STATUS_SUCCESS == ret){
+							config->App1Size = config->App2Size;
+					
+					}		
+				
+			}
+			
+			config->IapFlag = Startup_APP1;
+			SaveConfig();
+						
+	case Startup_APP1: /*启动最新的程序 */
+			
+			//crcCheck = crc16((const uint8_t *)APP1_ADDRESS, config->App1Size);
+			if((config->App1Size <= APP_SIZE) && App_MD5_Check(APP1_ADDRESS,config->App1Size))
+			{
+					JumpTOApp1();
+			}
+	
+		break;
+	default: ///< 启动失败
+	{
+		return;
+	}
+	}
+}
+
+

+ 35 - 2
User/IAP.h

@@ -41,12 +41,45 @@ extern "C" {
 #include "cfg.h"
 #include "ac780x_eflash.h"
   
-//extern Config_type *config;    
+///<cmd
+#define ControlCmd      (0x02)
+#define DataTransferCmd (0x40)
+#define DataTransferCmdHost (0x41)
+
+///<smd
+///<cmd=0x40
+#define DataTransferRequest (0x00)
+#define TransferData        (0x01)
+#define TransferExit        (0x02)
+
+///<cmd=0x02
+#define RequestReset        (0x00)
+
+/* cmd=0x40 smd=0x00 */
+/* Transfer type */
+#define IAP_BIN_FILE    (1)
+#define MaxBlockSize    (256)  //²»Äܳ¬³ö½ÓÊÕbuffer size  #define UART0_RECV_DATA_POOL_COUNT     320  
+
+/* Result */
+#define ResultSuccess					(0)
+#define ResultBusy						(1)
+#define ResultExceedLimit			(2)
+#define ResultTotalLengthErr	(3)
+#define ResultSequenceErr			(4)
+#define ResultCRCErr					(5)
+#define ResultParamErr        (6)
+#define ResultCmdErr        	(7)
+#define ResultExecErr         (8)
+
+extern uint8_t iap_reboot;
     
-//extern int IAP_Init(void);
+extern int IAP_Init(void);
 extern void IAP_Start(void);
 extern int IAP_Write(uint8_t *pdata, uint16_t length);
 extern int IAP_Stop(void);
+extern uint16_t IAP_CmdHandle(uint8_t* pdata, uint16_t length, uint8_t* pout);
+
+extern void Start_BootLoader(void);
     
 #ifdef __cplusplus
 }

+ 11 - 3
User/cfg.c

@@ -13,8 +13,9 @@ static void Factory_reset(void)
 		config->addr = DEFAULT_ADDR;
 		config->br_index = BaudRate_9600;
 	
-		config->AppSize = 0;
-		config->AppCrc = 0;
+		//config->App2Size = 0;
+		//config->App2Crc = 0;
+		//config->IapFlag = 0;
 
 }
 
@@ -24,11 +25,18 @@ int Config_Init(void)
     memcpy(reflectionBuff, (void *)CONFIG_ADDRESS, sizeof(Config_type));
     if (config->magic != CONFIG_MAGIC)
     {/* initiliaze config */
-        memset(reflectionBuff, 0, sizeof(Config_type)); 
+        memset(reflectionBuff, 0xFF, sizeof(Config_type)); 
         //config->magic = CONFIG_MAGIC;
 			
 			
 				Factory_reset();
+			
+				//
+				config->IapFlag = Startup_Normal;
+				config->hw_version = 0x0000;
+				config->devicetype = 0x0000;
+				config->deviceid = 0x00;
+				
         SaveConfig();
 			
     }

+ 28 - 9
User/cfg.h

@@ -39,36 +39,55 @@ extern "C" {
 #endif
 #include "ac780x.h"
 
+//控制程序,是否编译为  bootloader, hex 格式
+#define IS_BOOTLOADER       (1)   
+
+
+
 #define IAP_BOOTLOAD_ADDRESS    0x8000000    /* define bootload start address */
-#define CONFIG_ADDRESS      		0x08007800  /* 预留一个页用于存放配置信息 */
-#define APP_ADDRESS    					0x8008000    /* define app start address */   
-#define APP_ADDRESS_TEMP    		0x8014000    /* start address for ota package */   
+#define CONFIG_ADDRESS      		0x800A800    /* 预留一个页用于存放配置信息 */
+#define APP1_ADDRESS    					0x800B000    /* define app start address */   
+#define APP2_ADDRESS    					0x8015800    /* start address for ota package */   
     
 #define EFLASH_PAGE_SIZE_INIT 2048  /* define one-page size */
 #define EFLASH_PAGE_NUM_INIT  54    /* define APP page num, size=2048*54=108K */
     
-#define APP_SIZE        (EFLASH_PAGE_SIZE_INIT * EFLASH_PAGE_NUM_INIT)
-#define APP_ADDRESS_END     ((uint32_t)(APP_ADDRESS + APP_SIZE))   
+#define APP_SIZE        (EFLASH_PAGE_SIZE_INIT * 21)  /* 42 KB*/
+#define APP2_ADDRESS_END     ((uint32_t)(APP2_ADDRESS + APP_SIZE))   
   
 
-#define CONFIG_MAGIC   0xaa55
+#define CONFIG_MAGIC   		0xA5A5
 
 #define DEFAULT_ADDR     0x20
 #define BROADCAST_ADDR   0xFF
+
+/* 升级标志 */
+#define Startup_Normal 	0xBBBB	///< run bootloader 
+#define Startup_Update 	0xAAAA	///< copy app2 to app1 
+//#define Startup_Reset  	0x5555AAAA	///< ***???? ?????***
+#define Startup_APP1  	0x55BB	///< jump and run  app1,
+
 /*
-* sizeof(Config_type) 不能超出2KB 
+* sizeof(Config_type)  不能超出2KB 
+* sizeof(Config_type)  采用固定大小, 256 字节, 对于传感器来说足够用了。 最后是IAP 升级需要的信息; 要与前面的参数分开
 */
 typedef struct 
 {
 		uint16_t magic;
     uint8_t  addr;
     uint8_t  br_index;
+	
+		uint32_t  res[58];
+	
 		uint16_t hw_version;
 		uint16_t devicetype;
 	  uint32_t deviceid;
 	
-    uint32_t AppSize;
-    uint32_t AppCrc;
+		uint32_t App1Size;
+    uint32_t App2Size;
+	
+		uint16_t IapFlag;
+		uint16_t res2;
 }Config_type;
 
 extern Config_type *config;

+ 18 - 0
User/main.c

@@ -2,8 +2,11 @@
 #include "timer.h"
 #include "gpio.h"
 #include "cfg.h"
+#include "IAP.h"
 #include "process.h"
 #include "main_task.h"
+#include "watchdog.h"
+
 
 /*!
 * @brief entry 
@@ -13,10 +16,24 @@
 */
 int main(void)
 {
+	
+	#ifndef IS_BOOTLOADER
+		__ASM("CPSIE I");///使能全局中断
+    SCB->VTOR = APP1_ADDRESS;    ///<重映射向量表
+	#endif
+	
 		InitDelay();
 		Config_Init();
+		Watchdog_Init();
+		
+	
+	#ifdef IS_BOOTLOADER
+		Start_BootLoader();
+	#endif
 	
     GPIO_PortInit();
+	  IAP_Init();
+	
 	
 		Process_Init();
 	
@@ -26,6 +43,7 @@ int main(void)
 	
     while (1)
     {
+				Watchdog_Feed();
         Task_Handle();
     }
 		

+ 28 - 3
User/main_task.c

@@ -2,6 +2,7 @@
 #include "uart.h"
 #include "gpio.h"
 #include "cfg.h"
+#include "IAP.h"
 #include "protocol.h"
 #include "process.h"
 #include "crc16.h"
@@ -120,6 +121,12 @@ void Uart0_RxDataHandle(void)
 											ret_write = RET_NEED_SAVE|RET_NEED_REBOOT;
 											//NVIC_SystemReset();
 											break;
+										case Cmd_IapUpgrade:
+											tx_len += IAP_CmdHandle(rxbuf+tx_len, rx_len-4-2, txbuf+tx_len);
+											if(1 == iap_reboot){
+												ret_write |= RET_NEED_REBOOT;
+											}
+											break;
 										default:
 											ret_write = RET_CMDINVALID;
 											break;
@@ -137,9 +144,17 @@ void Uart0_RxDataHandle(void)
 											}
 									
 									}else{ 
-											//copy the whole rx frame
-											memcpy(txbuf, (void *)rxbuf, rx_len-2);
-											tx_len =rx_len-2;
+											if(Cmd_IapUpgrade != cmd){
+												
+												//copy the whole rx frame
+												memcpy(txbuf, (void *)rxbuf, rx_len-2);
+												tx_len =rx_len-2;
+												
+											}else{ 
+												//Éý¼¶ÃüÁɶ¶¼²»ÓÃ×ö
+											
+											}
+											
 									}
 									
 									crc = crc16(txbuf, tx_len);
@@ -153,6 +168,7 @@ void Uart0_RxDataHandle(void)
 									}
 									
 									if((ret_write&RET_NEED_REBOOT) > 0 ){
+											mdelay(100);
 											NVIC_SystemReset();
 									}
 	
@@ -211,6 +227,15 @@ void Uart0_RxDataHandle(void)
 void Task_Init(void)
 { 
     uart_Initialize();
+
+		memcpy(uart0_info.send_buffer, (void *)"Booted", 6);
+		
+		uart0_info.send_len = 6;
+		
+		uart0_TransmitData(uart0_info.send_buffer, uart0_info.send_len);
+		uart0_RecvData();
+		
+		
 }
 
 

+ 10 - 4
User/protocol.c

@@ -3,17 +3,23 @@
 #include "cfg.h"
 #include "uart.h"
 
-uint32_t Firmware_Version[8] = {1, 1, 0, 0, 0, 0, 0, 20231214};
+
+#ifdef IS_BOOTLOADER
+uint32_t Firmware_Version[4] = {1, 0, 0, 20231214};
+#else
+uint32_t Firmware_Version[4] = {1, 1, 0, 20240108};
+#endif
+
 
 uint16_t Read_FirmwareVersion(uint8_t *pBuf, uint16_t buf_len)
 {
 	int i;
 	
-	if( buf_len < 32){
+	if( buf_len < 16){
 		return 0;
 	}
 	
-	for (i = 0; i < 8; ++i)
+	for (i = 0; i < 4; ++i)
 	{
 		pBuf[i * 4] = (Firmware_Version[i] >> 24) & 0xff;
 		pBuf[i * 4 + 1] = (Firmware_Version[i] >> 16) & 0xff;
@@ -21,7 +27,7 @@ uint16_t Read_FirmwareVersion(uint8_t *pBuf, uint16_t buf_len)
 		pBuf[i * 4 + 3] = (Firmware_Version[i] >> 0) & 0xff;
 	}
 	
-	return 32;
+	return 16;
 	
 }
 

+ 2 - 0
User/protocol.h

@@ -53,6 +53,8 @@ extern "C" {
 #define Cmd_LockStatus    		 (0x0020)
 //#define Cmd_FirmwareVersion    (0x0000)
 
+#define Cmd_IapUpgrade    		 (0xAABB)
+
 #define MODBUS_FUNC_READ				(0x03)
 #define MODBUS_FUNC_WRITE				(0x06)