📄 main.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 : main.c
//* Object :
//* Creation : HIi 10/10/2003
//*----------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <AT91RM9200.h>
#include <lib_AT91RM9200.h>
#include <com.h>
#include <grentech.h>
#include <main.h>
#include <dataflash.h>
#include <norflash.h>
#include <config.h>
//#define AT91C_PLLA_VALUE 0x202CBE01 // crystal= 4MHz
#define AT91C_PLLA_VALUE 0x2026BE04 // crystal= 18.432MHz
#define DELAY_MAIN_FREQ 1000
#define DISP_LINE_LEN 16
/*Address in sdram bootloader copied by romboot to and start to run from*/
#define AT91C_BOOT_LOADER_ADDR 0x20600000 //1M byte
#define AT91C_BOOT_LOADER_SIZE 1024*1024 //1M
#ifdef INCLUDE_DATAFLASH_SUPPORT
#define AT91C_BOOT_LOADER_DATAFLASH_ADDR 0xC0008000
#endif
#ifdef INCLUDE_NOR_FLASH_SUPPORT
#define AT91C_BOOT_LOADER_DATAFLASH_ADDR 0x10010000
#endif
//* prototypes
extern void AT91F_DBGU_Printk(char *);
extern void AT91F_ST_ASM_HANDLER(void);
#ifdef INCLUDE_IRQ0_SUPPORT
extern void AT91F_IRQ0_ASM_HANDLER(void);
#endif
extern void Jump(unsigned int addr);
const char menu_separ[] = {
"*-------------------------------------------*\n\r"
};
const char *menu_dataflash[] = {
{
#ifdef INCLUDE_DEBUG_SUPPORT
"Please input command : 1-5\n\r\n\r"
#else
"Please input command : 1-3\n\r\n\r"
#endif
"1. Download romboot\n\r"
"2. Download bootloader\n\r"
"3. Start bootloader\n\r"
#ifdef INCLUDE_DEBUG_SUPPORT
#ifdef INCLUDE_HW_DETECT
"4. System HW test\n\r"
#else
"4. Test bootloader\n\r"
#endif
"5. Read Dataflash\n\r"
#if 0
"6. Read 16bits\n\r"
"7. Write 16bits\n\r"
#endif
#endif
}
};
//* Globales variables
char message[40];
volatile char XmodemComplete = 0;
unsigned int StTick;
AT91S_RomBoot const *pAT91;
AT91S_SBuffer sXmBuffer;
AT91S_SvcXmodem svcXmodem;
AT91S_Pipe xmodemPipe;
AT91S_CtlTempo ctlTempo;
AT91S_SvcTempo svcTempo; // Link to a AT91S_Tempo object
AT91S_ST *AT91S_SYS_ST;
//*--------------------------------------------------------------------------------------
//* Function Name : GetTickCount()
//* Object : Return the number of systimer tick
//* Input Parameters :
//* Output Parameters :
//*--------------------------------------------------------------------------------------
unsigned int GetTickCount(void)
{
return StTick;
}
//*--------------------------------------------------------------------------------------
//* Function Name : AT91_XmodemComplete()
//* Object : Perform the remap and jump to appli in RAM
//* Input Parameters :
//* Output Parameters :
//*--------------------------------------------------------------------------------------
void AT91_XmodemComplete(AT91S_PipeStatus status, void *pVoid)
{
// stop the Xmodem tempo
svcXmodem.tempo.Stop(&(svcXmodem.tempo));
XmodemComplete = 1;
}
//*--------------------------------------------------------------------------------------
//* Function Name : AT91F_XmodemProtocol(AT91S_PipeStatus status, void *pVoid)
//* Object : Xmodem dispatcher
//* Input Parameters :
//* Output Parameters :
//*--------------------------------------------------------------------------------------
void XmodemProtocol(AT91S_PipeStatus status, void *pVoid)
{
AT91PS_SBuffer pSBuffer = (AT91PS_SBuffer) xmodemPipe.pBuffer->pChild;
AT91PS_USART pUsart = svcXmodem.pUsart;
if (pSBuffer->szRdBuffer == 0) {
// Start a tempo to wait the Xmodem protocol complete
svcXmodem.tempo.Start(&(svcXmodem.tempo), 10, 0, AT91_XmodemComplete, pUsart);
}
}
//*-------------------------- Interrupt handlers ----------------------------------------
//*--------------------------------------------------------------------------------------
//* Function Name : irq1_c_handler()
//* Object : C Interrupt handler for Interrutp source 1
//* Input Parameters : none
//* Output Parameters : none
//*--------------------------------------------------------------------------------------
void AT91F_ST_HANDLER(void)
{
volatile unsigned int csr = *AT91C_DBGU_CSR;
unsigned int error;
/* ========== Systimer interrupt ============== */
if (AT91C_BASE_ST->ST_SR & 0x01) {
StTick++;
ctlTempo.CtlTempoTick(&ctlTempo);
return;
}
error = AT91F_US_Error((AT91PS_USART)AT91C_BASE_DBGU);
if (csr & error) {
// Stop previous Xmodem transmition
*(AT91C_DBGU_CR) = AT91C_US_RSTSTA;
AT91F_US_DisableIt((AT91PS_USART)AT91C_BASE_DBGU, AT91C_US_ENDRX);
AT91F_US_EnableIt((AT91PS_USART)AT91C_BASE_DBGU, AT91C_US_RXRDY);
}
else if (csr & (AT91C_US_TXRDY | AT91C_US_ENDTX | AT91C_US_TXEMPTY |
AT91C_US_RXRDY | AT91C_US_ENDRX | AT91C_US_TIMEOUT |
AT91C_US_RXBUFF)) {
if ( !(svcXmodem.eot) )
svcXmodem.Handler(&svcXmodem, csr);
}
}
//*-----------------------------------------------------------------------------
//* Function Name : AT91F_DisplayMenu()
//* Object :
//* Input Parameters :
//* Return value :
//*-----------------------------------------------------------------------------
void AT91F_DisplayMenu(void)
{
AT91F_ClrScr();
#if 0
#if 1
printf ("Romboot version: " "ATMEL LOADER" AT91C_VERSION "\n\r");
printf ("Creation date: %s %s\n\r\n\r", __DATE__, __TIME__);
#else
printf("\n\rATMEL LOADER %s %s %s\n\r",AT91C_VERSION,__DATE__,__TIME__);
printf("GRENTECH RRU MONITOR GROUP\n\r");
#endif
#endif
printf(menu_separ);
#if 0
AT91F_DataflashPrintInfo ();
printf(menu_separ);
#endif
printf(menu_dataflash[0]);
printf(menu_separ);
}
//*-----------------------------------------------------------------------------
//* Function Name : AsciiToHex()
//* Object : ascii to hexa conversion
//* Input Parameters :
//* Return value :
//*-----------------------------------------------------------------------------
unsigned int AsciiToHex(char *s, unsigned int *val)
{
int n;
*val=0;
if(s[0] == '0' && ((s[1] == 'x') || (s[1] == 'X')))
s+=2;
n = 0;
while((n < 8) && (s[n] !=0))
{
*val <<= 4;
if ( (s[n] >= '0') && (s[n] <='9'))
*val += (s[n] - '0');
else
if ((s[n] >= 'a') && (s[n] <='f'))
*val += (s[n] - 0x57);
else
if ((s[n] >= 'A') && (s[n] <='F'))
*val += (s[n] - 0x37);
else
return 0;
n++;
}
return 1;
}
#ifdef INCLUDE_DEBUG_SUPPORT
//*-----------------------------------------------------------------------------
//* Function Name : AT91F_MemoryDisplay()
//* Object : Display the content of the dataflash
//* Input Parameters :
//* Return value :
//*-----------------------------------------------------------------------------
int AT91F_MemoryDisplay(unsigned int addr, unsigned int size, unsigned int length)
{
unsigned long i, nbytes, linebytes;
char *cp;
unsigned int *uip;
unsigned short *usp;
unsigned char *ucp;
char linebuf[DISP_LINE_LEN];
nbytes = length * size;
do
{
uip = (unsigned int *)linebuf;
usp = (unsigned short *)linebuf;
ucp = (unsigned char *)linebuf;
printf("\a%08X:", addr);
linebytes = (nbytes > DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
read_datum_from_flash(addr, (linebytes/size)*size, linebuf);
for (i=0; i<linebytes; i+= size)
{
if (size == 4)
printf(" %08X", *uip++);
else if (size == 2)
printf(" %04X", *usp++);
else
printf(" %02X", *ucp++);
addr += size;
}
printf(" ");
cp = linebuf;
for (i=0; i<linebytes; i++) {
if ((*cp < 0x20) || (*cp > 0x7e))
printf(".");
else
printf("%c", *cp);
cp++;
}
printf("\n\r");
nbytes -= linebytes;
} while (nbytes > 0);
return 0;
}
#endif
//*--------------------------------------------------------------------------------------
//* Function Name : AT91F_SetPLL
//* Object : Set the PLLA to 180Mhz and Master clock to 60 Mhz
//* Input Parameters :
//* Output Parameters :
//*--------------------------------------------------------------------------------------
void AT91F_SetPLL(void)
{
volatile int tmp = 0;
AT91PS_PMC pPmc = AT91C_BASE_PMC;
AT91PS_CKGR pCkgr = AT91C_BASE_CKGR;
pPmc->PMC_IDR = 0xFFFFFFFF;
#if 0
//* Object : Set the PLLA to 180Mhz and Master clock to 60 Mhz
//* -Setup the PLL A
pCkgr->CKGR_PLLAR = 0x20263E08;
while(!(pPmc->PMC_SR & AT91C_PMC_MCKRDY) && (tmp++ < DELAY_MAIN_FREQ));
//* - Commuting Master Clock from PLLB to PLLA/3
pPmc->PMC_MCKR = 0x0000000A;
#else
//* Object : Set the PLLA to 180Mhz and Master clock to 60 Mhz
//* -Setup the PLL A
pCkgr->CKGR_PLLAR = AT91C_PLLA_VALUE;
while(!(pPmc->PMC_SR & AT91C_PMC_MCKRDY) && (tmp++ < DELAY_MAIN_FREQ));
//* - Commuting Master Clock from PLLB to PLLA/3
pPmc->PMC_MCKR = 0x00000202;
#endif
}
//*--------------------------------------------------------------------------------------
//* Function Name : AT91F_ResetRegisters
//* Object : Restore the initial state to registers
//* Input Parameters :
//* Output Parameters :
//*--------------------------------------------------------------------------------------
static void AT91F_ResetRegisters(void)
{
volatile int i = 0;
#if 1
//* set the PIOs in input
*AT91C_PIOA_ODR = 0xFFFFFFFF; /* Disables all the output pins */
*AT91C_PIOA_PER = 0xFFFFFFFF; /* Enables the PIO to control all the pins */
#endif
#if 1
//Disable the system interrupts
AT91F_AIC_DisableIt (AT91C_BASE_AIC, AT91C_ID_SYS);
/* close all peripheral clocks */
AT91C_BASE_PMC->PMC_PCDR = 0xFFFFFFFC;
//* Disable core interrupts and set supervisor mode
__asm
{
MSR CPSR_c, #0xDF //* ARM_MODE_SYS(0x1F) | I_BIT(0x80) | F_BIT(0x40)
}
//* Disable all the irq and fiq interrupts
*AT91C_AIC_IDCR = 0xffffffff;
//* Clear all the interrupts
*AT91C_AIC_ICCR = 0xffffffff;
/* read the AIC_IVR and AIC_FVR */
i = *AT91C_AIC_IVR;
i = *AT91C_AIC_FVR;
/* write the end of interrupt control register */
*AT91C_AIC_EOICR = 0;
#endif
#if 1
AT91F_SetPLL();
#endif
#if 0
Turn_on_debug_led();
#endif
}
#if 0
//*--------------------------------------------------------------------------------------
//* Function Name : AT91F_StartUboot
//* Object : copy uboot.bin to sdram and run it
//* Input Parameters :
//* Output Parameters :
//*--------------------------------------------------------------------------------------
void AT91F_StartUboot(unsigned int dummy, void* pdummy)
{
printf("Load U-BOOT from dataflash[%x] to SDRAM[%x]\n\r", AT91C_BOOT_LOADER_DATAFLASH_ADDR, AT91C_BOOT_LOADER_ADDR);
//read uboot.bin from addr c0080000 to 21f00000
read_dataflash (AT91C_BOOT_LOADER_DATAFLASH_ADDR, AT91C_BOOT_LOADER_SIZE, (char *)(AT91C_BOOT_LOADER_ADDR));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -