📄 lh7a400_timer_driver.c
字号:
* LH7A400_map.h
*
* Outputs: None
*
* Returns:
* The delay interval that will result given the clocking
* options. Return 0 if the timer is invalid.
*
* Notes:
*
**********************************************************************/
UNS_32 timer_get_delay(TIMERREGS * timer)
{
INT_64 counts_per_second;
if (timer == TIMER1 || timer == TIMER2)
{
if ((timer->control & TIMER_CTRL_508K) == TIMER_CTRL_508K)
{
counts_per_second = CLKSC_TIMER_SEL1_CLK;
}
else
counts_per_second = CLKSC_TIMER_SEL0_CLK;
}
else if (timer == TIMER3)
counts_per_second = CLKSC_TIMER3_CLK;
else
return 0;
return (UNS_32)(((INT_64)(timer->load + 1) * (INT_64)1000000) /
counts_per_second);
}
/**********************************************************************
*
* Function: timer_set_overflow_rate
*
* Purpose:
* Set the timer overflow rate in counts per second.
*
* Processing:
* If timer is TIMER1, TIMER2, or TIMER3, figure out number of
* counts per second (the timer clock as currently configured).
* Divide the counts per second by the overflows_per_second parameter
* to obtain the timer load value.
*
* Parameters:
* timer: Must be one of TIMER1, TIMER2 or TIMER3 as defined in
* LH7A400_map.h
* overflows_per_second: the overflow rate in overflows per second.
*
* Outputs: None
*
* Returns:
* The actual overflow rate in overflows per second.
* Return 0 if the timer is invalid.
*
* Notes:
*
**********************************************************************/
UNS_32 timer_set_overflow_rate(TIMERREGS * timer,
INT_32 overflows_per_second)
{
INT_32 counts_per_second;
if (timer == TIMER1 || timer == TIMER2)
{
if ((timer->control & TIMER_CTRL_508K) == TIMER_CTRL_508K)
{
counts_per_second = CLKSC_TIMER_SEL1_CLK;
}
else
counts_per_second = CLKSC_TIMER_SEL0_CLK;
}
else if (timer == TIMER3)
counts_per_second = CLKSC_TIMER3_CLK;
else
return 0;
timer->load = counts_per_second / overflows_per_second;
return timer_get_overflow_rate(timer);
}
/**********************************************************************
*
* Function: timer_get_overflow_rate
*
* Purpose:
* Set the timer overflow rate in counts per second.
*
* Processing:
* If timer is TIMER1, TIMER2, or TIMER3, figure out number of
* counts per second (the timer clock as currently configured).
* Divide the counts per second by the timer load value
* to obtain the timer load value.
*
* Parameters:
* timer: Must be one of TIMER1, TIMER2 or TIMER3 as defined in
* LH7A400_map.h
* overflows_per_second: the overflow rate in overflows per second.
*
* Outputs: None
*
* Returns:
* The actual overflow rate in overflows per second.
* Return 0 if the timer is invalid.
*
* Notes:
*
**********************************************************************/
UNS_32 timer_get_overflow_rate(TIMERREGS * timer)
{
INT_32 counts_per_second;
if (timer == TIMER1 || timer == TIMER2)
{
if ((timer->control & TIMER_CTRL_508K) == TIMER_CTRL_508K)
{
counts_per_second = CLKSC_TIMER_SEL1_CLK;
}
else
counts_per_second = CLKSC_TIMER_SEL0_CLK;
}
else if (timer == TIMER3)
counts_per_second = CLKSC_TIMER3_CLK;
else
return 0;
return counts_per_second / timer->load;
}
/**********************************************************************
*
* Function: timer_periodic
*
* Purpose:
* specified timer automatically reloads the count from the load
* register after timer overflow
*
* Processing:
* If timer is TIMER1, TIMER2, or TIMER3, set the TIMER_CTRL_PERIODIC
* bit in the timer control register. Otherwise, do nothing
*
* Parameters:
* timer: Must be one of TIMER1, TIMER2 or TIMER3 as defined in
* LH7A400_map.h
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void timer_periodic(TIMERREGS * timer)
{
if (timer == TIMER1 ||
timer == TIMER2 ||
timer == TIMER3)
{
timer->control |= TIMER_CTRL_PERIODIC;
}
}
/**********************************************************************
*
* Function: timer_free_run
*
* Purpose:
* timer count overflows from 0 to 0xffff
*
* Processing:
* If timer is TIMER1, TIMER2, or TIMER3, clear the TIMER_CTRL_PERIODIC
* bit in the timer control register. Otherwise, do nothing
*
* Parameters:
* timer: Must be one of TIMER1, TIMER2 or TIMER3 as defined in
* LH7A400_map.h
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void timer_free_run(TIMERREGS * timer)
{
if (timer == TIMER1 ||
timer == TIMER2 ||
timer == TIMER3)
{
timer->control &= ~TIMER_CTRL_PERIODIC;
}
}
/**********************************************************************
*
* Function: timer_int_enable
*
* Purpose:
* enable the overflow interrupt for the specified timer
*
* Processing:
* If timer is TIMER1, TIMER2, or TIMER3, call int_enable_interrupt()
* with the interrupt controller mask the corresponds to the timer.
* Otherwise, do nothing.
*
* Parameters:
* timer: Must be one of TIMER1, TIMER2 or TIMER3 as defined in
* LH7A400_map.h
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void timer_int_enable(TIMERREGS * timer)
{
if (timer == TIMER1)
int_enable_interrupt(INTC_TC1OINTR_BIT);
else if (timer == TIMER2)
int_enable_interrupt(INTC_TC2OINTR_BIT);
else if (timer == TIMER3)
int_enable_interrupt(INTC_TC3OINTR_BIT);
}
/**********************************************************************
*
* Function: timer_int_clear
*
* Purpose:
* clear the timer overflow flag for the pecified timer
*
* Processing:
* If timer is TIMER1, TIMER2, or TIMER3, write to the timer
* clear register. Otherwise, do nothing
*
* Parameters:
* timer: Must be one of TIMER1, TIMER2 or TIMER3 as defined in
* LH7A400_map.h
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void timer_int_clear(TIMERREGS * timer)
{
if (timer == TIMER1 ||
timer == TIMER2 ||
timer == TIMER3)
{
timer->clear = 0;
}
}
/**********************************************************************
*
* Function: timer_int_disable
*
* Purpose:
* disable the overflow interrupt for the specified timer
*
* Processing:
* If timer is TIMER1, TIMER2, or TIMER3, call int_disable_interrupt()
* with the interrupt controller mask the corresponds to the timer.
* Otherwise, do nothing.
*
* Parameters:
* timer: Must be one of TIMER1, TIMER2 or TIMER3 as defined in
* LH7A400_map.h
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void timer_int_disable(TIMERREGS * timer)
{
if (timer == TIMER1)
int_disable_interrupt(INTC_TC1OINTR_BIT);
else if (timer == TIMER2)
int_disable_interrupt(INTC_TC2OINTR_BIT);
else if (timer == TIMER3)
int_disable_interrupt(INTC_TC3OINTR_BIT);
}
/**********************************************************************
*
* Function: buzzer_high
*
* Purpose:
* set the buzzer output high.
*
* Processing:
* Set the BZTOG bit and clear BZMOD in the buzzer control register.
*
* Parameters: None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void buzzer_high(void)
{
BZCONT->bzcont = BZCONT_BZTOG;
}
/**********************************************************************
*
* Function: buzzer_low
*
* Purpose:
* set the buzzer output low
*
* Processing:
* clear the buzzer control register.
*
* Parameters: None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void buzzer_low(void)
{
BZCONT->bzcont = 0;
}
/**********************************************************************
*
* Function: buzzer_timer1
*
* Purpose:
* connect the buzzer output to a flip-flop that is clocked on
* TIMER1 overflow.
*
* Processing:
* Set the BZMOD bit and clear BZTOG in the buzzer control register
*
* Parameters: None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void buzzer_timer1(void)
{
BZCONT->bzcont = BZCONT_BZMOD;
}
/**********************************************************************
*
* Function: buzzer_squarewave_start
*
* Purpose:
* make the buzzer ouput a square wave with a specified period
* in microseconds. This function consumes TIMER1 and disables
* any TIMER1 interrupts.
*
* Processing:
* Reset TIMER1. Connect TIMER1 to the buzzer output flip-flop.
* Make TIMER1 periodic. Set TIMER1's overflow rate to half the
* period (the toggle flip-flop divides the interval by 2).
* Start TIMER1.
*
* Parameters:
* time_in_us: The desired buzzer period in microseconds
*
* Outputs: None
*
* Returns:
* The actual buzzer period.
*
* Notes:
*
**********************************************************************/
UNS_32 buzzer_squarewave_start(UNS_32 period_in_us)
{
UNS_32 period;
timer_init(TIMER1);
buzzer_timer1();
timer_periodic(TIMER1);
/* the output toggles every time TIMER1 overflows */
period = timer_set_delay(TIMER1, period_in_us / 2) * 2;
timer_start(TIMER1);
return period;
}
/**********************************************************************
*
* Function: buzzer_squarewave_stop
*
* Purpose:
* This function stops TIMER1 and immmediately makes the
* buzzer output low.
*
* Processing:
* Stop TIMER1 and set the buzzer output low.
*
* Parameters: None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void buzzer_squarewave_stop(void)
{
timer_stop(TIMER1);
buzzer_low();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -