📄 gptimer.c
字号:
/*******************************************************************************
*
* File:
* -----
*
*
* Copyright Statement:
* --------------------
* This software is protected by Copyright and the information contained herein
* is confidential. The software may not be copied and the information
* contained herein may not be used or disclosed except with the written
* permission of Infomax Co., Ltd. (C) 2007
*
* --------
*
* Software Unit: Timer drivers
* --------------
*
* Description: Driver for accessing GP timer
* ------------
*
******************************************************************************/
#include "gptimer.h"
UINT32 GPTIMER_event_set;
/*
* Function prototypes.
**********************
*/
/*******************************************************************************
* Function name : timer_init()
* Description : Initialization procedures for timer
* Parameters:
* Input data :
* timer_id: The identification of general purpose timer(1 or 2)
* time_unit: The time unit for counting down(min: 1/MCU_RefClk, max: 0x7F/MCU_RefClk)
* Output data :
* none
* Returns data :
* none
******************************************************************************/
VOID_FUNC timer_init(UINT8 timer_id)
{
static unsigned short us_cnt_set = 0;
/*check timer id */
if( us_cnt_set == 0 )
{
us_cnt_set = 1;
/* Set micro second counter register according to time_unit */
HW_WRITE(USCNTR_CTL_REG, ONE_MICRO_SEC);
}
if( timer_id == GPTIMER_0)
{
/* Disable/stop timer(write 0x3fffff to timer counter) */
HW_WRITE(GPT_TM0_REG, DISABLE_TIMER);
/* Clear timer interrupt flag */
HW_WRITE(GPT_CTL_REG, (HW_READ(GPT_CTL_REG) | GPTIMER_0_INT_FLAG) );
/* Disable timer interrupt */
HW_WRITE(GPT_CTL_REG, (HW_READ(GPT_CTL_REG) & ~GPTIMER_0_INT_MASK));
}
else if (timer_id == GPTIMER_1)
{
/* Disable/stop timer(write 0x3fffff to timer counter) */
HW_WRITE(GPT_TM1_REG, DISABLE_TIMER);
/* Clear timer interrupt flag */
HW_WRITE(GPT_CTL_REG, (HW_READ(GPT_CTL_REG) | GPTIMER_1_INT_FLAG) );
/* Disable timer interrupt */
HW_WRITE(GPT_CTL_REG, (HW_READ(GPT_CTL_REG) & ~GPTIMER_1_INT_MASK));
}
else /* timer_id is out of legal range */
{
/* raise ERROR message */
while(1);
}
}
/*******************************************************************************
* Function name : timer_ set()
* Description : Set a timer according to the parameters
* Parameters:
* Input data :
* timer_id: The identification of general purpose timer(1 or 2)
* timer_mode: kinds of timer interrupt(one-shot or periodic)
* period: The time for counting down(min: 1 usec, max: 4 sec)
* Output data :
* none
* Returns data :
* none
******************************************************************************/
boolean timer_set(UINT8 timer_id, UINT8 timer_mode, UINT32 period, timer_event_T timer_event)
{
/* check parameters */
if( ( (timer_id != GPTIMER_0) && (timer_id != GPTIMER_1) ) ||
( (timer_mode != ONE_SHOT) && (timer_mode != PERIODIC) ) ||
/* ( (period < MIN_PERIOD) || ( period > MAX_PERIOD) ) || */
( period > DISABLE_TIMER ) ||
!chk_timer_event(timer_event) )
{
/* One of parameter is out of range, raise warning message */
//SYS_WARNING(TRUE, "timer_set(): One of parameter is out of range. \n");
return FALSE;
}
//timer_init(timer_id);
/* Enable timer interrupt */
//timer_control(timer_id, ENABLE_TIMER_INT);
if(timer_id == GPTIMER_0)
{
/*set timer mode(one-shot or periodic) */
if( timer_mode == ONE_SHOT )
{
HW_WRITE(GPT_CTL_REG, HW_READ(GPT_CTL_REG) & ~GPTIMER_0_MODE_BIT );
}
else /* Periodic */
{
HW_WRITE(GPT_CTL_REG, HW_READ(GPT_CTL_REG) | GPTIMER_0_MODE_BIT );
}
/* set period */
HW_WRITE(GPT_TM0_REG, period);
}
else if(timer_id == GPTIMER_1)
{
/*set timer mode(one-shot or periodic) */
if( timer_mode == ONE_SHOT )
{
HW_WRITE(GPT_CTL_REG, HW_READ(GPT_CTL_REG) & ~GPTIMER_1_MODE_BIT );
}
else /* Periodic */
{
HW_WRITE(GPT_CTL_REG, HW_READ(GPT_CTL_REG) | GPTIMER_1_MODE_BIT );
}
/* set period */
HW_WRITE(GPT_TM1_REG, period);
}
/* Set timer event(Turned specified timer event bit) */
GPTIMER_event_set = timer_event;
return TRUE;
}
/*******************************************************************************
* Function name : timer_control()
* Description : Enable or disable a timer interrupt
* Parameters:
* Input data :
* timer_id: The identification of general purpose timer
* action: Enable or disable a timer
* Output data :
* none
* Returns data :
* none
******************************************************************************/
boolean timer_control(UINT8 timer_id, UINT8 action)
{
/* Validate parameters */
if( !chk_timer_id(timer_id)||
( (action != ENABLE_TIMER_INT) && (action != DISABLE_TIMER_INT) )
)
{
/* One of parameter is out of range, raise warning message */
/* raise ERROR message(unknown action) */
//SYS_WARNING(TRUE, "timer_control(): Action is unknown. \n");
return FALSE;
}
if( timer_id == GPTIMER_0)
{
/* Enable/disalbe the timer interrupt */
if( action == DISABLE_TIMER_INT )
{
HW_WRITE(GPT_CTL_REG, HW_READ(GPT_CTL_REG) & ~GPTIMER_0_INT_MASK );
}
else /* ENABLE_TIMER_INT */
{
HW_WRITE(GPT_CTL_REG, HW_READ(GPT_CTL_REG) | GPTIMER_0_INT_MASK );
}
}
else if( timer_id == GPTIMER_1)
{
/* Enable/disalbe the timer interrupt */
if( action == DISABLE_TIMER_INT )
{
HW_WRITE(GPT_CTL_REG, HW_READ(GPT_CTL_REG) & ~GPTIMER_1_INT_MASK );
}
else /* ENABLE_TIMER_INT */
{
HW_WRITE(GPT_CTL_REG, HW_READ(GPT_CTL_REG) | GPTIMER_1_INT_MASK );
}
}
return TRUE;
}
/*******************************************************************************
* Function name : gptimer_status()
* Description : Get the information of GP-timer
* Parameters:
* Input data :
* timer_id: The identification of general purpose timer (1or 2)
* Output data :
* none
* Returns data :
* int_flag: where the timer triggers an interrupt
* int_mask: whether the timer interrupt is enabled or masked
* timer_mode: the mode set to the timer is one-shot or periodic
* timer_running: TRUE if a timer is currently counting; FALSE if the timer is in stop/disable state
******************************************************************************/
boolean gptimer_status(UINT8 timer_id, gptimer_status_T *gptimer_status_data_p)
{
/* check timer_id */
if( chk_timer_id(timer_id) )
{
/* timer id is out of legal range */
/* raise ERROR message */
//SYS_WARNING(TRUE, "timer_status(): timer_id is out of range. \n");
return FALSE;
}
if( timer_id == GPTIMER_0 )
{
/* Read GP timer control/status register to obtain
* 1. interrupt flag
* 2. interrupt mask
* 3. timer mode
*/
gptimer_status_data_p->int_flag =( ( (HW_READ(GPT_CTL_REG) & GPTIMER_0_INT_FLAG) == GPTIMER_0_INT_FLAG)?INT_FLAG_ON: INT_FLAG_OFF);
gptimer_status_data_p->int_mask =( ( (HW_READ(GPT_CTL_REG) & GPTIMER_0_INT_MASK) == GPTIMER_0_INT_MASK) ? INT_MASK_ON : INT_MASK_OFF);
gptimer_status_data_p->timer_mode = ( ( (HW_READ(GPT_CTL_REG) & GPTIMER_0_MODE_BIT) == GPTIMER_0_MODE_BIT) ? PERIODIC : ONE_SHOT);
/* Read GP timer counter register */
if( HW_READ(GPT_TM0_REG) != DISABLE_TIMER )
{ /* The value is not equal to 0x3fffff(disable stop state) */
gptimer_status_data_p->timer_running = TRUE;
}
else
{
gptimer_status_data_p->timer_running = FALSE;
}
}
else if( timer_id == GPTIMER_1 )
{
/* Read GP timer control/status register to obtain
* 1. interrupt flag
* 2. interrupt mask
* 3. timer mode
*/
gptimer_status_data_p->int_flag = ( ( (HW_READ(GPT_CTL_REG) & GPTIMER_1_INT_FLAG) == GPTIMER_1_INT_FLAG)?INT_FLAG_ON: INT_FLAG_OFF);
gptimer_status_data_p->int_mask = ( ( (HW_READ(GPT_CTL_REG) & GPTIMER_1_INT_MASK) == GPTIMER_1_INT_MASK) ? INT_MASK_ON : INT_MASK_OFF);
gptimer_status_data_p->timer_mode = ( ( (HW_READ(GPT_CTL_REG) & GPTIMER_1_MODE_BIT) == GPTIMER_1_MODE_BIT) ? PERIODIC : ONE_SHOT);
/* Read GP timer counter register */
if( HW_READ(GPT_TM1_REG) != DISABLE_TIMER )
{ /* The value is not equal to 0x3fffff(disable stop state) */
gptimer_status_data_p->timer_running = TRUE;
}
else
{
gptimer_status_data_p->timer_running = FALSE;
}
}
return TRUE;
}
/*******************************************************************************
* Function name : timer_ISR()
* Description : Interrupt service routine for a general purpose timer
* Parameters:
* Input data :
* none
* Output data :
* none
* Returns data :
* none
******************************************************************************/
void timer_ISR(void)
{
UINT32 GPT_cntl_reg_val;
GPT_cntl_reg_val = HW_READ(GPT_CTL_REG);
/* Disable general purpose timer interrupts */
HW_WRITE(GPT_CTL_REG, HW_READ(GPT_CTL_REG) & DISALBE_ALL_GPTIMER_INT);
//
// /* switch timer_event(the variable is set when calling set_timer()) */
// switch(GPTIMER_event_set)
// {
// case TIMER_TEST:
// /* print some message here */
// break;
//
// case KEY_SCAN_EVENT:
// /* Scan the keypad again */
// handle_keypad();
// break;
//
// case FLASH_WRITE_CHECK:
// /* Action for this case */
// break;
//
// case LCM_DMA_TIMEOUT:
// /* procedure for dealing the timer event */
// break;
//
// default:
// /* Unknow timer event, raise ERROR message */
// SYS_WARNING(TRUE, "timer_ISR(): Error, unknow timer event. \n");
// break;
// }
//
/* Clear the general purpose timer interrupt */
HW_WRITE(GPT_CTL_REG, HW_READ(GPT_CTL_REG) | CLEAR_ALL_GPTIMER_INT);
/* Enable general purpose timer interrupts */
//HW_WRITE(GPT_CTL_REG, HW_READ(GPT_CTL_REG) | ENALBE_ALL_GPTIMER_INT);
/* Restore GPTIMER interrupt setting */
HW_WRITE(GPT_CTL_REG, GPT_cntl_reg_val);
}
/*******************************************************************************
* Function name : chk_timer_id
* Description : Check wheterh the timer id is in legal range(GPtimer 0 or 1)
* Parameters:
* Input data :
* timer_id: the id whcih will be checked
* Output data :
* none
* Returns data :
* TURE: Timer id is in legal range
* FALSE: The timer is is out of legal range
******************************************************************************/
boolean chk_timer_id(UINT8 timer_id)
{
if( timer_id != GPTIMER_0 && timer_id !=GPTIMER_1)
{
return FALSE;
}
else
{
return TRUE;
}
}
/*******************************************************************************
* Function name : chk_timer_event
* Description : Check wheterh the timer id is in legal range(GPtimer 0 or 1)
* Parameters:
* Input data :
* timer_id: the id whcih will be checked
* Output data :
* none
* Returns data :
* TURE: Timer id is in legal range
* FALSE: The timer is is out of legal range
******************************************************************************/
boolean chk_timer_event(UINT32 timer_event)
{
if( timer_event > MAX_TIMER_EVENT_ID)
{
return FALSE;
}
else
{
return TRUE;
}
}
/* Note: The time will be accurate when sleep_us is larger than 1000. */
void gpt_sleep_us(unsigned int sleep_us)
{
unsigned int remaining_time_us;
unsigned int count_down_val;
remaining_time_us = sleep_us - 1;
timer_init(GPTIMER_1);
timer_control(GPTIMER_1, DISABLE_TIMER_INT);
timer_set(GPTIMER_1, ONE_SHOT, DISABLE_TIMER, TIMER_TEST);
while(remaining_time_us > MAX_PERIOD)
{
//timer_set(GPTIMER_1, ONE_SHOT, MAX_PERIOD, TIMER_TEST);
HW_WRITE(GPT_TM1_REG, MAX_PERIOD);
do
{
count_down_val = HW_READ(GPT_TM1_REG);
}
//while(count_down_val && count_down_val!=DISABLE_TIMER);
while(count_down_val!=DISABLE_TIMER);
remaining_time_us -= MAX_PERIOD;
}
if(remaining_time_us > 0)
{
//timer_set(GPTIMER_1, ONE_SHOT, remaining_time_us, TIMER_TEST);
HW_WRITE(GPT_TM1_REG, remaining_time_us);
do
{
count_down_val = HW_READ(GPT_TM1_REG);
}
//while(count_down_val && count_down_val!=DISABLE_TIMER);
while(count_down_val!=DISABLE_TIMER);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -