📄 norflsh.c
字号:
//****************************************************************************
//
// NORFLSH.C - Routines for erasing and programing the Intel 28F128J3 FLASH
// memory.
//
// Copyright (c) 2001 Cirrus Logic, Inc.
//
//****************************************************************************
#include "ep7312.h"
#include "lib7312.h"
//****************************************************************************
//
// FlashNumSectors returns the number of sectors in the FLASH memory.
//
//****************************************************************************
long
FlashNumSectors(void)
{
//
// The Intel 28F128J3A has 32 sectors.
//
return(32);
}
//****************************************************************************
//
// FlashSectorInfo returns the starting offset and length of the specified
// sector of the FLASH memory.
//
//****************************************************************************
long
FlashSectorInfo(long lSector, long *plSectorOffset, long *plSectorLength)
{
//
// Make sure the sector number is valid.
//
if((lSector < 0) || (lSector >= 32))
{
return(0);
}
//
// Sectors are 128K in length.
//
else
{
*plSectorOffset = 0x00020000 * lSector;
*plSectorLength = 0x00020000;
}
//
// Success.
//
return(1);
}
//****************************************************************************
//
// FlashWaitForProgram waits until bit 7 becomes one, indicating that the
// embedded program (be it a chip erase, sector erase, or data store program)
// has completed executing.
//
//****************************************************************************
static void
FlashWaitForProgram(unsigned long ulFlashBase)
{
unsigned short * volatile pusPtr = (unsigned short *)ulFlashBase;
unsigned short usTemp;
//
// Send the Read status register command
//
// *pusPtr = 0x0070;
//
// Read from the FLASH memory until bit 7 is one.
//
while(1)
{
//
// Read the status register from the FLASH memory.
//
usTemp = *pusPtr;
//
// Mask off all bits except for bit 7.
//
usTemp &= 0x0080;
//
// If bit 7 is one, then quit waiting.
//
if(usTemp == 0x0080)
{
break;
}
}
//
// Clear the status register.
//
*pusPtr = 0x0050;
}
//****************************************************************************
//
// FlashClearLockBits clears the block lock bits. B3 FLASH devices don't have
// a block lock bit feature, so this function should not be called when
// erasing or programming those devices. For the C3 FLASH devices,
// this function has to be called for each locked block. For the J3 FLASH
// devices, calling this function once will clear all set block lock bits.
// For both the C3 and J3 devices block lock bits should be cleared before
// attempting to erase or program.
//
//****************************************************************************
void
FlashClearLockBits(unsigned long ulFlashBase, long lSector)
{
unsigned short * volatile pusPtr = (unsigned short *)(ulFlashBase + lSector);
//
// Put the FLASH into read array mode.
//
*pusPtr = 0x00FF;
//
// Clear the Block lock bit before attempting to erase or program C3 or
// J3 FLASH devices.
//
*pusPtr = 0x0060;
*pusPtr = 0x00D0;
//
// Wait until the Clear Block lock bit command has completed.
//
FlashWaitForProgram(ulFlashBase);
//
// Put the FLASH into read array mode.
//
*pusPtr = 0x00FF;
}
//****************************************************************************
//
// FlashEraseChip erases the entire FLASH memory.
//
//****************************************************************************
void
FlashEraseChip(unsigned long ulFlashBase)
{
long lSector, lSectorOffset, lSectorLength;
//
// Loop through all the sectors on the FLASH memory.
//
for(lSector = 0; lSector < FlashNumSectors(); lSector++)
{
//
// Get the offset and length of this sector.
//
FlashSectorInfo(lSector, &lSectorOffset, &lSectorLength);
//
// Erase this sector.
//
FlashEraseSector(ulFlashBase, lSectorOffset);
}
}
//****************************************************************************
//
// FlashEraseSector erases a specific sector of the FLASH memory.
//
//****************************************************************************
void
FlashEraseSector(unsigned long ulFlashBase, long lSector)
{
unsigned short * volatile pusPtr = (unsigned short *)(ulFlashBase + lSector);
unsigned short * volatile pusPtrBase = (unsigned short *)ulFlashBase;
//
// Make sure the specified sector is within the range of the FLASH memory.
//
if((lSector < 0) || (lSector >= 0x00800000))
{
return;
}
//
// Write the erase command to the FLASH memory.
//
*pusPtr = 0x0020;
*pusPtr = 0x00D0;
//
// Wait until the sector erase has completed.
//
FlashWaitForProgram(ulFlashBase);
//
// Put the FLASH into read array mode.
//
*pusPtrBase = 0x00FF;
}
//****************************************************************************
//
// FlashProgramBlock writes a block of data into the FLASH memory.
//
//****************************************************************************
void
FlashProgramBlock(unsigned long ulFlashBase, long lOffset,
unsigned char *pucData, long lNumBytes)
{
unsigned short * volatile pusPtr = (unsigned short *)ulFlashBase;
long lIdx;
//
// Make sure the destination is within the range of the FLASH memory.
//
if((lOffset < 0) || (lOffset >= 0x00800000) ||
((lOffset + lNumBytes) > 0x00800000))
{
return;
}
//
// Program the array data, one word at a time.
//
for(lIdx = 0; lIdx < lNumBytes; lIdx += 4)
{
//
// Write the program word command.
//
*pusPtr = 0x0040;
*(pusPtr + (unsigned short)(lOffset + lIdx)) =
*((unsigned short *)(pucData + lIdx));
*(pusPtr + (unsigned short)(lOffset + lIdx +2)) =
*((unsigned short *)(pucData + lIdx +2));
//
// Wait until the data word has been programmed.
//
FlashWaitForProgram(ulFlashBase);
}
//
// Put the FLASH into read array mode.
//
*pusPtr = 0x00FF;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -