📄 am29lv800.c
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#include <am29lv800.h>
#include <bsp.h>
#ifdef DEBUG
#define ZONE_ERROR DEBUGZONE(0)
#define ZONE_WARN DEBUGZONE(1)
#define ZONE_FUNCTION DEBUGZONE(2)
#define ZONE_INFO DEBUGZONE(3)
#else
#define ZONE_ERROR 0
#define ZONE_WARN 0
#define ZONE_FUNCTION 0
#define ZONE_INFO 0
#endif // DEBUG
// AMD AM29LV800BB flash manufacturer and device IDs.
//
#define AMD_MFG_CODE 0x01
#define AMD_DEV_CODE 0x225B // We only support the AMD29LV800BB.
// AMD flash commands.
//
#define AMD_CMD_RESET 0xF0F0
#define AMD_CMD_AUTOSEL 0x9090
#define AMD_CMD_PROGRAM 0xA0A0
#define AMD_CMD_UNLOCK1 0xAAAA
#define AMD_CMD_UNLOCK2 0x5555
#define AMD_CMD_SECTERASE 0x8080
#define AMD_CMD_SECTERASE_CONFM 0x3030
// AMD flash access macros.
//
#define AMD_WRITE_USHORT(addr, data) *((volatile unsigned short *)(addr)) = (data)
//inline VOID WriteUshort(volatile USHORT * pus, USHORT us) {
//}
#define AMD_READ_USHORT(addr) *((volatile unsigned short *)(addr))
static DWORD g_dwFlashStart = 0;
#define AMD_CMD_ADDR (g_dwFlashStart + (0x5555 << 1))
#define AMD_UNLOCK_ADDR1 (g_dwFlashStart + (0x5555 << 1))
#define AMD_UNLOCK_ADDR2 (g_dwFlashStart + (0x2AAA << 1))
#define AMD_UNLOCK_CHIP() AMD_WRITE_USHORT(AMD_UNLOCK_ADDR1, AMD_CMD_UNLOCK1); \
AMD_WRITE_USHORT(AMD_UNLOCK_ADDR2, AMD_CMD_UNLOCK2)
#define AMD_WRITE_CMD(cmd) AMD_WRITE_USHORT(AMD_CMD_ADDR, cmd)
/*
@func BOOL | AMD28LV800_Wait | Checks for BIT6 to toggle (indicates flash has completed command).
@rdesc TRUE = Bit6 toggled, FASE = Bit6 didn't toggle.
@comm
@xref
*/
static BOOL AM29LV800_Wait(void) //Check if the bit6 toggle ends.
{
volatile USHORT Status, OldStatus;
OldStatus = *((volatile unsigned short *)g_dwFlashStart);
while(1)
{
Status = *((volatile unsigned short *)g_dwFlashStart);
if ((OldStatus & 0x40) == (Status & 0x40))
break;
if(Status & 0x20)
{
OldStatus = *((volatile unsigned short *)g_dwFlashStart);
Status = *((volatile unsigned short *)g_dwFlashStart);
if((OldStatus & 0x40) == (Status & 0x40))
return(FALSE);
else
return(TRUE);
}
OldStatus = Status;
}
return(TRUE);
}
/*
@func BOOL | AM29LV800_Init | Initialize the AMD AM29LV800BB flash.
@rdesc TRUE = Success, FALSE = Failure.
@comm
@xref
*/
BOOL AM29LV800_Init(UINT32 dwAddr)
{
UINT16 MfgCode = 0;
UINT16 DevCode = 0;
BOOLEAN bResult = FALSE;
DEBUGMSG(ZONE_FUNCTION, (TEXT("+AM29LV800_Init.\r\n")));
g_dwFlashStart = dwAddr;
// Put part in autoselect mode so we can identify it.
//
AMD_WRITE_CMD(AMD_CMD_RESET);
AMD_UNLOCK_CHIP();
AMD_WRITE_CMD(AMD_CMD_AUTOSEL);
// Read the manufacturer and device codes.
//
MfgCode = AMD_READ_USHORT(g_dwFlashStart);
DevCode = AMD_READ_USHORT(g_dwFlashStart + 2);
// Is this the expected AMD flash part?
//
if (MfgCode != AMD_MFG_CODE || DevCode != AMD_DEV_CODE)
{
DEBUGMSG(ZONE_ERROR, (TEXT("ERROR: AM29LV800_Init: Bad manufacturer or device code: Mfg=0x%x, Dev=0x%x.\r\n"), MfgCode, DevCode));
goto CleanUp;
}
// Reset the part into normal operating mode.
//
AMD_WRITE_CMD(AMD_CMD_RESET);
bResult = TRUE;
CleanUp:
DEBUGMSG(ZONE_FUNCTION, (TEXT("-AM29LV800_Init.\r\n")));
return(bResult);
}
/*
@func UINT32 | GetSectorSize | Returns the size of the specified sector.
Note that the AMD AM29LV800BB is a "bottom" boot block non-uniform sector size part.
@rdesc Sector size for the specified sector.
@comm
@xref
*/
static UINT32 GetSectorSize(UINT32 SectorNumber)
{
switch(SectorNumber)
{
case 0:
return(16 * 1024);
case 1:
return(8 * 1024);
case 2:
return(8 * 1024);
case 3:
return(32 * 1024);
default:
return(64 * 1024);
}
return(0);
}
/*
@func UINT32 | GetSectorAddress | Returns the starting address (flash byte offset) for the specified sector number.
Note that the AMD AM29LV800BB is a "bottom" boot block non-uniform sector size part.
@rdesc Starting address of the specified sector.
@comm
@xref
*/
static UINT32 GetSectorAddress(UINT32 SectorNumber)
{
UINT32 dwSectorAddr = 0;
switch(SectorNumber)
{
case 0:
dwSectorAddr = 0;
break;
case 1:
dwSectorAddr = 0x4000;
break;
case 2:
dwSectorAddr = 0x6000;
break;
case 3:
dwSectorAddr = 0x8000;
break;
default:
dwSectorAddr = ((SectorNumber - 3) * 0x10000);
break;
}
return(g_dwFlashStart + dwSectorAddr);
}
/*
@func UINT8 | GetSectorNumber | Returns the sector number corresponding to the flash address specified.
Note that the AMD AM29LV800BB is a "bottom" boot block non-uniform sector size part.
@rdesc Sector number.
@comm
@xref
*/
static UINT32 GetSectorNumber(UINT32 dwOffset)
{
UINT32 SectorNumber = 0;
if (dwOffset < 0x4000)
{
SectorNumber = 0;
}
else if (dwOffset < 0x6000)
{
SectorNumber = 1;
}
else if (dwOffset < 0x8000)
{
SectorNumber = 2;
}
else if (dwOffset < 0x10000)
{
SectorNumber = 3;
}
else
{
SectorNumber = 4 + (UCHAR)((dwOffset - 0x10000) / (64 * 1024));
}
return(SectorNumber);
}
/*
@func BOOL | EraseSector | Erases the specified flash sector.
@rdesc TRUE = Success, FALSE = Failure.
@comm
@xref
*/
static BOOL EraseSector(UINT32 dwSect)
{
UINT32 dwSectorAddr = GetSectorAddress(dwSect);
AMD_UNLOCK_CHIP();
AMD_WRITE_CMD(AMD_CMD_SECTERASE);
AMD_UNLOCK_CHIP();
AMD_WRITE_USHORT(dwSectorAddr, AMD_CMD_SECTERASE_CONFM);
// DEBUGMSG(1, (TEXT("EraseSector: Erasing sector 0x%x - address 0x%x\r\n"), dwSect, dwSectorAddr));
AM29LV800_Wait();
return(TRUE);
}
/*
@func BOOL | AM29LV800_WriteFlash | Writes the specified data to AMD AM29LV800 flash starting at the sector offset specified.
@rdesc TRUE = Success, FALSE = Failure.
@comm
@xref
*/
BOOL AM29LV800_WriteFlash(UINT32 dwOffset, PBYTE pData, UINT32 dwLength)
{
UINT32 dwByteOffset;
const UINT32 dwStatusInterval = 0x400000; // Print message every 4 MB
DEBUGMSG(ZONE_INFO, (TEXT("AM29LV800_WriteFlash: Writing 0x%x bytes from 0x%x to flash (offset 0x%x bytes)...\r\n"), dwLength, pData, dwOffset));
for (dwByteOffset = 0 ; dwByteOffset < dwLength ; dwByteOffset += sizeof(USHORT))
{
AMD_UNLOCK_CHIP();
AMD_WRITE_CMD(AMD_CMD_PROGRAM);
AMD_WRITE_USHORT((g_dwFlashStart + dwOffset + dwByteOffset), *(PUSHORT)(pData + dwByteOffset));
AM29LV800_Wait();
if ( ((dwByteOffset % dwStatusInterval) == 0) && (dwByteOffset != 0) ) {
DEBUGMSG(ZONE_INFO, (TEXT("AM29LV800_WriteFlash: Written 0x%x bytes total. 0x%x bytes from 0x%x to flash at 0x%x\r\n"), dwByteOffset, dwStatusInterval, pData + dwByteOffset - dwStatusInterval, g_dwFlashStart + dwOffset + dwByteOffset - dwStatusInterval));
}
}
return(TRUE);
}
/*
@func BOOL | AM29LV800_ReadFlash | Reads the specified number of bytes from flash starting at the specified offset.
@rdesc TRUE = Success, FALSE = Failure.
@comm
@xref
*/
BOOL AM29LV800_ReadFlash(UINT32 dwOffset, PBYTE pData, UINT32 dwLength)
{
PBYTE pReadPtr = (PBYTE)(g_dwFlashStart + dwOffset);
DEBUGMSG(ZONE_INFO && ZONE_FUNCTION, (TEXT("AM29LV800_ReadFlash: Reading 0x%x bytes from flash (offset 0x%x bytes)...\r\n"), dwLength, dwOffset));
memcpy(pData, pReadPtr, dwLength);
return(TRUE);
}
/*
@func BOOL | AM29LV800_EraseFlash | Erases AMD AM29LV800 flash starting at the sector offset specified.
@rdesc TRUE = Success, FALSE = Failure.
@comm
@xref
*/
BOOL AM29LV800_EraseFlash(UINT32 dwOffset, UINT32 dwLength)
{
UINT32 NumSects = 0;
UINT32 dwEraseLength = 0;
UINT32 StartSector = GetSectorNumber(dwOffset);
while(dwEraseLength < dwLength)
{
dwEraseLength += GetSectorSize(StartSector + NumSects);
++NumSects;
}
if (!NumSects)
{
NumSects = 1;
dwEraseLength = GetSectorSize(StartSector);
}
DEBUGMSG(ZONE_INFO, (TEXT("AM29LV800_EraseFlash: Requested 0x%x bytes, Erasing 0x%x sectors (0x%x bytes) (offset 0x%x bytes)...\r\n"), dwLength, NumSects, dwEraseLength, dwOffset));
AMD_WRITE_CMD(AMD_CMD_RESET);
while(NumSects--)
{
if (!EraseSector(StartSector + NumSects))
{
DEBUGMSG(ZONE_INFO, (TEXT("ERROR: AM29LV800_EraseFlash: EraseSector failed on sector 0x%x!\r\n"), NumSects));
return(FALSE);
}
}
AMD_WRITE_CMD(AMD_CMD_RESET);
return(TRUE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -