diff options
Diffstat (limited to 'firmware/target/arm/at91sam/lyre_proto1/timer-lyre_proto1.c')
-rw-r--r-- | firmware/target/arm/at91sam/lyre_proto1/timer-lyre_proto1.c | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/firmware/target/arm/at91sam/lyre_proto1/timer-lyre_proto1.c b/firmware/target/arm/at91sam/lyre_proto1/timer-lyre_proto1.c new file mode 100644 index 0000000000..5924438d10 --- /dev/null +++ b/firmware/target/arm/at91sam/lyre_proto1/timer-lyre_proto1.c | |||
@@ -0,0 +1,123 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * | ||
9 | * | ||
10 | * Copyright (C) 2009 by Jorge Pinto | ||
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 | #include "cpu.h" | ||
24 | #include "system.h" | ||
25 | #include "timer.h" | ||
26 | #include "logf.h" | ||
27 | #include "at91sam9260.h" | ||
28 | |||
29 | /*----------------------------------------------------------------------------- | ||
30 | * Function Name : pitc_handler | ||
31 | * Object : Handler for PITC interrupt | ||
32 | *---------------------------------------------------------------------------*/ | ||
33 | void pitc_handler(void) | ||
34 | { | ||
35 | unsigned long pivr = 0; | ||
36 | unsigned long pisr = 0; | ||
37 | |||
38 | /* Read the PISR */ | ||
39 | pisr = AT91C_PITC_PISR & AT91C_PITC_PITS; | ||
40 | |||
41 | if (pisr != 0) | ||
42 | { | ||
43 | /* Read the PIVR. It acknowledges the IT */ | ||
44 | pivr = AT91C_PITC_PIVR; | ||
45 | |||
46 | if (pfn_timer != NULL) | ||
47 | pfn_timer(); | ||
48 | } | ||
49 | } | ||
50 | |||
51 | bool __timer_set(long cycles, bool start) | ||
52 | { | ||
53 | if (cycles < 1000) /* Max value on PITC?? */ | ||
54 | { | ||
55 | if (start && pfn_unregister != NULL) | ||
56 | { | ||
57 | pfn_unregister(); | ||
58 | pfn_unregister = NULL; | ||
59 | } | ||
60 | |||
61 | /* Configure a resolution of <cycles> ms */ | ||
62 | AT91C_PITC_PIMR = MCK_FREQ / ( 16 * cycles) - 1; | ||
63 | |||
64 | return true; | ||
65 | } | ||
66 | |||
67 | return false; | ||
68 | } | ||
69 | |||
70 | bool __timer_register(void) | ||
71 | { | ||
72 | bool retval = true; | ||
73 | volatile unsigned long pimr = 0; | ||
74 | |||
75 | /* Configure a resolution of 1 ms */ | ||
76 | AT91C_PITC_PIMR = MCK_FREQ / (16 * 1000) - 1; | ||
77 | |||
78 | /* Enable interrupts */ | ||
79 | /* Disable the interrupt on the interrupt controller */ | ||
80 | AT91C_AIC_IDCR = (1 << AT91C_ID_SYS); | ||
81 | |||
82 | /* Save the interrupt handler routine pointer and the interrupt priority */ | ||
83 | AT91C_AIC_SVR(AT91C_ID_SYS) = (unsigned long) pitc_handler; | ||
84 | /* Store the Source Mode Register */ | ||
85 | AT91C_AIC_SMR(AT91C_ID_SYS) = AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE | \ | ||
86 | AT91C_AIC_PRIOR_LOWEST; | ||
87 | /* Clear the interrupt on the interrupt controller */ | ||
88 | AT91C_AIC_ICCR = (1 << AT91C_ID_SYS); | ||
89 | |||
90 | /* Enable the interrupt on the interrupt controller */ | ||
91 | AT91C_AIC_IECR = (1 << AT91C_ID_SYS); | ||
92 | |||
93 | /* Enable the interrupt on the pit */ | ||
94 | pimr = AT91C_PITC_PIMR; | ||
95 | AT91C_PITC_PIMR = pimr | AT91C_PITC_PITIEN; | ||
96 | |||
97 | /* Enable the pit */ | ||
98 | pimr = AT91C_PITC_PIMR; | ||
99 | AT91C_PITC_PIMR = pimr | AT91C_PITC_PITEN; | ||
100 | |||
101 | return retval; | ||
102 | } | ||
103 | |||
104 | void __timer_unregister(void) | ||
105 | { | ||
106 | volatile unsigned long pimr = 0; | ||
107 | |||
108 | /* Disable the interrupt on the interrupt controller */ | ||
109 | AT91C_AIC_IDCR = (1 << AT91C_ID_SYS); | ||
110 | |||
111 | /* Clear the interrupt on the interrupt controller */ | ||
112 | AT91C_AIC_ICCR = (1 << AT91C_ID_SYS); | ||
113 | |||
114 | /* Disable the interrupt on the pit */ | ||
115 | pimr = AT91C_PITC_PIMR; | ||
116 | pimr &= ~AT91C_PITC_PITIEN; | ||
117 | AT91C_PITC_PIMR = pimr; | ||
118 | |||
119 | /* Disable the pit */ | ||
120 | pimr = AT91C_PITC_PIMR; | ||
121 | pimr &= ~AT91C_PITC_PITEN; | ||
122 | AT91C_PITC_PIMR = pimr; | ||
123 | } | ||