⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uhal.c

📁 ADS开发环境下的基础实验,代码简单易懂,适合学习.
💻 C
字号:

#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*/
	//								~~input for bios
	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;
}
/************************* PLL ********************************/
void ChangePllValue(int mdiv,int pdiv,int sdiv)//add by eric rong
{
    rPLLCON=(mdiv<<12)|(pdiv<<4)|sdiv;
}
/************************* 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 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=0x21;
	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;
}

/***************************************************************
	SIOTXD和SIORXD通过两个三态门连接,组成了SDIO的双向接口,
	通过GPF2来控制三态门的开启,从而控制SDIO的方向,
	对于处理器,GPF2=0的时候为输出,GPF2=1的时候为输入
	
****************************************************************/
#define SDIO_CTRLIO			(0x4)	//GPF2
#define SETSDIO_OUT()		(rPDATF&=(~SDIO_CTRLIO))
#define SETSDIO_IN()			(rPDATF|=SDIO_CTRLIO)

unsigned char ReadSDIO()
{
	SETSDIO_IN();
	SendSIOData(0);
	SETSDIO_OUT();

	return rSIODAT;
}

void ARMTargetInit(void)
{

/////////////////////////////////////////////////////////////////
		rSYSCFG=CACHECFG;	// Using 8KB Cache//
		ChangePllValue(PLL_M,PLL_P,PLL_S);
		Port_Init();
		Uart_Init(0,0,115200);
		Uart_Init(1,0,9600);
#ifdef DEBUG
		Uart_Printf("\nBegain to run uC/OS-II!\n");
#endif
		uHALr_InitInterrupts();

		uHALr_InitTimers();
		uHALr_InterruptRequestInit();
/////////////////////////////////////////////////////////////////

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -