📄 timer.c
字号:
//========================================================
// Timer Interrupt 0/1/2/3/4 test
//========================================================
void Test_TimerInt(void)
{
variable0 = 0;variable1 = 0;variable2 = 0;variable3 = 0;variable4 = 0;
// timer interrupt enable
// timer 3/4 source select with sub register - ows
//printf("rINTSUBMSK (Before) = 0x%8x\n",rINTSUBMSK);
rINTSUBMSK = ~( BIT_SUB_TIMER3 | BIT_SUB_TIMER4);
//printf("rINTMSK (Before) = 0x%8x\n",rINTMSK);
rINTMSK = ~( BIT_TIMER34_WDT| BIT_TIMER2 | BIT_TIMER1 | BIT_TIMER0);
//printf("rINTMSK (After) = 0x%8x <= Timer4,3,2,1 Bit[14:10]\n",rINTMSK);
pISR_TIMER0 = (int)Timer0Done;
pISR_TIMER1 = (int)Timer1Done;
pISR_TIMER2 = (int)Timer2Done;
pISR_TIMER34_WDT= (int)Timer3_4Done;
// pISR_TIMER3 = (int)Timer3Done;
// pISR_TIMER4 = (int)Timer4Done;
printf("\n[ Timer 0,1,2,3,4 Interrupt Test ]\n\n");
rTCFG0 = rTCFG0 & ~(0xffffff) | 0x000f0f; //Dead zone=0,Prescaler1=15(0x0f),Prescaler0=15(0x0f)
rTCFG1 =rTCFG1 & ~(0xffffff) | 0x001233; //All interrupt,Mux4=1/2,Mux3=1/4,Mux2=1/8,Mux1=1/16,Mux0=1/16
//Timer input clock frequency = PCLK/(prescaler value+1)/(divider value)
rTCNTB0 = 0xffff; //(1/(50MHz/16/16)) * 0xffff (65535) = 0.334s ( 2.994Hz)
rTCNTB1 = 0xffff; //(1/(50MHz/16/16)) * 0xffff (65535) = 0.334s ( 2.994Hz)
rTCNTB2 = 0xffff; //(1/(50MHz/16/8 )) * 0xffff (65535) = 0.163s ( 6.135Hz)
rTCNTB3 = 0xffff; //(1/(50MHz/16/4 )) * 0xffff (65535) = 0.078s (12.820Hz)
rTCNTB4 = 0xffff; //(1/(50MHz/16/2 )) * 0xffff (65535) =0.039s (25.641Hz)
// [22:20] [19:16] [15:12] [11:8] [7:4] [3:0]
// 110 1010 1010 1010 0000 0010
// rTCON = 0x6aaa02; //Auto reload (T0=One-shot), Inverter off, Manual update, Dead zone disable, Stop
rTCON = rTCON & ~(0xffffff) | 0x6aaa0a; //Auto reload, Inverter off, Manual update, Dead zone disable, Stop
// [22:20] [19:16] [15:12] [11:8] [7:4] [3:0]
// 101 1001 1001 1001 0000 0001
rTCON = rTCON & ~(0xffffff) | 0x599901; //Auto reload(T0=One-shot),Inverter off,No operation,Dead zone disable,Start
//printf("Probing PWM TOUT 0 : TP21\n");
//printf("Probing PWM TOUT 1 : TP20\n");
//printf("Probing PWM TOUT 2 : TP19\n");
//printf("Probing PWM TOUT 3 : TP18\n\n");
if(PCLK==50000000)
{
// printf("PCLK 50MHz, Timer TOUT 0 : 0.334s ( 2.994Hz)\n");
// printf("PCLK 50MHz, Timer TOUT 1 : 0.334s ( 2.994Hz)\n");
// printf("PCLK 50MHz, Timer TOUT 2 : 0.163s ( 6.135Hz)\n");
// printf("PCLK 50MHz, Timer TOUT 3 : 0.078s (12.820Hz)\n");
// printf("PCLK 50MHz, Timer TOUT 4 : 0.039s (25.641Hz) <= No Pin Out\n\n");
}
while(variable0 == 0);
Delay(1); //To compensate timer error(<1 tick period)
rTCON = 0x0; //One-shot, Inverter off, No operation, Dead zone disable, Stop
if(variable4 == 8 && variable3==4 && variable2==2 && variable1==1 && variable0==1)
{
printf("Timer 0,1,2,3,4Interrupt Test --> OK\n");
}
else
{
printf("Timer 0,1,2,3,4 Interrupt Test --> Fail............\n");
}
printf("Timer0 : %d (=1), Timer1 : %d (=1), Timer2 : %d (=2), Timer3 : %d (=4),Timer4 : %d (=8) \n",
variable0,variable1,variable2,variable3,variable4);
printf("Press any key to exit.....\n");
while(!Uart_getc()); //Key board press check
// timer interrupt disable
//printf("rINTSUBMSK (Before) = 0x%8x\n",rINTSUBMSK);
rINTSUBMSK = ( BIT_SUB_TIMER3 | BIT_SUB_TIMER4);
//printf("rINTMSK (Before) = 0x%8x\n",rINTMSK);
rINTMSK |= ( BIT_TIMER34_WDT| BIT_TIMER2 | BIT_TIMER1 | BIT_TIMER0);
//printf("rINTMSK (After) = 0x%8x\n",rINTMSK);
}
//========================================================
// Timer Interrupt Request
//========================================================
void __irq Timer0Done(void)
{
rSRCPND = BIT_TIMER0; //Clear pending bit
rINTPND = BIT_TIMER0;
rINTPND; //Prevent an double interrupt pending
variable0++;
}
//---------------------------------------------------------------------
void __irq Timer1Done(void)
{
rSRCPND = BIT_TIMER1; //Clear pending bit
rINTPND = BIT_TIMER1;
rINTPND;
variable1++;
}
//---------------------------------------------------------------------
void __irq Timer2Done(void)
{
rSRCPND = BIT_TIMER2; //Clear pending bit
rINTPND = BIT_TIMER2;
rINTPND;
variable2++;
}
//---------------------------------------------------------------------
void __irq Timer3_4Done(void)
{
if(rSUBSRCPND & 0x00000200){ // timer3 sub interrupt
rSUBSRCPND = BIT_SUB_TIMER3;
variable3++;
}
if(rSUBSRCPND & 0x00000400){ // timer4 sub interrupt
rSUBSRCPND = BIT_SUB_TIMER4;
variable4++;
}
rSRCPND = BIT_TIMER34_WDT; //Clear pending bit
rINTPND = BIT_TIMER34_WDT;
rINTPND;
}
/******************** Timer DMA Request Test ********************/
void Test_TimerDma(void)
{
int i;
rTCFG0 = rTCFG0 & ~(0xffffff) | 0x000021; //PCLK = 67.5MHz , prescaler 0x15= 21 ===>2MHz)
//rTCFG0 = rTCFG0 & ~(0xffffff) | 0x0; //Dead zone=0, Prescaler0,1 = 0
rTCFG1 = rTCFG1 & ~(0xffffff) | 0x0 << 0 | 0x1 << 20; //Mux0 1/16, DMA Request Channel 0
rTCNTB0 = 10000;
rTCMPB0 = 30000;
//rTCNTB1 = 65500;
//rTCMPB1 = 30000;
//rTCNTB2 = 65500;
//rTCMPB2 = 30000;
//rTCNTB3 = 65500;
//rTCMPB3 = 30000;
//rTCNTB4 = 65500;
rTCON = rTCON & ~(0xffffff) | 0x1<<1 | 0x1<<9 | 0x1<<13 | 0x1<<17 | 0x1<<21 ; //Manual update
printf("\nTimer 0 DMA Request Test \n\n");
// timer 0 DMA transfer test
//for(i=0 ; i < 4 ; i++)
M2M_Timer_Test(0, _NONCACHE_STARTADDRESS, _NONCACHE_STARTADDRESS + 0x80000, 0, 0, 0x80000);
printf("Press any key\n");
while(!Uart_getc()); //Key board press check
}
void M2M_Timer_Test(int ch, int s_addr, int d_addr, int burst, int dsz, int tc)
{
int i;
int trscount;
unsigned int tmemSum0=0, tmemSum1=0;
printf("DMA%d start\n",ch);
if(dsz==0)
trscount=tc*(burst ? 4:1)*1;
else if(dsz==1)
trscount=tc*(burst ? 4:1)*2;
else if(dsz==2)
trscount=tc*(burst ? 4:1)*4;
switch(ch)
{
case 0 :
pISR_DMA_SBUS = (int)TDma0Done;
rINTMSK = ~(BIT_DMA_SBUS);
rINTSUBMSK=~(BIT_SUB_DMA0);
break;
case 1 :
pISR_DMA_SBUS = (int)TDma1Done;
rINTMSK = ~(BIT_DMA_SBUS);
rINTSUBMSK=~(BIT_SUB_DMA1);
break;
case 2 :
pISR_DMA_SBUS = (int)TDma2Done;
rINTMSK = ~(BIT_DMA_SBUS);
rINTSUBMSK=~(BIT_SUB_DMA2);
break;
case 3 :
pISR_DMA_SBUS = (int)TDma3Done;
rINTMSK = ~(BIT_DMA_SBUS);
rINTSUBMSK=~(BIT_SUB_DMA3);
break;
}
dmaDone=0;
for(i=s_addr;i<s_addr+trscount;i+=4)
{
*((unsigned int *)i)=i;
tmemSum0+=i;
}
switch(ch)
{
case 0 :
rDISRC0=s_addr;
rDISRCC0=(0<<1)|(0<<0); // the source is in the system bus(AHB)
rDIDST0=d_addr;
rDIDSTC0=(0<<1)|(0<<0);
rDCON0=((unsigned)1<<31)|(1<<30)|(1<<29)|(burst<<28)|(1<<27)|(0x3<<24)|(1<<22)|(dsz<<20)|(tc);
rDMASKTRIG0=(1<<1)|(0<<0);
rDMAREQSEL0=(0x9<<1)|(0x1);//0x13
break;
case 1 :
rDISRC1=s_addr;
rDISRCC1=(0<<1)|(0<<0);
rDIDST1=d_addr;
rDIDSTC1=(0<<1)|(0<<0);
rDCON1=((unsigned)1<<31)|(1<<30)|(1<<29)|(burst<<28)|(1<<27)|(1<<22)|(dsz<<20)|(tc);
rDMASKTRIG1=(1<<1)|(1<<0);
rDMAREQSEL1=(0x9<<1)|(0x1);//0x13
break;
case 2 :
rDISRC2=s_addr;
rDISRCC2=(0<<1)|(0<<0);
rDIDST2=d_addr;
rDIDSTC2=(0<<1)|(0<<0);
rDCON2=((unsigned)1<<31)|(1<<30)|(1<<29)|(burst<<28)|(1<<27)|(1<<22)|(dsz<<20)|(tc);
rDMASKTRIG2=(1<<1)|(1<<0);
rDMAREQSEL2=(0x9<<1)|(0x1);//0x13
break;
case 3 :
rDISRC3=s_addr;
rDISRCC3=(0<<1)|(0<<0);
rDIDST3=d_addr;
rDIDSTC3=(0<<1)|(0<<0);
rDCON3=((unsigned)1<<31)|(1<<30)|(1<<29)|(burst<<28)|(1<<27)|(1<<22)|(dsz<<20)|(tc);
rDMASKTRIG3=(1<<1)|(1<<0);
rDMAREQSEL3=(0x9<<1)|(0x1);//0x13
break;
}
//printf("rDCDST0=%x\n",rDIDST0);
/*
for(i=d_addr;i<d_addr+trscount;i+=4)
{
tmemSum1+=*((unsigned int *)i)=i^0x55aa5aa5;
// printf("dest addr :%x, memory :%x\n",i,*((unsigned int *)i));
}
*/
/* DMA Start */
//Timer_Start(3);//128us resolution
rTCON = 0x1 | 0x1<<3; //Auto reload, Start
while(dmaDone==0);
rTCON = 0x0;
for(i=d_addr;i<d_addr+trscount;i+=4)
{
tmemSum1+=*((unsigned int *)i);
}
//Timer_Stop();
//printf("DMA transfer done.\n");
rINTMSK = BIT_ALLMSK;
rINTSUBMSK=BIT_SUB_ALLMSK1;
printf("memSum0=%x,memSum1=%x\n",tmemSum0,tmemSum1);
if(tmemSum0==tmemSum1)
printf("DMA test result......................O.K.\n");
else
printf("DMA test result......................ERROR.\n");
}
/***** DMA Interrupt Service Routine *****/
void __irq TDma0Done(void)
{
rSUBSRCPND=BIT_SUB_DMA0;
ClearPending(BIT_DMA_SBUS);
dmaDone=1;
}
void __irq TDma1Done(void)
{
rSUBSRCPND=BIT_SUB_DMA1;
ClearPending(BIT_DMA_SBUS);
dmaDone=1;
}
void __irq TDma2Done(void)
{
rSUBSRCPND=BIT_SUB_DMA2;
ClearPending(BIT_DMA_SBUS);
dmaDone=1;
}
void __irq TDma3Done(void)
{
rSUBSRCPND=BIT_SUB_DMA3;
ClearPending(BIT_DMA_SBUS);
dmaDone=1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -