📄 bf533_sst_flash_driver.c
字号:
/*
****************************************************************************************************
* File name: bf533_sst_flash_driver.c
* Description: This is a BF533 flash program download driver.
****************************************************************************************************
* Company: WASION METERS GROUP
* Created by: Chiron.ylq
* Date: 2009.02.12
* Description: VisualDSP++ "Flash Programmer" flash driver for use with the
* ADSP-BF533 EZ-KIT Lite containing the STMicroelectronics sst39vf020 flash device.
****************************************************************************************************
*/
#ifndef __BF533_SST_FLASH_DRIVER_C__
#define __BF533_SST_FLASH_DRIVER_C__
/*
****************************************************************************************************
* INCLUDE FILES
****************************************************************************************************
*/
#include "..\SST39VFXXX\src\sst39vfxxx.h"
#include "stdlib.h"
#include <string.h>
#include <stdio.h>
/*
*****************************************************************************
* MACRO PREDEFINE
*****************************************************************************
*/
#define TRUE 0x1
#define FALSE 0x0
#define BUFFER_SIZE 0x1000
#define NUM_SECTORS SST_SECTOR_NUM // number of sectors in the flash device
#define NUM_MODELS 4 // number of device models supported
#define DEBUG 0
/*
****************************************************************************
* DATA TYPES
* (Compiler Specific)
****************************************************************************
*/
// union for splitting word
typedef union
{
U16 loWord;
U16 hiWord;
}UNION_WORD;
// structure for flash sector information
typedef struct _SECTORLOCATION {
long lStartOff;
long lEndOff;
} SECTORLOCATION;
typedef struct _DEVICEINFO {
U8 ManufacturerCode;
U8 DeviceCode;
char *DeviceName;
U32 Size;
U16 NumSectors;
U32 SectorSize;
} DEVICEINFO;
const DEVICEINFO DeviceInfo[NUM_MODELS] = {
{0xBF, 0xD4, "SST39VF512", SIZE_64K, SIZE_64K/SST_SECTOR_SIZE, SST_SECTOR_SIZE}, // 512kbit
{0xBF, 0xD5, "SST39VF010", SIZE_128K, SIZE_128K/SST_SECTOR_SIZE, SST_SECTOR_SIZE}, // 1Mbit
{0xBF, 0xD6, "SST39VF020", SIZE_256K, SIZE_256K/SST_SECTOR_SIZE, SST_SECTOR_SIZE}, // 2Mbit
{0xBF, 0xD7, "SST39VF030", SIZE_512K, SIZE_512K/SST_SECTOR_SIZE, SST_SECTOR_SIZE} // 4Mbit
};
// Flash Programmer commands
typedef enum {
NO_COMMAND, // 0
GET_CODES, // 1
RESET, // 2
WRITE, // 3
FILL, // 4
ERASE_ALL, // 5
ERASE_SECT, // 6
READ, // 7
GET_SECTNUM, // 8
GET_SECSTARTEND // 9
} enProgCmds;
// function prototypes
ERROR_CODE SetupForFlash(void);
ERROR_CODE GetCodes(void);
ERROR_CODE ResetFlash(void);
ERROR_CODE WriteData( unsigned long ulStart, long lCount, long lStride, int *pnData );
ERROR_CODE FillData( unsigned long ulStart, long lCount, long lStride, int *pnData );
ERROR_CODE EraseFlash(void);
ERROR_CODE EraseBlock( int nBlock );
ERROR_CODE ReadData( unsigned long ulStart, long lCount, long lStride, int *pnData );
ERROR_CODE GetSectorNumber( unsigned long ulOffset, int *pnSector );
ERROR_CODE GetSectorStartEnd( long *lStartOff, long *lEndOff, int nSector );
// global data for use with the VisualDSP++ plug-in
char *AFP_Title = "SST39VFxxx Family Flash Programer Driver";
char *AFP_Description = "Presented by Chiron.ylq";
char *AFP_DeviceCompany = "Silicon Storage Technology, Inc."; // Device Company
char *AFP_DrvVersion = "1.00.0"; // Driver Version
char *AFP_BuildDate = __DATE__; // Driver Build Date
enProgCmds AFP_Command = NO_COMMAND;
int AFP_ManCode = -1;
int AFP_DevCode = -1;
unsigned long AFP_Offset = 0x00;
int *AFP_Buffer = NULL;
long AFP_Size = BUFFER_SIZE;
long AFP_Count = -1;
long AFP_Stride = -1;
int AFP_NumSectors = -1;
long AFP_SectorSize1 = -1; // size of main flash sectors
int AFP_SectorSize2 = -1; // size of boot flash sectors
int AFP_Sector = -1;
int AFP_Error = 0; // contains last error encountered
bool AFP_Verify = FALSE; // verify writes or not
long AFP_StartOff = 0x00; // sector start offset
long AFP_EndOff = 0x00; // sector end offset
int AFP_FlashWidth = 0x08; // width of the flash device
int *AFP_SectorInfo = NULL;
SECTORLOCATION SectorInfo[NUM_SECTORS];
const DEVICEINFO *gpDeviceInfo;
// flag telling us that the flash had to be reset
char reset = 0x0;
// exit flag
bool bExit = FALSE;
#if DEBUG == 1
char cDebug[100];
#endif
extern void Init_Clock(void);
extern void Init_UART(void);
extern void UART_TX(char *pStr, unsigned int nNum);
int main(void)
{
int i = 0;
Init_Clock();
Init_UART();
// by making AFP_Buffer as big as possible the plug-in can send and
// receive more data at a time making the data transfer quicker
//
// by allocating it on the heap the compiler does not create an
// initialized array therefore making the driver image smaller
// and faster to load
//
// we have modified the linker description file (LDF) so that the heap
// is large enough to store BUFFER_SIZE elements at this point
AFP_Buffer = (int *)malloc(BUFFER_SIZE);
// AFP_Buffer will be NULL if we could not allocate storage for the buffer
if ( AFP_Buffer == NULL )
{
// tell GUI that our buffer was not initialized
AFP_Error = BUFFER_IS_NULL;
}
// setup the flash so the DSP can access it
SetupForFlash();
//setup code so that flash programmer can just read memory instead of call GetCodes().
GetCodes();
// command processing loop
while ( !bExit )
{
// the plug-in will set a breakpoint at "AFP_BreakReady" so it knows
// when we are ready for a new command because the DSP will halt
//
// the jump is used so that the label will be part of the debug
// information in the driver image otherwise it may be left out
// since the label is not referenced anywhere
asm("AFP_BreakReady:");
if ( FALSE )
asm("jump AFP_BreakReady;");
// switch on the command
switch ( AFP_Command )
{
// get manufacturer and device codes
case GET_CODES:
AFP_Error = GetCodes();
break;
// reset
case RESET:
AFP_Error = ResetFlash();
break;
// write
case WRITE:
AFP_Error = WriteData( AFP_Offset, AFP_Count, AFP_Stride, AFP_Buffer );
break;
// fill
case FILL:
AFP_Error = FillData( AFP_Offset, AFP_Count, AFP_Stride, AFP_Buffer );
break;
// erase all
case ERASE_ALL:
AFP_Error = EraseFlash();
break;
// erase sector
case ERASE_SECT:
AFP_Error = EraseBlock( AFP_Sector );
break;
// read
case READ:
AFP_Error = ReadData( AFP_Offset, AFP_Count, AFP_Stride, AFP_Buffer );
break;
// get sector number based on address
case GET_SECTNUM:
AFP_Error = GetSectorNumber( AFP_Offset, &AFP_Sector );
break;
// get sector number start and end offset
case GET_SECSTARTEND:
AFP_Error = GetSectorStartEnd( &AFP_StartOff, &AFP_EndOff, AFP_Sector );
break;
// no command or unknown command do nothing
case NO_COMMAND:
default:
// set our error
AFP_Error = UNKNOWN_COMMAND;
break;
}
// clear the command
AFP_Command = NO_COMMAND;
}
// free the buffer if we were able to allocate one
if ( AFP_Buffer )
free( AFP_Buffer );
// all done
return 0;
}
//////////////////////////////////////////////////////////////
// ERROR_CODE SetupForFlash()
//
// Perform necessary setup for the processor to talk to the
// flash such as external memory interface registers, etc.
//
//////////////////////////////////////////////////////////////
ERROR_CODE SetupForFlash(void)
{
return SST_Setup_For_Flash();
}
//////////////////////////////////////////////////////////////
// ERROR_CODE GetCodes()
//
// Sends an "auto select" command to the flash which will allow
// us to get the manufacturer and device codes.
//
//////////////////////////////////////////////////////////////
ERROR_CODE GetCodes(void)
{
U32 i;
SST_Product_ID((void *)&AFP_ManCode, (void *)&AFP_DevCode);
AFP_ManCode &= 0x000000FF;
AFP_DevCode &= 0x000000FF;
if(AFP_ManCode != 0xBF)
{
return PROCESS_COMMAND_ERR; // device not supported
}
gpDeviceInfo = NULL;
for (i = 0; i < NUM_MODELS; i++)
{
if (AFP_DevCode == DeviceInfo[i].DeviceCode)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -