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

📄 download_testdataflash.c

📁 at91rm9200 的rom程序
💻 C
字号:
//*--------------------------------------------------------------------------------------
//*      ATMEL Microcontroller Software Support  -  ROUSSET  -
//*--------------------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*--------------------------------------------------------------------------------------
//* File Name           : download.c
//* Object              :
//* Translator          :
//* 1.0 19/03/01 HI		: Creation
//*--------------------------------------------------------------------------------------
#include "services/obj_romboot.h"
#include "appli/includes/main.h"
#include "appli/includes/eeprom.h"
#include "appli/includes/dataflash.h"
#include "appli/includes/Download.h"




/* ================================================================ */
/* ====================   External Variables  ===================== */
/* ================================================================ */
extern AT91S_RomBoot const *pAT91;
extern int SizeToDownload;

static void MemoryControllerInit(const AT91PS_MEMCDesc);
static int IsBootValid(unsigned char *);

#include "appli.h"

//*--------------------------------------------------------------------------------------
//* Function Name       : download_file_to_eeprom
//* Object              : download in E2PROM an image file
//* Input Parameters    :
//* Output Parameters   :
//* Functions called    :
//*--------------------------------------------------------------------------------------
int download_file_to_dataflash ( void )
{
	volatile int 	 index = 0;
    int 	         nb_byte = 0;
    int 			 size = 0;
	volatile unsigned char rxBuffer[128];

	SizeToDownload = (int)((EEPROM_TAB[23]<< 8) + EEPROM_TAB[22]);

    //* while not the end of file
    while(index <  SizeToDownload)
	{
		if (index + sizeof(rxBuffer) < SizeToDownload)
			size = sizeof(rxBuffer);
		else
			size = 	SizeToDownload - index;		
		for(nb_byte = 0; nb_byte < sizeof(rxBuffer); nb_byte++)
			rxBuffer[nb_byte] = EEPROM_TAB[index + nb_byte];
		dataflash_waitReady(200000);
		dataflash_write((unsigned char *)rxBuffer, index, size);

		index += sizeof(rxBuffer);
    }
    return ( TRUE ) ;
}




//* ================================================================
//*Download
//* ================================================================
//*----------------------------------------------------------------------------
//* Function Name       : MemoryControler_Init()
//* Object              :
//* Input Parameters    :
//* Output Parameters   :
//*----------------------------------------------------------------------------
static void MemoryControllerInit(const AT91PS_MEMCDesc pMemc)
{
	pMemc->memc_base->MEMC_PUIA[0] = 0;
	pMemc->memc_base->MEMC_PUP = 0;
	pMemc->memc_base->MEMC_PUER = 0;	//* Memmory controller protection unit disable
	pMemc->memc_base->MEMC_ASR = 0;
	pMemc->memc_base->MEMC_AASR = 0;

	pMemc->memc_base->MEMC_EBIC.EBIC_CSA = EBIC_CS0_SMC;
	pMemc->memc_base->MEMC_EBIC.EBIC_CR = (EBIC_PU_EN | EBIC_HOLD_DIS);
	pMemc->memc_base->MEMC_EBIC.EBIC_SMC.SMC_CSR[0] =  SMC_DBW_8;
}


//*--------------------------------------------------------------------------------------
//* Function Name       : ExternMemBoot
//* Object              : load the external memory on CS0 in SRAM at BASE_LOAD_ADDRESS
//* Input Parameters    :
//* Output Parameters   :
//*--------------------------------------------------------------------------------------
int ExternMemBoot(void)
{
	int i = 0;
    u_char *pt_sram = (u_char *)BASE_LOAD_ADDRESS;
    u_char *pt_ebi = (u_char *)BASE_EBI_CS0_ADDRESS;

	for(i = 0; i <  SizeToDownload; i++)
		*pt_sram++ = *pt_ebi++;
	return 1;
}


//*--------------------------------------------------------------------------------------
//* Function Name       : EepromBoot
//* Object              : load the content of the Eeprom to SRAM at BASE_LOAD_ADDRESS
//* Input Parameters    :
//* Output Parameters   :
//*--------------------------------------------------------------------------------------
int EepromBoot(void)
{
    char 	*pt_sram = (char *)BASE_LOAD_ADDRESS;

	EEPROM_SeqRead((const AT91PS_TWIDesc)&pAT91->TWI_DESC, 0, (char *)pt_sram,  SizeToDownload);

    return ( TRUE ) ;
}


//*--------------------------------------------------------------------------------------
//* Function Name       : DataflashBoot
//* Object              : load the content of the dataflash to SRAM at BASE_LOAD_ADDRESS
//* Input Parameters    :
//* Output Parameters   :
//*--------------------------------------------------------------------------------------
int DataflashBoot(void)
{
    int 	    j =0;
    u_char *pt_sram = (u_char *)BASE_LOAD_ADDRESS;

	/* copy the content of the dataflash in sram by 128 bytes */
    while( j < ( SizeToDownload / 128)) {
		/* wait the dataflash ready status */
		dataflash_waitReady(20000);
		/* read 128 bytes in the dataflash */
		if (!dataflash_read(j*128, pt_sram, 128))
			return 0;
		j++;
		pt_sram += 128;
    }
    return 1;
}



//*--------------------------------------------------------------------------------------
//* Function Name       : IsBootValid
//* Object              : verify that the first bytes of the buffer are valid ARM vectors
//* Input Parameters    :
//* Output Parameters   : TRUE if ok FALSE else
//*--------------------------------------------------------------------------------------
static int IsBootValid(unsigned char *buffer)
{
    int i = 3 ;
    SizeToDownload = 0;

	/* Verify if the 32 first bytes of the sram correspond to ARM vectors*/
	/* The sixth ARM vector contain the size of the code */
    while(i < 32) {
	/* the reserved vector contain the size of the code to download */
		if (i != 23){
			if ((buffer[i] != 0xEA) && (buffer[i] != 0xE5) )
				return 0;
		}
		i+=4;
    }
	/* calculate the size to download */
    SizeToDownload = ((int)buffer[23]<< 8) + buffer[22];
	if (SizeToDownload > MEMORY_SIZE )
		return 0;
    return 1;
}


//*--------------------------------------------------------------------------------------
//* Function Name       : DownloadEntry
//* Object              : detect device connected
//* Input Parameters    :
//* Output Parameters   : device id
//* 					: Read the 32 first bytes of the memory and check for ARM vectors
//*--------------------------------------------------------------------------------------
int DownloadEntry(void)
{
	int i = 0, j = 0, size  = 0;
	int err = 0;
	volatile unsigned char rxBuffer[128];
	AT91PS_Pio2Desc pPio;
	u_char *pt_ebi;	
	
	pt_ebi = (u_char *)BASE_EBI_CS0_ADDRESS;
	pPio = (const AT91PS_Pio2Desc)&pAT91->PIOA_DESC;

//* Open PIOA clock
	at91_apmc_pcer(APMC_BASE, pPio->periph_id);
	dataflash_open( (const AT91PS_SPIDesc)&pAT91->SPI_DESC );
#if 0	
	while(1)
	{
		rxBuffer[0] = 0x41;
		rxBuffer[1] = 0x42;
		rxBuffer[2] = 0x43;
		rxBuffer[3] = 0x44;
		rxBuffer[4] = 0;
		dataflash_waitReady(200000);
		dataflash_write((unsigned char *)rxBuffer, 0, 4);
		dataflash_waitReady(200000);
		dataflash_read(0, (unsigned char *)&rxBuffer[4], 4);
	}
	
#endif	
	download_file_to_dataflash();	
		
	while(i < SizeToDownload) {	
		dataflash_waitReady(200000);
		
		if (i + sizeof(rxBuffer) < SizeToDownload)
			size = sizeof(rxBuffer);
		else
			size = 	SizeToDownload - i;				
		dataflash_read(i, (unsigned char *)rxBuffer, size);

		for(j = 0; j <size; j++)
			if ( rxBuffer[j] != EEPROM_TAB[i + j] )
				err ++;
		i += sizeof(rxBuffer);	
	}
	
	return 1;
#if 0
//* detect dataflash device
    if (dataflash_open( (const AT91PS_SPIDesc)&pAT91->SPI_DESC ) == DATAFLASH_OPEN_OK) {
		if (dataflash_read(0, rxBuffer, 32) == DATAFLASH_READ_OK) {
			if (IsBootValid(rxBuffer))
				return AT91C_DATAFLASH_OK;
		}
    }

//* boot from TWI e2prom
	EEPROM_Init( (const AT91PS_TWIDesc)&pAT91->TWI_DESC );
	if (EEPROM_SeqRead((const AT91PS_TWIDesc)&pAT91->TWI_DESC, 0, (char *)rxBuffer, 32) == EEPROM_READ_OK){
		if (IsBootValid(rxBuffer))
			return AT91C_EEPROM_OK;
	}

//* boot from external 8 bits memory

#if 0
/* Setup of CS0 */
	MemoryControllerInit((const AT91PS_MEMCDesc)&pAT91->MEMC_DESC);

	for (i = 0; i < 32; i++)
		rxBuffer[i] = *(pt_ebi + i);

	if (IsBootValid(rxBuffer))
		return AT91C_EXTERNMEM_OK;
#endif
#endif
	return 0;
}

⌨️ 快捷键说明

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