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