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

📄 download.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 & Functions   ======== */
/* ================================================================ */
extern AT91S_RomBoot const 		*pAT91;
extern int 						SizeToDownload;
extern AT91S_SvcDataFlash 		svcDataFlash;

extern AT91S_SvcDataFlashStatus AT91F_DataFlashWaitReady(AT91PS_SvcDataFlash,u_int);

/* ================================================================ */
/* ====================   Internal Functions   ==================== */
/* ================================================================ */
static int IsBootValid(unsigned char *);

//*=================================================================
//*Download
//*=================================================================
#ifndef BOOSTER_LIGHT
static void MemoryControllerInit(const AT91PS_MEMCDesc);

//*----------------------------------------------------------------------------
//* 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 | SMC_DRP_EARLY;
}


//*--------------------------------------------------------------------------------------
//* 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 TRUE;
}
#endif


//*--------------------------------------------------------------------------------------
//* Function Name       : EepromBoot
//* Object              : load the content of the Eeprom to SRAM at BASE_LOAD_ADDRESS
//* Input Parameters    :
//* Output Parameters   :
//*--------------------------------------------------------------------------------------
int EepromBoot(void)
{
	EEPROM_SeqRead((const AT91PS_TWIDesc)&pAT91->TWI_DESC, 0, (char *)BASE_LOAD_ADDRESS,  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)
{
	/* wait the dataflash ready status */
	if(AT91F_DataFlashWaitReady(&svcDataFlash,TIMEOUT_READ_DATAFLASH) != DATAFLASH_OK)
			return FALSE;
	
	/* read 128 bytes in the dataflash */
	if (AT91F_DataFlashContinuousRead(&svcDataFlash,0,(u_char *)BASE_LOAD_ADDRESS,SizeToDownload) != DATAFLASH_OK)
			return FALSE;

	/* wait the dataflash ready status */
	if(AT91F_DataFlashWaitReady(&svcDataFlash,TIMEOUT_READ_DATAFLASH) != DATAFLASH_OK)
			return FALSE;

    return TRUE;
}

//*--------------------------------------------------------------------------------------
//* 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 , j = 0;
    char bool_offset=1;
    
    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 and dataflash features*/
    
    while(i < 32) 
    {
		if (i != 23)
		{
			if ((buffer[i] != 0xEA) && (buffer[i] != 0xE5) )
				return FALSE;
		}
		i+=4;
    }

    /* init the correct dataflash features in DataFlash structure AT91S_Dataflash */
    svcDataFlash.pDevice->pages_number 	= 1 <<( ((buffer[22] & 0x01) << 3) + ((buffer[21] & 0xE0) >> 5) );
	
	svcDataFlash.pDevice->pages_size 	=  ((buffer[23] & 0xFF) << 7) + ( (buffer[22] & 0xFE) >> 1);
	
	while(j<8 && bool_offset)
	{
		if( (buffer[23] & 0x80) == 0x80)
		{
			bool_offset=0;
			svcDataFlash.pDevice->page_offset = (15-j);
		}
		else
		{ 
			buffer[23] <<= 1;
			j++;
		}
	}
	
	svcDataFlash.pDevice->byte_mask 	= ( ((1<<(svcDataFlash.pDevice->page_offset - 8))-1) << 8); // byte_mask = 2^(offset-8) - 1 << 8 
	
	/* calculate the size to download */
    SizeToDownload = 512 * ( (int)(buffer[20] & 0xFF)  );
	if (SizeToDownload > MEMORY_SIZE )
		return FALSE;
    
    return TRUE;
}

//*--------------------------------------------------------------------------------------
//* 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)
{
#ifndef BOOSTER_LIGHT	
	int i;
#endif
	
	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);
	
	//* try to detect dataflash device thanks to a continuous read, by default AT45DB011B selected
	svcDataFlash.pDevice->pages_number = 512;
	svcDataFlash.pDevice->pages_size = 264;
	svcDataFlash.pDevice->page_offset = 9;
	svcDataFlash.pDevice->byte_mask = 0x100;
	
  	if (AT91F_DataFlashContinuousRead(&svcDataFlash,0, rxBuffer, 32) == DATAFLASH_OK)
		{
			/* wait the dataflash ready status */
			if(AT91F_DataFlashWaitReady(&svcDataFlash,TIMEOUT_READ_DATAFLASH) != DATAFLASH_OK)
				return FALSE;

			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;
	}

#ifndef BOOSTER_LIGHT	
	//* boot from external 8 bits memory only for thunder
	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
	return FALSE;
}

⌨️ 快捷键说明

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