summaryrefslogtreecommitdiff
path: root/firmware/target/arm/s5l8702/timer-s5l8702.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/arm/s5l8702/timer-s5l8702.c')
-rw-r--r--firmware/target/arm/s5l8702/timer-s5l8702.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/firmware/target/arm/s5l8702/timer-s5l8702.c b/firmware/target/arm/s5l8702/timer-s5l8702.c
new file mode 100644
index 0000000000..fb56a9ffcf
--- /dev/null
+++ b/firmware/target/arm/s5l8702/timer-s5l8702.c
@@ -0,0 +1,94 @@
1/***************************************************************************
2* __________ __ ___.
3* Open \______ \ ____ ____ | | _\_ |__ _______ ___
4* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7* \/ \/ \/ \/ \/
8* $Id: timer-s5l8700.c 23103 2009-10-11 11:35:14Z theseven $
9*
10* Copyright (C) 2009 Bertrik Sikken
11*
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.
16*
17* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18* KIND, either express or implied.
19*
20****************************************************************************/
21
22#include "config.h"
23
24#include "inttypes.h"
25#include "s5l8702.h"
26#include "system.h"
27#include "timer.h"
28
29//TODO: This needs calibration once we figure out the clocking
30
31void INT_TIMERC(void)
32{
33 /* clear interrupt */
34 TCCON = TCCON;
35
36 if (pfn_timer != NULL) {
37 pfn_timer();
38 }
39}
40
41bool timer_set(long cycles, bool start)
42{
43 static const int cs_table[] = {1, 2, 4, 6};
44 int prescale, cs;
45 long count;
46
47 /* stop and clear timer */
48 TCCMD = (1 << 1); /* TD_CLR */
49
50 /* optionally unregister any previously registered timer user */
51 if (start) {
52 if (pfn_unregister != NULL) {
53 pfn_unregister();
54 pfn_unregister = NULL;
55 }
56 }
57
58 /* scale the count down with the clock select */
59 for (cs = 0; cs < 4; cs++) {
60 count = cycles >> cs_table[cs];
61 if ((count < 65536) || (cs == 3)) {
62 break;
63 }
64 }
65
66 /* scale the count down with the prescaler */
67 prescale = 1;
68 while (count >= 65536) {
69 count >>= 1;
70 prescale <<= 1;
71 }
72
73 /* configure timer */
74 TCCON = (1 << 12) | /* TD_INT0_EN */
75 (cs << 8) | /* TS_CS */
76 (0 << 4); /* TD_MODE_SEL, 0 = interval mode */
77 TCPRE = prescale - 1;
78 TCDATA0 = count;
79 TCCMD = (1 << 0); /* TD_ENABLE */
80
81 return true;
82}
83
84bool timer_start(void)
85{
86 TCCMD = (1 << 0); /* TD_ENABLE */
87 return true;
88}
89
90void timer_stop(void)
91{
92 TCCMD = (0 << 0); /* TD_ENABLE */
93}
94