flash.hpp
字号:
/*++
Copyright (c) 1995, 1996, 1997, 1998 Microsoft Corporation
Module Name:
flash.hxx
Abstract:
This module contains definitions for flash programming service class
Author:
Jun Liu 2-Aug-1996
Revision History:
Ported to Altoona platform 98-Jun-24
--*/
#ifndef _FLASH_HXX_
#define _FLASH_HXX_
#define HEADER_FILE
//#include "basetype.h"
//#include "romstdio.h"
//#include "alt_def.h"
extern "C" VOID
startcount(
);
extern "C" ULONG
checkcount(
);
// in main.cxx
//extern void Sleep (DWORD);
#define Sleep(x)
//
// Define flash memory command set
//
#define ERASE_SETUP 0x20
#define PROGRAM_SETUP 0x40
#define CLEAR_STATUS_REGISTER 0x50
#define READ_STATUS_REGISTER 0x70
#define INTELLIGENT_IDENTIFIER 0x90
#define ERASE_SUSPEND 0xb0
#define ERASE_RESUME 0xd0
#define READ_ARRAY 0xff
// Define status register
typedef struct _FM_STATUS {
UCHAR Reserved : 3;
UCHAR VPPS : 1;
UCHAR PS : 1;
UCHAR ES : 1;
UCHAR ESS : 1;
UCHAR WSMS : 1;
} FM_STATUS, *PFM_STATUS;
#define FM_WSMS 0x80
#define FM_ESS 0x40
#define FM_ES 0x20
#define FM_PS 0x10
#define FM_VPPS 0x08 // jshih ??? 0x04
#define FM_ERROR_MASK FM_VPPS|FM_PS|FM_ES|FM_ESS
//
// Time constants (unit is 54.918 ms)
//
//#define PROGRAM_TIMEOUT 20 // Timeout value of write operation
//#define ERASE_TIMEOUT 400 // Timeout value of block erase operation
#define PROGRAM_TIMEOUT 109836000 // Timeout value of write operation
#define ERASE_TIMEOUT 2196720000 // Timeout value of block erase operation
//
// Flash memory error condition
//
enum FM_ERROR {
Fm_No_Error,
Vpp_Low,
Program_Error,
Program_Timeout,
Command_Sequence_Error,
Block_Erase_Error,
Block_Erase_Timeout,
Address_Overflow
};
//
// Flash memory chip despcriptor
//
typedef struct _FLASH_DESCRIPTOR {
BYTE ManufactureCode;
BYTE DeviceCode;
UINT NumberOfBlocks;
UINT InterfaceWidth;
UINT TotalSize;
ADDRESS* BlockEnd;
} FLASH_DESCRIPTOR, *PFLASH_DESCRIPTOR;
//
// Chip description table
//
#define NUMBER_OF_SUPPORTED_DEVICE 3
#define INTEL_28F008SA 0
#define INTEL_28F016SV 1
#define INTEL_28F0640J5 2
extern ADDRESS BlockEnd_28F008SA[];
extern ADDRESS BlockEnd_28F016SV[];
extern ADDRESS BlockEnd_28F0640J5[];
extern FLASH_DESCRIPTOR _fd[];
template<class T> class CFlashData;
/////////////////////////////////////////////////////////////////////
///+
template<class T> class CFlashData
///
/// Flash ROM data base class
///
{
//protected:
public:
volatile T* m_ptr; // Address pointer
public:
//
// Constructors
//
// CFlashData(ADDRESS addr) { m_ptr = (T*)addr; };
CFlashData(T* addr) { m_ptr = addr; };
CFlashData() { m_ptr = 0; };
//
// Status checking functions
//
virtual BOOL IsStateMachineReady()
{ return ((PFM_STATUS)m_ptr)->WSMS ? TRUE : FALSE; };
virtual BOOL IsEraseSupspended()
{ return ((PFM_STATUS)m_ptr)->ESS ? TRUE : FALSE; };
virtual BOOL IsBlockEraseError()
{ return ((PFM_STATUS)m_ptr)->ES ? TRUE : FALSE; };
virtual BOOL IsProgramError()
{ return ((PFM_STATUS)m_ptr)->PS ? TRUE : FALSE; };
virtual BOOL IsVppLowError()
{ return ((PFM_STATUS)m_ptr)->VPPS ? TRUE : FALSE; };
virtual BOOL IsError()
{ return *m_ptr & FM_ERROR_MASK ? TRUE : FALSE; };
//
// Return the increment size
//
virtual UINT Increment() { return sizeof(T); };
//
// Return the chip bus width in bytes
//
virtual UINT ChipWidth() { return sizeof(T); };
//
// Write command to flash ROM
//
virtual void IssueCommand(BYTE command) { *m_ptr = (T)command; };
//
// Copy a unit of data to flash ROM
//
virtual void Copy(void* src) { *m_ptr = *(T*)src; };
//
// Read a byte from flash ROM
//
virtual BYTE ReadByte(UINT offset)
{
return *(BYTE*)(m_ptr+offset);
};
//
// Address manipulation operators
//
virtual T operator [] (UINT offset)
{
return *(m_ptr+offset);
}
virtual CFlashData& Assign(ADDRESS a)
{
m_ptr = (T*)a;
return *this;
};
virtual CFlashData& operator = (void* p)
{
m_ptr = (T*)p;
return *this;
};
virtual CFlashData& operator = (CFlashData& f)
{
m_ptr = f.m_ptr;
return *this;
};
virtual CFlashData& operator + (UINT offset)
{
m_ptr = (T*)(((UINT)(m_ptr)) + offset);
return *this;
};
virtual CFlashData& operator - (UINT offset)
{
m_ptr = (T*)(((UINT)(m_ptr)) - offset);
return *this;
};
virtual CFlashData& operator += (UINT offset) { return *this+offset; };
virtual CFlashData& operator -= (UINT offset) { return *this-offset; };
virtual CFlashData& operator ++ () { return *this+=1; };
virtual CFlashData& operator -- () { return *this-=1; };
//
// Address compare operators
//
virtual BOOL operator > (ADDRESS a)
{
return (ADDRESS)m_ptr > a ? TRUE : FALSE;
};
virtual BOOL operator < (ADDRESS a)
{
return (ADDRESS)m_ptr < a ? TRUE : FALSE;
};
virtual BOOL operator >= (ADDRESS a)
{
return (ADDRESS)m_ptr >= a ? TRUE : FALSE;
};
virtual BOOL operator <= (ADDRESS a)
{
return (ADDRESS)m_ptr <= a ? TRUE : FALSE;
};
virtual BOOL operator == (ADDRESS a)
{
return (ADDRESS)m_ptr == a ? TRUE : FALSE;
};
virtual BOOL operator != (ADDRESS a)
{
return (ADDRESS)m_ptr != a ? TRUE : FALSE;
};
virtual BOOL operator == (CFlashData& d)
{
return m_ptr == d.m_ptr ? TRUE : FALSE;
};
virtual BOOL operator > (CFlashData& d)
{
return m_ptr > d.m_ptr ? TRUE : FALSE;
};
virtual BOOL operator < (CFlashData& d)
{
return m_ptr < d.m_ptr ? TRUE : FALSE;
};
virtual BOOL operator >= (CFlashData& d)
{
return m_ptr >= d.m_ptr ? TRUE : FALSE;
};
virtual BOOL operator <= (CFlashData& d)
{
return m_ptr <= d.m_ptr ? TRUE : FALSE;
};
};
/////////////////////////////////////////////////////////////////////
///+
class CFlashData08 : public CFlashData<BYTE>
///
/// 8 bit flash ROM data class
///
{
public:
virtual CFlashData08& operator = (CFlashData08& f)
{
m_ptr = f.m_ptr;
return *this;
};
};
/////////////////////////////////////////////////////////////////////
///+
class CFlashData16 : public CFlashData<WORD>
///
/// 16 bit flash ROM data class
///
/// Note: One 16 bit wide chip.
///
{
};
/////////////////////////////////////////////////////////////////////
///+
class CFlashData32 : public CFlashData<DWORD>
///
/// 32 bit flash ROM data class
///
/// Note: Two 16 bit wide chip.
///
{
#ifdef ExtendStatus32
#undef ExtendStatus32
#endif
#define ExtendStatus32(x) ((x<<16)|(x))
public:
//
// Status checking functions
//
virtual BOOL IsStateMachineReady()
{
Sleep(1);
return (*m_ptr & ExtendStatus32(FM_WSMS)) == ExtendStatus32(FM_WSMS)
? TRUE : FALSE;
};
virtual BOOL IsEraseSupspended()
{
return (*m_ptr & ExtendStatus32(FM_ES))
? TRUE : FALSE;
};
virtual BOOL IsBlockEraseError()
{
return (*m_ptr & ExtendStatus32(FM_ES))
? TRUE : FALSE;
};
virtual BOOL IsProgramError()
{
return (*m_ptr & ExtendStatus32(FM_PS))
? TRUE : FALSE;
};
virtual BOOL IsVppLowError()
{
return (*m_ptr & ExtendStatus32(FM_VPPS))
? TRUE : FALSE;
};
virtual BOOL IsError()
{
return (*m_ptr & ExtendStatus32(FM_ERROR_MASK))
? TRUE : FALSE;
};
//
// Return the chip bus width in bytes
//
virtual UINT ChipWidth() { return sizeof(WORD); };
//
// Write command to flash ROM
//
virtual void IssueCommand(BYTE command)
{
Sleep(1);
*m_ptr = command | command << 8 | command << 16 | command << 24;
Sleep(1);
};
};
/////////////////////////////////////////////////////////////////////
///+
class CFlashData64 : public CFlashData<DWORD>
///
/// 64 bit flash ROM data class
///
/// Note: Four 16 bit wide chip.
///
{
#ifdef ExtendStatus32
#undef ExtendStatus32
#endif
#define ExtendStatus32(x) ((x<<24)|(x<<16)|(x<<8)|(x))
public:
//
// Status checking functions
//
virtual BOOL IsStateMachineReady()
{
return (*m_ptr & ExtendStatus32(FM_WSMS)) == ExtendStatus32(FM_WSMS) &&
(*(m_ptr+1) & ExtendStatus32(FM_WSMS)) == ExtendStatus32(FM_WSMS)
? TRUE : FALSE;
};
virtual BOOL IsEraseSupspended()
{
return (*m_ptr & ExtendStatus32(FM_ESS)) ||
(*(m_ptr+1) & ExtendStatus32(FM_ESS))
? TRUE : FALSE;
};
virtual BOOL IsBlockEraseError()
{
return (*m_ptr & ExtendStatus32(FM_ES)) ||
(*(m_ptr+1) & ExtendStatus32(FM_ES))
? TRUE : FALSE;
};
virtual BOOL IsProgramError()
{
return (*m_ptr & ExtendStatus32(FM_PS)) ||
(*(m_ptr+1) & ExtendStatus32(FM_PS))
? TRUE : FALSE;
};
virtual BOOL IsVppLowError()
{
return (*m_ptr & ExtendStatus32(FM_VPPS)) ||
(*(m_ptr+1) & ExtendStatus32(FM_VPPS))
? TRUE : FALSE;
};
virtual BOOL IsError()
{
return (*m_ptr & ExtendStatus32(FM_ERROR_MASK)) ||
(*(m_ptr+1) & ExtendStatus32(FM_ERROR_MASK))
? TRUE : FALSE;
};
//
// Return the increment size
//
virtual UINT Increment() { return 2*sizeof(DWORD); };
//
// Return the chip bus width in bytes
//
virtual UINT ChipWidth() { return sizeof(BYTE); };
//
// Write command to flash ROM
//
virtual void IssueCommand(BYTE command)
{
DWORD lcommand = command | command<<8 | command<<16 | command<<24;
*m_ptr = lcommand;
*(m_ptr+1) = lcommand;
};
//
// Copy a unit of data to flash ROM
//
virtual void Copy(void* src)
{
*m_ptr = *(DWORD*)src;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -