www.pudn.com > S3C2440_uCos-II.rar > AppMain.c


/***************************************** 
*        uCos2.52 for S3C2410A           * 
*                                        * 
*****************************************/ 
#include  
#include  
#include  
#include  
#include  
 
//======================= 
#include    "../UCOS-II/includes.h"               /* uC/OS interface */ 
//======================= 
#include "option.h" 
#include "2440addr.h" 
#include "2440slib.h" 
#include "2440Int.h" 
#include "lcd_drv.h" 
 
#define STK_STACKSIZE   1024 
 
//-------------------------------- 
// 
//-------------------------------- 
void OSTickISR(void); 
void Time3_Int(void); 
 
//================================ 
// 串口0 发送函数 
//================================ 
void Uart0_Printf(char *fmt,...); 
extern void __irq Uart0_Int(void); 
 
char _Uart0_TX_BUF[1024]; 
unsigned int _Uart0_TXP = 0; 
unsigned int _Uart0_TXLEN = 0; 
volatile unsigned int _Uart0_isTXing = 0; 
char msgstr[] = "UCOS-II"; 
 
//===================================================================== 
 
 
//========================[ HCLK, PCLK ]=========================== 
void ChangeClockDivider(int hdivn,int pdivn) 
{ 
     // hdivn,pdivn FCLK:HCLK:PCLK 
     //     0,0         1:1:1 
     //     0,1         1:1:2 
     //     1,0         1:2:2 
     //     1,1         1:2:4 
    rCLKDIVN = (hdivn<<1) | pdivn; 
 
    if(hdivn) 
        MMU_SetAsyncBusMode(); 
    else 
        MMU_SetFastBusMode(); 
} 
 
//========================*[ MPLL ]==============================* 
void ChangeMPllValue(int mdiv,int pdiv,int sdiv) 
{ 
    rMPLLCON = (mdiv<<12) | (pdiv<<4) | sdiv; 
} 
 
//================================ 
//  启动任务 
//================================ 
OS_STK TaskStart_stack[1024]; 
void TaskStart(void *p); 
void OSTickISR(void); 
 
void Uart0_Initial(void); 
static void Timer_Initial(void); 
void PrintChar(char); 
//================================== 
// 主程序 
//================================== 
int Main(int argc, char **argv) 
{ 
    //ChangeClockDivider(1,1);                  // 1:2:4 
    ChangeClockDivider(HDIVN,PDIVN);            // 1:4:8 
    ChangeMPllValue(M_MDIV,M_PDIV,M_SDIV);      // FCLK=400.0MHz 
 
    //----------------- 
    IRQ_Init(); 
    //=========================== 
    // Uart0 初始化 
    //=========================== 
    Uart0_Initial(); 
    Uart0_Printf("Uart0 initialized!\r\n"); 
     
    //=========================== 
    // LCD 初始化 
    //=========================== 
    Lcd_Init(); 
     
    Lcd_White(); 
    Lcd_printf(280,230,RGB( 0xFF,0x00,0x00),RGB( 0xFF,0xFF,0xFF),0,"Loading..."); 
 
    //rGPFDAT = 0x0; 
 
    //============================= 
    //初始化IO 
    //IO_Init(); 
    //--------------------------------- 
    //OS_INT_Init(); 
    // 初始化 uC/OS 
    OSInit(); 
 
    //初始化 系统 时基 
    OSTimeSet(0); 
 
    //=========================== 
    OSTaskCreate(TaskStart, (void *)0, (OS_STK *)&TaskStart_stack[64 - 1], 15); 
    OSStart(); 
 
    return 1; 
} 
 
void TaskStart(void *p) 
{ 
    volatile unsigned int i = 0; 
 
    //--------------------------- 
    // 系统时钟初始化 
    //--------------------------- 
    Timer_Initial(); 
 
 
    Uart0_Printf("TaskStart 0.\r\n"); 
    //=========================== 
    // 创建别的任务 
    //=========================== 
    //OSTaskCreate(Task_LCD, (void *)0, (OS_STK *)&Task_LCD_stack[64 - 1], 3); 
 
 
    //=========================== 
    // 初始化系统 跟踪 状态 
    //=========================== 
    //OSStatInit(); 
 
    for(;;) { 
        i++; 
 
        Lcd_printf(120,60,RGB( 0xFF,0x00,0x00),RGB( 0xFF,0xFF,0xFF),0,"uCos2 Runing...:%d",i); 
        Uart0_Printf("uCos2 Runing...:%d.\r\n",i); 
        OSTimeDly( 1000 ); 
    } 
} 
 
 
//======================= 
#define GET_MDIV(x)		((x>>12)&0xff) 
#define GET_PDIV(x)		((x>>4)&0x3f) 
#define GET_SDIV(x)		(x&3) 
#define GET_PCLK		0 
#define GET_HCLK		1 
 
static unsigned long 
cal_bus_clk(unsigned long cpu_clk, unsigned long ratio, int who) 
{ 
	unsigned long hclk = 0,pclk = 0; 
 
	switch( (ratio&0x6) >> 1 ) { 
		case 0: 
			hclk = cpu_clk; 
			break; 
		case 1: 
			hclk = cpu_clk/2; 
			break; 
		case 2: 
			hclk = cpu_clk/4; 
			break; 
		case 3: 
			hclk = cpu_clk/3; 
			break; 
	} 
	switch ( ratio&0x1) { 
		case 0: 
			pclk = hclk; 
			break; 
		case 1: 
			pclk = hclk/2; 
			break; 
	} 
	return ( who ? hclk: pclk ); 
} 
 
/* 
 * cpu clock = ((2 * (mdiv + 8) * FIN) / ((pdiv + 2) * (1 << sdiv))) 
 *  FIN = Input Frequency (to CPU) 
 */ 
// 533MHz 0x7d(125) 0x01 0x01 
static unsigned long 
get_cpu_clk(void) 
{ 
	unsigned long val = rMPLLCON; 
	return ((2*(GET_MDIV(val) + 8) * FIN) / \ 
		((GET_PDIV(val) + 2) * (1 << GET_SDIV(val)))); 
} 
 
static unsigned long 
get_bus_clk(int who) 
{ 
	unsigned long cpu_clk = get_cpu_clk(); 
	unsigned long ratio = rCLKDIVN; 
	return (cal_bus_clk(cpu_clk, ratio, who)); 
} 
 
void RevSpeed_Int(void); 
void AdvAngle_Int(void); 
static void Timer_Initial(void) 
{ 
    /* ticks 使用 TIMER4 */ 
    /*    rTCFG0 = 0; 
    rTCFG1 = 0; 
    rTCNTB4 = 0x6306;*/ 
#if 1 
    int tcon; 
     
    //rTCFG0 = (195 << 8); 
    //rTCFG1 = (1 << 8) | (1 << 16); 
    //rTCNTB4 = 65; 
    rTCFG0 = (50 << 8); 
    rTCFG1 = (0 << 16); 
    rTCNTB4 = 500-1;//rTCNTB4 = 165; 
    tcon = rTCON & ~TCON_4_MASK; 
    rTCON = tcon | (TCON_4_AUTO | TCON_4_UPDATE | COUNT_4_OFF); 
    rTCON = tcon | (TCON_4_AUTO | COUNT_4_ON); 
     
    IRQ_Vector(ID_TIMER4,(unsigned int)OSTickISR); 
    IRQ_Enable(ID_TIMER4); 
#else 
    rTCFG0 = 195; 
    rTCFG1 = 1; 
    rTCNTB0 = 65; 
    rTCON = 0x0a; 
    rTCON = 0x09; 
    IRQ_Vector(ID_TIMER0,(unsigned int)OSTickISR); 
    IRQ_Enable(ID_TIMER0); 
#endif 
} 
void Uart0_Initial(void) 
{ 
    unsigned long uclk; 
    //-------------------------- 
    // 初始化串口 
    //-------------------------- 
    rGPHCON&=0x3c0000; 
    rGPHCON|=0x2faaa;	// nRTS1,nCTS1 
    rGPHUP|=0x1ff;	//Uart port pull-up disable 
 
    rULCON0 = 0x03; 
    rUCON0 = 0x105; 
    rUFCON0 = 0; 
    rUMCON0 = 0; 
 
    uclk = get_bus_clk(GET_PCLK); 
    rUBRDIV0 = ( (int)(uclk/(16*115200)) -1 ); 
 
    IRQ_Vector(ID_UART0,(unsigned int)Uart0_Int); 
    IRQ_Enable(ID_UART0); 
} 
//----------------------------------------- 
 
//----------------------------------------- 
void Uart0_Printf(char *fmt,...) 
{ 
    va_list ap; 
 
    while( _Uart0_isTXing ); 
 
    OS_ENTER_CRITICAL(); 
 
    va_start(ap,fmt); 
    vsprintf(_Uart0_TX_BUF,fmt,ap); 
    va_end(ap); 
 
    _Uart0_TXLEN = strlen(_Uart0_TX_BUF); 
 
    if( _Uart0_TXLEN > 0 ) 
    { 
        _Uart0_isTXing = 1; 
        _Uart0_TXP = 0; 
        WrUTXH0(_Uart0_TX_BUF[_Uart0_TXP]); 
        _Uart0_TXP++; 
        IRQ_SUB_Enable(ID_SUB_TXD0); 
    } 
 
    OS_EXIT_CRITICAL(); 
} 
 
void __irq Uart0_Int(void) 
{ 
    volatile unsigned char rd; 
    unsigned int status; 
    static int lastcr = 0; 
 
    status =  rUTRSTAT0 ; 
 
    //接受数据 
    if ( status & 0x01) 
    { 
         rd = RdURXH0(); 
    } 
 
    //数据发送完毕处理 
    if( (status & 0x02) && (_Uart0_isTXing) ) 
    { 
        if( (_Uart0_TXP >= _Uart0_TXLEN) && _Uart0_isTXing ) { 
            //IRQ_SUB_Disable( ID_SUB_TXD0 ); 
            _Uart0_isTXing = 0; 
            lastcr = 0; 
        } else { 
            if (lastcr || (_Uart0_TX_BUF[_Uart0_TXP]!='\n')) { 
                lastcr = 0; 
                WrUTXH0(_Uart0_TX_BUF[_Uart0_TXP]); 
                _Uart0_TXP++; 
            } else { 
                lastcr = 1; 
                WrUTXH0('\r'); 
            } 
        } 
    } 
 
    ClearPending(BIT_UART0); 
    rSUBSRCPND=(BIT_SUB_TXD0|BIT_SUB_RXD0|BIT_SUB_ERR0); 
}