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

📄 uhal.c

📁 一个基于S3C44B0芯片的示例代码,实现利用定时器中断点亮LED指示灯,UART通信功能
💻 C
字号:
/*********************************************************************************************
* File:		uhal.c
* Author:	embest
* Desc:		
* History:	
*********************************************************************************************/
#include "uhal.h"
#include "44b.h"
#include "44blib.h"

/*------------------------------------------------------------------------------------------*/
/*	 								constant define					 				    	*/
/*------------------------------------------------------------------------------------------*/
#define	OS_TICK		1000							//  1/1000 sec
#define OS_CLOCK	(200000/OS_TICK/10)               //(165*(1000/OS_TICK))

/*------------------------------------------------------------------------------------------*/
/*	 								function declare					 				    */
/*------------------------------------------------------------------------------------------*/
void breakpoint(void);
void debug_undef(void);
void debug_swi(void);
void debug_abort(void);
void debug_fiq(void);
void URXD0_Isr(void) __irq;
void OSTickISR(void) __irq;
void Timer2ISR(void) __irq;
void __irq iic_int(void);
void uhal_enable_int(unsigned ISPC_BIT);
void uhal_start_timers(void);
extern unsigned char _iicData[IICBUFSIZE];
extern volatile int _iicDataCount;
extern volatile int _iicStatus;
extern volatile int _iicMode;
extern int _iicPt;
extern int f_nGetACK;
unsigned  int k,i; 
/*********************************************************************************************
* name:		uhal_init_interrupts
* func:		Initialze interrupts.
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void uhal_init_interrupts(void)
{	
	rINTCON	= 0x1;	  								// Vectored mode, IRQ disable, FIQ disable    
    rINTMOD	= 0x0;	  								// All=IRQ mode
    rINTMSK	= 0x07FFFFFF;	  						// All interrupt is masked.
    rI_ISPC = 0xffffffff;							// Clear all interrupt pend

	// Set interrupt vector routine
//	pISR_RESET				//reserved
	pISR_UNDEF		= (unsigned) debug_undef;
	pISR_SWI		= (unsigned) debug_swi;
	pISR_PABORT		= (unsigned) debug_abort;
	pISR_DABORT		= (unsigned) debug_abort;
	pISR_RESERVED	= (unsigned) breakpoint;		// not used
//	pISR_IRQ		= (unsigned) 0;					// reserved
	pISR_FIQ		= (unsigned) debug_fiq;
	
	pISR_ADC		= (unsigned) breakpoint;
	pISR_RTC		= (unsigned) breakpoint;
	pISR_UTXD1		= (unsigned) breakpoint;
	pISR_UTXD0		= (unsigned) breakpoint;
	pISR_SIO		= (unsigned) breakpoint;
	pISR_IIC        = (unsigned) iic_int;
	pISR_URXD1		= (unsigned) breakpoint;
	pISR_URXD0		= (unsigned) URXD0_Isr;
	pISR_TIMER5		= (unsigned) breakpoint;
	pISR_TIMER4		= (unsigned) breakpoint;
	pISR_TIMER3		= (unsigned) breakpoint;
	pISR_TIMER2		= (unsigned) Timer2ISR;
	pISR_TIMER1		= (unsigned) breakpoint;
	pISR_TIMER0		= (unsigned) OSTickISR;
	pISR_UERR01		= (unsigned) breakpoint;
	pISR_WDT		= (unsigned) breakpoint;
	pISR_BDMA1		= (unsigned) breakpoint;
	pISR_BDMA0		= (unsigned) breakpoint;
	pISR_ZDMA1		= (unsigned) breakpoint;
	pISR_ZDMA0		= (unsigned) breakpoint;
	pISR_TICK		= (unsigned) breakpoint;
	pISR_EINT4567	= (unsigned) breakpoint;
	pISR_EINT3		= (unsigned) breakpoint;
	pISR_EINT2		= (unsigned) breakpoint;
	pISR_EINT1		= (unsigned) breakpoint;
	pISR_EINT0		= (unsigned) breakpoint;

	uhal_enable_int(BIT_URXD0);
	uhal_enable_int(BIT_TIMER0);
	uhal_enable_int(BIT_TIMER2); 	
}

/*********************************************************************************************
* name:		uhal_register_irq
* func:		
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
#define IRQBASE       _ISR_STARTADDRESS+4*8
#define IRQVECTOR(x)  (IRQBASE + ((unsigned int)x)*4)
void uhal_register_irq(NON_VECTOR_HANDLER IrqSrc, void* pIRQService)
{
  *(unsigned *)(IRQVECTOR(IrqSrc)) = (unsigned)pIRQService;
}

/*********************************************************************************************
* name:		uhal_enable_int
* func:		
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void uhal_enable_int(unsigned ISPC_BIT)
{
	rINTMSK &= ~(ISPC_BIT | BIT_GLOBAL);
}

/*********************************************************************************************
* name:		uhal_init_timers
* func:		Initialize timer that is used OS.
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void uhal_init_timers(void)
{
	rTCFG0 = 0x00009595;  							// dead zone=0, pre0= 100
	rTCFG1 = 0x00000303;  							// all interrupt, mux0= 1/4
	rTCNTB0= OS_CLOCK;     							// set T0 count
	rTCNTB2= OS_CLOCK;                              //set T2 cont
    rTCON  = 0x00002002;   							// update T0
	uhal_start_timers();
    rINTPND= 0x00002800; 							// T0 interrupt clear
}

/*********************************************************************************************
* name:		uhal_start_timers
* func:		Start timer that is used OS.
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void uhal_start_timers(void)
{
	rTCON  = 0x00009009;   							// T0,auto reload and start
}

/*********************************************************************************************
* name:		breakpoint
* func:		exception dealing, beep and light the LEDs
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void breakpoint(void)
{
	rPCONB = 0x1cf;											// PB4:LED1 PB5:LED2
	rPCONF = 0x24914A;										// PF3:LED4 PF4:LED3 PF2:nWait
	rPCONE = 0x25468;										// PE3:BUZZER	

	timer_start(1);											// reset, nDivider=0:16us,1:32us 2:64us 3:128us
	for(;;)
	{
		rPDATE = rPDATE|0xF;								// close beep
		rPDATB = rPDATB|0xFF;								// close LED 1,2 (D1204,D1205)
		rPDATF = rPDATF|0xFF;								// close LED 3,4 (D1206,D1207)
		delay(500);

		rPDATB = rPDATB&0x4F;								// LED 1,2 (D1204,D1205) on
		rPDATF = rPDATF&0xE7;								// LED 3,4 (D1206,D1207) on
		rPDATE = rPDATE&0xF7;								// Beep
		delay(10000);
	}
}

void debug_undef(void)
{
	uart_sendstring("!!!Enter UNDEFINED.\r\n");	
	breakpoint();
}

void debug_swi(void)
{
	uart_sendstring("!!!Enter SWI.\r\n");	
	breakpoint();
}

void debug_abort(void)
{
	uart_sendstring("!!!Enter ABORT\r\n");	
	breakpoint();
}

void debug_fiq(void)
{
	uart_sendstring("!!!Enter FIQ.\r\n");	
	breakpoint();
}

/*********************************************************************************************
* name:		URXD0_Isr
* func:		Uart interrupt handle
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void URXD0_Isr(void) __irq
{    
    rI_ISPC |=BIT_URXD0;    
    rINTMSK = rINTMSK | BIT_URXD0;
    
    while((rUFSTAT0&0xf)>0)
    {
	   //接收数据部分
       uart_sendbyte(rURXH0);                   
    }
    
    rINTMSK = rINTMSK & (~BIT_URXD0);      
}

/*********************************************************************************************
* name:		OSTickISR
* func:		时钟节拍,定时处理事务
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void OSTickISR(void) __irq
{    
    rI_ISPC |=BIT_TIMER0;    
    rINTMSK = rINTMSK | BIT_TIMER0;
	k++;
	if(k>1000)
	{
	  uart_printf("\n\r Timer0 Running!");
	  k=0;
	}
    if(k<500)
      rPDATC =rPDATC|0x00000008;	//D3灯闪烁
    if(k>=500)
     {
	  rPDATC =rPDATC&0xfffffff7;
	  
	 }
    rINTMSK = rINTMSK & (~BIT_TIMER0); 
	    
}  
void Timer2ISR(void) __irq
{
	rI_ISPC |=BIT_TIMER2;
    rINTMSK = rINTMSK | BIT_TIMER2;
	i++;
	if(i>1000)
	 i=0;
    if(i<500)
      rPDATC =rPDATC|0x00000004;	//D2灯闪烁
    if(i>=500)
      rPDATC =rPDATC&0xfffffffb;	
    rINTMSK = rINTMSK & (~BIT_TIMER2); 
}

/*
void __irq IicInt(void)
{
    unsigned long iicSt,i;
    rI_ISPC=BIT_IIC;

    iicSt=rIICSTAT; 
    if(iicSt&0x8){} // when bus arbitration is failed.
    if(iicSt&0x4){} // when a slave address is matched with IICADD
    if(iicSt&0x2){} // when a slave address is 0000000b
    if(iicSt&0x1){} // when ACK isn't received

    switch(_iicMode)
    {
	case POLLACK:
	    _iicStatus=iicSt;
	    break;

	case RDDATA:
	    if((_iicDataCount--)==0)
	    {
		_iicData[_iicPt++]=rIICDS;
	    
		rIICSTAT=0x90;  //stop MasRx condition 
		rIICCON=0xaf;   //resumes IIC operation.
		delay(1);	//wait until stop condtion is in effect.
				//too long time... 
		//The pending bit will not be set after issuing stop condition.
		break;    
	    }	     
	    _iicData[_iicPt++]=rIICDS;
				//The last data has to be read with no ack.
	    if((_iicDataCount)==0)
		rIICCON=0x2f;	//resumes IIC operation with NOACK.  
	    else 
		rIICCON=0xaf;	//resumes IIC operation with ACK
	    break;

	case WRDATA:
	    if((_iicDataCount--)==0)
	    {
		rIICSTAT=0xd0;	//stop MasTx condition 
		rIICCON=0xaf;	//resumes IIC operation.
	    delay(1);	//wait until stop condtion is in effect.
		//The pending bit will not be set after issuing stop condition.
		break;    
	    }
	    rIICDS=_iicData[_iicPt++];  //_iicData[0] has dummy.
	    for(i=0;i<10;i++);	    //for setup time until rising edge of IICSCL
	    rIICCON=0xaf;	    //resumes IIC operation.
	    break;

	case SETRDADDR:
	    //Uart_Printf("[S%d]",_iicDataCount);
	    if((_iicDataCount--)==0)
	    {
		break;  //IIC operation is stopped because of IICCON[4]    
	    }
	    rIICDS=_iicData[_iicPt++];
	    for(i=0;i<10;i++);  //for setup time until rising edge of IICSCL
	    rIICCON=0xaf;	    //resumes IIC operation.
	    break;

	default:
	    break;	  
    }
}
 */
 void __irq iic_int(void)
{
	//uart_printf("\n\r enter the iic_int");
	rI_ISPC=BIT_IIC;
	f_nGetACK = 1;

}

⌨️ 快捷键说明

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