Browse Source

nor_flash和ext_sram新增底层驱动

樊春春 2 years ago
parent
commit
c597c8d267
42 changed files with 1640 additions and 723 deletions
  1. BIN
      Libraries/CMSIS/Lib/ARM/arm_cortexM0b_math.lib
  2. BIN
      Libraries/CMSIS/Lib/ARM/arm_cortexM0l_math.lib
  3. BIN
      Libraries/CMSIS/Lib/ARM/arm_cortexM3b_math.lib
  4. BIN
      Libraries/CMSIS/Lib/ARM/arm_cortexM3l_math.lib
  5. BIN
      Libraries/CMSIS/Lib/ARM/arm_cortexM4b_math.lib
  6. BIN
      Libraries/CMSIS/Lib/ARM/arm_cortexM4bf_math.lib
  7. BIN
      Libraries/CMSIS/Lib/ARM/arm_cortexM4l_math.lib
  8. BIN
      Libraries/CMSIS/Lib/ARM/arm_cortexM4lf_math.lib
  9. BIN
      Libraries/CMSIS/Lib/ARM/arm_cortexM7b_math.lib
  10. BIN
      Libraries/CMSIS/Lib/ARM/arm_cortexM7bfdp_math.lib
  11. BIN
      Libraries/CMSIS/Lib/ARM/arm_cortexM7bfsp_math.lib
  12. BIN
      Libraries/CMSIS/Lib/ARM/arm_cortexM7l_math.lib
  13. BIN
      Libraries/CMSIS/Lib/ARM/arm_cortexM7lfdp_math.lib
  14. BIN
      Libraries/CMSIS/Lib/ARM/arm_cortexM7lfsp_math.lib
  15. 1 1
      User/Bsp/armfly_bsp.c
  16. 1 0
      User/Bsp/armfly_bsp.h
  17. 200 0
      User/Bsp/ext_sram/ext_sram.c
  18. 12 0
      User/Bsp/ext_sram/ext_sram.h
  19. 24 18
      User/Bsp/interface/interface.c
  20. 10 2
      User/Bsp/interface/interface.h
  21. 641 0
      User/Bsp/nor_flash/nor_flash.c
  22. 75 0
      User/Bsp/nor_flash/nor_flash.h
  23. 0 6
      User/app/iec104/PRIO_QUEUE_Iec10x.h
  24. 0 25
      User/app/iec104/common_datatype.h
  25. 0 0
      User/app/iec104/hxcp.c
  26. 74 72
      User/app/iec104/iec101.c
  27. 33 32
      User/app/iec104/iec101.h
  28. 106 119
      User/app/iec104/iec104.c
  29. 126 118
      User/app/iec104/iec104.h
  30. 0 167
      User/app/iec104/iec104_init.c
  31. 22 22
      User/app/iec104/iec10x.c
  32. 77 75
      User/app/iec104/iec10x.h
  33. 37 0
      User/app/iec104/iec10x_conf.h
  34. 149 0
      User/app/iec104/iec10x_init.c
  35. 9 23
      User/app/iec104/iec10x_prio_queue.c
  36. 6 0
      User/app/iec104/iec10x_prio_queue.h
  37. 24 0
      User/app/iec104/iec10x_type.h
  38. 3 11
      User/app/net/net.c
  39. 2 1
      User/main.c
  40. 2 1
      User/main.h
  41. 0 30
      User/project_var.h
  42. 6 0
      platformio.ini

BIN
Libraries/CMSIS/Lib/ARM/arm_cortexM0b_math.lib


BIN
Libraries/CMSIS/Lib/ARM/arm_cortexM0l_math.lib


BIN
Libraries/CMSIS/Lib/ARM/arm_cortexM3b_math.lib


BIN
Libraries/CMSIS/Lib/ARM/arm_cortexM3l_math.lib


BIN
Libraries/CMSIS/Lib/ARM/arm_cortexM4b_math.lib


BIN
Libraries/CMSIS/Lib/ARM/arm_cortexM4bf_math.lib


BIN
Libraries/CMSIS/Lib/ARM/arm_cortexM4l_math.lib


BIN
Libraries/CMSIS/Lib/ARM/arm_cortexM4lf_math.lib


BIN
Libraries/CMSIS/Lib/ARM/arm_cortexM7b_math.lib


BIN
Libraries/CMSIS/Lib/ARM/arm_cortexM7bfdp_math.lib


BIN
Libraries/CMSIS/Lib/ARM/arm_cortexM7bfsp_math.lib


BIN
Libraries/CMSIS/Lib/ARM/arm_cortexM7l_math.lib


BIN
Libraries/CMSIS/Lib/ARM/arm_cortexM7lfdp_math.lib


BIN
Libraries/CMSIS/Lib/ARM/arm_cortexM7lfsp_math.lib


+ 1 - 1
User/Bsp/armfly_bsp.c

@@ -18,7 +18,7 @@ void bsp_init(void)
     spi1_init();
     uart1_init();
     uart3_init();
-
+    nor_flash_init();
     lwip_setup();
     // can初始化
     can_network_init();

+ 1 - 0
User/Bsp/armfly_bsp.h

@@ -6,6 +6,7 @@
 #include "fly_param.h"
 #include "interface.h"
 #include "lwip_init.h"
+#include "nor_flash.h"
 #include "queue.h"
 #include "spi.h"
 #include "stm32f4x7_phy.h"

+ 200 - 0
User/Bsp/ext_sram/ext_sram.c

@@ -0,0 +1,200 @@
+/*
+*********************************************************************************************************
+*
+*	模块名称 : 外部SRAM驱动模块
+*	文件名称 : bsp_fsmc_sram.c
+*	版    本 : V2.4
+*	说    明 : 安富莱STM32-F4开发板标配的SRAM为 IS61WV102416BLL-10TL  容量2M字节,16Bit,10ns速度
+*
+*	修改记录 :
+*		版本号  日期        作者     说明
+*		V1.0    2013-02-01 armfly  正式发布
+*
+*	Copyright (C), 2013-2014, 安富莱电子 www.armfly.com
+*
+*********************************************************************************************************
+*/
+
+#include "ext_sram.h"
+#include "interface.h"
+#include <inttypes.h>
+
+/*
+*********************************************************************************************************
+*	函 数 名: ext_sram_init
+*	功能说明: 配置连接外部SRAM的GPIO和FSMC
+*	形    参:  无
+*	返 回 值: 无
+*********************************************************************************************************
+*/
+void ext_sram_init(void)
+{
+    FSMC_NORSRAMInitTypeDef       FSMC_NORSRAMInitStructure;
+    FSMC_NORSRAMTimingInitTypeDef p;
+
+    /* SRAM 的 GPIO :
+        PD0/FSMC_D2
+        PD1/FSMC_D3
+        PD4/FSMC_NOE
+        PD5/FSMC_NWE
+        PD8/FSMC_D13
+        PD9/FSMC_D14
+        PD10/FSMC_D15
+        PD11/FSMC_A16
+        PD12/FSMC_A17
+        PD13/FSMC_A18
+        PD14/FSMC_D0
+        PD15/FSMC_D1
+
+        PE0/FSMC_NBL0
+        PE1/FSMC_NBL1
+        PE3/FSMC_A19
+        PE4/FSMC_A20	-- 参与片选的译码
+        PE5/FSMC_A21	-- 参与片选的译码
+        PE7/FSMC_D4
+        PE8/FSMC_D5
+        PE9/FSMC_D6
+        PE10/FSMC_D7
+        PE11/FSMC_D8
+        PE12/FSMC_D9
+        PE13/FSMC_D10
+        PE14/FSMC_D11
+        PE15/FSMC_D12
+
+        PF0/FSMC_A0
+        PF1/FSMC_A1
+        PF2/FSMC_A2
+        PF3/FSMC_A3
+        PF4/FSMC_A4
+        PF5/FSMC_A5
+        PF12/FSMC_A6
+        PF13/FSMC_A7
+        PF14/FSMC_A8
+        PF15/FSMC_A9
+
+        PG0/FSMC_A10
+        PG1/FSMC_A11
+        PG2/FSMC_A12
+        PG3/FSMC_A13
+        PG4/FSMC_A14
+        PG5/FSMC_A15
+        PG10/FSMC_NE3	--- 片选主信号
+    */
+
+    /*-- FSMC Configuration ------------------------------------------------------*/
+    p.FSMC_AddressSetupTime      = 3; /* 设置为2会出错; 3正常 */
+    p.FSMC_AddressHoldTime       = 0;
+    p.FSMC_DataSetupTime         = 2; /* 设置为1出错,2正常 */
+    p.FSMC_BusTurnAroundDuration = 1;
+    p.FSMC_CLKDivision           = 0;
+    p.FSMC_DataLatency           = 0;
+    p.FSMC_AccessMode            = FSMC_AccessMode_A;
+
+    FSMC_NORSRAMInitStructure.FSMC_Bank                  = FSMC_Bank1_NORSRAM3;
+    FSMC_NORSRAMInitStructure.FSMC_DataAddressMux        = FSMC_DataAddressMux_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_MemoryType            = FSMC_MemoryType_SRAM; // FSMC_MemoryType_PSRAM;
+    FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth       = FSMC_MemoryDataWidth_16b;
+    FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode       = FSMC_BurstAccessMode_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait      = FSMC_AsynchronousWait_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity    = FSMC_WaitSignalPolarity_Low;
+    FSMC_NORSRAMInitStructure.FSMC_WrapMode              = FSMC_WrapMode_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive      = FSMC_WaitSignalActive_BeforeWaitState;
+    FSMC_NORSRAMInitStructure.FSMC_WriteOperation        = FSMC_WriteOperation_Enable;
+    FSMC_NORSRAMInitStructure.FSMC_WaitSignal            = FSMC_WaitSignal_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_ExtendedMode          = FSMC_ExtendedMode_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_WriteBurst            = FSMC_WriteBurst_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
+    FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct     = &p;
+
+    FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
+
+    /*!< Enable FSMC Bank1_SRAM3 Bank */
+    FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM3, ENABLE);
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: bsp_TestExtSRAM
+*	功能说明: 扫描测试外部SRAM
+*	形    参: 无
+*	返 回 值: 0 表示测试通过; 大于0表示错误单元的个数。
+*********************************************************************************************************
+*/
+uint8_t test_ext_sram(void)
+{
+    uint32_t      i;
+    uint32_t     *pSRAM;
+    uint8_t      *pBytes;
+    uint32_t      err;
+    const uint8_t ByteBuf[4] = {0x55, 0xA5, 0x5A, 0xAA};
+
+    /* 写SRAM */
+    pSRAM = (uint32_t *)EXT_SRAM_ADDR;
+    for (i = 0; i < EXT_SRAM_SIZE / 4; i++)
+    {
+        *pSRAM++ = i;
+    }
+
+    /* 读SRAM */
+    err   = 0;
+    pSRAM = (uint32_t *)EXT_SRAM_ADDR;
+    for (i = 0; i < EXT_SRAM_SIZE / 4; i++)
+    {
+        if (*pSRAM++ != i)
+        {
+            err++;
+        }
+    }
+
+    if (err > 0)
+    {
+        return (4 * err);
+    }
+
+    /* 对SRAM 的数据求反并写入 */
+    pSRAM = (uint32_t *)EXT_SRAM_ADDR;
+    for (i = 0; i < EXT_SRAM_SIZE / 4; i++)
+    {
+        *pSRAM = ~*pSRAM;
+        pSRAM++;
+    }
+
+    /* 再次比较SRAM的数据 */
+    err   = 0;
+    pSRAM = (uint32_t *)EXT_SRAM_ADDR;
+    for (i = 0; i < EXT_SRAM_SIZE / 4; i++)
+    {
+        if (*pSRAM++ != (~i))
+        {
+            err++;
+        }
+    }
+
+    if (err > 0)
+    {
+        return (4 * err);
+    }
+
+    /* 测试按字节方式访问, 目的是验证 FSMC_NBL0 、 FSMC_NBL1 口线 */
+    pBytes = (uint8_t *)EXT_SRAM_ADDR;
+    for (i = 0; i < sizeof(ByteBuf); i++)
+    {
+        *pBytes++ = ByteBuf[i];
+    }
+
+    /* 比较SRAM的数据 */
+    err    = 0;
+    pBytes = (uint8_t *)EXT_SRAM_ADDR;
+    for (i = 0; i < sizeof(ByteBuf); i++)
+    {
+        if (*pBytes++ != ByteBuf[i])
+        {
+            err++;
+        }
+    }
+    if (err > 0)
+    {
+        return err;
+    }
+    return 0;
+}

+ 12 - 0
User/Bsp/ext_sram/ext_sram.h

@@ -0,0 +1,12 @@
+#ifndef _FSMC_SRAM_H
+#define _FSMC_SRAM_H
+
+#include "includes.h"
+
+#define EXT_SRAM_ADDR ((uint32_t)0x68000000)
+#define EXT_SRAM_SIZE (2 * 1024 * 1024)
+
+void    ext_sram_init(void);
+uint8_t test_ext_sram(void);
+
+#endif

+ 24 - 18
User/Bsp/interface/interface.c

@@ -98,13 +98,17 @@ Interface_struct interface_info[] = {
     {.type = kFSMC, .GPIOx = GPIOD, .GPIO_Pin = GPIO_Pin_8, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOD, .GPIO_PinSource = GPIO_PinSource8, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
     {.type = kFSMC, .GPIOx = GPIOD, .GPIO_Pin = GPIO_Pin_9, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOD, .GPIO_PinSource = GPIO_PinSource9, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
     {.type = kFSMC, .GPIOx = GPIOD, .GPIO_Pin = GPIO_Pin_10, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOD, .GPIO_PinSource = GPIO_PinSource10, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    // {.type = kFSMC, .GPIOx = GPIOD, .GPIO_Pin = GPIO_Pin_11, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOD, .GPIO_PinSource = GPIO_PinSource11, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    // {.type = kFSMC, .GPIOx = GPIOD, .GPIO_Pin = GPIO_Pin_12, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOD, .GPIO_PinSource = GPIO_PinSource12, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOD, .GPIO_Pin = GPIO_Pin_11, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOD, .GPIO_PinSource = GPIO_PinSource11, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOD, .GPIO_Pin = GPIO_Pin_12, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOD, .GPIO_PinSource = GPIO_PinSource12, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
     {.type = kFSMC, .GPIOx = GPIOD, .GPIO_Pin = GPIO_Pin_13, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOD, .GPIO_PinSource = GPIO_PinSource13, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
     {.type = kFSMC, .GPIOx = GPIOD, .GPIO_Pin = GPIO_Pin_14, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOD, .GPIO_PinSource = GPIO_PinSource14, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
     {.type = kFSMC, .GPIOx = GPIOD, .GPIO_Pin = GPIO_Pin_15, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOD, .GPIO_PinSource = GPIO_PinSource15, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOE, .GPIO_Pin = GPIO_Pin_0, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOE, .GPIO_PinSource = GPIO_PinSource0, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOE, .GPIO_Pin = GPIO_Pin_1, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOE, .GPIO_PinSource = GPIO_PinSource1, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOE, .GPIO_Pin = GPIO_Pin_3, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOE, .GPIO_PinSource = GPIO_PinSource3, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
     {.type = kFSMC, .GPIOx = GPIOE, .GPIO_Pin = GPIO_Pin_4, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOE, .GPIO_PinSource = GPIO_PinSource4, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
     {.type = kFSMC, .GPIOx = GPIOE, .GPIO_Pin = GPIO_Pin_5, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOE, .GPIO_PinSource = GPIO_PinSource5, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOE, .GPIO_Pin = GPIO_Pin_6, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOE, .GPIO_PinSource = GPIO_PinSource6, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
     {.type = kFSMC, .GPIOx = GPIOE, .GPIO_Pin = GPIO_Pin_7, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOE, .GPIO_PinSource = GPIO_PinSource7, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
     {.type = kFSMC, .GPIOx = GPIOE, .GPIO_Pin = GPIO_Pin_8, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOE, .GPIO_PinSource = GPIO_PinSource8, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
     {.type = kFSMC, .GPIOx = GPIOE, .GPIO_Pin = GPIO_Pin_9, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOE, .GPIO_PinSource = GPIO_PinSource9, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
@@ -114,26 +118,28 @@ Interface_struct interface_info[] = {
     {.type = kFSMC, .GPIOx = GPIOE, .GPIO_Pin = GPIO_Pin_13, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOE, .GPIO_PinSource = GPIO_PinSource13, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
     {.type = kFSMC, .GPIOx = GPIOE, .GPIO_Pin = GPIO_Pin_14, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOE, .GPIO_PinSource = GPIO_PinSource14, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
     {.type = kFSMC, .GPIOx = GPIOE, .GPIO_Pin = GPIO_Pin_15, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOE, .GPIO_PinSource = GPIO_PinSource15, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    // {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_0, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource0, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    // {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_1, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource1, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    // {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_2, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource2, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    // {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_3, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource3, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    // {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_4, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource4, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    // {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_5, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource5, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    // {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_12, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource12, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    // {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_13, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource13, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    // {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_14, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource14, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    // {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_15, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource15, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    // {.type = kFSMC, .GPIOx = GPIOG, .GPIO_Pin = GPIO_Pin_0, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOG, .GPIO_PinSource = GPIO_PinSource0, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    // {.type = kFSMC, .GPIOx = GPIOG, .GPIO_Pin = GPIO_Pin_1, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOG, .GPIO_PinSource = GPIO_PinSource1, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    // {.type = kFSMC, .GPIOx = GPIOG, .GPIO_Pin = GPIO_Pin_2, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOG, .GPIO_PinSource = GPIO_PinSource2, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    // {.type = kFSMC, .GPIOx = GPIOG, .GPIO_Pin = GPIO_Pin_3, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOG, .GPIO_PinSource = GPIO_PinSource3, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    // {.type = kFSMC, .GPIOx = GPIOG, .GPIO_Pin = GPIO_Pin_4, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOG, .GPIO_PinSource = GPIO_PinSource4, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    // {.type = kFSMC, .GPIOx = GPIOG, .GPIO_Pin = GPIO_Pin_5, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOG, .GPIO_PinSource = GPIO_PinSource5, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_0, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource0, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_1, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource1, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_2, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource2, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_3, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource3, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_4, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource4, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_5, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource5, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_12, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource12, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_13, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource13, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_14, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource14, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOF, .GPIO_Pin = GPIO_Pin_15, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOF, .GPIO_PinSource = GPIO_PinSource15, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOG, .GPIO_Pin = GPIO_Pin_0, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOG, .GPIO_PinSource = GPIO_PinSource0, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOG, .GPIO_Pin = GPIO_Pin_1, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOG, .GPIO_PinSource = GPIO_PinSource1, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOG, .GPIO_Pin = GPIO_Pin_2, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOG, .GPIO_PinSource = GPIO_PinSource2, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOG, .GPIO_Pin = GPIO_Pin_3, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOG, .GPIO_PinSource = GPIO_PinSource3, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOG, .GPIO_Pin = GPIO_Pin_4, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOG, .GPIO_PinSource = GPIO_PinSource4, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOG, .GPIO_Pin = GPIO_Pin_5, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOG, .GPIO_PinSource = GPIO_PinSource5, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
+    {.type = kFSMC, .GPIOx = GPIOG, .GPIO_Pin = GPIO_Pin_9, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOG, .GPIO_PinSource = GPIO_PinSource9, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
     {.type = kFSMC, .GPIOx = GPIOG, .GPIO_Pin = GPIO_Pin_10, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOG, .GPIO_PinSource = GPIO_PinSource10, .GPIO_AF = GPIO_AF_FSMC}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
 
     // input
     {.type = kInput, .In_Type = DM9K_INIT, .GPIOx = DM9K_RESET_PORT, .GPIO_Pin = DM9K_RESET_PIN, .GPIO_Mode = GPIO_Mode_IN, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_NOPULL},
+    {.type = kInput, .In_Type = NOR_FLASH_STATE, .GPIOx = NOR_FLASH_STATE_PORT, .GPIO_Pin = NOR_FLASH_STATE_PIN, .GPIO_Mode = GPIO_Mode_IN, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_NOPULL},
 
     // output
     {.type = kOutput, .Out_Type = kRunLed, .GPIOx = LED0_RUN_PORT, .GPIO_Pin = LED0_RUN_PIN, .GPIO_Mode = GPIO_Mode_OUT, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_High_Speed, .GPIO_PuPd = GPIO_PuPd_DOWN},

+ 10 - 2
User/Bsp/interface/interface.h

@@ -67,6 +67,7 @@ typedef enum
 typedef enum
 {
     DM9K_INIT,
+    NOR_FLASH_STATE,
     kInputEnd,
 } Input_type;
 
@@ -134,13 +135,20 @@ typedef struct
 #define ETH_RESET_PIN  GPIO_Pin_6
 // #define ETH_RXER_PORT GPIOG
 // #define ETH_RXER_PIN GPIO_PIN_12
-#define ETH_RESET_ON    GPIO_ResetBits(ETH_RESET_PORT, ETH_RESET_PIN)
-#define ETH_RESET_OFF   GPIO_SetBits(ETH_RESET_PORT, ETH_RESET_PIN)
+#define ETH_RESET_ON  GPIO_ResetBits(ETH_RESET_PORT, ETH_RESET_PIN)
+#define ETH_RESET_OFF GPIO_SetBits(ETH_RESET_PORT, ETH_RESET_PIN)
+
+// input
 #define DM9K_RESET_PORT GPIOA
 #define DM9K_RESET_PIN  GPIO_Pin_15
 #define DM9K_RESET_ON   GPIO_ResetBits(DM9K_RESET_PORT, DM9K_RESET_PIN)
 #define DM9K_RESET_OFF  GPIO_SetBits(DM9K_RESET_PORT, DM9K_RESET_PIN)
 
+#define NOR_FLASH_STATE_PORT GPIOD
+#define NOR_FLASH_STATE_PIN  GPIO_Pin_6
+#define DM9K_RESET_ON        GPIO_ResetBits(NOR_FLASH_STATE_PORT, NOR_FLASH_STATE_PIN)
+#define DM9K_RESET_OFF       GPIO_SetBits(NOR_FLASH_STATE_PORT, NOR_FLASH_STATE_PIN)
+
 // output
 #define LED0_RUN_PORT GPIOC
 #define LED0_RUN_PIN  GPIO_Pin_2

+ 641 - 0
User/Bsp/nor_flash/nor_flash.c

@@ -0,0 +1,641 @@
+/*
+*********************************************************************************************************
+*
+*	模块名称 : NOR Flash驱动模块
+*	文件名称 : bsp_nor_flash.c
+*	版    本 : V1.0
+*	说    明 : 安富莱STM32-V5开发板标配的NOR Flash 为 S29GL128P10TFI01  容量16M字节,16Bit,100ns速度
+*				物理地址 : 0x6400 0000
+*	修改记录 :
+*		版本号  日期        作者     说明
+*		V1.0    2013-02-01 armfly  正式发布
+*
+*	Copyright (C), 2013-2014, 安富莱电子 www.armfly.com
+*
+*********************************************************************************************************
+*/
+
+/*
+    S29GL128P 内存组织结构: 每个扇区128K字节,一共128个扇区。总容量为 16M字节。按16Bit访问。
+
+    挂在STM32上,对应的物理地址范围为 : 0x6400 0000 - 0x64FF FFFF.  只能按16Bit模式访问。
+
+*/
+
+#include "nor_flash.h"
+#include "interface.h"
+#include <inttypes.h>
+
+#define ADDR_SHIFT(A)            (NOR_FLASH_ADDR + (2 * (A)))
+#define NOR_WRITE(Address, Data) (*(__IO uint16_t *)(Address) = (Data))
+
+/* 判忙时的执行语句循环次数 */
+#define BlockErase_Timeout ((uint32_t)0x00A00000)
+#define ChipErase_Timeout  ((uint32_t)0x30000000)
+#define Program_Timeout    ((uint32_t)0x00001400)
+
+/* PD6 是NOR Flash输出到STM32的忙信号, 通过GPIO查询方式判忙 */
+#define NOR_IS_BUSY() (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_6) == RESET)
+
+static void    NOR_QuitToReadStatus(void);
+static uint8_t NOR_GetStatus(uint32_t Timeout);
+
+/*
+*********************************************************************************************************
+*	函 数 名: bsp_InitNorFlash
+*	功能说明: 配置连接外部NOR Flash的GPIO和FSMC
+*	形    参: 无
+*	返 回 值: 无
+*********************************************************************************************************
+*/
+void nor_flash_init(void)
+{
+    FSMC_NORSRAMInitTypeDef       FSMC_NORSRAMInitStructure;
+    FSMC_NORSRAMTimingInitTypeDef p;
+    /* NOR Flash 的 GPIO :
+        PD0/FSMC_D2
+        PD1/FSMC_D3
+        PD4/FSMC_NOE
+        PD5/FSMC_NWE
+
+        PD6/FSMC_NWAIT		- 忙信号,配置为GPIO,输入模式,通过软件查询方式判忙
+
+        PD8/FSMC_D13
+        PD9/FSMC_D14
+        PD10/FSMC_D15
+        PD11/FSMC_CLE/FSMC_A16
+        PD12/FSMC_ALE/FSMC_A17
+        PD13/FSMC_A18
+        PD14/FSMC_D0
+        PD15/FSMC_D1
+
+        PE3/FSMC_A19
+        PE4/FSMC_A20
+        PE5/FSMC_A21
+        PE6/FSMC_A22
+        PE7/FSMC_D4
+        PE8/FSMC_D5
+        PE9/FSMC_D6
+        PE10/FSMC_D7
+        PE11/FSMC_D8
+        PE12/FSMC_D9
+        PE13/FSMC_D10
+        PE14/FSMC_D11
+        PE15/FSMC_D12
+
+        PF0/FSMC_A0
+        PF1/FSMC_A1
+        PF2/FSMC_A2
+        PF3/FSMC_A3
+        PF4/FSMC_A4
+        PF5/FSMC_A5
+        PF12/FSMC_A6
+        PF13/FSMC_A7
+        PF14/FSMC_A8
+        PF15/FSMC_A9
+
+        PG0/FSMC_A10
+        PG1/FSMC_A11
+        PG2/FSMC_A12
+        PG3/FSMC_A13
+        PG4/FSMC_A14
+        PG5/FSMC_A15
+        PG9/FSMC_NE2	- 片选信号
+    */
+
+    /*-- FSMC Configuration ------------------------------------------------------*/
+    p.FSMC_AddressSetupTime      = 0x06; /* 0x05正常, 0x04 出错 */
+    p.FSMC_AddressHoldTime       = 0x01;
+    p.FSMC_DataSetupTime         = 0x0C; /* 0x0B正常, 0x0A 出错 */
+    p.FSMC_BusTurnAroundDuration = 0x00;
+    p.FSMC_CLKDivision           = 0x00;
+    p.FSMC_DataLatency           = 0x00;
+    p.FSMC_AccessMode            = FSMC_AccessMode_B;
+
+    FSMC_NORSRAMInitStructure.FSMC_Bank                  = FSMC_Bank1_NORSRAM2;
+    FSMC_NORSRAMInitStructure.FSMC_DataAddressMux        = FSMC_DataAddressMux_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_MemoryType            = FSMC_MemoryType_NOR;
+    FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth       = FSMC_MemoryDataWidth_16b;
+    FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode       = FSMC_BurstAccessMode_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait      = FSMC_AsynchronousWait_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity    = FSMC_WaitSignalPolarity_Low;
+    FSMC_NORSRAMInitStructure.FSMC_WrapMode              = FSMC_WrapMode_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive      = FSMC_WaitSignalActive_BeforeWaitState;
+    FSMC_NORSRAMInitStructure.FSMC_WriteOperation        = FSMC_WriteOperation_Enable;
+    FSMC_NORSRAMInitStructure.FSMC_WaitSignal            = FSMC_WaitSignal_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_ExtendedMode          = FSMC_ExtendedMode_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_WriteBurst            = FSMC_WriteBurst_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
+    FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct     = &p;
+
+    FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
+
+    /*!< Enable FSMC Bank1_SRAM2 Bank */
+    FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2, ENABLE);
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: NOR_ReadID
+*	功能说明: 读取NOR Flash的器件ID
+*	形    参: 无
+*	返 回 值: 器件ID,32Bit, 高8bit 是Manufacturer_Code, 低24bit是器件ID
+*********************************************************************************************************
+*/
+uint32_t NOR_ReadID(void)
+{
+    uint32_t uiID;
+    uint8_t  id1, id2, id3, id4;
+
+    NOR_WRITE(ADDR_SHIFT(0x0555), 0x00AA);
+    NOR_WRITE(ADDR_SHIFT(0x02AA), 0x0055);
+    NOR_WRITE(ADDR_SHIFT(0x0555), 0x0090);
+
+    id1 = *(__IO uint16_t *)ADDR_SHIFT(0x0000);
+    id2 = *(__IO uint16_t *)ADDR_SHIFT(0x0001);
+    id3 = *(__IO uint16_t *)ADDR_SHIFT(0x000E);
+    id4 = *(__IO uint16_t *)ADDR_SHIFT(0x000F);
+
+    uiID = ((uint32_t)id1 << 24) | ((uint32_t)id2 << 16) | ((uint32_t)id3 << 8) | id4;
+
+    NOR_WRITE(NOR_FLASH_ADDR, 0x00F0); /* 退出ID模式 */
+
+    return uiID;
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: NOR_QuitToReadStatus
+*	功能说明: 复位NOR,退到读状态
+*	形    参: 无
+*	返 回 值: 无
+*********************************************************************************************************
+*/
+static void NOR_QuitToReadStatus(void)
+{
+    NOR_WRITE(ADDR_SHIFT(0x00555), 0x00AA);
+    NOR_WRITE(ADDR_SHIFT(0x002AA), 0x0055);
+    NOR_WRITE(NOR_FLASH_ADDR, 0x00F0);
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: NOR_GetStatus
+*	功能说明: 读取NOR的操作状态
+*	形    参: 无
+*	返 回 值: 0表示成功.   NOR_SUCCESS, NOR_ERROR, NOR_TIMEOUT
+*********************************************************************************************************
+*/
+static uint8_t NOR_GetStatus(uint32_t Timeout)
+{
+    uint16_t val1    = 0x00;
+    uint16_t val2    = 0x00;
+    uint8_t  status  = NOR_ONGOING;
+    uint32_t timeout = Timeout;
+
+    /* 等待NOR输出忙信号,高电平时等待。避免NOR的忙信号还未反映过来导致CPU提前认为不忙了 */
+    while ((!NOR_IS_BUSY()) && (timeout > 0))
+    {
+        timeout--;
+    }
+
+    /* 等待NOR忙信号结束,低电平时等待 */
+    timeout = Timeout;
+    while (NOR_IS_BUSY() && (timeout > 0))
+    {
+        timeout--;
+    }
+
+    /*
+        - DQ 6 编程时跳变
+        - DQ 6 和 DQ 2 在擦除时跳变
+        - DQ 2 在擦除挂起时跳变
+        - DQ 1 在编程错误时置1
+        - DQ 5 在超时时置1
+    */
+    /* 通过读取DQ6, DQ5 的数据位是否存在翻转现象判断NOR 内部操作是否完成。如果正忙,则第2次读和第1次读的数据不同 */
+    while ((Timeout != 0x00) && (status != NOR_SUCCESS))
+    {
+        Timeout--;
+
+        /* Read DQ6 */
+        val1 = *(__IO uint16_t *)(NOR_FLASH_ADDR);
+        val2 = *(__IO uint16_t *)(NOR_FLASH_ADDR);
+
+        /* If DQ6 did not toggle between the two reads then return NOR_Success */
+        if ((val1 & 0x0040) == (val2 & 0x0040))
+        {
+            return NOR_SUCCESS;
+        }
+
+        /* Read DQ2 */
+        if ((val1 & 0x0020) != 0x0020)
+        {
+            status = NOR_ONGOING;
+        }
+
+        val1 = *(__IO uint16_t *)(NOR_FLASH_ADDR);
+        val2 = *(__IO uint16_t *)(NOR_FLASH_ADDR);
+
+        if ((val1 & 0x0040) == (val2 & 0x0040))
+        {
+            return NOR_SUCCESS;
+        }
+        else if ((val1 & 0x0020) == 0x0020)
+        {
+            status = NOR_ERROR;
+            NOR_QuitToReadStatus();
+        }
+    }
+
+    if (Timeout == 0x00)
+    {
+        status = NOR_TIMEOUT;
+        NOR_QuitToReadStatus();
+    }
+
+    /* 返回操作状态 */
+    return (status);
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: NOR_EraseChip
+*	功能说明: 擦除NOR Flash整个芯片
+*	形    参: 无
+*	返 回 值: 0表示成功
+*********************************************************************************************************
+*/
+uint8_t NOR_EraseChip(void)
+{
+    NOR_WRITE(ADDR_SHIFT(0x0555), 0x00AA);
+    NOR_WRITE(ADDR_SHIFT(0x02AA), 0x0055);
+    NOR_WRITE(ADDR_SHIFT(0x0555), 0x0080);
+    NOR_WRITE(ADDR_SHIFT(0x0555), 0x00AA);
+    NOR_WRITE(ADDR_SHIFT(0x02AA), 0x0055);
+    NOR_WRITE(ADDR_SHIFT(0x0555), 0x0010);
+
+    return (NOR_GetStatus(ChipErase_Timeout));
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: NOR_StartEraseChip
+*	功能说明: 开始擦除NOR Flash整个芯片, 不等待结束
+*	形    参: 无
+*	返 回 值: 0表示成功
+*********************************************************************************************************
+*/
+void NOR_StartEraseChip(void)
+{
+    NOR_WRITE(ADDR_SHIFT(0x0555), 0x00AA);
+    NOR_WRITE(ADDR_SHIFT(0x02AA), 0x0055);
+    NOR_WRITE(ADDR_SHIFT(0x0555), 0x0080);
+    NOR_WRITE(ADDR_SHIFT(0x0555), 0x00AA);
+    NOR_WRITE(ADDR_SHIFT(0x02AA), 0x0055);
+    NOR_WRITE(ADDR_SHIFT(0x0555), 0x0010);
+
+    NOR_GetStatus(1000);
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: NOR_CheckComplete
+*	功能说明: 检测擦除是否完成
+*	形    参: 无
+*	返 回 值: 0表示成功   NOR_SUCCESS, NOR_ERROR, NOR_TIMEOUT
+*********************************************************************************************************
+*/
+uint8_t NOR_CheckStatus(void)
+{
+    uint16_t val1    = 0x00;
+    uint16_t val2    = 0x00;
+    uint8_t  status  = NOR_ONGOING;
+    uint32_t timeout = 10;
+
+    /*
+        - DQ 6 编程时跳变
+        - DQ 6 和 DQ 2 在擦除时跳变
+        - DQ 2 在擦除挂起时跳变
+        - DQ 1 在编程错误时置1
+        - DQ 5 在超时时置1
+    */
+    /* 通过读取DQ6, DQ5 的数据位是否存在翻转现象判断NOR 内部操作是否完成。如果正忙,则第2次读和第1次读的数据不同 */
+    while ((timeout != 0x00) && (status != NOR_SUCCESS))
+    {
+        timeout--;
+
+        /* Read DQ6 */
+        val1 = *(__IO uint16_t *)(NOR_FLASH_ADDR);
+        val2 = *(__IO uint16_t *)(NOR_FLASH_ADDR);
+
+        /* If DQ6 did not toggle between the two reads then return NOR_Success */
+        if ((val1 & 0x0040) == (val2 & 0x0040))
+        {
+            return NOR_SUCCESS;
+        }
+
+        /* Read DQ2 */
+        if ((val1 & 0x0020) != 0x0020)
+        {
+            status = NOR_ONGOING;
+        }
+
+        val1 = *(__IO uint16_t *)(NOR_FLASH_ADDR);
+        val2 = *(__IO uint16_t *)(NOR_FLASH_ADDR);
+
+        if ((val1 & 0x0040) == (val2 & 0x0040))
+        {
+            return NOR_SUCCESS;
+        }
+        else if ((val1 & 0x0020) == 0x0020)
+        {
+            status = NOR_ERROR;
+            NOR_QuitToReadStatus();
+        }
+    }
+
+    if (timeout == 0x00)
+    {
+        status = NOR_TIMEOUT;
+        // NOR_QuitToReadStatus();
+    }
+
+    /* 返回操作状态 */
+    return (status);
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: NOR_EraseSector
+*	功能说明: 擦除NOR Flash指定的扇区
+*	形    参: 扇区地址
+*	返 回 值: NOR_SUCCESS, NOR_ERROR, NOR_TIMEOUT
+*********************************************************************************************************
+*/
+uint8_t NOR_EraseSector(uint32_t _uiBlockAddr)
+{
+    NOR_WRITE(ADDR_SHIFT(0x0555), 0x00AA);
+    NOR_WRITE(ADDR_SHIFT(0x02AA), 0x0055);
+    NOR_WRITE(ADDR_SHIFT(0x0555), 0x0080);
+    NOR_WRITE(ADDR_SHIFT(0x0555), 0x00AA);
+    NOR_WRITE(ADDR_SHIFT(0x02AA), 0x0055);
+    NOR_WRITE((NOR_FLASH_ADDR + _uiBlockAddr), 0x30);
+
+    return (NOR_GetStatus(BlockErase_Timeout));
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: NOR_ReadByte
+*	功能说明: 读取单字节数据
+*	形    参: 	_uiWriteAddr : 偏移地址[0, 16*1024*1024 - 2]; 编程地址可以为偶数,也可以为奇数。
+*	返 回 值: 读取到的数据
+*********************************************************************************************************
+*/
+uint8_t NOR_ReadByte(uint32_t _uiWriteAddr)
+{
+    uint16_t usHalfWord;
+
+    if (_uiWriteAddr % 2) /* 奇数地址 */
+    {
+        usHalfWord = *(uint16_t *)(NOR_FLASH_ADDR + _uiWriteAddr - 1);
+        return (usHalfWord >> 8); /* 取高8Bit */
+    }
+    else /* 偶数地址 */
+    {
+        usHalfWord = *(uint16_t *)(NOR_FLASH_ADDR + _uiWriteAddr);
+        return usHalfWord; /* 取低8Bit */
+    }
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: NOR_ReadBuffer
+*	功能说明: 连续读取NOR Flash
+*	形    参: 	_pBuf : 字节型数据缓冲区,用于存放读出的数据
+*				_uiWriteAddr : 偏移地址[0, 16*1024*1024 - 2]; 编程地址可以为偶数,也可以为奇数。
+*				_uiBytes : 字节大小
+*	返 回 值: 读取到的数据
+*********************************************************************************************************
+*/
+void NOR_ReadBuffer(uint8_t *_pBuf, uint32_t _uiWriteAddr, uint32_t _uiBytes)
+{
+    uint16_t  usHalfWord;
+    uint16_t *pNor16;
+    uint32_t  i;
+    uint32_t  uiNum;
+
+    uiNum = _uiBytes;
+    /* 处理首字节 */
+    if (_uiWriteAddr % 2) /* 奇数地址 */
+    {
+        usHalfWord = *(uint16_t *)(NOR_FLASH_ADDR + _uiWriteAddr - 1);
+        *_pBuf++   = (usHalfWord >> 8); /* 取高8Bit */
+        uiNum--;
+        _uiWriteAddr++; /* 变为偶数 */
+    }
+
+    /* 按照双字节模式连续读取NOR数据至缓冲区_pBuf */
+    pNor16 = (uint16_t *)(NOR_FLASH_ADDR + _uiWriteAddr);
+    for (i = 0; i < uiNum / 2; i++)
+    {
+        usHalfWord = *pNor16++;
+        *_pBuf++   = usHalfWord;
+        *_pBuf++   = usHalfWord >> 8;
+        uiNum -= 2;
+    }
+
+    /* 处理最后1个字节 */
+    if (uiNum == 1)
+    {
+        *_pBuf++ = *pNor16;
+    }
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: NOR_WriteHalfWord
+*	功能说明: 半字编程. 编程前执行解锁命令序列。编程完毕后,自动退到读取模式。半字编程可以是随机地址。
+*				编程前需要保证存储单元是全0xFF状态。可以重复编程相同的数据。
+*	形    参: 	_uiWriteAddr : 偏移地址[0, 16*1024*1024 - 2]; 编程地址必须为偶数
+*				_usData      : 数据 16Bit
+*
+*	返 回 值: NOR_SUCCESS, NOR_ERROR, NOR_TIMEOUT
+*********************************************************************************************************
+*/
+uint8_t NOR_WriteHalfWord(uint32_t _uiWriteAddr, uint16_t _usData)
+{
+    NOR_WRITE(ADDR_SHIFT(0x0555), 0x00AA);
+    NOR_WRITE(ADDR_SHIFT(0x02AA), 0x0055);
+    NOR_WRITE(ADDR_SHIFT(0x0555), 0x00A0);
+    NOR_WRITE(NOR_FLASH_ADDR + _uiWriteAddr, _usData);
+
+    return (NOR_GetStatus(Program_Timeout));
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: NOR_WriteByte
+*	功能说明: 字节编程. 编程前需要保证存储单元是全0xFF状态。可以重复编程相同的数据。
+*	形    参: 	_uiWriteAddr : 偏移地址[0, 16*1024*1024 - 1]; 编程地址可以为奇数也可以为偶数
+*				_usData      : 数据 16Bit
+*
+*	返 回 值: NOR_SUCCESS, NOR_ERROR, NOR_TIMEOUT
+*********************************************************************************************************
+*/
+uint8_t NOR_WriteByte(uint32_t _uiWriteAddr, uint8_t _ucByte)
+{
+    uint16_t usHalfWord;
+
+    if (_uiWriteAddr % 2) /* 奇数地址 */
+    {
+        /* 读出2字节数据,然后改写高字节,维持以前的低字节数据不变 */
+        usHalfWord = *(uint16_t *)(NOR_FLASH_ADDR + _uiWriteAddr - 1);
+        usHalfWord &= 0x00FF;
+        usHalfWord |= (_ucByte << 8);
+    }
+    else
+    {
+        /* 读取NOR原始数据,保留高字节 */
+        usHalfWord = *(uint16_t *)(NOR_FLASH_ADDR + _uiWriteAddr);
+        usHalfWord &= 0xFF00;
+        usHalfWord |= _ucByte;
+    }
+    return NOR_WriteHalfWord(_uiWriteAddr, usHalfWord);
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: NOR_WriteInPage.
+*	功能说明: 页面内编程(64字节一个页面). 编程前需要保证存储单元是全0xFF状态。可以重复编程相同的数据。
+*	形    参: 	pBuffer : 数据存放在此缓冲区
+*				_uiWriteAddr : 偏移地址, 必须是偶数开始
+*				_usNumHalfword      : 数据格式,双字节为1个单位. 值域: 1-32
+*
+*	返 回 值: NOR_SUCCESS, NOR_ERROR, NOR_TIMEOUT
+*********************************************************************************************************
+*/
+uint8_t NOR_WriteInPage(uint16_t *pBuffer, uint32_t _uiWriteAddr, uint16_t _usNumHalfword)
+{
+    uint32_t lastloadedaddress;
+    uint32_t currentaddress;
+    uint32_t endaddress;
+
+    /* pdf 表7.7 写入缓冲器编程
+
+        写入缓冲器编程允许系统在一个编程操作中写入最多32 个字。与标准的“ 字” 编程算法相比,这可以有效地
+        加快字编程速度。
+    */
+
+    if (_usNumHalfword > 32)
+    {
+        return NOR_ERROR;
+    }
+
+    if ((_uiWriteAddr % 2) != 0)
+    {
+        return NOR_ERROR;
+    }
+
+    _uiWriteAddr = _uiWriteAddr / 2;
+
+    currentaddress    = _uiWriteAddr;
+    endaddress        = _uiWriteAddr + _usNumHalfword - 1;
+    lastloadedaddress = _uiWriteAddr;
+
+    /* 解锁命令序列 */
+    NOR_WRITE(ADDR_SHIFT(0x00555), 0x00AA);
+    NOR_WRITE(ADDR_SHIFT(0x02AA), 0x0055);
+
+    /* Write Write Buffer Load Command */
+    NOR_WRITE(ADDR_SHIFT(_uiWriteAddr), 0x0025);
+    NOR_WRITE(ADDR_SHIFT(_uiWriteAddr), (_usNumHalfword - 1));
+
+    /*  Load Data into NOR Buffer */
+    while (currentaddress <= endaddress)
+    {
+        /* Store last loaded address & data value (for polling) */
+        lastloadedaddress = currentaddress;
+
+        NOR_WRITE(ADDR_SHIFT(currentaddress), *pBuffer++);
+        currentaddress += 1;
+    }
+
+    NOR_WRITE(ADDR_SHIFT(lastloadedaddress), 0x29);
+
+    return (NOR_GetStatus(Program_Timeout));
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: NOR_WriteBuffer
+*	功能说明: 连续编程操作。采取半字编程模式。
+*			  S29GL 支持64字节页面大小的连续编程。本函数暂时不支持页面编程。
+*	形    参: _pBuf : 8位数据缓冲区
+*			 _uiWriteAddr : 写入的存储单元首地址, 必须为偶数
+*			 _uiBytes : 字节个数
+*	返 回 值: NOR_SUCCESS, NOR_ERROR, NOR_TIMEOUT
+*********************************************************************************************************
+*/
+uint8_t NOR_WriteBuffer(uint8_t *_pBuf, uint32_t _uiWriteAddr, uint32_t _uiBytes)
+{
+    uint16_t usHalfWord;
+    uint32_t i;
+    uint32_t uiNum;
+    uint8_t  ucStatus;
+
+    uiNum = _uiBytes;
+    /* 处理首字节 */
+    if (_uiWriteAddr % 2) /* 奇数地址 */
+    {
+        /* 读出2字节数据,然后改写高字节,维持以前的低字节数据不变 */
+        usHalfWord = *(uint16_t *)(NOR_FLASH_ADDR + _uiWriteAddr - 1);
+        usHalfWord &= 0x00FF;
+        usHalfWord |= ((*_pBuf++) << 8);
+
+        ucStatus = NOR_WriteHalfWord(_uiWriteAddr - 1, usHalfWord);
+        if (ucStatus != NOR_SUCCESS)
+        {
+            goto err_quit;
+        }
+
+        uiNum--;
+        _uiWriteAddr++; /* 变为偶数 */
+    }
+
+    /* 按照双字节模式连续编程NOR数据 */
+    for (i = 0; i < uiNum / 2; i++)
+    {
+        usHalfWord = *_pBuf++;
+        usHalfWord |= ((*_pBuf++) << 8);
+
+        ucStatus = NOR_WriteHalfWord(_uiWriteAddr, usHalfWord);
+        if (ucStatus != NOR_SUCCESS)
+        {
+            goto err_quit;
+        }
+
+        _uiWriteAddr += 2;
+    }
+
+    /* 处理最后1个字节 */
+    if (uiNum % 2)
+    {
+        /* 读取NOR原始数据,保留高字节 */
+        usHalfWord = *(uint16_t *)(NOR_FLASH_ADDR + _uiWriteAddr);
+        usHalfWord &= 0xFF00;
+        usHalfWord |= (*_pBuf++);
+
+        ucStatus = NOR_WriteHalfWord(_uiWriteAddr, usHalfWord);
+        if (ucStatus != NOR_SUCCESS)
+        {
+            goto err_quit;
+        }
+    }
+    ucStatus = NOR_SUCCESS;
+err_quit:
+    return ucStatus;
+}
+
+/***************************** 安富莱电子 www.armfly.com (END OF FILE) *********************************/

+ 75 - 0
User/Bsp/nor_flash/nor_flash.h

@@ -0,0 +1,75 @@
+/*
+*********************************************************************************************************
+*
+*	模块名称 : NOR Flash驱动模块
+*	文件名称 : bsp_nor_flash.h
+*	版    本 : V1.0
+*	说    明 : 头文件
+*
+*	修改记录 :
+*		版本号  日期       作者    说明
+*		v1.0    2012-10-12 armfly  ST固件库版本 V2.1.0
+*
+*	Copyright (C), 2012-2013, 安富莱电子 www.armfly.com
+*
+*********************************************************************************************************
+*/
+
+#ifndef _NOR_FLASH_H
+#define _NOR_FLASH_H
+
+#include "includes.h"
+
+/*
+    安富莱STM32-V5开发板 NOR Flash 型号 S29GL128P10TFI01
+    容量16M字节,16Bit,100ns速度
+    物理地址 : 0x6400 0000
+*/
+
+#define NOR_FLASH_ADDR ((uint32_t)0x64000000)
+
+#define NOR_SECTOR_SIZE  (128 * 1024) /* 扇区大小 */
+#define NOR_SECTOR_COUNT 128          /* 扇区个数 */
+#define NOR_FLASH_SIZE   (NOR_SECTOR_SIZE * NOR_SECTOR_COUNT)
+
+/*
+    制造商ID:Spansion   0x01
+
+    S29GL01GP	01 7E 28 01		1 Gigabit		128M字节
+    S29GL512P	01 7E 23 01		512 Megabit		64M字节
+    S29GL256P	01 7E 22 01		256 Megabit		32M字节
+    S29GL128P	01 7E 21 01		128 Megabit		16M字节
+*/
+typedef enum
+{
+    S29GL128P = 0x017E2101,
+    S29GL256P = 0x017E2201,
+    S29GL512P = 0x017E2301
+} NOR_CHIP_ID;
+
+/* NOR Status */
+typedef enum
+{
+    NOR_SUCCESS = 0,
+    NOR_ONGOING = 1,
+    NOR_ERROR   = 2,
+    NOR_TIMEOUT = 3
+} NOR_STATUS;
+
+void     nor_flash_init(void);
+uint32_t NOR_ReadID(void);
+uint8_t  NOR_EraseChip(void);
+uint8_t  NOR_EraseSector(uint32_t _uiBlockAddr);
+uint8_t  NOR_ReadByte(uint32_t _uiWriteAddr);
+void     NOR_ReadBuffer(uint8_t *_pBuf, uint32_t _uiWriteAddr, uint32_t _uiBytes);
+uint8_t  NOR_WriteHalfWord(uint32_t _uiWriteAddr, uint16_t _usData);
+uint8_t  NOR_WriteByte(uint32_t _uiWriteAddr, uint8_t _ucByte);
+uint8_t  NOR_WriteInPage(uint16_t *pBuffer, uint32_t _uiWriteAddr, uint16_t _usNumHalfword);
+uint8_t  NOR_WriteBuffer(uint8_t *_pBuf, uint32_t _uiWriteAddr, uint32_t _uiBytes);
+
+void    NOR_StartEraseChip(void);
+uint8_t NOR_CheckStatus(void);
+
+#endif
+
+/***************************** 安富莱电子 www.armfly.com (END OF FILE) *********************************/

+ 0 - 6
User/app/iec104/PRIO_QUEUE_Iec10x.h

@@ -1,6 +0,0 @@
-#ifndef __IEC10X_PRIO_QUEUE_H__
-#define __IEC10X_PRIO_QUEUE_H__
-
-#include "iec10x.h"
-
-#endif

+ 0 - 25
User/app/iec104/common_datatype.h

@@ -1,25 +0,0 @@
-// /* There is some amount of overlap with <sys/types.h> as known by inet code */
-// #ifndef __int8_t_defined
-// #define __int8_t_defined
-// typedef signed char int8_t;
-// typedef short int   int16_t;
-// typedef int         int32_t;
-// #if __WORDSIZE == 64
-// typedef long int int64_t;
-// #else
-// __extension__ typedef long long int int64_t;
-// #endif
-// #endif
-
-// /* Unsigned.  */
-// typedef unsigned char      uint8_t;
-// typedef unsigned short int uint16_t;
-// #ifndef __uint32_t_defined
-// typedef unsigned int uint32_t;
-// #define __uint32_t_defined
-// #endif
-// #if __WORDSIZE == 64
-// typedef unsigned long int uint64_t;
-// #else
-// __extension__ typedef unsigned long long int uint64_t;
-// #endif

+ 0 - 0
User/app/iec104/hxcp.c


+ 74 - 72
User/app/iec104/iec101.c

@@ -1,32 +1,34 @@
 
 
 #include "iec101.h"
-#include "project_var.h"
+#include "iec10x_conf.h"
+#include "iec10x_type.h"
+
 #ifdef IEC101_STM32
 /*
  * GLOABLE VARIALBLE
  */
-uint8_t Iec101_Respon_Confirm = 0;
-uint8_t Iec101_Sendbuf[IEC101_MAX_BUF_LEN];
+iec_8u Iec101_Respon_Confirm = 0;
+iec_8u Iec101_Sendbuf[IEC101_MAX_BUF_LEN];
 
-uint8_t  IEC10X_Call_AllQoi   = 0;
-uint8_t  IEC10X_Call_GroupQoi = 0;
-uint16_t IEC101_Pulse_Cnt     = 0;
+iec_8u  IEC10X_Call_AllQoi   = 0;
+iec_8u  IEC10X_Call_GroupQoi = 0;
+iec_16u IEC101_Pulse_Cnt     = 0;
 
 /*
  * STATE
  * */
-uint8_t IEC101_STATE_FLAG_INIT    = IEC101_FLAG_LINK_CLOSED;
-uint8_t IEC101_STATE_FLAG_CALLALL = IEC101_FLAG_LINK_CLOSED;
-uint8_t IEC101_STATE_FLAG_GROUP   = IEC101_FLAG_LINK_CLOSED;
-uint8_t IEC101_STATE_FLAG_CLOCK   = IEC101_FLAG_LINK_CLOSED;
-uint8_t IEC101_STATE_FLAG_DELAY   = IEC101_FLAG_LINK_CLOSED;
-uint8_t IEC101_STATE_FLAG_PULSE   = IEC101_FLAG_LINK_CLOSED;
-
-uint8_t IEC10X_RetStatusOk(uint16_t addr)
+iec_8u IEC101_STATE_FLAG_INIT    = IEC101_FLAG_LINK_CLOSED;
+iec_8u IEC101_STATE_FLAG_CALLALL = IEC101_FLAG_LINK_CLOSED;
+iec_8u IEC101_STATE_FLAG_GROUP   = IEC101_FLAG_LINK_CLOSED;
+iec_8u IEC101_STATE_FLAG_CLOCK   = IEC101_FLAG_LINK_CLOSED;
+iec_8u IEC101_STATE_FLAG_DELAY   = IEC101_FLAG_LINK_CLOSED;
+iec_8u IEC101_STATE_FLAG_PULSE   = IEC101_FLAG_LINK_CLOSED;
+
+iec_8u IEC10X_RetStatusOk(iec_16u addr)
 {
 
-    uint16_t     len;
+    iec_16u      len;
     PIEC101_10_T Iec10x = (PIEC101_10_T)Iec101_Sendbuf;
 
     len = IEC101_STABLE_LEN;
@@ -49,10 +51,10 @@ uint8_t IEC10X_RetStatusOk(uint16_t addr)
 
     return RET_SUCESS;
 }
-static uint8_t IEC10X_ResConfirm(uint8_t Prio)
+static iec_8u IEC10X_ResConfirm(iec_8u Prio)
 {
 
-    uint16_t len;
+    iec_16u len;
 
     PIEC101_10_T Iec10x = (PIEC101_10_T)Iec101_Sendbuf;
 
@@ -76,10 +78,10 @@ static uint8_t IEC10X_ResConfirm(uint8_t Prio)
 
     return RET_SUCESS;
 }
-uint8_t IEC10X_ReqLinkStatus()
+iec_8u IEC10X_ReqLinkStatus()
 {
 
-    uint16_t len;
+    iec_16u len;
 
     PIEC101_10_T Iec10x = (PIEC101_10_T)Iec101_Sendbuf;
 
@@ -107,10 +109,10 @@ uint8_t IEC10X_ReqLinkStatus()
     return RET_SUCESS;
 }
 
-uint8_t IEC10X_ResetLink(void)
+iec_8u IEC10X_ResetLink(void)
 {
 
-    uint16_t len;
+    iec_16u len;
 
     PIEC101_10_T Iec10x = (PIEC101_10_T)Iec101_Sendbuf;
 
@@ -136,11 +138,11 @@ uint8_t IEC10X_ResetLink(void)
     return RET_SUCESS;
 }
 
-uint8_t IEC101_BuildFinInit(void)
+iec_8u IEC101_BuildFinInit(void)
 {
 
-    uint16_t len     = 0;
-    uint8_t  cs_temp = 0, i;
+    iec_16u len     = 0;
+    iec_8u  cs_temp = 0, i;
 
     PIEC101_68_T   Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
     PIEC10X_ASDU_T asdu   = (PIEC10X_ASDU_T)(Iec10x->_asdu);
@@ -187,11 +189,11 @@ uint8_t IEC101_BuildFinInit(void)
 
     return RET_SUCESS;
 }
-uint8_t IEC101_BuildActConfirm(uint8_t qoi, uint8_t Prio)
+iec_8u IEC101_BuildActConfirm(iec_8u qoi, iec_8u Prio)
 {
 
-    uint16_t len     = 0;
-    uint8_t  cs_temp = 0, i;
+    iec_16u len     = 0;
+    iec_8u  cs_temp = 0, i;
 
     PIEC101_68_T   Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
     PIEC10X_ASDU_T asdu   = (PIEC10X_ASDU_T)(Iec10x->_asdu);
@@ -241,11 +243,11 @@ uint8_t IEC101_BuildActConfirm(uint8_t qoi, uint8_t Prio)
     return RET_SUCESS;
 }
 
-uint8_t IEC101_BuildActFinish(uint8_t qoi, uint8_t Prio)
+iec_8u IEC101_BuildActFinish(iec_8u qoi, iec_8u Prio)
 {
 
-    uint16_t len     = 0;
-    uint8_t  cs_temp = 0, i;
+    iec_16u len     = 0;
+    iec_8u  cs_temp = 0, i;
 
     PIEC101_68_T   Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
     PIEC10X_ASDU_T asdu   = (PIEC10X_ASDU_T)(Iec10x->_asdu);
@@ -296,11 +298,11 @@ uint8_t IEC101_BuildActFinish(uint8_t qoi, uint8_t Prio)
     return RET_SUCESS;
 }
 
-uint8_t IEC101_BuildSignal(uint8_t reason, uint8_t Prio)
+iec_8u IEC101_BuildSignal(iec_8u reason, iec_8u Prio)
 {
 
-    uint16_t len     = 0, addr;
-    uint8_t  cs_temp = 0, i, asdu_num = 0, *ptr = NULL, signal, sum;
+    iec_16u len     = 0, addr;
+    iec_8u  cs_temp = 0, i, asdu_num = 0, *ptr = NULL, signal, sum;
 
     /*init struct*/
     PIEC101_68_T   Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
@@ -364,11 +366,11 @@ uint8_t IEC101_BuildSignal(uint8_t reason, uint8_t Prio)
     return RET_SUCESS;
 }
 
-uint8_t IEC101_BuildDetect(uint8_t reason, uint8_t type, uint8_t Prio)
+iec_8u IEC101_BuildDetect(iec_8u reason, iec_8u type, iec_8u Prio)
 {
 
-    uint16_t           len     = 0, addr;
-    uint8_t            cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
+    iec_16u            len     = 0, addr;
+    iec_8u             cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
     PIEC10X_DETECT_T   detect   = NULL;
     PIEC10X_DETECT_F_T detect_f = NULL;
 
@@ -448,11 +450,11 @@ uint8_t IEC101_BuildDetect(uint8_t reason, uint8_t type, uint8_t Prio)
     return RET_SUCESS;
 }
 
-uint8_t IEC101_BuildSignal_Spont(uint8_t TimeFlag, uint8_t signalV, uint16_t addrV)
+iec_8u IEC101_BuildSignal_Spont(iec_8u TimeFlag, iec_8u signalV, iec_16u addrV)
 {
 
-    uint16_t      len     = 0;
-    uint8_t       cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
+    iec_16u       len     = 0;
+    iec_8u        cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
     PCP56Time2a_T time = NULL;
 
     /*init struct*/
@@ -529,11 +531,11 @@ uint8_t IEC101_BuildSignal_Spont(uint8_t TimeFlag, uint8_t signalV, uint16_t add
 
     return RET_SUCESS;
 }
-uint8_t IEC101_BuildDetect_Spont(uint8_t TimeFlag, PIEC10X_DETECT_T detectV, uint16_t addrV)
+iec_8u IEC101_BuildDetect_Spont(iec_8u TimeFlag, PIEC10X_DETECT_T detectV, iec_16u addrV)
 {
 
-    uint16_t         len     = 0;
-    uint8_t          cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
+    iec_16u          len     = 0;
+    iec_8u           cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
     PIEC10X_DETECT_T detect = NULL;
     PCP56Time2a_T    time   = NULL;
 
@@ -611,11 +613,11 @@ uint8_t IEC101_BuildDetect_Spont(uint8_t TimeFlag, PIEC10X_DETECT_T detectV, uin
 
     return RET_SUCESS;
 }
-uint8_t IEC101_BuildDetectF_Spont(uint8_t TimeFlag, float detectV, uint16_t addrV)
+iec_8u IEC101_BuildDetectF_Spont(iec_8u TimeFlag, float detectV, iec_16u addrV)
 {
 
-    uint16_t           len     = 0;
-    uint8_t            cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
+    iec_16u            len     = 0;
+    iec_8u             cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
     PIEC10X_DETECT_F_T detect = NULL;
     PCP56Time2a_T      time   = NULL;
 
@@ -693,12 +695,12 @@ uint8_t IEC101_BuildDetectF_Spont(uint8_t TimeFlag, float detectV, uint16_t addr
 
     return RET_SUCESS;
 }
-uint8_t IEC101_BuildDelayAct(uint16_t delay_time)
+iec_8u IEC101_BuildDelayAct(iec_16u delay_time)
 {
 
-    uint16_t len     = 0;
-    uint8_t  cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
-    uint16_t cp16time2a = delay_time;
+    iec_16u len     = 0;
+    iec_8u  cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
+    iec_16u cp16time2a = delay_time;
 
     /*init struct*/
     PIEC101_68_T   Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
@@ -734,8 +736,8 @@ uint8_t IEC101_BuildDelayAct(uint16_t delay_time)
     info->_addr = IEC10X_INFO_ADDR_NONE;
 
     /*delay value*/
-    ptr              = info->_element;
-    *(uint16_t *)ptr = IEC10X_Cp16time2a;
+    ptr             = info->_element;
+    *(iec_16u *)ptr = IEC10X_Cp16time2a;
     ptr += 2;
 
     /*len*/
@@ -756,11 +758,11 @@ uint8_t IEC101_BuildDelayAct(uint16_t delay_time)
 
     return RET_SUCESS;
 }
-uint8_t IEC101_BuildClockAct()
+iec_8u IEC101_BuildClockAct()
 {
 
-    uint16_t len     = 0;
-    uint8_t  cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
+    iec_16u len     = 0;
+    iec_8u  cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
 
     /*init struct*/
     PIEC101_68_T   Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
@@ -820,7 +822,7 @@ uint8_t IEC101_BuildClockAct()
 
     return RET_SUCESS;
 }
-IEC10X_ASDU_CALL_Qoi(uint8_t qoi)
+IEC10X_ASDU_CALL_Qoi(iec_8u qoi)
 {
 
     switch (qoi)
@@ -856,12 +858,12 @@ IEC10X_ASDU_CALL_Qoi(uint8_t qoi)
     }
 }
 
-uint8_t IEC10X_ASDU_Call(PIEC10X_ASDU_T Iec10x_Asdu)
+iec_8u IEC10X_ASDU_Call(PIEC10X_ASDU_T Iec10x_Asdu)
 {
 
     PASDU_INFO_T asdu_info = (PASDU_INFO_T)(Iec10x_Asdu->_info);
-    uint8_t      qoi       = asdu_info->_element[0];
-    uint8_t      Prio      = 0;
+    iec_8u       qoi       = asdu_info->_element[0];
+    iec_8u       Prio      = 0;
 
     if (asdu_info->_addr != 0)
     {
@@ -903,7 +905,7 @@ uint8_t IEC10X_ASDU_Call(PIEC10X_ASDU_T Iec10x_Asdu)
     return RET_SUCESS;
 }
 
-uint8_t IEC10X_ASDU_Delay(PIEC10X_ASDU_T Iec10x_Asdu)
+iec_8u IEC10X_ASDU_Delay(PIEC10X_ASDU_T Iec10x_Asdu)
 {
 
     PASDU_INFO_T asdu_info = (PASDU_INFO_T)(Iec10x_Asdu->_info);
@@ -918,14 +920,14 @@ uint8_t IEC10X_ASDU_Delay(PIEC10X_ASDU_T Iec10x_Asdu)
     {
 
     case IEC10X_COT_ACT:
-        IEC10X_Cp16time2a = *(uint16_t *)(asdu_info->_element);
+        IEC10X_Cp16time2a = *(iec_16u *)(asdu_info->_element);
         LOG("-%s- delay cmd (0x%x%x)(%d)ms \n", __FUNCTION__, asdu_info->_element[0], asdu_info->_element[1], IEC10X_Cp16time2a);
         IEC101_STATE_FLAG_DELAY = IEC101_FLAG_DELAY_ACT;
         IEC10X_ResConfirm(IEC10X_PRIO_DELAY);
         IEC101_BuildDelayAct(IEC10X_Cp16time2a);
         break;
     case IEC10X_COT_SPONT:
-        IEC10X_Cp16time2a_V = *(uint16_t *)(asdu_info->_element);
+        IEC10X_Cp16time2a_V = *(iec_16u *)(asdu_info->_element);
         LOG("-%s- delay cmd delay value(%d)ms \n", __FUNCTION__, IEC10X_Cp16time2a_V);
         IEC10X_ResConfirm(IEC10X_PRIO_DELAY);
         break;
@@ -936,7 +938,7 @@ uint8_t IEC10X_ASDU_Delay(PIEC10X_ASDU_T Iec10x_Asdu)
     return RET_SUCESS;
 }
 
-uint8_t IEC10X_ASDU_CLOCK(PIEC10X_ASDU_T Iec10x_Asdu)
+iec_8u IEC10X_ASDU_CLOCK(PIEC10X_ASDU_T Iec10x_Asdu)
 {
 
     PASDU_INFO_T  asdu_info = (PASDU_INFO_T)(Iec10x_Asdu->_info);
@@ -974,11 +976,11 @@ uint8_t IEC10X_ASDU_CLOCK(PIEC10X_ASDU_T Iec10x_Asdu)
     return RET_SUCESS;
 }
 
-uint8_t Iec10x_Deal_10(uint8_t *buf, uint16_t len)
+iec_8u Iec10x_Deal_10(iec_8u *buf, iec_16u len)
 {
 
-    uint8_t      cfun, sta, i;
-    uint8_t      cs_temp   = 0;
+    iec_8u       cfun, sta, i;
+    iec_8u       cs_temp   = 0;
     PIEC101_10_T Iec10x_10 = NULL;
 
     Iec10x_10 = (PIEC101_10_T)buf;
@@ -1063,11 +1065,11 @@ uint8_t Iec10x_Deal_10(uint8_t *buf, uint16_t len)
     return RET_SUCESS;
 }
 
-uint8_t Iec10x_Deal_68(uint8_t *buf, uint16_t len)
+iec_8u Iec10x_Deal_68(iec_8u *buf, iec_16u len)
 {
 
-    uint8_t        cfun, sta, i;
-    uint8_t        cs_temp     = 0;
+    iec_8u         cfun, sta, i;
+    iec_8u         cs_temp     = 0;
     PIEC101_68_T   Iec10x_68   = NULL;
     PIEC10X_ASDU_T Iec10x_Asdu = NULL;
 
@@ -1143,11 +1145,11 @@ uint8_t Iec10x_Deal_68(uint8_t *buf, uint16_t len)
     return RET_SUCESS;
 }
 
-void Iex101_main(uint8_t *buf, uint16_t len)
+void Iex101_main(iec_8u *buf, iec_16u len)
 {
 
-    uint8_t *BufTemp = NULL;
-    int16_t  LenRemain, LenTmp;
+    iec_8u *BufTemp = NULL;
+    iec_16s LenRemain, LenTmp;
 
     if (buf == NULL)
     {
@@ -1198,7 +1200,7 @@ void Iex101_main(uint8_t *buf, uint16_t len)
     return;
 }
 
-uint8_t Iec101_StateMachine(void)
+iec_8u Iec101_StateMachine(void)
 {
 
     /*Init link*/

+ 33 - 32
User/app/iec104/iec101.h

@@ -16,9 +16,10 @@ History      :
 #ifndef _IEC101_H
 #define _IEC101_H
 
-#include "PRIO_QUEUE_Iec10x.h"
 #include "iec10x.h"
-#include "project_var.h"
+#include "iec10x_conf.h"
+#include "iec10x_prio_queue.h"
+#include "iec10x_type.h"
 #include <stdio.h>
 #include <string.h>
 
@@ -95,22 +96,22 @@ extern "C"
     typedef __packed struct
     {
 
-        uint8_t _func : 4; /* function */
-        uint8_t _fcv  : 1;
-        uint8_t _fcb  : 1; /* Frame calculate bit */
-        uint8_t _prm  : 1; /* 1:from start station, 0:from end station */
-        uint8_t _dir  : 1;
+        iec_8u _func : 4; /* function */
+        iec_8u _fcv  : 1;
+        iec_8u _fcb  : 1; /* Frame calculate bit */
+        iec_8u _prm  : 1; /* 1:from start station, 0:from end station */
+        iec_8u _dir  : 1;
 
     } CTRL_DOWN_T;
 
     typedef __packed struct
     {
 
-        uint8_t _func : 4; /*function*/
-        uint8_t _dfc  : 1;
-        uint8_t _acd  : 1;
-        uint8_t _prm  : 1;
-        uint8_t _dir  : 1;
+        iec_8u _func : 4; /*function*/
+        iec_8u _dfc  : 1;
+        iec_8u _acd  : 1;
+        iec_8u _prm  : 1;
+        iec_8u _dir  : 1;
 
     } CTRL_UP_T;
 
@@ -118,7 +119,7 @@ extern "C"
     {
         CTRL_UP_T   up;
         CTRL_DOWN_T down;
-        uint8_t     val;
+        iec_8u      val;
     } CTRL_T, *PCTRL_T;
 
     /*
@@ -126,11 +127,11 @@ extern "C"
      * */
     typedef __packed struct
     {
-        uint8_t  _begin;
-        CTRL_T   _ctrl;
-        uint16_t _addr;
-        uint8_t  _cs;
-        uint8_t  _end;
+        iec_8u  _begin;
+        CTRL_T  _ctrl;
+        iec_16u _addr;
+        iec_8u  _cs;
+        iec_8u  _end;
     } IEC101_10_T, *PIEC101_10_T;
 
     /*
@@ -138,13 +139,13 @@ extern "C"
      * */
     typedef __packed struct
     {
-        uint8_t  _begin;
-        uint8_t  _len;
-        uint8_t  _len_cfm;
-        uint8_t  _begin_cfm;
-        CTRL_T   _ctrl;
-        uint16_t _addr;
-        uint8_t  _asdu[1];
+        iec_8u  _begin;
+        iec_8u  _len;
+        iec_8u  _len_cfm;
+        iec_8u  _begin_cfm;
+        CTRL_T  _ctrl;
+        iec_16u _addr;
+        iec_8u  _asdu[1];
     } IEC101_68_T, *PIEC101_68_T;
 
 #pragma pack()
@@ -176,20 +177,20 @@ extern "C"
     *                                           GLOABLE VARIABLE
     *********************************************************************************************************
     */
-    extern uint16_t IEC101_Pulse_Cnt;
-    extern uint8_t  IEC101_STATE_FLAG_INIT;
+    extern iec_16u IEC101_Pulse_Cnt;
+    extern iec_8u  IEC101_STATE_FLAG_INIT;
     /*
     *********************************************************************************************************
     *                                          FUNCTION PROTOTYPES
     *********************************************************************************************************
     */
 
-    void    Iex101_main(uint8_t *buf, uint16_t len);
-    uint8_t Iec101_StateMachine(void);
+    void   Iex101_main(iec_8u *buf, iec_16u len);
+    iec_8u Iec101_StateMachine(void);
 
-    uint8_t IEC101_BuildSignal_Spont(uint8_t TimeFlag, uint8_t signalV, uint16_t addrV);
-    uint8_t IEC101_BuildDetect_Spont(uint8_t TimeFlag, PIEC10X_DETECT_T detectV, uint16_t addrV);
-    uint8_t IEC101_BuildDetectF_Spont(uint8_t TimeFlag, float detectV, uint16_t addrV);
+    iec_8u IEC101_BuildSignal_Spont(iec_8u TimeFlag, iec_8u signalV, iec_16u addrV);
+    iec_8u IEC101_BuildDetect_Spont(iec_8u TimeFlag, PIEC10X_DETECT_T detectV, iec_16u addrV);
+    iec_8u IEC101_BuildDetectF_Spont(iec_8u TimeFlag, float detectV, iec_16u addrV);
 
 #ifdef __cplusplus
 }

+ 106 - 119
User/app/iec104/iec104.c

@@ -1,64 +1,51 @@
-/*******************************************************************
-Copyright (C):
-File name    :    Iec104.c
-DESCRIPTION  :
-AUTHOR       :
-Version      :    1.0
-Date         :    2014/07/31
-Others       :
-History      :
-++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-1) Date: 2014/07/31         Author: ChenDajie
-   content:
-
-*******************************************************************/
 #include "iec104.h"
-#include "project_var.h"
+#include "iec10x_conf.h"
+#include "iec10x_type.h"
 
 /*
  * GLOABLE VARIALBLE
  */
 
-uint8_t Iec104_Sendbuf[IEC104_MAX_BUF_LEN];
+iec_8u Iec104_Sendbuf[IEC104_MAX_BUF_LEN];
 /*
  * STATE
  * */
-uint8_t IEC104_STATE_FLAG_INIT    = IEC104_FLAG_CLOSED;
-uint8_t IEC104_STATE_FLAG_CALLALL = IEC104_FLAG_CLOSED;
-uint8_t IEC104_STATE_FLAG_GROUP   = IEC104_FLAG_CLOSED;
-uint8_t IEC104_STATE_FLAG_CLOCK   = IEC104_FLAG_CLOSED;
-uint8_t IEC104_STATE_FLAG_TESTER  = IEC104_FLAG_IDLE;
-uint8_t IEC104_STATE_FLAG_S_ACK   = IEC104_FLAG_CLOSED;
+iec_8u IEC104_STATE_FLAG_INIT    = IEC104_FLAG_CLOSED;
+iec_8u IEC104_STATE_FLAG_CALLALL = IEC104_FLAG_CLOSED;
+iec_8u IEC104_STATE_FLAG_GROUP   = IEC104_FLAG_CLOSED;
+iec_8u IEC104_STATE_FLAG_CLOCK   = IEC104_FLAG_CLOSED;
+iec_8u IEC104_STATE_FLAG_TESTER  = IEC104_FLAG_IDLE;
+iec_8u IEC104_STATE_FLAG_S_ACK   = IEC104_FLAG_CLOSED;
 /*
  * receive and send serial number
  * */
-int32_t Iec104_BuildSendSn = 0;
-int32_t Iec104_BuildRecvSn = 0;
-int32_t Iec104_DealSendSn  = -1;
-int32_t Iec104_DealRecvSn  = 0;
+iec_32s Iec104_BuildSendSn = 0;
+iec_32s Iec104_BuildRecvSn = 0;
+iec_32s Iec104_DealSendSn  = -1;
+iec_32s Iec104_DealRecvSn  = 0;
 
-uint8_t IEC104_Call_AllQoi   = 0;
-uint8_t IEC104_Call_GroupQoi = 0;
+iec_8u IEC104_Call_AllQoi   = 0;
+iec_8u IEC104_Call_GroupQoi = 0;
 
 #define IEC104_CYCLE_TIME_MS       100             /*100ms*/
 #define IEC104_RESEND_TIME_MS      (30 * 1000)     /*30s*/
 #define IEC104_S_ACK_TIMEOUT       (5 * 1000)      /*5s*/
 #define IEC104_TESTER_IDLE_TIMEOUT (1 * 30 * 1000) /*2min*/
-uint32_t Iec104_TimeCount            = 0;
-uint32_t Iec104_TimeCycle            = IEC104_RESEND_TIME_MS;
-uint32_t Iec104_TimeCycle_S          = 0;
-uint32_t Iec104_TimeCycle_TesterIdle = 0;
-uint8_t  Iec104_TesterCount          = 0;
+iec_32u Iec104_TimeCount            = 0;
+iec_32u Iec104_TimeCycle            = IEC104_RESEND_TIME_MS;
+iec_32u Iec104_TimeCycle_S          = 0;
+iec_32u Iec104_TimeCycle_TesterIdle = 0;
+iec_8u  Iec104_TesterCount          = 0;
 
-uint32_t Iec10x_Update_SeekAddr = 0;
-uint16_t FirmFlagCount          = 0;
+iec_32u Iec10x_Update_SeekAddr = 0;
+iec_16u FirmFlagCount          = 0;
 
-uint8_t IEC104_UploadAddr(void)
+iec_8u IEC104_UploadAddr(void)
 {
 
-    uint8_t  len    = 0;
-    uint8_t *ptr    = NULL;
-    uint32_t Temp32 = 0;
+    iec_8u  len    = 0;
+    iec_8u *ptr    = NULL;
+    iec_32u Temp32 = 0;
 
     PIEC104_DATA_T Iec104Data = (PIEC104_DATA_T)Iec104_Sendbuf;
     PIEC10X_ASDU_T asdu       = (PIEC10X_ASDU_T)(Iec104Data->Asdu);
@@ -105,12 +92,12 @@ uint8_t IEC104_UploadAddr(void)
     return RET_SUCESS;
 }
 
-uint8_t IEC104_Build_InitFin(void)
+iec_8u IEC104_Build_InitFin(void)
 {
 
-    uint8_t  len    = 0;
-    uint8_t *ptr    = NULL;
-    uint32_t Temp32 = 0;
+    iec_8u  len    = 0;
+    iec_8u *ptr    = NULL;
+    iec_32u Temp32 = 0;
 
     PIEC104_DATA_T Iec104Data = (PIEC104_DATA_T)Iec104_Sendbuf;
     PIEC10X_ASDU_T asdu       = (PIEC10X_ASDU_T)(Iec104Data->Asdu);
@@ -154,12 +141,12 @@ uint8_t IEC104_Build_InitFin(void)
     return RET_SUCESS;
 }
 
-uint8_t IEC104_BuildCallACK(uint8_t qoi, uint8_t Prio)
+iec_8u IEC104_BuildCallACK(iec_8u qoi, iec_8u Prio)
 {
 
-    uint8_t  len    = 0;
-    uint8_t *ptr    = NULL;
-    uint32_t Temp32 = 0;
+    iec_8u  len    = 0;
+    iec_8u *ptr    = NULL;
+    iec_32u Temp32 = 0;
 
     PIEC104_DATA_T Iec104Data = (PIEC104_DATA_T)Iec104_Sendbuf;
     PIEC10X_ASDU_T asdu       = (PIEC10X_ASDU_T)(Iec104Data->Asdu);
@@ -200,12 +187,12 @@ uint8_t IEC104_BuildCallACK(uint8_t qoi, uint8_t Prio)
     return RET_SUCESS;
 }
 
-uint8_t IEC104_BuildSignal_Spon(uint8_t TimeFlag, uint8_t signalV, uint16_t addrV)
+iec_8u IEC104_BuildSignal_Spon(iec_8u TimeFlag, iec_8u signalV, iec_16u addrV)
 {
 
-    uint8_t       len = 0, asdu_num = 0, i;
-    uint8_t      *ptr    = NULL;
-    uint32_t      Temp32 = 0;
+    iec_8u        len = 0, asdu_num = 0, i;
+    iec_8u       *ptr    = NULL;
+    iec_32u       Temp32 = 0;
     PCP56Time2a_T time   = NULL;
 
     PIEC104_DATA_T Iec104Data = (PIEC104_DATA_T)Iec104_Sendbuf;
@@ -269,12 +256,12 @@ uint8_t IEC104_BuildSignal_Spon(uint8_t TimeFlag, uint8_t signalV, uint16_t addr
 
     return RET_SUCESS;
 }
-uint8_t IEC104_BuildDetect_Spont(uint8_t TimeFlag, PIEC10X_DETECT_T detectV, uint16_t addrV)
+iec_8u IEC104_BuildDetect_Spont(iec_8u TimeFlag, PIEC10X_DETECT_T detectV, iec_16u addrV)
 {
 
-    uint8_t          len = 0, asdu_num = 0;
-    uint8_t         *ptr    = NULL;
-    uint32_t         Temp32 = 0;
+    iec_8u           len = 0, asdu_num = 0;
+    iec_8u          *ptr    = NULL;
+    iec_32u          Temp32 = 0;
     PIEC10X_DETECT_T detect = NULL;
     PCP56Time2a_T    time   = NULL;
 
@@ -338,12 +325,12 @@ uint8_t IEC104_BuildDetect_Spont(uint8_t TimeFlag, PIEC10X_DETECT_T detectV, uin
     return RET_SUCESS;
 }
 
-uint8_t IEC104_BuildDetectF_Spont(uint8_t TimeFlag, float detectV, uint16_t addrV)
+iec_8u IEC104_BuildDetectF_Spont(iec_8u TimeFlag, float detectV, iec_16u addrV)
 {
 
-    uint8_t            len = 0, asdu_num = 0;
-    uint8_t           *ptr    = NULL;
-    uint32_t           Temp32 = 0;
+    iec_8u             len = 0, asdu_num = 0;
+    iec_8u            *ptr    = NULL;
+    iec_32u            Temp32 = 0;
     PIEC10X_DETECT_F_T detect = NULL;
     PCP56Time2a_T      time   = NULL;
 
@@ -406,13 +393,13 @@ uint8_t IEC104_BuildDetectF_Spont(uint8_t TimeFlag, float detectV, uint16_t addr
 
     return RET_SUCESS;
 }
-uint8_t IEC104_BuildSignal(uint8_t reason, uint8_t Prio, uint8_t DevType)
+iec_8u IEC104_BuildSignal(iec_8u reason, iec_8u Prio, iec_8u DevType)
 {
 
-    uint8_t  len = 0, asdu_num = 0;
-    uint16_t i      = 0;
-    uint8_t *ptr    = NULL;
-    uint32_t Temp32 = 0;
+    iec_8u  len = 0, asdu_num = 0;
+    iec_16u i      = 0;
+    iec_8u *ptr    = NULL;
+    iec_32u Temp32 = 0;
 
     PIEC104_DATA_T Iec104Data = (PIEC104_DATA_T)Iec104_Sendbuf;
     PIEC10X_ASDU_T asdu       = (PIEC10X_ASDU_T)(Iec104Data->Asdu);
@@ -474,12 +461,12 @@ uint8_t IEC104_BuildSignal(uint8_t reason, uint8_t Prio, uint8_t DevType)
     return RET_SUCESS;
 }
 
-uint8_t IEC104_BuildDetect(uint8_t reason, uint8_t ValueType, uint8_t Prio, uint8_t DevType)
+iec_8u IEC104_BuildDetect(iec_8u reason, iec_8u ValueType, iec_8u Prio, iec_8u DevType)
 {
 
-    uint8_t            len = 0, asdu_num = 0, i;
-    uint8_t           *ptr      = NULL;
-    uint32_t           Temp32   = 0;
+    iec_8u             len = 0, asdu_num = 0, i;
+    iec_8u            *ptr      = NULL;
+    iec_32u            Temp32   = 0;
     PIEC10X_DETECT_T   detect   = NULL;
     PIEC10X_DETECT_F_T detect_f = NULL;
 
@@ -556,12 +543,12 @@ uint8_t IEC104_BuildDetect(uint8_t reason, uint8_t ValueType, uint8_t Prio, uint
     return RET_SUCESS;
 }
 
-uint8_t IEC104_BuildUpload(uint8_t ValueType, uint8_t Prio, uint8_t DevType)
+iec_8u IEC104_BuildUpload(iec_8u ValueType, iec_8u Prio, iec_8u DevType)
 {
 
-    uint8_t          len = 0, asdu_num = 0, i;
-    uint8_t         *ptr    = NULL;
-    uint32_t         Temp32 = 0;
+    iec_8u           len = 0, asdu_num = 0, i;
+    iec_8u          *ptr    = NULL;
+    iec_32u          Temp32 = 0;
     PIEC10X_DETECT_T detect = NULL;
 
     PIEC104_DATA_T Iec104Data = (PIEC104_DATA_T)Iec104_Sendbuf;
@@ -614,12 +601,12 @@ uint8_t IEC104_BuildUpload(uint8_t ValueType, uint8_t Prio, uint8_t DevType)
     return RET_SUCESS;
 }
 
-uint8_t IEC104_BuildActFinish(uint8_t qoi, uint8_t Prio)
+iec_8u IEC104_BuildActFinish(iec_8u qoi, iec_8u Prio)
 {
 
-    uint8_t  len = 0, asdu_num = 0;
-    uint8_t *ptr    = NULL;
-    uint32_t Temp32 = 0;
+    iec_8u  len = 0, asdu_num = 0;
+    iec_8u *ptr    = NULL;
+    iec_32u Temp32 = 0;
 
     PIEC104_DATA_T Iec104Data = (PIEC104_DATA_T)Iec104_Sendbuf;
     PIEC10X_ASDU_T asdu       = (PIEC10X_ASDU_T)(Iec104Data->Asdu);
@@ -661,11 +648,11 @@ uint8_t IEC104_BuildActFinish(uint8_t qoi, uint8_t Prio)
     return RET_SUCESS;
 }
 
-uint8_t IEC104_Build_U(uint8_t UType, uint8_t Ack)
+iec_8u IEC104_Build_U(iec_8u UType, iec_8u Ack)
 {
 
-    uint8_t  len = 0, Tester, Start, Stop;
-    uint8_t *ptr = NULL;
+    iec_8u  len = 0, Tester, Start, Stop;
+    iec_8u *ptr = NULL;
 
     PIEC104_DATA_T Iec104Data = (PIEC104_DATA_T)Iec104_Sendbuf;
 
@@ -718,11 +705,11 @@ uint8_t IEC104_Build_U(uint8_t UType, uint8_t Ack)
     return RET_SUCESS;
 }
 
-uint8_t IEC104_Build_S_Ack(void)
+iec_8u IEC104_Build_S_Ack(void)
 {
 
-    uint8_t  len = 0;
-    uint8_t *ptr = NULL;
+    iec_8u  len = 0;
+    iec_8u *ptr = NULL;
 
     PIEC104_DATA_T Iec104Data = (PIEC104_DATA_T)Iec104_Sendbuf;
 
@@ -750,14 +737,14 @@ uint8_t IEC104_Build_S_Ack(void)
 
     return RET_SUCESS;
 }
-uint8_t IEC104_ASDU_Call(PIEC10X_ASDU_T Iec10x_Asdu)
+iec_8u IEC104_ASDU_Call(PIEC10X_ASDU_T Iec10x_Asdu)
 {
 
     PASDU_INFO_T asdu_info = (PASDU_INFO_T)(Iec10x_Asdu->_info);
-    uint8_t      qoi       = asdu_info->_element[0];
-    uint8_t      Prio      = 0;
+    iec_8u       qoi       = asdu_info->_element[0];
+    iec_8u       Prio      = 0;
 
-    uint32_t InfoAddr = 0;
+    iec_32u InfoAddr = 0;
 
     /* check info addrest */
     memcpy(&InfoAddr, asdu_info->_addr, 3);
@@ -806,7 +793,7 @@ uint8_t IEC104_ASDU_Call(PIEC10X_ASDU_T Iec10x_Asdu)
     }
     return RET_SUCESS;
 }
-uint8_t Iec104_Aadu_Clock(PIEC10X_ASDU_T Iec10x_Asdu)
+iec_8u Iec104_Aadu_Clock(PIEC10X_ASDU_T Iec10x_Asdu)
 {
 
     PASDU_INFO_T asdu_info = (PASDU_INFO_T)(Iec10x_Asdu->_info);
@@ -841,14 +828,14 @@ uint8_t Iec104_Aadu_Clock(PIEC10X_ASDU_T Iec10x_Asdu)
     return RET_SUCESS;
 }
 
-uint8_t IEC104_ASDU_SetAct(PIEC10X_ASDU_T Iec10x_Asdu, uint8_t Type)
+iec_8u IEC104_ASDU_SetAct(PIEC10X_ASDU_T Iec10x_Asdu, iec_8u Type)
 {
 
     PASDU_INFO_T asdu_info = (PASDU_INFO_T)(Iec10x_Asdu->_info);
-    uint8_t     *ptr       = NULL;
-    uint8_t      n = Iec10x_Asdu->_num._num, Sq = Iec10x_Asdu->_num._sq, i;
+    iec_8u      *ptr       = NULL;
+    iec_8u       n = Iec10x_Asdu->_num._num, Sq = Iec10x_Asdu->_num._sq, i;
     float        Value    = 0.0;
-    uint32_t     InfoAddr = 0;
+    iec_32u      InfoAddr = 0;
 
     /* if sq == 1 */
     PIEC10X_DETECT_T   detect   = NULL;
@@ -936,11 +923,11 @@ uint8_t IEC104_ASDU_SetAct(PIEC10X_ASDU_T Iec10x_Asdu, uint8_t Type)
     return RET_SUCESS;
 }
 
-uint8_t IEC104_Build_SetAck(uint8_t Prio, uint8_t Type)
+iec_8u IEC104_Build_SetAck(iec_8u Prio, iec_8u Type)
 {
 
-    uint8_t  len = 0;
-    uint8_t *ptr = NULL;
+    iec_8u  len = 0;
+    iec_8u *ptr = NULL;
 
     PIEC104_DATA_T Iec104Data = (PIEC104_DATA_T)Iec104_Sendbuf;
     PIEC10X_ASDU_T asdu       = (PIEC10X_ASDU_T)(Iec104Data->Asdu);
@@ -975,7 +962,7 @@ uint8_t IEC104_Build_SetAck(uint8_t Prio, uint8_t Type)
     return RET_SUCESS;
 }
 
-uint8_t Iec104_Deal_SN(uint16_t SendSn, uint16_t RecvSn)
+iec_8u Iec104_Deal_SN(iec_16u SendSn, iec_16u RecvSn)
 {
 
     LOG("Receive Pakage I(%d,%d), Send(%d,%d)\n", SendSn, RecvSn, Iec104_BuildSendSn, Iec104_BuildRecvSn);
@@ -1020,12 +1007,12 @@ uint8_t Iec104_Deal_SN(uint16_t SendSn, uint16_t RecvSn)
 
     return RET_SUCESS;
 }
-int8_t Iec104_BuildDataAck(uint8_t TI, uint16_t COT, uint32_t InfoAddr, uint16_t Info, uint8_t Prio)
+iec_8s Iec104_BuildDataAck(iec_8u TI, iec_16u COT, iec_32u InfoAddr, iec_16u Info, iec_8u Prio)
 {
 
-    uint8_t  len    = 0;
-    uint8_t *ptr    = NULL;
-    uint32_t Temp32 = 0;
+    iec_8u  len    = 0;
+    iec_8u *ptr    = NULL;
+    iec_32u Temp32 = 0;
 
     /* build head */
     PIEC104_DATA_T Iec104Data = (PIEC104_DATA_T)Iec104_Sendbuf;
@@ -1076,21 +1063,21 @@ int8_t Iec104_BuildDataAck(uint8_t TI, uint16_t COT, uint32_t InfoAddr, uint16_t
     return RET_SUCESS;
 }
 
-int8_t Iec104_Deal_FirmUpdate(PIEC10X_ASDU_T asdu, uint8_t Len)
+iec_8s Iec104_Deal_FirmUpdate(PIEC10X_ASDU_T asdu, iec_8u Len)
 {
 
-    uint16_t     cot     = asdu->_reason._reason;
-    uint16_t     FlagNum = 0, i;
-    uint8_t      DataLen = Len - IEC104_DATA_LEN - 3; /* flag num 2, check sum 1 byte*/
+    iec_16u      cot     = asdu->_reason._reason;
+    iec_16u      FlagNum = 0, i;
+    iec_8u       DataLen = Len - IEC104_DATA_LEN - 3; /* flag num 2, check sum 1 byte*/
     PASDU_INFO_T element = (PASDU_INFO_T)(asdu->_info);
-    uint8_t     *DataPtr = (uint8_t *)(element->_element) + 3;
-    uint8_t      TI = asdu->_type, csum = 0, CsumTemp = 0;
-    uint32_t     FirmwareType = 0;
-    int8_t       ret          = 0;
+    iec_8u      *DataPtr = (iec_8u *)(element->_element) + 3;
+    iec_8u       TI = asdu->_type, csum = 0, CsumTemp = 0;
+    iec_32u      FirmwareType = 0;
+    iec_8s       ret          = 0;
 
     PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
 
-    FlagNum = *(uint16_t *)(info->_element);
+    FlagNum = *(iec_16u *)(info->_element);
     memcpy(&FirmwareType, asdu->_info, 3);
 
     if (Len == 0)
@@ -1248,12 +1235,12 @@ void Iec104_Tester_Timer(void)
     Iec104_TimeCycle_TesterIdle = 0;
 }
 
-uint8_t Iec104_Deal_I(PIEC104_DATA_T Iec104Data, uint16_t len)
+iec_8u Iec104_Deal_I(PIEC104_DATA_T Iec104Data, iec_16u len)
 {
 
-    uint8_t  Type;
-    uint16_t RecvSn, SendSn;
-    uint32_t FirmwareType = 0;
+    iec_8u  Type;
+    iec_16u RecvSn, SendSn;
+    iec_32u FirmwareType = 0;
 
     PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec104Data->Asdu);
 
@@ -1312,12 +1299,12 @@ uint8_t Iec104_Deal_I(PIEC104_DATA_T Iec104Data, uint16_t len)
     return RET_SUCESS;
 }
 
-uint8_t Iec104_Deal_S(PIEC104_DATA_T Iec104Data, uint16_t len)
+iec_8u Iec104_Deal_S(PIEC104_DATA_T Iec104Data, iec_16u len)
 {
 
     return RET_SUCESS;
 }
-uint8_t Iec104_Deal_U(PIEC104_DATA_T Iec104Data, uint16_t len)
+iec_8u Iec104_Deal_U(PIEC104_DATA_T Iec104Data, iec_16u len)
 {
 
     switch (Iec104Data->Ctrl.Func.Func)
@@ -1361,11 +1348,11 @@ uint8_t Iec104_Deal_U(PIEC104_DATA_T Iec104Data, uint16_t len)
     return RET_SUCESS;
 }
 
-void Iex104_Receive(uint8_t *buf, uint16_t len)
+void Iex104_Receive(iec_8u *buf, iec_16u len)
 {
 
-    uint8_t       *BufTemp = NULL;
-    int16_t        LenRemain, LenTmp;
+    iec_8u        *BufTemp = NULL;
+    iec_16s        LenRemain, LenTmp;
     PIEC104_DATA_T Iec104Data = NULL;
 
     if (buf == NULL)
@@ -1443,8 +1430,8 @@ void Iec104_ResetFlag(void)
     Iec104_TesterCount = 0;
 }
 
-uint32_t TestCount_Temp = 0;
-uint8_t  Iec104_StateMachine(void)
+iec_32u TestCount_Temp = 0;
+iec_8u  Iec104_StateMachine(void)
 {
 
     /*Init link*/

+ 126 - 118
User/app/iec104/iec104.h

@@ -4,68 +4,68 @@
 #define _IEC104_H
 
 #include "iec10x.h"
-#include "project_var.h"
+#include "iec10x_conf.h"
+#include "iec10x_type.h"
 
-#define     IEC104_MAX_BUF_LEN          256
+#define IEC104_MAX_BUF_LEN 256
+
+#define IEC104_HEAD 0X68
 
-#define     IEC104_HEAD                 0X68
 /*
  * PRIO
  * */
-#define             IEC104_PRIO_INITLINK            0
-#define             IEC104_PRIO_CALLALL             1
-#define             IEC104_PRIO_CALLGROUP           2
-#define             IEC104_PRIO_CLOCK               3
-#define             IEC104_PRIO_DELAY               3
-#define             IEC104_PRIO_PULSE               0
-#define             IEC104_PRIO_SPON                0
+#define IEC104_PRIO_INITLINK  0
+#define IEC104_PRIO_CALLALL   1
+#define IEC104_PRIO_CALLGROUP 2
+#define IEC104_PRIO_CLOCK     3
+#define IEC104_PRIO_DELAY     3
+#define IEC104_PRIO_PULSE     0
+#define IEC104_PRIO_SPON      0
 
 /*
  * Ctrl U Function
  * */
-#define             IEC104_U_FUNC_STARTDT           0x07		//0B00000111
-#define             IEC104_U_FUNC_STOPDT            0x13		//0B00010011
-#define             IEC104_U_FUNC_TESTER            0x43		//0B01000011
+#define IEC104_U_FUNC_STARTDT 0x07 // 0B00000111
+#define IEC104_U_FUNC_STOPDT  0x13 // 0B00010011
+#define IEC104_U_FUNC_TESTER  0x43 // 0B01000011
 
-#define             IEC104_U_FUNC_STARTDT_ACK       0x0b		//0B00001011
-#define             IEC104_U_FUNC_STOPDT_ACK        0x23		//0B00100011
-#define             IEC104_U_FUNC_TESTER_ACK        0x83		//0B10000011
+#define IEC104_U_FUNC_STARTDT_ACK 0x0b // 0B00001011
+#define IEC104_U_FUNC_STOPDT_ACK  0x23 // 0B00100011
+#define IEC104_U_FUNC_TESTER_ACK  0x83 // 0B10000011
 
 /*
  * ASDU Type Upload station address
  * */
-#define             IEC104_ASDU_TYPE_M_DTU_INF_1    180
-#define             IEC104_INFO_SIGNATURE           0XEB90EB90
-
+#define IEC104_ASDU_TYPE_M_DTU_INF_1 180
+#define IEC104_INFO_SIGNATURE        0XEB90EB90
 
 /*
  * Info Address
  * */
-#define             IEC104_DEV_TYPE_HXGF               0X03
-#define             IEC104_DEV_TYPE_HXTM               0X01
+#define IEC104_DEV_TYPE_HXGF 0X03
+#define IEC104_DEV_TYPE_HXTM 0X01
 
-#define             IEC104_INFOADDR_VALUE_HXGF         0X004001
-#define             IEC104_INFOADDR_VALUE_HXTM         0X004201
+#define IEC104_INFOADDR_VALUE_HXGF 0X004001
+#define IEC104_INFOADDR_VALUE_HXTM 0X004201
 
-#define             IEC104_INFOADDR_STATE_HXGF         0X000001
-#define             IEC104_INFOADDR_STATE_HXTM         0X000301
+#define IEC104_INFOADDR_STATE_HXGF 0X000001
+#define IEC104_INFOADDR_STATE_HXTM 0X000301
 
 /* base device information */
-#define             IEC104_INFOADDR_BASE_DEVINFO       0X102001
+#define IEC104_INFOADDR_BASE_DEVINFO 0X102001
 
 /*
- * device type 
+ * device type
  */
 //#define       ENDDEVICE_TYPE_ERR                  0
 //#define       ENDDEVICE_TYPE_HXPF                 2
-#define         ENDDEVICE_TYPE_HXTM                 1
-#define         ENDDEVICE_TYPE_HXGF                 3
+#define ENDDEVICE_TYPE_HXTM 1
+#define ENDDEVICE_TYPE_HXGF 3
 
-#define         AP_TYPE_BASE_INFO                   4    
+#define AP_TYPE_BASE_INFO 4
 
 /* COT */
-#define         AP_COT_BASE_INFO                    18  
-
+#define AP_COT_BASE_INFO 18
 
 #pragma pack(1)
 
@@ -73,109 +73,117 @@
  *  Iec104 package
  * */
 /* Control filed I type */
-typedef struct {
+typedef struct
+{
 
-    uint32_t Type:1;
-    uint32_t SendSn:15;
-    uint32_t Reserve:1;
-    uint32_t RecvSn:15;
-}IEC104_CTRL_I_T, *PIEC104_CTRL_I_T;
+    iec_32u Type    : 1;
+    iec_32u SendSn  : 15;
+    iec_32u Reserve : 1;
+    iec_32u RecvSn  : 15;
+} IEC104_CTRL_I_T, *PIEC104_CTRL_I_T;
 
 /* Control filed S type */
-typedef struct {
+typedef struct
+{
 
-    uint32_t Type1:1;
-    uint32_t Type2:1;
-    uint32_t Reserve:15;
-    uint32_t RecvSn:15;
-}IEC104_CTRL_S_T, *PEC104_CTRL_S_TP;
+    iec_32u Type1   : 1;
+    iec_32u Type2   : 1;
+    iec_32u Reserve : 15;
+    iec_32u RecvSn  : 15;
+} IEC104_CTRL_S_T, *PEC104_CTRL_S_TP;
 
 /* Control filed U type */
-typedef struct {
-
-    uint32_t Type1:1;
-    uint32_t Type2:1;
-    uint32_t Startdt:1;
-    uint32_t StartdtAck:1;
-    uint32_t Stopdt:1;
-    uint32_t StopdtAck:1;
-    uint32_t Tester:1;
-    uint32_t TesterAck:1;
-    uint32_t Reserve:24;
-}IEC104_CTRL_U_T, *PIEC104_CTRL_U_T;
+typedef struct
+{
+
+    iec_32u Type1      : 1;
+    iec_32u Type2      : 1;
+    iec_32u Startdt    : 1;
+    iec_32u StartdtAck : 1;
+    iec_32u Stopdt     : 1;
+    iec_32u StopdtAck  : 1;
+    iec_32u Tester     : 1;
+    iec_32u TesterAck  : 1;
+    iec_32u Reserve    : 24;
+} IEC104_CTRL_U_T, *PIEC104_CTRL_U_T;
 
 /* Control filed type */
-typedef struct {
+typedef struct
+{
 
-    uint32_t Type1:1;
-    uint32_t Type2:1;
-    uint32_t Reserve:30;
+    iec_32u Type1   : 1;
+    iec_32u Type2   : 1;
+    iec_32u Reserve : 30;
 
-}IEC104_CTRLType_T, *PIEC104_CTRLType_T;
+} IEC104_CTRLType_T, *PIEC104_CTRLType_T;
 
 /* Control filed type */
-typedef struct {
+typedef struct
+{
 
-    uint32_t Func:8;
-    uint32_t Reserve:24;
+    iec_32u Func    : 8;
+    iec_32u Reserve : 24;
 
-}IEC104_CTRLFunc_T, *PIEC104_CTRLFunc_T;
+} IEC104_CTRLFunc_T, *PIEC104_CTRLFunc_T;
 
-typedef union{
+typedef union
+{
 
-    IEC104_CTRL_I_T     I;
-    IEC104_CTRL_S_T     S;
-    IEC104_CTRL_U_T     U;
-    IEC104_CTRLType_T   Type;
-    IEC104_CTRLFunc_T   Func;
-}IEC104_CTRL_T, *PIEC104_CTRL_T;
+    IEC104_CTRL_I_T   I;
+    IEC104_CTRL_S_T   S;
+    IEC104_CTRL_U_T   U;
+    IEC104_CTRLType_T Type;
+    IEC104_CTRLFunc_T Func;
+} IEC104_CTRL_T, *PIEC104_CTRL_T;
 
-typedef struct{
+typedef struct
+{
 
-    uint8_t         Head;
-    uint8_t         Len;
-    IEC104_CTRL_T   Ctrl;
-    uint8_t         Asdu[1];
-}IEC104_DATA_T,*PIEC104_DATA_T;
+    iec_8u        Head;
+    iec_8u        Len;
+    IEC104_CTRL_T Ctrl;
+    iec_8u        Asdu[1];
+} IEC104_DATA_T, *PIEC104_DATA_T;
 
-typedef struct {
+typedef struct
+{
 
     float Current;
-    float Temperature;    
-}Iec10x_DevA_Info_T, *PIec10x_DevA_Info_T;
+    float Temperature;
+} Iec10x_DevA_Info_T, *PIec10x_DevA_Info_T;
 
 #pragma pack()
 
 /*
  * length
  * */
-#define     IEC104_HEAD_LEN          (sizeof(IEC104_DATA_T)-1)
-#define     IEC104_ASDU_LEN          (sizeof(IEC10X_ASDU_T)-1)
-#define     IEC104_INFO_LEN          (sizeof(ASDU_INFO_T)-1)
-#define     IEC104_DATA_LEN          (IEC104_HEAD_LEN+IEC104_ASDU_LEN+IEC104_INFO_LEN)
-
-
-enum {
-    IEC104_FLAG_RECV_CLOSED,        /*0*/
-    IEC104_FLAG_SEND_CLOSED,        /*1*/
-    IEC104_FLAG_CLOSED,             /*2*/
-    IEC104_FLAG_LINK_INIT,          /*3*/
-    IEC104_FLAG_IDLE,               /*4*/
-    IEC104_FLAG_UPLOAD_ADDR,             /*5*/
-    IEC104_FLAG_START_LINK,             /*6*/
-    IEC104_FLAG_REQ_LINK,             /*7*/
-    IEC104_FLAG_RESET_REMOTE_LINK,             /*8*/
-    IEC104_FLAG_INIT_FIN,             /*9*/
-    IEC104_FLAG_CALL_ALLDATA,             /*10*/
-    IEC101_FLAG_CALL_GROURPDATA,             /*11*/
-    IEC104_FLAG_CALL_ACT_FIN,             /*12*/
-    IEC104_FLAG_CALL_SIG_TOTAL,             /*13*/
-    IEC104_FLAG_CALL_DET_TOTAL,             /*14*/
-    IEC104_FLAG_CLOCK_SYS,             /*15*/
-    IEC104_FLAG_TESTER,             /*16*/
-    IEC104_FLAG_TESTER_STOP,             /*17*/
-    IEC104_FLAG_CALL_GROUP,             /*18*/
-    IEC104_FLAG_CONNECT_SUCESS,             /*19*/
+#define IEC104_HEAD_LEN (sizeof(IEC104_DATA_T) - 1)
+#define IEC104_ASDU_LEN (sizeof(IEC10X_ASDU_T) - 1)
+#define IEC104_INFO_LEN (sizeof(ASDU_INFO_T) - 1)
+#define IEC104_DATA_LEN (IEC104_HEAD_LEN + IEC104_ASDU_LEN + IEC104_INFO_LEN)
+
+enum
+{
+    IEC104_FLAG_RECV_CLOSED,       /*0*/
+    IEC104_FLAG_SEND_CLOSED,       /*1*/
+    IEC104_FLAG_CLOSED,            /*2*/
+    IEC104_FLAG_LINK_INIT,         /*3*/
+    IEC104_FLAG_IDLE,              /*4*/
+    IEC104_FLAG_UPLOAD_ADDR,       /*5*/
+    IEC104_FLAG_START_LINK,        /*6*/
+    IEC104_FLAG_REQ_LINK,          /*7*/
+    IEC104_FLAG_RESET_REMOTE_LINK, /*8*/
+    IEC104_FLAG_INIT_FIN,          /*9*/
+    IEC104_FLAG_CALL_ALLDATA,      /*10*/
+    IEC101_FLAG_CALL_GROURPDATA,   /*11*/
+    IEC104_FLAG_CALL_ACT_FIN,      /*12*/
+    IEC104_FLAG_CALL_SIG_TOTAL,    /*13*/
+    IEC104_FLAG_CALL_DET_TOTAL,    /*14*/
+    IEC104_FLAG_CLOCK_SYS,         /*15*/
+    IEC104_FLAG_TESTER,            /*16*/
+    IEC104_FLAG_TESTER_STOP,       /*17*/
+    IEC104_FLAG_CALL_GROUP,        /*18*/
+    IEC104_FLAG_CONNECT_SUCESS,    /*19*/
     IEC104_FLAG_S_ACK,             /*20*/
 };
 
@@ -184,21 +192,21 @@ enum {
 *                          EXTERN  VARIABLE
 *********************************************************************************************************
 */
-extern      uint8_t             IEC104_STATE_FLAG_INIT;
-extern      uint8_t             IEC104_STATE_FLAG_CALLALL;
-extern      uint8_t             IEC104_STATE_FLAG_GROUP;
-extern      uint8_t             IEC104_STATE_FLAG_CLOCK;
-extern      uint8_t             IEC104_STATE_FLAG_PULSE;
+extern iec_8u IEC104_STATE_FLAG_INIT;
+extern iec_8u IEC104_STATE_FLAG_CALLALL;
+extern iec_8u IEC104_STATE_FLAG_GROUP;
+extern iec_8u IEC104_STATE_FLAG_CLOCK;
+extern iec_8u IEC104_STATE_FLAG_PULSE;
 
 /*
 *********************************************************************************************************
 *                          FUNCTION
 *********************************************************************************************************
 */
-uint8_t         Iec104_StateMachine(void);
-uint8_t         IEC104_BuildSignal_Spon(uint8_t TimeFlag, uint8_t signalV, uint16_t addrV);
-uint8_t         IEC104_BuildDetectF_Spont(uint8_t TimeFlag, float detectV, uint16_t addrV);
-void            Iex104_Receive(uint8_t *buf, uint16_t len);
-uint8_t         IEC104_BuildDetect_Spont(uint8_t TimeFlag, PIEC10X_DETECT_T detectV, uint16_t addrV);
+iec_8u Iec104_StateMachine(void);
+iec_8u IEC104_BuildSignal_Spon(iec_8u TimeFlag, iec_8u signalV, iec_16u addrV);
+iec_8u IEC104_BuildDetectF_Spont(iec_8u TimeFlag, float detectV, iec_16u addrV);
+void   Iex104_Receive(iec_8u *buf, iec_16u len);
+iec_8u IEC104_BuildDetect_Spont(iec_8u TimeFlag, PIEC10X_DETECT_T detectV, iec_16u addrV);
 
 #endif

+ 0 - 167
User/app/iec104/iec104_init.c

@@ -1,167 +0,0 @@
-/*******************************************************************
-Copyright (C):
-File name    :    HXCP_STM32.C
-DESCRIPTION  :
-AUTHOR       :
-Version      :    1.0
-Date         :    2014/02/26
-Others       :
-History      :
-++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-1) Date: 2014/02/26         Author: ChenDajie
-   content:
-
-*******************************************************************/
-
-#include "PRIO_QUEUE_Iec10x.h"
-#include "dwt.h"
-#include "fly_param.h"
-#include "iec10x.h"
-#include "main.h"
-#include "project_var.h"
-// #include <errno.h>
-// #include <signal.h>
-// #include <stdio.h>
-// #include <stdlib.h>
-#include <string.h>
-// #include <sys/socket.h>
-// #include <sys/types.h>
-// #include <sys/wait.h>
-// #include <unistd.h>
-
-/*******************************************************************************
- * Function Name  : stm32f103_init
- * Description    : stm32f103_init program
- * Input          : None
- * Output         : None
- * Return         : None
- *******************************************************************************/
-static int
-Linux_init(void)
-{
-
-    return 0;
-}
-
-static void *Linux_Malloc(unsigned char NumByte)
-{
-
-    return malloc(NumByte);
-}
-
-static void Linux_Free(void *pblock)
-{
-
-    free(pblock);
-}
-
-static uint32_t Linux_SetTime(PCP56Time2a_T time)
-{
-
-    return RET_SUCESS;
-}
-static uint32_t Linux_GetTime(PCP56Time2a_T time)
-{
-
-    return RET_SUCESS;
-}
-
-static int8_t Linux_GetStationInfo(uint16_t *Addr, uint8_t n, uint8_t *MaxNum)
-{
-
-    *MaxNum = WTP_SUPPORT_END_NUMBER;
-    *Addr   = n;
-    return RET_SUCESS;
-}
-
-static float Linux_GetStaTemp(uint16_t Addr)
-{
-
-    return 30;
-}
-
-static uint16_t Linux_GetLinkAddr(void)
-{
-
-    return 0x0001;
-}
-
-static void Linux_CloseLink(void)
-{
-
-    // GPRSFlag = SYSTEM_FLAG_GPRS_CLOSE;
-    // IEC104_STATE_FLAG_INIT = IEC104_FLAG_CLOSED;
-}
-
-void delay_ms(uint16_t ms)
-{
-
-    us_delay(ms * 1000);
-}
-
-uint8_t Linuxsend(int socketfd, char *buf, int len)
-{
-
-    if (-1 == write(socketfd, buf, len))
-    {
-        LOG("-%s-,Send error \n", __FUNCTION__);
-        return RET_ERROR;
-    }
-    printf("Send Ok!\r\n");
-
-    return RET_SUCESS;
-}
-void LinuxLock(void)
-{
-    INT8U err;
-    OSMutexPend(net_mutex, 0, &err);
-    // pthread_mutex_lock(&mutex);
-}
-void LinuxUnlock()
-{
-    OSMutexPost(net_mutex);
-    // pthread_mutex_unlock(&mutex);
-}
-/*******************************************************************************
- * Function Name  : stm32f103
- * Description    : stm32f103
- * Input          : None
- * Output         : None
- * Return         : None
- *******************************************************************************/
-static IEC10X_T Linux = {
-    "Linux",
-    Linux_init,
-    delay_ms,
-    Linux_CloseLink,
-    Linux_Malloc,
-    Linux_Free,
-    IEC10X_PrioEnQueue,
-    IEC10X_PrioDeQueue,
-    IEC10X_PrioFindQueueHead,
-    IEC10X_HighestPrio, /* Get the highest Prio Queue*/
-    IEC10X_PrioInitQueue,
-    IEC10X_Prio_ClearQueue,
-    Linuxsend,
-    Linux_SetTime,
-    Linux_GetTime,
-    Linux_GetStationInfo,
-    Linux_GetStaTemp,
-    Linux_GetLinkAddr,
-
-#ifdef IEC10XLOCK
-    LinuxLock,
-    LinuxUnlock
-#endif
-};
-/*******************************************************************************
- * Function Name  : mstm32f103RegisterShtxxain
- * Description    : Main program
- * Input          : None
- * Output         : None
- * Return         : None
- *******************************************************************************/
-uint32_t Stm32f103RegisterIec10x(void)
-{
-    return RegisterIEC10XMoudle(&Linux);
-}

+ 22 - 22
User/app/iec104/iec10x.c

@@ -1,23 +1,15 @@
 #include "iec10x.h"
-#include "project_var.h"
+#include "iec10x_conf.h"
+#include "iec10x_type.h"
 
 PIEC10X_T          IEC10X          = NULL;
-uint16_t           Iec10x_Sta_Addr = 0;
+iec_16u            Iec10x_Sta_Addr = 0;
 Iec10x_PrioQueue_T Iec10x_PrioQueueArray[IEC10X_PRIO_MAX];
 
-uint16_t     IEC10X_Cp16time2a   = 0;
-uint16_t     IEC10X_Cp16time2a_V = 0;
+iec_16u      IEC10X_Cp16time2a   = 0;
+iec_16u      IEC10X_Cp16time2a_V = 0;
 CP56Time2a_T IEC10X_Cp56time2a;
 
-/*******************************************************************************
-* Function Name  : IEC10X_InitQ
-* Description    : Init queue.
-* Input          : None
-* Output         : None
-* Return         : 0 failure
-                   len sucess
-*******************************************************************************/
-
 void Iec10x_Lock(void)
 {
 
@@ -34,34 +26,42 @@ void Iec10x_UnLock(void)
         IEC10X->UNLOCK();
 #endif
 }
+
+/****************************************************
+ * @函数    : IEC10X_InitQ
+ * @作者    : 樊春春
+ * @功能    :
+ * @入参    :
+ * @说明    :
+ *****************************************************/
 void IEC10X_InitQ(void)
 {
-
-    uint8_t i;
+    iec_8u i;
     for (i = 0; i < IEC10X_PRIO_MAX; i++)
     {
         IEC10X->InitQueue(&Iec10x_PrioQueueArray[i]);
     }
 }
+
 void IEC10X_ClearQ(void)
 {
-    uint8_t i;
+    iec_8u i;
     for (i = 0; i < IEC10X_PRIO_MAX; i++)
     {
         IEC10X->ClearQueue(&Iec10x_PrioQueueArray[i]);
     }
 }
 
-uint8_t IEC10X_GetPrio(uint8_t State)
+iec_8u IEC10X_GetPrio(iec_8u State)
 {
 
     /*Prio from 0~7*/
-    uint8_t Prio = 0;
+    iec_8u Prio = 0;
 
     return Prio;
 }
 
-void IEC10X_Enqueue(uint8_t *EnQBuf, uint16_t Length, uint8_t Prio,
+void IEC10X_Enqueue(iec_8u *EnQBuf, iec_16u Length, iec_8u Prio,
                     void (*IEC10XCallBack)(Iec10x_CallbackArg_T *Arg), Iec10x_CallbackArg_T *CallbackArg)
 {
 
@@ -107,7 +107,7 @@ END:
 
 Iec10x_PrioNode_T *IEC10X_Dequeue(void)
 {
-    uint8_t            Prio;
+    iec_8u             Prio;
     Iec10x_PrioNode_T *ret;
     Prio = IEC10X->GetPrio();
     if (Prio >= 8)
@@ -126,7 +126,7 @@ Iec10x_PrioNode_T *IEC10X_Dequeue(void)
 Iec10x_PrioNode_T *IEC10X_FindQHead(void)
 {
 
-    uint8_t            Prio;
+    iec_8u             Prio;
     Iec10x_PrioNode_T *ret;
     Prio = IEC10X->GetPrio();
     if (Prio >= 8)
@@ -168,7 +168,7 @@ void Iec10x_Scheduled(int socketfd)
  * Output         : None
  * Return         : None
  *******************************************************************************/
-int32_t RegisterIEC10XMoudle(void *_IEC10X)
+iec_32s RegisterIEC10XMoudle(void *_IEC10X)
 {
 
     int ret;

+ 77 - 75
User/app/iec104/iec10x.h

@@ -17,8 +17,9 @@ History      :
 #ifndef _IEC10X_H
 #define _IEC10X_H
 
-#include "PRIO_QUEUE_Iec10x.h"
-#include "project_var.h"
+#include "iec10x_conf.h"
+#include "iec10x_prio_queue.h"
+#include "iec10x_type.h"
 #include <stdio.h>
 #include <string.h>
 
@@ -51,18 +52,18 @@ History      :
  */
 typedef struct IEC10X_CallbackArg
 {
-    uint8_t *value;
-    uint32_t PicSn;
-    uint32_t FramSerialNum;
+    iec_8u *value;
+    iec_32u PicSn;
+    iec_32u FramSerialNum;
 } Iec10x_CallbackArg_T;
 
 typedef struct IEC10X_NODE
 {
-    uint16_t            Length;
+    iec_16u             Length;
     struct IEC10X_NODE *Next;
     void (*CallBack)(Iec10x_CallbackArg_T *Arg);
     Iec10x_CallbackArg_T CallBackArg;
-    uint8_t              value[1];
+    iec_8u               value[1];
 } Iec10x_PrioNode_T;
 
 typedef struct IEC10X_QUEUE
@@ -226,6 +227,7 @@ typedef struct IEC10X_QUEUE
 #define IEC10X_TI_FIRM_UPDATE     128
 #define IEC10X_TI_AP_FIRM_BACKOFF 129
 #define IEC10X_TI_AP_BASE_INFO    130
+
 /* COT. */
 #define IEC10X_COT_ACT_TERMINAL     8
 #define IEC10X_COT_ACT_TERMINAL_ACK 9
@@ -244,8 +246,8 @@ typedef struct IEC10X_QUEUE
 typedef struct
 {
 
-    uint8_t _num : 7;
-    uint8_t _sq  : 1;
+    iec_8u _num : 7;
+    iec_8u _sq  : 1;
 
 } ASDU_NUM_T;
 /*
@@ -254,13 +256,13 @@ typedef struct
 typedef struct
 {
 #ifdef IEC101_STM32
-    uint8_t _reason : 6;
-    uint8_t _pn     : 1;
-    uint8_t _test   : 1;
+    iec_8u _reason : 6;
+    iec_8u _pn     : 1;
+    iec_8u _test   : 1;
 #elif defined(IEC104_STM32)
-    uint16_t _reason : 14;
-    uint16_t _pn     : 1;
-    uint16_t _test   : 1;
+    iec_16u _reason : 14;
+    iec_16u _pn     : 1;
+    iec_16u _test   : 1;
 #endif
 
 } ASDU_REASON_T;
@@ -270,11 +272,11 @@ typedef struct
 typedef struct
 {
 
-    uint8_t       _type;
+    iec_8u        _type;
     ASDU_NUM_T    _num;
     ASDU_REASON_T _reason;
-    uint16_t      _addr;
-    uint8_t       _info[1];
+    iec_16u       _addr;
+    iec_8u        _info[1];
 } IEC10X_ASDU_T, *PIEC10X_ASDU_T;
 /*
  *  asdu info
@@ -283,11 +285,11 @@ typedef struct
 {
 
 #ifdef IEC101_STM32
-    uint16_t _addr;
+    iec_16u _addr;
 #elif defined(IEC104_STM32)
-    uint8_t  _addr[3];
+    iec_8u  _addr[3];
 #endif
-    uint8_t _element[1];
+    iec_8u _element[1];
 } ASDU_INFO_T, *PASDU_INFO_T;
 
 /*
@@ -296,8 +298,8 @@ typedef struct
 typedef struct
 {
 
-    uint8_t _signal;
-    uint8_t _time;
+    iec_8u _signal;
+    iec_8u _time;
 } SIGNAL_TIME_T, *PSIGNAL_TIME_T;
 
 /*
@@ -307,16 +309,16 @@ typedef struct
 typedef struct
 {
 
-    int16_t _detect;
-    uint8_t _qds;
+    iec_16s _detect;
+    iec_8u  _qds;
 
 } IEC10X_DETECT_T, *PIEC10X_DETECT_T;
 /*float*/
 typedef struct
 {
 
-    float   _detect;
-    uint8_t _qds;
+    float  _detect;
+    iec_8u _qds;
 
 } IEC10X_DETECT_F_T, *PIEC10X_DETECT_F_T;
 
@@ -325,12 +327,12 @@ typedef struct
 {
 
 #ifdef IEC101_STM32
-    uint16_t _addr;
+    iec_16u _addr;
 #elif defined(IEC104_STM32)
-    uint8_t  _addr[3];
+    iec_8u  _addr[3];
 #endif
-    int16_t _detect;
-    uint8_t _qds;
+    iec_16s _detect;
+    iec_8u  _qds;
 
 } IEC10X_DETECT_SQ0_T, *PIEC10X_DETECT_SQ0_T;
 /*float sq=0*/
@@ -338,12 +340,12 @@ typedef struct
 {
 
 #ifdef IEC101_STM32
-    uint16_t _addr;
+    iec_16u _addr;
 #elif defined(IEC104_STM32)
-    uint8_t  _addr[3];
+    iec_8u  _addr[3];
 #endif
-    float   _detect;
-    uint8_t _qds;
+    float  _detect;
+    iec_8u _qds;
 
 } IEC10X_DETECT_SQ0_F_T, *PIEC10X_DETECT_SQ0_F_T;
 
@@ -353,40 +355,40 @@ typedef struct
 typedef struct
 {
 
-    uint8_t _minutes : 6;
-    uint8_t _res     : 1;
-    uint8_t _iv      : 1;
+    iec_8u _minutes : 6;
+    iec_8u _res     : 1;
+    iec_8u _iv      : 1;
 } IEC10X_Time_Min_T, *PIEC10X_Time_Min_T;
 
 typedef struct
 {
 
-    uint8_t _hours : 5;
-    uint8_t _res   : 2;
-    uint8_t _su    : 1;
+    iec_8u _hours : 5;
+    iec_8u _res   : 2;
+    iec_8u _su    : 1;
 } IEC10X_Time_Hour_T, *PIEC10X_Time_Hour_T;
 typedef struct
 {
 
-    uint8_t _dayofmonth : 5;
-    uint8_t _dayofweek  : 3;
+    iec_8u _dayofmonth : 5;
+    iec_8u _dayofweek  : 3;
 } IEC10X_Time_Day_T, *PIEC10X_Time_Day_T;
 typedef struct
 {
 
-    uint8_t _month : 4;
-    uint8_t _res   : 3;
+    iec_8u _month : 4;
+    iec_8u _res   : 3;
 } IEC10X_Time_Month_T, *PIEC10X_Time_Month_T;
 typedef struct
 {
 
-    uint8_t _year : 7;
-    uint8_t _res  : 1;
+    iec_8u _year : 7;
+    iec_8u _res  : 1;
 } IEC10X_Time_Year_T, *PIEC10X_Time_Year_T;
 typedef struct
 {
 
-    uint16_t            _milliseconds;
+    iec_16u             _milliseconds;
     IEC10X_Time_Min_T   _min;
     IEC10X_Time_Hour_T  _hour;
     IEC10X_Time_Day_T   _day;
@@ -401,8 +403,8 @@ typedef struct
 typedef struct
 {
 
-    uint16_t     _detect;
-    uint8_t      _qds;
+    iec_16u      _detect;
+    iec_8u       _qds;
     CP56Time2a_T _time;
 } DETECT_TIME_T, *PDETECT_TIME_T;
 
@@ -416,28 +418,28 @@ typedef struct _iec10x
 
     char *name;
     int (*Init)(void);
-    void (*Delay_ms)(uint16_t);
+    void (*Delay_ms)(iec_16u);
     void (*CloseLink)(void);
-    void *(*Malloc)(uint8_t NumByte);
+    void *(*Malloc)(iec_8u nbyte);
     void (*Free)(void *buffer);
-    uint8_t (*enqueue)(Iec10x_PrioQueue_T *QueueHdr, Iec10x_PrioNode_T *NewNode);
+    iec_8u (*enqueue)(Iec10x_PrioQueue_T *QueueHdr, Iec10x_PrioNode_T *newnode);
     Iec10x_PrioNode_T *(*dequeue)(Iec10x_PrioQueue_T *QueueHdr);
     Iec10x_PrioNode_T *(*FindQHead)(Iec10x_PrioQueue_T *QueueHdr);
     char (*GetPrio)(void);
     void (*InitQueue)(Iec10x_PrioQueue_T *PrioQueue);
     void (*ClearQueue)(Iec10x_PrioQueue_T *QueueHdr);
-    uint8_t (*Send)(int socketfd, char *data, int len);
-    uint32_t (*SetTime)(PCP56Time2a_T time);
-    uint32_t (*GetTime)(PCP56Time2a_T time);
-    int8_t (*GetStationState)(uint16_t Addr, uint8_t DevType);
-    float (*GetStaValue)(uint16_t Addr, uint8_t DevType);
-    uint16_t (*GetLinkAddr)(void);
-    int8_t (*GetInfoNum)(uint8_t *InfoNum, uint8_t DevType);
-    int8_t (*SetConfig)(long Value, uint32_t addr);
-    int8_t (*SaveFirmware)(uint8_t FirmLen, uint8_t *buf, uint32_t FirmwareType, uint32_t Iec10x_Update_SeekAddr);
-    int8_t (*CheckFirmware)(uint32_t FirmwareType, uint32_t TotalLen);
-    int8_t (*UpdateFirmware)(uint32_t FirmwareType);
-    int8_t (*BackoffFirmware)(uint32_t FirmwareType);
+    iec_8u (*Send)(int socketfd, char *data, int len);
+    iec_32u (*SetTime)(PCP56Time2a_T time);
+    iec_32u (*GetTime)(PCP56Time2a_T time);
+    iec_8s (*GetStationState)(iec_16u Addr, iec_8u DevType);
+    float (*GetStaValue)(iec_16u Addr, iec_8u DevType);
+    iec_16u (*GetLinkAddr)(void);
+    iec_8s (*GetInfoNum)(iec_8u *InfoNum, iec_8u DevType);
+    iec_8s (*SetConfig)(long Value, iec_32u addr);
+    iec_8s (*SaveFirmware)(iec_8u FirmLen, iec_8u *buf, iec_32u FirmwareType, iec_32u Iec10x_Update_SeekAddr);
+    iec_8s (*CheckFirmware)(iec_32u FirmwareType, iec_32u TotalLen);
+    iec_8s (*UpdateFirmware)(iec_32u FirmwareType);
+    iec_8s (*BackoffFirmware)(iec_32u FirmwareType);
 
 #ifdef IEC10XLOCK
     void (*LOCK)(void);
@@ -472,14 +474,14 @@ typedef struct _iec10x
 *********************************************************************************************************
 */
 extern PIEC10X_T IEC10X;
-extern uint16_t  Iec10x_Sta_Addr;
-extern int32_t   Iec104_BuildSendSn;
-extern int32_t   Iec104_BuildRecvSn;
+extern iec_16u   Iec10x_Sta_Addr;
+extern iec_32s   Iec104_BuildSendSn;
+extern iec_32s   Iec104_BuildRecvSn;
 
-extern uint16_t     IEC10X_Cp16time2a;
-extern uint16_t     IEC10X_Cp16time2a_V;
+extern iec_16u      IEC10X_Cp16time2a;
+extern iec_16u      IEC10X_Cp16time2a_V;
 extern CP56Time2a_T IEC10X_Cp56time2a;
-extern uint8_t      Iec10x_FirmwareUpdateFlag;
+extern iec_8u       Iec10x_FirmwareUpdateFlag;
 /*
 *********************************************************************************************************
 *                                          QUEUE FUNCTION PROTOTYPES
@@ -488,7 +490,7 @@ extern uint8_t      Iec10x_FirmwareUpdateFlag;
 /*
  * Prio
  * */
-uint8_t            IEC10X_PrioEnQueue(Iec10x_PrioQueue_T *QueueHdr, Iec10x_PrioNode_T *new_p);
+iec_8u             IEC10X_PrioEnQueue(Iec10x_PrioQueue_T *QueueHdr, Iec10x_PrioNode_T *new_p);
 Iec10x_PrioNode_T *IEC10X_PrioDeQueue(Iec10x_PrioQueue_T *QueueHdr);
 char               IEC10X_HighestPrio(void);
 void               IEC10X_PrioInitQueue(Iec10x_PrioQueue_T *PrioQueue);
@@ -499,12 +501,12 @@ Iec10x_PrioNode_T *IEC10X_PrioFindQueueHead(Iec10x_PrioQueue_T *QueueHdr);
  * */
 void               IEC10X_InitQ(void);
 void               IEC10X_ClearQ(void);
-uint8_t            IEC10X_GetPrio(uint8_t State);
-void               IEC10X_Enqueue(uint8_t *EnQBuf, uint16_t Length, uint8_t Prio,
+iec_8u             IEC10X_GetPrio(iec_8u State);
+void               IEC10X_Enqueue(iec_8u *EnQBuf, iec_16u Length, iec_8u Prio,
                                   void (*IEC10XCallBack)(Iec10x_CallbackArg_T *Arg), Iec10x_CallbackArg_T *CallbackArg);
 void               Iec10x_Scheduled(int socketfd);
 Iec10x_PrioNode_T *IEC10X_Dequeue(void);
-int32_t            RegisterIEC10XMoudle(void *_IEC10X);
+iec_32s            RegisterIEC10XMoudle(void *_IEC10X);
 void               IEC10X_ClearQ(void);
 
 void Iec10x_Lock(void);

+ 37 - 0
User/app/iec104/iec10x_conf.h

@@ -0,0 +1,37 @@
+
+#ifndef __IEC10x_CONF_H
+#define __IEC10x_CONF_H
+
+#include "iec10x_type.h"
+
+#define WTP_SUPPORT_END_NUMBER 9
+
+#define RET_SUCESS 0
+#define RET_ERROR  1
+
+//#define IEC101_STM32
+#define IEC104_STM32
+//#define IEC104_STM32_FUJIAN_HX
+
+//#define SIM900_MODE_SERVER
+#define SIM900_MODE_CLIENT
+
+#define SERIAL_DEBUG
+
+#ifdef SERIAL_DEBUG
+#define log(...) printf(__VA_ARGS__)
+#define LOG(...) printf(__VA_ARGS__)
+#define LogInit()
+#define PRINT_FUNLINE printf("%s %d\r\n", __FUNCTION__, __LINE__)
+#else
+#define log(...)
+#define LOG(...)
+#define DumpHEX(buffer, len)
+#define PRINT_FUNLINE
+#endif
+
+#ifdef SERIAL_DEBUG
+void DumpHEX(iec_8u *buffer, iec_32u len);
+#endif
+
+#endif

+ 149 - 0
User/app/iec104/iec10x_init.c

@@ -0,0 +1,149 @@
+
+#include "dwt.h"
+#include "fly_param.h"
+#include "iec10x.h"
+#include "iec10x_conf.h"
+#include "iec10x_prio_queue.h"
+#include "iec10x_type.h"
+#include "main.h"
+#include "nor_flash.h"
+#include <string.h>
+
+#define TEST_BUF_SIZE 2048
+
+#define TEST_SECTOR_NO 127 /* 测试扇区号,值域【0,128】 */
+
+uint8_t g_TestBuf[TEST_BUF_SIZE]; /* 数据测试用缓冲区 */
+
+/*******************************************************************************
+ * Function Name  : fly_init
+ * Description    : fly_init program
+ * Input          : None
+ * Output         : None
+ * Return         : None
+ *******************************************************************************/
+static int fly_init(void)
+{
+
+    return 0;
+}
+
+static void *fly_malloc(unsigned char nbyte)
+{
+
+    return malloc(nbyte);
+}
+
+static void fly_free(void *pblock)
+{
+
+    free(pblock);
+}
+
+static iec_32u fly_set_time(PCP56Time2a_T time)
+{
+
+    return RET_SUCESS;
+}
+static iec_32u fly_get_time(PCP56Time2a_T time)
+{
+
+    return RET_SUCESS;
+}
+
+static iec_8s fly_get_station_info(iec_16u *Addr, iec_8u n, iec_8u *MaxNum)
+{
+
+    *MaxNum = WTP_SUPPORT_END_NUMBER;
+    *Addr   = n;
+    return RET_SUCESS;
+}
+
+static float fly_get_station_temp(iec_16u Addr)
+{
+
+    return 30;
+}
+
+static iec_16u fly_get_link_addr(void)
+{
+
+    return 0x0001;
+}
+
+static void fly_close_link(void)
+{
+
+    // GPRSFlag = SYSTEM_FLAG_GPRS_CLOSE;
+    // IEC104_STATE_FLAG_INIT = IEC104_FLAG_CLOSED;
+}
+
+void delay_ms(iec_16u ms)
+{
+
+    us_delay(ms * 1000);
+}
+
+iec_8u fly_send(int socketfd, char *buf, int len)
+{
+    int i;
+    if (-1 == write(socketfd, buf, len))
+    {
+        LOG("-%s-,Send error \n", __FUNCTION__);
+        return RET_ERROR;
+    }
+
+    // for (i = 0; i < len; i++)
+    // {
+    // NOR_WriteByte(TEST_SECTOR_NO * NOR_SECTOR_SIZE, buf[i]);
+    if (NOR_WriteBuffer(buf, TEST_SECTOR_NO * NOR_SECTOR_SIZE + 1, len) == NOR_SUCCESS)
+    {
+        printf("send sram buffer ok!\r\n");
+    }
+    // }
+    printf("Send Ok!\r\n");
+
+    return RET_SUCESS;
+}
+void fly_lock(void)
+{
+    INT8U err;
+    OSMutexPend(net_mutex, 0, &err);
+    // pthread_mutex_lock(&mutex);
+}
+void fly_unlock()
+{
+    OSMutexPost(net_mutex);
+    // pthread_mutex_unlock(&mutex);
+}
+
+static IEC10X_T fly = {
+    "Linux",
+    fly_init,
+    delay_ms,
+    fly_close_link,
+    fly_malloc,
+    fly_free,
+    IEC10X_PrioEnQueue,
+    IEC10X_PrioDeQueue,
+    IEC10X_PrioFindQueueHead,
+    IEC10X_HighestPrio, /* Get the highest Prio Queue*/
+    IEC10X_PrioInitQueue,
+    IEC10X_Prio_ClearQueue,
+    fly_send,
+    fly_set_time,
+    fly_get_time,
+    fly_get_station_info,
+    fly_get_station_temp,
+    fly_get_link_addr,
+
+#ifdef IEC10XLOCK
+    fly_lock,
+    fly_unlock
+#endif
+};
+
+iec_32u RegisterIec10x(void)
+{
+    return RegisterIEC10XMoudle(&fly);
+}

+ 9 - 23
User/app/iec104/PRIO_QUEUE_Iec10x.c → User/app/iec104/iec10x_prio_queue.c

@@ -1,22 +1,8 @@
-/*******************************************************************
-Copyright (C):
-File name    :    PRIO_QUEUE.C
-DESCRIPTION  :
-AUTHOR       :
-Version      :    1.0
-Date         :    2014/02/26
-Others       :
-History      :
-++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-1) Date: 2014/02/26         Author: ChenDajie
-   content:
-
-*******************************************************************/
-#include "PRIO_QUEUE_Iec10x.h"
-#include "common_datatype.h"
+#include "iec10x_prio_queue.h"
 #include "iec10x.h"
+#include "iec10x_conf.h"
+#include "iec10x_type.h"
 #include "includes.h"
-#include "project_var.h"
 #include <stdio.h>
 
 extern PIEC10X_T          IEC10X;
@@ -35,9 +21,9 @@ extern Iec10x_PrioQueue_T Iec10x_PrioQueueArray[IEC10X_PRIO_MAX];
 char IEC10X_HighestPrio(void)
 {
 
-    int     i, prio;
-    uint8_t flag = 0;
-    prio         = -1;
+    int    i, prio;
+    iec_8u flag = 0;
+    prio        = -1;
     // LOG("%s \r\n",__FUNCTION__);
     for (i = 0; i < IEC10X_PRIO_MAX; i++)
     {
@@ -61,7 +47,7 @@ void IEC10X_PrioInitQueue(Iec10x_PrioQueue_T *PrioQueue)
     PrioQueue->Tail       = NULL;
     PrioQueue->ElementNum = 0;
 }
-uint8_t IEC10X_PrioEnQueue(Iec10x_PrioQueue_T *QueueHdr, Iec10x_PrioNode_T *new_p)
+iec_8u IEC10X_PrioEnQueue(Iec10x_PrioQueue_T *QueueHdr, Iec10x_PrioNode_T *new_p)
 {
 
     new_p->Next = NULL;
@@ -78,7 +64,7 @@ uint8_t IEC10X_PrioEnQueue(Iec10x_PrioQueue_T *QueueHdr, Iec10x_PrioNode_T *new_
     }
 
     QueueHdr->ElementNum++;
-    // LOG("%s ElementNum(%d) \r\n",__FUNCTION__,QueueHdr->ElementNum);
+    LOG("%s ElementNum(%d) \r\n", __FUNCTION__, QueueHdr->ElementNum);
     return RET_SUCESS;
 }
 
@@ -88,7 +74,7 @@ Iec10x_PrioNode_T *IEC10X_PrioDeQueue(Iec10x_PrioQueue_T *QueueHdr)
     Iec10x_PrioNode_T *PrioNode_DeQ;
     if (QueueHdr->Header == NULL)
     {
-        // LOG("PrioDeQueue,error \r\n");
+        LOG("PrioDeQueue,error \r\n");
         return NULL;
     }
     PrioNode_DeQ = QueueHdr->Header;

+ 6 - 0
User/app/iec104/iec10x_prio_queue.h

@@ -0,0 +1,6 @@
+#ifndef __PRIO_QUEUE_IEC10X_H__
+#define __PRIO_QUEUE_IEC10X_H__
+
+#include "iec10x.h"
+
+#endif

+ 24 - 0
User/app/iec104/iec10x_type.h

@@ -0,0 +1,24 @@
+// #ifndef __int8_t_defined
+#define __int8_t_defined
+typedef signed char iec_8s;
+typedef short int   iec_16s;
+typedef int         iec_32s;
+#if __WORDSIZE == 64
+typedef long int iec_64s;
+#else
+__extension__ typedef long long int          iec_64s;
+#endif
+// #endif
+
+/* Unsigned.  */
+typedef unsigned char      iec_8u;
+typedef unsigned short int iec_16u;
+#ifndef ___uint32_t_defined
+typedef unsigned int iec_32u;
+#define ___uint32_t_defined
+#endif
+#if __WORDSIZE == 64
+typedef unsigned long int iec_64u;
+#else
+__extension__ typedef unsigned long long int iec_64u;
+#endif

+ 3 - 11
User/app/net/net.c

@@ -12,22 +12,16 @@ static err_t bms_test_process(int fd, void *data, int len)
     return 1;
 }
 
-static err_t bms_net_process(int fd, void *data, int len)
+static err_t bms_net_process(int fd)
 {
 
     Iec10x_Scheduled(fd);
     Iec104_StateMachine();
-    // INT8U response[2] = {0xBB, 0xAA};
-    // if ((len == 2) && (((INT8U *)data)[0] == 0xAA) && (((INT8U *)data)[1] == 0xBB))
-    // {
-    //     send(fd, (void *)response, sizeof(response), 0);
-    // }
     return 1;
 }
 
 void net_task(void)
 {
-    int                StaCount = 0;
     INT8U              buf[50];
     INT32S             ret    = 0;
     INT32S             sockfd = -1, newfd = -1;
@@ -35,8 +29,6 @@ void net_task(void)
     struct sockaddr_in svr_addr, clt_addr;
 
     INT16U Iec104_RecvLen;
-    int    err;
-    int    staid = StaCount++;
 
     svr_addr.sin_family      = AF_INET;
     svr_addr.sin_port        = htons(TCP_PORT);
@@ -78,8 +70,8 @@ void net_task(void)
                 newfd = -1;
                 break;
             }
-            bms_net_process(newfd, buf, ret);
-            Iec104_RecvLen = read(newfd, buf, 50);
+            bms_net_process(newfd);
+            Iec104_RecvLen = read(newfd, buf, 1500);
             if (Iec104_RecvLen <= 0 || Iec104_RecvLen > 1500)
             {
                 printf("the other side has been closed (%d).\n", Iec104_RecvLen);

+ 2 - 1
User/main.c

@@ -69,7 +69,8 @@ int main(void)
     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
     OSInit();
     bsp_init();
-    Stm32f103RegisterIec10x();
+    RegisterIec10x();
+
     /* init task */
     /* 创建一个启动任务(也就是主任务)。启动任务会创建所有的应用程序任务 */
     OSTaskCreateExt(init_task,                                   /* 启动任务函数指针 */

+ 2 - 1
User/main.h

@@ -2,6 +2,7 @@
 #define __MAIN_H
 
 #include "fly_param.h"
+#include "iec10x_type.h"
 #include "includes.h"
 #include "interface.h"
 #include "led.h"
@@ -15,5 +16,5 @@ void bsp_init(void);
 void init_task(void *pvParameters);
 void misc_task(void);
 
-uint32_t Stm32f103RegisterIec10x(void);
+iec_32u RegisterIec10x(void);
 #endif

+ 0 - 30
User/project_var.h

@@ -15,34 +15,4 @@ typedef signed long long   INT64S;
 #define SW_VERSION1    0
 #define SW_VERSION2    2
 
-#define WTP_SUPPORT_END_NUMBER 9
-
-#define RET_SUCESS 0
-#define RET_ERROR  1
-
-//#define IEC101_STM32
-#define IEC104_STM32
-//#define IEC104_STM32_FUJIAN_HX
-
-//#define SIM900_MODE_SERVER
-#define SIM900_MODE_CLIENT
-
-#define SERIAL_DEBUG
-
-#ifdef SERIAL_DEBUG
-#define log(...) printf(__VA_ARGS__)
-#define LOG(...) printf(__VA_ARGS__)
-#define LogInit()
-#define PRINT_FUNLINE printf("%s %d\r\n", __FUNCTION__, __LINE__)
-#else
-#define log(...)
-#define LOG(...)
-#define DumpHEX(buffer, len)
-#define PRINT_FUNLINE
-#endif
-
-#ifdef SERIAL_DEBUG
-void DumpHEX(INT8U *buffer, INT32U len);
-#endif
-
 #endif

+ 6 - 0
platformio.ini

@@ -18,9 +18,11 @@ build_flags =
   -IUser/bsp/can
   -IUser/bsp/dm9k
   -IUser/bsp/eth
+  -IUser/bsp/ext_sram
   -IUser/bsp/dwt
   -IUser/bsp/interface
   -IUser/bsp/iwdg
+  -IUser/bsp/nor_flash
   -IUser/bsp/sdio
   -IUser/bsp/spi
   -IUser/bsp/timer
@@ -102,6 +104,10 @@ src_filter = +<User/>
             ; -<MiddleWare/FATFS/option/ccsbcs.c>
             ; -<MiddleWare/FATFS/option/syscall.c>
             ; -<MiddleWare/FATFS/option/unicode.c>
+; debug_tool = cmsis-dap
+; upload_protocol = cmsis-dap
+; debug_tool = jlink
+; upload_protocol = jlink
 debug_tool = stlink
 upload_protocol = stlink
 ; upload_protocol = custom