blob: be65e9c95ffdd7e26c9d2250d85169a002d33c96 [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <err.h>
Travis Geiselbrechteb946052008-09-07 22:32:49 -070024#include <debug.h>
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070025#include <sys/types.h>
26
27#include <kernel/thread.h>
28#include <platform/timer.h>
29#include <platform/interrupts.h>
30#include <platform/debug.h>
31#include <platform/at91sam7.h>
32
33#define FIXED_1KHZ_TIMER 0
34
35static platform_timer_callback timer_func;
36
37static volatile time_t ticks = 0;
38
39#if FIXED_1KHZ_TIMER
40static volatile int timer_interval;
41static volatile int timer_downcount;
42#else
43static int timer_ms_per_tick;
44#endif
45
46time_t current_time(void)
47{
48 return ticks;
49}
50
51static enum handler_return pit_irq_handler(void *arg)
52{
53 AT91PIT *pit = AT91PIT_ADDR;
54 unsigned n = PIT_PICNT(pit->PIVR);
55
56#if FIXED_1KHZ_TIMER
57 ticks += n;
58 timer_downcount -= n;
59
60 if(timer_downcount <= 0) {
61 timer_downcount = timer_interval;
62 return timer_func(0, ticks);
63 } else {
64 return INT_NO_RESCHEDULE;
65 }
66#else
67 ticks += (n * timer_ms_per_tick);
68 return timer_func(0, ticks);
69#endif
70}
71
72status_t platform_set_periodic_timer(platform_timer_callback callback,
73 void *arg, time_t interval)
74{
75 unsigned n;
76
77 AT91PIT *pit = AT91PIT_ADDR;
78
79 n = AT91_MCK_MHZ / 16 / 1000;
Travis Geiselbrechteb946052008-09-07 22:32:49 -070080 dprintf(INFO, "timer: MCK=%dKHz, n=%d\n", AT91_MCK_MHZ / 1000, n);
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070081
82 enter_critical_section();
83
84 timer_func = callback;
85
86#if FIXED_1KHZ_TIMER
87 timer_interval = interval;
88 timer_downcount = interval;
89#else
90 timer_ms_per_tick = interval;
91 n *= interval;
92#endif
93
94 pit->MR = PIT_PITEN | PIT_PITIEN | (n & 0xfffff);
95
96 register_int_handler(PID_SYSIRQ, pit_irq_handler, 0);
Travis Geiselbrechtf1cdea32008-09-13 14:13:54 -070097 unmask_interrupt(PID_SYSIRQ);
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070098
99 exit_critical_section();
100
101 return NO_ERROR;
102}
103