Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sun May 14 23:18:57 2017 +0000
Revision:
18:6a4db94011d3
Publishing again

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sahilmgandhi 18:6a4db94011d3 1 /**
sahilmgandhi 18:6a4db94011d3 2 * \file
sahilmgandhi 18:6a4db94011d3 3 *
sahilmgandhi 18:6a4db94011d3 4 * \brief Real-Time Clock (RTC) driver for SAM.
sahilmgandhi 18:6a4db94011d3 5 *
sahilmgandhi 18:6a4db94011d3 6 * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.
sahilmgandhi 18:6a4db94011d3 7 *
sahilmgandhi 18:6a4db94011d3 8 * \asf_license_start
sahilmgandhi 18:6a4db94011d3 9 *
sahilmgandhi 18:6a4db94011d3 10 * \page License
sahilmgandhi 18:6a4db94011d3 11 *
sahilmgandhi 18:6a4db94011d3 12 * Redistribution and use in source and binary forms, with or without
sahilmgandhi 18:6a4db94011d3 13 * modification, are permitted provided that the following conditions are met:
sahilmgandhi 18:6a4db94011d3 14 *
sahilmgandhi 18:6a4db94011d3 15 * 1. Redistributions of source code must retain the above copyright notice,
sahilmgandhi 18:6a4db94011d3 16 * this list of conditions and the following disclaimer.
sahilmgandhi 18:6a4db94011d3 17 *
sahilmgandhi 18:6a4db94011d3 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
sahilmgandhi 18:6a4db94011d3 19 * this list of conditions and the following disclaimer in the documentation
sahilmgandhi 18:6a4db94011d3 20 * and/or other materials provided with the distribution.
sahilmgandhi 18:6a4db94011d3 21 *
sahilmgandhi 18:6a4db94011d3 22 * 3. The name of Atmel may not be used to endorse or promote products derived
sahilmgandhi 18:6a4db94011d3 23 * from this software without specific prior written permission.
sahilmgandhi 18:6a4db94011d3 24 *
sahilmgandhi 18:6a4db94011d3 25 * 4. This software may only be redistributed and used in connection with an
sahilmgandhi 18:6a4db94011d3 26 * Atmel microcontroller product.
sahilmgandhi 18:6a4db94011d3 27 *
sahilmgandhi 18:6a4db94011d3 28 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
sahilmgandhi 18:6a4db94011d3 29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
sahilmgandhi 18:6a4db94011d3 30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
sahilmgandhi 18:6a4db94011d3 31 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
sahilmgandhi 18:6a4db94011d3 32 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sahilmgandhi 18:6a4db94011d3 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sahilmgandhi 18:6a4db94011d3 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sahilmgandhi 18:6a4db94011d3 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sahilmgandhi 18:6a4db94011d3 36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
sahilmgandhi 18:6a4db94011d3 37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
sahilmgandhi 18:6a4db94011d3 38 * POSSIBILITY OF SUCH DAMAGE.
sahilmgandhi 18:6a4db94011d3 39 *
sahilmgandhi 18:6a4db94011d3 40 * \asf_license_stop
sahilmgandhi 18:6a4db94011d3 41 *
sahilmgandhi 18:6a4db94011d3 42 */
sahilmgandhi 18:6a4db94011d3 43 /*
sahilmgandhi 18:6a4db94011d3 44 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
sahilmgandhi 18:6a4db94011d3 45 */
sahilmgandhi 18:6a4db94011d3 46
sahilmgandhi 18:6a4db94011d3 47 #include "rtc.h"
sahilmgandhi 18:6a4db94011d3 48
sahilmgandhi 18:6a4db94011d3 49 /// @cond 0
sahilmgandhi 18:6a4db94011d3 50 /**INDENT-OFF**/
sahilmgandhi 18:6a4db94011d3 51 #ifdef __cplusplus
sahilmgandhi 18:6a4db94011d3 52 extern "C" {
sahilmgandhi 18:6a4db94011d3 53 #endif
sahilmgandhi 18:6a4db94011d3 54 /**INDENT-ON**/
sahilmgandhi 18:6a4db94011d3 55 /// @endcond
sahilmgandhi 18:6a4db94011d3 56
sahilmgandhi 18:6a4db94011d3 57 /**
sahilmgandhi 18:6a4db94011d3 58 * \defgroup sam_drivers_rtc_group Real-Time Clock (RTC)
sahilmgandhi 18:6a4db94011d3 59 *
sahilmgandhi 18:6a4db94011d3 60 * See \ref sam_rtc_quickstart.
sahilmgandhi 18:6a4db94011d3 61 *
sahilmgandhi 18:6a4db94011d3 62 * The RTC provides a full binary-coded decimal (BCD) clock that includes
sahilmgandhi 18:6a4db94011d3 63 * century (19/20), year (with leap years), month, date, day, hour, minute
sahilmgandhi 18:6a4db94011d3 64 * and second.
sahilmgandhi 18:6a4db94011d3 65 *
sahilmgandhi 18:6a4db94011d3 66 * @{
sahilmgandhi 18:6a4db94011d3 67 */
sahilmgandhi 18:6a4db94011d3 68
sahilmgandhi 18:6a4db94011d3 69 /* RTC Write Protect Key "RTC" in ASCII */
sahilmgandhi 18:6a4db94011d3 70 #define RTC_WP_KEY (0x525443)
sahilmgandhi 18:6a4db94011d3 71
sahilmgandhi 18:6a4db94011d3 72 /* The BCD code shift value */
sahilmgandhi 18:6a4db94011d3 73 #define BCD_SHIFT 4
sahilmgandhi 18:6a4db94011d3 74
sahilmgandhi 18:6a4db94011d3 75 /* The BCD code mask value */
sahilmgandhi 18:6a4db94011d3 76 #define BCD_MASK 0xfu
sahilmgandhi 18:6a4db94011d3 77
sahilmgandhi 18:6a4db94011d3 78 /* The BCD mul/div factor value */
sahilmgandhi 18:6a4db94011d3 79 #define BCD_FACTOR 10
sahilmgandhi 18:6a4db94011d3 80
sahilmgandhi 18:6a4db94011d3 81 /**
sahilmgandhi 18:6a4db94011d3 82 * \brief Set the RTC hour mode.
sahilmgandhi 18:6a4db94011d3 83 *
sahilmgandhi 18:6a4db94011d3 84 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 85 * \param ul_mode 1 for 12-hour mode, 0 for 24-hour mode.
sahilmgandhi 18:6a4db94011d3 86 */
sahilmgandhi 18:6a4db94011d3 87 void rtc_set_hour_mode(Rtc *p_rtc, uint32_t ul_mode)
sahilmgandhi 18:6a4db94011d3 88 {
sahilmgandhi 18:6a4db94011d3 89 if (ul_mode) {
sahilmgandhi 18:6a4db94011d3 90 p_rtc->RTC_MR |= RTC_MR_HRMOD;
sahilmgandhi 18:6a4db94011d3 91 } else {
sahilmgandhi 18:6a4db94011d3 92 p_rtc->RTC_MR &= (~RTC_MR_HRMOD);
sahilmgandhi 18:6a4db94011d3 93 }
sahilmgandhi 18:6a4db94011d3 94 }
sahilmgandhi 18:6a4db94011d3 95
sahilmgandhi 18:6a4db94011d3 96 /**
sahilmgandhi 18:6a4db94011d3 97 * \brief Get the RTC hour mode.
sahilmgandhi 18:6a4db94011d3 98 *
sahilmgandhi 18:6a4db94011d3 99 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 100 *
sahilmgandhi 18:6a4db94011d3 101 * \return 1 for 12-hour mode, 0 for 24-hour mode.
sahilmgandhi 18:6a4db94011d3 102 */
sahilmgandhi 18:6a4db94011d3 103 uint32_t rtc_get_hour_mode(Rtc *p_rtc)
sahilmgandhi 18:6a4db94011d3 104 {
sahilmgandhi 18:6a4db94011d3 105 uint32_t ul_temp = p_rtc->RTC_MR;
sahilmgandhi 18:6a4db94011d3 106
sahilmgandhi 18:6a4db94011d3 107 if (ul_temp & RTC_MR_HRMOD) {
sahilmgandhi 18:6a4db94011d3 108 return 1;
sahilmgandhi 18:6a4db94011d3 109 } else {
sahilmgandhi 18:6a4db94011d3 110 return 0;
sahilmgandhi 18:6a4db94011d3 111 }
sahilmgandhi 18:6a4db94011d3 112 }
sahilmgandhi 18:6a4db94011d3 113
sahilmgandhi 18:6a4db94011d3 114 /**
sahilmgandhi 18:6a4db94011d3 115 * \brief Enable RTC interrupts.
sahilmgandhi 18:6a4db94011d3 116 *
sahilmgandhi 18:6a4db94011d3 117 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 118 * \param ul_sources Interrupts to be enabled.
sahilmgandhi 18:6a4db94011d3 119 */
sahilmgandhi 18:6a4db94011d3 120 void rtc_enable_interrupt(Rtc *p_rtc, uint32_t ul_sources)
sahilmgandhi 18:6a4db94011d3 121 {
sahilmgandhi 18:6a4db94011d3 122 p_rtc->RTC_IER = ul_sources;
sahilmgandhi 18:6a4db94011d3 123 }
sahilmgandhi 18:6a4db94011d3 124
sahilmgandhi 18:6a4db94011d3 125 /**
sahilmgandhi 18:6a4db94011d3 126 * \brief Disable RTC interrupts.
sahilmgandhi 18:6a4db94011d3 127 *
sahilmgandhi 18:6a4db94011d3 128 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 129 * \param ul_sources Interrupts to be disabled.
sahilmgandhi 18:6a4db94011d3 130 */
sahilmgandhi 18:6a4db94011d3 131 void rtc_disable_interrupt(Rtc *p_rtc, uint32_t ul_sources)
sahilmgandhi 18:6a4db94011d3 132 {
sahilmgandhi 18:6a4db94011d3 133 p_rtc->RTC_IDR = ul_sources;
sahilmgandhi 18:6a4db94011d3 134 }
sahilmgandhi 18:6a4db94011d3 135
sahilmgandhi 18:6a4db94011d3 136 /**
sahilmgandhi 18:6a4db94011d3 137 * \brief Read RTC interrupt mask.
sahilmgandhi 18:6a4db94011d3 138 *
sahilmgandhi 18:6a4db94011d3 139 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 140 *
sahilmgandhi 18:6a4db94011d3 141 * \return The interrupt mask value.
sahilmgandhi 18:6a4db94011d3 142 */
sahilmgandhi 18:6a4db94011d3 143 uint32_t rtc_get_interrupt_mask(Rtc *p_rtc)
sahilmgandhi 18:6a4db94011d3 144 {
sahilmgandhi 18:6a4db94011d3 145 return p_rtc->RTC_IMR;
sahilmgandhi 18:6a4db94011d3 146 }
sahilmgandhi 18:6a4db94011d3 147
sahilmgandhi 18:6a4db94011d3 148 /**
sahilmgandhi 18:6a4db94011d3 149 * \brief Get the RTC time value.
sahilmgandhi 18:6a4db94011d3 150 *
sahilmgandhi 18:6a4db94011d3 151 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 152 * \param pul_hour Current hour, 24-hour mode.
sahilmgandhi 18:6a4db94011d3 153 * \param pul_minute Current minute.
sahilmgandhi 18:6a4db94011d3 154 * \param pul_second Current second.
sahilmgandhi 18:6a4db94011d3 155 */
sahilmgandhi 18:6a4db94011d3 156 void rtc_get_time(Rtc *p_rtc, uint32_t *pul_hour, uint32_t *pul_minute,
sahilmgandhi 18:6a4db94011d3 157 uint32_t *pul_second)
sahilmgandhi 18:6a4db94011d3 158 {
sahilmgandhi 18:6a4db94011d3 159 uint32_t ul_time;
sahilmgandhi 18:6a4db94011d3 160 uint32_t ul_temp;
sahilmgandhi 18:6a4db94011d3 161
sahilmgandhi 18:6a4db94011d3 162 /* Get the current RTC time (multiple reads are necessary to insure a stable value). */
sahilmgandhi 18:6a4db94011d3 163 ul_time = p_rtc->RTC_TIMR;
sahilmgandhi 18:6a4db94011d3 164 while (ul_time != p_rtc->RTC_TIMR) {
sahilmgandhi 18:6a4db94011d3 165 ul_time = p_rtc->RTC_TIMR;
sahilmgandhi 18:6a4db94011d3 166 }
sahilmgandhi 18:6a4db94011d3 167
sahilmgandhi 18:6a4db94011d3 168 /* Hour */
sahilmgandhi 18:6a4db94011d3 169 if (pul_hour) {
sahilmgandhi 18:6a4db94011d3 170 ul_temp = (ul_time & RTC_TIMR_HOUR_Msk) >> RTC_TIMR_HOUR_Pos;
sahilmgandhi 18:6a4db94011d3 171 *pul_hour = (ul_temp >> BCD_SHIFT) * BCD_FACTOR + (ul_temp & BCD_MASK);
sahilmgandhi 18:6a4db94011d3 172
sahilmgandhi 18:6a4db94011d3 173 if ((ul_time & RTC_TIMR_AMPM) == RTC_TIMR_AMPM) {
sahilmgandhi 18:6a4db94011d3 174 *pul_hour += 12;
sahilmgandhi 18:6a4db94011d3 175 }
sahilmgandhi 18:6a4db94011d3 176 }
sahilmgandhi 18:6a4db94011d3 177
sahilmgandhi 18:6a4db94011d3 178 /* Minute */
sahilmgandhi 18:6a4db94011d3 179 if (pul_minute) {
sahilmgandhi 18:6a4db94011d3 180 ul_temp = (ul_time & RTC_TIMR_MIN_Msk) >> RTC_TIMR_MIN_Pos;
sahilmgandhi 18:6a4db94011d3 181 *pul_minute = (ul_temp >> BCD_SHIFT) * BCD_FACTOR + (ul_temp & BCD_MASK);
sahilmgandhi 18:6a4db94011d3 182 }
sahilmgandhi 18:6a4db94011d3 183
sahilmgandhi 18:6a4db94011d3 184 /* Second */
sahilmgandhi 18:6a4db94011d3 185 if (pul_second) {
sahilmgandhi 18:6a4db94011d3 186 ul_temp = (ul_time & RTC_TIMR_SEC_Msk) >> RTC_TIMR_SEC_Pos;
sahilmgandhi 18:6a4db94011d3 187 *pul_second = (ul_temp >> BCD_SHIFT) * BCD_FACTOR + (ul_temp & BCD_MASK);
sahilmgandhi 18:6a4db94011d3 188 }
sahilmgandhi 18:6a4db94011d3 189 }
sahilmgandhi 18:6a4db94011d3 190
sahilmgandhi 18:6a4db94011d3 191 /**
sahilmgandhi 18:6a4db94011d3 192 * \brief Set the RTC time value.
sahilmgandhi 18:6a4db94011d3 193 *
sahilmgandhi 18:6a4db94011d3 194 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 195 * \param ul_hour Current hour, 24-hour mode.
sahilmgandhi 18:6a4db94011d3 196 * \param ul_minute Current minute.
sahilmgandhi 18:6a4db94011d3 197 * \param ul_second Current second.
sahilmgandhi 18:6a4db94011d3 198 *
sahilmgandhi 18:6a4db94011d3 199 * \return 0 for OK, else invalid setting.
sahilmgandhi 18:6a4db94011d3 200 */
sahilmgandhi 18:6a4db94011d3 201 uint32_t rtc_set_time(Rtc *p_rtc, uint32_t ul_hour, uint32_t ul_minute,
sahilmgandhi 18:6a4db94011d3 202 uint32_t ul_second)
sahilmgandhi 18:6a4db94011d3 203 {
sahilmgandhi 18:6a4db94011d3 204 uint32_t ul_time = 0;
sahilmgandhi 18:6a4db94011d3 205
sahilmgandhi 18:6a4db94011d3 206 /* If 12-hour mode, set AMPM bit */
sahilmgandhi 18:6a4db94011d3 207 if ((p_rtc->RTC_MR & RTC_MR_HRMOD) == RTC_MR_HRMOD) {
sahilmgandhi 18:6a4db94011d3 208 if (ul_hour > 12) {
sahilmgandhi 18:6a4db94011d3 209 ul_hour -= 12;
sahilmgandhi 18:6a4db94011d3 210 ul_time |= RTC_TIMR_AMPM;
sahilmgandhi 18:6a4db94011d3 211 }
sahilmgandhi 18:6a4db94011d3 212 }
sahilmgandhi 18:6a4db94011d3 213
sahilmgandhi 18:6a4db94011d3 214 /* Hour */
sahilmgandhi 18:6a4db94011d3 215 ul_time |= ((ul_hour / BCD_FACTOR) << (RTC_TIMR_HOUR_Pos + BCD_SHIFT)) |
sahilmgandhi 18:6a4db94011d3 216 ((ul_hour % BCD_FACTOR) << RTC_TIMR_HOUR_Pos);
sahilmgandhi 18:6a4db94011d3 217
sahilmgandhi 18:6a4db94011d3 218 /* Minute */
sahilmgandhi 18:6a4db94011d3 219 ul_time |= ((ul_minute / BCD_FACTOR) << (RTC_TIMR_MIN_Pos + BCD_SHIFT)) |
sahilmgandhi 18:6a4db94011d3 220 ((ul_minute % BCD_FACTOR) << RTC_TIMR_MIN_Pos);
sahilmgandhi 18:6a4db94011d3 221
sahilmgandhi 18:6a4db94011d3 222 /* Second */
sahilmgandhi 18:6a4db94011d3 223 ul_time |= ((ul_second / BCD_FACTOR) << (RTC_TIMR_SEC_Pos + BCD_SHIFT)) |
sahilmgandhi 18:6a4db94011d3 224 ((ul_second % BCD_FACTOR) << RTC_TIMR_SEC_Pos);
sahilmgandhi 18:6a4db94011d3 225
sahilmgandhi 18:6a4db94011d3 226 /* Update time register. Check the spec for the flow. */
sahilmgandhi 18:6a4db94011d3 227 p_rtc->RTC_CR |= RTC_CR_UPDTIM;
sahilmgandhi 18:6a4db94011d3 228 while ((p_rtc->RTC_SR & RTC_SR_ACKUPD) != RTC_SR_ACKUPD);
sahilmgandhi 18:6a4db94011d3 229 p_rtc->RTC_SCCR = RTC_SCCR_ACKCLR;
sahilmgandhi 18:6a4db94011d3 230 p_rtc->RTC_TIMR = ul_time;
sahilmgandhi 18:6a4db94011d3 231 p_rtc->RTC_CR &= (~RTC_CR_UPDTIM);
sahilmgandhi 18:6a4db94011d3 232 p_rtc->RTC_SCCR |= RTC_SCCR_SECCLR;
sahilmgandhi 18:6a4db94011d3 233
sahilmgandhi 18:6a4db94011d3 234 return (p_rtc->RTC_VER & RTC_VER_NVTIM);
sahilmgandhi 18:6a4db94011d3 235 }
sahilmgandhi 18:6a4db94011d3 236
sahilmgandhi 18:6a4db94011d3 237 /**
sahilmgandhi 18:6a4db94011d3 238 * \brief Set the RTC alarm time value.
sahilmgandhi 18:6a4db94011d3 239 *
sahilmgandhi 18:6a4db94011d3 240 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 241 * \param ul_hour_flag 1 for setting, 0 for not setting.
sahilmgandhi 18:6a4db94011d3 242 * \param ul_hour Alarm hour value, 24-hour mode.
sahilmgandhi 18:6a4db94011d3 243 * \param ul_minute_flag 1 for setting, 0 for not setting.
sahilmgandhi 18:6a4db94011d3 244 * \param ul_minute Alarm minute value.
sahilmgandhi 18:6a4db94011d3 245 * \param ul_second_flag 1 for setting, 0 for not setting.
sahilmgandhi 18:6a4db94011d3 246 * \param ul_second Alarm second value.
sahilmgandhi 18:6a4db94011d3 247 *
sahilmgandhi 18:6a4db94011d3 248 * \return 0 for OK, else invalid setting.
sahilmgandhi 18:6a4db94011d3 249 */
sahilmgandhi 18:6a4db94011d3 250 uint32_t rtc_set_time_alarm(Rtc *p_rtc,
sahilmgandhi 18:6a4db94011d3 251 uint32_t ul_hour_flag, uint32_t ul_hour,
sahilmgandhi 18:6a4db94011d3 252 uint32_t ul_minute_flag, uint32_t ul_minute,
sahilmgandhi 18:6a4db94011d3 253 uint32_t ul_second_flag, uint32_t ul_second)
sahilmgandhi 18:6a4db94011d3 254 {
sahilmgandhi 18:6a4db94011d3 255 uint32_t ul_alarm = 0;
sahilmgandhi 18:6a4db94011d3 256
sahilmgandhi 18:6a4db94011d3 257 /* Hour alarm setting */
sahilmgandhi 18:6a4db94011d3 258 if (ul_hour_flag) {
sahilmgandhi 18:6a4db94011d3 259 /* If 12-hour mode, set AMPM bit */
sahilmgandhi 18:6a4db94011d3 260 if ((p_rtc->RTC_MR & RTC_MR_HRMOD) == RTC_MR_HRMOD) {
sahilmgandhi 18:6a4db94011d3 261 if (ul_hour > 12) {
sahilmgandhi 18:6a4db94011d3 262 ul_hour -= 12;
sahilmgandhi 18:6a4db94011d3 263 ul_alarm |= RTC_TIMR_AMPM;
sahilmgandhi 18:6a4db94011d3 264 }
sahilmgandhi 18:6a4db94011d3 265 }
sahilmgandhi 18:6a4db94011d3 266
sahilmgandhi 18:6a4db94011d3 267 ul_alarm |= ((ul_hour / BCD_FACTOR) << (RTC_TIMR_HOUR_Pos + BCD_SHIFT)) |
sahilmgandhi 18:6a4db94011d3 268 ((ul_hour % BCD_FACTOR) << RTC_TIMR_HOUR_Pos);
sahilmgandhi 18:6a4db94011d3 269 }
sahilmgandhi 18:6a4db94011d3 270
sahilmgandhi 18:6a4db94011d3 271 /* Minute alarm setting */
sahilmgandhi 18:6a4db94011d3 272 if (ul_minute_flag) {
sahilmgandhi 18:6a4db94011d3 273 ul_alarm |= ((ul_minute / BCD_FACTOR) << (RTC_TIMR_MIN_Pos + BCD_SHIFT)) |
sahilmgandhi 18:6a4db94011d3 274 ((ul_minute % BCD_FACTOR) << RTC_TIMR_MIN_Pos);
sahilmgandhi 18:6a4db94011d3 275 }
sahilmgandhi 18:6a4db94011d3 276
sahilmgandhi 18:6a4db94011d3 277 /* Second alarm setting */
sahilmgandhi 18:6a4db94011d3 278 if (ul_second_flag) {
sahilmgandhi 18:6a4db94011d3 279 ul_alarm |= ((ul_second / BCD_FACTOR) << (RTC_TIMR_SEC_Pos + BCD_SHIFT)) |
sahilmgandhi 18:6a4db94011d3 280 ((ul_second % BCD_FACTOR) << RTC_TIMR_SEC_Pos);
sahilmgandhi 18:6a4db94011d3 281 }
sahilmgandhi 18:6a4db94011d3 282
sahilmgandhi 18:6a4db94011d3 283 p_rtc->RTC_TIMALR &= ~(RTC_TIMALR_SECEN | RTC_TIMALR_MINEN | RTC_TIMALR_HOUREN);
sahilmgandhi 18:6a4db94011d3 284 p_rtc->RTC_TIMALR = ul_alarm;
sahilmgandhi 18:6a4db94011d3 285 p_rtc->RTC_TIMALR |= (RTC_TIMALR_SECEN | RTC_TIMALR_MINEN | RTC_TIMALR_HOUREN);
sahilmgandhi 18:6a4db94011d3 286
sahilmgandhi 18:6a4db94011d3 287 return (p_rtc->RTC_VER & RTC_VER_NVTIMALR);
sahilmgandhi 18:6a4db94011d3 288 }
sahilmgandhi 18:6a4db94011d3 289
sahilmgandhi 18:6a4db94011d3 290 /**
sahilmgandhi 18:6a4db94011d3 291 * \brief Get the RTC date value.
sahilmgandhi 18:6a4db94011d3 292 *
sahilmgandhi 18:6a4db94011d3 293 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 294 * \param pul_year Current year.
sahilmgandhi 18:6a4db94011d3 295 * \param pul_month Current month.
sahilmgandhi 18:6a4db94011d3 296 * \param pul_day Current day.
sahilmgandhi 18:6a4db94011d3 297 * \param pul_week Current day in current week.
sahilmgandhi 18:6a4db94011d3 298 */
sahilmgandhi 18:6a4db94011d3 299 void rtc_get_date(Rtc *p_rtc, uint32_t *pul_year, uint32_t *pul_month,
sahilmgandhi 18:6a4db94011d3 300 uint32_t *pul_day, uint32_t *pul_week)
sahilmgandhi 18:6a4db94011d3 301 {
sahilmgandhi 18:6a4db94011d3 302 uint32_t ul_date;
sahilmgandhi 18:6a4db94011d3 303 uint32_t ul_cent;
sahilmgandhi 18:6a4db94011d3 304 uint32_t ul_temp;
sahilmgandhi 18:6a4db94011d3 305
sahilmgandhi 18:6a4db94011d3 306 /* Get the current date (multiple reads are necessary to insure a stable value). */
sahilmgandhi 18:6a4db94011d3 307 ul_date = p_rtc->RTC_CALR;
sahilmgandhi 18:6a4db94011d3 308 while (ul_date != p_rtc->RTC_CALR) {
sahilmgandhi 18:6a4db94011d3 309 ul_date = p_rtc->RTC_CALR;
sahilmgandhi 18:6a4db94011d3 310 }
sahilmgandhi 18:6a4db94011d3 311
sahilmgandhi 18:6a4db94011d3 312 /* Retrieve year */
sahilmgandhi 18:6a4db94011d3 313 if (pul_year) {
sahilmgandhi 18:6a4db94011d3 314 ul_temp = (ul_date & RTC_CALR_CENT_Msk) >> RTC_CALR_CENT_Pos;
sahilmgandhi 18:6a4db94011d3 315 ul_cent = (ul_temp >> BCD_SHIFT) * BCD_FACTOR + (ul_temp & BCD_MASK);
sahilmgandhi 18:6a4db94011d3 316 ul_temp = (ul_date & RTC_CALR_YEAR_Msk) >> RTC_CALR_YEAR_Pos;
sahilmgandhi 18:6a4db94011d3 317 *pul_year = (ul_cent * BCD_FACTOR * BCD_FACTOR) +
sahilmgandhi 18:6a4db94011d3 318 (ul_temp >> BCD_SHIFT) * BCD_FACTOR + (ul_temp & BCD_MASK);
sahilmgandhi 18:6a4db94011d3 319 }
sahilmgandhi 18:6a4db94011d3 320
sahilmgandhi 18:6a4db94011d3 321 /* Retrieve month */
sahilmgandhi 18:6a4db94011d3 322 if (pul_month) {
sahilmgandhi 18:6a4db94011d3 323 ul_temp = (ul_date & RTC_CALR_MONTH_Msk) >> RTC_CALR_MONTH_Pos;
sahilmgandhi 18:6a4db94011d3 324 *pul_month = (ul_temp >> BCD_SHIFT) * BCD_FACTOR + (ul_temp & BCD_MASK);
sahilmgandhi 18:6a4db94011d3 325 }
sahilmgandhi 18:6a4db94011d3 326
sahilmgandhi 18:6a4db94011d3 327 /* Retrieve day */
sahilmgandhi 18:6a4db94011d3 328 if (pul_day) {
sahilmgandhi 18:6a4db94011d3 329 ul_temp = (ul_date & RTC_CALR_DATE_Msk) >> RTC_CALR_DATE_Pos;
sahilmgandhi 18:6a4db94011d3 330 *pul_day = (ul_temp >> BCD_SHIFT) * BCD_FACTOR + (ul_temp & BCD_MASK);
sahilmgandhi 18:6a4db94011d3 331 }
sahilmgandhi 18:6a4db94011d3 332
sahilmgandhi 18:6a4db94011d3 333 /* Retrieve week */
sahilmgandhi 18:6a4db94011d3 334 if (pul_week) {
sahilmgandhi 18:6a4db94011d3 335 *pul_week = ((ul_date & RTC_CALR_DAY_Msk) >> RTC_CALR_DAY_Pos);
sahilmgandhi 18:6a4db94011d3 336 }
sahilmgandhi 18:6a4db94011d3 337 }
sahilmgandhi 18:6a4db94011d3 338
sahilmgandhi 18:6a4db94011d3 339 /**
sahilmgandhi 18:6a4db94011d3 340 * \brief Set the RTC date.
sahilmgandhi 18:6a4db94011d3 341 *
sahilmgandhi 18:6a4db94011d3 342 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 343 * \param ul_year Current year.
sahilmgandhi 18:6a4db94011d3 344 * \param ul_month Current month.
sahilmgandhi 18:6a4db94011d3 345 * \param ul_day Current day.
sahilmgandhi 18:6a4db94011d3 346 * \param ul_week Current day in current week.
sahilmgandhi 18:6a4db94011d3 347 *
sahilmgandhi 18:6a4db94011d3 348 * \return 0 for OK, else invalid setting.
sahilmgandhi 18:6a4db94011d3 349 */
sahilmgandhi 18:6a4db94011d3 350 uint32_t rtc_set_date(Rtc *p_rtc, uint32_t ul_year, uint32_t ul_month,
sahilmgandhi 18:6a4db94011d3 351 uint32_t ul_day, uint32_t ul_week)
sahilmgandhi 18:6a4db94011d3 352 {
sahilmgandhi 18:6a4db94011d3 353 uint32_t ul_date = 0;
sahilmgandhi 18:6a4db94011d3 354
sahilmgandhi 18:6a4db94011d3 355 /* Cent */
sahilmgandhi 18:6a4db94011d3 356 ul_date |= ((ul_year / BCD_FACTOR / BCD_FACTOR / BCD_FACTOR) <<
sahilmgandhi 18:6a4db94011d3 357 (RTC_CALR_CENT_Pos + BCD_SHIFT) |
sahilmgandhi 18:6a4db94011d3 358 ((ul_year / BCD_FACTOR / BCD_FACTOR) % BCD_FACTOR) << RTC_CALR_CENT_Pos);
sahilmgandhi 18:6a4db94011d3 359
sahilmgandhi 18:6a4db94011d3 360 /* Year */
sahilmgandhi 18:6a4db94011d3 361 ul_date |= (((ul_year / BCD_FACTOR) % BCD_FACTOR) <<
sahilmgandhi 18:6a4db94011d3 362 (RTC_CALR_YEAR_Pos + BCD_SHIFT)) |
sahilmgandhi 18:6a4db94011d3 363 ((ul_year % BCD_FACTOR) << RTC_CALR_YEAR_Pos);
sahilmgandhi 18:6a4db94011d3 364
sahilmgandhi 18:6a4db94011d3 365 /* Month */
sahilmgandhi 18:6a4db94011d3 366 ul_date |= ((ul_month / BCD_FACTOR) << (RTC_CALR_MONTH_Pos + BCD_SHIFT)) |
sahilmgandhi 18:6a4db94011d3 367 ((ul_month % BCD_FACTOR) << RTC_CALR_MONTH_Pos);
sahilmgandhi 18:6a4db94011d3 368
sahilmgandhi 18:6a4db94011d3 369 /* Week */
sahilmgandhi 18:6a4db94011d3 370 ul_date |= (ul_week << RTC_CALR_DAY_Pos);
sahilmgandhi 18:6a4db94011d3 371
sahilmgandhi 18:6a4db94011d3 372 /* Day */
sahilmgandhi 18:6a4db94011d3 373 ul_date |= ((ul_day / BCD_FACTOR) << (RTC_CALR_DATE_Pos + BCD_SHIFT)) |
sahilmgandhi 18:6a4db94011d3 374 ((ul_day % BCD_FACTOR) << RTC_CALR_DATE_Pos);
sahilmgandhi 18:6a4db94011d3 375
sahilmgandhi 18:6a4db94011d3 376 /* Update calendar register. Check the spec for the flow. */
sahilmgandhi 18:6a4db94011d3 377 p_rtc->RTC_CR |= RTC_CR_UPDCAL;
sahilmgandhi 18:6a4db94011d3 378 while ((p_rtc->RTC_SR & RTC_SR_ACKUPD) != RTC_SR_ACKUPD);
sahilmgandhi 18:6a4db94011d3 379
sahilmgandhi 18:6a4db94011d3 380 p_rtc->RTC_SCCR = RTC_SCCR_ACKCLR;
sahilmgandhi 18:6a4db94011d3 381 p_rtc->RTC_CALR = ul_date;
sahilmgandhi 18:6a4db94011d3 382 p_rtc->RTC_CR &= (~RTC_CR_UPDCAL);
sahilmgandhi 18:6a4db94011d3 383 /* Clear SECENV in SCCR */
sahilmgandhi 18:6a4db94011d3 384 p_rtc->RTC_SCCR |= RTC_SCCR_SECCLR;
sahilmgandhi 18:6a4db94011d3 385
sahilmgandhi 18:6a4db94011d3 386 return (p_rtc->RTC_VER & RTC_VER_NVCAL);
sahilmgandhi 18:6a4db94011d3 387 }
sahilmgandhi 18:6a4db94011d3 388
sahilmgandhi 18:6a4db94011d3 389 /**
sahilmgandhi 18:6a4db94011d3 390 * \brief Set the RTC alarm date value.
sahilmgandhi 18:6a4db94011d3 391 *
sahilmgandhi 18:6a4db94011d3 392 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 393 * \param ul_month_flag 1 for setting, 0 for not setting.
sahilmgandhi 18:6a4db94011d3 394 * \param ul_month Alarm month value.
sahilmgandhi 18:6a4db94011d3 395 * \param ul_day_flag 1 for setting, 0 for not setting.
sahilmgandhi 18:6a4db94011d3 396 * \param ul_day Alarm day value.
sahilmgandhi 18:6a4db94011d3 397 *
sahilmgandhi 18:6a4db94011d3 398 * \return 0 for OK, else invalid setting.
sahilmgandhi 18:6a4db94011d3 399 */
sahilmgandhi 18:6a4db94011d3 400 uint32_t rtc_set_date_alarm(Rtc *p_rtc,
sahilmgandhi 18:6a4db94011d3 401 uint32_t ul_month_flag, uint32_t ul_month,
sahilmgandhi 18:6a4db94011d3 402 uint32_t ul_day_flag, uint32_t ul_day)
sahilmgandhi 18:6a4db94011d3 403 {
sahilmgandhi 18:6a4db94011d3 404 uint32_t ul_alarm = 0;
sahilmgandhi 18:6a4db94011d3 405
sahilmgandhi 18:6a4db94011d3 406 /* Month alarm setting */
sahilmgandhi 18:6a4db94011d3 407 if (ul_month_flag) {
sahilmgandhi 18:6a4db94011d3 408 ul_alarm |= ((ul_month / BCD_FACTOR) << (RTC_CALR_MONTH_Pos + BCD_SHIFT)) |
sahilmgandhi 18:6a4db94011d3 409 ((ul_month % BCD_FACTOR) << RTC_CALR_MONTH_Pos);
sahilmgandhi 18:6a4db94011d3 410 }
sahilmgandhi 18:6a4db94011d3 411
sahilmgandhi 18:6a4db94011d3 412 /* Day alarm setting */
sahilmgandhi 18:6a4db94011d3 413 if (ul_day_flag) {
sahilmgandhi 18:6a4db94011d3 414 ul_alarm |= ((ul_day / BCD_FACTOR) << (RTC_CALR_DATE_Pos + BCD_SHIFT)) |
sahilmgandhi 18:6a4db94011d3 415 ((ul_day % BCD_FACTOR) << RTC_CALR_DATE_Pos);
sahilmgandhi 18:6a4db94011d3 416 }
sahilmgandhi 18:6a4db94011d3 417
sahilmgandhi 18:6a4db94011d3 418 /* Set alarm */
sahilmgandhi 18:6a4db94011d3 419 p_rtc->RTC_CALALR &= ~(RTC_CALALR_MTHEN | RTC_CALALR_DATEEN);
sahilmgandhi 18:6a4db94011d3 420 p_rtc->RTC_CALALR = ul_alarm;
sahilmgandhi 18:6a4db94011d3 421 p_rtc->RTC_CALALR |= (RTC_CALALR_MTHEN | RTC_CALALR_DATEEN);
sahilmgandhi 18:6a4db94011d3 422
sahilmgandhi 18:6a4db94011d3 423 return (p_rtc->RTC_VER & RTC_VER_NVCALALR);
sahilmgandhi 18:6a4db94011d3 424 }
sahilmgandhi 18:6a4db94011d3 425
sahilmgandhi 18:6a4db94011d3 426 /**
sahilmgandhi 18:6a4db94011d3 427 * \brief Clear the RTC time alarm setting.
sahilmgandhi 18:6a4db94011d3 428 *
sahilmgandhi 18:6a4db94011d3 429 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 430 */
sahilmgandhi 18:6a4db94011d3 431 void rtc_clear_time_alarm(Rtc *p_rtc)
sahilmgandhi 18:6a4db94011d3 432 {
sahilmgandhi 18:6a4db94011d3 433 p_rtc->RTC_TIMALR = 0;
sahilmgandhi 18:6a4db94011d3 434 }
sahilmgandhi 18:6a4db94011d3 435
sahilmgandhi 18:6a4db94011d3 436 /**
sahilmgandhi 18:6a4db94011d3 437 * \brief Clear the RTC date alarm setting.
sahilmgandhi 18:6a4db94011d3 438 *
sahilmgandhi 18:6a4db94011d3 439 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 440 */
sahilmgandhi 18:6a4db94011d3 441 void rtc_clear_date_alarm(Rtc *p_rtc)
sahilmgandhi 18:6a4db94011d3 442 {
sahilmgandhi 18:6a4db94011d3 443 /* Need a valid value without enabling */
sahilmgandhi 18:6a4db94011d3 444 p_rtc->RTC_CALALR = RTC_CALALR_MONTH(0x01) | RTC_CALALR_DATE(0x01);
sahilmgandhi 18:6a4db94011d3 445 }
sahilmgandhi 18:6a4db94011d3 446
sahilmgandhi 18:6a4db94011d3 447 /**
sahilmgandhi 18:6a4db94011d3 448 * \brief Get the RTC status.
sahilmgandhi 18:6a4db94011d3 449 *
sahilmgandhi 18:6a4db94011d3 450 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 451 *
sahilmgandhi 18:6a4db94011d3 452 * \return Status of the RTC.
sahilmgandhi 18:6a4db94011d3 453 */
sahilmgandhi 18:6a4db94011d3 454 uint32_t rtc_get_status(Rtc *p_rtc)
sahilmgandhi 18:6a4db94011d3 455 {
sahilmgandhi 18:6a4db94011d3 456 return (p_rtc->RTC_SR);
sahilmgandhi 18:6a4db94011d3 457 }
sahilmgandhi 18:6a4db94011d3 458
sahilmgandhi 18:6a4db94011d3 459 /**
sahilmgandhi 18:6a4db94011d3 460 * \brief Set the RTC SCCR to clear status bits.
sahilmgandhi 18:6a4db94011d3 461 *
sahilmgandhi 18:6a4db94011d3 462 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 463 * \param ul_clear Some flag bits which will be cleared.
sahilmgandhi 18:6a4db94011d3 464 */
sahilmgandhi 18:6a4db94011d3 465 void rtc_clear_status(Rtc *p_rtc, uint32_t ul_clear)
sahilmgandhi 18:6a4db94011d3 466 {
sahilmgandhi 18:6a4db94011d3 467 p_rtc->RTC_SCCR = ul_clear;
sahilmgandhi 18:6a4db94011d3 468 }
sahilmgandhi 18:6a4db94011d3 469
sahilmgandhi 18:6a4db94011d3 470 /**
sahilmgandhi 18:6a4db94011d3 471 * \brief Get the RTC valid entry.
sahilmgandhi 18:6a4db94011d3 472 *
sahilmgandhi 18:6a4db94011d3 473 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 474 *
sahilmgandhi 18:6a4db94011d3 475 * \return 0 for no invalid data, else has contained invalid data.
sahilmgandhi 18:6a4db94011d3 476 */
sahilmgandhi 18:6a4db94011d3 477 uint32_t rtc_get_valid_entry(Rtc *p_rtc)
sahilmgandhi 18:6a4db94011d3 478 {
sahilmgandhi 18:6a4db94011d3 479 return (p_rtc->RTC_VER);
sahilmgandhi 18:6a4db94011d3 480 }
sahilmgandhi 18:6a4db94011d3 481
sahilmgandhi 18:6a4db94011d3 482 /**
sahilmgandhi 18:6a4db94011d3 483 * \brief Set the RTC time event selection.
sahilmgandhi 18:6a4db94011d3 484 *
sahilmgandhi 18:6a4db94011d3 485 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 486 * \param ul_selection Time event selection to be enabled.
sahilmgandhi 18:6a4db94011d3 487 */
sahilmgandhi 18:6a4db94011d3 488 void rtc_set_time_event(Rtc *p_rtc, uint32_t ul_selection)
sahilmgandhi 18:6a4db94011d3 489 {
sahilmgandhi 18:6a4db94011d3 490 p_rtc->RTC_CR &= ~RTC_CR_TIMEVSEL_Msk;
sahilmgandhi 18:6a4db94011d3 491 p_rtc->RTC_CR |= (ul_selection << RTC_CR_TIMEVSEL_Pos) & RTC_CR_TIMEVSEL_Msk;
sahilmgandhi 18:6a4db94011d3 492 }
sahilmgandhi 18:6a4db94011d3 493
sahilmgandhi 18:6a4db94011d3 494 /**
sahilmgandhi 18:6a4db94011d3 495 * \brief Set the RTC calendar event selection.
sahilmgandhi 18:6a4db94011d3 496 *
sahilmgandhi 18:6a4db94011d3 497 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 498 * \param ul_selection Calendar event selection to be enabled..
sahilmgandhi 18:6a4db94011d3 499 */
sahilmgandhi 18:6a4db94011d3 500 void rtc_set_calendar_event(Rtc *p_rtc, uint32_t ul_selection)
sahilmgandhi 18:6a4db94011d3 501 {
sahilmgandhi 18:6a4db94011d3 502 p_rtc->RTC_CR &= ~RTC_CR_CALEVSEL_Msk;
sahilmgandhi 18:6a4db94011d3 503 p_rtc->RTC_CR |= (ul_selection << RTC_CR_CALEVSEL_Pos) & RTC_CR_CALEVSEL_Msk;
sahilmgandhi 18:6a4db94011d3 504 }
sahilmgandhi 18:6a4db94011d3 505
sahilmgandhi 18:6a4db94011d3 506 #if ((SAM3S8) || (SAM3SD8) || (SAM4S) || (SAM4N) || (SAM4C) || (SAMG) || (SAM4CP) || (SAM4CM))
sahilmgandhi 18:6a4db94011d3 507 /**
sahilmgandhi 18:6a4db94011d3 508 * \brief Set the RTC calendar mode.
sahilmgandhi 18:6a4db94011d3 509 *
sahilmgandhi 18:6a4db94011d3 510 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 511 * \param ul_mode 1 for Persian mode,0 for Gregorian mode.
sahilmgandhi 18:6a4db94011d3 512 */
sahilmgandhi 18:6a4db94011d3 513 void rtc_set_calendar_mode(Rtc *p_rtc, uint32_t ul_mode)
sahilmgandhi 18:6a4db94011d3 514 {
sahilmgandhi 18:6a4db94011d3 515 if (ul_mode) {
sahilmgandhi 18:6a4db94011d3 516 p_rtc->RTC_MR |= RTC_MR_PERSIAN;
sahilmgandhi 18:6a4db94011d3 517 } else {
sahilmgandhi 18:6a4db94011d3 518 p_rtc->RTC_MR &= (~RTC_MR_PERSIAN);
sahilmgandhi 18:6a4db94011d3 519 }
sahilmgandhi 18:6a4db94011d3 520 }
sahilmgandhi 18:6a4db94011d3 521
sahilmgandhi 18:6a4db94011d3 522 /**
sahilmgandhi 18:6a4db94011d3 523 * \brief Get the RTC calendar mode.
sahilmgandhi 18:6a4db94011d3 524 *
sahilmgandhi 18:6a4db94011d3 525 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 526 *
sahilmgandhi 18:6a4db94011d3 527 * \return 1 for Persian calendar, 0 for Gregorian calendar.
sahilmgandhi 18:6a4db94011d3 528 */
sahilmgandhi 18:6a4db94011d3 529 uint32_t rtc_get_calendar_mode(Rtc *p_rtc)
sahilmgandhi 18:6a4db94011d3 530 {
sahilmgandhi 18:6a4db94011d3 531 uint32_t ul_temp = p_rtc->RTC_MR;
sahilmgandhi 18:6a4db94011d3 532
sahilmgandhi 18:6a4db94011d3 533 if (ul_temp & RTC_MR_PERSIAN) {
sahilmgandhi 18:6a4db94011d3 534 return 1;
sahilmgandhi 18:6a4db94011d3 535 } else {
sahilmgandhi 18:6a4db94011d3 536 return 0;
sahilmgandhi 18:6a4db94011d3 537 }
sahilmgandhi 18:6a4db94011d3 538 }
sahilmgandhi 18:6a4db94011d3 539
sahilmgandhi 18:6a4db94011d3 540 /**
sahilmgandhi 18:6a4db94011d3 541 * \brief Set the RTC calibration.
sahilmgandhi 18:6a4db94011d3 542 *
sahilmgandhi 18:6a4db94011d3 543 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 544 * \param ul_direction_ppm Positive/negative correction.
sahilmgandhi 18:6a4db94011d3 545 * \param ul_correction Correction value.
sahilmgandhi 18:6a4db94011d3 546 * \param ul_range_ppm Low/high range correction.
sahilmgandhi 18:6a4db94011d3 547 */
sahilmgandhi 18:6a4db94011d3 548 void rtc_set_calibration(Rtc *p_rtc, uint32_t ul_direction_ppm,
sahilmgandhi 18:6a4db94011d3 549 uint32_t ul_correction, uint32_t ul_range_ppm)
sahilmgandhi 18:6a4db94011d3 550 {
sahilmgandhi 18:6a4db94011d3 551 uint32_t ul_temp;
sahilmgandhi 18:6a4db94011d3 552
sahilmgandhi 18:6a4db94011d3 553 ul_temp = p_rtc->RTC_MR;
sahilmgandhi 18:6a4db94011d3 554
sahilmgandhi 18:6a4db94011d3 555 if (ul_direction_ppm) {
sahilmgandhi 18:6a4db94011d3 556 ul_temp |= RTC_MR_NEGPPM;
sahilmgandhi 18:6a4db94011d3 557 } else {
sahilmgandhi 18:6a4db94011d3 558 ul_temp &= (~RTC_MR_NEGPPM);
sahilmgandhi 18:6a4db94011d3 559 }
sahilmgandhi 18:6a4db94011d3 560
sahilmgandhi 18:6a4db94011d3 561 ul_temp |= RTC_MR_CORRECTION(ul_correction);
sahilmgandhi 18:6a4db94011d3 562
sahilmgandhi 18:6a4db94011d3 563 if (ul_range_ppm) {
sahilmgandhi 18:6a4db94011d3 564 ul_temp |= RTC_MR_HIGHPPM;
sahilmgandhi 18:6a4db94011d3 565 } else {
sahilmgandhi 18:6a4db94011d3 566 ul_temp &= (~RTC_MR_HIGHPPM);
sahilmgandhi 18:6a4db94011d3 567 }
sahilmgandhi 18:6a4db94011d3 568
sahilmgandhi 18:6a4db94011d3 569 p_rtc->RTC_MR = ul_temp;
sahilmgandhi 18:6a4db94011d3 570 }
sahilmgandhi 18:6a4db94011d3 571 #endif
sahilmgandhi 18:6a4db94011d3 572
sahilmgandhi 18:6a4db94011d3 573 #if ((SAM3S8) || (SAM3SD8) || (SAM4S) || (SAM4C) || (SAMG) || (SAM4CP) || (SAM4CM) || SAMV71 || SAMV70 || SAME70 || SAMS70)
sahilmgandhi 18:6a4db94011d3 574 /**
sahilmgandhi 18:6a4db94011d3 575 * \brief Set the RTC output waveform.
sahilmgandhi 18:6a4db94011d3 576 *
sahilmgandhi 18:6a4db94011d3 577 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 578 * \param ul_channel Output channel selection.
sahilmgandhi 18:6a4db94011d3 579 * \param ul_value Output source selection value.
sahilmgandhi 18:6a4db94011d3 580 */
sahilmgandhi 18:6a4db94011d3 581 void rtc_set_waveform(Rtc *p_rtc, uint32_t ul_channel, uint32_t ul_value)
sahilmgandhi 18:6a4db94011d3 582 {
sahilmgandhi 18:6a4db94011d3 583 if (ul_channel == 0) {
sahilmgandhi 18:6a4db94011d3 584 switch (ul_value) {
sahilmgandhi 18:6a4db94011d3 585 case 0:
sahilmgandhi 18:6a4db94011d3 586 p_rtc->RTC_MR &= ~RTC_MR_OUT0_Msk;
sahilmgandhi 18:6a4db94011d3 587 p_rtc->RTC_MR |= RTC_MR_OUT0_NO_WAVE;
sahilmgandhi 18:6a4db94011d3 588 break;
sahilmgandhi 18:6a4db94011d3 589
sahilmgandhi 18:6a4db94011d3 590 case 1:
sahilmgandhi 18:6a4db94011d3 591 p_rtc->RTC_MR &= ~RTC_MR_OUT0_Msk;
sahilmgandhi 18:6a4db94011d3 592 p_rtc->RTC_MR |= RTC_MR_OUT0_FREQ1HZ;
sahilmgandhi 18:6a4db94011d3 593 break;
sahilmgandhi 18:6a4db94011d3 594
sahilmgandhi 18:6a4db94011d3 595 case 2:
sahilmgandhi 18:6a4db94011d3 596 p_rtc->RTC_MR &= ~RTC_MR_OUT0_Msk;
sahilmgandhi 18:6a4db94011d3 597 p_rtc->RTC_MR |= RTC_MR_OUT0_FREQ32HZ;
sahilmgandhi 18:6a4db94011d3 598 break;
sahilmgandhi 18:6a4db94011d3 599
sahilmgandhi 18:6a4db94011d3 600 case 3:
sahilmgandhi 18:6a4db94011d3 601 p_rtc->RTC_MR &= ~RTC_MR_OUT0_Msk;
sahilmgandhi 18:6a4db94011d3 602 p_rtc->RTC_MR |= RTC_MR_OUT0_FREQ64HZ;
sahilmgandhi 18:6a4db94011d3 603 break;
sahilmgandhi 18:6a4db94011d3 604
sahilmgandhi 18:6a4db94011d3 605 case 4:
sahilmgandhi 18:6a4db94011d3 606 p_rtc->RTC_MR &= ~RTC_MR_OUT0_Msk;
sahilmgandhi 18:6a4db94011d3 607 p_rtc->RTC_MR |= RTC_MR_OUT0_FREQ512HZ;
sahilmgandhi 18:6a4db94011d3 608 break;
sahilmgandhi 18:6a4db94011d3 609
sahilmgandhi 18:6a4db94011d3 610 #if (!SAMG)
sahilmgandhi 18:6a4db94011d3 611 case 5:
sahilmgandhi 18:6a4db94011d3 612 p_rtc->RTC_MR &= ~RTC_MR_OUT0_Msk;
sahilmgandhi 18:6a4db94011d3 613 p_rtc->RTC_MR |= RTC_MR_OUT0_ALARM_TOGGLE;
sahilmgandhi 18:6a4db94011d3 614 break;
sahilmgandhi 18:6a4db94011d3 615 #endif
sahilmgandhi 18:6a4db94011d3 616
sahilmgandhi 18:6a4db94011d3 617 case 6:
sahilmgandhi 18:6a4db94011d3 618 p_rtc->RTC_MR &= ~RTC_MR_OUT0_Msk;
sahilmgandhi 18:6a4db94011d3 619 p_rtc->RTC_MR |= RTC_MR_OUT0_ALARM_FLAG;
sahilmgandhi 18:6a4db94011d3 620 break;
sahilmgandhi 18:6a4db94011d3 621
sahilmgandhi 18:6a4db94011d3 622 #if (!SAMG)
sahilmgandhi 18:6a4db94011d3 623 case 7:
sahilmgandhi 18:6a4db94011d3 624 p_rtc->RTC_MR &= ~RTC_MR_OUT0_Msk;
sahilmgandhi 18:6a4db94011d3 625 p_rtc->RTC_MR |= RTC_MR_OUT0_PROG_PULSE;
sahilmgandhi 18:6a4db94011d3 626 break;
sahilmgandhi 18:6a4db94011d3 627 #endif
sahilmgandhi 18:6a4db94011d3 628
sahilmgandhi 18:6a4db94011d3 629 default:
sahilmgandhi 18:6a4db94011d3 630 break;
sahilmgandhi 18:6a4db94011d3 631 }
sahilmgandhi 18:6a4db94011d3 632 } else {
sahilmgandhi 18:6a4db94011d3 633 #if (!SAM4C && !SAM4CP && !SAM4CM)
sahilmgandhi 18:6a4db94011d3 634 switch (ul_value) {
sahilmgandhi 18:6a4db94011d3 635 case 0:
sahilmgandhi 18:6a4db94011d3 636 p_rtc->RTC_MR &= ~RTC_MR_OUT1_Msk;
sahilmgandhi 18:6a4db94011d3 637 p_rtc->RTC_MR |= RTC_MR_OUT1_NO_WAVE;
sahilmgandhi 18:6a4db94011d3 638 break;
sahilmgandhi 18:6a4db94011d3 639
sahilmgandhi 18:6a4db94011d3 640 case 1:
sahilmgandhi 18:6a4db94011d3 641 p_rtc->RTC_MR &= ~RTC_MR_OUT1_Msk;
sahilmgandhi 18:6a4db94011d3 642 p_rtc->RTC_MR |= RTC_MR_OUT1_FREQ1HZ;
sahilmgandhi 18:6a4db94011d3 643 break;
sahilmgandhi 18:6a4db94011d3 644
sahilmgandhi 18:6a4db94011d3 645 case 2:
sahilmgandhi 18:6a4db94011d3 646 p_rtc->RTC_MR &= ~RTC_MR_OUT1_Msk;
sahilmgandhi 18:6a4db94011d3 647 p_rtc->RTC_MR |= RTC_MR_OUT1_FREQ32HZ;
sahilmgandhi 18:6a4db94011d3 648 break;
sahilmgandhi 18:6a4db94011d3 649
sahilmgandhi 18:6a4db94011d3 650 case 3:
sahilmgandhi 18:6a4db94011d3 651 p_rtc->RTC_MR &= ~RTC_MR_OUT1_Msk;
sahilmgandhi 18:6a4db94011d3 652 p_rtc->RTC_MR |= RTC_MR_OUT1_FREQ64HZ;
sahilmgandhi 18:6a4db94011d3 653 break;
sahilmgandhi 18:6a4db94011d3 654
sahilmgandhi 18:6a4db94011d3 655 case 4:
sahilmgandhi 18:6a4db94011d3 656 p_rtc->RTC_MR &= ~RTC_MR_OUT1_Msk;
sahilmgandhi 18:6a4db94011d3 657 p_rtc->RTC_MR |= RTC_MR_OUT1_FREQ512HZ;
sahilmgandhi 18:6a4db94011d3 658 break;
sahilmgandhi 18:6a4db94011d3 659
sahilmgandhi 18:6a4db94011d3 660 #if (!SAMG)
sahilmgandhi 18:6a4db94011d3 661 case 5:
sahilmgandhi 18:6a4db94011d3 662 p_rtc->RTC_MR &= ~RTC_MR_OUT1_Msk;
sahilmgandhi 18:6a4db94011d3 663 p_rtc->RTC_MR |= RTC_MR_OUT1_ALARM_TOGGLE;
sahilmgandhi 18:6a4db94011d3 664 break;
sahilmgandhi 18:6a4db94011d3 665 #endif
sahilmgandhi 18:6a4db94011d3 666
sahilmgandhi 18:6a4db94011d3 667 case 6:
sahilmgandhi 18:6a4db94011d3 668 p_rtc->RTC_MR &= ~RTC_MR_OUT1_Msk;
sahilmgandhi 18:6a4db94011d3 669 p_rtc->RTC_MR |= RTC_MR_OUT1_ALARM_FLAG;
sahilmgandhi 18:6a4db94011d3 670 break;
sahilmgandhi 18:6a4db94011d3 671
sahilmgandhi 18:6a4db94011d3 672 #if (!SAMG)
sahilmgandhi 18:6a4db94011d3 673 case 7:
sahilmgandhi 18:6a4db94011d3 674 p_rtc->RTC_MR &= ~RTC_MR_OUT1_Msk;
sahilmgandhi 18:6a4db94011d3 675 p_rtc->RTC_MR |= RTC_MR_OUT1_PROG_PULSE;
sahilmgandhi 18:6a4db94011d3 676 break;
sahilmgandhi 18:6a4db94011d3 677 #endif
sahilmgandhi 18:6a4db94011d3 678
sahilmgandhi 18:6a4db94011d3 679 default:
sahilmgandhi 18:6a4db94011d3 680 break;
sahilmgandhi 18:6a4db94011d3 681 }
sahilmgandhi 18:6a4db94011d3 682 #endif
sahilmgandhi 18:6a4db94011d3 683 }
sahilmgandhi 18:6a4db94011d3 684 }
sahilmgandhi 18:6a4db94011d3 685
sahilmgandhi 18:6a4db94011d3 686 #if ((SAM3S8) || (SAM3SD8) || (SAM4S) || (SAM4C) || SAMV71 || SAMV70 || SAME70 || SAMS70)
sahilmgandhi 18:6a4db94011d3 687 /**
sahilmgandhi 18:6a4db94011d3 688 * \brief Set the pulse output waveform parameters.
sahilmgandhi 18:6a4db94011d3 689 *
sahilmgandhi 18:6a4db94011d3 690 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 691 * \param ul_time_high High duration of the output pulse.
sahilmgandhi 18:6a4db94011d3 692 * \param ul_period Period of the output pulse.
sahilmgandhi 18:6a4db94011d3 693 */
sahilmgandhi 18:6a4db94011d3 694 void rtc_set_pulse_parameter(Rtc *p_rtc, uint32_t ul_time_high,
sahilmgandhi 18:6a4db94011d3 695 uint32_t ul_period)
sahilmgandhi 18:6a4db94011d3 696 {
sahilmgandhi 18:6a4db94011d3 697 uint32_t ul_temp;
sahilmgandhi 18:6a4db94011d3 698
sahilmgandhi 18:6a4db94011d3 699 ul_temp = p_rtc->RTC_MR;
sahilmgandhi 18:6a4db94011d3 700
sahilmgandhi 18:6a4db94011d3 701 ul_temp |= (RTC_MR_THIGH_Msk & ((ul_time_high) << RTC_MR_THIGH_Pos));
sahilmgandhi 18:6a4db94011d3 702 ul_temp |= (RTC_MR_TPERIOD_Msk & ((ul_period) << RTC_MR_TPERIOD_Pos));
sahilmgandhi 18:6a4db94011d3 703
sahilmgandhi 18:6a4db94011d3 704 p_rtc->RTC_MR = ul_temp;
sahilmgandhi 18:6a4db94011d3 705 }
sahilmgandhi 18:6a4db94011d3 706 #endif
sahilmgandhi 18:6a4db94011d3 707 #endif
sahilmgandhi 18:6a4db94011d3 708
sahilmgandhi 18:6a4db94011d3 709
sahilmgandhi 18:6a4db94011d3 710 #if ((SAM3N) || (SAM3U) || (SAM3XA))
sahilmgandhi 18:6a4db94011d3 711 /**
sahilmgandhi 18:6a4db94011d3 712 * \brief Enable or disable write protection of RTC registers.
sahilmgandhi 18:6a4db94011d3 713 *
sahilmgandhi 18:6a4db94011d3 714 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 715 * \param ul_enable 1 to enable, 0 to disable.
sahilmgandhi 18:6a4db94011d3 716 */
sahilmgandhi 18:6a4db94011d3 717 void rtc_set_writeprotect(Rtc *p_rtc, uint32_t ul_enable)
sahilmgandhi 18:6a4db94011d3 718 {
sahilmgandhi 18:6a4db94011d3 719 if (ul_enable) {
sahilmgandhi 18:6a4db94011d3 720 p_rtc->RTC_WPMR = RTC_WPMR_WPKEY(RTC_WP_KEY) | RTC_WPMR_WPEN;
sahilmgandhi 18:6a4db94011d3 721 } else {
sahilmgandhi 18:6a4db94011d3 722 p_rtc->RTC_WPMR = RTC_WPMR_WPKEY(RTC_WP_KEY);
sahilmgandhi 18:6a4db94011d3 723 }
sahilmgandhi 18:6a4db94011d3 724 }
sahilmgandhi 18:6a4db94011d3 725 #endif /* ((SAM3N) || (SAM3U) || (SAM3XA)) */
sahilmgandhi 18:6a4db94011d3 726
sahilmgandhi 18:6a4db94011d3 727 #if SAM4C || SAM4CP || SAM4CM
sahilmgandhi 18:6a4db94011d3 728 /**
sahilmgandhi 18:6a4db94011d3 729 * \brief Get the RTC tamper time value.
sahilmgandhi 18:6a4db94011d3 730 *
sahilmgandhi 18:6a4db94011d3 731 * \note This function should be called before rtc_get_tamper_source()
sahilmgandhi 18:6a4db94011d3 732 * function call, Otherwise the tamper time will be cleared.
sahilmgandhi 18:6a4db94011d3 733 *
sahilmgandhi 18:6a4db94011d3 734 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 735 * \param pul_hour Current hour, 24-hour mode.
sahilmgandhi 18:6a4db94011d3 736 * \param pul_minute Current minute.
sahilmgandhi 18:6a4db94011d3 737 * \param pul_second Current second.
sahilmgandhi 18:6a4db94011d3 738 * \param reg_num Current tamper register set number.
sahilmgandhi 18:6a4db94011d3 739 */
sahilmgandhi 18:6a4db94011d3 740 void rtc_get_tamper_time(Rtc *p_rtc, uint32_t *pul_hour, uint32_t *pul_minute,
sahilmgandhi 18:6a4db94011d3 741 uint32_t *pul_second, uint8_t reg_num)
sahilmgandhi 18:6a4db94011d3 742 {
sahilmgandhi 18:6a4db94011d3 743 uint32_t ul_time;
sahilmgandhi 18:6a4db94011d3 744 uint32_t ul_temp;
sahilmgandhi 18:6a4db94011d3 745
sahilmgandhi 18:6a4db94011d3 746 /* Get the current RTC time (multiple reads are to insure a stable value). */
sahilmgandhi 18:6a4db94011d3 747 ul_time = p_rtc->RTC_TS[reg_num].RTC_TSTR;
sahilmgandhi 18:6a4db94011d3 748 while (ul_time != p_rtc->RTC_TS[reg_num].RTC_TSTR) {
sahilmgandhi 18:6a4db94011d3 749 ul_time = p_rtc->RTC_TS[reg_num].RTC_TSTR;
sahilmgandhi 18:6a4db94011d3 750 }
sahilmgandhi 18:6a4db94011d3 751
sahilmgandhi 18:6a4db94011d3 752 /* Hour */
sahilmgandhi 18:6a4db94011d3 753 if (pul_hour) {
sahilmgandhi 18:6a4db94011d3 754 ul_temp = (ul_time & RTC_TSTR_HOUR_Msk) >> RTC_TSTR_HOUR_Pos;
sahilmgandhi 18:6a4db94011d3 755 *pul_hour = (ul_temp >> BCD_SHIFT) * BCD_FACTOR + (ul_temp & BCD_MASK);
sahilmgandhi 18:6a4db94011d3 756
sahilmgandhi 18:6a4db94011d3 757 if ((ul_time & RTC_TSTR_AMPM) == RTC_TSTR_AMPM) {
sahilmgandhi 18:6a4db94011d3 758 *pul_hour += 12;
sahilmgandhi 18:6a4db94011d3 759 }
sahilmgandhi 18:6a4db94011d3 760 }
sahilmgandhi 18:6a4db94011d3 761
sahilmgandhi 18:6a4db94011d3 762 /* Minute */
sahilmgandhi 18:6a4db94011d3 763 if (pul_minute) {
sahilmgandhi 18:6a4db94011d3 764 ul_temp = (ul_time & RTC_TSTR_MIN_Msk) >> RTC_TSTR_MIN_Pos;
sahilmgandhi 18:6a4db94011d3 765 *pul_minute = (ul_temp >> BCD_SHIFT) * BCD_FACTOR + (ul_temp & BCD_MASK);
sahilmgandhi 18:6a4db94011d3 766 }
sahilmgandhi 18:6a4db94011d3 767
sahilmgandhi 18:6a4db94011d3 768 /* Second */
sahilmgandhi 18:6a4db94011d3 769 if (pul_second) {
sahilmgandhi 18:6a4db94011d3 770 ul_temp = (ul_time & RTC_TSTR_SEC_Msk) >> RTC_TSTR_SEC_Pos;
sahilmgandhi 18:6a4db94011d3 771 *pul_second = (ul_temp >> BCD_SHIFT) * BCD_FACTOR + (ul_temp & BCD_MASK);
sahilmgandhi 18:6a4db94011d3 772 }
sahilmgandhi 18:6a4db94011d3 773 }
sahilmgandhi 18:6a4db94011d3 774
sahilmgandhi 18:6a4db94011d3 775 /**
sahilmgandhi 18:6a4db94011d3 776 * \brief Get the RTC tamper date.
sahilmgandhi 18:6a4db94011d3 777 *
sahilmgandhi 18:6a4db94011d3 778 * \note This function should be called before rtc_get_tamper_source()
sahilmgandhi 18:6a4db94011d3 779 * function call, Otherwise the tamper date will be cleared.
sahilmgandhi 18:6a4db94011d3 780 *
sahilmgandhi 18:6a4db94011d3 781 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 782 * \param pul_year Current year.
sahilmgandhi 18:6a4db94011d3 783 * \param pul_month Current month.
sahilmgandhi 18:6a4db94011d3 784 * \param pul_day Current day.
sahilmgandhi 18:6a4db94011d3 785 * \param pul_week Current day in current week.
sahilmgandhi 18:6a4db94011d3 786 * \param reg_num Current tamper register set number.
sahilmgandhi 18:6a4db94011d3 787 */
sahilmgandhi 18:6a4db94011d3 788 void rtc_get_tamper_date(Rtc *p_rtc, uint32_t *pul_year, uint32_t *pul_month,
sahilmgandhi 18:6a4db94011d3 789 uint32_t *pul_day, uint32_t *pul_week, uint8_t reg_num)
sahilmgandhi 18:6a4db94011d3 790 {
sahilmgandhi 18:6a4db94011d3 791 uint32_t ul_date;
sahilmgandhi 18:6a4db94011d3 792 uint32_t ul_cent;
sahilmgandhi 18:6a4db94011d3 793 uint32_t ul_temp;
sahilmgandhi 18:6a4db94011d3 794
sahilmgandhi 18:6a4db94011d3 795 /* Get the current date (multiple reads are to insure a stable value). */
sahilmgandhi 18:6a4db94011d3 796 ul_date = p_rtc->RTC_TS[reg_num].RTC_TSDR;
sahilmgandhi 18:6a4db94011d3 797 while (ul_date != p_rtc->RTC_TS[reg_num].RTC_TSDR) {
sahilmgandhi 18:6a4db94011d3 798 ul_date = p_rtc->RTC_TS[reg_num].RTC_TSDR;
sahilmgandhi 18:6a4db94011d3 799 }
sahilmgandhi 18:6a4db94011d3 800
sahilmgandhi 18:6a4db94011d3 801 /* Retrieve year */
sahilmgandhi 18:6a4db94011d3 802 if (pul_year) {
sahilmgandhi 18:6a4db94011d3 803 ul_temp = (ul_date & RTC_TSDR_CENT_Msk) >> RTC_TSDR_CENT_Pos;
sahilmgandhi 18:6a4db94011d3 804 ul_cent = (ul_temp >> BCD_SHIFT) * BCD_FACTOR + (ul_temp & BCD_MASK);
sahilmgandhi 18:6a4db94011d3 805 ul_temp = (ul_date & RTC_TSDR_YEAR_Msk) >> RTC_TSDR_YEAR_Pos;
sahilmgandhi 18:6a4db94011d3 806 *pul_year = (ul_cent * BCD_FACTOR * BCD_FACTOR) +
sahilmgandhi 18:6a4db94011d3 807 (ul_temp >> BCD_SHIFT) * BCD_FACTOR + (ul_temp & BCD_MASK);
sahilmgandhi 18:6a4db94011d3 808 }
sahilmgandhi 18:6a4db94011d3 809
sahilmgandhi 18:6a4db94011d3 810 /* Retrieve month */
sahilmgandhi 18:6a4db94011d3 811 if (pul_month) {
sahilmgandhi 18:6a4db94011d3 812 ul_temp = (ul_date & RTC_TSDR_MONTH_Msk) >> RTC_TSDR_MONTH_Pos;
sahilmgandhi 18:6a4db94011d3 813 *pul_month = (ul_temp >> BCD_SHIFT) * BCD_FACTOR + (ul_temp & BCD_MASK);
sahilmgandhi 18:6a4db94011d3 814 }
sahilmgandhi 18:6a4db94011d3 815
sahilmgandhi 18:6a4db94011d3 816 /* Retrieve day */
sahilmgandhi 18:6a4db94011d3 817 if (pul_day) {
sahilmgandhi 18:6a4db94011d3 818 ul_temp = (ul_date & RTC_TSDR_DATE_Msk) >> RTC_TSDR_DATE_Pos;
sahilmgandhi 18:6a4db94011d3 819 *pul_day = (ul_temp >> BCD_SHIFT) * BCD_FACTOR + (ul_temp & BCD_MASK);
sahilmgandhi 18:6a4db94011d3 820 }
sahilmgandhi 18:6a4db94011d3 821
sahilmgandhi 18:6a4db94011d3 822 /* Retrieve week */
sahilmgandhi 18:6a4db94011d3 823 if (pul_week) {
sahilmgandhi 18:6a4db94011d3 824 *pul_week = ((ul_date & RTC_TSDR_DAY_Msk) >> RTC_TSDR_DAY_Pos);
sahilmgandhi 18:6a4db94011d3 825 }
sahilmgandhi 18:6a4db94011d3 826 }
sahilmgandhi 18:6a4db94011d3 827
sahilmgandhi 18:6a4db94011d3 828 /**
sahilmgandhi 18:6a4db94011d3 829 * \brief Get the RTC tamper source.
sahilmgandhi 18:6a4db94011d3 830 *
sahilmgandhi 18:6a4db94011d3 831 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 832 * \param reg_num Current tamper register set number.
sahilmgandhi 18:6a4db94011d3 833 *
sahilmgandhi 18:6a4db94011d3 834 * \return Tamper source.
sahilmgandhi 18:6a4db94011d3 835 */
sahilmgandhi 18:6a4db94011d3 836 uint32_t rtc_get_tamper_source(Rtc *p_rtc, uint8_t reg_num)
sahilmgandhi 18:6a4db94011d3 837 {
sahilmgandhi 18:6a4db94011d3 838 return (p_rtc->RTC_TS[reg_num].RTC_TSSR & RTC_TSSR_TSRC_Msk) >>
sahilmgandhi 18:6a4db94011d3 839 RTC_TSSR_TSRC_Pos;
sahilmgandhi 18:6a4db94011d3 840 }
sahilmgandhi 18:6a4db94011d3 841
sahilmgandhi 18:6a4db94011d3 842 /**
sahilmgandhi 18:6a4db94011d3 843 * \brief Get the RTC tamper event counter.
sahilmgandhi 18:6a4db94011d3 844 *
sahilmgandhi 18:6a4db94011d3 845 * \note This function should be called before rtc_get_tamper_source()
sahilmgandhi 18:6a4db94011d3 846 * function call, Otherwise the tamper event counter will be cleared.
sahilmgandhi 18:6a4db94011d3 847 *
sahilmgandhi 18:6a4db94011d3 848 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 849 *
sahilmgandhi 18:6a4db94011d3 850 * \return Tamper event counter
sahilmgandhi 18:6a4db94011d3 851 */
sahilmgandhi 18:6a4db94011d3 852 uint32_t rtc_get_tamper_event_counter(Rtc *p_rtc)
sahilmgandhi 18:6a4db94011d3 853 {
sahilmgandhi 18:6a4db94011d3 854 return (p_rtc->RTC_TS[0].RTC_TSTR & RTC_TSTR_TEVCNT_Msk) >>
sahilmgandhi 18:6a4db94011d3 855 RTC_TSTR_TEVCNT_Pos;
sahilmgandhi 18:6a4db94011d3 856 }
sahilmgandhi 18:6a4db94011d3 857
sahilmgandhi 18:6a4db94011d3 858 /**
sahilmgandhi 18:6a4db94011d3 859 * \brief Check the system is in backup mode when RTC tamper event happen.
sahilmgandhi 18:6a4db94011d3 860 *
sahilmgandhi 18:6a4db94011d3 861 * \note This function should be called before rtc_get_tamper_source()
sahilmgandhi 18:6a4db94011d3 862 * function call, Otherwise the flag indicates tamper occur in backup
sahilmgandhi 18:6a4db94011d3 863 * mode will be cleared.
sahilmgandhi 18:6a4db94011d3 864 *
sahilmgandhi 18:6a4db94011d3 865 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 866 * \param reg_num Current tamper register set number.
sahilmgandhi 18:6a4db94011d3 867 *
sahilmgandhi 18:6a4db94011d3 868 * \return True - The system is in backup mode when the tamper event occurs.
sahilmgandhi 18:6a4db94011d3 869 * Flase - The system is different from backup mode.
sahilmgandhi 18:6a4db94011d3 870 */
sahilmgandhi 18:6a4db94011d3 871 bool rtc_is_tamper_occur_in_backup_mode(Rtc *p_rtc, uint8_t reg_num)
sahilmgandhi 18:6a4db94011d3 872 {
sahilmgandhi 18:6a4db94011d3 873 if(p_rtc->RTC_TS[reg_num].RTC_TSTR & RTC_TSTR_BACKUP) {
sahilmgandhi 18:6a4db94011d3 874 return true;
sahilmgandhi 18:6a4db94011d3 875 } else {
sahilmgandhi 18:6a4db94011d3 876 return false;
sahilmgandhi 18:6a4db94011d3 877 }
sahilmgandhi 18:6a4db94011d3 878 }
sahilmgandhi 18:6a4db94011d3 879 #endif
sahilmgandhi 18:6a4db94011d3 880
sahilmgandhi 18:6a4db94011d3 881 #if (SAMG55)
sahilmgandhi 18:6a4db94011d3 882 /**
sahilmgandhi 18:6a4db94011d3 883 * \brief Get the RTC milliseconds value.
sahilmgandhi 18:6a4db94011d3 884 *
sahilmgandhi 18:6a4db94011d3 885 * \param p_rtc Pointer to an RTC instance.
sahilmgandhi 18:6a4db94011d3 886 *
sahilmgandhi 18:6a4db94011d3 887 * \return Number of 1/1024 seconds elapsed within one second.
sahilmgandhi 18:6a4db94011d3 888 */
sahilmgandhi 18:6a4db94011d3 889 uint32_t rtc_get_milliseconds(Rtc *p_rtc)
sahilmgandhi 18:6a4db94011d3 890 {
sahilmgandhi 18:6a4db94011d3 891 return (p_rtc->RTC_MSR) & RTC_MSR_MS_Msk;
sahilmgandhi 18:6a4db94011d3 892 }
sahilmgandhi 18:6a4db94011d3 893 #endif
sahilmgandhi 18:6a4db94011d3 894
sahilmgandhi 18:6a4db94011d3 895
sahilmgandhi 18:6a4db94011d3 896 //@}
sahilmgandhi 18:6a4db94011d3 897
sahilmgandhi 18:6a4db94011d3 898 /// @cond 0
sahilmgandhi 18:6a4db94011d3 899 /**INDENT-OFF**/
sahilmgandhi 18:6a4db94011d3 900 #ifdef __cplusplus
sahilmgandhi 18:6a4db94011d3 901 }
sahilmgandhi 18:6a4db94011d3 902 #endif
sahilmgandhi 18:6a4db94011d3 903 /**INDENT-ON**/
sahilmgandhi 18:6a4db94011d3 904 /// @endcond