📄 copy2flash.c
字号:
/*****************************************************************************
* *
* ********** *
* ************ *
* *** *** *
* *** +++ *** *
* *** + + *** *
* *** + CHIPCON CC1010 *
* *** + + *** HAL - Copy2Flash *
* *** +++ *** *
* *** *** *
* *********** *
* ********* *
* *
*****************************************************************************
* *
*****************************************************************************
* Author: ROH *
*****************************************************************************
* Revision history: *
* *
* $Log: Copy2Flash.c,v $
* Revision 1.2 2003/02/25 09:24:11 tos
* Cosmetic change on header.
*
* Revision 1.1 2002/10/14 13:04:31 tos
* Initial version in CVS.
*
* *
****************************************************************************/
#include <chipcon/hal.h>
//----------------------------------------------------------------------------
// bool halCopy2Flash(...)
//
// Description:
// Copies _length_ bytes from the memory area pointed to by _src_ (in any
// memory space) to the memory area in CODE memory space pointed to by
// _flashPtr_. A 128 byte temporary buffer in the XDATA memory space must
// be pointed to by _ramBuffer_. None of the pointers (_flashPtr_, _src_
// or _ramBuffer_) need to be page-aligned (address mod 128=0) as with
// halFlashWritePage(...), neither does _length_ have to be a multiple of
// 128 bytes -- it can in fact be between 1 and 32768 bytes, although it
// goes without saying that it is unwise to overwrite the memory occupied
// by the code for this function. The result is undefined if the memory
// areas occupied by [_src_, _src_+_length_-1] and [_ramBuffer_,
// _ramBuffer+127] overlap.
// A verification of the data written is performed after programming - if
// this verification fails for some reason (flash page is write locked,
// flash programming failure) or some of the supplied arguments are
// invalid the function returns FALSE.
// All interrupts are turned off while this function executes.
// An empty Flash interrupt handler must be present in order for flash
// writing to return execution to the program, e.g:
//
// void isr_flash() interrupt INUM_FLASH {
// }
//
// Arguments:
// byte code* flashPtr
// Pointer to the destination of the write in code memory space.
// byte* src
// Pointer to the data source.
// word length
// The number of bytes to copy.
// byte xdata* ramBuffer
// A pointer to a 128 byte big buffer in XDATA memory space used to
// hold temporary data.
// word clkFreq
// The XOSC clock frequency in kHz.
//
// Return value:
// bool
// TRUE if the write was successful. False if programming/verification
// failed or the supplied arguments were invalid.
//----------------------------------------------------------------------------
bool halCopy2Flash(byte code* flashPtr, byte* src, word length, byte xdata* ramBuffer, word clkFreq) {
byte xdata* realRamBuf;
byte code* pFlash;
byte *pSrc;
byte movedRamLen, i, formerEA;
word len;
// Save interrupt enable and turn off
formerEA=EA;
EA=0;
FDIE=1;
// Calculate where the actual RAM buffer will reside and calculate how
// much data we have to move in RAM to make sure our buffer resides on
// a 128-byte page
realRamBuf=(word)ramBuffer&0x780;
movedRamLen=ramBuffer-realRamBuf;
// Move data in RAM to make room for the page-aligned buffer
if (movedRamLen) {
for (i=0; i<movedRamLen; i++)
realRamBuf[128+i]=realRamBuf[i];
}
// FLTIM >= clkFreq/800 -> FLTIM=clkFreq/(256*3)
FLTIM=(byte)(clkFreq>>8)/3;
FLCON=(FLCON&0xE0)|((word)realRamBuf>>7);
pSrc=src; pFlash=(word)flashPtr&0x7F80; len=length;
while (len) {
// Maybe we need to copy data from start of current flashpage into
// the RAM buffer
for (i=0; pFlash<flashPtr; i++)
realRamBuf[i]=*pFlash++;
// If the source is on the XDATA page used as RAM buffer we know that
// this must be the last page we will process. We change the pointer
// so that it refers to where we backed-up the contents of the start
// of the page used as RAM buffer
if (pSrc>=realRamBuf && pSrc<realRamBuf+128)
pSrc+=128;
// Copy length bytes from source or until end of flashpage, whichever
// occurs first.
for (; len && i<128; i++, len--, pFlash++)
realRamBuf[i]=*pSrc++;
// Perhaps we need to copy data at end of current flashpage into the
// RAM buffer.
for (; i<128; i++)
realRamBuf[i]=*pFlash++;
// Setup address
FLADR=(byte)((word)pFlash>>7)-1;
// Write to flash
FLCON|=0x10;
RESERVED|=0x80;
ENTER_IDLE_MODE();
}
// Move back data in RAM which was moved to make room for the page-aligned
// buffer
if (movedRamLen) {
for (i=0; i<movedRamLen; i++)
realRamBuf[i]=realRamBuf[128+i];
}
// Turn the interrupts back on
EA=formerEA;
// Verify
for (; length; length--)
if (*src++ != *flashPtr++)
return FALSE;
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -