1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2009 Bertrik Sikken
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 ****************************************************************************/
29 /* Timer driver for the S5L8700
31 The S5L8700 timer resolution is only 16-bit. Larger counts are done by using
32 both the clock-select and the clock prescaler to bring the count down into
33 the range of the 16-bit counter.
41 if (pfn_timer
!= NULL
) {
46 bool timer_set(long cycles
, bool start
)
48 static const int cs_table
[] = {1, 2, 4, 6};
52 /* stop and clear timer */
53 TCCMD
= (1 << 1); /* TD_CLR */
55 /* optionally unregister any previously registered timer user */
57 if (pfn_unregister
!= NULL
) {
59 pfn_unregister
= NULL
;
63 /* scale the count down with the clock select */
64 for (cs
= 0; cs
< 4; cs
++) {
65 count
= cycles
>> cs_table
[cs
];
66 if ((count
< 65536) || (cs
== 3)) {
71 /* scale the count down with the prescaler */
73 while (count
>= 65536) {
79 TCCON
= (1 << 12) | /* TD_INT0_EN */
80 (cs
<< 8) | /* TS_CS */
81 (0 << 4); /* TD_MODE_SEL, 0 = interval mode */
84 TCCMD
= (1 << 0); /* TD_ENABLE */
86 /* enable interrupt */
87 INTMSK
|= INTMSK_TIMERC
;
92 bool timer_start(void)
94 TCCMD
= (1 << 0); /* TD_ENABLE */
100 TCCMD
= (0 << 0); /* TD_ENABLE */