⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aduc702xflash.c

📁 FlashADuC702x 读写程序 FlashADuC702x 读写程序
💻 C
字号:
/**************************************************
 * Copyright 2004-2005 IAR Systems. All rights reserved.
 *
 * $Revision: 1.4 $
 **************************************************/

#include <ioaduc7020.h>
#include "Interface.h"


static int eraseFlags[124];

static int ErasePage (unsigned long adr) 
{

  unsigned long Flash_Status;

  // Start Erase Sector Command
  FEEADR = adr;                          //  Load Address to erase
  FEECON = 0x05;                         //  Erase Page Command
  Flash_Status = FEESTA;                 //  Load Status of Flash
  while ((Flash_Status & 4) == 1)        //  Wait until Flash Command    
  {                                      //  is completed
    Flash_Status = FEESTA;
  }
  if ((Flash_Status & 2) == 1)           //  Fail if Fail Bit set
  {
    return (1);                          //  Command Failed
  }
  else
  {   
    return (0);                          //  Command Passed
  }
}


/*
 *  Program Page in Flash Memory
 *    Parameter:      adr:  Page Start Address
 *                    sz:   Page Size
 *                    buf:  Page Data
 *    Return Value:   0 - OK,  1 - Failed
 */

static int ProgramPage (unsigned long addr, int halfword) {
  unsigned long Flash_Status;   
  

  
    // Start Program Command                 We write in half words
    FEEADR = addr;              //  Set Address to write too
    FEEDAT = halfword;  //  Load Data to write
    FEECON = 0x02;                       //  Execute Write
                                
    Flash_Status = FEESTA;
    while (Flash_Status & 4)      //  Wait until Flash command is
    if (Flash_Status & 2)         //  Fail if Fail Bit set 
      return (1);
 
 
return (0);                            //  Command Passed
}

// Write one byte to flash at addr.
// The bytes are buffered in sectorbuf. The sectorbuf is written to
// the flash when it overflows.
// If byte == -1 the flash loader framework signals a flush operation
// at the end of the input file.
static void FlashWriteByte(unsigned long addr, int byte)
{
  int index = 0;
  int ret;
  int saved_valid = 0;
  int saved_byte;
  unsigned long saved_addr;
  static unsigned short data = 0xffff;
  static unsigned long last_addr;
  static int first_time = 1;

  if (!first_time && byte != -1 && (addr != last_addr + 1))
  {
    // Non-consecutive address.
    if ((last_addr & 1) == 0)
    {
      // Last byte not written.
      saved_addr = addr;
      saved_byte = byte;
      saved_valid = 1;
      addr = last_addr + 1;
      byte = 0xff;
    }
    first_time = 0;
  }

again:
  if (byte == -1)
  {
    // Flush pending data.
    if (last_addr & 1)
    {
      // Already written.
      goto leave;
    }
    else
    {
      addr = last_addr + 1;
      byte = 0xff;
    }
  }
  else
  {
    // Next consecutive address.
    if (addr & 1)
    {
      data &= 0xff;
      data |= (byte  & 0xff) << 8;
      // Odd address, time to write.
    }
    else
    {
      // Even address, just store byte.
      data = (byte & 0xff) | 0xff00;
      goto leave;
    }
  }


  index = (addr & 0xffff) >> 9;
  
  if (index >= 124)
  {
    // Max size is 124*512 bytes (62 kbyte).
    FlMessageBox("Application is too large for flash.");
    FlErrorExit();
  }
  
  if(eraseFlags[index] != 1)
  {
    ret = ErasePage(index*0x200);
    if (ret == 1)
    {
      FlMessageBox("Erase page failed.");
      FlErrorExit();
    }
    eraseFlags[index] = 1;
  }
  
  ret = ProgramPage(addr, data);
  if (ret == 1)
  {
    FlMessageBox("Program page failed.");
    FlErrorExit();
  }
  data = 0xffff;

  if (saved_valid)
  {
    // Restore new byte and address.
    addr = saved_addr;
    byte = saved_byte;
    saved_valid = 0;
    goto again;
  }

leave:
  last_addr = addr;
}



void FlashDriverInitialize(int argc, char const* argv[])
{
  // Register the flash write function.
  FlRegisterWriteFunction(FlashWriteByte);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -