]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D3x_Xplained_IAR/AtmelFiles/libchip_sama5d3x/source/pit.c
Start of SAMA5D3 XPlained demo.
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D3x_Xplained_IAR / AtmelFiles / libchip_sama5d3x / source / pit.c
1 /* ----------------------------------------------------------------------------\r
2  *         SAM Software Package License \r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2011, 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 /** \addtogroup pit_module Working with PIT\r
32  * The PIT driver provides the Interface for configuration the Periodic \r
33  *  Interval Timer (PIT) peripheral.\r
34  *\r
35  * <ul>\r
36  * <li>  Initialize the PIT with the desired period using PIT_Init().\r
37  *    Alternatively, the Periodic Interval Value (PIV) can be configured\r
38  *    manually using PIT_SetPIV(). </li> \r
39  * <li>  Start the PIT counting using PIT_Enable().\r
40  * <li>  Enable & disable the PIT interrupt using PIT_EnableIT() and\r
41  *    PIT_DisableIT(). </li> \r
42  * <li>  Retrieve the current status of the PIT using PIT_GetStatus(). </li> \r
43  * <li>  To get the current value of the internal counter and the number of ticks\r
44  *    that have occurred, use either PIT_GetPIVR() or PIT_GetPIIR() depending\r
45  *    on whether you want the values to be cleared or not. </li> \r
46  *\r
47  * </ul>\r
48  * For more accurate information, please look at the PIT section of the\r
49  * Datasheet.\r
50  *\r
51  * Related files :\n\r
52  * \ref pit.c\n\r
53  * \ref pit.h.\n\r
54 */\r
55 /*@{*/\r
56 /*@}*/\r
57 \r
58 /**\r
59  * \file\r
60  *\r
61  * Implementation of PIT (Periodic Interval Timer) controller.\r
62  *\r
63  */\r
64 /*------------------------------------------------------------------------------\r
65  *         Headers\r
66  *------------------------------------------------------------------------------*/\r
67 #include "chip.h"\r
68 \r
69 /*------------------------------------------------------------------------------\r
70  *         Exported functions\r
71  *------------------------------------------------------------------------------*/\r
72 \r
73  /**\r
74  * \brief Initialize the Periodic Interval Timer to generate a tick at the \r
75  * specified period, given the current master clock frequency.\r
76  *\r
77  *  \param uperiod  Period in uSecond.\r
78  *  \param pit_frequency  Master clock frequency in MHz.\r
79  */\r
80 \r
81 void PIT_Init(uint32_t period, uint32_t pit_frequency)\r
82 {\r
83     PIT->PIT_MR = period? (period * pit_frequency + 8) >> 4 : 0;\r
84     PIT->PIT_MR |= PIT_MR_PITEN;\r
85 }\r
86 \r
87 /**\r
88  * \brief Set the Periodic Interval Value of the PIT.\r
89  *\r
90  *  \param piv  PIV value to set.\r
91  */\r
92 void PIT_SetPIV(uint32_t piv)\r
93 {\r
94     uint32_t dwMr = PIT->PIT_MR & (~PIT_MR_PIV_Msk);\r
95     PIT->PIT_MR = dwMr | PIT_MR_PIV(piv);\r
96 }\r
97 \r
98 /**\r
99  * \brief Enables the PIT if this is not already the case.\r
100  *\r
101  */\r
102 void PIT_Enable(void)\r
103 {\r
104     PIT->PIT_MR |= PIT_MR_PITEN;\r
105 }\r
106 \r
107 /**\r
108  * \brief Disnables the PIT when PIV value is reached.\r
109  *\r
110  */\r
111 void PIT_Disable(void)\r
112 {\r
113     PIT->PIT_MR &= ~PIT_MR_PITEN;\r
114 }\r
115 \r
116 /**\r
117  * \brief Enable the PIT periodic interrupt.\r
118  *\r
119  */\r
120 void PIT_EnableIT(void) \r
121 {\r
122     PIT->PIT_MR |= PIT_MR_PITIEN;\r
123 }\r
124 \r
125 /**\r
126  * \brief Disables the PIT periodic interrupt.\r
127  *\r
128  */\r
129 void PIT_DisableIT(void)\r
130 {\r
131     PIT->PIT_MR &= ~PIT_MR_PITIEN;\r
132 }\r
133 \r
134 /**\r
135  * \brief Returns the value of the PIT mode register.\r
136  *\r
137  * \return PIT_MR value.\r
138  */\r
139 uint32_t PIT_GetMode(void)\r
140 {\r
141     return PIT->PIT_MR;\r
142 }\r
143 \r
144 /**\r
145  * \brief Returns the value of the PIT status register, clearing it as a side effect.\r
146  *\r
147  * \return PIT_SR value.\r
148  */\r
149 uint32_t PIT_GetStatus(void)\r
150 {\r
151     return PIT->PIT_SR;\r
152 }\r
153 \r
154 /**\r
155  * \brief Returns the value of the PIT Image Register, to read PICNT and CPIV without\r
156  *  clearing the current values.\r
157  * \r
158  * \return PIT_PIIR value.\r
159  */\r
160 uint32_t PIT_GetPIIR(void)\r
161 {\r
162     return PIT->PIT_PIIR;\r
163 }\r
164 \r
165 /**\r
166  * \brief Returns the value of the PIT Value Register, clearing it as a side effect.\r
167  * \r
168  * \return PITC_PIVR value.\r
169  */\r
170 uint32_t PIT_GetPIVR(void)\r
171 {\r
172     return PIT->PIT_PIVR;\r
173 }\r
174 \r