⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main_serial.c

📁 ADI 公司的DSP ADSP21369 EZ-KIT LITE开发板的全部源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************************/
/*                                                                             */
/*   (C) Copyright 2004 - Analog Devices, Inc.  All rights reserved.           */
/*                                                                             */
/*    FILE:     M a i n ( )                                                    */
/*                                                                             */
/*    CHANGES:  1.00.0  - initial release    								   */
/*              1.00.1  - modified to support the AT25F2048 flash library      */
/*              1.00.2  - modified to support the M25P20 flash library         */
/*																			   */
/*    PURPOSE:  VisualDSP++ "Flash Programmer" flash driver for use with the   */
/*              ADSP-21xxx EZ-KIT Lite containing the Atmel AT25F2048 & M25P20 */
/*              flash device.   											   */
/*                                                                             */
/*******************************************************************************/


#ifdef __ADSP21375__
	#include <cdef21375.h>
	#include <def21375.h>
#elif __ADSP21369__
	#include <cdef21369.h>
	#include <def21369.h>
#elif (__ADSP21364__)
	#include <cdef21364.h>
	#include <def21364.h>
#elif (__ADSP21262__)
	#include <cdef21262.h>
	#include <def21262.h>
#endif

#include <stdio.h>
#include <stdlib.h>			//malloc 
#include <drivers\flash\util.h>
#include <drivers\flash\adi_m25p20.h>
#include <drivers\flash\adi_at25f2048.h>
#include <drivers\flash\Errors.h>
#include <sru.h>
#include <sysreg.h>

#define FLASH_START_ADDR	0x000000
#define BUFFER_SIZE			0x400
#define BAUD_RATE_DIVISOR 	100
#define	MAX_DEV				3
#define MAX_NUM_SECTORS 	4

//Flash Programmer commands
typedef enum
{
	FLASH_NO_COMMAND,		// 0
	FLASH_GET_CODES,		// 1
	FLASH_RESET,			// 2
	FLASH_WRITE,			// 3
	FLASH_FILL,				// 4
	FLASH_ERASE_ALL,		// 5
	FLASH_ERASE_SECT,		// 6
	FLASH_READ,				// 7
	FLASH_GET_SECTNUM,		// 8
	FLASH_GET_SECSTARTEND,	// 9
}enProgCmds;

//----- g l o b a l s -----//

char 			*AFP_Title ;						// EzKit info: ADSP-21xxx EZ-KIT Lite
char 			*AFP_Description;					// Device Description: Atmel m25p16
char			*AFP_DeviceCompany;					// Device Company
char 			*AFP_DrvVersion		= "2.00.0";		// Driver Version
char			*AFP_BuildDate		= __DATE__;		// Driver Build Date
enProgCmds 		AFP_Command 		= FLASH_NO_COMMAND;	// command sent down from the GUI
int 			AFP_ManCode 		= -1;			// 0x1F 	= Atmel
int 			AFP_DevCode 		= -1;			// 0x63 = m25p16, 0x60 = AT25F512
unsigned long 	AFP_Offset 			= 0x0;			// offset into flash
int 			*AFP_Buffer;						// buffer used to read and write flash
long 			AFP_Size 			= BUFFER_SIZE;	// buffer size
long 			AFP_Count 			= -1;			// count of locations to be read or written
long 			AFP_Stride 			= -1;			// stride used when reading or writing
int 			AFP_NumSectors 		= -1;			// number of sectors in the flash device
int 			AFP_Sector 			= -1;			// sector number
int 			AFP_Error 			= 0;			// contains last error encountered
bool 			AFP_Verify 			= FALSE;		// verify writes or not
unsigned long 	AFP_StartOff 		= 0x0;			// sector start offset
unsigned long 	AFP_EndOff 			= 0x0;			// sector end offset
int				AFP_FlashWidth		= 0x8;			// width of the flash device
int 			*AFP_SectorInfo;

bool bExit = FALSE; 								//exit flag

#ifdef __ADSP21375__
	static char *pEzKitTitle = "ADSP-21375 EZ-KIT Lite";
#elif __ADSP21369__
	static char *pEzKitTitle = "ADSP-21369 EZ-KIT Lite";
#elif (__ADSP21364__)
        static char *pEzKitTitle = "ADSP-21364 EZ-KIT Lite";
#elif (__ADSP21262__)
static char *pEzKitTitle = "ADSP-21262 EZ-KIT Lite";
#else
	#error "Error: Unknown Ez-Kit"
#endif


ADI_DEV_PDD_ENTRY_POINT *pADISPIFlashEntryPoint;

ADI_DEV_PDD_ENTRY_POINT *g_ADISPIFlashEntryPoint_PTR[MAX_DEV] = 
{
#if ( defined(__ADSP21262__) || defined(__ADSP21364__) )	
	&ADIM25P20EntryPoint,
#endif
	&ADIAT25F2048EntryPoint,
	&ADIAT25F2048EntryPoint
};

const unsigned int g_FlashDevCode[MAX_DEV] = 
{
#if ( defined(__ADSP21262__) || defined(__ADSP21364__) )	
	0x12,
#endif
	0x60,
	0x63 
};	

//----- c o n s t a n t   d e f i n i t i o n s -----//

// structure for flash sector information

typedef struct _SECTORLOCATION
{
 	unsigned long ulStartOff;
	unsigned long ulEndOff;
}SECTORLOCATION;

SECTORLOCATION SectorInfo[NUM_SECTORS];


//----- f u n c t i o n   p r o t o t y p e s -----//

ERROR_CODE OpenFlashDevice(void);
ERROR_CODE GetNumSectors(void);
ERROR_CODE AllocateAFPBuffer(void);
ERROR_CODE GetSectorMap(SECTORLOCATION *pSectInfo);
ERROR_CODE GetFlashInfo(void);
ERROR_CODE ProcessCommand(void);
ERROR_CODE FillData( unsigned long ulStart, long lCount, long lStride, int *pnData );
ERROR_CODE ReadData( unsigned long ulStart, long lCount, long lStride, int *pnData );
ERROR_CODE WriteData( unsigned long ulStart, long lCount, long lStride, int *pnData );
ERROR_CODE SetupForFlash(void);
void 	   FreeAFPBuffer(void);
void 	   InitPLL_SDRAM(void);


//------------- m a i n ( ) ----------------//

int main(void)
{
	SECTORLOCATION *pSectorInfo;

	// setup the device so the DSP can access it
	if (SetupForFlash() != NO_ERR)
		return FALSE;

	
	// The 21262 SPI Flash can support 2 devices M25P20 / AT25F2048
	// Since the system services does not support Sharc, we temporaily make a call
	// to device#1, get codes to determine if the device is correct. If the device
	// does not match then we open device #2 and proceed.
	// NOTE: Once the ssl is in place the DevMgr shall handle this.
	AFP_Error = OpenFlashDevice();


	// get the number of sectors for this device
	if( AFP_Error == NO_ERR )
	{
		AFP_Error = GetNumSectors();
	}

	if( AFP_Error == NO_ERR )
	{
		// malloc enough space to hold our start and end offsets
		pSectorInfo = (SECTORLOCATION *)malloc(AFP_NumSectors);
	}

	// allocate AFP_Buffer
	if( AFP_Error == NO_ERR )
	{
		AFP_Error = AllocateAFPBuffer();
	}

	// get sector map
	if( AFP_Error == NO_ERR )
	{
		AFP_Error = GetSectorMap(pSectorInfo);
	}

	// point AFP_SectorInfo to our sector info structure
	if( AFP_Error == NO_ERR )
	{
		AFP_SectorInfo = (int*)pSectorInfo;
	}

	// 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:");
       		asm("nop;");
			if ( FALSE )
				asm("jump AFP_BreakReady;");

		// Make a call to the ProcessCommand
		   AFP_Error = ProcessCommand();
	} 

	// Clear the AFP_Buffer
	FreeAFPBuffer();

	// Close the Device
	AFP_Error = (*pADISPIFlashEntryPoint).adi_pdd_Close(NULL);

	return TRUE;
}


