C51 COMPILER V9.52.0.0   FLASHWRITE                                                        09/27/2018 10:52:46 PAGE 1   


C51 COMPILER V9.52.0.0, COMPILATION OF MODULE FLASHWRITE
OBJECT MODULE PLACED IN .\Output\FlashWrite.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE ..\User\Source\Hardware\FlashWrite.c LARGE OMF2 WARNINGLEVEL(0) BROWSE INCD
                    -IR(..\Library\FU68xx_Hardware_Driver\Include;..\User\Include) DEBUG PRINT(.\Listing\FlashWrite.lst) OBJECT(.\Output\Flas
                    -hWrite.obj)

line level    source

   1          /**************************** (C) COPYRIGHT 2017 Fortiortech shenzhen *****************************
   2          * File Name          : FlashWrite.c
   3          * Author             : Javin.Liang,Fortiortech  Appliction Team
   4          * Version            : V1.0
   5          * Date               : 2018-9-26
   6          * Description        : This file contains Flash initial function used for Motor Control.
   7          ***************************************************************************************************
   8          * All Rights Reserved
   9          **************************************************************************************************/
  10          #include <FU68xx_2.h>
  11          #include <Myproject.h>
  12          
  13          /*Private variables------------------------------------------------------------------------------*/
  14          MCUFlash xdata FlashTest;
  15          
  16          /*-------------------------------------------------------------------------------------------------
  17                  Function Name : uint16 GetLastDataFromFlash(uint8 xdata *BlockStartAddr)
  18                  Description   : 从目标FLASH扇区读取2字节最新写入的数据
  19                  Input         : uint8 xdata *BlockStartAddr：目标FLASH扇区
  20                  Output        : 读出的数据
  21          -------------------------------------------------------------------------------------------------*/
  22          uint16 Get2ByteFromFlash(uint8 xdata *BlockStartAddr)
  23          {
  24   1              uint8 xdata *FlashStartAddr = BlockStartAddr;
  25   1              uint8 i;
  26   1              uint16 tempofFlashData;
  27   1              
  28   1              for(i=0;i<64;i++)
  29   1              {
  30   2                      tempofFlashData = *(uint16 code *)(FlashStartAddr+2*i);
  31   2                      if(tempofFlashData==0)
  32   2                      {
  33   3                              if(i!=0)
  34   3                              {
  35   4                                      tempofFlashData = *(uint16 code *)(FlashStartAddr+2*(i-1));
  36   4                                      return tempofFlashData;
  37   4                              }
  38   3                              else
  39   3                              {
  40   4                                      return 0;
  41   4                              }
  42   3                      }
  43   2                      else
  44   2                      {
  45   3                              if(i==63)
  46   3                              {
  47   4                                      return tempofFlashData;
  48   4                              }
  49   3                      }       
  50   2              }
  51   1              return 0;
  52   1      }
  53          
C51 COMPILER V9.52.0.0   FLASHWRITE                                                        09/27/2018 10:52:46 PAGE 2   

  54          /*-------------------------------------------------------------------------------------------------
  55                  Function Name : void WriteData2Flash(uint8 xdata *BlockStartAddr,uint16 NewData2Flash)
  56                  Description   : 写入2个字节到FLASH
  57                  Input         : uint8 xdata *BlockStartAddr：目标FLASH地址  NewData2Flash：被写入数据
  58              Output                :     1:扇区未满,写入完成  0:扇区已满,写入失败
  59          -------------------------------------------------------------------------------------------------*/
  60          uint8 Write2Byte2Flash(uint8 xdata *BlockStartAddr,uint16 NewData2Flash)
  61          {
  62   1              uint8 xdata *FlashStartAddr = BlockStartAddr;
  63   1              uint16 tempofFlashData=0;
  64   1              uint16 tempofNewFlashData=0;
  65   1              uint8 i;
  66   1              
  67   1              tempofNewFlashData = NewData2Flash;
  68   1              
  69   1              for(i=0;i<64;i++)
  70   1              {
  71   2                      tempofFlashData = *(uint16 code *)(FlashStartAddr+2*i);
  72   2                      if(tempofFlashData==0)
  73   2                      {
  74   3                              tempofFlashData = tempofNewFlashData>>8;                            //待写入数据首字节
  75   3                              Flash_Sector_Write((FlashStartAddr+2*i),(uint8)tempofFlashData);
  76   3                              _nop_();
  77   3                              tempofFlashData = tempofNewFlashData&0x00ff;                        //待写入数据末字节
  78   3                              Flash_Sector_Write((FlashStartAddr+2*i+1),(uint8)tempofFlashData);
  79   3                              _nop_();                
  80   3      
  81   3                              return 1;
  82   3                      }
  83   2                      else
  84   2                      {
  85   3                              if(i==63)
  86   3                              {
  87   4                                      return 0;
  88   4                              }
  89   3                      }
  90   2              }
  91   1              return 0;
  92   1      }
  93          /*-------------------------------------------------------------------------------------------------
  94              Function Name :     uint8 Get4ByteFromFlash(uint8 xdata *BlockStartAddr)
  95                  Description   : 从目标FLASH扇区读取4字节最新写入的数据
  96                  Input         : uint8 xdata *BlockStartAddr：目标FLASH扇区
  97              Output                :     读出的数据
  98          -------------------------------------------------------------------------------------------------*/
  99          uint32 Get4ByteFromFlash(uint8 xdata *BlockStartAddr)
 100          {
 101   1              uint8 xdata *FlashStartAddr = BlockStartAddr;
 102   1              uint8 i;
 103   1              uint32 tempofFlashData;
 104   1              
 105   1              for(i=0;i<32;i++)
 106   1              {
 107   2                      tempofFlashData = *(uint32 code *)(FlashStartAddr + 4*i);
 108   2                      if(tempofFlashData==0)
 109   2                      {
 110   3                              if(i!=0)
 111   3                              {
 112   4                                      tempofFlashData = *(uint32 code *)(FlashStartAddr + 4*(i-1));
 113   4                                      return tempofFlashData;
 114   4                              }
 115   3                              else
C51 COMPILER V9.52.0.0   FLASHWRITE                                                        09/27/2018 10:52:46 PAGE 3   

 116   3                              {
 117   4                                      return 0;
 118   4                              }
 119   3                      }
 120   2                      else
 121   2                      {
 122   3                              if(i==31)
 123   3                              {
 124   4                                      return tempofFlashData;
 125   4                              }
 126   3                      }       
 127   2              }
 128   1              return 0;
 129   1      }
 130          
 131          /*-------------------------------------------------------------------------------------------------
 132                  Function Name : void Write4Byte2Flash(uint8 xdata *BlockStartAddr,uint32 NewData2Flash)
 133                  Description   : 写入4个字节到FLASH
 134                  Input         : uint8 xdata *BlockStartAddr：目标FLASH地址  NewData2Flash：被写入数据
 135              Output                :     无
 136          -------------------------------------------------------------------------------------------------*/
 137          void Write4Byte2Flash(uint8 xdata *BlockStartAddr,uint32 NewData2Flash)
 138          {
 139   1              uint8 i;
 140   1          uint8 xdata *FlashStartAddr = BlockStartAddr;
 141   1              uint32 tempofFlashData=0;
 142   1              uint32 tempofNewFlashData=0;    
 143   1                      
 144   1              tempofNewFlashData = NewData2Flash;
 145   1              
 146   1              for(i=0;i<32;i++)
 147   1              {
 148   2                      tempofFlashData = *(uint32 code *)(FlashStartAddr+4*i);
 149   2                      if(tempofFlashData==0)
 150   2                      {
 151   3                              tempofFlashData = (tempofNewFlashData>>24)&0x000000ff;
 152   3                              Flash_Sector_Write((FlashStartAddr+4*i),(uint8)tempofFlashData);
 153   3                              _nop_();
 154   3                              tempofFlashData = (tempofNewFlashData>>16)&0x000000ff;
 155   3                              Flash_Sector_Write((FlashStartAddr+4*i+1),(uint8)tempofFlashData);
 156   3                              _nop_();
 157   3                              tempofFlashData = (tempofNewFlashData>>8)&0x000000ff;
 158   3                              Flash_Sector_Write((FlashStartAddr+4*i+2),(uint8)tempofFlashData);
 159   3                              _nop_();
 160   3                              tempofFlashData = tempofNewFlashData&0x000000ff;
 161   3                              Flash_Sector_Write((FlashStartAddr+4*i+3),(uint8)tempofFlashData);
 162   3                              _nop_();                        
 163   3                              break;
 164   3                      }
 165   2                      else
 166   2                      {
 167   3                              /*若扇区已写满,擦除后重新写入数据*/
 168   3                  if(i==31)
 169   3                              {
 170   4                                      Flash_Sector_Erase(FlashStartAddr);
 171   4                                      _nop_();
 172   4                                      tempofFlashData = (tempofNewFlashData>>24)&0x000000ff;
 173   4                                      Flash_Sector_Write((FlashStartAddr),(uint8)tempofFlashData);
 174   4                                      _nop_();
 175   4                                      tempofFlashData = (tempofNewFlashData>>16)&0x000000ff;
 176   4                                      Flash_Sector_Write((FlashStartAddr+1),(uint8)tempofFlashData);
 177   4                                      _nop_();
C51 COMPILER V9.52.0.0   FLASHWRITE                                                        09/27/2018 10:52:46 PAGE 4   

 178   4                                      tempofFlashData = (tempofNewFlashData>>8)&0x000000ff;
 179   4                                      Flash_Sector_Write((FlashStartAddr+2),(uint8)tempofFlashData);
 180   4                                      _nop_();
 181   4                                      tempofFlashData = tempofNewFlashData&0x000000ff;
 182   4                                      Flash_Sector_Write((FlashStartAddr+3),(uint8)tempofFlashData);
 183   4                                      _nop_();                                
 184   4                              }
 185   3                      }
 186   2              }       
 187   1      }
 188          
 189          /*-------------------------------------------------------------------------------------------------
 190                  Function Name : void WriteOrReadFlashTest(uint8 xdata *BlockStartAddr,uint16 NewData2Flash)
 191                  Description   : FLASH读写数据测试,先把NewData2Flash写入到目标FLASH地址BlockStartAddr后,再将其
 192                              FLASH值读出来
 193                  Input         : uint8 xdata *BlockStartAddr：目标FLASH地址  NewData2Flash：被写入数据
 194              Output                :     无
 195          -------------------------------------------------------------------------------------------------*/
 196          void WriteOrReadFlashTest(uint8 xdata *BlockStartAddr,uint16 NewData2Flash)
 197          {
 198   1              uint8 FlashWriteStatus=0;
 199   1      
 200   1              if(FlashTest.FlashWrite)
 201   1              {
 202   2              FlashWriteStatus = Write2Byte2Flash(BlockStartAddr,NewData2Flash);
 203   2              if(!FlashWriteStatus)
 204   2              {
 205   3                  //扇区写满时擦除扇区再写入
 206   3                  Flash_Sector_Erase(BlockStartAddr); 
 207   3                  FlashWriteStatus = Write2Byte2Flash(BlockStartAddr,NewData2Flash);                          
 208   3              }                       
 209   2                      FlashTest.FlashWrite = 0;
 210   2              FlashTest.FlashRead  = 1;
 211   2              }
 212   1          
 213   1          if(FlashTest.FlashRead)
 214   1              {       
 215   2                      FlashTest.FlashRead2Byte = Get2ByteFromFlash(BlockStartAddr);
 216   2                      FlashTest.FlashRead = 0;
 217   2              }
 218   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   1155    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =     14      37
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----    ----
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
   EDATA SIZE       =   ----    ----
   HDATA SIZE       =   ----    ----
   XDATA CONST SIZE =   ----    ----
   FAR CONST SIZE   =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)
