📄 at91sam7a3flash.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 : AT91SAM7A3Flash.c
//* Object : FLASH programmer for SAM7A3 product
//*
//* Creation : JPP 07/Mar/2005
//* 1.1 08/Sep/05 JPP : Suppress AT91F_LowLevelInit
//* V 1.3 14/nov/2005 JPP : Add checking
//*--------------------------------------------------------------------------------------
/* Include Standard c Libraries to allow stand alone compiling and operation */
#include <stdio.h>
#include <ioat91sam7A3.h>
#include "interface.h"
#include "driverConfig.h"
#include "AT91SAM7A3Flash.h"
//* Define Static variable
static char message[80];
static unsigned char RAMSectorData[FLASH_PAGE_SIZE_BYTE];
static int basseAddress ;
static unsigned int NbByte;
static int page,page_prev;
//*----------------------------------------------------------------------------
//* \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
//*----------------------------------------------------------------------------
int 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
while ((AT91C_BASE_MC->MC_FSR & AT91C_MC_EOL) != AT91C_MC_EOL ) {};
//* Check the result
if ( (AT91C_BASE_MC->MC_FSR & ( AT91C_MC_LOCKE ))!=0) return false;
return true;
}
//*----------------------------------------------------------------------------
//* \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)AT91C_IFLASH ) /FLASH_PAGE_SIZE_BYTE) & AT91C_PAGE_MASK;
//* copy the new value
for (i=0; (i < FLASH_PAGE_SIZE_BYTE) & (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 programming command
while ((AT91C_BASE_MC->MC_FSR & AT91C_MC_EOP) != AT91C_MC_EOP ) {};
//* Check the result
for (i=0; (i < FLASH_PAGE_SIZE_BYTE) & (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 ) /FLASH_PAGE_SIZE_BYTE) & AT91C_PAGE_MASK;
#ifdef DEBUG
FlMessageLog("+");
#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 0x%d\n",page_prev);
FlMessageLog(message);
//* Unlock current sector base address - curent address by sector size
if (!AT91F_Flash_Unlock(page_prev)) {
sprintf(message,"Cannot unlock page: %d\n",page_prev);
FlMessageBox(message);
FlErrorExit();
}
//* Write page and get status
if (!AT91F_Flash_Write( (page_prev*FLASH_PAGE_SIZE_BYTE)+ FLASH_ADDRESS, FLASH_PAGE_SIZE_BYTE , (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 < FLASH_PAGE_SIZE_BYTE; NbByte++)
{
RAMSectorData[NbByte] = ((unsigned char*) ((page*FLASH_PAGE_SIZE_BYTE)+basseAddress)) [NbByte];
}
//* set works page
page_prev = page ;
}
//* Write date
RAMSectorData[addr & FLASH_MASK_ADD_PAGE] = byte;
}
//*----------------------------------------------------------------------------
//* \fn FlashDriverInitialize
//* \brief Init the Flash driver
//*----------------------------------------------------------------------------
void FlashDriverInitialize(int argc, char const* argv[])
{ // check param
int i,status;
char val;
basseAddress = 0;
sprintf(message,"Downloader Version 1.2 08-SEP-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: offset to:%s",argv[1]);
//* Printf
FlMessageLog(message);
}
else
{
basseAddress = FLASH_ADDRESS;
status = false;
}
sprintf(message,"Download: AT91SAM7A3 At: 0x%X\n",basseAddress);
//* Get the Flash version
FlMessageLog(message);
//* Get the Flash version
sprintf(message,"Download: AT91SAM7A3 Version: 0x%X\n",*AT91C_MC_FVR);
FlMessageLog(message);
// get page
page = (((basseAddress )- (unsigned int) FLASH_ADDRESS ) /FLASH_PAGE_SIZE_BYTE )& AT91C_PAGE_MASK;;
page_prev = page;
for (NbByte = 0; NbByte < FLASH_PAGE_SIZE_BYTE; NbByte++)
{
RAMSectorData[NbByte] = ((unsigned char*) ( (page_prev*FLASH_PAGE_SIZE_BYTE)+basseAddress)) [NbByte];
}
if (status) basseAddress = 0;
// Register the flash write function.
FlRegisterWriteFunction(FlashWriteByte);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -