Kaynağa Gözat

首次推送

kinve 4 yıl önce
işleme
b5cf20edad
53 değiştirilmiş dosya ile 6494 ekleme ve 0 silme
  1. 1 0
      README.md
  2. 149 0
      stm32 Multi/Keys.c
  3. 88 0
      stm32 Multi/Keys.h
  4. 207 0
      stm32 Multi/Led.c
  5. 46 0
      stm32 Multi/Led.h
  6. 213 0
      stm32 Multi/TypeDef.h
  7. 215 0
      stm32 Multi/multi_button.c
  8. 65 0
      stm32 Multi/multi_button.h
  9. 169 0
      stm32 Multi/multi_led.c
  10. 65 0
      stm32 Multi/multi_led.h
  11. 108 0
      stm32 Multi/multi_timer.c
  12. 42 0
      stm32 Multi/multi_timer.h
  13. 151 0
      stm32 Multi/smg.c
  14. 20 0
      stm32 Multi/smg.h
  15. 431 0
      stm32 动态数组/ArrauList/ArrayList.c
  16. 33 0
      stm32 动态数组/ArrauList/ArrayList.h
  17. 21 0
      stm32 动态数组/ArrauList/PackMap.c
  18. 20 0
      stm32 动态数组/ArrauList/PackMap.h
  19. 22 0
      stm32 动态数组/ArrauList/SystemMemory.c
  20. 24 0
      stm32 动态数组/ArrauList/SystemMemory.h
  21. 435 0
      stm32 动态数组/ArrayList.c
  22. 39 0
      stm32 动态数组/ArrayList.h
  23. 546 0
      stm32 动态数组/方式2/ArrayLib.c
  24. 168 0
      stm32 动态数组/方式2/ArrayLib.h
  25. 59 0
      stm32 消息队列/FifoQueue.c
  26. 30 0
      stm32 消息队列/FifoQueue.h
  27. 431 0
      stm32 通用函数/Common/ArrauList/ArrayList.c
  28. 33 0
      stm32 通用函数/Common/ArrauList/ArrayList.h
  29. 21 0
      stm32 通用函数/Common/ArrauList/PackMap.c
  30. 20 0
      stm32 通用函数/Common/ArrauList/PackMap.h
  31. 22 0
      stm32 通用函数/Common/ArrauList/SystemMemory.c
  32. 24 0
      stm32 通用函数/Common/ArrauList/SystemMemory.h
  33. 435 0
      stm32 通用函数/Common/ArrayList.c
  34. 39 0
      stm32 通用函数/Common/ArrayList.h
  35. 192 0
      stm32 通用函数/Common/Delay.c
  36. 68 0
      stm32 通用函数/Common/Delay.h
  37. 94 0
      stm32 通用函数/Common/Delay1.c
  38. 40 0
      stm32 通用函数/Common/Delay1.h
  39. 108 0
      stm32 通用函数/Common/EE_Dataset.c
  40. 49 0
      stm32 通用函数/Common/EE_Dataset.h
  41. 59 0
      stm32 通用函数/Common/FifoQueue.c
  42. 30 0
      stm32 通用函数/Common/FifoQueue.h
  43. 667 0
      stm32 通用函数/Common/FlashEE.c
  44. 59 0
      stm32 通用函数/Common/FlashEE.h
  45. 76 0
      stm32 通用函数/Common/Menu.c
  46. 12 0
      stm32 通用函数/Common/Menu.h
  47. 70 0
      stm32 通用函数/Common/MyMach.h
  48. 225 0
      stm32 通用函数/Common/ProgramRun.c
  49. 54 0
      stm32 通用函数/Common/ProgramRun.h
  50. 56 0
      stm32 通用函数/Common/Stmflash.h
  51. 155 0
      stm32 通用函数/Common/stmflash.c
  52. 18 0
      stm32 通用函数/Common/sys.c
  53. 70 0
      stm32 通用函数/Common/sys.h

+ 1 - 0
README.md

@@ -0,0 +1 @@
+STM32 通用封装功能

+ 149 - 0
stm32 Multi/Keys.c

@@ -0,0 +1,149 @@
+#include "include.h"
+#include "multi_button.h"
+
+pKeyState GROUP_KEY = {0};
+
+struct Button kCS,kJS;
+
+void ScanKey ( void )	//5ms
+{
+	button_ticks();
+}
+
+uint8_t Read_CS_GPIO() 
+{
+	return (bool)GET_KEY_CS;
+}
+
+uint8_t Read_JS_GPIO() 
+{
+	return (bool)GET_KEY_JS;
+}
+
+void Keys_SIG(void* btn)
+{
+	int event;
+
+	if(btn == &kCS){
+		event = get_button_event(btn);
+		switch(event)
+		{
+			case SINGLE_CLICK:		GROUP_KEY.CS=1;			break;		//测试
+			case LONG_RRESS_START:	GROUP_KEY.CS_L=1;		break;		//长按测试
+		}
+	}else if(btn == &kJS){
+		event = get_button_event(btn);
+		switch(event)
+		{
+			case SINGLE_CLICK:		GROUP_KEY.JS=1; 		break;		//计时
+			case LONG_RRESS_START:	GROUP_KEY.JS_L=1;		break;		//长按计时
+		}
+	}
+}
+
+void Keys_Init(void)
+{
+	button_init_Head();
+	button_init(&kCS, Read_CS_GPIO, 0);		//低电平触发
+	button_init(&kJS, Read_JS_GPIO, 0);		//低电平触发
+	
+	button_attach(&kCS, SINGLE_CLICK,     		Keys_SIG);
+	button_attach(&kCS, LONG_RRESS_START,     	Keys_SIG);
+	button_attach(&kJS, SINGLE_CLICK,     		Keys_SIG);
+	button_attach(&kJS, LONG_RRESS_START,     	Keys_SIG);
+	
+	button_start(&kCS);
+	button_start(&kJS);
+	
+	//make the timer invoking the button_ticks() interval 5ms.
+	//This function is implemented by yourself.
+	//__timer_start(button_ticks, 0, 5); 
+
+}
+
+static bool JS_On = true;
+void Key_JS_Hanlder ( void* btn )
+{
+#if 0
+
+    if ( SysRunType == SysRun_Norm )
+    {
+        StopBeep();
+    }
+#endif		
+	Smg_Display_Next();
+}
+
+void Key_CS_Hanlder ( void* btn )
+{
+#if 0
+    switch ( SysRunType )
+    {
+    case SysRun_Norm:
+        SysRunType = SysRun_Test;
+        sysLed_Set ( LED_MODE_TEST_RUN );
+        break;
+
+    case SysRun_Test:
+        SysRunType = SysRun_Norm;
+        sysLed_Set ( LED_MODE_WORK_RUN );
+        break;
+    }
+
+    Fas_Param_Of_Mode_Change ( SysRunType );
+#endif	
+}
+
+void Key_JS_Long_Hanlder ( void* btn )
+{
+    //WaitKey_GotoTest();
+    JS_On = !JS_On;
+	Smg_Display_OnOff(JS_On);
+}
+
+void Key_CS_Long_Hanlder ( void* btn )
+{
+}
+
+void WaitKey_GotoTest ( void )
+{
+#if 0
+
+    static bool Can_enter_Factory = TRUE;
+
+    if ( sys_rtc_time <= 10 )				//启动10秒内,按下Key_CS
+    {
+        if ( Can_enter_Factory )
+        {
+            Can_enter_Factory = FALSE;
+            SysRunType = SysRun_Test;
+            sysLed_Set ( LED_MODE_TEST_RUN );
+        }
+    }
+#endif	
+}
+
+void Keys_All_Handler(void)
+{
+	if(GROUP_KEY.CS)
+	{
+		GROUP_KEY.CS = 0;
+		Key_CS_Hanlder(&kCS);
+	}
+	if(GROUP_KEY.JS)
+	{
+		GROUP_KEY.JS = 0;
+		Key_JS_Hanlder(&kJS);
+	}
+	if(GROUP_KEY.CS_L)
+	{
+		GROUP_KEY.CS_L = 0;
+		Key_CS_Long_Hanlder(&kCS);
+	}	
+	if(GROUP_KEY.JS_L)
+	{
+		GROUP_KEY.JS_L = 0;
+		Key_JS_Long_Hanlder(&kJS);
+	}	
+}
+

+ 88 - 0
stm32 Multi/Keys.h

@@ -0,0 +1,88 @@
+#ifndef __KEYS_H
+#define __KEYS_H
+
+#include "TypeDef.h"
+
+/* 类型定义 ------------------------------------------------------------------*/
+typedef struct aa
+{
+    u8 CS: 1 ;
+    u8 JS: 1 ;
+    u8 CS_L: 1 ;
+    u8 JS_L: 1 ;
+    u8 e: 1 ;
+    u8 f: 1 ;
+    u8 g: 1 ;
+    u8 h: 1 ;
+} pKeyState ;
+
+extern pKeyState GROUP_KEY;
+
+
+enum KEY_SIG_DEF
+{
+    KEY_CLICK_CS,		//测试
+    KEY_CLICK_JS,		//计时
+	KEY_LONG_CS, 		//长按测试
+	KEY_LONG_JS, 		//长按计时
+    KEYSIG_MAX
+};
+
+/* 常量定义 ------------------------------------------------------------------*/
+enum KEY_DEF
+{
+    KEY_CS,		//测试
+    KEY_JS,		//计时
+    KEY_MAX
+};
+
+enum KeyState
+{
+    KEY_UP_FLAG,
+    KEY_DOWN_FLAG
+};
+
+enum KeyRes
+{
+    NO_KEY_FLAG,
+    ONE_HITS_KEY_FLAG,
+    TWO_HITS_KEY_FLAG,
+    THREE_HITS_KEY_FLAG,
+    LONG_KEY_FLAG,
+    MAX_KEY_FLAG
+};
+
+typedef struct
+{
+    unsigned char ID;			//按键号
+    unsigned char State;		//状态
+    unsigned short DnTime;		//down计时
+    unsigned short UpTime;		//up计时
+    unsigned char HitNum;		//单击计次
+    unsigned char Res;			//最后结果
+} pKey;
+
+/* 宏定义 --------------------------------------------------------------------*/
+/*
+短按: 1次,80ms~280ms(down)
+
+长按: 1200ms~>>(down)
+
+双击: 2次,80ms~280ms(down) -> 0ms~280ms(up) -> 80ms~280ms(down)
+*/
+#define KEY_TIME 		(20)				//10ms任务,扫描一次
+#define CANCEL_KEY_DOWN (60/KEY_TIME)		//按下<80ms,取消
+#define SHORT_KEY_DOWN 	(320/KEY_TIME)		//短按80ms-280ms
+#define LONG_KEY_DOWN 	(3000/KEY_TIME)     //长按3s
+#define CANCEL_KEY_UP 	(80/KEY_TIME)		//松开超过80ms,取消
+#define MAL_KEY_DOWN    (10000/KEY_TIME)	//长按超过10s,故障
+
+#define HANDLE_KEY_TIME (100/KEY_TIME)		//按键处理时间
+
+
+/* 函数定义------------------------------------------------------------------ */
+extern void ScanKey ( void );
+extern void Keys_Init( void );
+extern void Keys_All_Handler(void);
+
+#endif

+ 207 - 0
stm32 Multi/Led.c

@@ -0,0 +1,207 @@
+#include "multi_led.h"
+#include "include.h"
+
+struct Led lRun,lAla,lErr;
+
+void ScanLed ( void )		//100ms
+{
+	led_ticks();
+}
+
+void Set_RUN_GPIO(bool state) 
+{
+	SET_RUN_LED(state);
+}
+void Set_ALA_GPIO(bool state) 
+{
+	SET_ALA_LED(state);
+}
+void Set_ERR_GPIO(bool state) 
+{
+	SET_ERR_LED(state);
+}
+
+
+void Leds_Init(void)
+{
+	led_init_Head();
+	led_init(&lRun, Set_RUN_GPIO);		
+	led_init(&lAla, Set_ALA_GPIO);		
+	led_init(&lErr, Set_ERR_GPIO);		
+
+	led_set_state(&lRun, LED_WORK, 	0, 	LED_STATE2_FLASH, 	1000, 	0, 	0 );
+	led_set_state(&lAla, LED_TX, 	0, 	LED_STATE0_OFF, 	0, 		0, 	0 );
+	led_set_state(&lErr, LED_GZ, 	0, 	LED_STATE0_OFF, 	0, 		0, 	0 );
+	
+	led_start(&lRun);
+	led_start(&lAla);
+	led_start(&lErr);	
+	//make the timer invoking the button_ticks() interval 5ms.
+	//This function is implemented by yourself.
+	//__timer_start(button_ticks, 0, 100); 
+
+}
+
+
+/*
+通用:WorkLed闪动
+通信正常: TxLed 亮 (其他功能可共用), GZLed 灭; 传输TxLed 闪
+通信故障: TxLed 灭, GZLed 亮:电源故障
+通信故障,100s声光提示,声可消除;光保持到,无线模块复位,或故障消除
+*/
+pFault FAULT_State = {.State = Bin ( 00 ) };
+pFault FAULT_StateOld = {.State = Bin ( 00 ) };
+bool FAULT_State_NoChange = true;
+u8 sysLed_Pretreat ( u8 State )	//预处理一下:故障和恢复
+{
+    u8 Res = State;
+
+    //3G/电源 故障与恢复
+    switch ( State )
+    {
+    case LED_MODE_WORK_3G_OK:
+        FAULT_State.Flag._3g = 0;
+        break;
+
+    case LED_MODE_WORK_3G_FAIL:
+        FAULT_State.Flag._3g = 1;
+        break;
+
+    case LED_MODE_WORK_DC_BAT_OK:
+        FAULT_State.Flag._dc_bat = 0;
+        break;
+
+    case LED_MODE_WORK_DC_BAT_FAIL:
+        FAULT_State.Flag._dc_bat = 1;
+        break;
+
+    default:
+        return State;
+    }
+
+    if ( FAULT_State.Flag._3g )				Res = LED_MODE_WORK_3G_FAIL;		//最优先3G
+    else if ( FAULT_State.Flag._dc_bat )	Res = LED_MODE_WORK_DC_BAT_FAIL;
+
+    FAULT_State_NoChange = false;
+
+    return Res;
+}
+
+
+
+#if 0
+//灯的控制和蜂鸣器的控制
+void sysLed_Set ( u8 State )
+{
+    State = sysLed_Pretreat ( State );
+
+    switch ( SysRunType )
+    {
+    case SysRun_Norm:
+        switch ( State )
+        {
+        case LED_MODE_WORK_RUN:
+            LedDoing ( LED_WORK, 0,	LED_STATE2_FLASH,	1000,	0,	0 );	//单位100ms: 500ms开,500ms关
+
+            if ( FAULT_State_NoChange )					//初始化状态
+            {
+                LedDoing ( LED_TX,	0,	LED_STATE0_OFF, 	0,		0,	0 );
+                LedDoing ( LED_GZ,	0,	LED_STATE0_OFF,		0,		0,	0 );
+            }
+            else if ( FAULT_State.Flag._3g )			//1.3g故障, 2.3g+电源故障
+            {
+                LedDoing ( LED_TX,	0,	LED_STATE0_OFF, 	0,		0,	0 );
+                LedDoing ( LED_GZ,	0,	LED_STATE1_ON,		0,		0,	0 );
+            }
+            else if ( FAULT_State.Flag._dc_bat )		//3g正常, 电源故障
+            {
+                LedDoing ( LED_TX,	0,	LED_STATE1_ON,		0,		0,	0 );
+                LedDoing ( LED_GZ,	0,	LED_STATE2_FLASH,	500,	0,	0 );
+            }
+            else										//3g正常, 电源正常
+            {
+                LedDoing ( LED_TX,	0,	LED_STATE1_ON,		0,		0,	0 );
+                LedDoing ( LED_GZ,	0,	LED_STATE0_OFF, 	0,		0,	0 );
+            }
+
+            break;
+
+        case LED_MODE_WORK_3G_OK:
+            LedDoing ( LED_TX,	0,	LED_STATE1_ON,		0,		0,	0 );
+            LedDoing ( LED_GZ,	0,	LED_STATE0_OFF, 	0,		0,	0 );
+            break;
+
+        case LED_MODE_WORK_3G_FAIL:
+            LedDoing ( LED_TX,	0,	LED_STATE0_OFF, 	0,		0,	0 );
+            LedDoing ( LED_GZ,	0,	LED_STATE1_ON,		0,		0,	0 );
+            break;
+
+        case LED_MODE_WORK_DC_BAT_OK:
+            LedDoing ( LED_GZ,	0,	LED_STATE0_OFF, 	0,		0,	0 );
+            break;
+
+        case LED_MODE_WORK_DC_BAT_FAIL:
+            LedDoing ( LED_GZ,	0,	LED_STATE2_FLASH,	500,	0,	0 );
+            break;
+
+        case LED_MODE_WORK_3G_TX:
+            LedDoing ( LED_TX,	0,	LED_STATE5_FLASH_DELAY, 100, 200, 1 ); //100ms开,开关1次
+            break;
+			
+		case LED_MODE_WORK_3G_RX:
+			LedDoing ( LED_TX,	0,	LED_STATE5_FLASH_DELAY, 100, 400, 1 ); //100ms开,开关2次
+			break;
+
+        case LED_MODE_TEST_RUN:
+        case LED_MODE_TEST_KGL_IN:
+            break;
+        }
+
+        break;
+
+    case SysRun_Test:
+        switch ( State )
+        {
+        case LED_MODE_WORK_RUN:
+            break;
+
+        case LED_MODE_WORK_3G_OK:
+            LedDoing ( LED_TX,	0,	LED_STATE1_ON,		0,		0,	0 );
+            break;
+
+        case LED_MODE_WORK_3G_FAIL:
+            LedDoing ( LED_TX,	0,	LED_STATE0_OFF, 	0,		0,	0 );
+            break;
+
+        case LED_MODE_WORK_DC_BAT_OK:
+        case LED_MODE_WORK_DC_BAT_FAIL:
+        case LED_MODE_WORK_3G_TX:
+		case LED_MODE_WORK_3G_RX:	
+            break;
+
+        case LED_MODE_TEST_RUN:
+            LedDoing ( LED_WORK, 0,	LED_STATE2_FLASH,	200,	0,	0 );
+            LedDoing ( LED_GZ,	0,	LED_STATE2_FLASH,	200,	0,	0 );
+            LedDoing ( LED_TX,	0,	LED_STATE2_FLASH,	200,	0,	0 );
+            break;
+
+        case LED_MODE_TEST_KGL_IN:
+            LedDoing ( LED_GZ,	0,	LED_STATE4_NOT, 	0,		0,	0 );
+            break;
+        }
+
+        break;
+    }
+
+    if ( SysRunType == SysRun_Norm )
+    {
+        if ( FAULT_StateOld.State != FAULT_State.State )                  
+        {
+            FAULT_StateOld.State = FAULT_State.State;
+            ( FAULT_State.State == 0 ) ? StopBeep() : StartBeep ( WARMING_BEEP );    //判断是否是正常状态,是正常则停,告警则响
+        }
+    }
+
+}
+
+#endif

+ 46 - 0
stm32 Multi/Led.h

@@ -0,0 +1,46 @@
+#ifndef __LED_H
+#define __LED_H
+/* 包含的头文件---------------------------------------------------------------*/
+#include "TypeDef.h"
+/* 类型定义 ------------------------------------------------------------------*/
+typedef union
+{
+    u8 State;
+    struct
+    {
+        u8 _3g: 1 ;			//3g故障
+        u8 _dc_bat: 1 ;		//电源或电池故障
+        u8 c: 1 ;
+        u8 d: 1 ;
+        u8 e: 1 ;
+        u8 f: 1 ;
+        u8 g: 1 ;
+        u8 h: 1 ;
+    } Flag;
+} pFault ;
+
+/* 常量定义 ------------------------------------------------------------------*/
+enum
+{
+    LED_MODE_WORK_RUN,				//正常
+    LED_MODE_WORK_3G_OK,			//正常:3G恢复
+    LED_MODE_WORK_3G_FAIL,			//正常:3G故障
+    LED_MODE_WORK_DC_BAT_OK, 		//正常:电源恢复
+    LED_MODE_WORK_DC_BAT_FAIL, 		//正常:电源故障
+    LED_MODE_WORK_3G_TX,			//正常:3G=传输..
+    LED_MODE_WORK_3G_RX,
+    LED_MODE_TEST_RUN,				//测试
+    LED_MODE_TEST_KGL_IN,			//测试:输入
+    LED_MODE_MAX,
+};
+
+/* 宏定义 --------------------------------------------------------------------*/
+extern pFault FAULT_State;
+
+/* 函数定义------------------------------------------------------------------ */
+void ScanLed ( void ); //100ms
+void Leds_Init(void);
+void sysLed_Set ( u8 State );
+
+#endif
+

+ 213 - 0
stm32 Multi/TypeDef.h

@@ -0,0 +1,213 @@
+#ifndef __TYPEDEF_H__
+#define __TYPEDEF_H__
+
+
+#define  nop()  __no_operation()
+//#define                        __disable_interrupt()
+//#define                       __enable_interrupt()
+#define  idata  __idata
+#define  pdata  __pdata
+#define  xdata  __xdata
+#define  code   __code
+#define  data   __data
+#define  idata  __idata
+#define  bit    __bit
+#define  bdata  __bdata
+#define  interrupt __interrupt
+
+#ifndef u8
+#define u8 uint8
+#endif
+#ifndef s8
+#define s8 int8
+#endif
+#ifndef u16
+#define u16 uint16
+#endif
+#ifndef u32
+#define u32 uint32
+#endif
+
+#ifndef uint8_t
+#define uint8_t uint8
+#endif
+#ifndef uint16_t
+#define uint16_t uint16
+#endif
+#ifndef uint32_t
+#define uint32_t uint32
+#endif
+
+#define CPU_BIT  8
+
+#if CPU_BIT == 8
+typedef unsigned char data  udata8;
+//-------------------------------
+typedef unsigned char       uint8;
+//------------------------------
+typedef unsigned char xdata uint8x;
+//------------------------------
+typedef signed char         int8;
+//------------------------------
+typedef signed char xdata   int8x;
+//------------------------------
+typedef unsigned int        uint16;
+//------------------------------
+typedef unsigned int xdata  uint16x;
+//------------------------------
+typedef signed int          int16;
+//------------------------------
+typedef signed int xdata    int16x;
+//------------------------------
+typedef unsigned long       uint32;
+//------------------------------
+typedef unsigned long xdata uint32x;
+//------------------------------
+typedef signed long         int32;
+//------------------------------
+typedef signed long xdata   int32x;
+//------------------------------
+
+typedef void* Object;           //定义任意类型为Object
+typedef const char * string;    //定义字符串类型
+
+
+//------------------------------
+//typedef signed long long  int64;
+
+//typedef unsigned long long uint64;
+
+//------------------------------
+typedef union
+{
+    uint16 word;
+    uint8  byte[2];
+} Word16;
+
+typedef union
+{
+    uint32 lword;
+    uint16 word[2];
+    uint8  byte[4];
+} Word32;
+
+typedef union
+{
+    Word32 word;
+    uint8  byte;
+} Word40;
+
+
+struct bit_def
+{
+    char    b0: 1;
+    char    b1: 1;
+    char    b2: 1;
+    char    b3: 1;
+    char    b4: 1;
+    char    b5: 1;
+    char    b6: 1;
+    char    b7: 1;
+};
+
+union byte_def
+{
+    struct bit_def bite;
+    unsigned char  byte;
+};
+
+#ifndef true
+#define true 1==1
+#endif
+
+#ifndef false
+#define false 0==1
+#endif
+
+/* another normal use */
+#define     BIT0        0x01
+#define     BIT1        0x02
+#define     BIT2        0x04
+#define     BIT3        0x08
+#define     BIT4        0x10
+#define     BIT5        0x20
+#define     BIT6        0x40
+#define     BIT7        0x80
+
+#define     EBIT0       0x0001
+#define     EBIT1       0x0002
+#define     EBIT2       0x0004
+#define     EBIT3       0x0008
+#define     EBIT4       0x0010
+#define     EBIT5       0x0020
+#define     EBIT6       0x0040
+#define     EBIT7       0x0080
+#define     EBIT8       0x0100
+#define     EBIT9       0x0200
+#define     EBIT10      0x0400
+#define     EBIT11      0x0800
+#define     EBIT12      0x1000
+#define     EBIT13      0x2000
+#define     EBIT14      0x4000
+#define     EBIT15      0x8000
+#define     EBIT16      0x00010000
+#define     EBIT17      0x00020000
+#define     EBIT18      0x00040000
+#define     EBIT19      0x00080000
+#define     EBIT20      0x00100000
+#define     EBIT21      0x00200000
+#define     EBIT22      0x00400000
+#define     EBIT23      0x00800000
+#define     EBIT24      0x01000000
+#define     EBIT25      0x02000000
+#define     EBIT26      0x04000000
+#define     EBIT27      0x08000000
+#define     EBIT28      0x10000000
+#define     EBIT29      0x20000000
+#define     EBIT30      0x40000000
+#define     EBIT31      0x80000000
+
+#ifdef __MAIN__
+const uint8 code BIT8MAP[] =
+{
+    BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7
+};
+
+const uint32 code BIT32MAP[] =
+{
+    EBIT0, EBIT1, EBIT2, EBIT3, EBIT4, EBIT5, EBIT6, EBIT7,
+    EBIT8, EBIT9, EBIT10, EBIT11, EBIT12, EBIT13, EBIT14, EBIT15,
+    EBIT16, EBIT17, EBIT18, EBIT19, EBIT20, EBIT21, EBIT22, EBIT23,
+    EBIT24, EBIT25, EBIT26, EBIT27, EBIT28, EBIT29, EBIT30, EBIT31,
+};
+#else
+extern const uint8 code  BIT8MAP[];
+extern const uint32 code BIT32MAP[];
+#endif
+
+#define     NONE    0
+
+
+
+#define dim(x) (sizeof(x)/sizeof(x[0]))     //求一个数组的维度
+
+
+#define ERR_OK      (uint8)0
+#define ERR_VAR     (uint8)1
+#define ERR_FT      (uint8)2
+
+#define SA_VAR_CHECK(X)  {if(X){return ERR_VAR;}}
+#define SA_OK()      {return ERR_OK;}
+
+
+
+
+#endif
+
+
+
+
+
+
+
+#endif

+ 215 - 0
stm32 Multi/multi_button.c

@@ -0,0 +1,215 @@
+
+/*
+ * Copyright (c) 2016 Zibin Zheng <[email protected]>
+ * All rights reserved
+ */
+
+#include "multi_button.h"
+
+#define EVENT_CB(ev)   if(handle->cb[ev])handle->cb[ev]((Button*)handle)
+	
+//button handle list head.
+static struct Button* head_handle = NULL;
+
+/**
+  * @brief  Initializes the button struct handle.
+  * @param  handle: the button handle strcut.
+  * @param  pin_level: read the HAL GPIO of the connet button level.
+  * @param  active_level: pressed GPIO level.
+  * @retval None
+  */
+void button_init_Head(void)
+{
+	head_handle = NULL;
+}
+void button_init(struct Button* handle, uint8_t(*pin_level)(), uint8_t active_level)
+{
+	memset(handle, 0, sizeof(struct Button));
+	handle->event = (uint8_t)NONE_PRESS;
+	handle->hal_button_Level = pin_level;
+	handle->button_level = handle->hal_button_Level();
+	handle->active_level = active_level;
+}
+
+/**
+  * @brief  Attach the button event callback function.
+  * @param  handle: the button handle strcut.
+  * @param  event: trigger event type.
+  * @param  cb: callback function.
+  * @retval None
+  */
+void button_attach(struct Button* handle, PressEvent event, BtnCallback cb)
+{
+	handle->cb[event] = cb;
+}
+
+/**
+  * @brief  Inquire the button event happen.
+  * @param  handle: the button handle strcut.
+  * @retval button event.
+  */
+PressEvent get_button_event(struct Button* handle)
+{
+	return (PressEvent)(handle->event);
+}
+
+/**
+  * @brief  Button driver core function, driver state machine.
+  * @param  handle: the button handle strcut.
+  * @retval None
+  */
+void button_handler(struct Button* handle)
+{
+	uint8_t read_gpio_level = handle->hal_button_Level();
+
+	//ticks counter working..
+	if((handle->state) > 0) handle->ticks++;
+
+	/*------------button debounce handle---------------*/
+	if(read_gpio_level != handle->button_level) { //not equal to prev one
+		//continue read 3 times same new level change
+		if(++(handle->debounce_cnt) >= DEBOUNCE_TICKS) {
+			handle->button_level = read_gpio_level;
+			handle->debounce_cnt = 0;
+		}
+	} else { //leved not change ,counter reset.
+		handle->debounce_cnt = 0;
+	}
+
+	/*-----------------State machine-------------------*/
+	switch (handle->state) {
+	case 0:
+		if(handle->button_level == handle->active_level) {	//start press down
+			handle->event = (uint8_t)PRESS_DOWN;
+			EVENT_CB(PRESS_DOWN);
+			handle->ticks = 0;
+			handle->repeat = 1;
+			handle->state = 1;
+		} else {
+			handle->event = (uint8_t)NONE_PRESS;
+		}
+		break;
+
+	case 1:
+		if(handle->button_level != handle->active_level) { //released press up
+			handle->event = (uint8_t)PRESS_UP;
+			EVENT_CB(PRESS_UP);
+			handle->ticks = 0;
+			handle->state = 2;
+
+		} else if(handle->ticks > LONG_TICKS) {
+			handle->event = (uint8_t)LONG_RRESS_START;
+			EVENT_CB(LONG_RRESS_START);
+			handle->state = 5;
+		}
+		break;
+
+	case 2:
+		if(handle->button_level == handle->active_level) { //press down again
+			handle->event = (uint8_t)PRESS_DOWN;
+			EVENT_CB(PRESS_DOWN);
+			handle->repeat++;
+			if(handle->repeat == 2) {
+				EVENT_CB(DOUBLE_CLICK); // repeat hit
+			} 
+			EVENT_CB(PRESS_REPEAT); // repeat hit
+			handle->ticks = 0;
+			handle->state = 3;
+		} else if(handle->ticks > SHORT_TICKS) { //released timeout
+			if(handle->repeat == 1) {
+				handle->event = (uint8_t)SINGLE_CLICK;
+				EVENT_CB(SINGLE_CLICK);
+			} else if(handle->repeat == 2) {
+				handle->event = (uint8_t)DOUBLE_CLICK;
+			}
+			handle->state = 0;
+		}
+		break;
+
+	case 3:
+		if(handle->button_level != handle->active_level) { //released press up
+			handle->event = (uint8_t)PRESS_UP;
+			EVENT_CB(PRESS_UP);
+			if(handle->ticks < SHORT_TICKS) {
+				handle->ticks = 0;
+				handle->state = 2; //repeat press
+			} else {
+				handle->state = 0;
+			}
+		}
+		break;
+
+	case 5:
+		if(handle->button_level == handle->active_level) {
+#ifndef LONG_TICKS_TRIG
+			//continue hold trigger
+			handle->event = (uint8_t)LONG_PRESS_HOLD;
+			EVENT_CB(LONG_PRESS_HOLD);
+#else
+			//长按后tick每周期触发
+			//增加长按周期触发
+			if(handle->ticks > (LONG_TICKS+LONG_TICKS_TRIG))
+			{
+				handle->ticks = LONG_TICKS;//重新启动
+				handle->event = (uint8_t)LONG_PRESS_HOLD;
+				EVENT_CB(LONG_PRESS_HOLD);
+			}
+#endif
+		} else { //releasd
+			handle->event = (uint8_t)PRESS_UP;
+			EVENT_CB(PRESS_UP);
+			handle->state = 0; //reset
+		}
+		break;
+	}
+}
+
+/**
+  * @brief  Start the button work, add the handle into work list.
+  * @param  handle: target handle strcut.
+  * @retval 0: succeed. -1: already exist.
+  */
+int button_start(struct Button* handle)
+{
+	struct Button* target = head_handle;
+	while(target) {
+		if(target == handle) return -1;	//already exist.
+		target = target->next;
+	}
+	handle->next = head_handle;
+	head_handle = handle;
+	return 0;
+}
+
+/**
+  * @brief  Stop the button work, remove the handle off work list.
+  * @param  handle: target handle strcut.
+  * @retval None
+  */
+void button_stop(struct Button* handle)
+{
+	struct Button** curr;
+	for(curr = &head_handle; *curr; ) {
+		struct Button* entry = *curr;
+		if (entry == handle) {
+			*curr = entry->next;
+//			free(entry);
+		} else {
+			curr = &entry->next;
+		}
+	}
+}
+
+/**
+  * @brief  background ticks, timer repeat invoking interval 5ms.
+  * @param  None.
+  * @retval None
+  */
+void button_ticks()
+{
+	struct Button* target;
+	for(target=head_handle; target; target=target->next) {
+		button_handler(target);
+	}
+}
+

+ 65 - 0
stm32 Multi/multi_button.h

@@ -0,0 +1,65 @@
+
+/*
+ * Copyright (c) 2016 Zibin Zheng <[email protected]>
+ * All rights reserved
+ */
+ 
+#ifndef _MULTI_BUTTON_H_
+#define _MULTI_BUTTON_H_
+
+//#include "stdint.h"
+#include "TypeDef.h"
+#include "string.h"
+
+//According to your need to modify the constants.
+#define TICKS_INTERVAL    6	//ms
+#define DEBOUNCE_TICKS    3	//MAX 8
+#define SHORT_TICKS       (120 /TICKS_INTERVAL)
+#define LONG_TICKS        (2000 /TICKS_INTERVAL)
+#define LONG_TICKS_TRIG   (120 /TICKS_INTERVAL)	//³¤°´,ÖÜÆÚÐÔµ÷ÓÃ
+
+
+typedef void (*BtnCallback)(void*);
+
+typedef enum {
+	PRESS_DOWN = 0,
+	PRESS_UP,
+	PRESS_REPEAT,
+	SINGLE_CLICK,
+	DOUBLE_CLICK,
+	LONG_RRESS_START,
+	LONG_PRESS_HOLD,
+	number_of_event,
+	NONE_PRESS
+}PressEvent;
+
+typedef struct Button {
+	uint16_t ticks;
+	uint8_t  repeat : 4;
+	uint8_t  event : 4;
+	uint8_t  state : 3;
+	uint8_t  debounce_cnt : 3; 
+	uint8_t  active_level : 1;
+	uint8_t  button_level : 1;
+	uint8_t  (*hal_button_Level)(void);
+	BtnCallback  cb[number_of_event];
+	struct Button* next;
+}Button;
+
+#ifdef __cplusplus  
+extern "C" {  
+#endif  
+
+void button_init_Head(void);
+void button_init(struct Button* handle, uint8_t(*pin_level)(), uint8_t active_level);
+void button_attach(struct Button* handle, PressEvent event, BtnCallback cb);
+PressEvent get_button_event(struct Button* handle);
+int  button_start(struct Button* handle);
+void button_stop(struct Button* handle);
+void button_ticks(void);
+
+#ifdef __cplusplus
+} 
+#endif
+
+#endif

+ 169 - 0
stm32 Multi/multi_led.c

@@ -0,0 +1,169 @@
+#include "multi_led.h"
+
+#define EVENT_CB(state)   if(handle->led_set_gpio) handle->led_set_gpio((bool)state)
+	
+//button handle list head.
+static struct Led* head_handle = NULL;
+
+/**
+  * @brief  Initializes the button struct handle.
+  * @param  handle: the button handle strcut.
+  * @param  pin_level: read the HAL GPIO of the connet button level.
+  * @param  active_level: pressed GPIO level.
+  * @retval None
+  */
+void led_init_Head(void)
+{
+	head_handle = NULL;
+}
+void led_init(struct Led* handle, LedCallback led_set_gpio)
+{
+	memset(handle, 0, sizeof(struct Led));
+	handle->led_set_gpio = led_set_gpio;
+}
+
+void led_set_state(struct Led* handle, u8 ID, u8 OnOff, u8 Type, u16 FlashTime, u16 CloseTime, bool Res )
+{
+	if ( Type < LED_STATE_MAX )
+	{
+		handle->Play.ID = ID;
+		handle->Play.Type = Type;
+
+		if ( Type != LED_STATE4_NOT )
+		{
+			handle->Play.OnOff = OnOff;
+		}
+
+		handle->Play.FlashTime = FlashTime;
+		handle->Play.CloseTime = CloseTime;
+		handle->Play.Count = 0;
+		handle->Play.Res = Res;
+	}
+}
+
+
+/**
+  * @brief  Button driver core function, driver state machine.
+  * @param  handle: the button handle strcut.
+  * @retval None
+  */
+void led_handler(struct Led* handle)
+{
+    switch ( handle->Play.Type )
+    {
+    case LED_STATE0_OFF: 	//关
+    	EVENT_CB(false);
+        handle->Play.Type = LED_STATE_MAX;
+        break;
+
+    case LED_STATE1_ON: 	//开
+    	EVENT_CB(true);
+        handle->Play.Type = LED_STATE_MAX;
+        break;
+
+    case LED_STATE2_FLASH: //闪烁
+        handle->Play.Count += LED_SCAN;
+
+        if ( handle->Play.Count >= handle->Play.FlashTime )
+        {
+            handle->Play.Count = 0;
+			EVENT_CB(handle->Play.OnOff);
+			handle->Play.OnOff = !handle->Play.OnOff;			
+        }
+
+        break;
+
+    case LED_STATE3_CONUT: //定时开关
+        if ( handle->Play.CloseTime > 0 )
+        {
+            handle->Play.CloseTime -= LED_SCAN;
+			EVENT_CB(handle->Play.Res);	//开始
+        }
+        else
+        {
+            handle->Play.CloseTime = 0;
+			EVENT_CB(!handle->Play.Res);	//结束,与结果相反
+            handle->Play.Type = LED_STATE_MAX;
+        }
+
+        break;
+
+    case LED_STATE4_NOT: 	//取反
+    	EVENT_CB(!handle->Play.OnOff);
+        handle->Play.Type = LED_STATE_MAX;
+        break;
+
+    case LED_STATE5_FLASH_DELAY: 	//闪烁,延时关闭
+        if ( handle->Play.CloseTime > 0 )
+        {
+            handle->Play.CloseTime -= LED_SCAN;
+            //闪烁
+            handle->Play.Count += LED_SCAN;		//开始
+
+            if ( handle->Play.Count >= handle->Play.FlashTime )
+            {
+                handle->Play.Count = 0;
+				EVENT_CB(handle->Play.OnOff);
+				handle->Play.OnOff = !handle->Play.OnOff;			
+            }
+        }
+        else
+        {
+            handle->Play.CloseTime = 0;
+			EVENT_CB(handle->Play.Res);	//结束,与结果
+            handle->Play.Type = LED_STATE_MAX;
+        }
+
+        break;
+    }
+}
+
+/**
+  * @brief  Start the button work, add the handle into work list.
+  * @param  handle: target handle strcut.
+  * @retval 0: succeed. -1: already exist.
+  */
+int led_start(struct Led* handle)
+{
+	struct Led* target = head_handle;
+	while(target) {
+		if(target == handle) return -1;	//already exist.
+		target = target->next;
+	}
+	handle->next = head_handle;
+	head_handle = handle;
+	return 0;
+}
+
+/**
+  * @brief  Stop the button work, remove the handle off work list.
+  * @param  handle: target handle strcut.
+  * @retval None
+  */
+void led_stop(struct Led* handle)
+{
+	struct Led** curr;
+	for(curr = &head_handle; *curr; ) {
+		struct Led* entry = *curr;
+		if (entry == handle) {
+			*curr = entry->next;
+//			free(entry);
+		} else {
+			curr = &entry->next;
+		}
+	}
+}
+
+/**
+  * @brief  background ticks, timer repeat invoking interval 5ms.
+  * @param  None.
+  * @retval None
+  */
+void led_ticks()
+{
+	struct Led* target;
+	for(target=head_handle; target; target=target->next) {
+		led_handler(target);
+	}
+}
+

+ 65 - 0
stm32 Multi/multi_led.h

@@ -0,0 +1,65 @@
+#ifndef _MULTI_LED_H_
+#define _MULTI_LED_H_
+
+//#include "stdint.h"
+#include "TypeDef.h"
+#include <string.h>
+#include <stdbool.h>
+
+//According to your need to modify the constants.
+#define LED_SCAN    100
+
+typedef void (*LedCallback)(bool);
+
+enum LED_NUM_DEF
+{
+    LED_WORK,
+    LED_TX,
+    LED_GZ,
+    LED_MAX
+};
+
+enum LED_STATE_DEF
+{
+    LED_STATE0_OFF,				//关
+    LED_STATE1_ON,				//开
+    LED_STATE2_FLASH,			//闪烁
+    LED_STATE3_CONUT,			//延时关闭
+    LED_STATE4_NOT,				//取反
+    LED_STATE5_FLASH_DELAY,		//闪烁,延时关闭
+    LED_STATE_MAX
+};
+
+typedef struct
+{
+    u8 ID;				//LED号
+    bool OnOff;			//状态
+    u8 Type;          	//类型:0.关 1.开 2.闪烁 3.定时开关
+    u16 FlashTime;      //闪烁时间
+    u16 CloseTime;      //关闭时间/闪烁占空比
+    u16 Count;    		//计时
+    bool Res;			//最后结果
+} pLed;
+
+typedef struct Led {
+	pLed Play;
+	LedCallback led_set_gpio;
+	struct Led* next;
+}Led;
+
+#ifdef __cplusplus  
+extern "C" {  
+#endif  
+
+void led_init_Head(void);
+void led_init(struct Led* handle, LedCallback led_set_gpio);
+void led_set_state(struct Led* handle, u8 ID, u8 OnOff, u8 Type, u16 FlashTime, u16 CloseTime, bool Res );
+int  led_start(struct Led* handle);
+void led_stop(struct Led* handle);
+void led_ticks(void);
+
+#ifdef __cplusplus
+} 
+#endif
+
+#endif

+ 108 - 0
stm32 Multi/multi_timer.c

@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2016 Zibin Zheng <[email protected]>
+ * All rights reserved
+ */
+
+#include "multi_timer.h"
+
+//timer handle list head.
+static struct Timer* head_handle = NULL;
+
+//Timer ticks
+//static uint32_t _timer_ticks = 0;
+static uint16_t _timer_ticks = 0;
+
+
+/**
+  * @brief  Initializes the timer struct handle.
+  * @param  handle: the timer handle strcut.
+  * @param  timeout_cb: timeout callback.
+  * @param  repeat: repeat interval time.
+  * @retval None
+  */
+void timer_init_Head(void)
+{
+	head_handle = NULL;
+}
+void timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat)
+{
+	// memset(handle, 0, sizeof(struct Timer));
+	handle->timeout_cb = timeout_cb;
+	handle->timeout = _timer_ticks + timeout;
+	handle->repeat = repeat;
+}
+
+void timer_app(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat)
+{
+	timer_init(handle, timeout_cb, timeout, repeat);
+	timer_start(handle);
+}
+/**
+  * @brief  Start the timer work, add the handle into work list.
+  * @param  btn: target handle strcut.
+  * @retval 0: succeed. -1: already exist.
+  */
+int timer_start(struct Timer* handle)
+{
+	struct Timer* target = head_handle;
+	while(target) {
+		if(target == handle) return -1;	//already exist.
+		target = target->next;
+	}
+	handle->next = head_handle;
+	head_handle = handle;
+	return 0;
+}
+
+/**
+  * @brief  Stop the timer work, remove the handle off work list.
+  * @param  handle: target handle strcut.
+  * @retval None
+  */
+void timer_stop(struct Timer* handle)
+{
+	struct Timer** curr;
+	for(curr = &head_handle; *curr; ) {
+		struct Timer* entry = *curr;
+		if (entry == handle) {
+			*curr = entry->next;
+//			free(entry);
+		} else
+			curr = &entry->next;
+	}
+}
+
+/**
+  * @brief  main loop.
+  * @param  None.
+  * @retval None
+  */
+void timer_loop()
+{
+	struct Timer* target;
+	for(target=head_handle; target; target=target->next) {
+		if(((target->timeout < TIMER_MAX) && (_timer_ticks >= target->timeout))	|| \
+			((target->timeout >= TIMER_MAX) && (_timer_ticks+TIMER_MAX >= target->timeout))
+		)
+		{
+			if(target->repeat == 0) {		//Ò»´ÎÐÔ
+				timer_stop(target);
+			} else {						//Ñ­»·
+				target->timeout = _timer_ticks + target->repeat;
+			}
+			target->timeout_cb();
+		}
+	}
+}
+
+/**
+  * @brief  background ticks, timer repeat invoking interval 1ms.
+  * @param  None.
+  * @retval None.
+  */
+void timer_ticks()
+{
+	_timer_ticks += TIMER_TICK;
+	if(_timer_ticks >= TIMER_MAX)	_timer_ticks = 0;
+}
+

+ 42 - 0
stm32 Multi/multi_timer.h

@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2016 Zibin Zheng <[email protected]>
+ * All rights reserved
+ */
+ 
+#ifndef _MULTI_TIMER_H_
+#define _MULTI_TIMER_H_
+
+//#include "stdint.h"
+#include "TypeDef.h"
+#include "string.h"
+
+#define TIMER_TICK	10
+#define TIMER_MAX	60000	//86400000	//
+
+typedef struct Timer {
+    uint32_t timeout;
+    uint32_t repeat;
+    void (*timeout_cb)(void);
+    struct Timer* next;
+}Timer;
+
+#ifdef __cplusplus  
+extern "C" {  
+#endif  
+
+extern void timer_init_Head(void);
+extern void timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat);
+extern int  timer_start(struct Timer* handle);
+extern void timer_stop(struct Timer* handle);
+extern void timer_ticks(void);
+extern void timer_loop(void);
+extern void timer_app(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat);
+
+// void timer_again(struct Timer* handle);
+// void timer_set_repeat(struct Timer* handle, uint32_t repeat);
+
+#ifdef __cplusplus
+} 
+#endif
+
+#endif

+ 151 - 0
stm32 Multi/smg.c

@@ -0,0 +1,151 @@
+//引脚对应P5.0~5.7: BCEDFAGH
+#include "Include.h"
+
+static u8 DigNum = 0;
+
+static u8 const smgduan[16] = 
+{
+    /*0,1,2,3,4,5,6,7,8,9,A,b,C,d,E,F,*/
+    0x3F,0x03,0xAD,0xAB,0x93,0xBA,0xBE,0x23,0xBF,0xBB,0xB7,0x9E,0x3C,0x8F,0xBC,0xB4
+    //0xC0,0xFC,0x52,0x54,0x6C,0x45,0x41,0xDC,0x40,0x44,0x48,0x61,0xC3,0x70,0x43,0x4B  
+};//显示0~F的值
+
+static u8 Smg_Arrary[] =
+{
+	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0xFF
+};
+static u8 Smg_Arrary_i = 0;
+
+/*******************************************************************************
+* 函 数 名 : delay
+* 函数功能 : 延时函数,i=1时,大约延时10us
+*******************************************************************************/
+void delay ( u16 i )
+{
+    while ( i-- );
+}
+
+void Smg_Init ( void )
+{
+
+}
+
+void Smg_Display(u8 Num)
+{ 
+	DigNum = Num;	
+}
+
+
+void Smg_Display_Next(void)
+{
+	if(++Smg_Arrary_i >= ARRAY_SIZE(Smg_Arrary))	Smg_Arrary_i = 0;
+	DigNum = Smg_Arrary[Smg_Arrary_i];
+}
+
+void Smg_Display_OnOff(bool On)
+{
+	Smg_Arrary_i = (On)?(0):(ARRAY_SIZE(Smg_Arrary)-1);
+	DigNum = Smg_Arrary[Smg_Arrary_i];
+}
+
+//pos:0~1
+//Num:0~9, 
+void Smg_PlayOne(u8 Pos, u8 Num)
+{ 
+	Num = (Num<=9)?(~smgduan[Num]):(0xFF);
+	switch ( Pos ) //位选,选择点亮的数码管,
+	{
+	case 0:
+		LED_DATA = 0;
+		LED_COM1(0);    //低亮
+		LED_COM2(1);
+		LED_DATA = Num; //发送段码
+		break;//显示第0位
+	
+	case 1:
+		LED_DATA = 0;	
+		LED_COM1(1);            
+		LED_COM2(0);    //低亮
+		LED_DATA = Num; //发送段码
+		break;//显示第1位
+	}	
+}
+
+void Smg_test(void)	//10ms
+{ 
+	
+	static u8 time = 0;      
+ #if 0       
+        static u8 Num = 0;
+	if(time++>=100)
+	{
+       
+		time = 0;	
+		LED_COM1(1);			
+		LED_COM2(1);	//低亮
+		Num++;
+		if(Num>7)	Num=0;
+		LED_DATA = ~(1<<Num); //发送段码
+                 
+	}
+#else
+        if(time++>=50)
+	{
+            time = 0;
+            DigNum += 11;
+            if(DigNum%11 != 0)	DigNum=0;
+            if(DigNum>99)	DigNum=0;
+        }
+#endif       
+}
+
+void Smg_Refrensh(void)	//10ms
+{ 
+	static u8 Step = 0;
+	u8 units,tens;
+	
+	switch ( Step ) 
+	{
+	case 0 :				//个位
+		units = (DigNum<99)?(DigNum%10):(0xFF);
+		Smg_PlayOne(0, units);
+		Step = 1;
+		break;
+	case 1 :				//十位
+		tens = (DigNum<99)?((DigNum/10)%10):(0xFF);
+		Smg_PlayOne(1, tens);
+		Step = 0;
+		break;          
+	}
+	//Smg_test();
+}
+
+/*******************************************************************************
+* 函 数 名 : DigDisplay
+* 函数功能 : 数码管动态扫描函数,循环扫描8个数码管显示
+*******************************************************************************/
+void DigDisplay()
+{ 
+    u8 i;
+
+    for ( i = 0; i < 4; i++ )
+    {
+        switch ( i ) //位选,选择点亮的数码管,
+        {
+        case ( 0 ) :
+            LED_COM1(0);
+            LED_COM2(1);
+            break;//显示第0位
+
+        case ( 1 ) :
+            LED_COM1(1);
+            LED_COM2(0);
+            break;//显示第1位
+        }
+
+        LED_DATA = smgduan[i]; //发送段码
+        delay ( 100 ); //间隔一段时间扫描
+        LED_DATA = 0x00; //消隐
+    }
+}
+

+ 20 - 0
stm32 Multi/smg.h

@@ -0,0 +1,20 @@
+#ifndef __SMG_H__
+#define __SMG_H__
+#define FRCom
+#define Const_DyUart1Over   0xff                        // 模拟口的溢出保护,在10ms中执行,等于0xff则初始化
+
+#define Uart1_RevEn()
+#define Uart1_SendEn()
+
+#define CONST_UART1_DEFBPS  Const_BPS_2400
+
+
+void Smg_Init ( void );
+void Smg_Display(u8 Num);
+void Smg_Refrensh(void);
+
+
+void Smg_Display_OnOff(bool On);
+void Smg_Display_Next(void);
+
+#endif

+ 431 - 0
stm32 动态数组/ArrauList/ArrayList.c

@@ -0,0 +1,431 @@
+#include "ArrayList.h"
+#include "ArrayList.h"
+
+void *new_byte(int size){
+    void *mem = malloc(size);
+	if(mem != NULL){
+         memset(mem,NULL,size);
+		 return mem;
+	}
+    return NULL;
+}
+
+/****************************************************************
+func:   new_array_list
+note:   创建一个new_array_list对象,并返回
+@head:  集合
+@value: value
+@size:  value的长度
+return: 返回创建的 ArrayList
+****************************************************************/
+ArrayList *new_array_list(ArrayList *head,void *value,int size){
+     ArrayList *obj = new_object(ArrayList);
+     if(obj != NULL){
+          reset_object(ArrayList,obj);  
+		  obj->sValue = size;
+		  obj->value = new_byte(size);
+		  if(obj->value != NULL){
+		  	  if(value != NULL){
+                   memcpy(obj->value,value,size);
+		  	  }
+			  return obj;
+		  }
+		  free(obj);
+	 }
+     return NULL;
+}
+
+
+/****************************************************************
+func:   add_array_list
+note:   添加value
+@head:  集合
+@value: value
+@size:  value的长度
+return: 返回head
+****************************************************************/
+ArrayList *add_array_list(ArrayList *head,void *value,int size){
+	ArrayList *obj = head;    
+    if(head == NULL){
+		return new_array_list(NULL,value,size);
+	}else{
+		obj = get_array_list_last(head);
+		obj->next = new_array_list(NULL,value,size);
+	    return head;
+	}
+}
+
+
+
+
+/****************************************************************
+func:   put_string_map
+note:   修改指定的key的值
+@head:  集合
+@key:   key
+@value: value
+return: 返回head
+****************************************************************/
+ArrayList *put_array_list(ArrayList *head,int indexof,void *value,int size){
+    ArrayList *obj = get_array_list(head,indexof);
+    if(obj != NULL){
+         free(obj->value);
+		 obj->sValue = size;
+		 obj->value = new_byte(size);
+		 if(value != NULL){
+		     memcpy(obj->value,value,size);
+		 }
+		 return head;
+	}
+	return NULL;
+}
+
+
+/****************************************************************
+func:   get_array_list
+note:   获得indexof位置的对象
+@head:      集合
+@indexof:   坐标位置
+return: 返回获得indexof的对象,如果indexof不存在返回NULL
+****************************************************************/
+ArrayList *get_array_list(ArrayList *head,int indexof){
+	ArrayList *obj = head;
+	test_object(head);
+    for(int i=0;obj != NULL;i++){
+		if(i == indexof){
+			return obj;
+		}
+        obj = obj->next;
+	}
+	return NULL;
+}
+
+
+
+/****************************************************************
+func:   get_array_list_previous
+note:   获得indexof的上一个对象
+@head:  集合
+@indexof:   indexof
+return: 返回获得indexof的上一个对象,如果indexof不存在返回NULL
+****************************************************************/
+ArrayList *get_array_list_previous(ArrayList *head,int indexof){
+	ArrayList *obj = head;
+	ArrayList *last = NULL;
+	test_object(head);
+    for(int i=0;obj != NULL;i++){
+		if(i == indexof){
+			return last;
+		}
+		last = obj;
+        obj = obj->next;
+	}
+	return NULL;
+}
+
+/****************************************************************
+func:   get_array_list_last
+note:   获得集合最后的对象
+@head:  集合
+return: 返回获得集合最后的对象
+****************************************************************/
+ArrayList *get_array_list_last(ArrayList *head){
+	ArrayList *obj = head;
+	test_object(head);
+    while(obj->next != NULL){
+        obj = obj->next;
+	}
+	return obj;
+}
+
+
+/****************************************************************
+func:   get_array_list_size
+note:   获得集合的对象数量
+@head:  集合
+return: 返回集合的对象数量
+****************************************************************/
+int get_array_list_size(ArrayList *head){
+	int i = 0;
+	ArrayList *obj = head;
+	test_object(head);
+    for(i=0;obj != NULL;i++){
+        obj = obj->next;
+	}
+	return i;
+}
+
+
+
+
+/****************************************************************
+func:   remove_string_map
+note:   移除集合中的对象
+@head:  集合
+return: 移除集合中的对象后一个StringMap对象
+****************************************************************/
+ArrayList *remove_array_list(ArrayList *head){
+	 ArrayList *obj = head->next;
+	 test_object(head);
+	 free(head->value);
+	 free(head);
+	 return obj;
+}
+
+
+
+
+/****************************************************************
+func:   close_array_list
+note:   移除所有集合中的对象
+@head:  集合
+return: NULL
+****************************************************************/
+ArrayList *close_array_list(ArrayList *head){
+	ArrayList *obj = head;
+	test_object(head);
+	for(int i=0;obj != NULL;i++){
+        obj = remove_array_list(obj);
+	}
+	return NULL;
+}
+
+
+
+
+/****************************************************************
+func:   remove_array_list_index
+note:   移除坐标的对象
+@head:  集合
+@index: 坐标
+return: 返回移除后的集合,如果没有返回NULL
+****************************************************************/
+ArrayList *remove_array_list_index(ArrayList *head,int index){
+	ArrayList *obj = NULL;
+	ArrayList *previous = NULL;;
+	test_object(head);
+	if(index < get_array_list_size(head)){
+        previous = get_array_list_previous(head,index);
+        if(previous == NULL || index == 0){
+			return remove_array_list(head);
+		}else{
+		    obj = get_array_list(head,index);
+			previous->next = remove_array_list(obj);
+			return head;
+		}
+	}
+	return NULL;
+}
+
+
+/****************************************************************
+func:   get_array_list_memsize
+note:   获得集合字符串内存块大小
+@head:  集合
+return: 字符串的内存块大小
+****************************************************************/
+int get_array_list_memsize(ArrayList *head){
+	int memsize = 0;
+	ArrayList *obj = head;
+    for(int i=0;obj != NULL;i++){
+		memsize += obj->sValue;
+        obj = obj->next;
+	}
+	return memsize;
+}
+
+
+
+
+/****************************************************************
+func:   get_array_list_disksize
+note:   获得当前对象占用磁盘空间大小
+@head:  集合
+return: 占用磁盘空间大小
+****************************************************************/
+int get_array_list_disksize(ArrayList *head){
+	int memsize = get_array_list_memsize(head);
+    int objsize = get_array_list_size(head) * (sizeof(int)*1);
+    int sum =  sizeof(int) + objsize + memsize;
+    return sum;
+}
+
+
+
+
+uint8_t *write_buff_array_list(ArrayList *head,uint8_t *mBuf){
+	
+	if(mBuf == NULL) return NULL;
+	
+	memcpy(mBuf,&head->sValue,sizeof(int));
+	mBuf = mBuf + sizeof(int);
+	
+	memcpy(mBuf,head->value,head->sValue);
+	mBuf = mBuf + head->sValue;
+	
+    return mBuf;
+}
+
+
+uint8_t *read_buff_array_list(ArrayList *head,uint8_t *mBuf){
+	
+	if(mBuf == NULL) return NULL;
+	
+	memcpy(&head->sValue,mBuf,sizeof(int));
+	mBuf = mBuf + sizeof(int);
+	
+    head->value = new_byte(head->sValue);
+    if(head->value != NULL ){
+		memcpy(head->value,mBuf,head->sValue);
+		mBuf = mBuf + head->sValue;
+		return mBuf;
+    }
+    return NULL;
+}
+
+
+/****************************************************************
+func:   pack_array_list
+note:   打包集合数据,打包方式
+        int(总的长度) + ( (int)sValue + (data)value  ...
+@head:  集合
+return: 返回打包好的PackMap数据对象,失败返回NULL
+****************************************************************/
+PackMap *pack_array_list(ArrayList *head){
+	PackMap *pack = NULL;
+	ArrayList *obj = head;
+    int sum = get_array_list_disksize(head);
+    uint8_t *buf = new_byte(sum);
+	uint8_t *mBuf = buf;
+    if(buf != NULL){
+		 memset(buf,0,sum);
+		 memcpy(mBuf,&sum,sizeof(int));
+	     mBuf = mBuf + sizeof(int);
+         while(obj != NULL){
+			 mBuf = write_buff_array_list(obj,mBuf);
+	         obj = obj->next;
+		 }
+		 pack = new_object(PackMap);
+		 pack->sum = sum;
+		 pack->buff = buf;
+	     return pack;
+	}
+    return NULL;
+}
+
+
+
+
+/****************************************************************
+func:   unpack_array_list
+note:   解包ArrayList集合数据
+@pack:  PackMap对象
+return: 返回ArrayList集合,失败返回NULL
+****************************************************************/
+ArrayList *unpack_array_list(PackMap *pack){
+	ArrayList *head = NULL;
+	ArrayList *obj = NULL;
+	ArrayList *temp = NULL;
+	uint8_t *mBuf = NULL;
+	int sum = 0;
+	if(pack != NULL){
+		mBuf = pack->buff;
+		memcpy(&sum,mBuf,sizeof(int));
+	    mBuf = mBuf + sizeof(int);
+		head = new_object(ArrayList);
+		mBuf = read_buff_array_list(head,mBuf);
+		obj = head;
+		if(pack->sum == sum){
+	        while((pack->buff + sum) > mBuf && mBuf != NULL){
+				temp = new_object(ArrayList);
+				mBuf = read_buff_array_list(temp,mBuf);
+	            obj->next = temp;
+				obj->next->next = NULL;
+				obj = obj->next;
+			}
+			return head;
+		}
+	}
+	return NULL;
+}
+
+
+#if 1
+void test_ArrayList(void){
+   uint8_t buf[50];
+   ArrayList *temp;
+   ArrayList *object = new_array_list(NULL,"0123456789",10);
+   
+   printf("\rArrayList --> test\r");
+   add_array_list(object,"A123456\0",8);
+   add_array_list(object,"B123456\0",8);
+   add_array_list(object,"C123456\0",8);
+   add_array_list(object,"D123456\0",8);
+#if 1
+   //string 类型
+   add_array_list(object,"123",3);
+   temp = get_array_list(object,1);
+   char *stp = temp->value;
+   printf("0x%x 0x%x 0x%x\r",stp[0],stp[1],stp[2]);
+
+   //int 类型
+   int in = 1024;
+   add_array_list(object,&in,sizeof(int));
+   int *inp = get_array_list(object,2)->value;
+   printf("int type p:%d\r",*inp);
+
+   //double 类型
+   double dou = 1024.5555;
+   add_array_list(object,&dou,sizeof(double));
+   double *pdo = get_array_list(object,3)->value;
+   printf("double type p:%f\r",*pdo);
+#endif
+   
+  /* for(int i=0;i<1000;i++){
+   	   snprintf(buf,50,"Guihui->%d \0",i);
+       add_array_list(object,buf,strlen(buf));
+   }*/
+
+   PackMap *pack = pack_array_list(object);
+   if(pack != NULL){
+   	   printf("ArrayList PackMap Test\r");
+       object = close_array_list(object);
+       for(int i=0; i<pack->sum; i++){
+            printf("%x ",pack->buff[i]);
+	   }
+	   printf("\r");
+       /*解包*/
+	   object = unpack_array_list(pack);
+	   
+   }
+
+   temp = remove_array_list_index(object,0);
+   if(temp == NULL){
+       printf("ERROR remove index\r");
+   }else{
+	   object = temp;
+   }
+
+   temp = remove_array_list_index(object,3);
+   if(temp == NULL){
+	   printf("ERROR remove index\r");
+   }else{
+	   object = temp;
+   }
+
+   //object = remove_array_list_index(object,3);
+
+   temp = object; 
+   for(int i=0; temp != NULL; i++){
+   	   snprintf(buf,temp->sValue,"%s",temp->value);
+   	   printf("%0.2d -> %s\r",i,buf);
+	   temp = temp->next;
+   }
+   
+   close_array_list(object);
+}
+
+#endif
+
+
+

+ 33 - 0
stm32 动态数组/ArrauList/ArrayList.h

@@ -0,0 +1,33 @@
+#ifndef _ARRAY_LIST_H
+#define _ARRAY_LIST_H
+
+//#include "PackMap.h"
+
+
+typedef struct ArrayList{
+     int sValue;
+	 void *value;
+	 struct ArrayList *next;
+}ArrayList;
+
+
+extern ArrayList *new_array_list(ArrayList *head,void *value,int size);
+extern ArrayList *add_array_list(ArrayList *head,void *value,int size);
+extern ArrayList *put_array_list(ArrayList *head,int indexof,void *value,int size);
+extern ArrayList *get_array_list(ArrayList *head,int indexof);
+extern ArrayList *get_array_list_previous(ArrayList *head,int indexof);
+extern ArrayList *get_array_list_last(ArrayList *head);
+extern int get_array_list_size(ArrayList *head);
+extern ArrayList *remove_array_list(ArrayList *head);
+extern ArrayList *close_array_list(ArrayList *head);
+extern ArrayList *remove_array_list_index(ArrayList *head,int index);
+extern int get_array_list_memsize(ArrayList *head);
+extern int get_array_list_disksize(ArrayList *head);
+extern PackMap *pack_array_list(ArrayList *head);
+extern ArrayList *unpack_array_list(PackMap *pack);
+
+
+#endif
+
+
+

+ 21 - 0
stm32 动态数组/ArrauList/PackMap.c

@@ -0,0 +1,21 @@
+#include "PackMap.h"
+
+
+
+/****************************************************************
+func:   remove_pack_map
+note:   ÒÆ³ýPackMap¶ÔÏó
+@pack:  ¶ÔÏó
+return: ·µ»ØNULL
+****************************************************************/
+int remove_pack_map(PackMap *pack){
+    free(pack->buff);
+	free(pack);
+    return NULL;
+}
+
+
+
+
+
+

+ 20 - 0
stm32 动态数组/ArrauList/PackMap.h

@@ -0,0 +1,20 @@
+#ifndef _PACK_MAP_H
+#define _PACK_MAP_H
+
+/*
+* PackMap是通用类,所有对像都可以用它来打包成PackMap并保存
+*/
+
+#include "SystemMemory.h"
+
+typedef struct {
+    int sum;
+	uint8_t *buff;
+}PackMap;
+
+
+extern int remove_pack_map(PackMap *pack);
+
+
+#endif
+

+ 22 - 0
stm32 动态数组/ArrauList/SystemMemory.c

@@ -0,0 +1,22 @@
+#include "SystemMemory.h"
+#include "StringMap.h"
+#include "PackMap.h"
+#include "ArrayList.h"
+
+
+void *new_byte(int size){
+    void *mem = malloc(size);
+	if(mem != NULL){
+         memset(mem,NULL,size);
+		 return mem;
+	}
+    return NULL;
+}
+
+
+void test_memory(void){
+
+
+}
+
+

+ 24 - 0
stm32 动态数组/ArrauList/SystemMemory.h

@@ -0,0 +1,24 @@
+#ifndef _SYTEM_MEMORY_H
+#define _SYTEM_MEMORY_H
+
+#include <mqx.h>
+#include <bsp.h>
+#include <stdlib.h>
+
+
+#define test_object(object)  if(object == NULL)return NULL
+
+
+#define new_string(str)               (char*)malloc(strlen(str)+1)
+#define new_object(object)            (object*)malloc(sizeof(object))
+#define reset_object(object,p)         memset((void*)p,NULL,sizeof(object))
+
+extern void *new_byte(int size);
+
+
+extern void test_memory(void);
+
+
+#endif
+
+

+ 435 - 0
stm32 动态数组/ArrayList.c

@@ -0,0 +1,435 @@
+#include "ArrayList.h"
+
+#define test_object(object)  if(object == NULL)return NULL
+#define new_string(str)               (char*)malloc(strlen(str)+1)
+#define new_object(object)            (object*)malloc(sizeof(object))
+#define reset_object(object,p)         memset((void*)p,NULL,sizeof(object))
+
+void *new_byte(int size){
+    void *mem = malloc(size);
+	if(mem != NULL){
+         memset(mem,NULL,size);
+		 return mem;
+	}
+    return NULL;
+}
+
+/****************************************************************
+func:   new_array_list
+note:   创建一个new_array_list对象,并返回
+@head:  集合
+@value: value
+@size:  value的长度
+return: 返回创建的 ArrayList
+****************************************************************/
+ArrayList *new_array_list(ArrayList *head,void *value,int size){
+     ArrayList *obj = new_object(ArrayList);
+     if(obj != NULL){
+          reset_object(ArrayList,obj);  
+		  obj->sValue = size;
+		  obj->value = new_byte(size);
+		  if(obj->value != NULL){
+		  	  if(value != NULL){
+                   memcpy(obj->value,value,size);
+		  	  }
+			  return obj;
+		  }
+		  free(obj);
+	 }
+     return NULL;
+}
+
+
+/****************************************************************
+func:   add_array_list
+note:   添加value
+@head:  集合
+@value: value
+@size:  value的长度
+return: 返回head
+****************************************************************/
+ArrayList *add_array_list(ArrayList *head,void *value,int size){
+	ArrayList *obj = head;    
+    if(head == NULL){
+		return new_array_list(NULL,value,size);
+	}else{
+		obj = get_array_list_last(head);
+		obj->next = new_array_list(NULL,value,size);
+	    return head;
+	}
+}
+
+
+
+
+/****************************************************************
+func:   put_string_map
+note:   修改指定的key的值
+@head:  集合
+@key:   key
+@value: value
+return: 返回head
+****************************************************************/
+ArrayList *put_array_list(ArrayList *head,int indexof,void *value,int size){
+    ArrayList *obj = get_array_list(head,indexof);
+    if(obj != NULL){
+         free(obj->value);
+		 obj->sValue = size;
+		 obj->value = new_byte(size);
+		 if(value != NULL){
+		     memcpy(obj->value,value,size);
+		 }
+		 return head;
+	}
+	return NULL;
+}
+
+
+/****************************************************************
+func:   get_array_list
+note:   获得indexof位置的对象
+@head:      集合
+@indexof:   坐标位置
+return: 返回获得indexof的对象,如果indexof不存在返回NULL
+****************************************************************/
+ArrayList *get_array_list(ArrayList *head,int indexof){
+	ArrayList *obj = head;
+	test_object(head);
+    for(int i=0;obj != NULL;i++){
+		if(i == indexof){
+			return obj;
+		}
+        obj = obj->next;
+	}
+	return NULL;
+}
+
+
+
+/****************************************************************
+func:   get_array_list_previous
+note:   获得indexof的上一个对象
+@head:  集合
+@indexof:   indexof
+return: 返回获得indexof的上一个对象,如果indexof不存在返回NULL
+****************************************************************/
+ArrayList *get_array_list_previous(ArrayList *head,int indexof){
+	ArrayList *obj = head;
+	ArrayList *last = NULL;
+	test_object(head);
+    for(int i=0;obj != NULL;i++){
+		if(i == indexof){
+			return last;
+		}
+		last = obj;
+        obj = obj->next;
+	}
+	return NULL;
+}
+
+/****************************************************************
+func:   get_array_list_last
+note:   获得集合最后的对象
+@head:  集合
+return: 返回获得集合最后的对象
+****************************************************************/
+ArrayList *get_array_list_last(ArrayList *head){
+	ArrayList *obj = head;
+	test_object(head);
+    while(obj->next != NULL){
+        obj = obj->next;
+	}
+	return obj;
+}
+
+
+/****************************************************************
+func:   get_array_list_size
+note:   获得集合的对象数量
+@head:  集合
+return: 返回集合的对象数量
+****************************************************************/
+int get_array_list_size(ArrayList *head){
+	int i = 0;
+	ArrayList *obj = head;
+	test_object(head);
+    for(i=0;obj != NULL;i++){
+        obj = obj->next;
+	}
+	return i;
+}
+
+
+
+
+/****************************************************************
+func:   remove_string_map
+note:   移除集合中的对象
+@head:  集合
+return: 移除集合中的对象后一个StringMap对象
+****************************************************************/
+ArrayList *remove_array_list(ArrayList *head){
+	 ArrayList *obj = head->next;
+	 test_object(head);
+	 free(head->value);
+	 free(head);
+	 return obj;
+}
+
+
+
+
+/****************************************************************
+func:   close_array_list
+note:   移除所有集合中的对象
+@head:  集合
+return: NULL
+****************************************************************/
+ArrayList *close_array_list(ArrayList *head){
+	ArrayList *obj = head;
+	test_object(head);
+	for(int i=0;obj != NULL;i++){
+        obj = remove_array_list(obj);
+	}
+	return NULL;
+}
+
+
+
+
+/****************************************************************
+func:   remove_array_list_index
+note:   移除坐标的对象
+@head:  集合
+@index: 坐标
+return: 返回移除后的集合,如果没有返回NULL
+****************************************************************/
+ArrayList *remove_array_list_index(ArrayList *head,int index){
+	ArrayList *obj = NULL;
+	ArrayList *previous = NULL;;
+	test_object(head);
+	if(index < get_array_list_size(head)){
+        previous = get_array_list_previous(head,index);
+        if(previous == NULL || index == 0){
+			return remove_array_list(head);
+		}else{
+		    obj = get_array_list(head,index);
+			previous->next = remove_array_list(obj);
+			return head;
+		}
+	}
+	return NULL;
+}
+
+
+/****************************************************************
+func:   get_array_list_memsize
+note:   获得集合字符串内存块大小
+@head:  集合
+return: 字符串的内存块大小
+****************************************************************/
+int get_array_list_memsize(ArrayList *head){
+	int memsize = 0;
+	ArrayList *obj = head;
+    for(int i=0;obj != NULL;i++){
+		memsize += obj->sValue;
+        obj = obj->next;
+	}
+	return memsize;
+}
+
+
+
+
+/****************************************************************
+func:   get_array_list_disksize
+note:   获得当前对象占用磁盘空间大小
+@head:  集合
+return: 占用磁盘空间大小
+****************************************************************/
+int get_array_list_disksize(ArrayList *head){
+	int memsize = get_array_list_memsize(head);
+    int objsize = get_array_list_size(head) * (sizeof(int)*1);
+    int sum =  sizeof(int) + objsize + memsize;
+    return sum;
+}
+
+
+
+
+uint8_t *write_buff_array_list(ArrayList *head,uint8_t *mBuf){
+	
+	if(mBuf == NULL) return NULL;
+	
+	memcpy(mBuf,&head->sValue,sizeof(int));
+	mBuf = mBuf + sizeof(int);
+	
+	memcpy(mBuf,head->value,head->sValue);
+	mBuf = mBuf + head->sValue;
+	
+    return mBuf;
+}
+
+
+uint8_t *read_buff_array_list(ArrayList *head,uint8_t *mBuf){
+	
+	if(mBuf == NULL) return NULL;
+	
+	memcpy(&head->sValue,mBuf,sizeof(int));
+	mBuf = mBuf + sizeof(int);
+	
+    head->value = new_byte(head->sValue);
+    if(head->value != NULL ){
+		memcpy(head->value,mBuf,head->sValue);
+		mBuf = mBuf + head->sValue;
+		return mBuf;
+    }
+    return NULL;
+}
+
+
+/****************************************************************
+func:   pack_array_list
+note:   打包集合数据,打包方式
+        int(总的长度) + ( (int)sValue + (data)value  ...
+@head:  集合
+return: 返回打包好的PackMap数据对象,失败返回NULL
+****************************************************************/
+PackMap *pack_array_list(ArrayList *head){
+	PackMap *pack = NULL;
+	ArrayList *obj = head;
+    int sum = get_array_list_disksize(head);
+    uint8_t *buf = new_byte(sum);
+	uint8_t *mBuf = buf;
+    if(buf != NULL){
+		 memset(buf,0,sum);
+		 memcpy(mBuf,&sum,sizeof(int));
+	     mBuf = mBuf + sizeof(int);
+         while(obj != NULL){
+			 mBuf = write_buff_array_list(obj,mBuf);
+	         obj = obj->next;
+		 }
+		 pack = new_object(PackMap);
+		 pack->sum = sum;
+		 pack->buff = buf;
+	     return pack;
+	}
+    return NULL;
+}
+
+
+
+
+/****************************************************************
+func:   unpack_array_list
+note:   解包ArrayList集合数据
+@pack:  PackMap对象
+return: 返回ArrayList集合,失败返回NULL
+****************************************************************/
+ArrayList *unpack_array_list(PackMap *pack){
+	ArrayList *head = NULL;
+	ArrayList *obj = NULL;
+	ArrayList *temp = NULL;
+	uint8_t *mBuf = NULL;
+	int sum = 0;
+	if(pack != NULL){
+		mBuf = pack->buff;
+		memcpy(&sum,mBuf,sizeof(int));
+	    mBuf = mBuf + sizeof(int);
+		head = new_object(ArrayList);
+		mBuf = read_buff_array_list(head,mBuf);
+		obj = head;
+		if(pack->sum == sum){
+	        while((pack->buff + sum) > mBuf && mBuf != NULL){
+				temp = new_object(ArrayList);
+				mBuf = read_buff_array_list(temp,mBuf);
+	            obj->next = temp;
+				obj->next->next = NULL;
+				obj = obj->next;
+			}
+			return head;
+		}
+	}
+	return NULL;
+}
+
+
+#if 0
+void test_ArrayList(void){
+   uint8_t buf[50];
+   ArrayList *temp;
+   ArrayList *object = new_array_list(NULL,"0123456789",10);
+   
+   printf("\rArrayList --> test\r");
+   add_array_list(object,"A123456\0",8);
+   add_array_list(object,"B123456\0",8);
+   add_array_list(object,"C123456\0",8);
+   add_array_list(object,"D123456\0",8);
+#if 1
+   //string 类型
+   add_array_list(object,"123",3);
+   temp = get_array_list(object,1);
+   char *stp = temp->value;
+   printf("0x%x 0x%x 0x%x\r",stp[0],stp[1],stp[2]);
+
+   //int 类型
+   int in = 1024;
+   add_array_list(object,&in,sizeof(int));
+   int *inp = get_array_list(object,2)->value;
+   printf("int type p:%d\r",*inp);
+
+   //double 类型
+   double dou = 1024.5555;
+   add_array_list(object,&dou,sizeof(double));
+   double *pdo = get_array_list(object,3)->value;
+   printf("double type p:%f\r",*pdo);
+#endif
+   
+  /* for(int i=0;i<1000;i++){
+   	   snprintf(buf,50,"Guihui->%d \0",i);
+       add_array_list(object,buf,strlen(buf));
+   }*/
+
+   PackMap *pack = pack_array_list(object);
+   if(pack != NULL){
+   	   printf("ArrayList PackMap Test\r");
+       object = close_array_list(object);
+       for(int i=0; i<pack->sum; i++){
+            printf("%x ",pack->buff[i]);
+	   }
+	   printf("\r");
+       /*解包*/
+	   object = unpack_array_list(pack);
+	   
+   }
+
+   temp = remove_array_list_index(object,0);
+   if(temp == NULL){
+       printf("ERROR remove index\r");
+   }else{
+	   object = temp;
+   }
+
+   temp = remove_array_list_index(object,3);
+   if(temp == NULL){
+	   printf("ERROR remove index\r");
+   }else{
+	   object = temp;
+   }
+
+   //object = remove_array_list_index(object,3);
+
+   temp = object; 
+   for(int i=0; temp != NULL; i++){
+   	   snprintf((char *)buf,temp->sValue,"%s",(char *)temp->value);
+   	   printf("%0.2d -> %s\r",i,buf);
+	   temp = temp->next;
+   }
+   
+   close_array_list(object);
+}
+
+#endif
+
+
+

+ 39 - 0
stm32 动态数组/ArrayList.h

@@ -0,0 +1,39 @@
+#ifndef _ARRAY_LIST_H
+#define _ARRAY_LIST_H
+#include <stdio.h>
+#include <stdint.h>             //uint8
+#include <string.h>             //memcpy
+#include <stdlib.h>             //malloc¡¢free
+
+typedef struct {
+    int sum;
+	uint8_t *buff;
+}PackMap;
+
+typedef struct ArrayList{
+     int sValue;
+	 void *value;
+	 struct ArrayList *next;
+}ArrayList;
+
+
+extern ArrayList *new_array_list(ArrayList *head,void *value,int size);
+extern ArrayList *add_array_list(ArrayList *head,void *value,int size);
+extern ArrayList *put_array_list(ArrayList *head,int indexof,void *value,int size);
+extern ArrayList *get_array_list(ArrayList *head,int indexof);
+extern ArrayList *get_array_list_previous(ArrayList *head,int indexof);
+extern ArrayList *get_array_list_last(ArrayList *head);
+extern int get_array_list_size(ArrayList *head);
+extern ArrayList *remove_array_list(ArrayList *head);
+extern ArrayList *close_array_list(ArrayList *head);
+extern ArrayList *remove_array_list_index(ArrayList *head,int index);
+extern int get_array_list_memsize(ArrayList *head);
+extern int get_array_list_disksize(ArrayList *head);
+extern PackMap *pack_array_list(ArrayList *head);
+extern ArrayList *unpack_array_list(PackMap *pack);
+
+
+#endif
+
+
+

+ 546 - 0
stm32 动态数组/方式2/ArrayLib.c

@@ -0,0 +1,546 @@
+/********************************************
+ * 文件名:arrayLib.c
+ * 文件描述:动态数组库函数所有功能函数的定义
+ * 编辑人:王廷云
+ * 编辑日期:2017-10-1
+ * 修改日期:2018-1-10
+*********************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "arrayLib.h"
+
+/* 
+ * 函数名: initArray
+ * 函数功能: 初始化动态数组
+ * 参数: 数组元素的空间大小
+ * 返回值: 成功则返回初始化后的动态数组指针
+ *         失败返回NULL 
+*/
+Array_t *initArray(int size)
+{
+    Array_t *ar;
+    ar = malloc(sizeof(Array_t));
+    if (ar == NULL) return NULL;
+
+    ar->size = size;
+    ar->n = 0;
+    ar->addr = NULL;
+
+    return ar;
+}
+
+/*
+ * 函数名: appendArrayTail
+ * 函数功能: 尾部追加数组元素
+ * 参数: 1.需要追加的数组指针 2.需要追加的元素指针
+ * 返回值: 成功追加返回0 失败返回-1
+*/
+int appendArrayTail(Array_t *ar, const void *data)
+{
+    void *temp;
+#if 0
+    /* 分配比原来多一个数据的空间 */
+    temp = realloc(ar->addr, (ar->n+1)*ar->size);
+    if (temp == NULL)
+    {
+        return -1;
+    }
+#else
+    /* 分配比原来多一个数据的空间 */
+    temp = malloc((ar->n+1) * ar->size);
+    if (temp == NULL)
+    {
+        return -1;
+    }
+    /* 把原来数据移动到新空间 */
+    memmove(temp, ar->addr, ar->n * ar->size);
+    /* 释放原来数据空间 */
+    free(ar->addr);
+#endif
+    /* 把新数据复制到新空间的尾部 */
+    memmove(temp+(ar->n*ar->size), data, ar->size);
+    /* 数据个数加1 */
+    ar->n += 1;
+    /* 更新数据地址指针 */
+    ar->addr = temp;
+
+    return 0;
+}
+
+/*
+ * 函数名: appendArrayTop
+ * 函数功能: 头部追加数组元素
+ * 参数: 1.需要追加的数组指针 2.需要追加的元素指针
+ * 返回值: 成功追加返回0 失败返回-1
+*/
+int appendArrayTop(Array_t *ar, const void *data)
+{
+    void *temp;
+#if 0
+    /* 分配比原来多一个数据的空间 */
+    temp = realloc(ar->addr, (ar->n+1)*ar->size);
+    if (temp == NULL)
+    {
+        return -1;
+    }
+#else
+    /* 分配比原来多一个数据的空间 */
+    temp = malloc((ar->n+1) * ar->size);
+    if (temp == NULL)
+    {
+        return -1;
+    }
+    /* 把原来数据移动到新空间 */
+    memmove(temp+ar->size, ar->addr, ar->n * ar->size);
+    /* 释放原来数据空间 */
+    free(ar->addr);
+#endif
+    /* 把新数据复制到新空间的头部 */
+    memmove(temp, data, ar->size);
+    /* 数据个数加1 */
+    ar->n += 1;
+    /* 更新数据地址指针 */
+    ar->addr = temp;
+
+    return 0;
+}
+
+/*
+ * 函数名: insertArrayByIndex
+ * 函数功能: 指定数组下标插入数组元素
+ *           <函数会检查下标的合法性>
+ * 参数: 1.需要插入数据的数组指针 2.需要插入的数据指针
+ *       2.数组下标值
+ * 返回值: 成功插入返回0 失败返回-1
+*/
+int insertArrayByIndex(Array_t *ar, const void *data, int idx)
+{
+    /* 检查下标合法性 */
+    if (idx < 0 || idx > ar->n)
+    {
+        printf("Error! index is illegal!\n");
+        return -1;
+    }
+
+    void *temp;
+#if 0
+    /* 分配比原来多一个数据的空间 */
+    temp = realloc(ar->addr, (ar->n+1)*ar->size);
+    if (temp == NULL)
+    {
+        return -1;
+    }
+    /* 把原来空间idx以及后面的数据移动到新空间的idx之后 */
+    memmove(temp+(idx+1)*ar->size, temp+idx*ar->size, \
+                                (ar->n-idx)*ar->size);
+    /* 把新数据插入到新空间的idx处 */
+    memmove(temp+idx*ar->size, data, ar->size);
+#else
+    /* 分配比原来多一个数据的空间 */
+    temp = malloc((ar->n+1)*ar->size);
+    if (temp == NULL)
+    {
+        return -1;
+    }
+    /* 把原来空间idx以前的数据移动到新空间前面 */
+    memmove(temp, ar->addr, idx*ar->size);
+    /* 把新数据插入到新空间的idx处 */
+    memmove(temp+idx*ar->size, data, ar->size);
+    /* 把原来空间idx以及后面的数据移动到新空间的idx之后 */
+    memmove(temp+(idx+1)*ar->size, ar->addr+(idx*ar->size), \
+                                (ar->n-idx)*ar->size);
+#endif
+    /* 数据元素个数加1 */
+    ar->n += 1;
+    /* 更新数据地址 */
+    ar->addr = temp;
+
+    return 0;
+}
+
+/*
+ * 函数名: travelArray
+ * 函数功能: 遍历动态数组元素
+ * 参数: 1.需要遍历的数组指针 2.回调函数
+ * 返回值: 无
+*/
+void travelArray(Array_t *ar, void (*func)(void *data))
+{
+    int i;
+    for (i = 0; i < ar->n; i++)
+    {
+        /* ar->addr + i*ar->size: 每个数据的起始地址 */
+        func(ar->addr + i*ar->size);
+    }
+}
+
+/*
+ * 函数名: searchArrayByIndex
+ * 函数功能: 根据下标查找数组元素<会检查下标的合法性>
+ * 参数: 1.需要查找的数组指针 2.查找的下标
+ * 返回值: 数据存在返回数据地址 不存在则返回NULL
+*/
+void *searchArrayByIndex(Array_t *ar, const int idx)
+{
+    /* 下标合法性检查 */
+    if (idx < 0 || idx >= ar->n)
+    {
+        fprintf(stderr,"Error! index is illegal!\n");
+        return NULL;
+    }
+    return ar->addr + idx*ar->size;
+}
+
+/*
+ * 函数名: searchArrayOneByCond
+ * 函数功能: 根据条件查找元素,如果有多个则找第一个
+ * 参数: 1.需要查找的数组指针 2.比较回调函数指针 3.条件指针
+ * 返回值: 数据存在返回数据地址 不存在则返回NULL
+*/
+void *searchArrayOneByCond(Array_t *ar, compare_t *func, \
+                                        const void *key)
+{
+    int i;
+    for (i = 0; i < ar->n; i++)
+    {
+        /* 比较函数条件匹配func返回1 */
+        if (func(ar->addr+i*ar->size, key) == 1)
+        {
+            return ar->addr + i*ar->size;
+        }
+    }
+    return NULL;
+}
+/*
+ * 函数名: searchArrayByCond
+ * 函数功能: 根据条件查找元素
+ * 参数: 1.需要查找的数组指针 2.比较回调函数指针 3.条件指针
+ * 返回值: 数据存在返回数据数组 不存在则返回NULL
+*/
+Array_t *searchArrayByCond(Array_t *ar, compare_t *func, \
+                                        const void *key)
+{
+    int i, ret;
+    Array_t *result;
+
+    /* 创建用于保存结果的数组 */
+    result = initArray(ar->size);
+    if (result == NULL)
+    {
+        return NULL;
+    }
+    /* 遍历查找 */
+    for (i = 0; i < ar->n; i++)
+    {
+        /* 比较函数条件匹配func返回1 */
+        if (func(ar->addr+i*ar->size, key) == 1)
+        {
+            /* 把匹配的数据尾部追加到数组里 */
+            ret = appendArrayTail(result, ar->addr+i*ar->size);
+            if (ret != 0)
+            {
+                destoryArray(result);
+                return NULL;
+            }
+        }
+    }
+    return result;
+}
+
+/*
+ * 函数名: deleteArrayByIndex
+ * 函数功能: 根据下标删除数组元素
+ * 参数: 1.数组指针 2.下标
+ * 返回值: 成功删除返回0 失败返回-1
+*/
+int deleteArrayByIndex(Array_t *ar, const int idx)
+{
+    /* 检查下标合法性 */
+    if (idx < 0 || idx >= ar->n)
+    {
+        fprintf(stderr, "Error! index is illegal!\n");
+        return -1;
+    }
+    /* 通过数据搬运达到删除目的 */
+    void *temp;
+    temp = malloc((ar->n-1)*ar->size);
+    if (temp == NULL)
+    {
+        return -1;
+    }
+    /* 把idx之前的数据移到新空间 */
+    memmove(temp, ar->addr, idx*ar->size);
+    /* 把idx之后的数据移到新空间 */
+    memmove(temp+idx*ar->size, ar->addr+(idx+1)*ar->size, \
+                                  (ar->n-idx-1)*ar->size);
+    /* 释放原来数据空间 */
+    free(ar->addr);
+    /* 更新元素个数 */
+    ar->n -= 1;
+    /* 更新数组元素地址 */
+    ar->addr = temp;
+
+    return 0;
+}
+
+/*
+ * 函数名: deleteArrayOneByCond
+ * 函数功能: 根据条件删除数组元素,如果多个匹配则只删除第一个
+ * 参数: 1.数组指针 2.比较回调函数指针 3.条件
+ * 返回值: 成功删除返回0 失败返回-1
+*/
+int deleteArrayOneByCond(Array_t *ar, compare_t *func, \
+                                     const void *key)
+{
+    int i;
+    for (i = 0; i < ar->n; i++)
+    {
+        /* 比较函数条件匹配返回1 */
+        if (func(ar->addr+i*ar->size, key) == 1)
+        {
+            return deleteArrayByIndex(ar,i);
+        }
+    }
+    return -1;
+}
+
+/*
+ * 函数名: deleteArrayByCond
+ * 函数功能: 根据条件删除所有匹配的数组元素
+ * 参数: 1.数组指针 2.比较回调函数指针 3.条件
+ * 返回值: 返回成功删除的元素个数
+*/
+int deleteArrayByCond(Array_t *ar, compare_t *func, \
+                                   const void *key)
+{
+    int i;
+    int count = 0;
+    for (i = 0; i < ar->n; i++)
+    {
+        /* 比较函数条件匹配返回1 */
+        if (func(ar->addr+i*ar->size, key) == 1)
+        {
+            if (deleteArrayByIndex(ar,i) == 0)
+            {
+                count++;
+                i--; /* 弥补按下标删除元素后的下标变化 */
+            }
+        }
+    }
+    return count;
+}
+/*
+ * 函数名: sortArray
+ * 函数功能: 对数组元素进行排序
+ *          <至于按什么顺序排列则由用户的回调函数具体来实现>
+ * 参数: 1.数组指针 2.比较回调函数指针
+ * 返回值: 排序成功返回0 失败返回-1
+*/
+int sortArray(Array_t *ar, compare_t *func)
+{
+    int i, j;
+    void *temp;
+    temp = malloc(ar->size);
+    if (temp == NULL) return -1;
+    for (i = 0; i < ar->n; i++)
+    {
+        for (j = 1; j < ar->n-i; j++)
+        {
+            /* 比较函数条件匹配返回1 */
+            if (func(ar->addr+i*ar->size, \
+                    ar->addr+j*ar->size) == 1)
+            {
+                memmove(temp, ar->addr+i*ar->size, ar->size);
+                memmove(ar->addr+i*ar->size, \
+                        ar->addr+j*ar->size, ar->size);
+                memmove(ar->addr+j*ar->size, temp, ar->size);
+            }
+        }
+    }
+    free(temp);
+    return 0;   
+}
+
+/*
+ * 函数名: saveArrayToFile
+ * 函数功能: 把数组元素数据保存到文件中
+ * 参数: 1.数组指针 2.需要保存的文件
+ * 返回值: 保存成功返回0 失败返回-1
+*/
+int saveArrayToFile(Array_t *ar, const char *file)
+{
+    FILE *fout;
+    int ret;
+
+    /* 打开文件流 */
+    fout = fopen(file, "w");
+    if (fout == NULL)
+    {
+        perror(file);
+        return -1;
+    }
+    /* 写入数组元素个数 */
+    ret = fwrite(&ar->n, sizeof(ar->n), 1, fout);
+    if (ret != 1)
+    {
+        /* 写入失败:关闭文件流,关闭文件 */
+        fclose(fout);
+        remove(file);
+        return -1;
+    }
+    /* 写入数组元素空间大小 */
+    ret = fwrite(&ar->size, sizeof(ar->size), 1, fout);
+    if (ret != 1)
+    {
+        /* 写入失败:关闭文件流,关闭文件 */
+        fclose(fout);
+        remove(file);
+        return -1;
+    }
+    /* 写入数据 */
+    ret = 0;
+    while (ret != ar->n) /* 保证写完 */
+    {
+        ret += fwrite(ar->addr+ret*ar->size, \
+                    ar->size, ar->n-ret, fout);
+        /* 如果遇到写文件错误 */
+        if (ferror(fout))
+        {
+            fclose(fout);
+            remove(file);
+            return -1;
+        }
+    }
+
+    fclose(fout);
+    return 0;
+}
+
+/*
+ * 函数名: loadArrayFromFile
+ * 函数功能: 从文件中加载数组数据
+ * 参数: 需要加载的文件
+ * 返回值: 成功加载返回数组指针 失败返回NULL
+*/
+Array_t *loadArrayFromFile(const char *file)
+{
+    int ret;
+    FILE *fin;
+    Array_t *ar;
+
+    /* 打开文件 */
+    fin = fopen(file, "r");
+    if (fin == NULL)
+    {
+        return NULL;
+    }
+    /* 成功打开文件 */
+
+    ar = initArray(0);
+    if (ar == NULL)
+    {
+        fclose(fin);
+        return NULL;
+    }
+    /* 成功初始化数组 */
+
+    /* 读取数组元素个数 */
+    ret = fread(&ar->n, sizeof(ar->n), 1, fin);
+    if (ret != 1)
+    {
+        fclose(fin);
+        destoryArray(ar);
+        return NULL;
+    }
+    /* 读取数组元素空间大小 */
+    ret = fread(&ar->size, sizeof(ar->size), 1, fin);
+    if (ret != 1)
+    {
+        fclose(fin);
+        destoryArray(ar);
+        return NULL;
+    }
+    /* 读取数组元素数据 */
+    ar->addr = malloc(ar->n * ar->size);
+    if (ar->addr == NULL)
+    {
+        fclose(fin);
+        destoryArray(ar);
+        return NULL;
+    }
+    ret = 0;
+    while (ret != ar->n) /* 保证读完 */
+    {
+        ret += fread(ar->addr + ret*ar->size, \
+                        ar->size, ar->n-ret, fin);
+        if (ferror(fin))
+        {
+            if (ret > 0) /* 已经读取到了一部分 */
+            {
+                break;
+            }
+            else
+            {
+                fclose(fin);
+                destoryArray(ar);
+                return NULL;
+            }
+        }
+        /* 有可能文件记录的个数与实际的不一样 */
+        else if (feof(fin)) 
+        {
+            break;
+        }
+    }
+    /* 文件数据丢失,没有之前存储的那么多 */
+    if (ar->n != ret)
+    {
+        void *temp;
+        ar->n = ret;
+        temp = realloc(ar->addr, ar->n*ar->size);
+    }
+
+    fclose(fin);   // 关闭打开的文件
+    return ar;
+}
+
+/*
+ * 函数名: destoryArray
+ * 函数功能: 销毁动态数组线性表
+ * 参数: 数组指针
+ * 返回值: 无
+*/
+void destoryArray(Array_t *ar)
+{
+    /* 释放数组元素空间 */
+    free(ar->addr);
+    /* 释放数组本身空间 */
+    free(ar);
+    /* 避免再次使用释放的空间 */
+    ar = NULL;
+}
+
+/*
+ * 函数名: isEmptyArray
+ * 函数功能: 判断是否是空数组
+ * 参数: 数组指针
+ * 返回值: 为空返回1 不为空返回0
+*/
+int isEmptyArray(Array_t *ar)
+{
+    /* 元素个数为零同时元素地址为NULL */
+    return ar->n==0 && ar->addr==NULL;
+}
+
+/*
+ * 函数名: arrayLenth
+ * 函数功能: 求数组的长度(元素个数)
+ * 参数: 数组指针
+ * 返回值: 返回数组的元素个数
+*/
+int arrayLenth(Array_t *ar)
+{
+    return ar->n;
+}

+ 168 - 0
stm32 动态数组/方式2/ArrayLib.h

@@ -0,0 +1,168 @@
+/***********************************************
+ * 文件名:arrayLib.h
+ * 文件描述:动态数组库函数头文件,包含了动态数组
+ *           所有功能的函数的声明以及类型定义等
+ * 编辑人:王廷云
+ * 编辑日期:2017-10-1
+ * 修改日期:2018-1-10
+************************************************/
+
+#ifndef  _ARRAY_LIB_H_
+#define  _ARRAY_LIB_H_
+
+/* 动态数组结构体封装 */
+typedef struct {
+    int  n;     // 动态数组的元素个数
+    int  size;  // 动态数组的元素空间大小
+    void *addr; // 动态数组的数据起始地址
+} Array_t;
+
+/* 比较函数类型定义 */
+typedef int compare_t(const void *data1, const void *data2);
+
+/******************************
+ * 以下为所有动态数组库函数声明
+*******************************/
+
+/* 
+ * 函数名: initArray
+ * 函数功能: 初始化动态数组
+ * 参数: 数组元素的空间大小
+ * 返回值: 成功则返回初始化后的动态数组指针
+ *         失败返回NULL 
+*/
+Array_t *initArray(int size);
+
+/*
+ * 函数名: appendArrayTail
+ * 函数功能: 尾部追加数组元素
+ * 参数: 1.需要追加的数组指针 2.需要追加的元素指针
+ * 返回值: 成功追加返回0 失败返回-1
+*/
+int appendArrayTail(Array_t *ar, const void *data);
+
+/*
+ * 函数名: appendArrayTop
+ * 函数功能: 头部追加数组元素
+ * 参数: 1.需要追加的数组指针 2.需要追加的元素指针
+ * 返回值: 成功追加返回0 失败返回-1
+*/
+int appendArrayTop(Array_t *ar, const void *data);
+
+/*
+ * 函数名: insertArrayByIndex
+ * 函数功能: 指定数组下标插入数组元素
+ *           <函数会检查下标的合法性>
+ * 参数: 1.需要插入数据的数组指针 2.需要插入的数据指针
+ *       2.数组下标值
+ * 返回值: 成功插入返回0 失败返回-1
+*/
+int insertArrayByIndex(Array_t *ar, const void *data, int idx);
+
+/*
+ * 函数名: travelArray
+ * 函数功能: 遍历动态数组元素
+ * 参数: 1.需要遍历的数组指针 2.回调函数
+ * 返回值: 无
+*/
+void travelArray(Array_t *ar, void (*func)(void *data));
+
+/*
+ * 函数名: searchArrayByIndex
+ * 函数功能: 根据下标查找数组元素<会检查下标的合法性>
+ * 参数: 1.需要查找的数组指针 2.查找的下标
+ * 返回值: 数据存在返回数据地址 不存在则返回NULL
+*/
+void *searchArrayByIndex(Array_t *ar, const int idx);
+
+/*
+ * 函数名: searchArrayOneByCond
+ * 函数功能: 根据条件查找元素,如果有多个则找第一个
+ * 参数: 1.需要查找的数组指针 2.比较回调函数指针 3.条件指针
+ * 返回值: 数据存在返回数据地址 不存在则返回NULL
+*/
+void *searchArrayOneByCond(Array_t *ar, compare_t *func, \
+                                        const void *key);
+/*
+ * 函数名: searchArrayByCond
+ * 函数功能: 根据条件查找元素
+ * 参数: 1.需要查找的数组指针 2.比较回调函数指针 3.条件指针
+ * 返回值: 数据存在返回数据数组 不存在则返回NULL
+*/
+Array_t *searchArrayByCond(Array_t *ar, compare_t *func, \
+                                        const void *key);
+/*
+ * 函数名: deleteArrayByIndex
+ * 函数功能: 根据下标删除数组元素
+ * 参数: 1.数组指针 2.下标
+ * 返回值: 成功删除返回0 失败返回-1
+*/
+int deleteArrayByIndex(Array_t *ar, const int idx);
+
+/*
+ * 函数名: deleteArrayOneByCond
+ * 函数功能: 根据条件删除数组元素,如果多个匹配则只删除第一个
+ * 参数: 1.数组指针 2.比较回调函数指针 3.条件
+ * 返回值: 成功删除返回0 失败返回-1
+*/
+int deleteArrayOneByCond(Array_t *ar, compare_t *func, \
+                                     const void *key);
+/*
+ * 函数名: deleteArrayByCond
+ * 函数功能: 根据条件删除所有匹配的数组元素
+ * 参数: 1.数组指针 2.比较回调函数指针 3.条件
+ * 返回值: 返回成功删除的元素个数
+*/
+int deleteArrayByCond(Array_t *ar, compare_t *func, \
+                                   const void *key);
+/*
+ * 函数名: sortArray
+ * 函数功能: 对数组元素进行排序
+ *          <至于按什么顺序排列则由用户的回调函数具体来实现>
+ * 参数: 1.数组指针 2.比较回调函数指针
+ * 返回值: 排序成功返回0 失败返回-1
+*/
+int sortArray(Array_t *ar, compare_t *func);
+
+/*
+ * 函数名: saveArrayToFile
+ * 函数功能: 把数组元素数据保存到文件中
+ * 参数: 1.数组指针 2.需要保存的文件
+ * 返回值: 保存成功返回0 失败返回-1
+*/
+int saveArrayToFile(Array_t *ar, const char *file);
+
+/*
+ * 函数名: loadArrayFromFile
+ * 函数功能: 从文件中加载数组数据
+ * 参数: 需要加载的文件
+ * 返回值: 成功加载返回数组指针 失败返回NULL
+*/
+Array_t *loadArrayFromFile(const char *file);
+
+/*
+ * 函数名: destoryArray
+ * 函数功能: 销毁动态数组线性表
+ * 参数: 数组指针
+ * 返回值: 无
+*/
+void destoryArray(Array_t *ar);
+
+/*
+ * 函数名: isEmptyArray
+ * 函数功能: 判断是否是空数组
+ * 参数: 数组指针
+ * 返回值: 为空返回1 不为空返回0
+*/
+int isEmptyArray(Array_t *ar);
+
+/*
+ * 函数名: arrayLenth
+ * 函数功能: 求数组的长度(元素个数)
+ * 参数: 数组指针
+ * 返回值: 返回数组的元素个数
+*/
+int arrayLenth(Array_t *ar);
+
+#endif  /* _ARRAY_LIB_H_ */
+

+ 59 - 0
stm32 消息队列/FifoQueue.c

@@ -0,0 +1,59 @@
+#include "FifoQueue.h"
+
+#define FALSE	false
+#define TRUE	true
+
+struct FifoQueue Fas_Tasks;	//fas协议任务
+struct FifoQueue Pub_Tasks; //公共任务
+
+/*
+使用:
+QueueInit(&SysMsg);
+if(QueueIn(&SysMsg,i) == QueueFull) break;
+if(QueueOut(&SysMsg,&sh) == QueueEmpty) break;
+*/
+
+//Queue Init
+void QueueInit ( struct FifoQueue *Queue )
+{
+    Queue->front = Queue->rear;//初始化时队列头队列首相连
+    Queue->count = 0;   //队列计数为0
+}
+
+// Queue In
+bool QueueIn ( struct FifoQueue *Queue, ElemType sdat ) //数据进入队列
+{
+    if ( ( Queue->front == Queue->rear ) && ( Queue->count == QueueSize ) )
+    {
+        // full //判断如果队列满了
+        return FALSE;    //返回队列满的标志
+    }
+    else
+    {
+        // in
+        Queue->dat[Queue->rear] = sdat;
+        Queue->rear = ( Queue->rear + 1 ) % QueueSize;
+        Queue->count = Queue->count + 1;
+        return TRUE;
+    }
+}
+
+// Queue Out
+bool QueueOut ( struct FifoQueue *Queue, ElemType *sdat )
+{
+    if ( ( Queue->front == Queue->rear ) && ( Queue->count == 0 ) )
+    {
+        // empty
+        return FALSE;
+    }
+    else
+    {
+        // out
+        *sdat = Queue->dat[Queue->front];
+        Queue->front = ( Queue->front + 1 ) % QueueSize;
+        Queue->count = Queue->count - 1;
+        return TRUE;
+    }
+}
+
+

+ 30 - 0
stm32 消息队列/FifoQueue.h

@@ -0,0 +1,30 @@
+#ifndef _FIFOQUEUE_H
+#define _FIFOQUEUE_H
+/* 包含的头文件-----------------------------------------------------------*/
+#include "stm32f10x.h"
+#include <stdbool.h>
+/* 宏定义 --------------------------------------------------------------------*/
+#define uint8 u8
+#define uint16 u16
+#define ElemType       uint8
+#define QueueSize      20 //fifo队列的大小
+/* 类型定义 -----------------------------------------------------------------*/
+struct FifoQueue
+{
+    uint16 front;     	//队列头
+    uint16 rear;        //队列尾
+    uint16 count;       //队列计数
+    ElemType dat[QueueSize];
+};
+/* 常量定义 ----------------------------------------------------------------*/
+extern struct FifoQueue Fas_Tasks;
+extern struct FifoQueue Pub_Tasks;
+
+/* 函数定义---------------------------------------------------------------- */
+extern void QueueInit ( struct FifoQueue *Queue );
+extern bool QueueIn ( struct FifoQueue *Queue, ElemType sdat );
+extern bool QueueOut ( struct FifoQueue *Queue, ElemType *sdat );
+
+#endif
+
+

+ 431 - 0
stm32 通用函数/Common/ArrauList/ArrayList.c

@@ -0,0 +1,431 @@
+#include "ArrayList.h"
+#include "ArrayList.h"
+
+void *new_byte(int size){
+    void *mem = malloc(size);
+	if(mem != NULL){
+         memset(mem,NULL,size);
+		 return mem;
+	}
+    return NULL;
+}
+
+/****************************************************************
+func:   new_array_list
+note:   创建一个new_array_list对象,并返回
+@head:  集合
+@value: value
+@size:  value的长度
+return: 返回创建的 ArrayList
+****************************************************************/
+ArrayList *new_array_list(ArrayList *head,void *value,int size){
+     ArrayList *obj = new_object(ArrayList);
+     if(obj != NULL){
+          reset_object(ArrayList,obj);  
+		  obj->sValue = size;
+		  obj->value = new_byte(size);
+		  if(obj->value != NULL){
+		  	  if(value != NULL){
+                   memcpy(obj->value,value,size);
+		  	  }
+			  return obj;
+		  }
+		  free(obj);
+	 }
+     return NULL;
+}
+
+
+/****************************************************************
+func:   add_array_list
+note:   添加value
+@head:  集合
+@value: value
+@size:  value的长度
+return: 返回head
+****************************************************************/
+ArrayList *add_array_list(ArrayList *head,void *value,int size){
+	ArrayList *obj = head;    
+    if(head == NULL){
+		return new_array_list(NULL,value,size);
+	}else{
+		obj = get_array_list_last(head);
+		obj->next = new_array_list(NULL,value,size);
+	    return head;
+	}
+}
+
+
+
+
+/****************************************************************
+func:   put_string_map
+note:   修改指定的key的值
+@head:  集合
+@key:   key
+@value: value
+return: 返回head
+****************************************************************/
+ArrayList *put_array_list(ArrayList *head,int indexof,void *value,int size){
+    ArrayList *obj = get_array_list(head,indexof);
+    if(obj != NULL){
+         free(obj->value);
+		 obj->sValue = size;
+		 obj->value = new_byte(size);
+		 if(value != NULL){
+		     memcpy(obj->value,value,size);
+		 }
+		 return head;
+	}
+	return NULL;
+}
+
+
+/****************************************************************
+func:   get_array_list
+note:   获得indexof位置的对象
+@head:      集合
+@indexof:   坐标位置
+return: 返回获得indexof的对象,如果indexof不存在返回NULL
+****************************************************************/
+ArrayList *get_array_list(ArrayList *head,int indexof){
+	ArrayList *obj = head;
+	test_object(head);
+    for(int i=0;obj != NULL;i++){
+		if(i == indexof){
+			return obj;
+		}
+        obj = obj->next;
+	}
+	return NULL;
+}
+
+
+
+/****************************************************************
+func:   get_array_list_previous
+note:   获得indexof的上一个对象
+@head:  集合
+@indexof:   indexof
+return: 返回获得indexof的上一个对象,如果indexof不存在返回NULL
+****************************************************************/
+ArrayList *get_array_list_previous(ArrayList *head,int indexof){
+	ArrayList *obj = head;
+	ArrayList *last = NULL;
+	test_object(head);
+    for(int i=0;obj != NULL;i++){
+		if(i == indexof){
+			return last;
+		}
+		last = obj;
+        obj = obj->next;
+	}
+	return NULL;
+}
+
+/****************************************************************
+func:   get_array_list_last
+note:   获得集合最后的对象
+@head:  集合
+return: 返回获得集合最后的对象
+****************************************************************/
+ArrayList *get_array_list_last(ArrayList *head){
+	ArrayList *obj = head;
+	test_object(head);
+    while(obj->next != NULL){
+        obj = obj->next;
+	}
+	return obj;
+}
+
+
+/****************************************************************
+func:   get_array_list_size
+note:   获得集合的对象数量
+@head:  集合
+return: 返回集合的对象数量
+****************************************************************/
+int get_array_list_size(ArrayList *head){
+	int i = 0;
+	ArrayList *obj = head;
+	test_object(head);
+    for(i=0;obj != NULL;i++){
+        obj = obj->next;
+	}
+	return i;
+}
+
+
+
+
+/****************************************************************
+func:   remove_string_map
+note:   移除集合中的对象
+@head:  集合
+return: 移除集合中的对象后一个StringMap对象
+****************************************************************/
+ArrayList *remove_array_list(ArrayList *head){
+	 ArrayList *obj = head->next;
+	 test_object(head);
+	 free(head->value);
+	 free(head);
+	 return obj;
+}
+
+
+
+
+/****************************************************************
+func:   close_array_list
+note:   移除所有集合中的对象
+@head:  集合
+return: NULL
+****************************************************************/
+ArrayList *close_array_list(ArrayList *head){
+	ArrayList *obj = head;
+	test_object(head);
+	for(int i=0;obj != NULL;i++){
+        obj = remove_array_list(obj);
+	}
+	return NULL;
+}
+
+
+
+
+/****************************************************************
+func:   remove_array_list_index
+note:   移除坐标的对象
+@head:  集合
+@index: 坐标
+return: 返回移除后的集合,如果没有返回NULL
+****************************************************************/
+ArrayList *remove_array_list_index(ArrayList *head,int index){
+	ArrayList *obj = NULL;
+	ArrayList *previous = NULL;;
+	test_object(head);
+	if(index < get_array_list_size(head)){
+        previous = get_array_list_previous(head,index);
+        if(previous == NULL || index == 0){
+			return remove_array_list(head);
+		}else{
+		    obj = get_array_list(head,index);
+			previous->next = remove_array_list(obj);
+			return head;
+		}
+	}
+	return NULL;
+}
+
+
+/****************************************************************
+func:   get_array_list_memsize
+note:   获得集合字符串内存块大小
+@head:  集合
+return: 字符串的内存块大小
+****************************************************************/
+int get_array_list_memsize(ArrayList *head){
+	int memsize = 0;
+	ArrayList *obj = head;
+    for(int i=0;obj != NULL;i++){
+		memsize += obj->sValue;
+        obj = obj->next;
+	}
+	return memsize;
+}
+
+
+
+
+/****************************************************************
+func:   get_array_list_disksize
+note:   获得当前对象占用磁盘空间大小
+@head:  集合
+return: 占用磁盘空间大小
+****************************************************************/
+int get_array_list_disksize(ArrayList *head){
+	int memsize = get_array_list_memsize(head);
+    int objsize = get_array_list_size(head) * (sizeof(int)*1);
+    int sum =  sizeof(int) + objsize + memsize;
+    return sum;
+}
+
+
+
+
+uint8_t *write_buff_array_list(ArrayList *head,uint8_t *mBuf){
+	
+	if(mBuf == NULL) return NULL;
+	
+	memcpy(mBuf,&head->sValue,sizeof(int));
+	mBuf = mBuf + sizeof(int);
+	
+	memcpy(mBuf,head->value,head->sValue);
+	mBuf = mBuf + head->sValue;
+	
+    return mBuf;
+}
+
+
+uint8_t *read_buff_array_list(ArrayList *head,uint8_t *mBuf){
+	
+	if(mBuf == NULL) return NULL;
+	
+	memcpy(&head->sValue,mBuf,sizeof(int));
+	mBuf = mBuf + sizeof(int);
+	
+    head->value = new_byte(head->sValue);
+    if(head->value != NULL ){
+		memcpy(head->value,mBuf,head->sValue);
+		mBuf = mBuf + head->sValue;
+		return mBuf;
+    }
+    return NULL;
+}
+
+
+/****************************************************************
+func:   pack_array_list
+note:   打包集合数据,打包方式
+        int(总的长度) + ( (int)sValue + (data)value  ...
+@head:  集合
+return: 返回打包好的PackMap数据对象,失败返回NULL
+****************************************************************/
+PackMap *pack_array_list(ArrayList *head){
+	PackMap *pack = NULL;
+	ArrayList *obj = head;
+    int sum = get_array_list_disksize(head);
+    uint8_t *buf = new_byte(sum);
+	uint8_t *mBuf = buf;
+    if(buf != NULL){
+		 memset(buf,0,sum);
+		 memcpy(mBuf,&sum,sizeof(int));
+	     mBuf = mBuf + sizeof(int);
+         while(obj != NULL){
+			 mBuf = write_buff_array_list(obj,mBuf);
+	         obj = obj->next;
+		 }
+		 pack = new_object(PackMap);
+		 pack->sum = sum;
+		 pack->buff = buf;
+	     return pack;
+	}
+    return NULL;
+}
+
+
+
+
+/****************************************************************
+func:   unpack_array_list
+note:   解包ArrayList集合数据
+@pack:  PackMap对象
+return: 返回ArrayList集合,失败返回NULL
+****************************************************************/
+ArrayList *unpack_array_list(PackMap *pack){
+	ArrayList *head = NULL;
+	ArrayList *obj = NULL;
+	ArrayList *temp = NULL;
+	uint8_t *mBuf = NULL;
+	int sum = 0;
+	if(pack != NULL){
+		mBuf = pack->buff;
+		memcpy(&sum,mBuf,sizeof(int));
+	    mBuf = mBuf + sizeof(int);
+		head = new_object(ArrayList);
+		mBuf = read_buff_array_list(head,mBuf);
+		obj = head;
+		if(pack->sum == sum){
+	        while((pack->buff + sum) > mBuf && mBuf != NULL){
+				temp = new_object(ArrayList);
+				mBuf = read_buff_array_list(temp,mBuf);
+	            obj->next = temp;
+				obj->next->next = NULL;
+				obj = obj->next;
+			}
+			return head;
+		}
+	}
+	return NULL;
+}
+
+
+#if 1
+void test_ArrayList(void){
+   uint8_t buf[50];
+   ArrayList *temp;
+   ArrayList *object = new_array_list(NULL,"0123456789",10);
+   
+   printf("\rArrayList --> test\r");
+   add_array_list(object,"A123456\0",8);
+   add_array_list(object,"B123456\0",8);
+   add_array_list(object,"C123456\0",8);
+   add_array_list(object,"D123456\0",8);
+#if 1
+   //string 类型
+   add_array_list(object,"123",3);
+   temp = get_array_list(object,1);
+   char *stp = temp->value;
+   printf("0x%x 0x%x 0x%x\r",stp[0],stp[1],stp[2]);
+
+   //int 类型
+   int in = 1024;
+   add_array_list(object,&in,sizeof(int));
+   int *inp = get_array_list(object,2)->value;
+   printf("int type p:%d\r",*inp);
+
+   //double 类型
+   double dou = 1024.5555;
+   add_array_list(object,&dou,sizeof(double));
+   double *pdo = get_array_list(object,3)->value;
+   printf("double type p:%f\r",*pdo);
+#endif
+   
+  /* for(int i=0;i<1000;i++){
+   	   snprintf(buf,50,"Guihui->%d \0",i);
+       add_array_list(object,buf,strlen(buf));
+   }*/
+
+   PackMap *pack = pack_array_list(object);
+   if(pack != NULL){
+   	   printf("ArrayList PackMap Test\r");
+       object = close_array_list(object);
+       for(int i=0; i<pack->sum; i++){
+            printf("%x ",pack->buff[i]);
+	   }
+	   printf("\r");
+       /*解包*/
+	   object = unpack_array_list(pack);
+	   
+   }
+
+   temp = remove_array_list_index(object,0);
+   if(temp == NULL){
+       printf("ERROR remove index\r");
+   }else{
+	   object = temp;
+   }
+
+   temp = remove_array_list_index(object,3);
+   if(temp == NULL){
+	   printf("ERROR remove index\r");
+   }else{
+	   object = temp;
+   }
+
+   //object = remove_array_list_index(object,3);
+
+   temp = object; 
+   for(int i=0; temp != NULL; i++){
+   	   snprintf(buf,temp->sValue,"%s",temp->value);
+   	   printf("%0.2d -> %s\r",i,buf);
+	   temp = temp->next;
+   }
+   
+   close_array_list(object);
+}
+
+#endif
+
+
+

+ 33 - 0
stm32 通用函数/Common/ArrauList/ArrayList.h

@@ -0,0 +1,33 @@
+#ifndef _ARRAY_LIST_H
+#define _ARRAY_LIST_H
+
+//#include "PackMap.h"
+
+
+typedef struct ArrayList{
+     int sValue;
+	 void *value;
+	 struct ArrayList *next;
+}ArrayList;
+
+
+extern ArrayList *new_array_list(ArrayList *head,void *value,int size);
+extern ArrayList *add_array_list(ArrayList *head,void *value,int size);
+extern ArrayList *put_array_list(ArrayList *head,int indexof,void *value,int size);
+extern ArrayList *get_array_list(ArrayList *head,int indexof);
+extern ArrayList *get_array_list_previous(ArrayList *head,int indexof);
+extern ArrayList *get_array_list_last(ArrayList *head);
+extern int get_array_list_size(ArrayList *head);
+extern ArrayList *remove_array_list(ArrayList *head);
+extern ArrayList *close_array_list(ArrayList *head);
+extern ArrayList *remove_array_list_index(ArrayList *head,int index);
+extern int get_array_list_memsize(ArrayList *head);
+extern int get_array_list_disksize(ArrayList *head);
+extern PackMap *pack_array_list(ArrayList *head);
+extern ArrayList *unpack_array_list(PackMap *pack);
+
+
+#endif
+
+
+

+ 21 - 0
stm32 通用函数/Common/ArrauList/PackMap.c

@@ -0,0 +1,21 @@
+#include "PackMap.h"
+
+
+
+/****************************************************************
+func:   remove_pack_map
+note:   ÒÆ³ýPackMap¶ÔÏó
+@pack:  ¶ÔÏó
+return: ·µ»ØNULL
+****************************************************************/
+int remove_pack_map(PackMap *pack){
+    free(pack->buff);
+	free(pack);
+    return NULL;
+}
+
+
+
+
+
+

+ 20 - 0
stm32 通用函数/Common/ArrauList/PackMap.h

@@ -0,0 +1,20 @@
+#ifndef _PACK_MAP_H
+#define _PACK_MAP_H
+
+/*
+* PackMap是通用类,所有对像都可以用它来打包成PackMap并保存
+*/
+
+#include "SystemMemory.h"
+
+typedef struct {
+    int sum;
+	uint8_t *buff;
+}PackMap;
+
+
+extern int remove_pack_map(PackMap *pack);
+
+
+#endif
+

+ 22 - 0
stm32 通用函数/Common/ArrauList/SystemMemory.c

@@ -0,0 +1,22 @@
+#include "SystemMemory.h"
+#include "StringMap.h"
+#include "PackMap.h"
+#include "ArrayList.h"
+
+
+void *new_byte(int size){
+    void *mem = malloc(size);
+	if(mem != NULL){
+         memset(mem,NULL,size);
+		 return mem;
+	}
+    return NULL;
+}
+
+
+void test_memory(void){
+
+
+}
+
+

+ 24 - 0
stm32 通用函数/Common/ArrauList/SystemMemory.h

@@ -0,0 +1,24 @@
+#ifndef _SYTEM_MEMORY_H
+#define _SYTEM_MEMORY_H
+
+#include <mqx.h>
+#include <bsp.h>
+#include <stdlib.h>
+
+
+#define test_object(object)  if(object == NULL)return NULL
+
+
+#define new_string(str)               (char*)malloc(strlen(str)+1)
+#define new_object(object)            (object*)malloc(sizeof(object))
+#define reset_object(object,p)         memset((void*)p,NULL,sizeof(object))
+
+extern void *new_byte(int size);
+
+
+extern void test_memory(void);
+
+
+#endif
+
+

+ 435 - 0
stm32 通用函数/Common/ArrayList.c

@@ -0,0 +1,435 @@
+#include "ArrayList.h"
+
+#define test_object(object)  if(object == NULL)return NULL
+#define new_string(str)               (char*)malloc(strlen(str)+1)
+#define new_object(object)            (object*)malloc(sizeof(object))
+#define reset_object(object,p)         memset((void*)p,NULL,sizeof(object))
+
+void *new_byte(int size){
+    void *mem = malloc(size);
+	if(mem != NULL){
+         memset(mem,NULL,size);
+		 return mem;
+	}
+    return NULL;
+}
+
+/****************************************************************
+func:   new_array_list
+note:   创建一个new_array_list对象,并返回
+@head:  集合
+@value: value
+@size:  value的长度
+return: 返回创建的 ArrayList
+****************************************************************/
+ArrayList *new_array_list(ArrayList *head,void *value,int size){
+     ArrayList *obj = new_object(ArrayList);
+     if(obj != NULL){
+          reset_object(ArrayList,obj);  
+		  obj->sValue = size;
+		  obj->value = new_byte(size);
+		  if(obj->value != NULL){
+		  	  if(value != NULL){
+                   memcpy(obj->value,value,size);
+		  	  }
+			  return obj;
+		  }
+		  free(obj);
+	 }
+     return NULL;
+}
+
+
+/****************************************************************
+func:   add_array_list
+note:   添加value
+@head:  集合
+@value: value
+@size:  value的长度
+return: 返回head
+****************************************************************/
+ArrayList *add_array_list(ArrayList *head,void *value,int size){
+	ArrayList *obj = head;    
+    if(head == NULL){
+		return new_array_list(NULL,value,size);
+	}else{
+		obj = get_array_list_last(head);
+		obj->next = new_array_list(NULL,value,size);
+	    return head;
+	}
+}
+
+
+
+
+/****************************************************************
+func:   put_string_map
+note:   修改指定的key的值
+@head:  集合
+@key:   key
+@value: value
+return: 返回head
+****************************************************************/
+ArrayList *put_array_list(ArrayList *head,int indexof,void *value,int size){
+    ArrayList *obj = get_array_list(head,indexof);
+    if(obj != NULL){
+         free(obj->value);
+		 obj->sValue = size;
+		 obj->value = new_byte(size);
+		 if(value != NULL){
+		     memcpy(obj->value,value,size);
+		 }
+		 return head;
+	}
+	return NULL;
+}
+
+
+/****************************************************************
+func:   get_array_list
+note:   获得indexof位置的对象
+@head:      集合
+@indexof:   坐标位置
+return: 返回获得indexof的对象,如果indexof不存在返回NULL
+****************************************************************/
+ArrayList *get_array_list(ArrayList *head,int indexof){
+	ArrayList *obj = head;
+	test_object(head);
+    for(int i=0;obj != NULL;i++){
+		if(i == indexof){
+			return obj;
+		}
+        obj = obj->next;
+	}
+	return NULL;
+}
+
+
+
+/****************************************************************
+func:   get_array_list_previous
+note:   获得indexof的上一个对象
+@head:  集合
+@indexof:   indexof
+return: 返回获得indexof的上一个对象,如果indexof不存在返回NULL
+****************************************************************/
+ArrayList *get_array_list_previous(ArrayList *head,int indexof){
+	ArrayList *obj = head;
+	ArrayList *last = NULL;
+	test_object(head);
+    for(int i=0;obj != NULL;i++){
+		if(i == indexof){
+			return last;
+		}
+		last = obj;
+        obj = obj->next;
+	}
+	return NULL;
+}
+
+/****************************************************************
+func:   get_array_list_last
+note:   获得集合最后的对象
+@head:  集合
+return: 返回获得集合最后的对象
+****************************************************************/
+ArrayList *get_array_list_last(ArrayList *head){
+	ArrayList *obj = head;
+	test_object(head);
+    while(obj->next != NULL){
+        obj = obj->next;
+	}
+	return obj;
+}
+
+
+/****************************************************************
+func:   get_array_list_size
+note:   获得集合的对象数量
+@head:  集合
+return: 返回集合的对象数量
+****************************************************************/
+int get_array_list_size(ArrayList *head){
+	int i = 0;
+	ArrayList *obj = head;
+	test_object(head);
+    for(i=0;obj != NULL;i++){
+        obj = obj->next;
+	}
+	return i;
+}
+
+
+
+
+/****************************************************************
+func:   remove_string_map
+note:   移除集合中的对象
+@head:  集合
+return: 移除集合中的对象后一个StringMap对象
+****************************************************************/
+ArrayList *remove_array_list(ArrayList *head){
+	 ArrayList *obj = head->next;
+	 test_object(head);
+	 free(head->value);
+	 free(head);
+	 return obj;
+}
+
+
+
+
+/****************************************************************
+func:   close_array_list
+note:   移除所有集合中的对象
+@head:  集合
+return: NULL
+****************************************************************/
+ArrayList *close_array_list(ArrayList *head){
+	ArrayList *obj = head;
+	test_object(head);
+	for(int i=0;obj != NULL;i++){
+        obj = remove_array_list(obj);
+	}
+	return NULL;
+}
+
+
+
+
+/****************************************************************
+func:   remove_array_list_index
+note:   移除坐标的对象
+@head:  集合
+@index: 坐标
+return: 返回移除后的集合,如果没有返回NULL
+****************************************************************/
+ArrayList *remove_array_list_index(ArrayList *head,int index){
+	ArrayList *obj = NULL;
+	ArrayList *previous = NULL;;
+	test_object(head);
+	if(index < get_array_list_size(head)){
+        previous = get_array_list_previous(head,index);
+        if(previous == NULL || index == 0){
+			return remove_array_list(head);
+		}else{
+		    obj = get_array_list(head,index);
+			previous->next = remove_array_list(obj);
+			return head;
+		}
+	}
+	return NULL;
+}
+
+
+/****************************************************************
+func:   get_array_list_memsize
+note:   获得集合字符串内存块大小
+@head:  集合
+return: 字符串的内存块大小
+****************************************************************/
+int get_array_list_memsize(ArrayList *head){
+	int memsize = 0;
+	ArrayList *obj = head;
+    for(int i=0;obj != NULL;i++){
+		memsize += obj->sValue;
+        obj = obj->next;
+	}
+	return memsize;
+}
+
+
+
+
+/****************************************************************
+func:   get_array_list_disksize
+note:   获得当前对象占用磁盘空间大小
+@head:  集合
+return: 占用磁盘空间大小
+****************************************************************/
+int get_array_list_disksize(ArrayList *head){
+	int memsize = get_array_list_memsize(head);
+    int objsize = get_array_list_size(head) * (sizeof(int)*1);
+    int sum =  sizeof(int) + objsize + memsize;
+    return sum;
+}
+
+
+
+
+uint8_t *write_buff_array_list(ArrayList *head,uint8_t *mBuf){
+	
+	if(mBuf == NULL) return NULL;
+	
+	memcpy(mBuf,&head->sValue,sizeof(int));
+	mBuf = mBuf + sizeof(int);
+	
+	memcpy(mBuf,head->value,head->sValue);
+	mBuf = mBuf + head->sValue;
+	
+    return mBuf;
+}
+
+
+uint8_t *read_buff_array_list(ArrayList *head,uint8_t *mBuf){
+	
+	if(mBuf == NULL) return NULL;
+	
+	memcpy(&head->sValue,mBuf,sizeof(int));
+	mBuf = mBuf + sizeof(int);
+	
+    head->value = new_byte(head->sValue);
+    if(head->value != NULL ){
+		memcpy(head->value,mBuf,head->sValue);
+		mBuf = mBuf + head->sValue;
+		return mBuf;
+    }
+    return NULL;
+}
+
+
+/****************************************************************
+func:   pack_array_list
+note:   打包集合数据,打包方式
+        int(总的长度) + ( (int)sValue + (data)value  ...
+@head:  集合
+return: 返回打包好的PackMap数据对象,失败返回NULL
+****************************************************************/
+PackMap *pack_array_list(ArrayList *head){
+	PackMap *pack = NULL;
+	ArrayList *obj = head;
+    int sum = get_array_list_disksize(head);
+    uint8_t *buf = new_byte(sum);
+	uint8_t *mBuf = buf;
+    if(buf != NULL){
+		 memset(buf,0,sum);
+		 memcpy(mBuf,&sum,sizeof(int));
+	     mBuf = mBuf + sizeof(int);
+         while(obj != NULL){
+			 mBuf = write_buff_array_list(obj,mBuf);
+	         obj = obj->next;
+		 }
+		 pack = new_object(PackMap);
+		 pack->sum = sum;
+		 pack->buff = buf;
+	     return pack;
+	}
+    return NULL;
+}
+
+
+
+
+/****************************************************************
+func:   unpack_array_list
+note:   解包ArrayList集合数据
+@pack:  PackMap对象
+return: 返回ArrayList集合,失败返回NULL
+****************************************************************/
+ArrayList *unpack_array_list(PackMap *pack){
+	ArrayList *head = NULL;
+	ArrayList *obj = NULL;
+	ArrayList *temp = NULL;
+	uint8_t *mBuf = NULL;
+	int sum = 0;
+	if(pack != NULL){
+		mBuf = pack->buff;
+		memcpy(&sum,mBuf,sizeof(int));
+	    mBuf = mBuf + sizeof(int);
+		head = new_object(ArrayList);
+		mBuf = read_buff_array_list(head,mBuf);
+		obj = head;
+		if(pack->sum == sum){
+	        while((pack->buff + sum) > mBuf && mBuf != NULL){
+				temp = new_object(ArrayList);
+				mBuf = read_buff_array_list(temp,mBuf);
+	            obj->next = temp;
+				obj->next->next = NULL;
+				obj = obj->next;
+			}
+			return head;
+		}
+	}
+	return NULL;
+}
+
+
+#if 0
+void test_ArrayList(void){
+   uint8_t buf[50];
+   ArrayList *temp;
+   ArrayList *object = new_array_list(NULL,"0123456789",10);
+   
+   printf("\rArrayList --> test\r");
+   add_array_list(object,"A123456\0",8);
+   add_array_list(object,"B123456\0",8);
+   add_array_list(object,"C123456\0",8);
+   add_array_list(object,"D123456\0",8);
+#if 1
+   //string 类型
+   add_array_list(object,"123",3);
+   temp = get_array_list(object,1);
+   char *stp = temp->value;
+   printf("0x%x 0x%x 0x%x\r",stp[0],stp[1],stp[2]);
+
+   //int 类型
+   int in = 1024;
+   add_array_list(object,&in,sizeof(int));
+   int *inp = get_array_list(object,2)->value;
+   printf("int type p:%d\r",*inp);
+
+   //double 类型
+   double dou = 1024.5555;
+   add_array_list(object,&dou,sizeof(double));
+   double *pdo = get_array_list(object,3)->value;
+   printf("double type p:%f\r",*pdo);
+#endif
+   
+  /* for(int i=0;i<1000;i++){
+   	   snprintf(buf,50,"Guihui->%d \0",i);
+       add_array_list(object,buf,strlen(buf));
+   }*/
+
+   PackMap *pack = pack_array_list(object);
+   if(pack != NULL){
+   	   printf("ArrayList PackMap Test\r");
+       object = close_array_list(object);
+       for(int i=0; i<pack->sum; i++){
+            printf("%x ",pack->buff[i]);
+	   }
+	   printf("\r");
+       /*解包*/
+	   object = unpack_array_list(pack);
+	   
+   }
+
+   temp = remove_array_list_index(object,0);
+   if(temp == NULL){
+       printf("ERROR remove index\r");
+   }else{
+	   object = temp;
+   }
+
+   temp = remove_array_list_index(object,3);
+   if(temp == NULL){
+	   printf("ERROR remove index\r");
+   }else{
+	   object = temp;
+   }
+
+   //object = remove_array_list_index(object,3);
+
+   temp = object; 
+   for(int i=0; temp != NULL; i++){
+   	   snprintf((char *)buf,temp->sValue,"%s",(char *)temp->value);
+   	   printf("%0.2d -> %s\r",i,buf);
+	   temp = temp->next;
+   }
+   
+   close_array_list(object);
+}
+
+#endif
+
+
+

+ 39 - 0
stm32 通用函数/Common/ArrayList.h

@@ -0,0 +1,39 @@
+#ifndef _ARRAY_LIST_H
+#define _ARRAY_LIST_H
+#include <stdio.h>
+#include <stdint.h>             //uint8
+#include <string.h>             //memcpy
+#include <stdlib.h>             //malloc¡¢free
+
+typedef struct {
+    int sum;
+	uint8_t *buff;
+}PackMap;
+
+typedef struct ArrayList{
+     int sValue;
+	 void *value;
+	 struct ArrayList *next;
+}ArrayList;
+
+
+extern ArrayList *new_array_list(ArrayList *head,void *value,int size);
+extern ArrayList *add_array_list(ArrayList *head,void *value,int size);
+extern ArrayList *put_array_list(ArrayList *head,int indexof,void *value,int size);
+extern ArrayList *get_array_list(ArrayList *head,int indexof);
+extern ArrayList *get_array_list_previous(ArrayList *head,int indexof);
+extern ArrayList *get_array_list_last(ArrayList *head);
+extern int get_array_list_size(ArrayList *head);
+extern ArrayList *remove_array_list(ArrayList *head);
+extern ArrayList *close_array_list(ArrayList *head);
+extern ArrayList *remove_array_list_index(ArrayList *head,int index);
+extern int get_array_list_memsize(ArrayList *head);
+extern int get_array_list_disksize(ArrayList *head);
+extern PackMap *pack_array_list(ArrayList *head);
+extern ArrayList *unpack_array_list(PackMap *pack);
+
+
+#endif
+
+
+

+ 192 - 0
stm32 通用函数/Common/Delay.c

@@ -0,0 +1,192 @@
+#include "delay.h"
+//#include "sys.h"
+//////////////////////////////////////////////////////////////////////////////////
+//如果使用ucos,则包括下面的头文件即可.
+#if SYSTEM_SUPPORT_UCOS
+#include "includes.h"					//ucos 使用	  
+#endif
+//////////////////////////////////////////////////////////////////////////////////
+//本程序只供学习使用,未经作者许可,不得用于其它任何用途
+//ALIENTEK STM32开发板
+//使用SysTick的普通计数模式对延迟进行管理
+//包括delay_us,delay_ms
+//正点原子@ALIENTEK
+//技术论坛:www.openedv.com
+//修改日期:2012/9/2
+//版本:V1.5
+//版权所有,盗版必究。
+//Copyright(C) 广州市星翼电子科技有限公司 2009-2019
+//All rights reserved
+//********************************************************************************
+//V1.2修改说明
+//修正了中断中调用出现死循环的错误
+//防止延时不准确,采用do while结构!
+
+//V1.3修改说明
+//增加了对UCOSII延时的支持.
+//如果使用ucosII,delay_init会自动设置SYSTICK的值,使之与ucos的TICKS_PER_SEC对应.
+//delay_ms和delay_us也进行了针对ucos的改造.
+//delay_us可以在ucos下使用,而且准确度很高,更重要的是没有占用额外的定时器.
+//delay_ms在ucos下,可以当成OSTimeDly来用,在未启动ucos时,它采用delay_us实现,从而准确延时
+//可以用来初始化外设,在启动了ucos之后delay_ms根据延时的长短,选择OSTimeDly实现或者delay_us实现.
+
+//V1.4修改说明 20110929
+//修改了使用ucos,但是ucos未启动的时候,delay_ms中中断无法响应的bug.
+//V1.5修改说明 20120902
+//在delay_us加入ucos上锁,防止由于ucos打断delay_us的执行,可能导致的延时不准。
+//////////////////////////////////////////////////////////////////////////////////
+static u8  fac_us = 0; //us延时倍乘数
+static u16 fac_ms = 0; //ms延时倍乘数
+#ifdef OS_CRITICAL_METHOD 	//如果OS_CRITICAL_METHOD定义了,说明使用ucosII了.
+//systick中断服务函数,使用ucos时用到
+void SysTick_Handler ( void )
+{
+    OSIntEnter();		//进入中断
+    OSTimeTick();       //调用ucos的时钟服务程序
+    OSIntExit();        //触发任务切换软中断
+}
+#endif
+
+//初始化延迟函数
+//当使用ucos的时候,此函数会初始化ucos的时钟节拍
+//SYSTICK的时钟固定为HCLK时钟的1/8
+//SYSCLK:系统时钟
+void delay_init()
+{
+
+#ifdef OS_CRITICAL_METHOD 	//如果OS_CRITICAL_METHOD定义了,说明使用ucosII了.
+    u32 reload;
+#endif
+    SysTick_CLKSourceConfig ( SysTick_CLKSource_HCLK_Div8 );	//选择外部时钟  HCLK/8
+    fac_us = SystemCoreClock / 8000000;	//为系统时钟的1/8
+
+#ifdef OS_CRITICAL_METHOD 	//如果OS_CRITICAL_METHOD定义了,说明使用ucosII了.
+    reload = SystemCoreClock / 8000000;		//每秒钟的计数次数 单位为K
+    reload *= 1000000 / OS_TICKS_PER_SEC; //根据OS_TICKS_PER_SEC设定溢出时间
+    //reload为24位寄存器,最大值:16777216,在72M下,约合1.86s左右
+    fac_ms = 1000 / OS_TICKS_PER_SEC; //代表ucos可以延时的最少单位
+    SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;   	//开启SYSTICK中断
+    SysTick->LOAD = reload; 	//每1/OS_TICKS_PER_SEC秒中断一次
+    SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;   	//开启SYSTICK
+#else
+    fac_ms = ( u16 ) fac_us * 1000; //非ucos下,代表每个ms需要的systick时钟数
+#endif
+}
+
+#ifdef OS_CRITICAL_METHOD	//使用了ucos
+//延时nus
+//nus为要延时的us数.
+void delay_us ( u32 nus )
+{
+    u32 ticks;
+    u32 told, tnow, tcnt = 0;
+    u32 reload = SysTick->LOAD;	//LOAD的值
+    ticks = nus * fac_us; 			//需要的节拍数
+    tcnt = 0;
+    told = SysTick->VAL;        	//刚进入时的计数器值
+
+    while ( 1 )
+    {
+        tnow = SysTick->VAL;
+
+        if ( tnow != told )
+        {
+            if ( tnow < told ) tcnt += told - tnow; //这里注意一下SYSTICK是一个递减的计数器就可以了.
+            else tcnt += reload - tnow + told;
+
+            told = tnow;
+
+            if ( tcnt >= ticks ) break; //时间超过/等于要延迟的时间,则退出.
+        }
+    };
+}
+//延时nms
+//nms:要延时的ms数
+void delay_ms ( u16 nms )
+{
+    if ( OSRunning == TRUE ) //如果os已经在跑了
+    {
+        if ( nms >= fac_ms ) //延时的时间大于ucos的最少时间周期
+        {
+            OSTimeDly ( nms / fac_ms ); //ucos延时
+        }
+
+        nms %= fac_ms;				//ucos已经无法提供这么小的延时了,采用普通方式延时
+    }
+
+    delay_us ( ( u32 ) ( nms * 1000 ) );	//普通方式延时,此时ucos无法启动调度.
+}
+#else//不用ucos时
+//延时nus
+//nus为要延时的us数.
+void delay_us ( u32 nus )
+{
+    u32 temp;
+    SysTick->LOAD = nus * fac_us; //时间加载
+    SysTick->VAL = 0x00;      //清空计数器
+    SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk ;        //开始倒数
+
+    do
+    {
+        temp = SysTick->CTRL;
+    }
+    while ( temp & 0x01 && ! ( temp & ( 1 << 16 ) ) ); //等待时间到达
+
+    SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;     //关闭计数器
+    SysTick->VAL = 0X00;      //清空计数器
+}
+//延时nms
+//注意nms的范围
+//SysTick->LOAD为24位寄存器,所以,最大延时为:
+//nms<=0xffffff*8*1000/SYSCLK
+//SYSCLK单位为Hz,nms单位为ms
+//对72M条件下,nms<=1864
+void delay_ms ( u16 nms )
+{
+    u32 temp;
+    SysTick->LOAD = ( u32 ) nms * fac_ms; //时间加载(SysTick->LOAD为24bit)
+    SysTick->VAL = 0x00;          //清空计数器
+    SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk ;        //开始倒数
+
+    do
+    {
+        temp = SysTick->CTRL;
+    }
+    while ( temp & 0x01 && ! ( temp & ( 1 << 16 ) ) ); //等待时间到达
+
+    SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;     //关闭计数器
+    SysTick->VAL = 0X00;      //清空计数器
+}
+#endif
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ 68 - 0
stm32 通用函数/Common/Delay.h

@@ -0,0 +1,68 @@
+#ifndef __DELAY_H
+#define __DELAY_H
+//#include "sys.h"
+#include "stm32f10x.h"
+//////////////////////////////////////////////////////////////////////////////////
+//本程序只供学习使用,未经作者许可,不得用于其它任何用途
+//ALIENTEK STM32开发板
+//使用SysTick的普通计数模式对延迟进行管理
+//包括delay_us,delay_ms
+//正点原子@ALIENTEK
+//技术论坛:www.openedv.com
+//修改日期:2012/9/2
+//版本:V1.5
+//版权所有,盗版必究。
+//Copyright(C) 广州市星翼电子科技有限公司 2009-2019
+//All rights reserved
+//********************************************************************************
+//V1.2修改说明
+//修正了中断中调用出现死循环的错误
+//防止延时不准确,采用do while结构!
+
+//V1.3修改说明
+//增加了对UCOSII延时的支持.
+//如果使用ucosII,delay_init会自动设置SYSTICK的值,使之与ucos的TICKS_PER_SEC对应.
+//delay_ms和delay_us也进行了针对ucos的改造.
+//delay_us可以在ucos下使用,而且准确度很高,更重要的是没有占用额外的定时器.
+//delay_ms在ucos下,可以当成OSTimeDly来用,在未启动ucos时,它采用delay_us实现,从而准确延时
+//可以用来初始化外设,在启动了ucos之后delay_ms根据延时的长短,选择OSTimeDly实现或者delay_us实现.
+
+//V1.4修改说明 20110929
+//修改了使用ucos,但是ucos未启动的时候,delay_ms中中断无法响应的bug.
+//V1.5修改说明 20120902
+//在delay_us加入ucos上锁,防止由于ucos打断delay_us的执行,可能导致的延时不准。
+//////////////////////////////////////////////////////////////////////////////////
+void delay_init ( void );
+void delay_ms ( u16 nms );
+void delay_us ( u32 nus );
+
+#endif
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ 94 - 0
stm32 通用函数/Common/Delay1.c

@@ -0,0 +1,94 @@
+#include <stm32f10x.h>
+#include "delay.h"
+
+//使用SysTick的普通计数模式对延迟进行管理
+
+
+
+
+//2010/5/27
+
+#if 0
+static u8  fac_us = 0; //us延时倍乘数
+static u16 fac_ms = 0; //ms延时倍乘数
+//初始化延迟函数
+//SYSTICK的时钟固定为HCLK时钟的1/8
+//SYSCLK:系统时钟
+void delay_init ( u8 SYSCLK )
+{
+    SysTick->CTRL &= 0xfffffffb; //bit2清空,选择外部时钟  HCLK/8
+    fac_us = SYSCLK / 8;
+    fac_ms = ( u16 ) fac_us * 1000;
+}
+//延时nms
+//注意nms的范围
+//SysTick->LOAD为24位寄存器,所以,最大延时为:
+//nms<=0xffffff*8*1000/SYSCLK
+//SYSCLK单位为Hz,nms单位为ms
+//对72M条件下,nms<=1864
+void delay_ms ( u16 nms )
+{
+    u32 temp;
+    SysTick->LOAD = ( u32 ) nms * fac_ms; //时间加载(SysTick->LOAD为24bit)
+    SysTick->VAL = 0x00;          //清空计数器
+    SysTick->CTRL = 0x01 ;        //开始倒数
+
+    do
+    {
+        temp = SysTick->CTRL;
+    }
+    while ( temp & 0x01 && ! ( temp & ( 1 << 16 ) ) ); //等待时间到达
+
+    SysTick->CTRL = 0x00;     //关闭计数器
+    SysTick->VAL = 0X00;      //清空计数器
+}
+//延时nus
+//nus为要延时的us数.
+void delay_us ( u32 nus )
+{
+    u32 temp;
+    SysTick->LOAD = nus * fac_us; //时间加载
+    SysTick->VAL = 0x00;      //清空计数器
+    SysTick->CTRL = 0x01 ;    //开始倒数
+
+    do
+    {
+        temp = SysTick->CTRL;
+    }
+    while ( temp & 0x01 && ! ( temp & ( 1 << 16 ) ) ); //等待时间到达
+
+    SysTick->CTRL = 0x00;     //关闭计数器
+    SysTick->VAL = 0X00;      //清空计数器
+}
+#if 1
+void Delay ( vu32 nCount )
+{
+    for ( ; nCount != 0; nCount-- );
+}
+#endif
+
+#endif
+
+#define DLY_LOOP_CONST  (8500000)
+#define DLY_LOOP_CONST1 (8500)
+
+void Dly1us ( unsigned int DelayVal )
+{
+    while ( DelayVal-- )
+    {
+        for ( unsigned int i = SystemClockFreq / DLY_LOOP_CONST; i; --i );
+    }
+}
+
+
+void Dly1ms ( unsigned int DelayVal )
+{
+    while ( DelayVal-- )
+    {
+        for ( unsigned int i = SystemClockFreq / DLY_LOOP_CONST1; i; --i );
+    }
+}
+
+
+
+

+ 40 - 0
stm32 通用函数/Common/Delay1.h

@@ -0,0 +1,40 @@
+#ifndef __DELAY_H_
+#define __DELAY_H_
+
+#include <stm32f10x.h>
+
+#if 1
+void Dly1us ( unsigned int DelayVal );
+void Dly10us ( unsigned int DelayVal );
+void Dly100us ( unsigned int DelayVal );
+
+extern unsigned int SystemClockFreq;
+#define DLY_PARA_US	(SystemClockFreq*1.0/8500000)		//延时us参数
+#define DLY_CALL_US	(8)									//调用us耗时,自定义应< DLY_PARA_US(譬如8.47)
+#define delayUs(x) { 					 				\
+	unsigned int _dcnt = (DLY_PARA_US*x-DLY_CALL_US);   \
+	while(_dcnt-- > 0);		 		 	 				\
+}
+
+#define  delay_us(us)   delayUs(us)
+#define  delay_ms(ms)   delayUs(ms*1000)
+
+
+//STM32F103CBT6测试结果:
+//#define DLY_CALL_US	(8)								//调用us耗时,自定义
+//106.4ms -> 10.6ms -> 1.06ms
+//90us-114us -> 16us-19us -> 9us
+
+#else
+
+void delay_init ( u8 SYSCLK );
+void delay_ms ( u16 nms );
+void delay_us ( u32 nus );
+void Delay ( vu32 nCount );
+
+#endif
+
+
+
+#endif
+

+ 108 - 0
stm32 通用函数/Common/EE_Dataset.c

@@ -0,0 +1,108 @@
+/************************************************************************
+                 Grt-Mars, Inc.
+                 240 Sheffield Str.
+                 Mountainside, New Jersey 07092
+
+PROJECT  : ProEX
+Author   : Peter Strezev
+*******************************************************************************
+Functions to handle storage and retrieval of Irrigation programs in FLASH
+*******************************************************************************/
+#include <stdio.h>
+#include <string.h>
+#include "EE_Dataset.h"
+#include "FlashEE.h"
+
+/* --- Table of 16-bit virtual addresses (VA) for all variables. Each VA consists of 2 parts: Program Number (0..5) in the --- */
+/* --- upper byte, variable number (0..76) in the lower byte. --- */
+/*
+1k的空间,最大存储地址是512,即0、2...1024
+将地址分成8部分,则:
+0~128:  		0\2\4...\128 + 0x000		64byte
+130~258:	0\2\4...\128 + 0x100		64byte
+...
+896~1024:	0\2\4...\128 + 0x800		64byte
+*/
+const unsigned short VirtAddVarTab[NumbOfVar] =
+{
+    //52byte -> (0~51)*2
+    0, 										//Net+DNSFlag
+    2,	4, 									//ComGateway[4]		--2个地址
+    6,	8,	10,	12,	14,	16,	18,	20,	22, 24,	//SoctType+SysIP[31] 	--16个地址
+    26,	28,	30,	32,	34,	36,
+    38,	40,									//DNSPort[4]			--2个地址
+    42,										//CTime+Cir1Probe
+    44,										//Cir2Probe+Cir3Probe
+    46,										//Cir4Probe+AppAddr
+    48,										//UpDateStep+WorkMode
+    50,										//u16 GYMAX;
+	52,										//u16 QYMAX;
+	54										//u16 GLMAX;
+};
+
+
+void EEprom_Init ( void )
+{
+    FLASH_Unlock();
+    EE_Init();
+    FLASH_Lock();
+}
+
+void LoadDtuConfig ( unsigned short addr )
+{
+    char ConfigBuffer[sizeof ( SysPara )];
+    unsigned short Temp, VAddr;
+    int i;
+    tEEstat ErrCode;
+
+    FLASH_Unlock();
+
+    for ( i = 0; i <= sizeof ( SysPara ) - 2; i += 2 )
+    {
+        VAddr = addr + i;
+        ErrCode = ( tEEstat ) EE_ReadVariable ( VAddr, &Temp );
+
+        if ( ErrCode == EE_DONE )
+        {
+            ConfigBuffer[i] = ( char ) ( Temp >> 8 );
+            ConfigBuffer[i + 1] = ( char ) ( Temp & 0xFF ); 		//todo What if we overflow the buffer (if size is odd?)
+        }
+    }
+
+    FLASH_Lock();
+
+    memcpy ( &SysPara, &ConfigBuffer, sizeof ( SysPara ) );
+}
+
+void SaveDtuConfig ( unsigned short addr )
+{
+    char ConfigBuffer[ ( sizeof ( SysPara ) + 1 )];
+    unsigned short Temp, VAddr, Data;
+    int i;
+    tEEstat ErrCode;
+
+    memcpy ( &ConfigBuffer, &SysPara, sizeof ( SysPara ) );
+
+    FLASH_Unlock();
+
+    for ( i = 0; i <= sizeof ( SysPara ) - 2; i += 2 )
+    {
+        VAddr = addr + i;
+        Data = ( unsigned short ) ( ConfigBuffer[i] << 8 );
+        Data |= ( unsigned short ) ( ConfigBuffer[i + 1] );
+
+        ErrCode = ( tEEstat ) EE_ReadVariable ( VAddr, &Temp );
+
+        if ( ( ErrCode == EE_BADVAR ) || ( Data != Temp ) )	//Only save new variables or if data is different
+        {
+            __disable_interrupt();
+            ErrCode = ( tEEstat ) EE_WriteVariable ( VAddr, Data );
+            __enable_interrupt();
+        }
+    }
+
+    FLASH_Lock();
+}
+
+
+

+ 49 - 0
stm32 通用函数/Common/EE_Dataset.h

@@ -0,0 +1,49 @@
+/************************************************************************
+                 Grt-Mars, Inc.
+                 240 Sheffield Str.
+                 Mountainside, New Jersey 07092
+
+PROJECT  : ProEX
+Author   : Peter Strezev
+*******************************************************************************
+--------------------- EEPROM emulation headers  -----------------------
+EEPROM emulator is used to store the irrigation programs in FLASH. Total of 4
+programs can be stored USER-A, USER-B, USER-C and Contractor. The 3 User programs
+can be recalled at any time by placing to Program Select switch in proper position.
+The Contractor program is only accessible through "SAVE" and "RESTORE" functions
+of the "Spetial Features" user interface menu.
+*******************************************************************************/
+#ifndef	_EE_DATASET
+#define _EE_DATASET
+#include "stm32f10x_conf.h"
+#include "config.h"
+
+
+/*
+笔记:
+模拟1k,实际需要2k空间(2页交互使用)
+能保存1k/2=512byte数据(另外512byte存地址)
+注意:数据越小越好建议在1k/4=256byte内,这样才有意义
+<=256byte, 用1k
+<=512byte, 用2k
+*/
+
+/* Virtual addresses for the FLASH variables */
+#define EE_ADDR_COMGATEWAY	0
+
+#define __disable_interrupt()	__disable_irq()
+#define __enable_interrupt()	__enable_irq()
+
+typedef enum
+{
+    EE_DONE = 0,
+    EE_BADVAR = 1,
+    EE_BADPRG = 2,
+    EE_BADPAGE = 0xAB,
+} tEEstat;
+
+void EEprom_Init ( void );
+void SaveDtuConfig ( unsigned short addr );
+void LoadDtuConfig ( unsigned short addr );
+
+#endif

+ 59 - 0
stm32 通用函数/Common/FifoQueue.c

@@ -0,0 +1,59 @@
+#include "FifoQueue.h"
+
+#define FALSE	false
+#define TRUE	true
+
+struct FifoQueue Fas_Tasks;	//fas协议任务
+struct FifoQueue Pub_Tasks; //公共任务
+
+/*
+使用:
+QueueInit(&SysMsg);
+if(QueueIn(&SysMsg,i) == QueueFull) break;
+if(QueueOut(&SysMsg,&sh) == QueueEmpty) break;
+*/
+
+//Queue Init
+void QueueInit ( struct FifoQueue *Queue )
+{
+    Queue->front = Queue->rear;//初始化时队列头队列首相连
+    Queue->count = 0;   //队列计数为0
+}
+
+// Queue In
+bool QueueIn ( struct FifoQueue *Queue, ElemType sdat ) //数据进入队列
+{
+    if ( ( Queue->front == Queue->rear ) && ( Queue->count == QueueSize ) )
+    {
+        // full //判断如果队列满了
+        return FALSE;    //返回队列满的标志
+    }
+    else
+    {
+        // in
+        Queue->dat[Queue->rear] = sdat;
+        Queue->rear = ( Queue->rear + 1 ) % QueueSize;
+        Queue->count = Queue->count + 1;
+        return TRUE;
+    }
+}
+
+// Queue Out
+bool QueueOut ( struct FifoQueue *Queue, ElemType *sdat )
+{
+    if ( ( Queue->front == Queue->rear ) && ( Queue->count == 0 ) )
+    {
+        // empty
+        return FALSE;
+    }
+    else
+    {
+        // out
+        *sdat = Queue->dat[Queue->front];
+        Queue->front = ( Queue->front + 1 ) % QueueSize;
+        Queue->count = Queue->count - 1;
+        return TRUE;
+    }
+}
+
+

+ 30 - 0
stm32 通用函数/Common/FifoQueue.h

@@ -0,0 +1,30 @@
+#ifndef _FIFOQUEUE_H
+#define _FIFOQUEUE_H
+/* 包含的头文件-----------------------------------------------------------*/
+#include "stm32f10x.h"
+#include <stdbool.h>
+/* 宏定义 --------------------------------------------------------------------*/
+#define uint8 u8
+#define uint16 u16
+#define ElemType       uint8
+#define QueueSize      20 //fifo队列的大小
+/* 类型定义 -----------------------------------------------------------------*/
+struct FifoQueue
+{
+    uint16 front;     	//队列头
+    uint16 rear;        //队列尾
+    uint16 count;       //队列计数
+    ElemType dat[QueueSize];
+};
+/* 常量定义 ----------------------------------------------------------------*/
+extern struct FifoQueue Fas_Tasks;
+extern struct FifoQueue Pub_Tasks;
+
+/* 函数定义---------------------------------------------------------------- */
+extern void QueueInit ( struct FifoQueue *Queue );
+extern bool QueueIn ( struct FifoQueue *Queue, ElemType sdat );
+extern bool QueueOut ( struct FifoQueue *Queue, ElemType *sdat );
+
+#endif
+
+

+ 667 - 0
stm32 通用函数/Common/FlashEE.c

@@ -0,0 +1,667 @@
+/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
+* File Name          : eeprom.c
+* Author             : MCD Application Team
+* Version            : V2.0.0
+* Date               : 06/16/2008
+* Description        : This file provides all the EEPROM emulation firmware functions.
+********************************************************************************
+* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
+* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
+* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
+* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
+* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
+* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
+*******************************************************************************/
+
+/* Includes ------------------------------------------------------------------*/
+#include "FlashEE.h"
+
+/* Private typedef -----------------------------------------------------------*/
+/* Private define ------------------------------------------------------------*/
+/* Private macro -------------------------------------------------------------*/
+/* Private variables ---------------------------------------------------------*/
+#define PAGE_SIZE  (unsigned short)(0x400)  /* Page size for eeprom emulator = 1KByte */
+
+/* Global variable used to store variable value in read sequence */
+unsigned short DataVar = 0;
+
+/* Virtual address defined by the user: 0xFFFF value is prohibited */
+extern unsigned short VirtAddVarTab[NumbOfVar];
+
+/* Private function prototypes -----------------------------------------------*/
+/* Private functions ---------------------------------------------------------*/
+FLASH_Status FlashStatus;
+static FLASH_Status EE_Format ( void );
+static unsigned short EE_VerifyPageFullWriteVariable ( unsigned short VirtAddress, unsigned short Data );
+static unsigned short EE_FindValidPage ( char Operation );
+static unsigned short EE_PageTransfer ( unsigned short VirtAddress, unsigned short Data );
+
+/*******************************************************************************
+* Function Name  : EE_Init
+* Description    : Restore the pages to a known good state in case of pages'
+*                  status corruption after a power loss.
+* Input          : None.
+* Output         : None.
+* Return         : - Flash error code: on write Flash error
+*                  - FLASH_COMPLETE: on success
+*******************************************************************************/
+unsigned short EE_Init ( void )
+{
+    unsigned short PageStatus0 = 6, PageStatus1 = 6;
+    unsigned short VarIdx = 0;
+    unsigned short EepromStatus = 0, ReadStatus = 0;
+    short x = -1;
+    unsigned short  FlashStatus;
+
+    /* Get Page0 status */
+    PageStatus0 = ( * ( vu16* ) PAGE0_BASE_ADDRESS );
+    /* Get Page1 status */
+    PageStatus1 = ( * ( vu16* ) PAGE1_BASE_ADDRESS );
+
+    /* Check for invalid header states and repair if necessary */
+    switch ( PageStatus0 )
+    {
+    case ERASED:
+        if ( PageStatus1 == VALID_PAGE ) /* Page0 erased, Page1 valid */
+        {
+            /* Erase Page0 */
+            FlashStatus = FLASH_ErasePage ( PAGE0_BASE_ADDRESS );
+
+            /* If erase operation was failed, a Flash error code is returned */
+            if ( FlashStatus != FLASH_COMPLETE )
+            {
+                return FlashStatus;
+            }
+        }
+        else if ( PageStatus1 == RECEIVE_DATA ) /* Page0 erased, Page1 receive */
+        {
+            /* Erase Page0 */
+            FlashStatus = FLASH_ErasePage ( PAGE0_BASE_ADDRESS );
+
+            /* If erase operation was failed, a Flash error code is returned */
+            if ( FlashStatus != FLASH_COMPLETE )
+            {
+                return FlashStatus;
+            }
+
+            /* Mark Page1 as valid */
+            FlashStatus = FLASH_ProgramHalfWord ( PAGE1_BASE_ADDRESS, VALID_PAGE );
+
+            /* If program operation was failed, a Flash error code is returned */
+            if ( FlashStatus != FLASH_COMPLETE )
+            {
+                return FlashStatus;
+            }
+        }
+        else /* First EEPROM access (Page0&1 are erased) or invalid state -> format EEPROM */
+        {
+            /* Erase both Page0 and Page1 and set Page0 as valid page */
+            FlashStatus = EE_Format();
+
+            /* If erase/program operation was failed, a Flash error code is returned */
+            if ( FlashStatus != FLASH_COMPLETE )
+            {
+                return FlashStatus;
+            }
+        }
+
+        break;
+
+    case RECEIVE_DATA:
+        if ( PageStatus1 == VALID_PAGE ) /* Page0 receive, Page1 valid */
+        {
+            /* Transfer data from Page1 to Page0 */
+            for ( VarIdx = 0; VarIdx < NumbOfVar; VarIdx++ )
+            {
+                if ( ( * ( vu16* ) ( PAGE0_BASE_ADDRESS + 6 ) ) == VirtAddVarTab[VarIdx] )
+                {
+                    x = VarIdx;
+                }
+
+                if ( VarIdx != x )
+                {
+                    /* Read the last variables' updates */
+                    ReadStatus = EE_ReadVariable ( VirtAddVarTab[VarIdx], &DataVar );
+
+                    /* In case variable corresponding to the virtual address was found */
+                    if ( ReadStatus != 0x1 )
+                    {
+                        /* Transfer the variable to the Page0 */
+                        EepromStatus = EE_VerifyPageFullWriteVariable ( VirtAddVarTab[VarIdx], DataVar );
+
+                        /* If program operation was failed, a Flash error code is returned */
+                        if ( EepromStatus != FLASH_COMPLETE )
+                        {
+                            return EepromStatus;
+                        }
+                    }
+                }
+            }
+
+            /* Mark Page0 as valid */
+            FlashStatus = FLASH_ProgramHalfWord ( PAGE0_BASE_ADDRESS, VALID_PAGE );
+
+            /* If program operation was failed, a Flash error code is returned */
+            if ( FlashStatus != FLASH_COMPLETE )
+            {
+                return FlashStatus;
+            }
+
+            /* Erase Page1 */
+            FlashStatus = FLASH_ErasePage ( PAGE1_BASE_ADDRESS );
+
+            /* If erase operation was failed, a Flash error code is returned */
+            if ( FlashStatus != FLASH_COMPLETE )
+            {
+                return FlashStatus;
+            }
+        }
+        else if ( PageStatus1 == ERASED ) /* Page0 receive, Page1 erased */
+        {
+            /* Erase Page1 */
+            FlashStatus = FLASH_ErasePage ( PAGE1_BASE_ADDRESS );
+
+            /* If erase operation was failed, a Flash error code is returned */
+            if ( FlashStatus != FLASH_COMPLETE )
+            {
+                return FlashStatus;
+            }
+
+            /* Mark Page0 as valid */
+            FlashStatus = FLASH_ProgramHalfWord ( PAGE0_BASE_ADDRESS, VALID_PAGE );
+
+            /* If program operation was failed, a Flash error code is returned */
+            if ( FlashStatus != FLASH_COMPLETE )
+            {
+                return FlashStatus;
+            }
+        }
+        else /* Invalid state -> format eeprom */
+        {
+            /* Erase both Page0 and Page1 and set Page0 as valid page */
+            FlashStatus = EE_Format();
+
+            /* If erase/program operation was failed, a Flash error code is returned */
+            if ( FlashStatus != FLASH_COMPLETE )
+            {
+                return FlashStatus;
+            }
+        }
+
+        break;
+
+    case VALID_PAGE:
+        if ( PageStatus1 == VALID_PAGE ) /* Invalid state -> format eeprom */
+        {
+            /* Erase both Page0 and Page1 and set Page0 as valid page */
+            FlashStatus = EE_Format();
+
+            /* If erase/program operation was failed, a Flash error code is returned */
+            if ( FlashStatus != FLASH_COMPLETE )
+            {
+                return FlashStatus;
+            }
+        }
+        else if ( PageStatus1 == ERASED ) /* Page0 valid, Page1 erased */
+        {
+            /* Erase Page1 */
+            FlashStatus = FLASH_ErasePage ( PAGE1_BASE_ADDRESS );
+
+            /* If erase operation was failed, a Flash error code is returned */
+            if ( FlashStatus != FLASH_COMPLETE )
+            {
+                return FlashStatus;
+            }
+        }
+        else /* Page0 valid, Page1 receive */
+        {
+            /* Transfer data from Page0 to Page1 */
+            for ( VarIdx = 0; VarIdx < NumbOfVar; VarIdx++ )
+            {
+                if ( ( * ( vu16* ) ( PAGE1_BASE_ADDRESS + 6 ) ) == VirtAddVarTab[VarIdx] )
+                {
+                    x = VarIdx;
+                }
+
+                if ( VarIdx != x )
+                {
+                    /* Read the last variables' updates */
+                    ReadStatus = EE_ReadVariable ( VirtAddVarTab[VarIdx], &DataVar );
+
+                    /* In case variable corresponding to the virtual address was found */
+                    if ( ReadStatus != 0x1 )
+                    {
+                        /* Transfer the variable to the Page1 */
+                        EepromStatus = EE_VerifyPageFullWriteVariable ( VirtAddVarTab[VarIdx], DataVar );
+
+                        /* If program operation was failed, a Flash error code is returned */
+                        if ( EepromStatus != FLASH_COMPLETE )
+                        {
+                            return EepromStatus;
+                        }
+                    }
+                }
+            }
+
+            /* Mark Page1 as valid */
+            FlashStatus = FLASH_ProgramHalfWord ( PAGE1_BASE_ADDRESS, VALID_PAGE );
+
+            /* If program operation was failed, a Flash error code is returned */
+            if ( FlashStatus != FLASH_COMPLETE )
+            {
+                return FlashStatus;
+            }
+
+            /* Erase Page0 */
+            FlashStatus = FLASH_ErasePage ( PAGE0_BASE_ADDRESS );
+
+            /* If erase operation was failed, a Flash error code is returned */
+            if ( FlashStatus != FLASH_COMPLETE )
+            {
+                return FlashStatus;
+            }
+        }
+
+        break;
+
+    default:  /* Any other state -> format eeprom */
+        /* Erase both Page0 and Page1 and set Page0 as valid page */
+        FlashStatus = EE_Format();
+
+        /* If erase/program operation was failed, a Flash error code is returned */
+        if ( FlashStatus != FLASH_COMPLETE )
+        {
+            return FlashStatus;
+        }
+
+        break;
+    }
+
+    return FLASH_COMPLETE;
+}
+
+/*******************************************************************************
+* Function Name  : EE_ReadVariable
+* Description    : Returns the last stored variable data, if found, which
+*                  correspond to the passed virtual address
+* Input          : - VirtAddress: Variable virtual address
+*                  - Data: Global variable contains the read variable value
+* Output         : None
+* Return         : - Success or error status:
+*                      - 0: if variable was found
+*                      - 1: if the variable was not found
+*                      - NO_VALID_PAGE: if no valid page was found.
+*******************************************************************************/
+unsigned short EE_ReadVariable ( unsigned short VirtAddress, unsigned short* Data )
+{
+    unsigned short ValidPage = PAGE0;
+    unsigned short AddressValue = 0x5555, ReadStatus = 1;
+    unsigned int Address, PageStartAddress;
+
+    /* Get active Page for read operation */
+    ValidPage = EE_FindValidPage ( READ_FROM_VALID_PAGE );
+
+    /* Check if there is no valid page */
+    if ( ValidPage == NO_VALID_PAGE )
+    {
+        return  NO_VALID_PAGE;
+    }
+
+    /* Get the valid Page start Address */
+    PageStartAddress = ( unsigned int ) ( EEPROM_START_ADDRESS + ( unsigned int ) ( ValidPage * PAGE_SIZE ) );
+
+    /* Get the valid Page end Address */
+    Address = ( unsigned int ) ( ( EEPROM_START_ADDRESS - 2 ) + ( unsigned int ) ( ( 1 + ValidPage ) * PAGE_SIZE ) );
+
+    /* Check each active page address starting from end */
+    while ( Address > ( PageStartAddress + 2 ) )
+    {
+        /* Get the current location content to be compared with virtual address */
+        AddressValue = ( * ( vu16* ) Address );
+
+        /* Compare the read address with the virtual address */
+        if ( AddressValue == VirtAddress )
+        {
+            /* Get content of Address-2 which is variable value */
+            *Data = ( * ( vu16* ) ( Address - 2 ) );
+
+            /* In case variable value is read, reset ReadStatus flag */
+            ReadStatus = 0;
+
+            break;
+        }
+        else
+        {
+            /* Next address location */
+            Address = Address - 4;
+        }
+    }
+
+    /* Return ReadStatus value: (0: variable exist, 1: variable doesn't exist) */
+    return ReadStatus;
+}
+
+/*******************************************************************************
+* Function Name  : EE_WriteVariable
+* Description    : Writes/upadtes variable data in EEPROM.
+* Input          : - VirtAddress: Variable virtual address
+*                  - Data: 16 bit data to be written
+* Output         : None
+* Return         : - Success or error status:
+*                      - FLASH_COMPLETE: on success,
+*                      - PAGE_FULL: if valid page is full
+*                      - NO_VALID_PAGE: if no valid page was found
+*                      - Flash error code: on write Flash error
+*******************************************************************************/
+unsigned short EE_WriteVariable ( unsigned short VirtAddress, unsigned short Data )
+{
+    unsigned short Status = 0;
+
+    /* Write the variable virtual address and value in the EEPROM */
+    Status = EE_VerifyPageFullWriteVariable ( VirtAddress, Data );
+
+    /* In case the EEPROM active page is full */
+    if ( Status == PAGE_FULL )
+    {
+        /* Perform Page transfer */
+        Status = EE_PageTransfer ( VirtAddress, Data );
+    }
+
+    /* Return last operation status */
+    return Status;
+}
+
+/*******************************************************************************
+* Function Name  : EE_Format
+* Description    : Erases PAGE0 and PAGE1 and writes VALID_PAGE header to PAGE0
+* Input          : None
+* Output         : None
+* Return         : Status of the last operation (Flash write or erase) done during
+*                  EEPROM formating
+*******************************************************************************/
+static FLASH_Status EE_Format ( void )
+{
+    FLASH_Status FlashStatus = FLASH_COMPLETE;
+
+    /* Erase Page0 */
+    FlashStatus = FLASH_ErasePage ( PAGE0_BASE_ADDRESS );
+
+    /* If erase operation was failed, a Flash error code is returned */
+    if ( FlashStatus != FLASH_COMPLETE )
+    {
+        return FlashStatus;
+    }
+
+    /* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */
+    FlashStatus = FLASH_ProgramHalfWord ( PAGE0_BASE_ADDRESS, VALID_PAGE );
+
+    /* If program operation was failed, a Flash error code is returned */
+    if ( FlashStatus != FLASH_COMPLETE )
+    {
+        return FlashStatus;
+    }
+
+    /* Erase Page1 */
+    FlashStatus = FLASH_ErasePage ( PAGE1_BASE_ADDRESS );
+
+    /* Return Page1 erase operation status */
+    return FlashStatus;
+}
+
+/*******************************************************************************
+* Function Name  : EE_FindValidPage
+* Description    : Find valid Page for write or read operation
+* Input          : - Operation: operation to achieve on the valid page:
+*                      - READ_FROM_VALID_PAGE: read operation from valid page
+*                      - WRITE_IN_VALID_PAGE: write operation from valid page
+* Output         : None
+* Return         : Valid page number (PAGE0 or PAGE1) or NO_VALID_PAGE in case
+*                  of no valid page was found
+*******************************************************************************/
+static unsigned short EE_FindValidPage ( char Operation )
+{
+    unsigned short PageStatus0 = 6, PageStatus1 = 6;
+
+    /* Get Page0 actual status */
+    PageStatus0 = ( * ( vu16* ) PAGE0_BASE_ADDRESS );
+
+    /* Get Page1 actual status */
+    PageStatus1 = ( * ( vu16* ) PAGE1_BASE_ADDRESS );
+
+    /* Write or read operation */
+    switch ( Operation )
+    {
+    case WRITE_IN_VALID_PAGE:   /* ---- Write operation ---- */
+        if ( PageStatus1 == VALID_PAGE )
+        {
+            /* Page0 receiving data */
+            if ( PageStatus0 == RECEIVE_DATA )
+            {
+                return PAGE0;         /* Page0 valid */
+            }
+            else
+            {
+                return PAGE1;         /* Page1 valid */
+            }
+        }
+        else if ( PageStatus0 == VALID_PAGE )
+        {
+            /* Page1 receiving data */
+            if ( PageStatus1 == RECEIVE_DATA )
+            {
+                return PAGE1;         /* Page1 valid */
+            }
+            else
+            {
+                return PAGE0;         /* Page0 valid */
+            }
+        }
+        else
+        {
+            return NO_VALID_PAGE;   /* No valid Page */
+        }
+
+    case READ_FROM_VALID_PAGE:  /* ---- Read operation ---- */
+        if ( PageStatus0 == VALID_PAGE )
+        {
+            return PAGE0;           /* Page0 valid */
+        }
+        else if ( PageStatus1 == VALID_PAGE )
+        {
+            return PAGE1;           /* Page1 valid */
+        }
+        else
+        {
+            return NO_VALID_PAGE ;  /* No valid Page */
+        }
+
+    default:
+        return PAGE0;             /* Page0 valid */
+    }
+}
+
+/*******************************************************************************
+* Function Name  : EE_VerifyPageFullWriteVariable
+* Description    : Verify if active page is full and Writes variable in EEPROM.
+* Input          : - VirtAddress: 16 bit virtual address of the variable
+*                  - Data: 16 bit data to be written as variable value
+* Output         : None
+* Return         : - Success or error status:
+*                      - FLASH_COMPLETE: on success
+*                      - PAGE_FULL: if valid page is full
+*                      - NO_VALID_PAGE: if no valid page was found
+*                      - Flash error code: on write Flash error
+*******************************************************************************/
+static unsigned short EE_VerifyPageFullWriteVariable ( unsigned short VirtAddress, unsigned short Data )
+{
+    FLASH_Status FlashStatus = FLASH_COMPLETE;
+    unsigned short ValidPage = PAGE0;
+    unsigned int Address, PageEndAddress;
+
+    /* Get valid Page for write operation */
+    ValidPage = EE_FindValidPage ( WRITE_IN_VALID_PAGE );
+
+    /* Check if there is no valid page */
+    if ( ValidPage == NO_VALID_PAGE )
+    {
+        return  NO_VALID_PAGE;
+    }
+
+    /* Get the valid Page start Address */
+    Address = ( unsigned int ) ( EEPROM_START_ADDRESS + ( unsigned int ) ( ValidPage * PAGE_SIZE ) );
+
+    /* Get the valid Page end Address */
+    PageEndAddress = ( unsigned int ) ( ( EEPROM_START_ADDRESS - 2 ) + ( unsigned int ) ( ( 1 + ValidPage ) * PAGE_SIZE ) );
+
+    /* Check each active page address starting from begining */
+    while ( Address < PageEndAddress )
+    {
+        /* Verify if Address and Address+2 contents are 0xFFFFFFFF */
+        if ( ( * ( vu32* ) Address ) == 0xFFFFFFFF )
+        {
+            /* Set variable data */
+            FlashStatus = FLASH_ProgramHalfWord ( Address, Data );
+
+            /* If program operation was failed, a Flash error code is returned */
+            if ( FlashStatus != FLASH_COMPLETE )
+            {
+                return FlashStatus;
+            }
+
+            /* Set variable virtual address */
+            FlashStatus = FLASH_ProgramHalfWord ( Address + 2, VirtAddress );
+            /* Return program operation status */
+            return FlashStatus;
+        }
+        else
+        {
+            /* Next address location */
+            Address = Address + 4;
+        }
+    }
+
+    /* Return PAGE_FULL in case the valid page is full */
+    return PAGE_FULL;
+}
+
+/*******************************************************************************
+* Function Name  : EE_PageTransfer
+* Description    : Transfers last updated variables data from the full Page to
+*                  an empty one.
+* Input          : - VirtAddress: 16 bit virtual address of the variable
+*                  - Data: 16 bit data to be written as variable value
+* Output         : None
+* Return         : - Success or error status:
+*                      - FLASH_COMPLETE: on success,
+*                      - PAGE_FULL: if valid page is full
+*                      - NO_VALID_PAGE: if no valid page was found
+*                      - Flash error code: on write Flash error
+*******************************************************************************/
+static unsigned short EE_PageTransfer ( unsigned short VirtAddress, unsigned short Data )
+{
+    FLASH_Status FlashStatus = FLASH_COMPLETE;
+    unsigned int NewPageAddress, OldPageAddress;
+    unsigned short ValidPage = PAGE0, VarIdx = 0;
+    unsigned short EepromStatus = 0, ReadStatus = 0;
+
+    /* Get active Page for read operation */
+    ValidPage = EE_FindValidPage ( READ_FROM_VALID_PAGE );
+
+    if ( ValidPage == PAGE1 )     /* Page1 valid */
+    {
+        /* New page address where variable will be moved to */
+        NewPageAddress = PAGE0_BASE_ADDRESS;
+
+        /* Old page address where variable will be taken from */
+        OldPageAddress = PAGE1_BASE_ADDRESS;
+    }
+    else if ( ValidPage == PAGE0 ) /* Page0 valid */
+    {
+        /* New page address where variable will be moved to */
+        NewPageAddress = PAGE1_BASE_ADDRESS;
+
+        /* Old page address where variable will be taken from */
+        OldPageAddress = PAGE0_BASE_ADDRESS;
+    }
+    else
+    {
+        return NO_VALID_PAGE;       /* No valid Page */
+    }
+
+    /* Set the new Page status to RECEIVE_DATA status */
+    FlashStatus = FLASH_ProgramHalfWord ( NewPageAddress, RECEIVE_DATA );
+
+    /* If program operation was failed, a Flash error code is returned */
+    if ( FlashStatus != FLASH_COMPLETE )
+    {
+        return FlashStatus;
+    }
+
+    /* Write the variable passed as parameter in the new active page */
+    EepromStatus = EE_VerifyPageFullWriteVariable ( VirtAddress, Data );
+
+    /* If program operation was failed, a Flash error code is returned */
+    if ( EepromStatus != FLASH_COMPLETE )
+    {
+        return EepromStatus;
+    }
+
+    /* Transfer process: transfer variables from old to the new active page */
+    for ( VarIdx = 0; VarIdx < NumbOfVar; VarIdx++ )
+    {
+        if ( VirtAddVarTab[VarIdx] != VirtAddress ) /* Check each variable except the one passed as parameter */
+        {
+            /* Read the other last variable updates */
+            ReadStatus = EE_ReadVariable ( VirtAddVarTab[VarIdx], &DataVar );
+
+            /* In case variable corresponding to the virtual address was found */
+            if ( ReadStatus != 0x1 )
+            {
+                /* Transfer the variable to the new active page */
+                EepromStatus = EE_VerifyPageFullWriteVariable ( VirtAddVarTab[VarIdx], DataVar );
+
+                /* If program operation was failed, a Flash error code is returned */
+                if ( EepromStatus != FLASH_COMPLETE )
+                {
+                    return EepromStatus;
+                }
+            }
+        }
+    }
+
+    /* Erase the old Page: Set old Page status to ERASED status */
+    FlashStatus = FLASH_ErasePage ( OldPageAddress );
+
+    /* If erase operation was failed, a Flash error code is returned */
+    if ( FlashStatus != FLASH_COMPLETE )
+    {
+        return FlashStatus;
+    }
+
+    /* Set new Page status to VALID_PAGE status */
+    FlashStatus = FLASH_ProgramHalfWord ( NewPageAddress, VALID_PAGE );
+
+    /* If program operation was failed, a Flash error code is returned */
+    if ( FlashStatus != FLASH_COMPLETE )
+    {
+        return FlashStatus;
+    }
+
+    /* Return last operation flash status */
+    return FlashStatus;
+}
+
+/*******************************************************************************
+* Function Name  : EE_XX
+* Description    : Force the complete reset of the entire space of eeprom
+*                  saves.
+* Input          :none
+** Output        : None
+* Return         : None
+*******************************************************************************/
+void EE_XX ( void )
+{
+    __disable_interrupt();
+    FlashStatus = EE_Format();
+    __enable_interrupt();
+}

+ 59 - 0
stm32 通用函数/Common/FlashEE.h

@@ -0,0 +1,59 @@
+/*******************************************************************************
+* File Name          : FLASH_EE.h
+* Author             : MCD Application Team
+* Description        : This file contains all the functions prototypes for the
+*                      EEPROM emulation firmware library.
+*******************************************************************************/
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef _FLASHEE_H
+#define _FLASHEE_H
+
+/* Includes ------------------------------------------------------------------*/
+#include "stm32f10x_conf.h"
+#include "EE_Dataset.h"
+
+/* Exported constants --------------------------------------------------------*/
+/* EEPROM start address in Flash */
+//128k的最后2k,stm32F103CB 有128k,(共128页,每页1k)
+#define EEPROM_START_ADDRESS    ((unsigned int)(0x08000000+(128-2)*1024)) /* EEPROM emulation start address:*/
+
+/* Pages 0 and 1 base and end addresses */
+#define PAGE0_BASE_ADDRESS      ((unsigned int)(EEPROM_START_ADDRESS + 0x000))
+#define PAGE0_END_ADDRESS       ((unsigned int)(EEPROM_START_ADDRESS + (PAGE_SIZE - 1)))
+
+#define PAGE1_BASE_ADDRESS      ((unsigned int)(EEPROM_START_ADDRESS + PAGE_SIZE))
+#define PAGE1_END_ADDRESS       ((unsigned int)(EEPROM_START_ADDRESS + (2 * PAGE_SIZE - 1)))
+
+/* Used Flash pages for EEPROM emulation */
+#define PAGE0                   ((unsigned short)0x0000)
+#define PAGE1                   ((unsigned short)0x0001)
+
+/* No valid page define */
+#define NO_VALID_PAGE           ((unsigned short)0x00AB)
+
+/* Page status definitions */
+#define ERASED                  ((unsigned short)0xFFFF)     /* PAGE is empty */
+#define RECEIVE_DATA            ((unsigned short)0xEEEE)     /* PAGE is marked to receive data */
+#define VALID_PAGE              ((unsigned short)0x0000)     /* PAGE containing valid data */
+
+/* Valid pages in read and write defines */
+#define READ_FROM_VALID_PAGE    ((char)0x00)
+#define WRITE_IN_VALID_PAGE     ((char)0x01)
+
+/* Page full define */
+#define PAGE_FULL               ((char)0x80)
+
+/* Variables' number */
+//#define NumbOfVar               (char)(sizeof(tPrg) * 6)  /* DNJ - Since we have 6 programs to store, this variable is no more char ! */
+#define NumbOfVar               (unsigned short)(sizeof(SysPara)/2)
+
+/* Exported types ------------------------------------------------------------*/
+/* Exported macro ------------------------------------------------------------*/
+/* Exported functions ------------------------------------------------------- */
+unsigned short EE_Init ( void );
+unsigned short EE_ReadVariable ( unsigned short, unsigned short* );
+unsigned short EE_WriteVariable ( unsigned short, unsigned short );
+void EE_XX ( void );
+
+#endif

+ 76 - 0
stm32 通用函数/Common/Menu.c

@@ -0,0 +1,76 @@
+#include "Menu.h"
+
+#define uchar 	u8
+#define code	const
+
+typedef struct
+{
+     uchar	KeyStateIndex ;				//当前状态索引号
+     uchar	KeyDnState ;				//按下 “向下” 键时转向的状态索引号
+     uchar	KeyUpState ;				//按下 “向上” 键时转向的状态索引号
+     uchar	KeyCrState ;				//按下 “回车” 键时转向的状态索引号
+     uchar	KeyBackState ;				//按下 “退回” 键时转向的状态索引号
+     void	( *CurrentOperate ) ( ) ;		//当前状态应该执行的功能操作
+} KbdTabStruct ;
+
+#define SIZE_OF_KEYBD_MENU 55   	//菜单总长度
+KbdTabStruct code KeyTab[SIZE_OF_KEYBD_MENU] =
+{
+    /*
+     {0 ,0 ,0 ,1 ,0 , 		( *MainJob1) } ,
+     {1 ,7 ,2 ,8 ,0 , 		( *Dsp Point) } ,		//第一层
+     {2 ,1 ,3 ,8 ,0 ,		( *DspCurve) ) ,		//第一层
+     {3 ,2 ,4 ,36 ,0 , 	( *Dsp Kout) } ,   //第一层
+     {4 ,3 ,5 ,50 ,0 , 	( *DisCloseDown) } ,	//第一层
+
+     {5 ,4 ,6 ,8 ,0 , 		( *ModifyPoint) } ,		//第一层
+     {6 ,5 ,7 ,52 ,0 , 	( *SetCloseDown) } ,	//第一层
+     {7 ,6 ,1 ,0 ,0 , 		( *Cancel) } ,			//第一层
+    //	....
+    //	....
+     {52 ,53 ,53 ,0 ,1 , 	( *OkSetCloseDown1) } ,
+     {53 ,52 ,52 ,0 ,1 , 	( *OkSetCloseDown2) } ,
+     {54 ,0 ,0 ,0 ,0 , 	( *Disable) } ,
+    */
+};
+
+
+uchar KeyFuncIndex = 0;		//当前画面索引号
+/*
+1.由当前画面的[按键],找到下个画面
+2.在下个画面,执行对应程序
+*/
+void GetKeylnput ( void )
+{
+    void *KeyFuncPtr = NULL;
+
+    switch ( status & 0xf0 )
+    {
+    case 0xe0 :				//回车键,找出新的菜单状态编号
+        KeyFuncIndex = KeyTab[ KeyFuncIndex ].KeyCrState ;
+        break ;
+
+    case 0xb0 :				//向下键,找出新的菜单状态编号
+        KeyFuncIndex = KeyTab[ KeyFuncIndex ].KeyDn2State ;
+        break ;
+
+    case 0xd0 :				//向上键,找出新的菜单状态编号
+        KeyFuncIndex = KeyTab[ KeyFuncIndex ].KeyUp2State ;
+        break ;
+
+    case 0x70 :				//回退键,找出新的菜单状态编号
+        KeyFuncIndex = KeyTab[ KeyFuncIndex ].KeyBack2State ;
+        break ;
+
+    case 0 ;
+        return;
+          		//错误的处理
+        break ;
+    }
+
+    KeyFuncPtr = KeyTab[ KeyFuncIndex ].CurrentOperate ;
+    ( *KeyFuncPtr ) () ;		//执行当前按键的操作
+}
+
+
+

+ 12 - 0
stm32 通用函数/Common/Menu.h

@@ -0,0 +1,12 @@
+#ifndef __MENU_H
+#define __MENU_H
+/* 包含的头文件---------------------------------------------------------------*/
+#include "stm32f10x.h"
+/* 类型定义 ------------------------------------------------------------------*/
+/* 常量定义 ------------------------------------------------------------------*/
+/* 宏定义 --------------------------------------------------------------------*/
+/* 函数定义------------------------------------------------------------------ */
+
+
+#endif
+

+ 70 - 0
stm32 通用函数/Common/MyMach.h

@@ -0,0 +1,70 @@
+#ifndef __MY_MACH_H
+#define __MY_MACH_H
+/* 包含的头文件---------------------------------------------------------------*/
+#include <stdio.h>
+/* 类型定义 ------------------------------------------------------------------*/
+/* 常量定义 ------------------------------------------------------------------*/
+enum
+{
+    A_SENDING,			//发送中
+    A_WAITTING,			//等待回复
+    A_RE_SENDING,		//重发
+    A_RES_OK,			//结果:成功
+    A_RES_ERROR,		//结果:一次错误
+    A_RES_OUTTIME,		//结果:超时
+    A_RES_ALLSTEP_OK,	//结果:所有步骤都成功
+    A_RES_DOMSG_OK,	//获取DO信息OK
+    A_MAX
+};
+
+/* 宏定义 --------------------------------------------------------------------*/
+#define max(a,b) ( ((a)>(b)) ? (a):(b) )
+#define min(a,b) ( ((a)>(b)) ? (b):(a) )
+
+#define ARRAY_SIZE( ARRAY ) (sizeof (ARRAY) / sizeof (ARRAY[0]))
+
+#define GetBit(dat,bit)     (((dat)&(1<<(bit)))?1:0)
+#define SetBit(dat,bit,val) ((val)?((dat)|=(1<<(bit))):((dat)&=(~(1<<(bit)))))
+#define setBitMask(dat, bitMask, setval) ((dat)=(((dat)&(~(bitMask)))|((setval)&(bitMask))))
+
+#define Put16Byte(Low8Byte,Hig8Byte) (((Low8Byte&(0xFF*1UL)))|(((Hig8Byte&(0xFF*1UL)))<<8))
+#define GetHig8Byte(dat) (((dat*1UL)>>8)&0xFF)
+#define GetLow8Byte(dat) (((dat*1UL)>>0)&0xFF)
+
+typedef unsigned int uint_32 ;
+typedef unsigned short uint_16 ;
+//16位
+#define BSWAP_16(x) \
+    (uint_16)((((uint_16)(x) & 0x00ff) << 8) | \
+              (((uint_16)(x) & 0xff00) >> 8) \
+             )
+
+//32位
+#define BSWAP_32(x) \
+    (uint_32)((((uint_32)(x) & 0xff000000) >> 24) | \
+              (((uint_32)(x) & 0x00ff0000) >> 8) | \
+              (((uint_32)(x) & 0x0000ff00) << 8) | \
+              (((uint_32)(x) & 0x000000ff) << 24) \
+             )
+
+#define BinToHex(n) \
+( \
+((n>>21)&0x80)| \
+((n>>18)&0x40)| \
+((n>>15)&0x20)| \
+((n>>12)&0x10)| \
+((n>>9)&0x08)| \
+((n>>6)&0x04)| \
+((n>>3)&0x02)| \
+((n)&0x01) \
+)
+#define Bin(n) BinToHex(0x##n##l)
+
+#define _STR(s)      #s
+#define STR(s)       _STR(s)           // 转换宏 
+#define _CONS(a,b)   int(a##e##b)
+#define CONS(a,b)    _CONS(a,b)        // 转换宏 
+
+/* 函数定义------------------------------------------------------------------ */
+#endif
+

+ 225 - 0
stm32 通用函数/Common/ProgramRun.c

@@ -0,0 +1,225 @@
+#include "ProgramRun.h"
+#include "FifoQueue.h"
+#include "Config.h"			//常用功能
+#include "Bat.h"			//电池
+#include "Beep.h"			//蜂鸣器
+#include "Keys.h"			//按键
+#include "3G_A8500.h"		//3G
+#include "Uart.h"			//串口
+#include "Fas_Base.h"		//fas处理
+#include "ParaConfig.h"		//falsh配置
+#include "Fas_Test485.h" 		//测试485
+#include "Fas_Test232.h" 		//测试232
+#include "Mypara.h"
+#include "Exit.h"				//外部中断
+#include "Led.h"				//led
+#include "Fas_Base.h"
+#include "Fas_YDH30P_Alone.h"
+#include "Fas_ZZSA_dianhutanceqi.h"
+#include "Fas_CJ_xianliuqi.h"
+#include "Fas_YDH10P.h"
+
+tPubAppEvents PubAppEvent;
+tFasAppStates FasAppState;
+tFasAppEvents FasAppEvent;
+
+/*	此函数仅用于超时计时
+	计时速度必须大于T10MS_EV_Handler, 不然T10MS_EV_Handler会重复执行
+*/
+void T10MS_Count_Handler ( void ) //10ms
+{
+    //3g计时
+    a8500_delay_cnt += 10;
+
+    if ( a8500_delay_cnt >= 60000 )  	//0~60000
+    {
+        a8500_delay_cnt = 0;
+    }
+
+    Fas_T10MS_Even();
+}
+
+/*
+按测试按键切换正常模式和测试模式
+测试模式:
+	1.发送485,原数据返回
+	2.发送232,生产配置IP 端口等
+正常模式:
+	1.发送485,雅达-独立式
+	2.发送232,原数据返回
+*/
+void T10MS_EV_Handler ( void )
+{
+    A8500_Handler();					//3g模块,不要太快,最好大于1ms。模块初始化
+
+    switch ( SysRunType )				//切换协议
+    {
+	    case SysRun_Norm:
+	        Fas_Handler();
+	        break;
+
+	    case SysRun_Test:                   //按测试按键切换正常模式和测试模式
+	        Fas_Handler();					//测试:MCU -> 雅达参数
+	        ProduSet_Handler();				//测试:工具->MCU 参数
+	        Test232_Handler();
+	        Test485_Handler();
+	        break;
+    }
+}
+
+void T100MS_EV_Handler ( void )
+{
+    Hard_FeedDog();					//硬件看门狗
+    LedEven();						//LED处理
+}
+
+
+void T1S_EV_Handler ( void )
+{
+    DC_BAT_Handler();				//adc任务,电量采集
+
+    if ( SysRunType == SysRun_Test )
+    {
+        Gprs_Msg_3s_Even();			//测试模式,无效
+    }
+}
+
+void T1min_EV_Handler ( void )
+{
+    static u8 CTime_min = 0;
+
+    if ( SysPara.CTime )
+    {
+        if ( CTime_min++ >= SysPara.CTime * 30 )	//巡检一次,单位0.5小时
+        {
+            CTime_min = 0;
+            QueueIn ( &Pub_Tasks, CTIME_EV );
+            Set_All485_CTimeFlag();	//参数获取:巡检,之后返回成功或失败
+        }
+    }
+}
+
+void T8min_EV_Handler ( void )
+{
+    DTU_Heart(); 					//心跳
+}
+
+void Time_EV_Handler ( void )
+{
+    if ( System_Flag_10Ms )
+    {
+        System_Flag_10Ms = 0;
+        T10MS_EV_Handler();        //设备状态获取,参数配置,以及数据的发送
+    }
+
+    if ( System_Flag_100Ms )
+    {
+        System_Flag_100Ms = 0;
+        T100MS_EV_Handler();       //LED处理,和喂硬件看门狗
+    }
+
+    if ( System_Flag_1s )
+    {
+        System_Flag_1s = 0;
+        T1S_EV_Handler();          //网关电池电量的计算处理,以及网关数据发送
+    }
+
+    if ( System_Flag_1min )
+    {
+        System_Flag_1min = 0;
+        T1min_EV_Handler();        //切换到巡检数据发送状态
+    }
+
+    if ( System_Flag_8min )
+    {
+        System_Flag_8min = 0;
+        T8min_EV_Handler();        //心跳包的发送
+    }
+
+
+}
+
+
+/**********************信号处理函数**************************/
+void UART1_RECV_EV_Handler ( void ) //485
+{
+    switch ( SysRunType )				//切换协议
+    {
+    case SysRun_Norm:
+        Fas_Recv_A_Frame_Msg();
+        break;
+
+    case SysRun_Test:
+        if ( Test485_Recv_A_Frame_Msg() );
+        else if ( Fas_Recv_A_Frame_Msg() );
+
+        break;
+    }
+}
+void UART2_RECV_EV_Handler ( void ) //3g
+{
+    A8500_Recv_A_Frame_Msg();
+}
+void UART3_RECV_EV_Handler ( void ) //232口用+配置串口
+{
+    switch ( SysRunType )				//切换协议
+    {
+    case SysRun_Norm:
+
+        break;
+
+    case SysRun_Test:
+        if ( Test232_Recv_A_Frame_Msg() );
+        else if ( ProduSet_Recv_A_Frame_Msg() );
+
+        break;
+    }
+}
+
+
+/************************* Application States & Events **************************/
+void Ignore ( void )
+{}
+
+void CTIME_EV_Handler ( void )
+{
+    DTU_Msg ( Command_DevPoll, Get_All485State() );
+}
+
+const tAppStateFp PubAppTb[PUBAPP_MAX_EV] =
+{
+    // ** STATE ***								*** EVENT ** //
+    &Ignore,				//ACDC_EV,
+    &KGL_IN_Hanlder,		//KGL_IN_EV,
+    &Key_XY_Hanlder,		//KEY_XY_EV,          //消音
+    &Key_CS_Hanlder,		//KEY_CS_EV,          //测试
+    &Ignore,				//KGL_IN_LONG_EV,
+    &Key_XY_Long_Hanlder,	//KEY_XY_LONG_EV,     //长按消音键
+    &Key_CS_Long_Hanlder,	//KEY_CS_LONG_EV,     //长按测试键
+    &UART1_RECV_EV_Handler, //UART1_RECV_EV,
+    &UART2_RECV_EV_Handler,	//UART2_RECV_EV,
+    &UART3_RECV_EV_Handler,	//UART3_RECV_EV,
+    &CTIME_EV_Handler,		//CTIME_EV,
+};
+const tAppStateFp FasAppTb[FAS_MAX_ST][FASAPP_MAX_EV] =
+{
+    // ** STATE ***								*** EVENT ** //
+    {
+        &Ignore,
+        &Ignore,
+    },
+};
+
+//if(QueueIn(&SysMsg,i) == QueueFull) break;
+//if(QueueOut(&SysMsg,&sh) == QueueEmpty) break;
+void Init_Param ( void )
+{
+    QueueInit ( &Fas_Tasks );	//fas协议任务
+    QueueInit ( &Pub_Tasks );	//公共任务
+    WGpara.GPRS = 0x01;		//网络制式 2G网络
+    WGpara.YYS = 0x01;			//运营商 1:中国移动
+	EnableMsg.XX = 1;
+}
+
+
+/**********************************封包**********************************************/

+ 54 - 0
stm32 通用函数/Common/ProgramRun.h

@@ -0,0 +1,54 @@
+#ifndef __PROGRAM_RUN_H
+#define __PROGRAM_RUN_H
+/* 包含的头文件---------------------------------------------------------------*/
+#include "stm32f10x.h"
+/* 类型定义 ------------------------------------------------------------------*/
+typedef void ( *tAppStateFp ) ( void );
+
+typedef enum
+{
+    FAS_YDH30P_ST,
+    FAS_MAX_ST
+} tFasAppStates;
+
+/* Events */
+typedef enum
+{
+    UART_RCV_EV,
+    SENDTO_SERVER_EV,
+    FASAPP_MAX_EV
+} tFasAppEvents;
+
+typedef enum
+{
+    ACDC_EV,
+    KGL_IN_EV,
+    KEY_XY_EV,
+    KEY_CS_EV,
+    KGL_IN_LONG_EV,
+    KEY_XY_LONG_EV,
+    KEY_CS_LONG_EV,
+    UART1_RECV_EV,
+    UART2_RECV_EV,
+    UART3_RECV_EV,
+    CTIME_EV,
+    PUBAPP_MAX_EV
+} tPubAppEvents;
+
+/* 常量定义 ------------------------------------------------------------------*/
+extern const tAppStateFp PubAppTb[PUBAPP_MAX_EV];
+extern const tAppStateFp FasAppTb[FAS_MAX_ST][FASAPP_MAX_EV];
+extern tPubAppEvents PubAppEvent;
+extern tFasAppStates FasAppState;
+extern tFasAppEvents FasAppEvent;
+
+/* 宏定义 --------------------------------------------------------------------*/
+/* 函数定义------------------------------------------------------------------ */
+void Init_Param ( void );
+void Time_EV_Handler ( void );
+void T10MS_Count_Handler ( void );
+
+
+
+#endif
+

+ 56 - 0
stm32 通用函数/Common/Stmflash.h

@@ -0,0 +1,56 @@
+#ifndef __STMFLASH_H__
+#define __STMFLASH_H__
+#include "stm32f10x.h"
+//////////////////////////////////////////////////////////////////////////////////
+//本程序只供学习使用,未经作者许可,不得用于其它任何用途
+//ALIENTEK战舰STM32开发板
+//STM32 FLASH 驱动代码
+//正点原子@ALIENTEK
+//技术论坛:www.openedv.com
+//修改日期:2012/9/13
+//版本:V1.0
+//版权所有,盗版必究。
+//Copyright(C) 广州市星翼电子科技有限公司 2009-2019
+//All rights reserved
+//////////////////////////////////////////////////////////////////////////////////
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////
+//用户根据自己的需要设置
+#define FLASH_START_ADDR      0x08005000    //应用起始地址
+#define STM32_FLASH_SIZE 			128 	 				//所选STM32的FLASH容量大小(单位为K)
+#define STM32_FLASH_WREN 			1             //使能FLASH写入(0,不是能;1,使能)
+#define STM32_APP_SIZE 	 			64   					//应用大小:64k
+//////////////////////////////////////////////////////////////////////////////////////////////////////
+
+//FLASH起始地址
+#define STM32_FLASH_BASE 0x08000000 	//STM32 FLASH的起始地址
+//FLASH解锁键值
+
+void STMFLASH_Write_NoCheck ( u32 WriteAddr, u16 *pBuffer, u16 NumToWrite );
+
+u16 STMFLASH_ReadHalfWord ( u32 faddr );		 //读出半字
+void STMFLASH_WriteLenByte ( u32 WriteAddr, u32 DataToWrite, u16 Len );	//指定地址开始写入指定长度的数据
+u32 STMFLASH_ReadLenByte ( u32 ReadAddr, u16 Len );						//指定地址开始读取指定长度数据
+void STMFLASH_Write ( u32 WriteAddr, u16 *pBuffer, u16 NumToWrite );		//从指定地址开始写入指定长度的数据
+void STMFLASH_Read ( u32 ReadAddr, u16 *pBuffer, u16 NumToRead );   		//从指定地址开始读出指定长度的数据
+
+//测试写入
+void Test_Write ( u32 WriteAddr, u16 WriteData );
+#endif
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ 155 - 0
stm32 通用函数/Common/stmflash.c

@@ -0,0 +1,155 @@
+#include "stmflash.h"
+
+//////////////////////////////////////////////////////////////////////////////////
+//本程序只供学习使用,未经作者许可,不得用于其它任何用途
+//ALIENTEK战舰STM32开发板
+//STM32 FLASH 驱动代码
+//正点原子@ALIENTEK
+//技术论坛:www.openedv.com
+//修改日期:2012/9/13
+//版本:V1.0
+//版权所有,盗版必究。
+//Copyright(C) 广州市星翼电子科技有限公司 2009-2019
+//All rights reserved
+//////////////////////////////////////////////////////////////////////////////////
+
+//读取指定地址的半字(16位数据)
+//faddr:读地址(此地址必须为2的倍数!!)
+//返回值:对应数据.
+u16 STMFLASH_ReadHalfWord ( u32 faddr )
+{
+    return * ( vu16* ) faddr;
+}
+#if STM32_FLASH_WREN	//如果使能了写   
+//不检查的写入
+//WriteAddr:起始地址
+//pBuffer:数据指针
+//NumToWrite:半字(16位)数
+void STMFLASH_Write_NoCheck ( u32 WriteAddr, u16 *pBuffer, u16 NumToWrite )
+{
+    u16 i;
+
+    for ( i = 0; i < NumToWrite; i++ )
+    {
+        FLASH_ProgramHalfWord ( WriteAddr, pBuffer[i] );
+        WriteAddr += 2; //地址增加2.
+    }
+}
+//从指定地址开始写入指定长度的数据
+//WriteAddr:起始地址(此地址必须为2的倍数!!)
+//pBuffer:数据指针
+//NumToWrite:半字(16位)数(就是要写入的16位数据的个数.)
+#if STM32_FLASH_SIZE<256
+#define STM_SECTOR_SIZE 1024 //字节
+#else
+#define STM_SECTOR_SIZE	2048
+#endif
+u16 STMFLASH_BUF[STM_SECTOR_SIZE / 2]; //最多是2K字节
+
+extern uint32_t flash_flag;
+
+void STMFLASH_Write ( u32 WriteAddr, u16 *pBuffer, u16 NumToWrite )
+{
+    u32 secpos;	   //扇区地址
+    u16 secoff;	   //扇区内偏移地址(16位字计算)
+    u16 secremain; //扇区内剩余地址(16位字计算)
+    u16 i;
+    u32 offaddr;   //去掉0X08000000后的地址
+
+    FLASH_Unlock();						//解锁
+    offaddr = WriteAddr - STM32_FLASH_BASE;		//实际偏移地址.
+    secpos = offaddr / STM_SECTOR_SIZE;			//扇区地址  0~127 for STM32F103RBT6
+    secoff = ( offaddr % STM_SECTOR_SIZE ) / 2;		//在扇区内的偏移(2个字节为基本单位.)
+    secremain = STM_SECTOR_SIZE / 2 - secoff;		//扇区剩余空间大小
+
+    if ( NumToWrite <= secremain ) secremain = NumToWrite; //不大于该扇区范围
+
+    while ( 1 )
+    {
+        STMFLASH_Read ( secpos * STM_SECTOR_SIZE + STM32_FLASH_BASE, STMFLASH_BUF, STM_SECTOR_SIZE / 2 ); //读出整个扇区的内容
+
+        for ( i = 0; i < secremain; i++ ) //校验数据
+        {
+            if ( STMFLASH_BUF[secoff + i] != 0XFFFF ) break; //需要擦除
+        }
+
+        if ( i < secremain ) //需要擦除
+        {
+            FLASH_ErasePage ( secpos * STM_SECTOR_SIZE + STM32_FLASH_BASE ); //擦除这个扇区
+
+            for ( i = 0; i < secremain; i++ ) //复制
+            {
+                STMFLASH_BUF[i + secoff] = pBuffer[i];
+            }
+
+            STMFLASH_Write_NoCheck ( secpos * STM_SECTOR_SIZE + STM32_FLASH_BASE, STMFLASH_BUF, STM_SECTOR_SIZE / 2 ); //写入整个扇区
+        }
+        else STMFLASH_Write_NoCheck ( WriteAddr, pBuffer, secremain ); //写已经擦除了的,直接写入扇区剩余区间.
+
+        if ( NumToWrite == secremain ) break; //写入结束了
+        else//写入未结束
+        {
+            secpos++;				//扇区地址增1
+            secoff = 0;				//偏移位置为0
+            pBuffer += secremain;  	//指针偏移
+            WriteAddr += secremain;	//写地址偏移
+            NumToWrite -= secremain;	//字节(16位)数递减
+
+            if ( NumToWrite > ( STM_SECTOR_SIZE / 2 ) ) secremain = STM_SECTOR_SIZE / 2; //下一个扇区还是写不完
+            else secremain = NumToWrite; //下一个扇区可以写完了
+        }
+    };
+
+    FLASH_Lock();//上锁
+}
+#endif
+
+//从指定地址开始读出指定长度的数据
+//ReadAddr:起始地址
+//pBuffer:数据指针
+//NumToWrite:半字(16位)数
+void STMFLASH_Read ( u32 ReadAddr, u16 *pBuffer, u16 NumToRead )
+{
+    uint32_t addr = 0x08016000 + 400 * 1024;
+    u16 i;
+
+    if ( ReadAddr >= addr )
+    {
+        for ( i = 0; i < NumToRead; i++ )
+        {
+            pBuffer[i] = 0x0000;
+        }
+
+        return;
+    }
+
+    for ( i = 0; i < NumToRead; i++ )
+    {
+        pBuffer[i] = STMFLASH_ReadHalfWord ( ReadAddr ); //读取2个字节.
+        ReadAddr += 2; //偏移2个字节.
+    }
+}
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////
+//WriteAddr:起始地址
+//WriteData:要写入的数据
+void Test_Write ( u32 WriteAddr, u16 WriteData )
+{
+    STMFLASH_Write ( WriteAddr, &WriteData, 1 ); //写入一个字
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ 18 - 0
stm32 通用函数/Common/sys.c

@@ -0,0 +1,18 @@
+#include "sys.h"
+
+//////////////////////////////////////////////////////////////////////////////////
+//本程序只供学习使用,未经作者许可,不得用于其它任何用途
+//ALIENTEK Mini STM32开发板
+//系统中断分组设置化
+//正点原子@ALIENTEK
+//技术论坛:www.openedv.com
+//修改日期:2012/9/10
+//版本:V1.4
+//版权所有,盗版必究。
+//Copyright(C) 正点原子 2009-2019
+//All rights reserved
+//********************************************************************************
+void NVIC_Configuration ( void )
+{
+    NVIC_PriorityGroupConfig ( NVIC_PriorityGroup_2 );	//设置NVIC中断分组2:2位抢占优先级,2位响应优先级
+}

+ 70 - 0
stm32 通用函数/Common/sys.h

@@ -0,0 +1,70 @@
+#ifndef __SYS_H
+#define __SYS_H
+#include "stm32f10x.h"
+//////////////////////////////////////////////////////////////////////////////////
+//本程序只供学习使用,未经作者许可,不得用于其它任何用途
+//ALIENTEK STM32开发板
+//正点原子@ALIENTEK
+//技术论坛:www.openedv.com
+//修改日期:2012/8/18
+//版本:V1.7
+//版权所有,盗版必究。
+//Copyright(C) 广州市星翼电子科技有限公司 2009-2019
+//All rights reserved
+//////////////////////////////////////////////////////////////////////////////////
+
+//0,不支持ucos
+//1,支持ucos
+#define SYSTEM_SUPPORT_UCOS		0		//定义系统文件夹是否支持UCOS
+
+
+//位带操作,实现51类似的GPIO控制功能
+//具体实现思想,参考<<CM3权威指南>>第五章(87页~92页).
+//IO口操作宏定义
+#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
+#define MEM_ADDR(addr)  *((volatile unsigned long  *)(addr))
+#define BIT_ADDR(addr, bitnum)   MEM_ADDR(BITBAND(addr, bitnum))
+//IO口地址映射
+#define GPIOA_ODR_Addr    (GPIOA_BASE+12) //0x4001080C 
+#define GPIOB_ODR_Addr    (GPIOB_BASE+12) //0x40010C0C 
+#define GPIOC_ODR_Addr    (GPIOC_BASE+12) //0x4001100C 
+#define GPIOD_ODR_Addr    (GPIOD_BASE+12) //0x4001140C 
+#define GPIOE_ODR_Addr    (GPIOE_BASE+12) //0x4001180C 
+#define GPIOF_ODR_Addr    (GPIOF_BASE+12) //0x40011A0C    
+#define GPIOG_ODR_Addr    (GPIOG_BASE+12) //0x40011E0C    
+
+#define GPIOA_IDR_Addr    (GPIOA_BASE+8) //0x40010808 
+#define GPIOB_IDR_Addr    (GPIOB_BASE+8) //0x40010C08 
+#define GPIOC_IDR_Addr    (GPIOC_BASE+8) //0x40011008 
+#define GPIOD_IDR_Addr    (GPIOD_BASE+8) //0x40011408 
+#define GPIOE_IDR_Addr    (GPIOE_BASE+8) //0x40011808 
+#define GPIOF_IDR_Addr    (GPIOF_BASE+8) //0x40011A08 
+#define GPIOG_IDR_Addr    (GPIOG_BASE+8) //0x40011E08 
+
+//IO口操作,只对单一的IO口!
+//确保n的值小于16!
+#define PAout(n)   BIT_ADDR(GPIOA_ODR_Addr,n)  //输出 
+#define PAin(n)    BIT_ADDR(GPIOA_IDR_Addr,n)  //输入 
+
+#define PBout(n)   BIT_ADDR(GPIOB_ODR_Addr,n)  //输出 
+#define PBin(n)    BIT_ADDR(GPIOB_IDR_Addr,n)  //输入 
+
+#define PCout(n)   BIT_ADDR(GPIOC_ODR_Addr,n)  //输出 
+#define PCin(n)    BIT_ADDR(GPIOC_IDR_Addr,n)  //输入 
+
+#define PDout(n)   BIT_ADDR(GPIOD_ODR_Addr,n)  //输出 
+#define PDin(n)    BIT_ADDR(GPIOD_IDR_Addr,n)  //输入 
+
+#define PEout(n)   BIT_ADDR(GPIOE_ODR_Addr,n)  //输出 
+#define PEin(n)    BIT_ADDR(GPIOE_IDR_Addr,n)  //输入
+
+#define PFout(n)   BIT_ADDR(GPIOF_ODR_Addr,n)  //输出 
+#define PFin(n)    BIT_ADDR(GPIOF_IDR_Addr,n)  //输入
+
+#define PGout(n)   BIT_ADDR(GPIOG_ODR_Addr,n)  //输出 
+#define PGin(n)    BIT_ADDR(GPIOG_IDR_Addr,n)  //输入
+
+
+void NVIC_Configuration ( void );
+
+#endif