]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D2x_Xplained_IAR/AtmelFiles/drivers/peripherals/pit.c
Add SAMA5D2 Xplained IAR demo.
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D2x_Xplained_IAR / AtmelFiles / drivers / peripherals / pit.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 /** \addtogroup pit_module Working with PIT\r
31  * \section Purpose\r
32  * The PIT driver provides the Interface for configuration the Periodic\r
33  *  Interval Timer (PIT) peripheral.\r
34  *\r
35  * \section Usage\r
36  * <ul>\r
37  * <li>  Initialize the PIT with the desired period using pit_init().\r
38  *    Alternatively, the Periodic Interval Value (PIV) can be configured\r
39  *    manually using pit_set_piv(). </li>\r
40  * <li>  Start the PIT counting using pit_enable().\r
41  * <li>  Enable & disable the PIT interrupt using pit_enable_it() and\r
42  *    pit_disable_it(). </li>\r
43  * <li>  Retrieve the current status of the PIT using pit_get_status(). </li>\r
44  * <li>  To get the current value of the internal counter and the number of ticks\r
45  *    that have occurred, use either pit_get_pivr() or pit_get_piir() depending\r
46  *    on whether you want the values to be cleared or not. </li>\r
47  *\r
48  * </ul>\r
49  * For more accurate information, please look at the PIT section of the\r
50  * Datasheet.\r
51  *\r
52  * Related files :\n\r
53  * \ref pit.c\n\r
54  * \ref pit.h.\n\r
55 */\r
56 /*@{*/\r
57 /*@}*/\r
58 \r
59 /**\r
60  * \file\r
61  *\r
62  * Implementation of PIT (Periodic Interval Timer) controller.\r
63  *\r
64  */\r
65 /*------------------------------------------------------------------------------\r
66  *         Headers\r
67  *------------------------------------------------------------------------------*/\r
68 \r
69 #include "chip.h"\r
70 #include "peripherals/pit.h"\r
71 #include "peripherals/pmc.h"\r
72 \r
73 /*------------------------------------------------------------------------------\r
74  *         Exported functions\r
75  *------------------------------------------------------------------------------*/\r
76 \r
77 void pit_init(uint32_t period)\r
78 {\r
79         uint32_t pit_frequency = pmc_get_peripheral_clock(ID_PIT) / 1000000;\r
80         PIT->PIT_MR = period ? (period * pit_frequency + 8) >> 4 : 0;\r
81         PIT->PIT_MR |= PIT_MR_PITEN;\r
82 }\r
83 \r
84 void pit_set_piv(uint32_t piv)\r
85 {\r
86         uint32_t dwMr = PIT->PIT_MR & (~PIT_MR_PIV_Msk);\r
87         PIT->PIT_MR = dwMr | PIT_MR_PIV(piv);\r
88 }\r
89 \r
90 void pit_enable(void)\r
91 {\r
92         PIT->PIT_MR |= PIT_MR_PITEN;\r
93 }\r
94 \r
95 void pit_disable(void)\r
96 {\r
97         PIT->PIT_MR &= ~PIT_MR_PITEN;\r
98 }\r
99 \r
100 void pit_enable_it(void)\r
101 {\r
102         PIT->PIT_MR |= PIT_MR_PITIEN;\r
103 }\r
104 \r
105 void pit_disable_it(void)\r
106 {\r
107         PIT->PIT_MR &= ~PIT_MR_PITIEN;\r
108 }\r
109 \r
110 uint32_t pit_get_mode(void)\r
111 {\r
112         return PIT->PIT_MR;\r
113 }\r
114 \r
115 uint32_t pit_get_status(void)\r
116 {\r
117         return PIT->PIT_SR;\r
118 }\r
119 \r
120 uint32_t pit_get_piir(void)\r
121 {\r
122         return PIT->PIT_PIIR;\r
123 }\r
124 \r
125 uint32_t pit_get_pivr(void)\r
126 {\r
127         return PIT->PIT_PIVR;\r
128 }\r