?? flash.c
字號:
#include <c8051f120.h> // SFR declarations
#include "flash.h"
#define LOG_START 0x04000L // Starting address of LOG
#define LOG_END 0x1F800L // Last address in LOG + 1
#define RECORD_LEN 8 // Record length in bytes
#define FLASH_PAGESIZE 1024 // Number of bytes in each FLASH page
#define COBANK 0xF0 // Bit mask for the high nibble of PSBANK
#define COBANK0 0x00 // These macros define the bit mask values
#define COBANK1 0x10 // for the PSBANK register used for
#define COBANK2 0x20 // selecting COBANK. COBANK should always
#define COBANK3 0x30 // be cleared then OR-Equaled (|=) with
// the proper bit mask to avoid changing
// the other bits in the PSBANK register
typedef union ULong { // Byte addressable unsigned long
unsigned long ULong;
unsigned int Int[2];
unsigned char Char[4];
} ULong;
// FLASH support routines
void FLASH_PageErase (unsigned long addr,char select);
void FLASH_Write (unsigned long dest, char* src, unsigned int numbytes,char select);
void FLASH_ByteWrite (unsigned long dest, char dat,char select);
void FLASH_Read ( char* dest, unsigned long src, unsigned int numbytes,char select);
unsigned char FLASH_ByteRead (unsigned long addr,char select);
/****************************************************************************
* 功 能:This function erases the FLASH page containing <addr>.
*---------------------------------------------------------------------------*
* 入口參數:addr: flash 地址
* select:flash 模式選擇: 0-->BANK_FLASH;1-->TEMP_FLASH.
* 出口參數:nill
****************************************************************************/
void FLASH_PageErase (unsigned long addr,char select)
{
char SFRPAGE_SAVE = SFRPAGE; // Preserve current SFR page
char PSBANK_SAVE = PSBANK; // Preserve current code bank
unsigned char EA_SAVE = EA; // Preserve interrupt state
ULong temp_addr; // Temporary ULong
char xdata * idata pwrite; // FLASH write/erase pointer
temp_addr.ULong = addr; // copy <addr> to a byte addressable
// unsigned long
// Extract address information from <addr>
pwrite = (char xdata *) temp_addr.Int[1];
// Extract code bank information from <addr>
PSBANK &= ~COBANK; // Clear the COBANK bits
if( temp_addr.Char[1] == 0x00){ // If the address is less than
// 0x10000, the Common area and
PSBANK |= COBANK1; // Bank1 provide a 64KB linear
// address space
} else { // Else, Bank2 and Bank3 provide
// a 64KB linear address space
if (temp_addr.Char[2] & 0x80){ // If bit 15 of the address is
// a ‘1’, then the operation should
PSBANK |= COBANK3; // target Bank3, else target Bank2
} else {
PSBANK |= COBANK2;
temp_addr.Char[2] |= 0x80;
pwrite = (char xdata *) temp_addr.Int[1];
}
}
SFRPAGE = LEGACY_PAGE;
EA = 0; // Disable interrupts
if(select) //select Scrachpad Memory area(0x20000-0x200FF)
PSCTL |= 0x04; //SFLE(PSCTL.2)=1
FLSCL |= 0x01; // Enable FLASH writes/erases
PSCTL |= 0x03; // MOVX erases FLASH page
*pwrite = 0; // Initiate FLASH page erase
FLSCL &= ~0x01; // Disable FLASH writes/erases
PSCTL = 0x00; // MOVX targets XRAM
EA = EA_SAVE; // Restore interrupt state
PSBANK = PSBANK_SAVE; // Restore current code bank
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
/**************************************************************************************
* 功 能:FLASH_Write: copies <numbytes> from <src> to the FLASH addressed by <dest>.
*-------------------------------------------------------------------------------------*
* 入口參數:dest: 目的flash 地址
* src: 源數據指針
* numbytes: 數據的個數
* select: flash 模式選擇: 0-->BANK_FLASH; 1-->TEMP_FLASH.
* 出口參數:nill
**************************************************************************************/
void FLASH_Write (unsigned long dest, char* src, unsigned int numbytes,char select)
{
unsigned int i; // Software Counter
for (i = 0; i < numbytes; i++) {
FLASH_ByteWrite( dest++, *src++,select);
}
}
/**************************************************************************************
* 功 能:FLASH_ByteWrite:writes <dat> to the FLASH byte addressed by <dest>.
*-------------------------------------------------------------------------------------*
* 入口參數:dest: 目的flash 地址
* dat: 寫入的數據
* select: flash 模式選擇: 0-->BANK_FLASH; 1-->TEMP_FLASH.
* 出口參數:nill
**************************************************************************************/
void FLASH_ByteWrite (unsigned long dest, char dat,char select)
{
char SFRPAGE_SAVE = SFRPAGE; // Preserve current SFR page
char PSBANK_SAVE = PSBANK; // Preserve current code bank
bit EA_SAVE = EA; // Preserve interrupt state
ULong temp_dest; // Temporary ULong
char xdata * idata pwrite; // FLASH write/erase pointer
temp_dest.ULong = dest; // copy <dest> to a byte
// addressable unsigned long
// Check if data byte being written is 0xFF
// There is no need to write 0xFF to FLASH since erased
// FLASH defaults to 0xFF.
if(dat != 0xFF){
// Extract address information from <dest>
pwrite = (char xdata *) temp_dest.Int[1];
// Extract code bank information from <addr>
PSBANK &= ~COBANK; // Clear the COBANK bits
if( temp_dest.Char[1] == 0x00){ // If the address is less than
// 0x10000, the Common area and
PSBANK |= COBANK1; // Bank1 provide a 64KB linear
// address space
} else { // Else, Bank2 and Bank3 provide
// a 64KB linear address space
if (temp_dest.Char[2] & 0x80){ // If bit 15 of the address is
// a ‘1’, then the operation should
PSBANK |= COBANK3; // target Bank3, else target Bank2
} else {
PSBANK |= COBANK2;
temp_dest.Char[2] |= 0x80;
pwrite = (char xdata *) temp_dest.Int[1];
}
}
SFRPAGE = LEGACY_PAGE;
EA = 0; // Disable interrupts
if(select) //select Scrachpad Memory area(0x20000-0x200FF)
PSCTL |= 0x04; //SFLE(PSCTL.2)=1
FLSCL |= 0x01; // Enable FLASH writes/erases
PSCTL |= 0x01; // MOVX writes FLASH byte
*pwrite = dat; // Write FLASH byte
FLSCL &= ~0x01; // Disable FLASH writes/erases
PSCTL = 0x00; // MOVX targets XRAM
}
EA = EA_SAVE; // Restore interrupt state
PSBANK = PSBANK_SAVE; // Restore current code bank
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
/**************************************************************************************
* 功 能: FLASH_Read: copies <numbytes> from FLASH addressed by <src> to <dest>.
*-------------------------------------------------------------------------------------*
* 入口參數:dest: 目的flash 地址指針
* src: 源flash 地址
* numbytes: 數據的個數
* select: flash 模式選擇: 0-->BANK_FLASH; 1-->TEMP_FLASH.
* 出口參數:nill
**************************************************************************************/
void FLASH_Read ( char* dest, unsigned long src, unsigned int numbytes,char select)
{
unsigned int i; // Software Counter
for (i = 0; i < numbytes; i++)
{
*dest++ = FLASH_ByteRead(src++,select);
}
}
/**************************************************************************************
* 功 能:FLASH_ByteRead: returns to the value of the FLASH byte addressed by <addr>.
*-------------------------------------------------------------------------------------*
* 入口參數:addr: 要讀取的flash 地址
* select: flash 模式選擇: 0-->BANK_FLASH; 1-->TEMP_FLASH.
* 出口參數:讀取的值
**************************************************************************************/
unsigned char FLASH_ByteRead (unsigned long addr,char select)
{
char SFRPAGE_SAVE = SFRPAGE; // Preserve current SFR page
char PSBANK_SAVE = PSBANK; // Preserve current code bank
ULong temp_addr; // Temporary ULong
char temp_char; // Temporary char
char code * idata pread; // FLASH read pointer
temp_addr.ULong = addr; // copy <addr> to a byte addressable
// unsigned long
// Extract address information from <addr>
pread = (char code *) temp_addr.Int[1];
// Extract code bank information from <addr>
PSBANK &= ~COBANK; // Clear the COBANK bits
if( temp_addr.Char[1] == 0x00){ // If the address is less than
// 0x10000, the Common area and
PSBANK |= COBANK1; // Bank1 provide a 64KB linear
// address space
} else { // Else, Bank2 and Bank3 provide
// a 64KB linear address space
if (temp_addr.Char[2] & 0x80){ // If bit 15 of the address is
// a ‘1’, then the operation should
PSBANK |= COBANK3; // target Bank3, else target Bank2
} else {
PSBANK |= COBANK2;
temp_addr.Char[2] |= 0x80;
pread = (char code *) temp_addr.Int[1];
}
}
if(select) //select Scrachpad Memory area(0x20000-0x200FF)
{
SFRPAGE = LEGACY_PAGE;
PSCTL |= 0x04; //SFLE(PSCTL.2)=1
}
temp_char = *pread; // Read FLASH byte
PSCTL = 0x00;
PSBANK = PSBANK_SAVE; // Restore current code bank
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
return temp_char;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -