1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2007 by Karl Kurbjun
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
28 /* GPB0/TOUT0 should already have been configured as output so that pin
29 should not be a functional pin and TIMER0 output unseen there */
30 void TIMER0(void) __attribute__ ((section(".icode")));
33 IO_INTC_IRQ0
= INTR_IRQ0_TMR0
; /* clear TIMER0 interrupt */
34 if (pfn_timer
!= NULL
)
38 bool timer_set(long cycles
, bool start
)
41 unsigned int divider
=cycles
, prescaler
=0;
46 oldlevel
= set_irq_level(HIGHEST_IRQ_LEVEL
);
48 IO_CLK_MOD2
|= CLK_MOD2_TMR0
; //enable TIMER0 clock!!!!!!!!!
50 IO_TIMER0_TMMD
= CONFIG_TIMER0_TMMD_STOP
;
52 if (start
&& pfn_unregister
!= NULL
)
55 pfn_unregister
= NULL
;
58 oldlevel
= disable_irq_save();
60 /* Increase prescale values starting from 0 to make the cycle count fit */
61 while(divider
>65535 && prescaler
<1024)
64 divider
=cycles
/(prescaler
+1);
67 IO_TIMER0_TMPRSCL
= prescaler
;
68 IO_TIMER0_TMDIV
= divider
;
70 restore_irq(oldlevel
);
75 static void stop_timer(void)
77 IO_INTC_EINT0
&= ~INTR_EINT0_TMR0
; //disable TIMER0 interrupt
79 IO_INTC_IRQ0
= INTR_IRQ0_TMR0
; //clear TIMER0 interrupt
81 IO_TIMER0_TMMD
= CONFIG_TIMER0_TMMD_STOP
;
83 IO_CLK_MOD2
&= ~CLK_MOD2_TMR0
; //disable TIMER0 clock
86 bool timer_start(void)
88 int oldstatus
= disable_interrupt_save(IRQ_FIQ_STATUS
);
92 IO_CLK_MOD2
|= CLK_MOD2_TMR0
; //enable TIMER0 clock!!!!!!!!!
94 /* Turn Timer0 to Free Run mode */
95 IO_TIMER0_TMMD
= CONFIG_TIMER0_TMMD_FREE_RUN
;
97 IO_INTC_EINT0
|= INTR_EINT0_TMR0
; //enable TIMER0 interrupt
99 restore_interrupt(oldstatus
);
104 void timer_stop(void)
106 int oldstatus
= disable_interrupt_save(IRQ_FIQ_STATUS
);
108 restore_interrupt(oldstatus
);