2b396fa1a56d0cc4e02361b61715ad3701441840
[bertos.git] / drv / at91 / timer.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \version $Id$
9  *
10  * \author Francesco Sacchi <batt@develer.com>
11  *
12  * \brief Low-level timer module for Atmel AT91 (inplementation).
13  */
14
15 #include <drv/timer_at91.h>
16 #include <cfg/macros.h> // BV()
17
18 #include <cfg/cpu.h>
19
20
21 /** HW dependent timer initialization  */
22 #if (CONFIG_TIMER == TIMER_ON_PIT)
23         #warning Very untested!
24         INLINE static void timer_hw_irq(void)
25         {
26                 /* Reset counters, this is needed to start timer and interrupt flags */
27                 volatile uint32_t dummy = PIT_PIVR;
28         }
29
30
31         static void timer_hw_init(void)
32         {
33                 cpuflags_t flags;
34                 IRQ_SAVE_DISABLE(flags);
35
36                 PIT_MR = CLOCK_FREQ / (16 * TIMER_TICKS_PER_SEC) - 1;
37                 /* Register system interrupt handler. */
38                 sysirq_setHandler(SYSIRQ_PIT, timer_handler);
39
40                 /* Enable interval timer and interval timer interrupts */
41                 PIT_MR |= BV(PIT_PITEN);
42                 sysirq_setEnable(SYSIRQ_PIT, true);
43
44                 /* Reset counters, this is needed to start timer and interrupt flags */
45                 volatile uint32_t dummy = PIT_PIVR;
46
47                 IRQ_RESTORE(flags);
48         }
49
50         INLINE hptime_t timer_hw_hpread(void)
51         {
52                 /* In the upper part of PIT_PIIR there is unused data */
53                 return PIT_PIIR & 0xfffff;
54         }
55
56 #else
57         #error Unimplemented value for CONFIG_TIMER
58 #endif /* CONFIG_TIMER */