The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Thu Nov 08 11:45:42 2018 +0000
Revision:
171:3a7713b1edbc
Parent:
TARGET_SAMR21G18A/TARGET_Atmel/TARGET_SAM_CortexM0P/drivers/rtc/rtc_count.h@111:4336505e4b1c
mbed library. Release version 164

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 111:4336505e4b1c 1 /**
Kojto 111:4336505e4b1c 2 * \file
Kojto 111:4336505e4b1c 3 *
Kojto 111:4336505e4b1c 4 * \brief SAM RTC Driver (Count Mode)
Kojto 111:4336505e4b1c 5 *
Kojto 111:4336505e4b1c 6 * Copyright (C) 2012-2015 Atmel Corporation. All rights reserved.
Kojto 111:4336505e4b1c 7 *
Kojto 111:4336505e4b1c 8 * \asf_license_start
Kojto 111:4336505e4b1c 9 *
Kojto 111:4336505e4b1c 10 * \page License
Kojto 111:4336505e4b1c 11 *
Kojto 111:4336505e4b1c 12 * Redistribution and use in source and binary forms, with or without
Kojto 111:4336505e4b1c 13 * modification, are permitted provided that the following conditions are met:
Kojto 111:4336505e4b1c 14 *
Kojto 111:4336505e4b1c 15 * 1. Redistributions of source code must retain the above copyright notice,
Kojto 111:4336505e4b1c 16 * this list of conditions and the following disclaimer.
Kojto 111:4336505e4b1c 17 *
Kojto 111:4336505e4b1c 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
Kojto 111:4336505e4b1c 19 * this list of conditions and the following disclaimer in the documentation
Kojto 111:4336505e4b1c 20 * and/or other materials provided with the distribution.
Kojto 111:4336505e4b1c 21 *
Kojto 111:4336505e4b1c 22 * 3. The name of Atmel may not be used to endorse or promote products derived
Kojto 111:4336505e4b1c 23 * from this software without specific prior written permission.
Kojto 111:4336505e4b1c 24 *
Kojto 111:4336505e4b1c 25 * 4. This software may only be redistributed and used in connection with an
Kojto 111:4336505e4b1c 26 * Atmel microcontroller product.
Kojto 111:4336505e4b1c 27 *
Kojto 111:4336505e4b1c 28 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
Kojto 111:4336505e4b1c 29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
Kojto 111:4336505e4b1c 30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
Kojto 111:4336505e4b1c 31 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
Kojto 111:4336505e4b1c 32 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Kojto 111:4336505e4b1c 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Kojto 111:4336505e4b1c 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Kojto 111:4336505e4b1c 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
Kojto 111:4336505e4b1c 36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
Kojto 111:4336505e4b1c 37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Kojto 111:4336505e4b1c 38 * POSSIBILITY OF SUCH DAMAGE.
Kojto 111:4336505e4b1c 39 *
Kojto 111:4336505e4b1c 40 * \asf_license_stop
Kojto 111:4336505e4b1c 41 *
Kojto 111:4336505e4b1c 42 */
Kojto 111:4336505e4b1c 43 /*
Kojto 111:4336505e4b1c 44 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
Kojto 111:4336505e4b1c 45 */
Kojto 111:4336505e4b1c 46
Kojto 111:4336505e4b1c 47 #ifndef RTC_COUNT_H_INCLUDED
Kojto 111:4336505e4b1c 48 #define RTC_COUNT_H_INCLUDED
Kojto 111:4336505e4b1c 49
Kojto 111:4336505e4b1c 50 /**
Kojto 111:4336505e4b1c 51 * \defgroup asfdoc_sam0_rtc_count_group SAM RTC Count Driver (RTC COUNT)
Kojto 111:4336505e4b1c 52 *
Kojto 111:4336505e4b1c 53 * This driver for Atmel&reg; | SMART SAM devices provides an interface for the configuration
Kojto 111:4336505e4b1c 54 * and management of the device's Real Time Clock functionality in Count
Kojto 111:4336505e4b1c 55 * operating mode, for the configuration and retrieval of the current RTC
Kojto 111:4336505e4b1c 56 * counter value. The following driver API modes are covered by this
Kojto 111:4336505e4b1c 57 * manual:
Kojto 111:4336505e4b1c 58 *
Kojto 111:4336505e4b1c 59 * - Polled APIs
Kojto 111:4336505e4b1c 60 * \if RTC_COUNT_CALLBACK_MODE
Kojto 111:4336505e4b1c 61 * - Callback APIs
Kojto 111:4336505e4b1c 62 * \endif
Kojto 111:4336505e4b1c 63 *
Kojto 111:4336505e4b1c 64 * The following peripherals are used by this module:
Kojto 111:4336505e4b1c 65 * - RTC (Real Time Clock)
Kojto 111:4336505e4b1c 66 *
Kojto 111:4336505e4b1c 67 * The following devices can use this module:
Kojto 111:4336505e4b1c 68 * - Atmel | SMART SAM D20/D21
Kojto 111:4336505e4b1c 69 * - Atmel | SMART SAM R21
Kojto 111:4336505e4b1c 70 * - Atmel | SMART SAM D10/D11
Kojto 111:4336505e4b1c 71 * - Atmel | SMART SAM L21
Kojto 111:4336505e4b1c 72 * - Atmel | SMART SAM DAx
Kojto 111:4336505e4b1c 73 * - Atmel | SMART SAM C20/C21
Kojto 111:4336505e4b1c 74 *
Kojto 111:4336505e4b1c 75 * The outline of this documentation is as follows:
Kojto 111:4336505e4b1c 76 * - \ref asfdoc_sam0_rtc_count_prerequisites
Kojto 111:4336505e4b1c 77 * - \ref asfdoc_sam0_rtc_count_module_overview
Kojto 111:4336505e4b1c 78 * - \ref asfdoc_sam0_rtc_count_special_considerations
Kojto 111:4336505e4b1c 79 * - \ref asfdoc_sam0_rtc_count_extra_info
Kojto 111:4336505e4b1c 80 * - \ref asfdoc_sam0_rtc_count_examples
Kojto 111:4336505e4b1c 81 * - \ref asfdoc_sam0_rtc_count_api_overview
Kojto 111:4336505e4b1c 82 *
Kojto 111:4336505e4b1c 83 *
Kojto 111:4336505e4b1c 84 * \section asfdoc_sam0_rtc_count_prerequisites Prerequisites
Kojto 111:4336505e4b1c 85 *
Kojto 111:4336505e4b1c 86 * There are no prerequisites for this module.
Kojto 111:4336505e4b1c 87 *
Kojto 111:4336505e4b1c 88 *
Kojto 111:4336505e4b1c 89 * \section asfdoc_sam0_rtc_count_module_overview Module Overview
Kojto 111:4336505e4b1c 90 *
Kojto 111:4336505e4b1c 91 * The RTC module in the SAM devices is a 32-bit counter, with a 10-bit
Kojto 111:4336505e4b1c 92 * programmable prescaler. Typically, the RTC clock is run continuously,
Kojto 111:4336505e4b1c 93 * including in the device's low-power sleep modes, to track the current time
Kojto 111:4336505e4b1c 94 * and date information. The RTC can be used as a source to wake up the system
Kojto 111:4336505e4b1c 95 * at a scheduled time or periodically using the alarm functions.
Kojto 111:4336505e4b1c 96 *
Kojto 111:4336505e4b1c 97 * In this driver, the RTC is operated in Count mode. This allows for an
Kojto 111:4336505e4b1c 98 * easy integration of an asynchronous counter into a user application, which is
Kojto 111:4336505e4b1c 99 * capable of operating while the device is in sleep mode.
Kojto 111:4336505e4b1c 100 *
Kojto 111:4336505e4b1c 101 * Whilst operating in Count mode, the RTC features:
Kojto 111:4336505e4b1c 102 * - 16-bit counter mode
Kojto 111:4336505e4b1c 103 * - Selectable counter period
Kojto 111:4336505e4b1c 104 * - Up to six configurable compare values
Kojto 111:4336505e4b1c 105 * - 32-bit counter mode
Kojto 111:4336505e4b1c 106 * - Clear counter value on match
Kojto 111:4336505e4b1c 107 * - Up to four configurable compare values
Kojto 111:4336505e4b1c 108 *
Kojto 111:4336505e4b1c 109 * \subsection asfdoc_sam0_rtc_count_features Driver Feature Macro Definition
Kojto 111:4336505e4b1c 110 * <table>
Kojto 111:4336505e4b1c 111 * <tr>
Kojto 111:4336505e4b1c 112 * <th>Driver Feature Macro</th>
Kojto 111:4336505e4b1c 113 * <th>Supported devices</th>
Kojto 111:4336505e4b1c 114 * </tr>
Kojto 111:4336505e4b1c 115 * <tr>
Kojto 111:4336505e4b1c 116 * <td>FEATURE_RTC_PERIODIC_INT</td>
Kojto 111:4336505e4b1c 117 * <td>SAML21/C20/C21</td>
Kojto 111:4336505e4b1c 118 * </tr>
Kojto 111:4336505e4b1c 119 * <tr>
Kojto 111:4336505e4b1c 120 * <td>FEATURE_RTC_PRESCALER_OFF</td>
Kojto 111:4336505e4b1c 121 * <td>SAML21/C20/C21</td>
Kojto 111:4336505e4b1c 122 * </tr>
Kojto 111:4336505e4b1c 123 * <tr>
Kojto 111:4336505e4b1c 124 * <td>FEATURE_RTC_CLOCK_SELECTION</td>
Kojto 111:4336505e4b1c 125 * <td>SAML21/C20/C21</td>
Kojto 111:4336505e4b1c 126 * </tr>
Kojto 111:4336505e4b1c 127 * <tr>
Kojto 111:4336505e4b1c 128 * <td>FEATURE_RTC_GENERAL_PURPOSE_REG</td>
Kojto 111:4336505e4b1c 129 * <td>SAML21</td>
Kojto 111:4336505e4b1c 130 * </tr>
Kojto 111:4336505e4b1c 131 * <tr>
Kojto 111:4336505e4b1c 132 * <td>FEATURE_RTC_CONTINUOUSLY_UPDATED</td>
Kojto 111:4336505e4b1c 133 * <td>SAMD20, SAMD21, SAMR21, SAMD10, SAMD11, SAMDAx</td>
Kojto 111:4336505e4b1c 134 * </tr>
Kojto 111:4336505e4b1c 135 * </table>
Kojto 111:4336505e4b1c 136 * \note The specific features are only available in the driver when the
Kojto 111:4336505e4b1c 137 * selected device supports those features.
Kojto 111:4336505e4b1c 138 *
Kojto 111:4336505e4b1c 139 * \section asfdoc_sam0_rtc_count_module_overview_compares Compare and Overflow
Kojto 111:4336505e4b1c 140 * The RTC can be used with up to 4/6 compare values (depending on selected
Kojto 111:4336505e4b1c 141 * operation mode). These compare values will trigger on match with the current
Kojto 111:4336505e4b1c 142 * RTC counter value, and can be set up to trigger an interrupt, event, or both.
Kojto 111:4336505e4b1c 143 * The RTC can also be configured to clear the counter value on compare match
Kojto 111:4336505e4b1c 144 * in 32-bit mode, resetting the count value back to zero.
Kojto 111:4336505e4b1c 145 *
Kojto 111:4336505e4b1c 146 * If the RTC is operated without the Clear on Match option enabled, or in
Kojto 111:4336505e4b1c 147 * 16-bit mode, the RTC counter value will instead be cleared on overflow once
Kojto 111:4336505e4b1c 148 * the maximum count value has been reached:
Kojto 111:4336505e4b1c 149 *
Kojto 111:4336505e4b1c 150 * \f[ COUNT_{MAX} = 2^{32}-1 \f] for 32-bit counter mode, and
Kojto 111:4336505e4b1c 151 * \f[ COUNT_{MAX} = 2^{16}-1 \f] for 16-bit counter mode.
Kojto 111:4336505e4b1c 152 *
Kojto 111:4336505e4b1c 153 * When running in 16-bit mode, the overflow value is selectable with a period
Kojto 111:4336505e4b1c 154 * value. The counter overflow will then occur when the counter value reaches
Kojto 111:4336505e4b1c 155 * the specified period value.
Kojto 111:4336505e4b1c 156 *
Kojto 111:4336505e4b1c 157 * \subsection asfdoc_sam0_rtc_count_module_overview_periodic Periodic Events
Kojto 111:4336505e4b1c 158 * The RTC can generate events at periodic intervals, allowing for direct
Kojto 111:4336505e4b1c 159 * peripheral actions without CPU intervention. The periodic events can be
Kojto 111:4336505e4b1c 160 * generated on the upper eight bits of the RTC prescaler, and will be generated on
Kojto 111:4336505e4b1c 161 * the rising edge transition of the specified bit. The resulting periodic
Kojto 111:4336505e4b1c 162 * frequency can be calculated by the following formula:
Kojto 111:4336505e4b1c 163 *
Kojto 111:4336505e4b1c 164 * \f[ f_{PERIODIC}=\frac{f_{ASY}}{2^{n+3}} \f]
Kojto 111:4336505e4b1c 165 *
Kojto 111:4336505e4b1c 166 * Where \f$f_{ASY}\f$ refers to the \e asynchronous clock set up in the RTC
Kojto 111:4336505e4b1c 167 * module configuration. The \b n parameter is the event source generator index
Kojto 111:4336505e4b1c 168 * of the RTC module. If the asynchronous clock is operated at the recommended
Kojto 111:4336505e4b1c 169 * frequency of 1KHz, the formula results in the values shown in
Kojto 111:4336505e4b1c 170 * \ref asfdoc_sam0_rtc_count_module_rtc_hz "the table below".
Kojto 111:4336505e4b1c 171 *
Kojto 111:4336505e4b1c 172 * \anchor asfdoc_sam0_rtc_count_module_rtc_hz
Kojto 111:4336505e4b1c 173 * <table>
Kojto 111:4336505e4b1c 174 * <caption>RTC Event Frequencies for Each Prescaler Bit Using a 1KHz Clock</caption>
Kojto 111:4336505e4b1c 175 * <tr>
Kojto 111:4336505e4b1c 176 * <th>n</th> <th>Periodic event</th>
Kojto 111:4336505e4b1c 177 * </tr>
Kojto 111:4336505e4b1c 178 * <tr>
Kojto 111:4336505e4b1c 179 * <td>7</td> <td>1Hz</td>
Kojto 111:4336505e4b1c 180 * </tr>
Kojto 111:4336505e4b1c 181 * <tr>
Kojto 111:4336505e4b1c 182 * <td>6</td> <td>2Hz</td>
Kojto 111:4336505e4b1c 183 * </tr>
Kojto 111:4336505e4b1c 184 * <tr>
Kojto 111:4336505e4b1c 185 * <td>5</td> <td>4Hz</td>
Kojto 111:4336505e4b1c 186 * </tr>
Kojto 111:4336505e4b1c 187 * <tr>
Kojto 111:4336505e4b1c 188 * <td>4</td> <td>8Hz</td>
Kojto 111:4336505e4b1c 189 * </tr>
Kojto 111:4336505e4b1c 190 * <tr>
Kojto 111:4336505e4b1c 191 * <td>3</td> <td>16Hz</td>
Kojto 111:4336505e4b1c 192 * </tr>
Kojto 111:4336505e4b1c 193 * <tr>
Kojto 111:4336505e4b1c 194 * <td>2</td> <td>32Hz</td>
Kojto 111:4336505e4b1c 195 * </tr>
Kojto 111:4336505e4b1c 196 * <tr>
Kojto 111:4336505e4b1c 197 * <td>1</td> <td>64Hz</td>
Kojto 111:4336505e4b1c 198 * </tr>
Kojto 111:4336505e4b1c 199 * <tr>
Kojto 111:4336505e4b1c 200 * <td>0</td> <td>128Hz</td>
Kojto 111:4336505e4b1c 201 * </tr>
Kojto 111:4336505e4b1c 202 * </table>
Kojto 111:4336505e4b1c 203 *
Kojto 111:4336505e4b1c 204 * \note The connection of events between modules requires the use of the
Kojto 111:4336505e4b1c 205 * \ref asfdoc_sam0_events_group "SAM Event System Driver (EVENTS)"
Kojto 111:4336505e4b1c 206 * to route output event of one module to the the input event of another.
Kojto 111:4336505e4b1c 207 * For more information on event routing, refer to the event driver
Kojto 111:4336505e4b1c 208 * documentation.
Kojto 111:4336505e4b1c 209 *
Kojto 111:4336505e4b1c 210 * \subsection asfdoc_sam0_rtc_count_module_overview_correction Digital Frequency Correction
Kojto 111:4336505e4b1c 211 * The RTC module contains Digital Frequency Correction logic to compensate for
Kojto 111:4336505e4b1c 212 * inaccurate source clock frequencies which would otherwise result in skewed
Kojto 111:4336505e4b1c 213 * time measurements. The correction scheme requires that at least two bits
Kojto 111:4336505e4b1c 214 * in the RTC module prescaler are reserved by the correction logic. As a
Kojto 111:4336505e4b1c 215 * result of this implementation, frequency correction is only available when
Kojto 111:4336505e4b1c 216 * the RTC is running from a 1Hz reference clock.
Kojto 111:4336505e4b1c 217 *
Kojto 111:4336505e4b1c 218 * The correction procedure is implemented by subtracting or adding a single
Kojto 111:4336505e4b1c 219 * cycle from the RTC prescaler every 1024 RTC GCLK cycles. The adjustment is
Kojto 111:4336505e4b1c 220 * applied the specified number of time (maximum 127) over 976 of these periods. The
Kojto 111:4336505e4b1c 221 * corresponding correction in PPM will be given by:
Kojto 111:4336505e4b1c 222 *
Kojto 111:4336505e4b1c 223 * \f[ Correction(PPM) = \frac{VALUE}{999424}10^6 \f]
Kojto 111:4336505e4b1c 224 *
Kojto 111:4336505e4b1c 225 * The RTC clock will tick faster if provided with a positive correction value,
Kojto 111:4336505e4b1c 226 * and slower when given a negative correction value.
Kojto 111:4336505e4b1c 227 *
Kojto 111:4336505e4b1c 228 *
Kojto 111:4336505e4b1c 229 * \section asfdoc_sam0_rtc_count_special_considerations Special Considerations
Kojto 111:4336505e4b1c 230 *
Kojto 111:4336505e4b1c 231 * \subsection asfdoc_sam0_rtc_count_special_considerations_clock Clock Setup
Kojto 111:4336505e4b1c 232 * \subsubsection asfdoc_sam0_rtc_count_clock_samd_r SAM D20/D21/R21/D10/D11/DA0/DA1 Clock Setup
Kojto 111:4336505e4b1c 233 * The RTC is typically clocked by a specialized GCLK generator that has a
Kojto 111:4336505e4b1c 234 * smaller prescaler than the others. By default the RTC clock is on, selected
Kojto 111:4336505e4b1c 235 * to use the internal 32KHz RC-oscillator with a prescaler of 32, giving a
Kojto 111:4336505e4b1c 236 * resulting clock frequency of 1KHz to the RTC. When the internal RTC
Kojto 111:4336505e4b1c 237 * prescaler is set to 1024, this yields an end-frequency of 1Hz.
Kojto 111:4336505e4b1c 238 *
Kojto 111:4336505e4b1c 239 * The implementer also has the option to set other end-frequencies.
Kojto 111:4336505e4b1c 240 * \ref asfdoc_sam0_rtc_count_rtc_out_freq "The table below" lists the
Kojto 111:4336505e4b1c 241 * available RTC frequencies for each possible GCLK and RTC input prescaler
Kojto 111:4336505e4b1c 242 * options.
Kojto 111:4336505e4b1c 243 *
Kojto 111:4336505e4b1c 244 * \anchor asfdoc_sam0_rtc_count_rtc_out_freq
Kojto 111:4336505e4b1c 245 * <table>
Kojto 111:4336505e4b1c 246 * <caption>RTC Output Frequencies from Allowable Input Clocks</caption>
Kojto 111:4336505e4b1c 247 * <tr>
Kojto 111:4336505e4b1c 248 * <th>End-frequency</th>
Kojto 111:4336505e4b1c 249 * <th>GCLK prescaler</th>
Kojto 111:4336505e4b1c 250 * <th>RTC prescaler</th>
Kojto 111:4336505e4b1c 251 * </tr>
Kojto 111:4336505e4b1c 252 * <tr>
Kojto 111:4336505e4b1c 253 * <td>32KHz</td>
Kojto 111:4336505e4b1c 254 * <td>1</td>
Kojto 111:4336505e4b1c 255 * <td>1</td>
Kojto 111:4336505e4b1c 256 * </tr>
Kojto 111:4336505e4b1c 257 * <tr>
Kojto 111:4336505e4b1c 258 * <td>1KHz</td>
Kojto 111:4336505e4b1c 259 * <td>32</td>
Kojto 111:4336505e4b1c 260 * <td>1</td>
Kojto 111:4336505e4b1c 261 * </tr>
Kojto 111:4336505e4b1c 262 * <tr>
Kojto 111:4336505e4b1c 263 * <td>1Hz</td>
Kojto 111:4336505e4b1c 264 * <td>32</td>
Kojto 111:4336505e4b1c 265 * <td>1024</td>
Kojto 111:4336505e4b1c 266 * </tr>
Kojto 111:4336505e4b1c 267 * </table>
Kojto 111:4336505e4b1c 268 *
Kojto 111:4336505e4b1c 269 * The overall RTC module clocking scheme is shown in
Kojto 111:4336505e4b1c 270 * \ref asfdoc_sam0_rtc_count_rtc_clock_fig "the figure below".
Kojto 111:4336505e4b1c 271 *
Kojto 111:4336505e4b1c 272 * \anchor asfdoc_sam0_rtc_count_rtc_clock_fig
Kojto 111:4336505e4b1c 273 * \dot
Kojto 111:4336505e4b1c 274 * digraph clocking_scheme {
Kojto 111:4336505e4b1c 275 * rankdir=LR;
Kojto 111:4336505e4b1c 276 * GCLK [shape="record", label="<f0> GCLK | <f1> RTC_GCLK",
Kojto 111:4336505e4b1c 277 * bgcolor="lightgray", style="filled"];
Kojto 111:4336505e4b1c 278 * RTCPRE [shape="record" label="<f0> RTC | <f1> RTC PRESCALER"];
Kojto 111:4336505e4b1c 279 * RTC [shape="record", label="<f0> RTC | <f1> RTC CLOCK"];
Kojto 111:4336505e4b1c 280 *
Kojto 111:4336505e4b1c 281 * GCLK:f1 -> RTCPRE:f1;
Kojto 111:4336505e4b1c 282 * RTCPRE:f1 -> RTC:f1;
Kojto 111:4336505e4b1c 283 * }
Kojto 111:4336505e4b1c 284 * \enddot
Kojto 111:4336505e4b1c 285 *
Kojto 111:4336505e4b1c 286 * \subsubsection asfdoc_sam0_rtc_count_clock_saml SAM L21/C20/C21 Clock Setup
Kojto 111:4336505e4b1c 287 * The RTC clock can be selected from OSC32K,XOSC32K or OSCULP32K , and a 32KHz
Kojto 111:4336505e4b1c 288 * or 1KHz oscillator clock frequency is required. This clock must be
Kojto 111:4336505e4b1c 289 * configured and enabled in the 32KHz oscillator controller before using the RTC.
Kojto 111:4336505e4b1c 290 *
Kojto 111:4336505e4b1c 291 * The table below lists the available RTC clock \ref asfdoc_sam0_rtc_count_rtc_clk
Kojto 111:4336505e4b1c 292 *
Kojto 111:4336505e4b1c 293 * \anchor asfdoc_sam0_rtc_count_rtc_clk
Kojto 111:4336505e4b1c 294 * <table>
Kojto 111:4336505e4b1c 295 * <caption>RTC clocks source</caption>
Kojto 111:4336505e4b1c 296 * <tr>
Kojto 111:4336505e4b1c 297 * <th>RTC clock frequency</th>
Kojto 111:4336505e4b1c 298 * <th>Clock source</th>
Kojto 111:4336505e4b1c 299 * <th>Description</th>
Kojto 111:4336505e4b1c 300 * </tr>
Kojto 111:4336505e4b1c 301 * <tr>
Kojto 111:4336505e4b1c 302 * <td>1.024KHz</td>
Kojto 111:4336505e4b1c 303 * <td>ULP1K</td>
Kojto 111:4336505e4b1c 304 * <td>1.024KHz from 32KHz internal ULP oscillator</td>
Kojto 111:4336505e4b1c 305 * </tr>
Kojto 111:4336505e4b1c 306 * <tr>
Kojto 111:4336505e4b1c 307 * <td>32.768KHz</td>
Kojto 111:4336505e4b1c 308 * <td>ULP32K</td>
Kojto 111:4336505e4b1c 309 * <td>32.768KHz from 32KHz internal ULP oscillator</td>
Kojto 111:4336505e4b1c 310 * </tr>
Kojto 111:4336505e4b1c 311 * <tr>
Kojto 111:4336505e4b1c 312 * <td>1.024KHz</td>
Kojto 111:4336505e4b1c 313 * <td>OSC1K</td>
Kojto 111:4336505e4b1c 314 * <td>1.024KHz from 32KHz internal oscillator</td>
Kojto 111:4336505e4b1c 315 * </tr>
Kojto 111:4336505e4b1c 316 * <tr>
Kojto 111:4336505e4b1c 317 * <td>32.768KHz</td>
Kojto 111:4336505e4b1c 318 * <td>OSC32K</td>
Kojto 111:4336505e4b1c 319 * <td>32.768KHz from 32KHz internal oscillator</td>
Kojto 111:4336505e4b1c 320 * </tr>
Kojto 111:4336505e4b1c 321 * <tr>
Kojto 111:4336505e4b1c 322 * <td>1.024KHz</td>
Kojto 111:4336505e4b1c 323 * <td>XOSC1K</td>
Kojto 111:4336505e4b1c 324 * <td>1.024KHz from 32KHz internal oscillator</td>
Kojto 111:4336505e4b1c 325 * </tr>
Kojto 111:4336505e4b1c 326 * <tr>
Kojto 111:4336505e4b1c 327 * <td>32.768KHz</td>
Kojto 111:4336505e4b1c 328 * <td>XOSC32K</td>
Kojto 111:4336505e4b1c 329 * <td>32.768KHz from 32KHz external crystal oscillator</td>
Kojto 111:4336505e4b1c 330 * </tr>
Kojto 111:4336505e4b1c 331 * </table>
Kojto 111:4336505e4b1c 332 *
Kojto 111:4336505e4b1c 333 * \section asfdoc_sam0_rtc_count_extra_info Extra Information
Kojto 111:4336505e4b1c 334 *
Kojto 111:4336505e4b1c 335 * For extra information, see \ref asfdoc_sam0_rtc_count_extra. This includes:
Kojto 111:4336505e4b1c 336 * - \ref asfdoc_sam0_rtc_count_extra_acronyms
Kojto 111:4336505e4b1c 337 * - \ref asfdoc_sam0_rtc_count_extra_dependencies
Kojto 111:4336505e4b1c 338 * - \ref asfdoc_sam0_rtc_count_extra_errata
Kojto 111:4336505e4b1c 339 * - \ref asfdoc_sam0_rtc_count_extra_history
Kojto 111:4336505e4b1c 340 *
Kojto 111:4336505e4b1c 341 *
Kojto 111:4336505e4b1c 342 * \section asfdoc_sam0_rtc_count_examples Examples
Kojto 111:4336505e4b1c 343 *
Kojto 111:4336505e4b1c 344 * For a list of examples related to this driver, see
Kojto 111:4336505e4b1c 345 * \ref asfdoc_sam0_rtc_count_exqsg.
Kojto 111:4336505e4b1c 346 *
Kojto 111:4336505e4b1c 347 *
Kojto 111:4336505e4b1c 348 * \section asfdoc_sam0_rtc_count_api_overview API Overview
Kojto 111:4336505e4b1c 349 * @{
Kojto 111:4336505e4b1c 350 */
Kojto 111:4336505e4b1c 351
Kojto 111:4336505e4b1c 352 #include <compiler.h>
Kojto 111:4336505e4b1c 353 #include <conf_clocks.h>
Kojto 111:4336505e4b1c 354
Kojto 111:4336505e4b1c 355 #if RTC_COUNT_ASYNC == true
Kojto 111:4336505e4b1c 356 # include <system_interrupt.h>
Kojto 111:4336505e4b1c 357 #endif
Kojto 111:4336505e4b1c 358
Kojto 111:4336505e4b1c 359 #ifdef __cplusplus
Kojto 111:4336505e4b1c 360 extern "C" {
Kojto 111:4336505e4b1c 361 #endif
Kojto 111:4336505e4b1c 362
Kojto 111:4336505e4b1c 363 /**
Kojto 111:4336505e4b1c 364 * Define port features set according to different device family
Kojto 111:4336505e4b1c 365 * @{
Kojto 111:4336505e4b1c 366 */
Kojto 111:4336505e4b1c 367 #if (SAML21) || (SAMC20) || (SAMC21) || defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 368 /** RTC periodic interval interrupt. */
Kojto 111:4336505e4b1c 369 # define FEATURE_RTC_PERIODIC_INT
Kojto 111:4336505e4b1c 370 /** RTC prescaler is off. */
Kojto 111:4336505e4b1c 371 # define FEATURE_RTC_PRESCALER_OFF
Kojto 111:4336505e4b1c 372 /** RTC clock selection. */
Kojto 111:4336505e4b1c 373 # define FEATURE_RTC_CLOCK_SELECTION
Kojto 111:4336505e4b1c 374 # if !(SAMC20) && !(SAMC21)
Kojto 111:4336505e4b1c 375 /** General purpose registers. */
Kojto 111:4336505e4b1c 376 # define FEATURE_RTC_GENERAL_PURPOSE_REG
Kojto 111:4336505e4b1c 377 # endif
Kojto 111:4336505e4b1c 378 #else
Kojto 111:4336505e4b1c 379 /** RTC continuously updated. */
Kojto 111:4336505e4b1c 380 # define FEATURE_RTC_CONTINUOUSLY_UPDATED
Kojto 111:4336505e4b1c 381 #endif
Kojto 111:4336505e4b1c 382 /*@}*/
Kojto 111:4336505e4b1c 383
Kojto 111:4336505e4b1c 384 #ifdef FEATURE_RTC_CLOCK_SELECTION
Kojto 111:4336505e4b1c 385 /**
Kojto 111:4336505e4b1c 386 * \brief Available clock source for RTC.
Kojto 111:4336505e4b1c 387 * RTC clock source.
Kojto 111:4336505e4b1c 388 */
Kojto 111:4336505e4b1c 389 enum rtc_clock_sel {
Kojto 111:4336505e4b1c 390 /** 1.024KHz from 32KHz internal ULP oscillator. */
Kojto 111:4336505e4b1c 391 RTC_CLOCK_SELECTION_ULP1K = OSC32KCTRL_RTCCTRL_RTCSEL_ULP1K_Val,
Kojto 111:4336505e4b1c 392 /** 32.768KHz from 32KHz internal ULP oscillator. */
Kojto 111:4336505e4b1c 393 RTC_CLOCK_SELECTION_ULP32K = OSC32KCTRL_RTCCTRL_RTCSEL_ULP32K_Val,
Kojto 111:4336505e4b1c 394 /** 1.024KHz from 32KHz internal oscillator. */
Kojto 111:4336505e4b1c 395 RTC_CLOCK_SELECTION_OSC1K = OSC32KCTRL_RTCCTRL_RTCSEL_OSC1K_Val,
Kojto 111:4336505e4b1c 396 /** 32.768KHz from 32KHz internal oscillator. */
Kojto 111:4336505e4b1c 397 RTC_CLOCK_SELECTION_OSC32K = OSC32KCTRL_RTCCTRL_RTCSEL_OSC32K_Val,
Kojto 111:4336505e4b1c 398 /** 1.024KHz from 32KHz internal oscillator. */
Kojto 111:4336505e4b1c 399 RTC_CLOCK_SELECTION_XOSC1K = OSC32KCTRL_RTCCTRL_RTCSEL_XOSC1K_Val,
Kojto 111:4336505e4b1c 400 /** 32.768KHz from 32.768KHz external crystal oscillator. */
Kojto 111:4336505e4b1c 401 RTC_CLOCK_SELECTION_XOSC32K = OSC32KCTRL_RTCCTRL_RTCSEL_XOSC32K_Val,
Kojto 111:4336505e4b1c 402 };
Kojto 111:4336505e4b1c 403 #endif
Kojto 111:4336505e4b1c 404
Kojto 111:4336505e4b1c 405 /**
Kojto 111:4336505e4b1c 406 * \brief Available operation modes for the RTC.
Kojto 111:4336505e4b1c 407 *
Kojto 111:4336505e4b1c 408 * RTC Count operating modes, to select the counting width and associated module
Kojto 111:4336505e4b1c 409 * operation.
Kojto 111:4336505e4b1c 410 */
Kojto 111:4336505e4b1c 411 enum rtc_count_mode {
Kojto 111:4336505e4b1c 412 /** RTC Count module operates in 16-bit mode. */
Kojto 111:4336505e4b1c 413 RTC_COUNT_MODE_16BIT = 0,
Kojto 111:4336505e4b1c 414 /** RTC Count module operates in 32-bit mode. */
Kojto 111:4336505e4b1c 415 RTC_COUNT_MODE_32BIT = 1,
Kojto 111:4336505e4b1c 416 };
Kojto 111:4336505e4b1c 417
Kojto 111:4336505e4b1c 418 #if !defined (RTC_NUM_OF_COMP16) && defined(RTC_COMP16_NUM)
Kojto 111:4336505e4b1c 419 #define RTC_NUM_OF_COMP16 RTC_COMP16_NUM
Kojto 111:4336505e4b1c 420 #endif
Kojto 111:4336505e4b1c 421
Kojto 111:4336505e4b1c 422 /**
Kojto 111:4336505e4b1c 423 * \brief Available compare channels.
Kojto 111:4336505e4b1c 424 *
Kojto 111:4336505e4b1c 425 * \note Not all compare channels are available in all devices and modes.
Kojto 111:4336505e4b1c 426 */
Kojto 111:4336505e4b1c 427 enum rtc_count_compare {
Kojto 111:4336505e4b1c 428 /** Compare channel 0. */
Kojto 111:4336505e4b1c 429 RTC_COUNT_COMPARE_0 = 0,
Kojto 111:4336505e4b1c 430 #if (RTC_NUM_OF_COMP16 > 1) || defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 431 /** Compare channel 1. */
Kojto 111:4336505e4b1c 432 RTC_COUNT_COMPARE_1 = 1,
Kojto 111:4336505e4b1c 433 #endif
Kojto 111:4336505e4b1c 434 #if (RTC_NUM_OF_COMP16 > 2) || defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 435 /** Compare channel 2. */
Kojto 111:4336505e4b1c 436 RTC_COUNT_COMPARE_2 = 2,
Kojto 111:4336505e4b1c 437 #endif
Kojto 111:4336505e4b1c 438 #if (RTC_NUM_OF_COMP16 > 3) || defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 439 /** Compare channel 3. */
Kojto 111:4336505e4b1c 440 RTC_COUNT_COMPARE_3 = 3,
Kojto 111:4336505e4b1c 441 #endif
Kojto 111:4336505e4b1c 442 #if (RTC_NUM_OF_COMP16 > 4) || defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 443 /** Compare channel 4. */
Kojto 111:4336505e4b1c 444 RTC_COUNT_COMPARE_4 = 4,
Kojto 111:4336505e4b1c 445 #endif
Kojto 111:4336505e4b1c 446 #if (RTC_NUM_OF_COMP16 > 5) || defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 447 /** Compare channel 5. */
Kojto 111:4336505e4b1c 448 RTC_COUNT_COMPARE_5 = 5,
Kojto 111:4336505e4b1c 449 #endif
Kojto 111:4336505e4b1c 450 };
Kojto 111:4336505e4b1c 451
Kojto 111:4336505e4b1c 452 #ifdef FEATURE_RTC_PERIODIC_INT
Kojto 111:4336505e4b1c 453 /**
Kojto 111:4336505e4b1c 454 * \brief Available periodic interval source.
Kojto 111:4336505e4b1c 455 */
Kojto 111:4336505e4b1c 456 enum rtc_count_periodic_interval {
Kojto 111:4336505e4b1c 457 /** Periodic interval 0. */
Kojto 111:4336505e4b1c 458 RTC_COUNT_PERIODIC_INTERVAL_0 = 0,
Kojto 111:4336505e4b1c 459 /** Periodic interval 1. */
Kojto 111:4336505e4b1c 460 RTC_COUNT_PERIODIC_INTERVAL_1 = 1,
Kojto 111:4336505e4b1c 461 /** Periodic interval 2. */
Kojto 111:4336505e4b1c 462 RTC_COUNT_PERIODIC_INTERVAL_2 = 2,
Kojto 111:4336505e4b1c 463 /** Periodic interval 3. */
Kojto 111:4336505e4b1c 464 RTC_COUNT_PERIODIC_INTERVAL_3 = 3,
Kojto 111:4336505e4b1c 465 /** Periodic interval 4. */
Kojto 111:4336505e4b1c 466 RTC_COUNT_PERIODIC_INTERVAL_4 = 4,
Kojto 111:4336505e4b1c 467 /** Periodic interval 5. */
Kojto 111:4336505e4b1c 468 RTC_COUNT_PERIODIC_INTERVAL_5 = 5,
Kojto 111:4336505e4b1c 469 /** Periodic interval 6. */
Kojto 111:4336505e4b1c 470 RTC_COUNT_PERIODIC_INTERVAL_6 = 6,
Kojto 111:4336505e4b1c 471 /** Periodic interval 7. */
Kojto 111:4336505e4b1c 472 RTC_COUNT_PERIODIC_INTERVAL_7 = 7,
Kojto 111:4336505e4b1c 473 };
Kojto 111:4336505e4b1c 474 #endif
Kojto 111:4336505e4b1c 475
Kojto 111:4336505e4b1c 476 #if RTC_COUNT_ASYNC == true
Kojto 111:4336505e4b1c 477 #ifdef FEATURE_RTC_PERIODIC_INT
Kojto 111:4336505e4b1c 478 /**
Kojto 111:4336505e4b1c 479 * \brief Callback types.
Kojto 111:4336505e4b1c 480 *
Kojto 111:4336505e4b1c 481 * The available callback types for the RTC count module.
Kojto 111:4336505e4b1c 482 */
Kojto 111:4336505e4b1c 483 enum rtc_count_callback {
Kojto 111:4336505e4b1c 484 /** Callback for Periodic Interval 0 Interrupt. */
Kojto 111:4336505e4b1c 485 RTC_COUNT_CALLBACK_PERIODIC_INTERVAL_0 = 0,
Kojto 111:4336505e4b1c 486 /** Callback for Periodic Interval 1 Interrupt. */
Kojto 111:4336505e4b1c 487 RTC_COUNT_CALLBACK_PERIODIC_INTERVAL_1,
Kojto 111:4336505e4b1c 488 /** Callback for Periodic Interval 2 Interrupt. */
Kojto 111:4336505e4b1c 489 RTC_COUNT_CALLBACK_PERIODIC_INTERVAL_2,
Kojto 111:4336505e4b1c 490 /** Callback for Periodic Interval 3 Interrupt. */
Kojto 111:4336505e4b1c 491 RTC_COUNT_CALLBACK_PERIODIC_INTERVAL_3,
Kojto 111:4336505e4b1c 492 /** Callback for Periodic Interval 4 Interrupt. */
Kojto 111:4336505e4b1c 493 RTC_COUNT_CALLBACK_PERIODIC_INTERVAL_4,
Kojto 111:4336505e4b1c 494 /** Callback for Periodic Interval 5 Interrupt. */
Kojto 111:4336505e4b1c 495 RTC_COUNT_CALLBACK_PERIODIC_INTERVAL_5,
Kojto 111:4336505e4b1c 496 /** Callback for Periodic Interval 6 Interrupt. */
Kojto 111:4336505e4b1c 497 RTC_COUNT_CALLBACK_PERIODIC_INTERVAL_6,
Kojto 111:4336505e4b1c 498 /** Callback for Periodic Interval 7 Interrupt. */
Kojto 111:4336505e4b1c 499 RTC_COUNT_CALLBACK_PERIODIC_INTERVAL_7,
Kojto 111:4336505e4b1c 500 /** Callback for compare channel 0. */
Kojto 111:4336505e4b1c 501 RTC_COUNT_CALLBACK_COMPARE_0,
Kojto 111:4336505e4b1c 502 # if (RTC_NUM_OF_COMP16 > 1) || defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 503 /** Callback for compare channel 1. */
Kojto 111:4336505e4b1c 504 RTC_COUNT_CALLBACK_COMPARE_1,
Kojto 111:4336505e4b1c 505 # endif
Kojto 111:4336505e4b1c 506 # if (RTC_NUM_OF_COMP16 > 2) || defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 507 /** Callback for compare channel 2. */
Kojto 111:4336505e4b1c 508 RTC_COUNT_CALLBACK_COMPARE_2,
Kojto 111:4336505e4b1c 509 # endif
Kojto 111:4336505e4b1c 510 # if (RTC_NUM_OF_COMP16 > 3) || defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 511 /** Callback for compare channel 3. */
Kojto 111:4336505e4b1c 512 RTC_COUNT_CALLBACK_COMPARE_3,
Kojto 111:4336505e4b1c 513 # endif
Kojto 111:4336505e4b1c 514 # if (RTC_NUM_OF_COMP16 > 4) || defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 515 /** Callback for compare channel 4. */
Kojto 111:4336505e4b1c 516 RTC_COUNT_CALLBACK_COMPARE_4,
Kojto 111:4336505e4b1c 517 # endif
Kojto 111:4336505e4b1c 518 # if (RTC_NUM_OF_COMP16 > 5) || defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 519 /** Callback for compare channel 5. */
Kojto 111:4336505e4b1c 520 RTC_COUNT_CALLBACK_COMPARE_5,
Kojto 111:4336505e4b1c 521 # endif
Kojto 111:4336505e4b1c 522
Kojto 111:4336505e4b1c 523 /** Callback for overflow. */
Kojto 111:4336505e4b1c 524 RTC_COUNT_CALLBACK_OVERFLOW,
Kojto 111:4336505e4b1c 525 # if !defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 526 /** Total number of callbacks. */
Kojto 111:4336505e4b1c 527 _RTC_COUNT_CALLBACK_N
Kojto 111:4336505e4b1c 528 # endif
Kojto 111:4336505e4b1c 529 };
Kojto 111:4336505e4b1c 530 #else
Kojto 111:4336505e4b1c 531 /**
Kojto 111:4336505e4b1c 532 * \brief Callback types.
Kojto 111:4336505e4b1c 533 *
Kojto 111:4336505e4b1c 534 * The available callback types for the RTC count module.
Kojto 111:4336505e4b1c 535 */
Kojto 111:4336505e4b1c 536 enum rtc_count_callback {
Kojto 111:4336505e4b1c 537 /** Callback for compare channel 0. */
Kojto 111:4336505e4b1c 538 RTC_COUNT_CALLBACK_COMPARE_0 = 0,
Kojto 111:4336505e4b1c 539 # if (RTC_NUM_OF_COMP16 > 1) || defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 540 /** Callback for compare channel 1. */
Kojto 111:4336505e4b1c 541 RTC_COUNT_CALLBACK_COMPARE_1,
Kojto 111:4336505e4b1c 542 # endif
Kojto 111:4336505e4b1c 543 # if (RTC_NUM_OF_COMP16 > 2) || defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 544 /** Callback for compare channel 2. */
Kojto 111:4336505e4b1c 545 RTC_COUNT_CALLBACK_COMPARE_2,
Kojto 111:4336505e4b1c 546 # endif
Kojto 111:4336505e4b1c 547 # if (RTC_NUM_OF_COMP16 > 3) || defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 548 /** Callback for compare channel 3. */
Kojto 111:4336505e4b1c 549 RTC_COUNT_CALLBACK_COMPARE_3,
Kojto 111:4336505e4b1c 550 # endif
Kojto 111:4336505e4b1c 551 # if (RTC_NUM_OF_COMP16 > 4) || defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 552 /** Callback for compare channel 4. */
Kojto 111:4336505e4b1c 553 RTC_COUNT_CALLBACK_COMPARE_4,
Kojto 111:4336505e4b1c 554 # endif
Kojto 111:4336505e4b1c 555 # if (RTC_NUM_OF_COMP16 > 5) || defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 556 /** Callback for compare channel 5. */
Kojto 111:4336505e4b1c 557 RTC_COUNT_CALLBACK_COMPARE_5,
Kojto 111:4336505e4b1c 558 # endif
Kojto 111:4336505e4b1c 559 /** Callback for overflow. */
Kojto 111:4336505e4b1c 560 RTC_COUNT_CALLBACK_OVERFLOW,
Kojto 111:4336505e4b1c 561 # if !defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 562 /** Total number of callbacks. */
Kojto 111:4336505e4b1c 563 _RTC_COUNT_CALLBACK_N
Kojto 111:4336505e4b1c 564 # endif
Kojto 111:4336505e4b1c 565 };
Kojto 111:4336505e4b1c 566 #endif
Kojto 111:4336505e4b1c 567
Kojto 111:4336505e4b1c 568 # if !defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 569 typedef void (*rtc_count_callback_t)(void);
Kojto 111:4336505e4b1c 570 # endif
Kojto 111:4336505e4b1c 571 #endif
Kojto 111:4336505e4b1c 572
Kojto 111:4336505e4b1c 573 #ifdef FEATURE_RTC_PRESCALER_OFF
Kojto 111:4336505e4b1c 574 /**
Kojto 111:4336505e4b1c 575 * \brief RTC input clock prescaler settings.
Kojto 111:4336505e4b1c 576 *
Kojto 111:4336505e4b1c 577 * The available input clock prescaler values for the RTC count module.
Kojto 111:4336505e4b1c 578 */
Kojto 111:4336505e4b1c 579 enum rtc_count_prescaler {
Kojto 111:4336505e4b1c 580 /** RTC prescaler is off, and the input clock frequency is
Kojto 111:4336505e4b1c 581 prescaled by a factor of 1. */
Kojto 111:4336505e4b1c 582 RTC_COUNT_PRESCALER_OFF = RTC_MODE0_CTRLA_PRESCALER_OFF,
Kojto 111:4336505e4b1c 583 /** RTC input clock frequency is prescaled by a factor of 1. */
Kojto 111:4336505e4b1c 584 RTC_COUNT_PRESCALER_DIV_1 = RTC_MODE0_CTRLA_PRESCALER_DIV1,
Kojto 111:4336505e4b1c 585 /** RTC input clock frequency is prescaled by a factor of 2. */
Kojto 111:4336505e4b1c 586 RTC_COUNT_PRESCALER_DIV_2 = RTC_MODE0_CTRLA_PRESCALER_DIV2,
Kojto 111:4336505e4b1c 587 /** RTC input clock frequency is prescaled by a factor of 4. */
Kojto 111:4336505e4b1c 588 RTC_COUNT_PRESCALER_DIV_4 = RTC_MODE0_CTRLA_PRESCALER_DIV4,
Kojto 111:4336505e4b1c 589 /** RTC input clock frequency is prescaled by a factor of 8. */
Kojto 111:4336505e4b1c 590 RTC_COUNT_PRESCALER_DIV_8 = RTC_MODE0_CTRLA_PRESCALER_DIV8,
Kojto 111:4336505e4b1c 591 /** RTC input clock frequency is prescaled by a factor of 16. */
Kojto 111:4336505e4b1c 592 RTC_COUNT_PRESCALER_DIV_16 = RTC_MODE0_CTRLA_PRESCALER_DIV16,
Kojto 111:4336505e4b1c 593 /** RTC input clock frequency is prescaled by a factor of 32. */
Kojto 111:4336505e4b1c 594 RTC_COUNT_PRESCALER_DIV_32 = RTC_MODE0_CTRLA_PRESCALER_DIV32,
Kojto 111:4336505e4b1c 595 /** RTC input clock frequency is prescaled by a factor of 64. */
Kojto 111:4336505e4b1c 596 RTC_COUNT_PRESCALER_DIV_64 = RTC_MODE0_CTRLA_PRESCALER_DIV64,
Kojto 111:4336505e4b1c 597 /** RTC input clock frequency is prescaled by a factor of 128. */
Kojto 111:4336505e4b1c 598 RTC_COUNT_PRESCALER_DIV_128 = RTC_MODE0_CTRLA_PRESCALER_DIV128,
Kojto 111:4336505e4b1c 599 /** RTC input clock frequency is prescaled by a factor of 256. */
Kojto 111:4336505e4b1c 600 RTC_COUNT_PRESCALER_DIV_256 = RTC_MODE0_CTRLA_PRESCALER_DIV256,
Kojto 111:4336505e4b1c 601 /** RTC input clock frequency is prescaled by a factor of 512. */
Kojto 111:4336505e4b1c 602 RTC_COUNT_PRESCALER_DIV_512 = RTC_MODE0_CTRLA_PRESCALER_DIV512,
Kojto 111:4336505e4b1c 603 /** RTC input clock frequency is prescaled by a factor of 1024. */
Kojto 111:4336505e4b1c 604 RTC_COUNT_PRESCALER_DIV_1024 = RTC_MODE0_CTRLA_PRESCALER_DIV1024,
Kojto 111:4336505e4b1c 605 };
Kojto 111:4336505e4b1c 606 #else
Kojto 111:4336505e4b1c 607 /**
Kojto 111:4336505e4b1c 608 * \brief RTC input clock prescaler settings.
Kojto 111:4336505e4b1c 609 *
Kojto 111:4336505e4b1c 610 * The available input clock prescaler values for the RTC count module.
Kojto 111:4336505e4b1c 611 */
Kojto 111:4336505e4b1c 612 enum rtc_count_prescaler {
Kojto 111:4336505e4b1c 613 /** RTC input clock frequency is prescaled by a factor of 1. */
Kojto 111:4336505e4b1c 614 RTC_COUNT_PRESCALER_DIV_1 = RTC_MODE0_CTRL_PRESCALER_DIV1,
Kojto 111:4336505e4b1c 615 /** RTC input clock frequency is prescaled by a factor of 2. */
Kojto 111:4336505e4b1c 616 RTC_COUNT_PRESCALER_DIV_2 = RTC_MODE0_CTRL_PRESCALER_DIV2,
Kojto 111:4336505e4b1c 617 /** RTC input clock frequency is prescaled by a factor of 4. */
Kojto 111:4336505e4b1c 618 RTC_COUNT_PRESCALER_DIV_4 = RTC_MODE0_CTRL_PRESCALER_DIV4,
Kojto 111:4336505e4b1c 619 /** RTC input clock frequency is prescaled by a factor of 8. */
Kojto 111:4336505e4b1c 620 RTC_COUNT_PRESCALER_DIV_8 = RTC_MODE0_CTRL_PRESCALER_DIV8,
Kojto 111:4336505e4b1c 621 /** RTC input clock frequency is prescaled by a factor of 16. */
Kojto 111:4336505e4b1c 622 RTC_COUNT_PRESCALER_DIV_16 = RTC_MODE0_CTRL_PRESCALER_DIV16,
Kojto 111:4336505e4b1c 623 /** RTC input clock frequency is prescaled by a factor of 32. */
Kojto 111:4336505e4b1c 624 RTC_COUNT_PRESCALER_DIV_32 = RTC_MODE0_CTRL_PRESCALER_DIV32,
Kojto 111:4336505e4b1c 625 /** RTC input clock frequency is prescaled by a factor of 64. */
Kojto 111:4336505e4b1c 626 RTC_COUNT_PRESCALER_DIV_64 = RTC_MODE0_CTRL_PRESCALER_DIV64,
Kojto 111:4336505e4b1c 627 /** RTC input clock frequency is prescaled by a factor of 128. */
Kojto 111:4336505e4b1c 628 RTC_COUNT_PRESCALER_DIV_128 = RTC_MODE0_CTRL_PRESCALER_DIV128,
Kojto 111:4336505e4b1c 629 /** RTC input clock frequency is prescaled by a factor of 256. */
Kojto 111:4336505e4b1c 630 RTC_COUNT_PRESCALER_DIV_256 = RTC_MODE0_CTRL_PRESCALER_DIV256,
Kojto 111:4336505e4b1c 631 /** RTC input clock frequency is prescaled by a factor of 512. */
Kojto 111:4336505e4b1c 632 RTC_COUNT_PRESCALER_DIV_512 = RTC_MODE0_CTRL_PRESCALER_DIV512,
Kojto 111:4336505e4b1c 633 /** RTC input clock frequency is prescaled by a factor of 1024. */
Kojto 111:4336505e4b1c 634 RTC_COUNT_PRESCALER_DIV_1024 = RTC_MODE0_CTRL_PRESCALER_DIV1024,
Kojto 111:4336505e4b1c 635 };
Kojto 111:4336505e4b1c 636 #endif
Kojto 111:4336505e4b1c 637
Kojto 111:4336505e4b1c 638 /**
Kojto 111:4336505e4b1c 639 * \brief RTC Count event enable/disable structure.
Kojto 111:4336505e4b1c 640 *
Kojto 111:4336505e4b1c 641 * Event flags for the \ref rtc_count_enable_events() and
Kojto 111:4336505e4b1c 642 * \ref rtc_count_disable_events().
Kojto 111:4336505e4b1c 643 */
Kojto 111:4336505e4b1c 644 struct rtc_count_events {
Kojto 111:4336505e4b1c 645 /** Generate an output event on each overflow of the RTC count. */
Kojto 111:4336505e4b1c 646 bool generate_event_on_overflow;
Kojto 111:4336505e4b1c 647 /** Generate an output event on a compare channel match against the RTC
Kojto 111:4336505e4b1c 648 * count. */
Kojto 111:4336505e4b1c 649 bool generate_event_on_compare[RTC_NUM_OF_COMP16];
Kojto 111:4336505e4b1c 650 /** Generate an output event periodically at a binary division of the RTC
Kojto 111:4336505e4b1c 651 * counter frequency. */
Kojto 111:4336505e4b1c 652 bool generate_event_on_periodic[8];
Kojto 111:4336505e4b1c 653 };
Kojto 111:4336505e4b1c 654
Kojto 111:4336505e4b1c 655 #if !defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 656 /**
Kojto 111:4336505e4b1c 657 * \brief Device structure.
Kojto 111:4336505e4b1c 658 */
Kojto 111:4336505e4b1c 659 struct rtc_module {
Kojto 111:4336505e4b1c 660 /** RTC hardware module. */
Kojto 111:4336505e4b1c 661 Rtc *hw;
Kojto 111:4336505e4b1c 662 /** Operation mode of count. */
Kojto 111:4336505e4b1c 663 enum rtc_count_mode mode;
Kojto 111:4336505e4b1c 664 #ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED
Kojto 111:4336505e4b1c 665 /** Set if counter value should be continuously updated. */
Kojto 111:4336505e4b1c 666 bool continuously_update;
Kojto 111:4336505e4b1c 667 #endif
Kojto 111:4336505e4b1c 668 # if RTC_COUNT_ASYNC == true
Kojto 111:4336505e4b1c 669 /** Pointers to callback functions. */
Kojto 111:4336505e4b1c 670 volatile rtc_count_callback_t callbacks[_RTC_COUNT_CALLBACK_N];
Kojto 111:4336505e4b1c 671 /** Mask for registered callbacks. */
Kojto 111:4336505e4b1c 672 volatile uint16_t registered_callback;
Kojto 111:4336505e4b1c 673 /** Mask for enabled callbacks. */
Kojto 111:4336505e4b1c 674 volatile uint16_t enabled_callback;
Kojto 111:4336505e4b1c 675 # endif
Kojto 111:4336505e4b1c 676 };
Kojto 111:4336505e4b1c 677 #endif
Kojto 111:4336505e4b1c 678
Kojto 111:4336505e4b1c 679 /**
Kojto 111:4336505e4b1c 680 * \brief RTC Count configuration structure.
Kojto 111:4336505e4b1c 681 *
Kojto 111:4336505e4b1c 682 * Configuration structure for the RTC instance. This structure should
Kojto 111:4336505e4b1c 683 * be initialized using the \ref rtc_count_get_config_defaults() before any
Kojto 111:4336505e4b1c 684 * user configurations are set.
Kojto 111:4336505e4b1c 685 */
Kojto 111:4336505e4b1c 686 struct rtc_count_config {
Kojto 111:4336505e4b1c 687 /** Input clock prescaler for the RTC module. */
Kojto 111:4336505e4b1c 688 enum rtc_count_prescaler prescaler;
Kojto 111:4336505e4b1c 689 /** Select the operation mode of the RTC.*/
Kojto 111:4336505e4b1c 690 enum rtc_count_mode mode;
Kojto 111:4336505e4b1c 691 /** If true, clears the counter value on compare match. Only available
Kojto 111:4336505e4b1c 692 * whilst running in 32-bit mode. */
Kojto 111:4336505e4b1c 693 bool clear_on_match;
Kojto 111:4336505e4b1c 694 #ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED
Kojto 111:4336505e4b1c 695 /** Continuously update the counter value so no synchronization is
Kojto 111:4336505e4b1c 696 * needed for reading. */
Kojto 111:4336505e4b1c 697 bool continuously_update;
Kojto 111:4336505e4b1c 698 #endif
Kojto 111:4336505e4b1c 699 #if (SAML21) || (SAMC20) || (SAMC21)
Kojto 111:4336505e4b1c 700 /** Enable count read synchronization. The COUNT value requires
Kojto 111:4336505e4b1c 701 * synchronization when reading. Disabling the synchronization
Kojto 111:4336505e4b1c 702 * will prevent the COUNT value from displaying the current value. */
Kojto 111:4336505e4b1c 703 bool enable_read_sync;
Kojto 111:4336505e4b1c 704 #endif
Kojto 111:4336505e4b1c 705
Kojto 111:4336505e4b1c 706 /** Array of Compare values. Not all Compare values are available in 32-bit
Kojto 111:4336505e4b1c 707 * mode. */
Kojto 111:4336505e4b1c 708 uint32_t compare_values[RTC_NUM_OF_COMP16];
Kojto 111:4336505e4b1c 709 };
Kojto 111:4336505e4b1c 710
Kojto 111:4336505e4b1c 711
Kojto 111:4336505e4b1c 712 /**
Kojto 111:4336505e4b1c 713 * \name Configuration and Initialization
Kojto 111:4336505e4b1c 714 * @{
Kojto 111:4336505e4b1c 715 */
Kojto 111:4336505e4b1c 716
Kojto 111:4336505e4b1c 717 /**
Kojto 111:4336505e4b1c 718 * \brief Gets the RTC default configurations.
Kojto 111:4336505e4b1c 719 *
Kojto 111:4336505e4b1c 720 * Initializes the configuration structure to default values. This
Kojto 111:4336505e4b1c 721 * function should be called at the start of any RTC initialization.
Kojto 111:4336505e4b1c 722 *
Kojto 111:4336505e4b1c 723 * The default configuration is as follows:
Kojto 111:4336505e4b1c 724 * - Input clock divided by a factor of 1024
Kojto 111:4336505e4b1c 725 * - RTC in 32-bit mode
Kojto 111:4336505e4b1c 726 * - Clear on compare match off
Kojto 111:4336505e4b1c 727 * - Continuously sync count register off
Kojto 111:4336505e4b1c 728 * - No event source on
Kojto 111:4336505e4b1c 729 * - All compare values equal 0
Kojto 111:4336505e4b1c 730 * - Count read synchronization is disabled for SAML21
Kojto 111:4336505e4b1c 731 *
Kojto 111:4336505e4b1c 732 * \param[out] config Configuration structure to be initialized to default
Kojto 111:4336505e4b1c 733 * values.
Kojto 111:4336505e4b1c 734 */
Kojto 111:4336505e4b1c 735 static inline void rtc_count_get_config_defaults(
Kojto 111:4336505e4b1c 736 struct rtc_count_config *const config)
Kojto 111:4336505e4b1c 737 {
Kojto 111:4336505e4b1c 738 /* Sanity check argument */
Kojto 111:4336505e4b1c 739 Assert(config);
Kojto 111:4336505e4b1c 740
Kojto 111:4336505e4b1c 741 /* Set default into configuration structure */
Kojto 111:4336505e4b1c 742 config->prescaler = RTC_COUNT_PRESCALER_DIV_1024;
Kojto 111:4336505e4b1c 743 config->mode = RTC_COUNT_MODE_32BIT;
Kojto 111:4336505e4b1c 744 config->clear_on_match = false;
Kojto 111:4336505e4b1c 745
Kojto 111:4336505e4b1c 746 #ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED
Kojto 111:4336505e4b1c 747 config->continuously_update = false;
Kojto 111:4336505e4b1c 748 #endif
Kojto 111:4336505e4b1c 749 #if (SAML21)
Kojto 111:4336505e4b1c 750 config->enable_read_sync = false;
Kojto 111:4336505e4b1c 751 #endif
Kojto 111:4336505e4b1c 752
Kojto 111:4336505e4b1c 753 for (uint8_t i = 0; i < RTC_NUM_OF_COMP16; i++) {
Kojto 111:4336505e4b1c 754 config->compare_values[i] = 0;
Kojto 111:4336505e4b1c 755 }
Kojto 111:4336505e4b1c 756 }
Kojto 111:4336505e4b1c 757
Kojto 111:4336505e4b1c 758 void rtc_count_reset(struct rtc_module *const module);
Kojto 111:4336505e4b1c 759 void rtc_count_enable(struct rtc_module *const module);
Kojto 111:4336505e4b1c 760 void rtc_count_disable(struct rtc_module *const module);
Kojto 111:4336505e4b1c 761
Kojto 111:4336505e4b1c 762 #if (RTC_INST_NUM > 1) && !defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 763 /**
Kojto 111:4336505e4b1c 764 * \internal Find the index of given RTC module instance.
Kojto 111:4336505e4b1c 765 *
Kojto 111:4336505e4b1c 766 * \param[in] RTC module instance pointer
Kojto 111:4336505e4b1c 767 *
Kojto 111:4336505e4b1c 768 * \return Index of the given AC module instance.
Kojto 111:4336505e4b1c 769 */
Kojto 111:4336505e4b1c 770 uint8_t _rtc_get_inst_index(
Kojto 111:4336505e4b1c 771 Rtc *const hw)
Kojto 111:4336505e4b1c 772 {
Kojto 111:4336505e4b1c 773 /* List of available RTC modules. */
Kojto 111:4336505e4b1c 774 static Rtc *const rtc_modules[RTC_INST_NUM] = RTC_INSTS;
Kojto 111:4336505e4b1c 775
Kojto 111:4336505e4b1c 776 /* Find index for RTC instance. */
Kojto 111:4336505e4b1c 777 for (uint32_t i = 0; i < RTC_INST_NUM; i++) {
Kojto 111:4336505e4b1c 778 if (hw == rtc_modules[i]) {
Kojto 111:4336505e4b1c 779 return i;
Kojto 111:4336505e4b1c 780 }
Kojto 111:4336505e4b1c 781 }
Kojto 111:4336505e4b1c 782
Kojto 111:4336505e4b1c 783 /* Invalid data given. */
Kojto 111:4336505e4b1c 784 Assert(false);
Kojto 111:4336505e4b1c 785 return 0;
Kojto 111:4336505e4b1c 786 }
Kojto 111:4336505e4b1c 787 #endif /* (RTC_INST_NUM > 1) && !defined(__DOXYGEN__) */
Kojto 111:4336505e4b1c 788
Kojto 111:4336505e4b1c 789 enum status_code rtc_count_init(
Kojto 111:4336505e4b1c 790 struct rtc_module *const module,
Kojto 111:4336505e4b1c 791 Rtc *const hw,
Kojto 111:4336505e4b1c 792 const struct rtc_count_config *const config);
Kojto 111:4336505e4b1c 793
Kojto 111:4336505e4b1c 794 enum status_code rtc_count_frequency_correction(
Kojto 111:4336505e4b1c 795 struct rtc_module *const module,
Kojto 111:4336505e4b1c 796 const int8_t value);
Kojto 111:4336505e4b1c 797
Kojto 111:4336505e4b1c 798 /** @} */
Kojto 111:4336505e4b1c 799
Kojto 111:4336505e4b1c 800 /** \name Count and Compare Value Management
Kojto 111:4336505e4b1c 801 * @{
Kojto 111:4336505e4b1c 802 */
Kojto 111:4336505e4b1c 803 enum status_code rtc_count_set_count(
Kojto 111:4336505e4b1c 804 struct rtc_module *const module,
Kojto 111:4336505e4b1c 805 const uint32_t count_value);
Kojto 111:4336505e4b1c 806
Kojto 111:4336505e4b1c 807 uint32_t rtc_count_get_count(struct rtc_module *const module);
Kojto 111:4336505e4b1c 808
Kojto 111:4336505e4b1c 809 enum status_code rtc_count_set_compare(
Kojto 111:4336505e4b1c 810 struct rtc_module *const module,
Kojto 111:4336505e4b1c 811 const uint32_t comp_value,
Kojto 111:4336505e4b1c 812 const enum rtc_count_compare comp_index);
Kojto 111:4336505e4b1c 813
Kojto 111:4336505e4b1c 814 enum status_code rtc_count_get_compare(
Kojto 111:4336505e4b1c 815 struct rtc_module *const module,
Kojto 111:4336505e4b1c 816 uint32_t *const comp_value,
Kojto 111:4336505e4b1c 817 const enum rtc_count_compare comp_index);
Kojto 111:4336505e4b1c 818
Kojto 111:4336505e4b1c 819 enum status_code rtc_count_set_period(
Kojto 111:4336505e4b1c 820 struct rtc_module *const module,
Kojto 111:4336505e4b1c 821 uint16_t period_value);
Kojto 111:4336505e4b1c 822
Kojto 111:4336505e4b1c 823 enum status_code rtc_count_get_period(
Kojto 111:4336505e4b1c 824 struct rtc_module *const module,
Kojto 111:4336505e4b1c 825 uint16_t *const period_value);
Kojto 111:4336505e4b1c 826
Kojto 111:4336505e4b1c 827 /** @} */
Kojto 111:4336505e4b1c 828
Kojto 111:4336505e4b1c 829
Kojto 111:4336505e4b1c 830 /** \name Status Management
Kojto 111:4336505e4b1c 831 * @{
Kojto 111:4336505e4b1c 832 */
Kojto 111:4336505e4b1c 833
Kojto 111:4336505e4b1c 834 /**
Kojto 111:4336505e4b1c 835 * \brief Check if an RTC overflow has occurred.
Kojto 111:4336505e4b1c 836 *
Kojto 111:4336505e4b1c 837 * Checks the overflow flag in the RTC. The flag is set when there
Kojto 111:4336505e4b1c 838 * is an overflow in the clock.
Kojto 111:4336505e4b1c 839 *
Kojto 111:4336505e4b1c 840 * \param[in,out] module RTC hardware module
Kojto 111:4336505e4b1c 841 *
Kojto 111:4336505e4b1c 842 * \return Overflow state of the RTC module.
Kojto 111:4336505e4b1c 843 *
Kojto 111:4336505e4b1c 844 * \retval true If the RTC count value has overflowed
Kojto 111:4336505e4b1c 845 * \retval false If the RTC count value has not overflowed
Kojto 111:4336505e4b1c 846 */
Kojto 111:4336505e4b1c 847
Kojto 111:4336505e4b1c 848 static inline bool rtc_count_is_overflow(struct rtc_module *const module)
Kojto 111:4336505e4b1c 849 {
Kojto 111:4336505e4b1c 850 /* Sanity check arguments */
Kojto 111:4336505e4b1c 851 Assert(module);
Kojto 111:4336505e4b1c 852 Assert(module->hw);
Kojto 111:4336505e4b1c 853
Kojto 111:4336505e4b1c 854 Rtc *const rtc_module = module->hw;
Kojto 111:4336505e4b1c 855
Kojto 111:4336505e4b1c 856 /* Return status of flag */
Kojto 111:4336505e4b1c 857 return (rtc_module->MODE0.INTFLAG.reg & RTC_MODE0_INTFLAG_OVF);
Kojto 111:4336505e4b1c 858 }
Kojto 111:4336505e4b1c 859
Kojto 111:4336505e4b1c 860 /**
Kojto 111:4336505e4b1c 861 * \brief Clears the RTC overflow flag.
Kojto 111:4336505e4b1c 862 *
Kojto 111:4336505e4b1c 863 * Clears the RTC module counter overflow flag, so that new overflow conditions
Kojto 111:4336505e4b1c 864 * can be detected.
Kojto 111:4336505e4b1c 865 *
Kojto 111:4336505e4b1c 866 * \param[in,out] module RTC hardware module
Kojto 111:4336505e4b1c 867 */
Kojto 111:4336505e4b1c 868 static inline void rtc_count_clear_overflow(struct rtc_module *const module)
Kojto 111:4336505e4b1c 869 {
Kojto 111:4336505e4b1c 870 /* Sanity check arguments */
Kojto 111:4336505e4b1c 871 Assert(module);
Kojto 111:4336505e4b1c 872 Assert(module->hw);
Kojto 111:4336505e4b1c 873
Kojto 111:4336505e4b1c 874 Rtc *const rtc_module = module->hw;
Kojto 111:4336505e4b1c 875
Kojto 111:4336505e4b1c 876 /* Clear OVF flag */
Kojto 111:4336505e4b1c 877 rtc_module->MODE0.INTFLAG.reg = RTC_MODE0_INTFLAG_OVF;
Kojto 111:4336505e4b1c 878 }
Kojto 111:4336505e4b1c 879
Kojto 111:4336505e4b1c 880 #ifdef FEATURE_RTC_PERIODIC_INT
Kojto 111:4336505e4b1c 881 /**
Kojto 111:4336505e4b1c 882 * \brief Check if an RTC periodic interval interrupt has occurred.
Kojto 111:4336505e4b1c 883 *
Kojto 111:4336505e4b1c 884 * Checks the periodic interval flag in the RTC.
Kojto 111:4336505e4b1c 885 *
Kojto 111:4336505e4b1c 886 * \param[in,out] module RTC hardware module
Kojto 111:4336505e4b1c 887 * \param[in] n RTC periodic interval interrupt
Kojto 111:4336505e4b1c 888 *
Kojto 111:4336505e4b1c 889 * \return periodic interval interrupt state of the RTC module.
Kojto 111:4336505e4b1c 890 *
Kojto 111:4336505e4b1c 891 * \retval true RTC periodic interval interrupt occurs
Kojto 111:4336505e4b1c 892 * \retval false RTC periodic interval interrupt dosen't occurs
Kojto 111:4336505e4b1c 893 */
Kojto 111:4336505e4b1c 894 static inline bool rtc_count_is_periodic_interval(struct rtc_module *const module,
Kojto 111:4336505e4b1c 895 enum rtc_count_periodic_interval n)
Kojto 111:4336505e4b1c 896 {
Kojto 111:4336505e4b1c 897 /* Sanity check arguments */
Kojto 111:4336505e4b1c 898 Assert(module);
Kojto 111:4336505e4b1c 899 Assert(module->hw);
Kojto 111:4336505e4b1c 900
Kojto 111:4336505e4b1c 901 Rtc *const rtc_module = module->hw;
Kojto 111:4336505e4b1c 902
Kojto 111:4336505e4b1c 903 /* Return status of flag */
Kojto 111:4336505e4b1c 904 return (rtc_module->MODE0.INTFLAG.reg & RTC_MODE0_INTFLAG_PER(1 << n));
Kojto 111:4336505e4b1c 905 }
Kojto 111:4336505e4b1c 906
Kojto 111:4336505e4b1c 907 /**
Kojto 111:4336505e4b1c 908 * \brief Clears the RTC periodic interval flag.
Kojto 111:4336505e4b1c 909 *
Kojto 111:4336505e4b1c 910 * Clears the RTC module counter periodic interval flag, so that new periodic
Kojto 111:4336505e4b1c 911 * interval conditions can be detected.
Kojto 111:4336505e4b1c 912 *
Kojto 111:4336505e4b1c 913 * \param[in,out] module RTC hardware module
Kojto 111:4336505e4b1c 914 * \param[in] n RTC periodic interval interrupt
Kojto 111:4336505e4b1c 915 */
Kojto 111:4336505e4b1c 916 static inline void rtc_count_clear_periodic_interval(struct rtc_module *const module,
Kojto 111:4336505e4b1c 917 enum rtc_count_periodic_interval n)
Kojto 111:4336505e4b1c 918 {
Kojto 111:4336505e4b1c 919 /* Sanity check arguments */
Kojto 111:4336505e4b1c 920 Assert(module);
Kojto 111:4336505e4b1c 921 Assert(module->hw);
Kojto 111:4336505e4b1c 922
Kojto 111:4336505e4b1c 923 Rtc *const rtc_module = module->hw;
Kojto 111:4336505e4b1c 924
Kojto 111:4336505e4b1c 925 /* Clear periodic interval flag */
Kojto 111:4336505e4b1c 926 rtc_module->MODE0.INTFLAG.reg = RTC_MODE0_INTFLAG_PER(1 << n);
Kojto 111:4336505e4b1c 927 }
Kojto 111:4336505e4b1c 928 #endif
Kojto 111:4336505e4b1c 929 bool rtc_count_is_compare_match(
Kojto 111:4336505e4b1c 930 struct rtc_module *const module,
Kojto 111:4336505e4b1c 931 const enum rtc_count_compare comp_index);
Kojto 111:4336505e4b1c 932
Kojto 111:4336505e4b1c 933 enum status_code rtc_count_clear_compare_match(
Kojto 111:4336505e4b1c 934 struct rtc_module *const module,
Kojto 111:4336505e4b1c 935 const enum rtc_count_compare comp_index);
Kojto 111:4336505e4b1c 936
Kojto 111:4336505e4b1c 937 /** @} */
Kojto 111:4336505e4b1c 938
Kojto 111:4336505e4b1c 939
Kojto 111:4336505e4b1c 940 /**
Kojto 111:4336505e4b1c 941 * \name Event Management
Kojto 111:4336505e4b1c 942 * @{
Kojto 111:4336505e4b1c 943 */
Kojto 111:4336505e4b1c 944
Kojto 111:4336505e4b1c 945 /**
Kojto 111:4336505e4b1c 946 * \brief Enables a RTC event output.
Kojto 111:4336505e4b1c 947 *
Kojto 111:4336505e4b1c 948 * Enables one or more output events from the RTC module. See
Kojto 111:4336505e4b1c 949 * \ref rtc_count_events for a list of events this module supports.
Kojto 111:4336505e4b1c 950 *
Kojto 111:4336505e4b1c 951 * \note Events cannot be altered while the module is enabled.
Kojto 111:4336505e4b1c 952 *
Kojto 111:4336505e4b1c 953 * \param[in,out] module RTC hardware module
Kojto 111:4336505e4b1c 954 * \param[in] events Struct containing flags of events to enable
Kojto 111:4336505e4b1c 955 */
Kojto 111:4336505e4b1c 956 static inline void rtc_count_enable_events(
Kojto 111:4336505e4b1c 957 struct rtc_module *const module,
Kojto 111:4336505e4b1c 958 struct rtc_count_events *const events)
Kojto 111:4336505e4b1c 959 {
Kojto 111:4336505e4b1c 960 /* Sanity check arguments */
Kojto 111:4336505e4b1c 961 Assert(module);
Kojto 111:4336505e4b1c 962 Assert(module->hw);
Kojto 111:4336505e4b1c 963
Kojto 111:4336505e4b1c 964 Rtc *const rtc_module = module->hw;
Kojto 111:4336505e4b1c 965
Kojto 111:4336505e4b1c 966 uint32_t event_mask = 0;
Kojto 111:4336505e4b1c 967
Kojto 111:4336505e4b1c 968 /* Check if the user has requested an overflow event. */
Kojto 111:4336505e4b1c 969 if (events->generate_event_on_overflow) {
Kojto 111:4336505e4b1c 970 event_mask |= RTC_MODE0_EVCTRL_OVFEO;
Kojto 111:4336505e4b1c 971 }
Kojto 111:4336505e4b1c 972
Kojto 111:4336505e4b1c 973 /* Check if the user has requested any compare events. */
Kojto 111:4336505e4b1c 974 for (uint8_t i = 0; i < RTC_NUM_OF_COMP16; i++) {
Kojto 111:4336505e4b1c 975 if (events->generate_event_on_compare[i]) {
Kojto 111:4336505e4b1c 976 event_mask |= RTC_MODE0_EVCTRL_CMPEO(1 << i);
Kojto 111:4336505e4b1c 977 }
Kojto 111:4336505e4b1c 978 }
Kojto 111:4336505e4b1c 979
Kojto 111:4336505e4b1c 980 /* Check if the user has requested any periodic events. */
Kojto 111:4336505e4b1c 981 for (uint8_t i = 0; i < 8; i++) {
Kojto 111:4336505e4b1c 982 if (events->generate_event_on_periodic[i]) {
Kojto 111:4336505e4b1c 983 event_mask |= RTC_MODE0_EVCTRL_PEREO(1 << i);
Kojto 111:4336505e4b1c 984 }
Kojto 111:4336505e4b1c 985 }
Kojto 111:4336505e4b1c 986
Kojto 111:4336505e4b1c 987 /* Enable given event(s). */
Kojto 111:4336505e4b1c 988 rtc_module->MODE0.EVCTRL.reg |= event_mask;
Kojto 111:4336505e4b1c 989 }
Kojto 111:4336505e4b1c 990
Kojto 111:4336505e4b1c 991 /**
Kojto 111:4336505e4b1c 992 * \brief Disables a RTC event output.
Kojto 111:4336505e4b1c 993 *
Kojto 111:4336505e4b1c 994 * Disabled one or more output events from the RTC module. See
Kojto 111:4336505e4b1c 995 * \ref rtc_count_events for a list of events this module supports.
Kojto 111:4336505e4b1c 996 *
Kojto 111:4336505e4b1c 997 * \note Events cannot be altered while the module is enabled.
Kojto 111:4336505e4b1c 998 *
Kojto 111:4336505e4b1c 999 * \param[in,out] module RTC hardware module
Kojto 111:4336505e4b1c 1000 * \param[in] events Struct containing flags of events to disable
Kojto 111:4336505e4b1c 1001 */
Kojto 111:4336505e4b1c 1002 static inline void rtc_count_disable_events(
Kojto 111:4336505e4b1c 1003 struct rtc_module *const module,
Kojto 111:4336505e4b1c 1004 struct rtc_count_events *const events)
Kojto 111:4336505e4b1c 1005 {
Kojto 111:4336505e4b1c 1006 /* Sanity check arguments */
Kojto 111:4336505e4b1c 1007 Assert(module);
Kojto 111:4336505e4b1c 1008 Assert(module->hw);
Kojto 111:4336505e4b1c 1009
Kojto 111:4336505e4b1c 1010 Rtc *const rtc_module = module->hw;
Kojto 111:4336505e4b1c 1011
Kojto 111:4336505e4b1c 1012 uint32_t event_mask = 0;
Kojto 111:4336505e4b1c 1013
Kojto 111:4336505e4b1c 1014 /* Check if the user has requested an overflow event. */
Kojto 111:4336505e4b1c 1015 if (events->generate_event_on_overflow) {
Kojto 111:4336505e4b1c 1016 event_mask |= RTC_MODE0_EVCTRL_OVFEO;
Kojto 111:4336505e4b1c 1017 }
Kojto 111:4336505e4b1c 1018
Kojto 111:4336505e4b1c 1019 /* Check if the user has requested any compare events. */
Kojto 111:4336505e4b1c 1020 for (uint8_t i = 0; i < RTC_NUM_OF_COMP16; i++) {
Kojto 111:4336505e4b1c 1021 if (events->generate_event_on_compare[i]) {
Kojto 111:4336505e4b1c 1022 event_mask |= RTC_MODE0_EVCTRL_CMPEO(1 << i);
Kojto 111:4336505e4b1c 1023 }
Kojto 111:4336505e4b1c 1024 }
Kojto 111:4336505e4b1c 1025
Kojto 111:4336505e4b1c 1026 /* Check if the user has requested any periodic events. */
Kojto 111:4336505e4b1c 1027 for (uint8_t i = 0; i < 8; i++) {
Kojto 111:4336505e4b1c 1028 if (events->generate_event_on_periodic[i]) {
Kojto 111:4336505e4b1c 1029 event_mask |= RTC_MODE0_EVCTRL_PEREO(1 << i);
Kojto 111:4336505e4b1c 1030 }
Kojto 111:4336505e4b1c 1031 }
Kojto 111:4336505e4b1c 1032
Kojto 111:4336505e4b1c 1033 /* Disable given event(s). */
Kojto 111:4336505e4b1c 1034 rtc_module->MODE0.EVCTRL.reg &= ~event_mask;
Kojto 111:4336505e4b1c 1035 }
Kojto 111:4336505e4b1c 1036
Kojto 111:4336505e4b1c 1037 /** @} */
Kojto 111:4336505e4b1c 1038
Kojto 111:4336505e4b1c 1039 #ifdef FEATURE_RTC_GENERAL_PURPOSE_REG
Kojto 111:4336505e4b1c 1040 /**
Kojto 111:4336505e4b1c 1041 * \name RTC General Purpose Registers
Kojto 111:4336505e4b1c 1042 * @{
Kojto 111:4336505e4b1c 1043 */
Kojto 111:4336505e4b1c 1044
Kojto 111:4336505e4b1c 1045 /**
Kojto 111:4336505e4b1c 1046 * \brief Write a value into general purpose register.
Kojto 111:4336505e4b1c 1047 *
Kojto 111:4336505e4b1c 1048 * \param[in] module Pointer to the software instance struct
Kojto 111:4336505e4b1c 1049 * \param[in] n General purpose type
Kojto 111:4336505e4b1c 1050 * \param[in] index General purpose register index (0..3)
Kojto 111:4336505e4b1c 1051 *
Kojto 111:4336505e4b1c 1052 */
Kojto 111:4336505e4b1c 1053 static inline void rtc_write_general_purpose_reg(
Kojto 111:4336505e4b1c 1054 struct rtc_module *const module,
Kojto 111:4336505e4b1c 1055 const uint8_t index,
Kojto 111:4336505e4b1c 1056 uint32_t value)
Kojto 111:4336505e4b1c 1057 {
Kojto 111:4336505e4b1c 1058 /* Sanity check arguments */
Kojto 111:4336505e4b1c 1059 Assert(module);
Kojto 111:4336505e4b1c 1060 Assert(module->hw);
Kojto 111:4336505e4b1c 1061 Assert(index <= 3);
Kojto 111:4336505e4b1c 1062
Kojto 111:4336505e4b1c 1063 Rtc *const rtc_module = module->hw;
Kojto 111:4336505e4b1c 1064
Kojto 111:4336505e4b1c 1065 rtc_module->MODE0.GP[index].reg = value;
Kojto 111:4336505e4b1c 1066 }
Kojto 111:4336505e4b1c 1067
Kojto 111:4336505e4b1c 1068 /**
Kojto 111:4336505e4b1c 1069 * \brief Read the value from general purpose register.
Kojto 111:4336505e4b1c 1070 *
Kojto 111:4336505e4b1c 1071 * \param[in] module Pointer to the software instance struct
Kojto 111:4336505e4b1c 1072 * \param[in] index General purpose register index (0..3)
Kojto 111:4336505e4b1c 1073 *
Kojto 111:4336505e4b1c 1074 * \return Value of general purpose register
Kojto 111:4336505e4b1c 1075 */
Kojto 111:4336505e4b1c 1076 static inline uint32_t rtc_read_general_purpose_reg(
Kojto 111:4336505e4b1c 1077 struct rtc_module *const module,
Kojto 111:4336505e4b1c 1078 const uint8_t index)
Kojto 111:4336505e4b1c 1079 {
Kojto 111:4336505e4b1c 1080 /* Sanity check arguments */
Kojto 111:4336505e4b1c 1081 Assert(module);
Kojto 111:4336505e4b1c 1082 Assert(module->hw);
Kojto 111:4336505e4b1c 1083 Assert(index <= 3);
Kojto 111:4336505e4b1c 1084
Kojto 111:4336505e4b1c 1085 Rtc *const rtc_module = module->hw;
Kojto 111:4336505e4b1c 1086
Kojto 111:4336505e4b1c 1087 return rtc_module->MODE0.GP[index].reg;
Kojto 111:4336505e4b1c 1088 }
Kojto 111:4336505e4b1c 1089
Kojto 111:4336505e4b1c 1090 /** @} */
Kojto 111:4336505e4b1c 1091 #endif
Kojto 111:4336505e4b1c 1092
Kojto 111:4336505e4b1c 1093 /** @} */
Kojto 111:4336505e4b1c 1094
Kojto 111:4336505e4b1c 1095 #ifdef __cplusplus
Kojto 111:4336505e4b1c 1096 }
Kojto 111:4336505e4b1c 1097 #endif
Kojto 111:4336505e4b1c 1098
Kojto 111:4336505e4b1c 1099 /**
Kojto 111:4336505e4b1c 1100 * \page asfdoc_sam0_rtc_count_extra Extra Information for RTC COUNT Driver
Kojto 111:4336505e4b1c 1101 *
Kojto 111:4336505e4b1c 1102 * \section asfdoc_sam0_rtc_count_extra_acronyms Acronyms
Kojto 111:4336505e4b1c 1103 * Below is a table listing the acronyms used in this module, along with their
Kojto 111:4336505e4b1c 1104 * intended meanings.
Kojto 111:4336505e4b1c 1105 *
Kojto 111:4336505e4b1c 1106 * <table>
Kojto 111:4336505e4b1c 1107 * <tr>
Kojto 111:4336505e4b1c 1108 * <th>Acronym</td>
Kojto 111:4336505e4b1c 1109 * <th>Description</td>
Kojto 111:4336505e4b1c 1110 * </tr>
Kojto 111:4336505e4b1c 1111 * <tr>
Kojto 111:4336505e4b1c 1112 * <td>RTC</td>
Kojto 111:4336505e4b1c 1113 * <td>Real Time Counter</td>
Kojto 111:4336505e4b1c 1114 * </tr>
Kojto 111:4336505e4b1c 1115 * <tr>
Kojto 111:4336505e4b1c 1116 * <td>PPM</td>
Kojto 111:4336505e4b1c 1117 * <td>Part Per Million</td>
Kojto 111:4336505e4b1c 1118 * </tr>
Kojto 111:4336505e4b1c 1119 * <tr>
Kojto 111:4336505e4b1c 1120 * <td>RC</td>
Kojto 111:4336505e4b1c 1121 * <td>Resistor/Capacitor</td>
Kojto 111:4336505e4b1c 1122 * </tr>
Kojto 111:4336505e4b1c 1123 * </table>
Kojto 111:4336505e4b1c 1124 *
Kojto 111:4336505e4b1c 1125 *
Kojto 111:4336505e4b1c 1126 * \section asfdoc_sam0_rtc_count_extra_dependencies Dependencies
Kojto 111:4336505e4b1c 1127 * This driver has the following dependencies:
Kojto 111:4336505e4b1c 1128 *
Kojto 111:4336505e4b1c 1129 * - None
Kojto 111:4336505e4b1c 1130 *
Kojto 111:4336505e4b1c 1131 *
Kojto 111:4336505e4b1c 1132 * \section asfdoc_sam0_rtc_count_extra_errata Errata
Kojto 111:4336505e4b1c 1133 * There are no errata related to this driver.
Kojto 111:4336505e4b1c 1134 *
Kojto 111:4336505e4b1c 1135 *
Kojto 111:4336505e4b1c 1136 * \section asfdoc_sam0_rtc_count_extra_history Module History
Kojto 111:4336505e4b1c 1137 * An overview of the module history is presented in the table below, with
Kojto 111:4336505e4b1c 1138 * details on the enhancements and fixes made to the module since its first
Kojto 111:4336505e4b1c 1139 * release. The current version of this corresponds to the newest version in
Kojto 111:4336505e4b1c 1140 * the table.
Kojto 111:4336505e4b1c 1141 *
Kojto 111:4336505e4b1c 1142 * <table>
Kojto 111:4336505e4b1c 1143 * <tr>
Kojto 111:4336505e4b1c 1144 * <th>Changelog</th>
Kojto 111:4336505e4b1c 1145 * </tr>
Kojto 111:4336505e4b1c 1146 * <tr>
Kojto 111:4336505e4b1c 1147 * <td>Added support for SAMC21</td>
Kojto 111:4336505e4b1c 1148 * </tr>
Kojto 111:4336505e4b1c 1149 * <tr>
Kojto 111:4336505e4b1c 1150 * <td>Added support for SAML21</td>
Kojto 111:4336505e4b1c 1151 * </tr>
Kojto 111:4336505e4b1c 1152 * <tr>
Kojto 111:4336505e4b1c 1153 * <td>
Kojto 111:4336505e4b1c 1154 * Added support for SAMD21 and added driver instance parameter to all
Kojto 111:4336505e4b1c 1155 * API function calls, except get_config_defaults
Kojto 111:4336505e4b1c 1156 * </td>
Kojto 111:4336505e4b1c 1157 * </tr>
Kojto 111:4336505e4b1c 1158 * <tr>
Kojto 111:4336505e4b1c 1159 * <td>Updated initialization function to also enable the digital interface
Kojto 111:4336505e4b1c 1160 * clock to the module if it is disabled</td>
Kojto 111:4336505e4b1c 1161 * </tr>
Kojto 111:4336505e4b1c 1162 * <tr>
Kojto 111:4336505e4b1c 1163 * <td>Initial Release</td>
Kojto 111:4336505e4b1c 1164 * </tr>
Kojto 111:4336505e4b1c 1165 * </table>
Kojto 111:4336505e4b1c 1166 */
Kojto 111:4336505e4b1c 1167
Kojto 111:4336505e4b1c 1168 /**
Kojto 111:4336505e4b1c 1169 * \page asfdoc_sam0_rtc_count_exqsg Examples for RTC (COUNT) Driver
Kojto 111:4336505e4b1c 1170 *
Kojto 111:4336505e4b1c 1171 * This is a list of the available Quick Start guides (QSGs) and example
Kojto 111:4336505e4b1c 1172 * applications for \ref asfdoc_sam0_rtc_count_group. QSGs are simple
Kojto 111:4336505e4b1c 1173 * examples with step-by-step instructions to configure and use this driver in a
Kojto 111:4336505e4b1c 1174 * selection of use cases. Note that QSGs can be compiled as a standalone
Kojto 111:4336505e4b1c 1175 * application or be added to the user application.
Kojto 111:4336505e4b1c 1176 *
Kojto 111:4336505e4b1c 1177 * - \subpage asfdoc_sam0_rtc_count_basic_use_case
Kojto 111:4336505e4b1c 1178 * \if RTC_COUNT_CALLBACK_MODE
Kojto 111:4336505e4b1c 1179 * - \subpage asfdoc_sam0_rtc_count_callback_use_case
Kojto 111:4336505e4b1c 1180 * \endif
Kojto 111:4336505e4b1c 1181 *
Kojto 111:4336505e4b1c 1182 * \page asfdoc_sam0_rtc_count_document_revision_history Document Revision History
Kojto 111:4336505e4b1c 1183 *
Kojto 111:4336505e4b1c 1184 * <table>
Kojto 111:4336505e4b1c 1185 * <tr>
Kojto 111:4336505e4b1c 1186 * <th>Doc. Rev.</td>
Kojto 111:4336505e4b1c 1187 * <th>Date</td>
Kojto 111:4336505e4b1c 1188 * <th>Comments</td>
Kojto 111:4336505e4b1c 1189 * </tr>
Kojto 111:4336505e4b1c 1190 * <tr>
Kojto 111:4336505e4b1c 1191 * <td>E</td>
Kojto 111:4336505e4b1c 1192 * <td>06/2015</td>
Kojto 111:4336505e4b1c 1193 * <td>Added support for SAML21, SAMC21, and SAMDAx.</td>
Kojto 111:4336505e4b1c 1194 * </tr>
Kojto 111:4336505e4b1c 1195 * <tr>
Kojto 111:4336505e4b1c 1196 * <td>D</td>
Kojto 111:4336505e4b1c 1197 * <td>12/2014</td>
Kojto 111:4336505e4b1c 1198 * <td>Added support for SAMR21 and SAMD10/D11.</td>
Kojto 111:4336505e4b1c 1199 * </tr>
Kojto 111:4336505e4b1c 1200 * <tr>
Kojto 111:4336505e4b1c 1201 * <td>C</td>
Kojto 111:4336505e4b1c 1202 * <td>01/2014</td>
Kojto 111:4336505e4b1c 1203 * <td>Added support for SAMD21.</td>
Kojto 111:4336505e4b1c 1204 * </tr>
Kojto 111:4336505e4b1c 1205 * <tr>
Kojto 111:4336505e4b1c 1206 * <td>B</td>
Kojto 111:4336505e4b1c 1207 * <td>06/2013</td>
Kojto 111:4336505e4b1c 1208 * <td>Added additional documentation on the event system. Corrected
Kojto 111:4336505e4b1c 1209 * documentation typos.</td>
Kojto 111:4336505e4b1c 1210 * </tr>
Kojto 111:4336505e4b1c 1211 * <tr>
Kojto 111:4336505e4b1c 1212 * <td>A</td>
Kojto 111:4336505e4b1c 1213 * <td>06/2013</td>
Kojto 111:4336505e4b1c 1214 * <td>Initial release</td>
Kojto 111:4336505e4b1c 1215 * </tr>
Kojto 111:4336505e4b1c 1216 * </table>
Kojto 111:4336505e4b1c 1217 */
Kojto 111:4336505e4b1c 1218
Kojto 111:4336505e4b1c 1219 #endif /* RTC_COUNT_H_INCLUDED */