2007-09-20 04:46:41 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2007-09-22 15:43:38 +00:00
|
|
|
* $Id$
|
2007-09-20 04:46:41 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2007 by Karl Kurbjun
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include "logf.h"
|
|
|
|
|
|
|
|
/* GPB0/TOUT0 should already have been configured as output so that pin
|
|
|
|
should not be a functional pin and TIMER0 output unseen there */
|
|
|
|
void TIMER0(void)
|
|
|
|
{
|
|
|
|
if (pfn_timer != NULL)
|
|
|
|
pfn_timer();
|
2007-09-22 23:17:52 +00:00
|
|
|
IO_INTC_IRQ0 |= 1<<IRQ_TIMER0;
|
2007-09-20 04:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool __timer_set(long cycles, bool start)
|
|
|
|
{
|
2007-09-22 23:17:52 +00:00
|
|
|
int oldlevel;
|
2007-11-07 05:30:31 +00:00
|
|
|
unsigned int divider=cycles, prescaler=0;
|
2007-09-20 04:46:41 +00:00
|
|
|
|
2007-11-07 05:30:31 +00:00
|
|
|
if(cycles<1)
|
|
|
|
return false;
|
2007-09-20 04:46:41 +00:00
|
|
|
|
2007-11-07 05:30:31 +00:00
|
|
|
IO_TIMER0_TMMD = CONFIG_TIMER0_TMMD_STOP;
|
2007-09-20 04:46:41 +00:00
|
|
|
|
2007-09-22 23:17:52 +00:00
|
|
|
if (start && pfn_unregister != NULL)
|
2007-09-20 04:46:41 +00:00
|
|
|
{
|
2007-09-22 23:17:52 +00:00
|
|
|
pfn_unregister();
|
|
|
|
pfn_unregister = NULL;
|
|
|
|
}
|
2007-09-20 04:46:41 +00:00
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
oldlevel = disable_irq_save();
|
2007-09-20 04:46:41 +00:00
|
|
|
|
2007-11-07 05:30:31 +00:00
|
|
|
/* Increase prescale values starting from 0 to make the cycle count fit */
|
|
|
|
while(divider>65535 && prescaler<1024)
|
|
|
|
{
|
|
|
|
prescaler++;
|
|
|
|
divider=cycles/(prescaler+1);
|
|
|
|
}
|
2007-09-20 04:46:41 +00:00
|
|
|
|
2007-09-22 23:17:52 +00:00
|
|
|
IO_TIMER0_TMPRSCL = prescaler;
|
|
|
|
IO_TIMER0_TMDIV = divider;
|
2007-09-20 04:46:41 +00:00
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
restore_irq(oldlevel);
|
2007-09-20 04:46:41 +00:00
|
|
|
|
2007-09-22 23:17:52 +00:00
|
|
|
return true;
|
2007-09-20 04:46:41 +00:00
|
|
|
}
|
|
|
|
|
2007-11-07 05:30:31 +00:00
|
|
|
static void stop_timer(void)
|
2007-09-20 04:46:41 +00:00
|
|
|
{
|
2007-11-07 05:30:31 +00:00
|
|
|
IO_INTC_EINT0 &= ~(1<<IRQ_TIMER0);
|
|
|
|
|
|
|
|
IO_INTC_IRQ0 |= 1<<IRQ_TIMER0;
|
|
|
|
|
|
|
|
IO_TIMER0_TMMD = CONFIG_TIMER0_TMMD_STOP;
|
|
|
|
}
|
2007-09-20 04:46:41 +00:00
|
|
|
|
2007-11-07 05:30:31 +00:00
|
|
|
bool __timer_register(void)
|
|
|
|
{
|
2008-03-31 06:00:23 +00:00
|
|
|
int oldstatus = disable_interrupt_save(IRQ_FIQ_STATUS);
|
2007-09-20 04:46:41 +00:00
|
|
|
|
|
|
|
stop_timer();
|
|
|
|
|
|
|
|
/* Turn Timer0 to Free Run mode */
|
2007-11-07 05:30:31 +00:00
|
|
|
IO_TIMER0_TMMD = CONFIG_TIMER0_TMMD_FREE_RUN;
|
2007-09-22 23:17:52 +00:00
|
|
|
|
2007-11-07 05:30:31 +00:00
|
|
|
IO_INTC_EINT0 |= 1<<IRQ_TIMER0;
|
2007-09-20 04:46:41 +00:00
|
|
|
|
2008-03-31 06:00:23 +00:00
|
|
|
restore_interrupt(oldstatus);
|
2007-09-20 04:46:41 +00:00
|
|
|
|
2007-11-07 05:30:31 +00:00
|
|
|
return true;
|
2007-09-20 04:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void __timer_unregister(void)
|
|
|
|
{
|
2008-03-31 06:00:23 +00:00
|
|
|
int oldstatus = disable_interrupt_save(IRQ_FIQ_STATUS);
|
2007-09-20 04:46:41 +00:00
|
|
|
stop_timer();
|
2008-03-31 06:00:23 +00:00
|
|
|
restore_interrupt(oldstatus);
|
2007-09-20 04:46:41 +00:00
|
|
|
}
|