📄 uhal.c
字号:
//something related to the HardWare#include <stdarg.h>#include <string.h>#include <stdlib.h>#include <stdio.h>#include <ctype.h>#include "../ucos-ii/includes.h"#include "../inc/uhal.h"#include "../inc/def.h"#include "../startup/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 10000char tbuf[MAX_TBUF]= {0, };int delayLoopCount= 400;//int whichUart= 0;#define SIO_START 0x08 //SIO Startvoid (*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 Uart_SendByte(int Uartnum, U8 data)//ok eric rong{ if(Uartnum==0) { while(!(rUTRSTAT0 & 0x2)); //Wait until THR is empty. Delay(10); WrUTXH0(data); } else { while(!(rUTRSTAT1 & 0x2)); //Wait until THR is empty. Delay(10); WrUTXH1(data); } } 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_printfvoid 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){}//uHALrResetMMUvoid 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 timer0{ /*only for 44A0X,and remove by eric rong rTCFG=0x000f0f; //mux0,1,2,3=4,pre0=0xf,pre1=0xf rTCNTB0= _CLOCK; rTCMPB0= 0x0; rTCON=0x2; //update mode for TCNTB0 and TCMPB0. rTCON=0x9; //timer0 = auto reload, start */ //the following code is for 44B0X rTCFG0=0x0f0f0f; rTCFG1=0x11111; rTCNTB0= _CLOCK; rTCMPB0= 0x0; rTCON=0x2; //update mode for TCNTB0 and TCMPB0. rTCON=0x9; //timer0 = auto reload, start }//uHALr_InitTimersint 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}//BreakPointvoid 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 __Tick(void);/*** Called by ARMTargetStart() ***///Request the system timer.//return value 1:success 0:failint uHALr_RequestSystemTimer(void *tick, const unsigned char *str){ pISR_TIMER0= (unsigned)__Tick; pISR_UNDEF= (unsigned) DebugUNDEF; pISR_SWI= (unsigned) DebugSWI; pISR_PABORT= (unsigned) DebugABORT; pISR_DABORT= (unsigned) DebugABORT;// pISR_RESERVED// pISR_IRQ= (unsigned) 0; pISR_FIQ= (unsigned) DebugFIQ; 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; return 1;}//uHALr_RequestSystemTimer//Start system timer & enable the interrupt.void uHALr_InstallSystemTimer(void){ //Non-vectored,IRQ enable,FIQ disable rINTCON=0x5; //Non maksed TIMER0 . rINTMSK=~(BIT_TIMER0 | 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 */ /* GPA9 GPA8 ADDR22 ADDR21 GPA5 ADDR19 ADDR18 ADDR17 ADDR16 GPA0 */ /* 0, 0, 1, 1, 0, 1, 1, 1, 1, 0 */ /* GPA9->SMCALE*/ /* GPA8->SMCCLE*/ /* GPA5->SMCCE*/ /* GPE0->SMCRB*/ rPCONA = 0xde; // 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 */ /* (Reserve) IDE USB FLASH SRAM No care No Care 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 I O nDREQ1 nDACK1(used as output IO) */ /* LCD_BK LCDDSP Uart1 Uart1 tchPen tchScrCS USB USB */ /* 01 01 11 11 00 01 00 01 */ /* PC 7 6 5 4 3 2 1 0*/ /* O O O I I I I I*/ /* VD4 VD5 VD6 VD7 key key key key*/ /* 11 11 11 11 00 00 00 00*/ rPDATC = 0x3fff; //All IO is high rPCONC = 0x5f11ff00; rPUPC = 0x3000; //PULL UP RESISTOR should be enabled to I/O /* PORT D GROUP(I/O OR LCD) */ /* BIT 7 6 5 4 3 2 1 0 */ /* 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 O O O O Res RXD0 TXD0 SMRB(I) */ /* 10 01 01 01 01 00 10 10 00 */ rPDATE = 0x1ff; rPCONE = 0x25528; rPUPE = 0x106; //PORT F GROUP /* Bit 8 7 6 5 4 3 2 1 0 */ /* SIOCLK SIORxD GPF6(i) SIOTxD Input Input O IICSDA IICSCL */ /* 011 011 000 011 00 00 01 10 10 */ rPDATF = 0x0; rPCONF = 0x1B0C1A;//0x24900a; rPUPF = 0x0;//0x1e3; //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 */ rPDATG = 0xff; rPCONG = 0xffff; rPUPG = 0x0; //should be enabled rSPUCR=0x7; //D15-D0 pull-up disable/*定义非Cache区*/ rNCACHBE0=((Non_Cache_End>>12)<<16)|(Non_Cache_Start>>12); /*所有的外部硬件中断为低电平触发*/ rEXTINT=0x0;/*对usbn9603进行硬件复位*/ rPDATC=(rPDATC & 0xffdf) | ((0)<<5); rPDATE=rPDATE&0x1FE; Delay(1000); rPDATC=(rPDATC & 0xffdf) | ((1)<<5); rPDATE=rPDATE|0x1; // rPDATA = 0x3ff;//A18-> LCD Reset is Pull High}/************************* PLL ********************************/void ChangePllValue(int mdiv,int pdiv,int sdiv)//add by eric rong{ rPLLCON=(mdiv<<12)|(pdiv<<4)|sdiv;}/************************* UART ****************************/void Uart_Init(int Uartnum, int mclk,int baud){ int i; if(mclk==0) mclk=MCLK; if(Uartnum==0){//UART0 rUFCON0=0x0; //FIFO disable rUMCON0=0x0; //UART0 rULCON0=0x3; //Normal,No parity,1 stop,8 bit rUCON0=0x245; //rx=edge,tx=level,disable timeout int.,enable rx error int.,normal,interrupt or polling rUBRDIV0=( (int)(mclk/16./baud + 0.5) -1 ); } else{ rUFCON1=0x0; rUMCON1=0x0; //UART1 rULCON1=0x3; rUCON1=0x245; rUBRDIV1=( (int)(mclk/16./baud + 0.5) -1 ); } for(i=0;i<100;i++);}void Uart_SendString(int Uartnum, char *pt){ while(*pt){ if(*pt=='\n'){ Uart_SendByte(Uartnum, '\r'); Uart_SendByte(Uartnum, *pt++); } else Uart_SendByte(Uartnum, *pt++); }}//if you don't use vsprintf(), the code size is reduced very much.void Uart_Printf(char *fmt,...){ va_list ap; char string[256]; va_start(ap,fmt); vsprintf(string,fmt,ap); Uart_SendString(0, string); va_end(ap);}void Uart_TxEmpty(int Uartnum){ if(Uartnum==0) while(!(rUTRSTAT0 & 0x4)); //wait until tx shifter is empty. else while(!(rUTRSTAT1 & 0x4)); //wait until tx shifter is empty.}char Uart_Getch(char* Revdata, int Uartnum, int timeout){ int i=0; if(Uartnum==0){ while(!(rUTRSTAT0 & 0x1)){ //Receive data read OSTimeDly(1); if(timeout==0) continue; if(++i>=timeout) return FALSE; } *Revdata=RdURXH0(); return TRUE; } else{ while(!(rUTRSTAT1 & 0x1)){ //Receive data read OSTimeDly(1); if(timeout==0) continue; if(++i>=timeout) return FALSE; } *Revdata=RdURXH1(); return TRUE; }}/************************* General Library **********************/void Cache_Flush(void){ int i,saveSyscfg; saveSyscfg=rSYSCFG; rSYSCFG=SYSCFG_0KB; for(i=0x10004000;i<0x10004800;i+=16) { *((int *)i)=0x0; } rSYSCFG=saveSyscfg; }/************************* Timer ********************************/void Timer_Start(int divider) //0:16us,1:32us 2:64us 3:128us{ rWTCON=((MCLK/1000000-1)<<8)|(divider<<3); rWTDAT=0xffff; rWTCNT=0xffff; // 1/16/(65+1),nRESET & interrupt disable rWTCON=((MCLK/1000000-1)<<8)|(divider<<3)|(1<<5); }int Timer_Stop(void){ rWTCON=((MCLK/1000000-1)<<8); return (0xffff-rWTCNT);}/*void (*OSUart0RevFunction)(U8) ;void (*OSUart1RevFunction)(U8) ;OS_EVENT* Uart1_Receive_MBox;__irq void Uart0_ReceiveData(){}__irq void Uart1_ReceiveData(){ IrqStart(); rI_ISPC|=BIT_URXD1; (*OSUart1RevFunction)(RdURXH1()); IrqFinish();}void EnableURXD(int Uartnum,void *func){ if(Uartnum==0){ pISR_URXD0=(unsigned) Uart0_ReceiveData; rINTMSK=(rINTMSK&(~BIT_URXD0)); } else{ pISR_URXD1=(unsigned)func;// Uart1_ReceiveData; OSUart1RevFunction=(void (*)(U8))func; rINTMSK=(rINTMSK&(~BIT_URXD1));// Uart1_Receive_MBox=OSMboxCreate(NULL); }}*/void init_SIO(){ /*7 6 5 4 3 2 1:0 */ /*Internal clock,MSB mode,Transmit/Receive mode,rising edge clock,No action,Non hand-shaking mode,SIO interrupt mode*/ /*0 0 1 1 0 0 01*/ rSIOCON=0x31; rSBRDR=15;//band rate = 60MHz/2/(15+1)=1.875MHz rIVTCNT=7;//Intervals=60MHz/4/(7+1)=1.875MHz}unsigned char ReadSIOData(){// while(rSIOCON&SIO_START);//等待发送 return rSIODAT;}void SendSIOData(unsigned char data){// while(rSIOCON&SIO_START);//等待发送 rI_ISPC=BIT_SIO; rSIODAT=data; rSIOCON|=SIO_START; while(!(rINTPND&BIT_SIO)); rI_ISPC=BIT_SIO;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -