e40bb0ac2de6e19224579780e6055927af8bef97
[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 "timer.h"
16 #include "at91sam7s.h"
17 #include "sysirq.h"
18
19 #include <cfg/macros.h> // BV()
20 #include <cfg/module.h>
21 #include <cfg/cpu.h>
22
23
24 /** HW dependent timer initialization  */
25 #if (CONFIG_TIMER == TIMER_ON_PIT)
26         INLINE void timer_hw_irq(void)
27         {
28                 /* Reset counters, this is needed to reset timer and interrupt flags */
29                 uint32_t dummy = PIVR;
30                 (void) dummy;
31         }
32
33         INLINE bool timer_hw_triggered(void)
34         {
35                 return PIT_SR & BV(PITS);
36         }
37
38         INLINE void timer_hw_init(void)
39         {
40                 cpuflags_t flags;
41
42                 MOD_CHECK(sysirq);
43
44                 IRQ_SAVE_DISABLE(flags);
45
46                 PIT_MR = TIMER_HW_CNT;
47                 /* Register system interrupt handler. */
48                 sysirq_setHandler(SYSIRQ_PIT, timer_handler);
49
50                 /* Enable interval timer and interval timer interrupts */
51                 PIT_MR |= BV(PITEN);
52                 sysirq_setEnable(SYSIRQ_PIT, true);
53
54                 /* Reset counters, this is needed to start timer and interrupt flags */
55                 uint32_t dummy = PIVR;
56                 (void) dummy;
57
58                 IRQ_RESTORE(flags);
59         }
60
61         INLINE hptime_t timer_hw_hpread(void)
62         {
63                 /* In the upper part of PIT_PIIR there is unused data */
64                 return PIIR & CPIV_MASK;
65         }
66
67 #else
68         #error Unimplemented value for CONFIG_TIMER
69 #endif /* CONFIG_TIMER */