]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_SAMV71_Xplained/libchip_samv7/source/rtt.c
Update version number ready for V8.2.1 release.
[freertos] / FreeRTOS / Demo / CORTEX_M7_SAMV71_Xplained / libchip_samv7 / source / rtt.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 /** \addtogroup rtt_module Working with RTT\r
31  *  \ingroup peripherals_module\r
32  * The RTT driver provides the interface to configure and use the RTT\r
33  * peripheral.\r
34  *\r
35  * The Real-time Timer is used to count elapsed seconds.\n\r
36  * This timer is clocked by the 32kHz system clock divided by a programmable\r
37  * 16-bit balue. To be accurate, it is better to use an\r
38  * external 32kHz crystal instead of the internal 32kHz RC.\n\r
39  *\r
40  * To count elapsed seconds, the user could follow these few steps:\r
41  * <ul>\r
42  * <li>Programming PTPRES in RTT_MR to feeding the timer with a 1Hz signal.</li>\r
43  * <li>Writing the bit RTTRST in RTT_MR to restart the timer with new settings.</li>\r
44  * </ul>\r
45  *\r
46  * An alarm can be set to happen on second by setting alarm value in RTT_AR.\r
47  * Alarm occurence can be detected by polling or interrupt.\r
48  *\r
49  * For more accurate information, please look at the RTT section of the\r
50  * Datasheet.\r
51  *\r
52  * Related files :\n\r
53  * \ref rtt.c\n\r
54  * \ref rtt.h.\n\r
55  */\r
56 /*@{*/\r
57 /*@}*/\r
58 \r
59 /**\r
60  * \file\r
61  *\r
62  * Implementation of Real Time Timer (RTT) controller.\r
63  *\r
64  */\r
65 \r
66 /*----------------------------------------------------------------------------\r
67  *        Headers\r
68  *----------------------------------------------------------------------------*/\r
69 #include "chip.h"\r
70 \r
71 #include <assert.h>\r
72 \r
73 /*----------------------------------------------------------------------------\r
74  *        Exported functions\r
75  *----------------------------------------------------------------------------*/\r
76 \r
77 /**\r
78  * \brief Changes the prescaler value of the given RTT and restarts it.\r
79  *\r
80  * \note This function disables RTT interrupt sources.\r
81  *\r
82  * \param rtt  Pointer to a Rtt instance.\r
83  * \param prescaler  Prescaler value for the RTT.\r
84  */\r
85 void RTT_SetPrescaler(Rtt *rtt, uint16_t prescaler)\r
86 {\r
87     rtt->RTT_MR = (prescaler |  RTT_MR_RTTRST);\r
88 }\r
89 \r
90 /**\r
91  * \brief Returns the current value of the RTT timer value.\r
92  *\r
93  * \param rtt  Pointer to a Rtt instance.\r
94  */\r
95 uint32_t RTT_GetTime(Rtt *rtt)\r
96 {\r
97     return rtt->RTT_VR;\r
98 }\r
99 \r
100 /**\r
101  * \brief Enables the specified RTT interrupt sources.\r
102  *\r
103  * \param rtt  Pointer to a Rtt instance.\r
104  * \param sources  Bitmask of interrupts to enable.\r
105  */\r
106 void RTT_EnableIT(Rtt *rtt, uint32_t sources)\r
107 {\r
108     assert( (sources & 0x0004FFFF) == 0 ) ;\r
109     rtt->RTT_MR |= sources;\r
110 }\r
111 \r
112 /**\r
113  * \brief Returns the status register value of the given RTT.\r
114  *\r
115  * \param rtt  Pointer to an Rtt instance.\r
116  */\r
117 uint32_t RTT_GetStatus(Rtt *rtt)\r
118 {\r
119     return rtt->RTT_SR;\r
120 }\r
121 \r
122 /**\r
123  * \brief Configures the RTT to generate an alarm at the given time.\r
124  *\r
125  * \param pRtt  Pointer to an Rtt instance.\r
126  * \param time  Alarm time.\r
127  */\r
128 void RTT_SetAlarm(Rtt *pRtt, uint32_t time)\r
129 {\r
130     assert(time > 0);\r
131 \r
132     pRtt->RTT_AR = time - 1;\r
133 }\r