#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "uhal.h"
#include "def.h"
#include "isr.h"
#include "44b.h"
//MCLK= 50000000
//50000000hz/256/32= 6103.51hz
#define _TICK 1000 // 1/1000 sec
//#define _TICK 100 // 1/100 sec
//#define _TICK 10 // 1/10 sec
#define _CLOCK (61035/(_TICK/10))
//#define _CLOCK 61035 // 1 sec
#define MAX_TBUF 10000
char tbuf[MAX_TBUF]= {0, };
int delayLoopCount= 400;
//int whichUart= 0;
#define SIO_START 0x08 //SIO Start
//void timer0_handler();
void (*restart)(void)=(void (*)(void))0x0;
//void (*run)(void)=(void (*)(void))DOWNLOAD_ADDRESS;
void Delay(int time)//ok eric rong
// time=0: adjust the Delay function by WatchDog timer.
// time>0: the number of loop time
// 100us resolution.
{
int i,adjust=0;
if(time==0)
{
time=200;
adjust=1;
delayLoopCount=400;
rWTCON=((MCLK/1000000-1)<<8)|(2<<3); // 1M/64,Watch-dog,nRESET,interrupt disable
rWTDAT=0xffff;
rWTCNT=0xffff;
rWTCON=((MCLK/1000000-1)<<8)|(2<<3)|(1<<5); // 1M/64,Watch-dog enable,nRESET,interrupt disable
}
for(;time>0;time--)
for(i=0;i<delayLoopCount;i++);
if(adjust==1)
{
rWTCON=((MCLK/1000000-1)<<8)|(2<<3);
i=0xffff-rWTCNT; // 1count/16us?????????
delayLoopCount=8000000/(i*64); //400*100/(i*64/200)
}
}
void Led_Display(int data)//ok eric rong
{
// rPDATE=(rPDATE & 0x13f) | ((data & 0x03)<<6);
}//Led_Display
//Output through LCD or serial port to print information.
int uHALr_printf(const char *format, ...)
{
va_list v_list;
char *ptr;
int i= 0;
va_start(v_list, format); // Initialize variable arguments.
vsprintf(tbuf, format, v_list );
va_end(v_list);
ptr= tbuf;
while( (*ptr) && i<MAX_TBUF) {
Uart_SendByte(0,*ptr);
ptr++; i++;
}//while
return 1;
}//uHALr_printf
void BreakPoint(void)
{
uHALr_printf("!!!Enter break point.");
while(1) {
Led_Display(0xf);
Delay(1000);
Led_Display(0x0);
Delay(1000);
}//while
}//BreakPoint
/*** Called by ARMTargetInit() ***/
//Reset memory management unit.
void uHALr_ResetMMU(void)
{
}//uHALrResetMMU
void OutDebug(unsigned int num)
{
uHALr_printf("\r\n***STACK***:%d\r\n", num);
}//OutDebug
//Define pre & post-process routines for Interrupt.
void uHALir_DefineIRQ(void *is, void *iq, void *n)
{
}//uHALir_DefineIRQ
//Initialze interrupts.
void uHALr_InitInterrupts(void)
{
// Non-vectored,IRQ disable,FIQ disable
rINTCON=0x0;
// All=IRQ mode
rINTMOD=0x0;
// All interrupt is masked.
rINTMSK=BIT_GLOBAL;
}//uHALr_InitInterrupts
//Initialize timer that is used OS.
void uHALr_InitTimers(void)//use timer1
{
//the following code is for 44B0X
rTCFG0=0x0f0f0f;
rTCFG1=0x11111;
rTCNTB1= _CLOCK;
rTCMPB1= 0x0;
rTCON=(0x2<<8); //update mode for TCNTB1 and TCMPB1.
rTCON=(0x9<<8); //timer1 = auto reload, start
}//uHALr_InitTimers
int I_COUNT=0;
void DebugUNDEF(void)
{
uHALr_printf("!!!Enter UNDEFINED. %d\r\n", I_COUNT);
while(1) {
Led_Display(0xf);
Delay(1000);
Led_Display(0x0);
Delay(1000);
}//while
}//BreakPoint
void DebugSWI(void)
{
uHALr_printf("!!!Enter SWI. %d\r\n", I_COUNT);
while(1) {
Led_Display(0xf);
Delay(1000);
Led_Display(0x0);
Delay(1000);
}//while
}
void DebugABORT(void)
{
uHALr_printf("!!!Enter ABORT %d\r\n", I_COUNT);
while(1) {
Led_Display(0xf);
Delay(1000);
Led_Display(0x0);
Delay(1000);
}//while
}
void DebugFIQ(void)
{
uHALr_printf("!!!Enter FIQ. %d\r\n", I_COUNT);
while(1) {
Led_Display(0xf);
Delay(1000);
Led_Display(0x0);
Delay(1000);
}//while
}
extern void IRQ_Handler(void);
void uHALr_InterruptRequestInit()
{
pISR_UNDEF= (unsigned) DebugUNDEF;
pISR_SWI= (unsigned) DebugSWI;
pISR_PABORT= (unsigned) DebugABORT;
pISR_DABORT= (unsigned) DebugABORT;
// pISR_RESERVED
pISR_IRQ= (unsigned) IRQ_Handler; //irq interrupt
pISR_FIQ= (unsigned) DebugFIQ;
//SetISR_Interrupt(INT_TIMER1_OFFSET,OSTimeTick,NULL);
///////////////////////No use////////////////////////
pISR_ADC= (unsigned) BreakPoint;
pISR_RTC= (unsigned) BreakPoint;
pISR_UTXD1= (unsigned) BreakPoint;
pISR_UTXD0= (unsigned) BreakPoint;
pISR_SIO= (unsigned) BreakPoint;
pISR_IIC= (unsigned) BreakPoint;
pISR_URXD1= (unsigned) BreakPoint;
pISR_URXD0= (unsigned) BreakPoint;
//remove by eric rong// pISR_EINT67= (unsigned) BreakPoint;
pISR_WDT= (unsigned) BreakPoint;
pISR_TIMER3= (unsigned) BreakPoint;
pISR_TIMER2= (unsigned) BreakPoint;
pISR_TIMER1= (unsigned) BreakPoint;
// pISR_TIMER0= (unsigned) BreakPoint;
//remove by eric rong// pISR_UERR1= (unsigned) BreakPoint;
//remove by eric rong// pISR_UERR0= (unsigned) BreakPoint;
pISR_BDMA1= (unsigned) BreakPoint;
pISR_BDMA0= (unsigned) BreakPoint;
pISR_ZDMA1= (unsigned) BreakPoint;
pISR_ZDMA0= (unsigned) BreakPoint;
//remove by eric rong// pISR_EINT5= (unsigned) BreakPoint;
//remove by eric rong// pISR_EINT4= (unsigned) BreakPoint;
pISR_EINT3= (unsigned) BreakPoint;
pISR_EINT2= (unsigned) BreakPoint;
pISR_EINT1= (unsigned) BreakPoint;
pISR_EINT0= (unsigned) BreakPoint;
/////////////////////////////////////////////////////
}
//Start system timer & enable the interrupt.
void uHALr_InstallSystemTimer(void)
{
//Non-vectored,IRQ enable,FIQ disable
rINTCON=0x5;
//Non maksed TIMER0 .
rI_ISPC=BIT_TIMER1;
rINTMSK=~(BIT_TIMER1 | BIT_GLOBAL); //Default value=0x7ffffff
}//uHALr_InstallSystemTimer
/************************* PORTS ****************************/
void Port_Init(void)//add by eric rong
{
//CAUTION:Follow the configuration order for setting the ports.
// 1) setting value
// 2) setting control register
// 3) configure pull-up resistor.
//16bit data bus configuration
// PORT A GROUP
/* BIT 9 8 7 6 5 4 3 2 1 0 */
/* ADDR24 ADDR23 ADDR22 ADDR21 ADDR20 ADDR19 ADDR18 ADDR17 ADDR16 ADDR0 */
/* 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 */
rPCONA = 0x3ff;
// PORT B GROUP
/* BIT 10 9 8 7 6 5 4 3 2 1 0*/
/* /CS5 /CS4 /CS3 /CS2 /CS1 nWBE3 nWBE2 /SRAS /SCAS SCLK SCKE*/
/* rtl8019 (Reserve)(Reserve)FLASH D12 Out Out Sdram Sdram Sdram Sdram*/
/* 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1*/
rPDATB = 0x7ff;
rPCONB = 0x7cf;
//PORT C GROUP
//BUSWIDTH=16*/
/* PC 15 14 13 12 11 10 9 8 */
/* O O RXD1 TXD1 O O O O*/
/* Nand-CE UDA-CE Uart1 Uart1 NandCLE NandALE L3DATA L3CLK*/
/* 01 01 11 11 01 01 01 01 */
/* PC 7 6 5 4 3 2 1 0*/
/* O O O I IISCLK IISDI IISDO IISLRCK*/
/* VD4 VD5 VD6 VD7 [ for UDA1341 ]*/
/* 11 11 11 11 11 11 11 11*/
rPDATC = 0x3fff; //All IO is high
rPCONC = 0x5f55ffff;
rPUPC = 0x3000; //PULL UP RESISTOR should be enabled to I/O
//PORT D GROUP for LCD
/*BIT 7 6 5 4 3 2 1 0*/
/* VF VM VLINE VCLK VD3 VD2 VD1 VD0*/
/* 10 10 10 10 10 10 10 10*/
rPDATD= 0xff;
rPCOND= 0xaaaa;
rPUPD = 0x0;
//PORT E GROUP
/* Bit 8 7 6 5 4 3 2 1 0 */
/* CODECLK TOUT4 TOUT3 TOUT2 TOUT1 TOUT0 RXD0 TXD0 SMRB(I) */
/* 10 10 10 10 10 10 10 10 00 */
rPDATE = 0x1ff;
rPCONE = 0x2aaa8;
rPUPE = 0x106;
//PORT F GROUP
/* Bit 8 7 6 5 4 3 2 1 0*/
/* SIOCLK SIORxD 7843CS SIOTxD [Input(DMA) ] Output IICSDA IICSCL*/
/* 011 011 001 011 00 00 01 10 10*/
rPDATF = 0x1fb; //GPF2=0
rPCONF = 0x1B2C1A; //0x9241A;
rPUPF = 0x3;
//PORT G GROUP
/*BIT 7 6 5 4 3 2 1 0*/
/* INT7 INT6 INT5 INT4 INT3 INT2 INT1 INT0*/
/* 11 11 11 11 11 11 11 11