Gordon Craig / mbed-dev

Fork of mbed-dev by mbed official

Committer:
Dollyparton
Date:
Tue Dec 19 12:50:13 2017 +0000
Revision:
174:ed647f63e28d
Parent:
154:37f96f9d4de2
Added RAW socket.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 154:37f96f9d4de2 1 /*
<> 154:37f96f9d4de2 2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
<> 154:37f96f9d4de2 3 * All rights reserved.
<> 154:37f96f9d4de2 4 *
<> 154:37f96f9d4de2 5 * Redistribution and use in source and binary forms, with or without modification,
<> 154:37f96f9d4de2 6 * are permitted provided that the following conditions are met:
<> 154:37f96f9d4de2 7 *
<> 154:37f96f9d4de2 8 * o Redistributions of source code must retain the above copyright notice, this list
<> 154:37f96f9d4de2 9 * of conditions and the following disclaimer.
<> 154:37f96f9d4de2 10 *
<> 154:37f96f9d4de2 11 * o Redistributions in binary form must reproduce the above copyright notice, this
<> 154:37f96f9d4de2 12 * list of conditions and the following disclaimer in the documentation and/or
<> 154:37f96f9d4de2 13 * other materials provided with the distribution.
<> 154:37f96f9d4de2 14 *
<> 154:37f96f9d4de2 15 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
<> 154:37f96f9d4de2 16 * contributors may be used to endorse or promote products derived from this
<> 154:37f96f9d4de2 17 * software without specific prior written permission.
<> 154:37f96f9d4de2 18 *
<> 154:37f96f9d4de2 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
<> 154:37f96f9d4de2 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
<> 154:37f96f9d4de2 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
<> 154:37f96f9d4de2 22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
<> 154:37f96f9d4de2 23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
<> 154:37f96f9d4de2 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
<> 154:37f96f9d4de2 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
<> 154:37f96f9d4de2 26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
<> 154:37f96f9d4de2 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
<> 154:37f96f9d4de2 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<> 154:37f96f9d4de2 29 */
<> 154:37f96f9d4de2 30
<> 154:37f96f9d4de2 31 #include "fsl_rtc.h"
<> 154:37f96f9d4de2 32
<> 154:37f96f9d4de2 33 /*******************************************************************************
<> 154:37f96f9d4de2 34 * Definitions
<> 154:37f96f9d4de2 35 ******************************************************************************/
<> 154:37f96f9d4de2 36 #define SECONDS_IN_A_DAY (86400U)
<> 154:37f96f9d4de2 37 #define SECONDS_IN_A_HOUR (3600U)
<> 154:37f96f9d4de2 38 #define SECONDS_IN_A_MINUTE (60U)
<> 154:37f96f9d4de2 39 #define DAYS_IN_A_YEAR (365U)
<> 154:37f96f9d4de2 40 #define YEAR_RANGE_START (1970U)
<> 154:37f96f9d4de2 41 #define YEAR_RANGE_END (2099U)
<> 154:37f96f9d4de2 42
<> 154:37f96f9d4de2 43 /*******************************************************************************
<> 154:37f96f9d4de2 44 * Prototypes
<> 154:37f96f9d4de2 45 ******************************************************************************/
<> 154:37f96f9d4de2 46 /*!
<> 154:37f96f9d4de2 47 * @brief Checks whether the date and time passed in is valid
<> 154:37f96f9d4de2 48 *
<> 154:37f96f9d4de2 49 * @param datetime Pointer to structure where the date and time details are stored
<> 154:37f96f9d4de2 50 *
<> 154:37f96f9d4de2 51 * @return Returns false if the date & time details are out of range; true if in range
<> 154:37f96f9d4de2 52 */
<> 154:37f96f9d4de2 53 static bool RTC_CheckDatetimeFormat(const rtc_datetime_t *datetime);
<> 154:37f96f9d4de2 54
<> 154:37f96f9d4de2 55 /*!
<> 154:37f96f9d4de2 56 * @brief Converts time data from datetime to seconds
<> 154:37f96f9d4de2 57 *
<> 154:37f96f9d4de2 58 * @param datetime Pointer to datetime structure where the date and time details are stored
<> 154:37f96f9d4de2 59 *
<> 154:37f96f9d4de2 60 * @return The result of the conversion in seconds
<> 154:37f96f9d4de2 61 */
<> 154:37f96f9d4de2 62 static uint32_t RTC_ConvertDatetimeToSeconds(const rtc_datetime_t *datetime);
<> 154:37f96f9d4de2 63
<> 154:37f96f9d4de2 64 /*!
<> 154:37f96f9d4de2 65 * @brief Converts time data from seconds to a datetime structure
<> 154:37f96f9d4de2 66 *
<> 154:37f96f9d4de2 67 * @param seconds Seconds value that needs to be converted to datetime format
<> 154:37f96f9d4de2 68 * @param datetime Pointer to the datetime structure where the result of the conversion is stored
<> 154:37f96f9d4de2 69 */
<> 154:37f96f9d4de2 70 static void RTC_ConvertSecondsToDatetime(uint32_t seconds, rtc_datetime_t *datetime);
<> 154:37f96f9d4de2 71
<> 154:37f96f9d4de2 72 /*******************************************************************************
<> 154:37f96f9d4de2 73 * Code
<> 154:37f96f9d4de2 74 ******************************************************************************/
<> 154:37f96f9d4de2 75 static bool RTC_CheckDatetimeFormat(const rtc_datetime_t *datetime)
<> 154:37f96f9d4de2 76 {
<> 154:37f96f9d4de2 77 assert(datetime);
<> 154:37f96f9d4de2 78
<> 154:37f96f9d4de2 79 /* Table of days in a month for a non leap year. First entry in the table is not used,
<> 154:37f96f9d4de2 80 * valid months start from 1
<> 154:37f96f9d4de2 81 */
<> 154:37f96f9d4de2 82 uint8_t daysPerMonth[] = {0U, 31U, 28U, 31U, 30U, 31U, 30U, 31U, 31U, 30U, 31U, 30U, 31U};
<> 154:37f96f9d4de2 83
<> 154:37f96f9d4de2 84 /* Check year, month, hour, minute, seconds */
<> 154:37f96f9d4de2 85 if ((datetime->year < YEAR_RANGE_START) || (datetime->year > YEAR_RANGE_END) || (datetime->month > 12U) ||
<> 154:37f96f9d4de2 86 (datetime->month < 1U) || (datetime->hour >= 24U) || (datetime->minute >= 60U) || (datetime->second >= 60U))
<> 154:37f96f9d4de2 87 {
<> 154:37f96f9d4de2 88 /* If not correct then error*/
<> 154:37f96f9d4de2 89 return false;
<> 154:37f96f9d4de2 90 }
<> 154:37f96f9d4de2 91
<> 154:37f96f9d4de2 92 /* Adjust the days in February for a leap year */
<> 154:37f96f9d4de2 93 if ((((datetime->year & 3U) == 0) && (datetime->year % 100 != 0)) || (datetime->year % 400 == 0))
<> 154:37f96f9d4de2 94 {
<> 154:37f96f9d4de2 95 daysPerMonth[2] = 29U;
<> 154:37f96f9d4de2 96 }
<> 154:37f96f9d4de2 97
<> 154:37f96f9d4de2 98 /* Check the validity of the day */
<> 154:37f96f9d4de2 99 if ((datetime->day > daysPerMonth[datetime->month]) || (datetime->day < 1U))
<> 154:37f96f9d4de2 100 {
<> 154:37f96f9d4de2 101 return false;
<> 154:37f96f9d4de2 102 }
<> 154:37f96f9d4de2 103
<> 154:37f96f9d4de2 104 return true;
<> 154:37f96f9d4de2 105 }
<> 154:37f96f9d4de2 106
<> 154:37f96f9d4de2 107 static uint32_t RTC_ConvertDatetimeToSeconds(const rtc_datetime_t *datetime)
<> 154:37f96f9d4de2 108 {
<> 154:37f96f9d4de2 109 assert(datetime);
<> 154:37f96f9d4de2 110
<> 154:37f96f9d4de2 111 /* Number of days from begin of the non Leap-year*/
<> 154:37f96f9d4de2 112 /* Number of days from begin of the non Leap-year*/
<> 154:37f96f9d4de2 113 uint16_t monthDays[] = {0U, 0U, 31U, 59U, 90U, 120U, 151U, 181U, 212U, 243U, 273U, 304U, 334U};
<> 154:37f96f9d4de2 114 uint32_t seconds;
<> 154:37f96f9d4de2 115
<> 154:37f96f9d4de2 116 /* Compute number of days from 1970 till given year*/
<> 154:37f96f9d4de2 117 seconds = (datetime->year - 1970U) * DAYS_IN_A_YEAR;
<> 154:37f96f9d4de2 118 /* Add leap year days */
<> 154:37f96f9d4de2 119 seconds += ((datetime->year / 4) - (1970U / 4));
<> 154:37f96f9d4de2 120 /* Add number of days till given month*/
<> 154:37f96f9d4de2 121 seconds += monthDays[datetime->month];
<> 154:37f96f9d4de2 122 /* Add days in given month. We subtract the current day as it is
<> 154:37f96f9d4de2 123 * represented in the hours, minutes and seconds field*/
<> 154:37f96f9d4de2 124 seconds += (datetime->day - 1);
<> 154:37f96f9d4de2 125 /* For leap year if month less than or equal to Febraury, decrement day counter*/
<> 154:37f96f9d4de2 126 if ((!(datetime->year & 3U)) && (datetime->month <= 2U))
<> 154:37f96f9d4de2 127 {
<> 154:37f96f9d4de2 128 seconds--;
<> 154:37f96f9d4de2 129 }
<> 154:37f96f9d4de2 130
<> 154:37f96f9d4de2 131 seconds = (seconds * SECONDS_IN_A_DAY) + (datetime->hour * SECONDS_IN_A_HOUR) +
<> 154:37f96f9d4de2 132 (datetime->minute * SECONDS_IN_A_MINUTE) + datetime->second;
<> 154:37f96f9d4de2 133
<> 154:37f96f9d4de2 134 return seconds;
<> 154:37f96f9d4de2 135 }
<> 154:37f96f9d4de2 136
<> 154:37f96f9d4de2 137 static void RTC_ConvertSecondsToDatetime(uint32_t seconds, rtc_datetime_t *datetime)
<> 154:37f96f9d4de2 138 {
<> 154:37f96f9d4de2 139 assert(datetime);
<> 154:37f96f9d4de2 140
<> 154:37f96f9d4de2 141 uint32_t x;
<> 154:37f96f9d4de2 142 uint32_t secondsRemaining, days;
<> 154:37f96f9d4de2 143 uint16_t daysInYear;
<> 154:37f96f9d4de2 144 /* Table of days in a month for a non leap year. First entry in the table is not used,
<> 154:37f96f9d4de2 145 * valid months start from 1
<> 154:37f96f9d4de2 146 */
<> 154:37f96f9d4de2 147 uint8_t daysPerMonth[] = {0U, 31U, 28U, 31U, 30U, 31U, 30U, 31U, 31U, 30U, 31U, 30U, 31U};
<> 154:37f96f9d4de2 148
<> 154:37f96f9d4de2 149 /* Start with the seconds value that is passed in to be converted to date time format */
<> 154:37f96f9d4de2 150 secondsRemaining = seconds;
<> 154:37f96f9d4de2 151
<> 154:37f96f9d4de2 152 /* Calcuate the number of days, we add 1 for the current day which is represented in the
<> 154:37f96f9d4de2 153 * hours and seconds field
<> 154:37f96f9d4de2 154 */
<> 154:37f96f9d4de2 155 days = secondsRemaining / SECONDS_IN_A_DAY + 1;
<> 154:37f96f9d4de2 156
<> 154:37f96f9d4de2 157 /* Update seconds left*/
<> 154:37f96f9d4de2 158 secondsRemaining = secondsRemaining % SECONDS_IN_A_DAY;
<> 154:37f96f9d4de2 159
<> 154:37f96f9d4de2 160 /* Calculate the datetime hour, minute and second fields */
<> 154:37f96f9d4de2 161 datetime->hour = secondsRemaining / SECONDS_IN_A_HOUR;
<> 154:37f96f9d4de2 162 secondsRemaining = secondsRemaining % SECONDS_IN_A_HOUR;
<> 154:37f96f9d4de2 163 datetime->minute = secondsRemaining / 60U;
<> 154:37f96f9d4de2 164 datetime->second = secondsRemaining % SECONDS_IN_A_MINUTE;
<> 154:37f96f9d4de2 165
<> 154:37f96f9d4de2 166 /* Calculate year */
<> 154:37f96f9d4de2 167 daysInYear = DAYS_IN_A_YEAR;
<> 154:37f96f9d4de2 168 datetime->year = YEAR_RANGE_START;
<> 154:37f96f9d4de2 169 while (days > daysInYear)
<> 154:37f96f9d4de2 170 {
<> 154:37f96f9d4de2 171 /* Decrease day count by a year and increment year by 1 */
<> 154:37f96f9d4de2 172 days -= daysInYear;
<> 154:37f96f9d4de2 173 datetime->year++;
<> 154:37f96f9d4de2 174
<> 154:37f96f9d4de2 175 /* Adjust the number of days for a leap year */
<> 154:37f96f9d4de2 176 if (datetime->year & 3U)
<> 154:37f96f9d4de2 177 {
<> 154:37f96f9d4de2 178 daysInYear = DAYS_IN_A_YEAR;
<> 154:37f96f9d4de2 179 }
<> 154:37f96f9d4de2 180 else
<> 154:37f96f9d4de2 181 {
<> 154:37f96f9d4de2 182 daysInYear = DAYS_IN_A_YEAR + 1;
<> 154:37f96f9d4de2 183 }
<> 154:37f96f9d4de2 184 }
<> 154:37f96f9d4de2 185
<> 154:37f96f9d4de2 186 /* Adjust the days in February for a leap year */
<> 154:37f96f9d4de2 187 if (!(datetime->year & 3U))
<> 154:37f96f9d4de2 188 {
<> 154:37f96f9d4de2 189 daysPerMonth[2] = 29U;
<> 154:37f96f9d4de2 190 }
<> 154:37f96f9d4de2 191
<> 154:37f96f9d4de2 192 for (x = 1U; x <= 12U; x++)
<> 154:37f96f9d4de2 193 {
<> 154:37f96f9d4de2 194 if (days <= daysPerMonth[x])
<> 154:37f96f9d4de2 195 {
<> 154:37f96f9d4de2 196 datetime->month = x;
<> 154:37f96f9d4de2 197 break;
<> 154:37f96f9d4de2 198 }
<> 154:37f96f9d4de2 199 else
<> 154:37f96f9d4de2 200 {
<> 154:37f96f9d4de2 201 days -= daysPerMonth[x];
<> 154:37f96f9d4de2 202 }
<> 154:37f96f9d4de2 203 }
<> 154:37f96f9d4de2 204
<> 154:37f96f9d4de2 205 datetime->day = days;
<> 154:37f96f9d4de2 206 }
<> 154:37f96f9d4de2 207
<> 154:37f96f9d4de2 208 void RTC_Init(RTC_Type *base, const rtc_config_t *config)
<> 154:37f96f9d4de2 209 {
<> 154:37f96f9d4de2 210 assert(config);
<> 154:37f96f9d4de2 211
<> 154:37f96f9d4de2 212 uint32_t reg;
<> 154:37f96f9d4de2 213
<> 154:37f96f9d4de2 214 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
<> 154:37f96f9d4de2 215 CLOCK_EnableClock(kCLOCK_Rtc0);
<> 154:37f96f9d4de2 216 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
<> 154:37f96f9d4de2 217
<> 154:37f96f9d4de2 218 /* Issue a software reset if timer is invalid */
<> 154:37f96f9d4de2 219 if (RTC_GetStatusFlags(RTC) & kRTC_TimeInvalidFlag)
<> 154:37f96f9d4de2 220 {
<> 154:37f96f9d4de2 221 RTC_Reset(RTC);
<> 154:37f96f9d4de2 222 }
<> 154:37f96f9d4de2 223
<> 154:37f96f9d4de2 224 reg = base->CR;
<> 154:37f96f9d4de2 225 /* Setup the update mode and supervisor access mode */
<> 154:37f96f9d4de2 226 reg &= ~(RTC_CR_UM_MASK | RTC_CR_SUP_MASK);
<> 154:37f96f9d4de2 227 reg |= RTC_CR_UM(config->updateMode) | RTC_CR_SUP(config->supervisorAccess);
<> 154:37f96f9d4de2 228 #if defined(FSL_FEATURE_RTC_HAS_WAKEUP_PIN_SELECTION) && FSL_FEATURE_RTC_HAS_WAKEUP_PIN_SELECTION
<> 154:37f96f9d4de2 229 /* Setup the wakeup pin select */
<> 154:37f96f9d4de2 230 reg &= ~(RTC_CR_WPS_MASK);
<> 154:37f96f9d4de2 231 reg |= RTC_CR_WPS(config->wakeupSelect);
<> 154:37f96f9d4de2 232 #endif /* FSL_FEATURE_RTC_HAS_WAKEUP_PIN */
<> 154:37f96f9d4de2 233 base->CR = reg;
<> 154:37f96f9d4de2 234
<> 154:37f96f9d4de2 235 /* Configure the RTC time compensation register */
<> 154:37f96f9d4de2 236 base->TCR = (RTC_TCR_CIR(config->compensationInterval) | RTC_TCR_TCR(config->compensationTime));
<> 154:37f96f9d4de2 237 }
<> 154:37f96f9d4de2 238
<> 154:37f96f9d4de2 239 void RTC_GetDefaultConfig(rtc_config_t *config)
<> 154:37f96f9d4de2 240 {
<> 154:37f96f9d4de2 241 assert(config);
<> 154:37f96f9d4de2 242
<> 154:37f96f9d4de2 243 /* Wakeup pin will assert if the RTC interrupt asserts or if the wakeup pin is turned on */
<> 154:37f96f9d4de2 244 config->wakeupSelect = false;
<> 154:37f96f9d4de2 245 /* Registers cannot be written when locked */
<> 154:37f96f9d4de2 246 config->updateMode = false;
<> 154:37f96f9d4de2 247 /* Non-supervisor mode write accesses are not supported and will generate a bus error */
<> 154:37f96f9d4de2 248 config->supervisorAccess = false;
<> 154:37f96f9d4de2 249 /* Compensation interval used by the crystal compensation logic */
<> 154:37f96f9d4de2 250 config->compensationInterval = 0;
<> 154:37f96f9d4de2 251 /* Compensation time used by the crystal compensation logic */
<> 154:37f96f9d4de2 252 config->compensationTime = 0;
<> 154:37f96f9d4de2 253 }
<> 154:37f96f9d4de2 254
<> 154:37f96f9d4de2 255 status_t RTC_SetDatetime(RTC_Type *base, const rtc_datetime_t *datetime)
<> 154:37f96f9d4de2 256 {
<> 154:37f96f9d4de2 257 assert(datetime);
<> 154:37f96f9d4de2 258
<> 154:37f96f9d4de2 259 /* Return error if the time provided is not valid */
<> 154:37f96f9d4de2 260 if (!(RTC_CheckDatetimeFormat(datetime)))
<> 154:37f96f9d4de2 261 {
<> 154:37f96f9d4de2 262 return kStatus_InvalidArgument;
<> 154:37f96f9d4de2 263 }
<> 154:37f96f9d4de2 264
<> 154:37f96f9d4de2 265 /* Set time in seconds */
<> 154:37f96f9d4de2 266 base->TSR = RTC_ConvertDatetimeToSeconds(datetime);
<> 154:37f96f9d4de2 267
<> 154:37f96f9d4de2 268 return kStatus_Success;
<> 154:37f96f9d4de2 269 }
<> 154:37f96f9d4de2 270
<> 154:37f96f9d4de2 271 void RTC_GetDatetime(RTC_Type *base, rtc_datetime_t *datetime)
<> 154:37f96f9d4de2 272 {
<> 154:37f96f9d4de2 273 assert(datetime);
<> 154:37f96f9d4de2 274
<> 154:37f96f9d4de2 275 uint32_t seconds = 0;
<> 154:37f96f9d4de2 276
<> 154:37f96f9d4de2 277 seconds = base->TSR;
<> 154:37f96f9d4de2 278 RTC_ConvertSecondsToDatetime(seconds, datetime);
<> 154:37f96f9d4de2 279 }
<> 154:37f96f9d4de2 280
<> 154:37f96f9d4de2 281 status_t RTC_SetAlarm(RTC_Type *base, const rtc_datetime_t *alarmTime)
<> 154:37f96f9d4de2 282 {
<> 154:37f96f9d4de2 283 assert(alarmTime);
<> 154:37f96f9d4de2 284
<> 154:37f96f9d4de2 285 uint32_t alarmSeconds = 0;
<> 154:37f96f9d4de2 286 uint32_t currSeconds = 0;
<> 154:37f96f9d4de2 287
<> 154:37f96f9d4de2 288 /* Return error if the alarm time provided is not valid */
<> 154:37f96f9d4de2 289 if (!(RTC_CheckDatetimeFormat(alarmTime)))
<> 154:37f96f9d4de2 290 {
<> 154:37f96f9d4de2 291 return kStatus_InvalidArgument;
<> 154:37f96f9d4de2 292 }
<> 154:37f96f9d4de2 293
<> 154:37f96f9d4de2 294 alarmSeconds = RTC_ConvertDatetimeToSeconds(alarmTime);
<> 154:37f96f9d4de2 295
<> 154:37f96f9d4de2 296 /* Get the current time */
<> 154:37f96f9d4de2 297 currSeconds = base->TSR;
<> 154:37f96f9d4de2 298
<> 154:37f96f9d4de2 299 /* Return error if the alarm time has passed */
<> 154:37f96f9d4de2 300 if (alarmSeconds < currSeconds)
<> 154:37f96f9d4de2 301 {
<> 154:37f96f9d4de2 302 return kStatus_Fail;
<> 154:37f96f9d4de2 303 }
<> 154:37f96f9d4de2 304
<> 154:37f96f9d4de2 305 /* Set alarm in seconds*/
<> 154:37f96f9d4de2 306 base->TAR = alarmSeconds;
<> 154:37f96f9d4de2 307
<> 154:37f96f9d4de2 308 return kStatus_Success;
<> 154:37f96f9d4de2 309 }
<> 154:37f96f9d4de2 310
<> 154:37f96f9d4de2 311 void RTC_GetAlarm(RTC_Type *base, rtc_datetime_t *datetime)
<> 154:37f96f9d4de2 312 {
<> 154:37f96f9d4de2 313 assert(datetime);
<> 154:37f96f9d4de2 314
<> 154:37f96f9d4de2 315 uint32_t alarmSeconds = 0;
<> 154:37f96f9d4de2 316
<> 154:37f96f9d4de2 317 /* Get alarm in seconds */
<> 154:37f96f9d4de2 318 alarmSeconds = base->TAR;
<> 154:37f96f9d4de2 319
<> 154:37f96f9d4de2 320 RTC_ConvertSecondsToDatetime(alarmSeconds, datetime);
<> 154:37f96f9d4de2 321 }
<> 154:37f96f9d4de2 322
<> 154:37f96f9d4de2 323 void RTC_ClearStatusFlags(RTC_Type *base, uint32_t mask)
<> 154:37f96f9d4de2 324 {
<> 154:37f96f9d4de2 325 /* The alarm flag is cleared by writing to the TAR register */
<> 154:37f96f9d4de2 326 if (mask & kRTC_AlarmFlag)
<> 154:37f96f9d4de2 327 {
<> 154:37f96f9d4de2 328 base->TAR = 0U;
<> 154:37f96f9d4de2 329 }
<> 154:37f96f9d4de2 330
<> 154:37f96f9d4de2 331 /* The timer overflow flag is cleared by initializing the TSR register.
<> 154:37f96f9d4de2 332 * The time counter should be disabled for this write to be successful
<> 154:37f96f9d4de2 333 */
<> 154:37f96f9d4de2 334 if (mask & kRTC_TimeOverflowFlag)
<> 154:37f96f9d4de2 335 {
<> 154:37f96f9d4de2 336 base->TSR = 1U;
<> 154:37f96f9d4de2 337 }
<> 154:37f96f9d4de2 338
<> 154:37f96f9d4de2 339 /* The timer overflow flag is cleared by initializing the TSR register.
<> 154:37f96f9d4de2 340 * The time counter should be disabled for this write to be successful
<> 154:37f96f9d4de2 341 */
<> 154:37f96f9d4de2 342 if (mask & kRTC_TimeInvalidFlag)
<> 154:37f96f9d4de2 343 {
<> 154:37f96f9d4de2 344 base->TSR = 1U;
<> 154:37f96f9d4de2 345 }
<> 154:37f96f9d4de2 346 }
<> 154:37f96f9d4de2 347
<> 154:37f96f9d4de2 348 #if defined(FSL_FEATURE_RTC_HAS_MONOTONIC) && (FSL_FEATURE_RTC_HAS_MONOTONIC)
<> 154:37f96f9d4de2 349
<> 154:37f96f9d4de2 350 void RTC_GetMonotonicCounter(RTC_Type *base, uint64_t *counter)
<> 154:37f96f9d4de2 351 {
<> 154:37f96f9d4de2 352 assert(counter);
<> 154:37f96f9d4de2 353
<> 154:37f96f9d4de2 354 *counter = (((uint64_t)base->MCHR << 32) | ((uint64_t)base->MCLR));
<> 154:37f96f9d4de2 355 }
<> 154:37f96f9d4de2 356
<> 154:37f96f9d4de2 357 void RTC_SetMonotonicCounter(RTC_Type *base, uint64_t counter)
<> 154:37f96f9d4de2 358 {
<> 154:37f96f9d4de2 359 /* Prepare to initialize the register with the new value written */
<> 154:37f96f9d4de2 360 base->MER &= ~RTC_MER_MCE_MASK;
<> 154:37f96f9d4de2 361
<> 154:37f96f9d4de2 362 base->MCHR = (uint32_t)((counter) >> 32);
<> 154:37f96f9d4de2 363 base->MCLR = (uint32_t)(counter);
<> 154:37f96f9d4de2 364 }
<> 154:37f96f9d4de2 365
<> 154:37f96f9d4de2 366 status_t RTC_IncrementMonotonicCounter(RTC_Type *base)
<> 154:37f96f9d4de2 367 {
<> 154:37f96f9d4de2 368 if (base->SR & (RTC_SR_MOF_MASK | RTC_SR_TIF_MASK))
<> 154:37f96f9d4de2 369 {
<> 154:37f96f9d4de2 370 return kStatus_Fail;
<> 154:37f96f9d4de2 371 }
<> 154:37f96f9d4de2 372
<> 154:37f96f9d4de2 373 /* Prepare to switch to increment mode */
<> 154:37f96f9d4de2 374 base->MER |= RTC_MER_MCE_MASK;
<> 154:37f96f9d4de2 375 /* Write anything so the counter increments*/
<> 154:37f96f9d4de2 376 base->MCLR = 1U;
<> 154:37f96f9d4de2 377
<> 154:37f96f9d4de2 378 return kStatus_Success;
<> 154:37f96f9d4de2 379 }
<> 154:37f96f9d4de2 380
<> 154:37f96f9d4de2 381 #endif /* FSL_FEATURE_RTC_HAS_MONOTONIC */