1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2008 by Maurus Cuelenaere
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 ****************************************************************************/
27 /* Interrupt handler */
30 __tcu_clear_full_match_flag(1);
32 if (pfn_timer
!= NULL
)
36 bool timer_set(long cycles
, bool start
)
38 unsigned int divider
= cycles
, prescaler_bit
= 0, prescaler
= 1, old_irq
;
43 if(start
&& pfn_unregister
!= NULL
)
46 pfn_unregister
= NULL
;
49 /* Increase prescale values starting from 0 to make the cycle count fit */
50 while(divider
> 65535 && prescaler
<= 1024)
52 prescaler
<<= 2; /* 1, 4, 16, 64, 256, 1024 */
54 divider
= cycles
/ prescaler
;
57 old_irq
= disable_irq_save();
59 __tcu_stop_counter(1);
62 __tcu_disable_pwm_output(1);
64 __tcu_mask_half_match_irq(1);
65 __tcu_unmask_full_match_irq(1);
67 /* EXTAL clock = CFG_EXTAL (12Mhz in most targets) */
68 __tcu_select_extalclk(1);
71 REG_TCU_TCSR(1) = (REG_TCU_TCSR(1) & ~TCU_TCSR_PRESCALE_MASK
) | (prescaler_bit
<< TCU_TCSR_PRESCALE_BIT
);
74 REG_TCU_TDFR(1) = divider
;
76 __tcu_clear_full_match_flag(1);
80 system_enable_irq(IRQ_TCU1
);
81 __tcu_start_counter(1);
89 bool timer_start(void)
91 __tcu_start_counter(1);
98 unsigned int old_irq
= disable_irq_save();
99 __tcu_stop_counter(1);
100 restore_irq(old_irq
);