//----------- P r o c e s s   C o m m a n d  ( ) ----------//
//
//  PURPOSE
//  	Process each command sent by the GUI based on the value in
//  	the AFP_Command.
//
// 	RETURN VALUE
//  	ERROR_CODE - value if any error occurs during Opcode scan
//  	NO_ERR     - otherwise
//
// 	CHANGES
//  	9-28-2005 Created

ERROR_CODE ProcessCommand()
{
	ERROR_CODE ErrorCode = 	NO_ERR; 		//return error code

	COMMAND_STRUCT CmdStruct;

	// switch on the command and fill command structure.
	switch ( AFP_Command )
	{

		// erase all
		case FLASH_ERASE_ALL:
			CmdStruct.SEraseAll.ulFlashStartAddr 	= FLASH_START_ADDR;	//FlashStartAddress
			ErrorCode = (ERROR_CODE) (*pADISPIFlashEntryPoint).adi_pdd_Control(NULL, CNTRL_ERASE_ALL, &CmdStruct );
			break;

		// erase sector
		case FLASH_ERASE_SECT:
			CmdStruct.SEraseSect.nSectorNum  		= AFP_Sector;		// Sector Number to erase
			CmdStruct.SEraseSect.ulFlashStartAddr 	= FLASH_START_ADDR;	// FlashStartAddress
			ErrorCode = (ERROR_CODE) (*pADISPIFlashEntryPoint).adi_pdd_Control(NULL, CNTRL_ERASE_SECT, &CmdStruct);
			break;

		// fill
		case FLASH_FILL:
			ErrorCode = FillData( AFP_Offset, AFP_Count, AFP_Stride, AFP_Buffer );
			break;

		// get manufacturer and device codes
		case FLASH_GET_CODES:
			CmdStruct.SGetCodes.pManCode 			= (unsigned long *)&AFP_ManCode;	// Manufacturer Code
			CmdStruct.SGetCodes.pDevCode 			= (unsigned long *)&AFP_DevCode;	// Device Code
			CmdStruct.SGetCodes.ulFlashStartAddr 	= FLASH_START_ADDR;
			ErrorCode = (ERROR_CODE) (*pADISPIFlashEntryPoint).adi_pdd_Control(NULL, CNTRL_GET_CODES, &CmdStruct);
			break;

		// get sector number based on address
		case FLASH_GET_SECTNUM:
			CmdStruct.SGetSectNum.ulOffset 			= AFP_Offset;	// offset from the base address
			CmdStruct.SGetSectNum.pSectorNum 		= (unsigned long *)&AFP_Sector;	//Sector Number
			ErrorCode = (ERROR_CODE) (*pADISPIFlashEntryPoint).adi_pdd_Control(NULL, CNTRL_GET_SECTNUM, &CmdStruct);
			break;

		// get sector number start and end offset
		case FLASH_GET_SECSTARTEND:
			CmdStruct.SSectStartEnd.nSectorNum 		= AFP_Sector;	// Sector Number
			CmdStruct.SSectStartEnd.pStartOffset 	= &AFP_StartOff;// sector start address
			CmdStruct.SSectStartEnd.pEndOffset	 	= &AFP_EndOff;	// sector end address
			ErrorCode = (ERROR_CODE) (*pADISPIFlashEntryPoint).adi_pdd_Control(NULL, CNTRL_GET_SECSTARTEND, &CmdStruct );
			break;

		// read
		case FLASH_READ:
			ErrorCode = ReadData( AFP_Offset, AFP_Count, AFP_Stride, AFP_Buffer );
			break;

		// reset
		case FLASH_RESET:
			CmdStruct.SGetCodes.ulFlashStartAddr 	= FLASH_START_ADDR; //Flash start address
			ErrorCode = (ERROR_CODE) (*pADISPIFlashEntryPoint).adi_pdd_Control(NULL, CNTRL_RESET, &CmdStruct);
			break;

		// write
		case FLASH_WRITE:
			ErrorCode = WriteData( AFP_Offset, AFP_Count, AFP_Stride, AFP_Buffer );
			break;

		// no command or unknown command do nothing
		case FLASH_NO_COMMAND:
		default:
			// set our error
			ErrorCode = UNKNOWN_COMMAND;
			break;
	}

	// clear the command
	AFP_Command = FLASH_NO_COMMAND;

	return(ErrorCode);
}


//----------- S e t u p F o r F l a s h ( ) ----------//
//
//  PURPOSE
// 		Perform necessary setup for the processor to talk to the
//		flash such as external memory interface registers, etc.
//
//  RETURN VALUE
//  	ERROR_CODE - value if any error occurs during Opcode scan
//  	NO_ERR     - otherwise

ERROR_CODE SetupForFlash()
{

#if ( defined(__ADSP21375__) || defined(__ADSP21369__) )
	SRU(SPI_CLK_O,DPI_PB03_I);
	SRU(HIGH,DPI_PBEN03_I);

	// for the flag pins to act as chip select
	SRU(FLAG4_O, DPI_PB05_I);
	SRU(HIGH, DPI_PBEN05_I);


	//First set flag 4 as an output
	sysreg_bit_set( sysreg_FLAGS, FLG4O ); //asm("bit set flags FLG4O;");
	sysreg_bit_set( sysreg_FLAGS, FLG4 ); //asm("bit set flags FLG4;"); //Logic high

#elif (__ADSP21364__) || (__ADSP21262__)

	//First set flag 0 as an output
	sysreg_bit_set( sysreg_FLAGS, FLG0O ); //asm("bit set flags FLG0O;");
	sysreg_bit_set( sysreg_FLAGS, FLG0 ); //asm("bit set flags FLG0;"); //Logic high
#endif

	*pSPIDMAC = 0;
	*pSPIBAUD = 0;
	*pSPIFLG = 0xF80;
	*pSPICTL = 0x400;

	return NO_ERR;
}


//----------- O p e n F l a s h D e v i c e ( ) ----------//
//
//  PURPOSE
//  	Opens the appropriate flash device.
//
// 	RETURN VALUE
//  	ERROR_CODE - value if any error occurs during Opcode scan
//  	NO_ERR     - otherwise
//
// 	CHANGES
//  	9-28-2005 Created

ERROR_CODE OpenFlashDevice(void)
{
	ERROR_CODE ErrorCode = 	NO_ERR; 		//return error code
	int n;
	
	for( n=0; n<MAX_DEV; n++)
	{

		pADISPIFlashEntryPoint = g_ADISPIFlashEntryPoint_PTR[n];
		
		// open the device	
	   	ErrorCode = (ERROR_CODE) (*g_ADISPIFlashEntryPoint_PTR[n]).adi_pdd_Open(	NULL,		// DevMgr handle
								n,										// pdd entry point
								NULL,									// device instance
								NULL,									// client handle callback identifier
					  			ADI_DEV_DIRECTION_BIDIRECTIONAL,		// data direction for this device
								NULL,			// DevMgr handle for this device
								NULL,			// handle to DmaMgr for this device
								NULL,			// handle to deferred callback service
								NULL );			// client's callback function
								
		// get flash manufacturer & device codes, title & desc
		if( ErrorCode == NO_ERR )
		{
			ErrorCode = GetFlashInfo();
		}				

		if( AFP_DevCode == g_FlashDevCode[n] )	
			break;
	}	
	
	return(ErrorCode);
}

		
//----------- A l l o c a t e A F P B u f f e r  ( ) ----------//
//
//  PURPOSE
//  	Allocate memory for the AFP_Buffer
//
// 	RETURN VALUE
//  	ERROR_CODE - value if any error occurs
//  	NO_ERR     - otherwise

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -