?? main.c
字號:
#include "AT91RM9200.h"
#include "lib_AT91RM9200.h"
#include "def.h"
#include "config.h"
#include "slib.h"
#include "console.h"
#include "twi.h"
#include "params.h"
//void test_s1d13506(U32 a1,U32 a2);
void Test_SED1356_LCD_640_480( U32 m , U32 n );
void Test_SED1356_VGA_640_480( U32 m , U32 n );
void Test_SED1356_LCD_240_320( U32 m , U32 n );
// The following DBGU ASM handler is defined in asm_isr.s
//extern void AT91F_ASM_DBGU_Handler(void);
U32 downloadAddress, downloadFileSize;
//char hex_ch[16] = {'0','1','2','3','4','5','6','7',
// '8','9','a','b','c','d','e','f'};
static U8 OurEmacAddr[6] =
{
0x01, 0x02, 0x03, 0x04, 0x05, 0x06
};
volatile U32 StTick;
void ( *StIrqHandler )( void );
void ( *DbguIrqHandler )( void );
static void SysIrqHandler( void )
{
/* ========== Systimer interrupt ============== */
if ( AT91F_ST_GetInterruptMaskStatus( AT91C_BASE_ST ) & AT91C_ST_PITS )
{
if ( AT91C_BASE_ST->ST_SR & AT91C_ST_PITS )
{
StTick++;
if ( *StIrqHandler )
( *StIrqHandler ) ();
return;
}
}
if ( *DbguIrqHandler )
( *DbguIrqHandler ) ();
}
//統一的中斷入口處理
void __irq irq_handler( void )
{
void ( *svr ) ( void );
AT91PS_AIC ptr = AT91C_BASE_AIC;
U32 i;
// U8 irq_idx;
//取得中斷入口地址可用AIC_IVR或用AIC_ISR作索引得到AIC_SVR數組中的地址
i = ptr->AIC_IVR; //read AIC_IVR
// irq_idx = ptr->AIC_ISR&0x1f;
//邊沿觸發中斷必須以此清中斷
// AT91F_AIC_ClearIt(AT91C_BASE_AIC, irq_idx);
// Write in the IVR to support Protect Mode
// No effect in Normal Mode
// De-assert the NIRQ and clear the source in Protect Mode
ptr->AIC_IVR = ( AT91_REG ) ptr;
// putch('I');
// printf("%x,%x,%x\n", irq_idx, ptr->AIC_IPR, ptr->AIC_CISR);
svr = ( void ( * ) ( void ) ) i;//ptr->AIC_SVR[irq_idx];
( *svr ) ();
AT91F_AIC_AcknowledgeIt( ptr ); //退出中斷前必須應答
}
static void InitBuzzer( void )
{
int val;
AT91PS_TC pTC = AT91C_BASE_TC2;
AT91F_TC2_CfgPMC();
pTC->TC_IDR = 0xff; //disable all interrupts
//select TIMER_CLOCK2 = MCK/8, CPCTRG, up mode, Waveform mode, RB compare set, RC compare clear, software trigger clear
pTC->TC_CMR = 1 | ( 1 << 10 ) | ( 2 << 13 ) | ( 1 << 15 ) | ( 1 << 24 ) | ( 2 << 26 ) | ( 2UL << 30 ); //must not set EEVT as TIOB!
val = ( AT91F_PMC_GetMasterClock( AT91C_BASE_PMC , AT91C_BASE_CKGR , 32768 ) >> 3 ) / 2100;
pTC->TC_RB = val >> 1;
pTC->TC_RC = val;
pTC->TC_CCR = 5; //enable timer-counter and trig it
/*
AT91F_PIO_CfgPeriph(
AT91C_BASE_PIOA, // PIO controller base address
0, // Peripheral A
AT91C_PA22_TIOB2); // Peripheral B
delay(120);
AT91F_PIO_CfgOutput(AT91C_BASE_PIOA, AT91C_PIO_PA22);
//AT91F_PIO_SetOutput(AT91C_BASE_PIOA, AT91C_PIO_PA22);
AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, AT91C_PIO_PA22);
*/
}
static void InitPio( void )
{
//led
AT91F_PIO_CfgOutput( AT91C_BASE_PIOB , AT91C_PIO_PB8 | AT91C_PIO_PB15 | AT91C_PIO_PB16 | AT91C_PIO_PB17 );
//AT91F_PIO_ClearOutput(AT91C_BASE_PIOB, AT91C_PIO_PB8|AT91C_PIO_PB15|AT91C_PIO_PB16|AT91C_PIO_PB17);
AT91F_PIO_SetOutput( AT91C_BASE_PIOB , AT91C_PIO_PB8 | AT91C_PIO_PB15 | AT91C_PIO_PB16 | AT91C_PIO_PB17 );
//key
AT91F_PIO_CfgInput( AT91C_BASE_PIOA , AT91C_PIO_PA24 );
AT91F_PIO_CfgInput( AT91C_BASE_PIOB , AT91C_PIO_PB1 | AT91C_PIO_PB2 | AT91C_PIO_PB6 );
AT91F_PIO_CfgOutput( AT91C_BASE_PIOB , AT91C_PIO_PB7 );
AT91F_PIO_ClearOutput( AT91C_BASE_PIOB , AT91C_PIO_PB7 );
//buzzer
InitBuzzer();
}
/****************add by cgf**************************/
//Function:AT91F_RS485_putc
//Object :Send a character
/*****************************************************/
static void AT91F_RS485_putc(char buffer)
{
if(buffer=='\n') {
while(!AT91F_US_TxReady((AT91PS_USART)AT91C_BASE_US3));
AT91F_US_PutChar((AT91PS_USART)AT91C_BASE_US3, '\r');
delay(1);
}
while(!AT91F_US_TxReady((AT91PS_USART)AT91C_BASE_US3));
AT91F_US_PutChar(AT91C_BASE_US3,buffer);
delay(5);
}
//*----------------------------------------------------------------------------
//* \fn puts
//* \brief This function is used to send a string through the DBGU channel (Very low level debugging)
//*----------------------------------------------------------------------------
void AT91F_RS485_puts(char *buffer) // \arg pointer to a string ending by \0
{
while(*buffer != '\0') {
AT91F_RS485_putc(*buffer++);
}
}
/****************add by cgf**************************/
//Function:AT91F_RS485_getc()
//Object :Get a character
/*****************************************************/
char AT91F_RS485_getc()
{
while(!AT91F_US_RxReady((AT91PS_USART)AT91C_BASE_US3));
return AT91F_US_GetChar(AT91C_BASE_US3);
}
/****************add by cgf**************************/
//Function:InitRs485
//Object :Set PIO & PMC for USART2
/*****************************************************/
static void InitRS485(void)
{
//AT91F_US2_CfgPIO(); //Configure PIO controllers to drive US2 singal
AT91F_PIO_CfgPeriph(
AT91C_BASE_PIOA, // PIO controller base address
0,
((unsigned int) AT91C_PA5_TXD3 ) |
((unsigned int) AT91C_PA6_RXD3 )); // Peripheral A); // Peripheral B
AT91F_US3_CfgPMC(); //Enable Peripheral clock in PMC for US2
//AT91F_PMC_EnablePeriphClock(AT91C_BASE_PMC, ((U32) 1 << AT91C_ID_US3));
AT91F_PIO_CfgOutput(AT91C_BASE_PIOB,AT91C_PIO_PB0);
// Configure USART2
AT91F_US_Configure (
AT91C_BASE_US3, // USART2 base address
AT91C_MASTER_CLOCK, // 48 MHz
AT91C_US_ASYNC_MODE,//AT91C_US_ASYNC_RS485_MODE , // mode Register to be programmed
115200 , // baudrate to be programmed
0); // timeguard to be programmed
//c=AT91F_RS485_getc();
}
/****************add by cgf**************************/
//Function:TestRs485
//Object :RS485 Test
/*****************************************************/
void TestRs485(U32 a1,U32 a2)
{
char c;
InitRS485();
//AT91F_PIO_SetOutput(AT91C_BASE_PIOB,AT91C_PIO_PB0);//Enable RS485
//c=AT91F_RS485_getc();
//while(1)
//{
puts("RS485 Test\n");
//}
c='a';
puts("Please switch to RS485 connect\n'ESC' key to exit...\n");
AT91F_US_EnableRx(AT91C_BASE_US3);
AT91F_US_EnableTx(AT91C_BASE_US3);
while((getkey()!=ESC_KEY))
{
AT91F_PIO_SetOutput(AT91C_BASE_PIOB,AT91C_PIO_PB0);
AT91F_RS485_putc(c);
while ( !(AT91C_BASE_US3->US_CSR & AT91C_US_TXEMPTY) );
delay(8000);
AT91F_PIO_ClearOutput(AT91C_BASE_PIOB,AT91C_PIO_PB0);
c=AT91F_RS485_getc();
if (c == ESC_KEY)
break;
putch(c);
delay(8000);
}
}
U32 GetFlashID( void );
int SectorProg( U32 begin , U16* data , U32 size );
int SectorRead( U32 begin , U16* data , U32 size );
void start_kernel( U32 , U32 );
static void InitNorFlash( void )
{
//printf("into InitNorFlash\n");
#ifdef NOR_SUPPORT
// Setup MEMC to support CS0 = static memory
AT91C_BASE_EBI->EBI_CSA &= ~1;
// [D15:0] pull-up
AT91C_BASE_EBI->EBI_CFGR = ( AT91C_EBI_DBPUC & 0x00 ) | ( AT91C_EBI_EBSEN & 0x00 );
// Setup Flash 存儲器參數設置CS0-CS7對應SMC2_CSR[0]-SMC2_CSR[7]
AT91C_BASE_SMC2->SMC2_CSR[0] = ( AT91C_SMC2_NWS & 0x7f ) |
AT91C_SMC2_WSEN |
( AT91C_SMC2_TDF & 0x200 ) |
AT91C_SMC2_BAT |
AT91C_SMC2_DBW_16 |
AT91C_SMC2_ACSS_STANDARD;/* |
AT91C_SMC2_RWSETUP | AT91C_SMC2_RWHOLD; */
printf( "Nor FLASH ID = %x\n" , GetFlashID() );
#endif
}
static void RunProgFrNor( U32 addr , U32 size )
{
#ifdef NOR_SUPPORT
downloadAddress = 0x20800000;
downloadFileSize = 0x200000;
// printf( "Read Program From Nor Flash to 0x%8x, Size 0x%x\n" , downloadAddress , downloadFileSize );
SectorRead( 0x100000 , ( U16 * ) downloadAddress , downloadFileSize );
start_kernel( downloadAddress , 0 );
#endif
}
static void WrFileToNor( U32 addr , U32 size )
{
#ifdef NOR_SUPPORT
//printf( "Write Data From 0x%8x, Size 0x%x to Nor Flash\n" , addr , size );
SectorProg( 0x000000 , ( U16 * ) addr , size );
#endif
}
void GetNandFlashChip( void );
static void InitNandFlash( void )
{
#ifdef NAND_SUPPORT
AT91F_PIO_CfgPeriph( AT91C_BASE_PIOC , // PIO controller base address
( unsigned int ) AT91C_PC1_BFRDY_SMOE | ( unsigned int ) AT91C_PC3_BFBAA_SMWE , // Peripheral A
0 ); // Peripheral B
// Setup MEMC to support CS3 = NAND Flash
AT91C_BASE_EBI->EBI_CSA |= AT91C_EBI_CS3A_SMC_SmartMedia;
// [D15:0] pull-up
AT91C_BASE_EBI->EBI_CFGR = ( AT91C_EBI_DBPUC & 0x00 ) | ( AT91C_EBI_EBSEN & 0x00 );
// Setup Flash 存儲器參數設置CS0-CS7對應SMC2_CSR[0]-SMC2_CSR[7]
AT91C_BASE_SMC2->SMC2_CSR[3] = ( AT91C_SMC2_NWS & 0x4 ) |
AT91C_SMC2_WSEN |
( AT91C_SMC2_TDF & 0x200 ) |
AT91C_SMC2_BAT |
AT91C_SMC2_DBW_8;
// Enable PIOC clock for input
AT91F_PMC_EnablePeriphClock( AT91C_BASE_PMC , ( ( unsigned int ) 1 << AT91C_ID_PIOC ) );
AT91F_PIO_CfgInput( AT91C_BASE_PIOC , AT91C_PIO_PC14 );
AT91F_PIO_CfgPullup( AT91C_BASE_PIOC , AT91C_PIO_PC14 );
AT91F_PIO_CfgOutput( AT91C_BASE_PIOC , AT91C_PIO_PC15 );
// AT91F_PIO_GetInput(AT91C_BASE_PIOC) & AT91C_PIO_PC14;
GetNandFlashChip();
AT91F_PIO_CfgPeriph( AT91C_BASE_PIOC , // PIO controller base address
AT91C_PC6_NWAIT , // Peripheral A
0 ); // Peripheral B
AT91C_BASE_SMC2->SMC2_CSR[7] = ( AT91C_SMC2_NWS & 4 ) |
AT91C_SMC2_WSEN |
( AT91C_SMC2_TDF & 0x100 ) | //AT91C_SMC2_BAT |
//AT91C_SMC2_DBW_16 | AT91C_SMC2_ACSS_1_CYCLE |
AT91C_SMC2_DBW_16 |
AT91C_SMC2_ACSS_STANDARD |
( AT91C_SMC2_RWSETUP & ( 0 << 24 ) ) |
( AT91C_SMC2_RWHOLD & ( 0 << 28 ) );
#endif
}
#ifdef IIC_SUPPORT
#define EEP_RW_CHK_CNT 32
#endif /* IIC_SUPPORT */
static void InitTwi( void )
{
#ifdef IIC_SUPPORT
int loop;
char data[EEP_RW_CHK_CNT];
// Configure TWI PIOs
AT91F_TWI_CfgPIO();
AT91F_PIO_CfgOpendrain( AT91C_BASE_PIOA , ( unsigned int ) AT91C_PA25_TWD );
// Configure PMC by enabling TWI clock
AT91F_TWI_CfgPMC();
// Configure TWI in master mode
AT91F_TWI_Configure( AT91C_BASE_TWI );
// Set TWI Clock Waveform Generator Register
AT91F_SetTwiClock( AT91C_BASE_TWI );
// IIC啟動,先測試IIC讀
//printf( "EEPREOM address is 0x%x\n" , AT91C_EEPROM_I2C_ADDRESS );
// for(loop=0; loop<EEP_RW_CHK_CNT; loop++)
// data[loop] = loop;
// Write and read iic
// AT91F_TWI_Write(AT91C_BASE_TWI, 0x0, data, EEP_RW_CHK_CNT);
// Wait 10 ms before data is written into EEPROM
// puts("Wait at least 10 ms before value is written into EEPROM\n");
// for (loop=0; loop<100000; loop++);
for ( loop = 0; loop < EEP_RW_CHK_CNT; loop++ )
data[loop] = 0;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -