📄 sst39vfxxx.c
字号:
/*
****************************************************************************************************
* File name: sst39vfxxx.c
* Description: This is a BF533 flash driver program.
****************************************************************************************************
* Company: WASION METERS GROUP
* Created by: Chiron.ylq
* Date: 2009.02.11
* Description: VisualDSP++ "Flash Programmer" flash driver for use with the
* ADSP-BF533 EZ-KIT Lite containing the STMicroelectronics sst39vf020 flash device.
****************************************************************************************************
*/
#ifndef __SST39VF020_C__
#define __SST39VF020_C__
/*
****************************************************************************************************
* INCLUDE FILES
****************************************************************************************************
*/
#include "sst39vfxxx.h"
//////////////////////////////////////////////////////////////
// ERROR_CODE SST_Setup_For_Flash(void)
//
// Perform necessary setup for the processor to talk to the
// flash such as external memory interface registers, etc.
//
//////////////////////////////////////////////////////////////
#define EBIU_AMGCTL 0xFFC00A00 /* Asynchronous Memory Global Control Register */
#define EBIU_AMBCTL0 0xFFC00A04 /* Asynchronous Memory Bank Control Register 0 */
#define EBIU_AMBCTL1 0xFFC00A08 /* Asynchronous Memory Bank Control Register 1 */
#define pEBIU_AMGCTL ((volatile unsigned short *)EBIU_AMGCTL)
#define pEBIU_AMBCTL0 ((volatile unsigned long *)EBIU_AMBCTL0)
#define pEBIU_AMBCTL1 ((volatile unsigned long *)EBIU_AMBCTL1)
ERROR_CODE SST_Setup_For_Flash(void)
{
// Using default setup (reset value)
*pEBIU_AMGCTL = 0x00F2;
*pEBIU_AMBCTL0 = 0xFFC2FFC2;
*pEBIU_AMBCTL1 = 0xFFC2FFC2;
return NO_ERR;
}
//////////////////////////////////////////////////////////////
// ERROR_CODE SST_Reset_Flash(void);
//
// Sends a "reset" command to the flash.
//
//////////////////////////////////////////////////////////////
ERROR_CODE SST_Reset_Flash(void)
{
*pSST_CMD_ADDR0 = 0xF0;
asm ("SSYNC;");
// reset should be complete
return NO_ERR;
}
//////////////////////////////////////////////////////////////
// ERROR_CODE SST_Product_ID (U8 *pManufacturerID, U8 *pDeviceID)
//
// Sends an "auto select" command to the flash which will allow
// us to get the manufacturer and device codes.
//
//////////////////////////////////////////////////////////////
ERROR_CODE SST_Product_ID (U8 *pManufacturerID, U8 *pDeviceID)
{
// send the auto select command to the flash
*pSST_CMD_ADDR0 = 0xAA;
asm ("SSYNC;");
*pSST_CMD_ADDR1 = 0x55;
asm ("SSYNC;");
*pSST_CMD_ADDR0 = 0x90;
asm ("SSYNC;");
// now we can read the codes
*pManufacturerID = (U8)(*((U16*)((0x0000 << 1) + SST_ROM_BASE)));
asm ("SSYNC;");
// we need to issue another command to get the part out
// of auto select mode so issue a reset which just puts
// the device back in read mode
SST_Reset_Flash();
// send the auto select command to the flash
*pSST_CMD_ADDR0 = 0xAA;
asm ("SSYNC;");
*pSST_CMD_ADDR1 = 0x55;
asm ("SSYNC;");
*pSST_CMD_ADDR0 = 0x90;
asm ("SSYNC;");
// now we can read the codes
*pDeviceID = (U8)(*((U16*)((0x0001 << 1) + SST_ROM_BASE)));
asm ("SSYNC;");
// we need to issue another command to get the part out
// of auto select mode so issue a reset which just puts
// the device back in read mode
SST_Reset_Flash();
// ok
return NO_ERR;
}
//////////////////////////////////////////////////////////////
// ERROR_CODE SST_Toggle_Bit(U32 ulOffset, U32 nTimeOut)
//
// Toggle the DQ6 bit in the flash to see when the operation
// is complete.
//
// Inputs: unsigned long ulOffset - offset in flash
//
//////////////////////////////////////////////////////////////
ERROR_CODE SST_Toggle_Bit(U32 ulOffset, U32 nTimeOut)
{
U16 *pDataAddr;
volatile U16 Data0, Data1;
pDataAddr = (U16 *)((ulOffset << 1) + SST_ROM_BASE);
asm ("SSYNC;");
for( ; nTimeOut > 0; nTimeOut--)
{
Data0 = *pDataAddr;
asm ("SSYNC;");
Data1 = *pDataAddr;
asm ("SSYNC;");
Data0 ^= Data1;
if(!(Data0 & (1 << 6)))
{
return NO_ERR;
}
};
SST_Reset_Flash();
// we can return
return POLL_TIMEOUT;
}
//////////////////////////////////////////////////////////////
// ERROR_CODE SST_Polling_Data(U32 ulOffset, U16 nData, U32 nTimeOut)
//
// Polls the DQ7 bit in the flash to see when the operation
// is complete.
//
// Inputs: unsigned long ulOffset - offset in flash
// unsigned short nData - data write in the flash
//
//////////////////////////////////////////////////////////////
ERROR_CODE SST_Polling_Data(U32 ulOffset, U16 nData, U32 nTimeOut)
{
U16 *pDataAddr;
volatile U16 Data0;
extern U16 Probe;
pDataAddr = (U16 *)((ulOffset << 1) + SST_ROM_BASE);
asm ("SSYNC;");
for( ; nTimeOut > 0; nTimeOut--)
{
Data0 = *pDataAddr;
asm ("SSYNC;");
Data0 ^= nData;
if(!(Data0 & (1 << 7)))
{
return NO_ERR;
}
};
SST_Reset_Flash();
// we can return
return POLL_TIMEOUT;
}
//////////////////////////////////////////////////////////////
// ERROR_CODE SST_Chip_Erase(void)
//
// Sends an "erase all" command to the flash.
//
//////////////////////////////////////////////////////////////
ERROR_CODE SST_Chip_Erase(void)
{
ERROR_CODE ErrorCode = NO_ERR; // tells us if there was an error erasing flash
SST_Reset_Flash();
*pSST_CMD_ADDR0 = 0xAA;
asm ("SSYNC;");
*pSST_CMD_ADDR1 = 0x55;
asm ("SSYNC;");
*pSST_CMD_ADDR0 = 0x80;
asm ("SSYNC;");
*pSST_CMD_ADDR0 = 0xAA;
asm ("SSYNC;");
*pSST_CMD_ADDR1 = 0x55;
asm ("SSYNC;");
*pSST_CMD_ADDR0 = 0x10;
asm ("SSYNC;");
// poll until the command has completed
ErrorCode = SST_Toggle_Bit(0x0000, SST_TO_70_MS);
// erase should be complete
return ErrorCode;
}
//////////////////////////////////////////////////////////////
// ERROR_CODE SST_Sector_Erase(U32 nSector)
//
// Sends an "erase sector" command to the flash.
//
//////////////////////////////////////////////////////////////
ERROR_CODE SST_Sector_Erase(U32 nSector)
{
U32 ulSectorOff;
U16 *pSectorAddr;
ERROR_CODE ErrorCode = NO_ERR; // tells us if there was an error erasing flash
SST_Reset_Flash();
// if the block is invalid just return
if ( nSector >= SST_SECTOR_NUM )
{
return INVALID_BLOCK;
}
ulSectorOff = nSector * SST_SECTOR_SIZE;
pSectorAddr = (U16 *)((ulSectorOff << 1) + SST_ROM_BASE);
*pSST_CMD_ADDR0 = 0xAA;
asm ("SSYNC;");
*pSST_CMD_ADDR1 = 0x55;
asm ("SSYNC;");
*pSST_CMD_ADDR0 = 0x80;
asm ("SSYNC;");
*pSST_CMD_ADDR0 = 0xAA;
asm ("SSYNC;");
*pSST_CMD_ADDR1 = 0x55;
asm ("SSYNC;");
// the last write has to be at an address in the sector
*pSectorAddr = 0x30;
asm ("SSYNC;");
// poll until the command has completed
ErrorCode = SST_Toggle_Bit(ulSectorOff, SST_TO_18_MS);
// block erase should be complete
return ErrorCode;
}
//////////////////////////////////////////////////////////////
// ERROR_CODE SST_Read_Byte(U32 ulOffset, U8 *pData)
//
// Read a value from an offset in flash.
//
// Inputs: unsigned long ulOffset - offset to read from
// int pnValue - pointer to store value read from flash
//
//////////////////////////////////////////////////////////////
ERROR_CODE SST_Read_Byte(U32 ulOffset, U8 *pData)
{
*pData = (U8)(*((U16*)((ulOffset << 1) + SST_ROM_BASE)));
asm ("SSYNC;");
// ok
return NO_ERR;
}
//////////////////////////////////////////////////////////////
// ERROR_CODE SST_Write_Byte(U32 ulOffset, U8 nValue)
//
// Write a value to an offset in flash.
//
// Inputs: unsigned long ulOffset - offset to write to
// int nValue - value to write
//
//////////////////////////////////////////////////////////////
ERROR_CODE SST_Write_Byte(U32 ulOffset, U8 nValue)
{
ERROR_CODE ErrorCode = NO_ERR; // tells us if there was an error erasing flash
U16 *pDataAddr;
pDataAddr = (U16 *)((ulOffset << 1) + SST_ROM_BASE);
asm ("SSYNC;");
*pSST_CMD_ADDR0 = 0xAA;
asm ("SSYNC;");
*pSST_CMD_ADDR1 = 0x55;
asm ("SSYNC;");
*pSST_CMD_ADDR0 = 0xA0;
asm ("SSYNC;");
*pDataAddr = (U16)nValue;
asm ("SSYNC;");
// poll until the command has completed
ErrorCode = SST_Polling_Data(ulOffset, nValue, SST_TO_14_US);
// ok
return ErrorCode;
}
//////////////////////////////////////////////////////////////
// ERROR_CODE SST_Read_Data(U32 ulOffset, U8 *pStr, U32 Num)
//
// Read multiple value from an offset in flash.
//
// Inputs: unsigned long ulOffset - offset to read from
// unsigned char *ptr - pointer to store value read from flash
// short num -
//
//////////////////////////////////////////////////////////////
ERROR_CODE SST_Read_Data(U32 ulOffset, U8 *pStr, U32 Num)
{
U32 i;
for(i = 0; i < Num; i++)
{
SST_Read_Byte(ulOffset++, pStr++);
}
return NO_ERR;
}
//////////////////////////////////////////////////////////////
// ERROR_CODE SST_Write_Data(U32 ulOffset, U8 *pStr, U32 Num)
//
// Write multiple value to an offset in flash.
//
// Inputs: unsigned long ulOffset - offset to write to
// unsigned char *ptr - pointer to source value write to flash
// short num - number of value to write
//
//////////////////////////////////////////////////////////////
ERROR_CODE SST_Write_Data(U32 ulOffset, U8 *pStr, U32 Num)
{
ERROR_CODE ErrorCode = NO_ERR; // tells us if there was an error erasing flash
U32 i;
for(i = 0; i < Num; i++)
{
ErrorCode = SST_Write_Byte(ulOffset++, *pStr++);
if( ErrorCode != NO_ERR)
break;
}
return ErrorCode;
}
#endif // __SST39VF020_C__
/*********************************************************************************************************
** End Of File
********************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -