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

📄 gp_timers.c.svn-base

📁 Spearhead2000 的 USB驱动程序
💻 SVN-BASE
字号:
/********************************************************************************                                                                           ****  Copyright (c) 1999 ST Microelectronics                                   ****  All rights reserved                                                      ****                                                                           ****      Filename  :  gp_timers.c                                             ****      Author    :  Armando Visconti                                        ****      Revision  :  1.0                                                     ****                                                                           ****                                                                           ****                                                                           *********************************************************************************/#include "gp_timers.h"#include "vic_pl190.h"#include "uart.h"#include "gpio.h"/*static volatile unsigned int gpt2_done = 0;static volatile unsigned int gpt3_done = 0;static volatile unsigned int gpt4_done = 0; */volatile unsigned int tmr1_count = 0;volatile unsigned int tmr2_count = 0;volatile unsigned int tmr3_count = 0;volatile unsigned int tmr4_count = 0;unsigned int seconds = 0;/******************** set timer routine ****************************/void gptSetTimer( unsigned int timer, unsigned int control, unsigned int value){    switch(timer)    {    case TIMER1:    	 GPT1Cntl->TIMER_COMPARE_REG = value;    	 GPT1Cntl->TIMER_CONTROL_REG = control;         break;    case TIMER2:    	 GPT2Cntl->TIMER_COMPARE_REG = value;    	 GPT2Cntl->TIMER_CONTROL_REG = control;         break;    case TIMER3:    	 GPT3Cntl->TIMER_COMPARE_REG = value;    	 GPT3Cntl->TIMER_CONTROL_REG = control;         break;    case TIMER4:    	 GPT4Cntl->TIMER_COMPARE_REG = value;    	 GPT4Cntl->TIMER_CONTROL_REG = control;         break;    }}/*******************************************************************//******************* second to print_time **************************/void sec_to_pt(int sec){	int h=0, m=0, s=0;	int app=0;	if(sec == 3000)	{			UART_tx_vec("3 Second Completed",strlen("3 Second Completed"));	}	s = sec%60;	app = sec/60;	m = app%60;	app /= 60;	h = app%60;	//UART_print_time(h, m, s);}/*******************************************************************//******************** interrupt routine ****************************/void gptInterrupt(unsigned int vector){    if (vector & ITC_GPT1_int)    {        GPT1Cntl->TIMER_STATUS_RES_INT_ACK = GPT_MATCH; // MATCH interrupt	  tmr1_count++;	  if(tmr1_count >= 4)	  {		tmr1_count = 0;		sec_to_pt(seconds);	  	++seconds;	  }	/*  if(tmr1_count == 1000)	  {		  UART_tx_vec("One Second Completed",strlen("One Second Completed"));	  }*/    }    if (vector & ITC_GPT2_int)    {        GPT2Cntl->TIMER_STATUS_RES_INT_ACK = GPT_MATCH; // MATCH interrupt	  tmr2_count++;	  if(tmr2_count >= 4)	  {		tmr2_count = 0;		sec_to_pt(seconds);	  	++seconds;	  }    }    if (vector & ITC_GPT3_int)    {        GPT3Cntl->TIMER_STATUS_RES_INT_ACK = GPT_MATCH; // MATCH interrupt	  tmr3_count++;	  if(tmr3_count >= 4)	  {		tmr3_count = 0;		sec_to_pt(seconds);	  	++seconds;	  }    }    if (vector & ITC_GPT4_int)    {        GPT4Cntl->TIMER_STATUS_RES_INT_ACK = GPT_MATCH; // MATCH interrupt	  tmr4_count++;	  if(tmr4_count >= 4)	  {		tmr4_count = 0;		sec_to_pt(seconds);	  	++seconds;	  }    }/*    if (vector & ITC_GPT2_int)    {        GPT2Cntl->TIMER_STATUS_RES_INT_ACK = GPT_MATCH; // MATCH interrupt        gpt2_done = 1;        gpioWrite(2, 0);        gpioWrite(2, 1);        gpioWrite(2, 1);        gpioWrite(2, 1);        gpioWrite(2, 1);        gpioWrite(2, 1);        gpioWrite(2, 1);        gpioWrite(2, 0);    }                          */}/*******************************************************************//************ Interrupt enabling and GPT2 in free run **************/void gptInit(void){    intctlIntRegister(ITC_GPT1_int, gptInterrupt, 0);    intctlIntRegister(ITC_GPT2_int, gptInterrupt, 0);    intctlIntRegister(ITC_GPT3_int, gptInterrupt, 0);    intctlIntRegister(ITC_GPT4_int, gptInterrupt, 0);    /* Timer 2,3,4 are used as a free running counter *//*    gptSetTimer(TIMER2, GPT_ENAB |                        GPT_FREQ_3Mhz |			GPT_AUTO_RELOAD |			GPT_CAPT_NONE |			GPT_MATCH_INT, 1500);    gptSetTimer(TIMER3, GPT_ENAB |                        GPT_FREQ_3Mhz |			GPT_AUTO_RELOAD |			GPT_CAPT_NONE |			GPT_MATCH_INT, 1500);    gptSetTimer(TIMER4, GPT_ENAB |                        GPT_FREQ_3Mhz |			GPT_AUTO_RELOAD |			GPT_CAPT_NONE |			GPT_MATCH_INT, 1500);     */}/*******************************************************************/#if 0/****************** test to set GPT by SCOPE ***********************/void gptTest(void){    	static char in_buf;	short freq = 0x0000;	unsigned int prescaler;	while (!(UART_getc(&in_buf)));	switch(in_buf)	{	case '0': freq = GPT_FREQ_48Mhz;    break;	case '1': freq = GPT_FREQ_24Mhz;    break;	case '2': freq = GPT_FREQ_12Mhz;    break;	case '3': freq = GPT_FREQ_6Mhz;     break;	case '4': freq = GPT_FREQ_3Mhz;     break;	case '5': freq = GPT_FREQ_1_5Mhz;   break;	case '6': freq = GPT_FREQ_750Khz;   break;	case '7': freq = GPT_FREQ_375Khz;   break;	case '8': freq = GPT_FREQ_187_5Khz; break;	default:  freq = GPT_FREQ_3Mhz;     break;	}	while (!(UART_getc(&in_buf)));	switch(in_buf)	{	case '0':prescaler = 1;      break;	case '1':prescaler = 10;     break;	case '2':prescaler = 100;    break;	case '3':prescaler = 1000;   break;	case '4':prescaler = 10000;  break;	default:prescaler =  0xffff; break;	}	while (!(UART_getc(&in_buf)));	if(in_buf == 'r') gptSetTimer(TIMER2, GPT_ENAB | freq | GPT_AUTO_RELOAD | GPT_CAPT_NONE | GPT_MATCH_INT, prescaler);	while(in_buf != 's') UART_getc(&in_buf);	GPT1Cntl->TIMER_CONTROL_REG &= ~GPT_ENAB;}/*******************************************************************/#endif/************** implements a watch on UART *************************/void gptWatch(void){    	static char in_buf;	while (!(UART_getc(&in_buf)));	if(in_buf == 'r')	{	gptSetTimer(TIMER1,		GPT_ENAB |		GPT_FREQ_259_766Khz |		GPT_AUTO_RELOAD |		GPT_CAPT_NONE |		GPT_MATCH_INT,		0xf428);	while(in_buf != 's') UART_getc(&in_buf);	GPT1Cntl->TIMER_CONTROL_REG &= ~GPT_ENAB;	seconds = 0;	}}/*******************************************************************//************** implements a watch on UART *************************/void gpt_test(void){    	static char schedule;    	static char select;    	unsigned int timer_sel;	unsigned int speed;	schedule = 0;	UART_tx_vec("Select timer... (s to stop)",strlen("Select timer... (s to stop)"));	UART_tx_vec("1 for TIMER1; 2 for TIMER2; 3 for TIMER3; 4 for TIMER4;",strlen("1 for TIMER1; 2 for TIMER2; 3 for TIMER3; 4 for TIMER4;"));	UART_tx_vec("  1/1000 sec;    1/100 sec;     1/10 sec;        1 sec;",strlen("1 for TIMER1; 2 for TIMER2; 3 for TIMER3; 4 for TIMER4;"));	while (!(UART_getc(&select)));	switch(select)	{		case '1':			timer_sel = TIMER1;			speed = 0x40;			UART_tx_vec("TIMER1 selected",strlen("TIMER1 selected"));			intctlIntRegister(ITC_GPT1_int, gptInterrupt, 0);		break;		case '2':			timer_sel = TIMER2;			speed = 0x289;			UART_tx_vec("TIMER2 selected",strlen("TIMER2 selected"));			intctlIntRegister(ITC_GPT2_int, gptInterrupt, 0);		break;		case '3':			timer_sel = TIMER3;			speed = 0x195e;			UART_tx_vec("TIMER3 selected",strlen("TIMER3 selected"));			intctlIntRegister(ITC_GPT3_int, gptInterrupt, 0);		break;		case '4':			timer_sel = TIMER4;			speed = 0xfdad;			UART_tx_vec("TIMER4 selected",strlen("TIMER4 selected"));			intctlIntRegister(ITC_GPT4_int, gptInterrupt, 0);		break;		default:			timer_sel = 5;			speed = 0xfdad;			UART_tx_vec("no timer selected",strlen("no timer selected"));		break;	}	gptSetTimer(timer_sel, GPT_ENAB|GPT_FREQ_259_766Khz|GPT_AUTO_RELOAD|GPT_CAPT_NONE|GPT_MATCH_INT, speed);	while(schedule != 's') UART_getc(&schedule);	//APB_show_regs((unsigned int*)0x12001080, (unsigned int*)0x00020080, 6);//	APB_show_regs((unsigned int*)0x12001100, (unsigned int*)0x00020100, 6);	switch(select)	{	case '1':GPT1Cntl->TIMER_CONTROL_REG &= ~GPT_ENAB; break;	case '2':GPT2Cntl->TIMER_CONTROL_REG &= ~GPT_ENAB; break;	case '3':GPT3Cntl->TIMER_CONTROL_REG &= ~GPT_ENAB; break;	case '4':GPT4Cntl->TIMER_CONTROL_REG &= ~GPT_ENAB; break;	}//	APB_show_regs((unsigned int*)0x12001080, (unsigned int*)0x00020080, 6);//	APB_show_regs((unsigned int*)0x12001100, (unsigned int*)0x00020100, 6);	seconds = 0;	while (!(UART_putc(13)));	while (!(UART_putc(10)));}/*******************************************************************/unsigned intgptGetTimerVal(unsigned int timer){    switch(timer)    {    case TIMER1:         return(GPT1Cntl->TIMER_COUNT_REG);    case TIMER2:         return(GPT2Cntl->TIMER_COUNT_REG);    case TIMER3:         return(GPT3Cntl->TIMER_COUNT_REG);    case TIMER4:         return(GPT4Cntl->TIMER_COUNT_REG);    }    return(0);}

⌨️ 快捷键说明

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