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

📄 at91sam7flash.c

📁 很好的资料
💻 C
字号:
//*--------------------------------------------------------------------------------------
//*      ATMEL Microcontroller Software Support  -  ROUSSET  -
//*--------------------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*--------------------------------------------------------------------------------------
//* File Name               : AT91SAM7Flash.c
//* Object                  : FLASH programmer for SAM7X product
//*
//* Creation                : JPP  04/Oct/2005
//* V 1.1 14/Nov/2005  JPP  : Change comment
//*--------------------------------------------------------------------------------------

/* Include Standard c Libraries to allow stand alone compiling and operation */
#include <stdio.h>
#include <ioat91sam7x256.h>

#include "interface.h"
#include "driverConfig.h"

#define true	-1
#define false	0

/*-------------------------------*/
/* Flash Status Field Definition */
/*-------------------------------*/
#define  FLASH_PAGE_SIZE_BYTE_MAX	256

#define  FLASH_BASE_ADDRESS		0x00100000
#define AT91C_PAGE_MASK       ((unsigned int) 0x0000FFFF ) // NB maximun pages
#define AT91C_MC_CORRECT_KEY  ((unsigned int) 0x5A << 24)  // (MC) Correct Protect Key


static char message[70];
static unsigned char RAMSectorData[FLASH_PAGE_SIZE_BYTE_MAX];
static int basseAddress ;
static unsigned int NbByte;
static int page,page_prev;

static unsigned int FlashPageSize;
#define STANDARD_DEVICE	 0
#define SAM7A3_DEVICE	 1
#define AT91C_MC_EOL          ((unsigned int) 0x1 <<  1) // (MC) End Of Lock/Unlock Flag
static unsigned int Device_type;

//*----------------------------------------------------------------------------
//* \fn    AT91F_Flash_Ready
//* \brief Wait the flash ready
//* \ On AT91SAM7A3 SAM7A3C_MC_EOP = AT91C_MC_FRDY
//*----------------------------------------------------------------------------
void AT91F_Flash_Ready (void)
{
    //* Wait the and of command
     while ((AT91C_BASE_MC->MC_FSR & AT91C_MC_FRDY) != AT91C_MC_FRDY ) {};
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_Flash_Unlock
//* \brief Clear the Non Volatile Memory Bits and set at 1 FSR bit=0
//* \input page number (0-1023) a same region have some page (16)
//* \output Region
//*----------------------------------------------------------------------------
void AT91F_Flash_Unlock(unsigned int Flash_Lock_Page)
{    //* Write the Errase All command
        AT91C_BASE_MC->MC_FCR = AT91C_MC_CORRECT_KEY | AT91C_MC_FCMD_UNLOCK | (AT91C_MC_PAGEN & (Flash_Lock_Page << 8) ) ;
   //* Wait the and of command
   if (Device_type == STANDARD_DEVICE)
   {
        AT91F_Flash_Ready();
   } else {
        while ((AT91C_BASE_MC->MC_FSR & AT91C_MC_EOL) != AT91C_MC_EOL ) {};
   }
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_Flash_Write
//* \brief Write in one Flash page located in AT91C_IFLASH,  size in 32 bits
//* \input Flash_Address: start at 0x0010 0000 size: in byte
//*----------------------------------------------------------------------------
int AT91F_Flash_Write( unsigned int Flash_Address ,int size ,unsigned int * buff)
{    //* set the Flasc controler base address
    AT91PS_MC ptMC = AT91C_BASE_MC;
    unsigned int i, page;
    unsigned int * Flash, * Dest, * source;
//    sprintf(message,"Download:add %x\n",Flash_Address);
//    FlMessageLog(message);

    //* init flash pointer
        Flash = (unsigned int *) Flash_Address;
        Dest = Flash;
        source = buff;
    //* Get the Flasg page number
        page = ((Flash_Address - (unsigned int)FLASH_BASE_ADDRESS ) /FlashPageSize) & AT91C_PAGE_MASK;
   //* copy the new value
	for (i=0; (i < FlashPageSize) & (size > 0) ;i++, Flash++,buff++,size-=4 ){
	//* copy the flash to the write buffer ensure that code generation
	    *Flash=*buff;
	}
    //* Write the Errase_All command
        ptMC->MC_FCR = AT91C_MC_CORRECT_KEY | AT91C_MC_FCMD_START_PROG | (AT91C_MC_PAGEN & (page <<8)) ;
    //* Wait the end of command
         AT91F_Flash_Ready();
    //*
    //* Check the result
       	for (i=0; (i < FlashPageSize) & (size > 0) ;i++, Dest++,source++,size-=4 ){
       	//* copy the flash to the write buffer ensure that code generation
            if ( *Dest!=*source) {
				sprintf(message,"Error At 0x%X read 0x%X but write 0x%X",Dest, *Dest,*source);
		        FlMessageLog(message);
	        }
     	}

        if ( (ptMC->MC_FSR & ( AT91C_MC_PROGE | AT91C_MC_LOCKE ))!=0) return false;

  return true;
}
//*----------------------------------------------------------------------------
//* \fn    FlashWriteByte
//* \brief The bytes are buffered in sectorbuf. The sectorbuf is written to
//*        the flash when it overflows.
//* \input Flash_Address: start at 0x0010 0000 size: in byte
//*----------------------------------------------------------------------------
static void FlashWriteByte(unsigned long  addr, int byte)
{
  // get page
  page  = ((basseAddress + addr) - FLASH_BASE_ADDRESS ) / FlashPageSize;
#ifdef DEBUG
       sprintf(message,"val 0x%X ",addr);
       FlMessageLog(message);
#endif
  if (byte == -1)
  { // Flush pending data.
     page = page_prev + 1 ;
  } // end flush

  //* Check if page must write
  if (page != page_prev)
  {// New Page
           sprintf(message,"Download:page %d\n",page_prev);
           FlMessageLog(message);
        //* Unlock current sector base address - curent address by sector size
        AT91F_Flash_Unlock(page_prev);
        //* Write page and get status
        if (!AT91F_Flash_Write( (page_prev*FlashPageSize)+ FLASH_BASE_ADDRESS, FlashPageSize , (unsigned int *) &RAMSectorData)) {
            sprintf(message,"Flasher cannot write page: %d\n",page_prev);
            FlMessageBox(message);
            FlErrorExit();
        }
 	// Copy Rom page to RAM page
        for (NbByte = 0; NbByte < FlashPageSize; NbByte++)
        {
           RAMSectorData[NbByte] = ((unsigned char*) ((page*FlashPageSize)+basseAddress)) [NbByte];
        }
        //* set works page
        page_prev = page ;
   }
   //* Write data
    RAMSectorData[ addr & (FlashPageSize-1)] = byte;
}

//*----------------------------------------------------------------------------
//* \fn    FlashDriverInitialize
//* \brief Init the Flash driver
//*----------------------------------------------------------------------------
void FlashDriverInitialize(int argc, char const* argv[])
{    // check param

    int i,status,Device;
    char val;
    basseAddress = 0;

    sprintf(message,"Downloader Version 1.3 14-NOV-2005\n");
    FlMessageLog(message);

    // get the Flash base address
    if (argc >= 2){
        NbByte=sprintf(message,"%s",argv[1]);
        if (message[1]!='x'){
            FlMessageBox("Param ERROR ! must be 0xYYYYYY");

         FlErrorExit();
        }
        for (i=0 ; NbByte > 1 ; NbByte--, i++){
            val = message[NbByte-1] & 0x0f;
            if ( message[NbByte-1]  > 0x40)  val += 9;
            basseAddress +=  val << (4 *i) ;
        }
        status = true;
        sprintf(message,"Download: AT91SAM7 At:%s",argv[1]);
    }
    else
    {
        basseAddress = FLASH_BASE_ADDRESS;
        sprintf(message,"Download1: AT91SAM7 At: 0x%X\n",basseAddress);
        status = false;
    }
    FlMessageLog(message);
    // Get chip ID for Flash Page size
    Device = *AT91C_DBGU_CIDR;
    if ((Device & 0x0000FF00) < 0x0700)
    {
	FlashPageSize=128;
    } else {
	FlashPageSize=256;
    }
    // * check if SAM7A3 device
    Device_type = STANDARD_DEVICE;
    if ((Device == 0x170A0940) || (Device == 0x260A0941))
    {
	Device_type = SAM7A3_DEVICE;
    }

    sprintf(message,"Device ID: 0x%X\n",Device);
    //* Get the Flash version
    FlMessageLog(message);
    sprintf(message,"Page Size: %d\n",FlashPageSize);
    //* Get the Flash version
    FlMessageLog(message);

   // get page
    page  =  (((basseAddress )- (unsigned int) FLASH_BASE_ADDRESS ) /FlashPageSize)&AT91C_PAGE_MASK;
    page_prev = page;
    for (NbByte = 0; NbByte < FlashPageSize; NbByte++)
    {
        RAMSectorData[NbByte] = ((unsigned char*) ( (page_prev*FlashPageSize)+basseAddress)) [NbByte];
    }
    if (status) basseAddress = 0;

    // Register the flash write function.
    FlRegisterWriteFunction(FlashWriteByte);
}

⌨️ 快捷键说明

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