]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D2x_Xplained_IAR/AtmelFiles/utils/timer.c
Add SAMA5D2 Xplained IAR demo.
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D2x_Xplained_IAR / AtmelFiles / utils / timer.c
1 /* ----------------------------------------------------------------------------\r
2  *         SAM Software Package License\r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2015, Atmel Corporation\r
5  *\r
6  * All rights reserved.\r
7  *\r
8  * Redistribution and use in source and binary forms, with or without\r
9  * modification, are permitted provided that the following conditions are met:\r
10  *\r
11  * - Redistributions of source code must retain the above copyright notice,\r
12  * this list of conditions and the disclaimer below.\r
13  *\r
14  * Atmel's name may not be used to endorse or promote products derived from\r
15  * this software without specific prior written permission.\r
16  *\r
17  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR\r
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
20  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,\r
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,\r
23  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
26  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
27  * ----------------------------------------------------------------------------\r
28  */\r
29 \r
30 /**\r
31  *  \file\r
32  *  Implement simple PIT usage as system tick.\r
33  */\r
34 \r
35 /*----------------------------------------------------------------------------\r
36  *         Headers\r
37  *----------------------------------------------------------------------------*/\r
38 \r
39 #include "board.h"\r
40 #include "timer.h"\r
41 #include "peripherals/tc.h"\r
42 #include "peripherals/pit.h"\r
43 #include "peripherals/aic.h"\r
44 #include "peripherals/pmc.h"\r
45 \r
46 /*----------------------------------------------------------------------------\r
47  *         Local variables\r
48  *----------------------------------------------------------------------------*/\r
49 \r
50 /** Tick Counter */\r
51 static volatile uint32_t _timer = 0;\r
52 static uint32_t _resolution = 0;\r
53 \r
54 /*----------------------------------------------------------------------------\r
55  *         Exported Functions\r
56  *----------------------------------------------------------------------------*/\r
57 \r
58 /**\r
59  *  \brief Handler for Sytem Tick interrupt.\r
60  */\r
61 static void timer_increment(void)\r
62 {\r
63         uint32_t status;\r
64 \r
65         /* Read the PIT status register */\r
66         status = pit_get_status() & PIT_SR_PITS;\r
67         if (status != 0) {\r
68 \r
69                 /* 1 = The Periodic Interval timer has reached PIV\r
70                  * since the last read of PIT_PIVR. Read the PIVR to\r
71                  * acknowledge interrupt and get number of ticks\r
72                  * Returns the number of occurrences of periodic\r
73                  * intervals since the last read of PIT_PIVR. */\r
74                 _timer += (pit_get_pivr() >> 20);\r
75         }\r
76 }\r
77 \r
78 uint32_t timer_configure(uint32_t resolution)\r
79 {\r
80         pit_disable_it();\r
81         if (!resolution)\r
82                 resolution = BOARD_TIMER_RESOLUTION;\r
83         _timer = 0;\r
84         pmc_enable_peripheral(ID_PIT);\r
85         pit_init(resolution);\r
86         aic_set_source_vector(ID_PIT, timer_increment);\r
87         aic_enable(ID_PIT);\r
88         pit_enable_it();\r
89         pit_enable();\r
90         _resolution = resolution;\r
91         return 0;\r
92 }\r
93 \r
94 uint32_t timer_get_resolution(void)\r
95 {\r
96         return _resolution;\r
97 }\r
98 \r
99 uint32_t timer_get_interval(uint32_t start, uint32_t end)\r
100 {\r
101         if (end >= start)\r
102                 return (end - start);\r
103         return (end + (0xFFFFFFFF - start) + 1);\r
104 }\r
105 \r
106 void timer_start_timeout(struct _timeout* timeout, uint32_t count)\r
107 {\r
108         timeout->start = _timer;\r
109         timeout->count = count;\r
110 }\r
111 \r
112 uint8_t timer_timeout_reached(struct _timeout* timeout)\r
113 {\r
114         return timer_get_interval(timeout->start, _timer) >= timeout->count;\r
115 }\r
116 \r
117 void timer_wait(uint32_t count)\r
118 {\r
119         uint32_t start, current;\r
120         start = _timer;\r
121         do {\r
122                 current = _timer;\r
123         } while (timer_get_interval(start, current) < count);\r
124 }\r
125 \r
126 void timer_sleep(uint32_t count)\r
127 {\r
128         uint32_t start, current;\r
129         asm("CPSIE   I");\r
130         start = _timer;\r
131 \r
132         do {\r
133                 asm("WFI");\r
134                 current = _timer;\r
135         } while (timer_get_interval(start, current) < count);\r
136 }\r
137 \r
138 uint32_t timer_get_tick(void)\r
139 {\r
140         return _timer;\r
141 }\r