mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

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

Committer:
mbed_official
Date:
Thu Sep 18 14:00:17 2014 +0100
Revision:
324:406fd2029f23
Parent:
149:1fb5f62b92bd
Synchronized with git revision a73f28e6fbca9559fbed2726410eeb4c0534a4a5

Full URL: https://github.com/mbedmicro/mbed/commit/a73f28e6fbca9559fbed2726410eeb4c0534a4a5/

Extended #476, which does not break ethernet for K64F

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 146:f64d43ff0c18 1 /*
mbed_official 146:f64d43ff0c18 2 * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
mbed_official 146:f64d43ff0c18 3 * All rights reserved.
mbed_official 146:f64d43ff0c18 4 *
mbed_official 146:f64d43ff0c18 5 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 146:f64d43ff0c18 6 * are permitted provided that the following conditions are met:
mbed_official 146:f64d43ff0c18 7 *
mbed_official 146:f64d43ff0c18 8 * o Redistributions of source code must retain the above copyright notice, this list
mbed_official 146:f64d43ff0c18 9 * of conditions and the following disclaimer.
mbed_official 146:f64d43ff0c18 10 *
mbed_official 146:f64d43ff0c18 11 * o Redistributions in binary form must reproduce the above copyright notice, this
mbed_official 146:f64d43ff0c18 12 * list of conditions and the following disclaimer in the documentation and/or
mbed_official 146:f64d43ff0c18 13 * other materials provided with the distribution.
mbed_official 146:f64d43ff0c18 14 *
mbed_official 146:f64d43ff0c18 15 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
mbed_official 146:f64d43ff0c18 16 * contributors may be used to endorse or promote products derived from this
mbed_official 146:f64d43ff0c18 17 * software without specific prior written permission.
mbed_official 146:f64d43ff0c18 18 *
mbed_official 146:f64d43ff0c18 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
mbed_official 146:f64d43ff0c18 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
mbed_official 146:f64d43ff0c18 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 146:f64d43ff0c18 22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
mbed_official 146:f64d43ff0c18 23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
mbed_official 146:f64d43ff0c18 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
mbed_official 146:f64d43ff0c18 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
mbed_official 146:f64d43ff0c18 26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
mbed_official 146:f64d43ff0c18 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
mbed_official 146:f64d43ff0c18 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 146:f64d43ff0c18 29 */
mbed_official 146:f64d43ff0c18 30
mbed_official 146:f64d43ff0c18 31 #include "fsl_rtc_hal.h"
mbed_official 146:f64d43ff0c18 32 #include "fsl_device_registers.h"
mbed_official 146:f64d43ff0c18 33
mbed_official 146:f64d43ff0c18 34 /*******************************************************************************
mbed_official 324:406fd2029f23 35 * Definitions
mbed_official 324:406fd2029f23 36 ******************************************************************************/
mbed_official 324:406fd2029f23 37
mbed_official 324:406fd2029f23 38 #define SECONDS_IN_A_DAY (86400U)
mbed_official 324:406fd2029f23 39 #define SECONDS_IN_A_HOUR (3600U)
mbed_official 324:406fd2029f23 40 #define SECONDS_IN_A_MIN (60U)
mbed_official 324:406fd2029f23 41 #define MINS_IN_A_HOUR (60U)
mbed_official 324:406fd2029f23 42 #define HOURS_IN_A_DAY (24U)
mbed_official 324:406fd2029f23 43 #define DAYS_IN_A_YEAR (365U)
mbed_official 324:406fd2029f23 44 #define DAYS_IN_A_LEAP_YEAR (366U)
mbed_official 324:406fd2029f23 45 #define YEAR_RANGE_START (1970U)
mbed_official 324:406fd2029f23 46 #define YEAR_RANGE_END (2099U)
mbed_official 324:406fd2029f23 47
mbed_official 324:406fd2029f23 48 /*******************************************************************************
mbed_official 324:406fd2029f23 49 * Variables
mbed_official 324:406fd2029f23 50 ******************************************************************************/
mbed_official 324:406fd2029f23 51
mbed_official 324:406fd2029f23 52 /* Table of month length (in days) for the Un-leap-year*/
mbed_official 324:406fd2029f23 53 static const uint8_t ULY[] = {0U, 31U, 28U, 31U, 30U, 31U, 30U, 31U, 31U, 30U,
mbed_official 324:406fd2029f23 54 31U,30U,31U};
mbed_official 324:406fd2029f23 55
mbed_official 324:406fd2029f23 56 /* Table of month length (in days) for the Leap-year*/
mbed_official 324:406fd2029f23 57 static const uint8_t LY[] = {0U, 31U, 29U, 31U, 30U, 31U, 30U, 31U, 31U, 30U,
mbed_official 324:406fd2029f23 58 31U,30U,31U};
mbed_official 324:406fd2029f23 59
mbed_official 324:406fd2029f23 60 /* Number of days from begin of the non Leap-year*/
mbed_official 324:406fd2029f23 61 static const uint16_t MONTH_DAYS[] = {0U, 0U, 31U, 59U, 90U, 120U, 151U, 181U,
mbed_official 324:406fd2029f23 62 212U, 243U, 273U, 304U, 334U};
mbed_official 324:406fd2029f23 63
mbed_official 324:406fd2029f23 64 /*******************************************************************************
mbed_official 146:f64d43ff0c18 65 * Code
mbed_official 146:f64d43ff0c18 66 ******************************************************************************/
mbed_official 146:f64d43ff0c18 67
mbed_official 324:406fd2029f23 68 /*FUNCTION**********************************************************************
mbed_official 324:406fd2029f23 69 *
mbed_official 324:406fd2029f23 70 * Function Name : RTC_HAL_ConvertSecsToDatetime
mbed_official 324:406fd2029f23 71 * Description : converts time data from seconds to a datetime structure.
mbed_official 324:406fd2029f23 72 * This function will convert time data from seconds to a datetime structure.
mbed_official 324:406fd2029f23 73 *
mbed_official 324:406fd2029f23 74 *END**************************************************************************/
mbed_official 324:406fd2029f23 75 void RTC_HAL_ConvertSecsToDatetime(const uint32_t * seconds, rtc_datetime_t * datetime)
mbed_official 324:406fd2029f23 76 {
mbed_official 324:406fd2029f23 77 uint32_t x;
mbed_official 324:406fd2029f23 78 uint32_t Seconds, Days, Days_in_year;
mbed_official 324:406fd2029f23 79 const uint8_t *Days_in_month;
mbed_official 324:406fd2029f23 80
mbed_official 324:406fd2029f23 81 /* Start from 1970-01-01*/
mbed_official 324:406fd2029f23 82 Seconds = *seconds;
mbed_official 324:406fd2029f23 83 /* days*/
mbed_official 324:406fd2029f23 84 Days = Seconds / SECONDS_IN_A_DAY;
mbed_official 324:406fd2029f23 85 /* seconds left*/
mbed_official 324:406fd2029f23 86 Seconds = Seconds % SECONDS_IN_A_DAY;
mbed_official 324:406fd2029f23 87 /* hours*/
mbed_official 324:406fd2029f23 88 datetime->hour = Seconds / SECONDS_IN_A_HOUR;
mbed_official 324:406fd2029f23 89 /* seconds left*/
mbed_official 324:406fd2029f23 90 Seconds = Seconds % SECONDS_IN_A_HOUR;
mbed_official 324:406fd2029f23 91 /* minutes*/
mbed_official 324:406fd2029f23 92 datetime->minute = Seconds / SECONDS_IN_A_MIN;
mbed_official 324:406fd2029f23 93 /* seconds*/
mbed_official 324:406fd2029f23 94 datetime->second = Seconds % SECONDS_IN_A_MIN;
mbed_official 324:406fd2029f23 95 /* year*/
mbed_official 324:406fd2029f23 96 datetime->year = YEAR_RANGE_START;
mbed_official 324:406fd2029f23 97 Days_in_year = DAYS_IN_A_YEAR;
mbed_official 324:406fd2029f23 98
mbed_official 324:406fd2029f23 99 while (Days > Days_in_year)
mbed_official 324:406fd2029f23 100 {
mbed_official 324:406fd2029f23 101 Days -= Days_in_year;
mbed_official 324:406fd2029f23 102 datetime->year++;
mbed_official 324:406fd2029f23 103 if (datetime->year & 3U)
mbed_official 324:406fd2029f23 104 {
mbed_official 324:406fd2029f23 105 Days_in_year = DAYS_IN_A_YEAR;
mbed_official 324:406fd2029f23 106 }
mbed_official 324:406fd2029f23 107 else
mbed_official 324:406fd2029f23 108 {
mbed_official 324:406fd2029f23 109 Days_in_year = DAYS_IN_A_LEAP_YEAR;
mbed_official 324:406fd2029f23 110 }
mbed_official 324:406fd2029f23 111 }
mbed_official 324:406fd2029f23 112
mbed_official 324:406fd2029f23 113 if (datetime->year & 3U)
mbed_official 324:406fd2029f23 114 {
mbed_official 324:406fd2029f23 115 Days_in_month = ULY;
mbed_official 324:406fd2029f23 116 }
mbed_official 324:406fd2029f23 117 else
mbed_official 324:406fd2029f23 118 {
mbed_official 324:406fd2029f23 119 Days_in_month = LY;
mbed_official 324:406fd2029f23 120 }
mbed_official 324:406fd2029f23 121
mbed_official 324:406fd2029f23 122 for (x=1U; x <= 12U; x++)
mbed_official 324:406fd2029f23 123 {
mbed_official 324:406fd2029f23 124 if (Days <= (*(Days_in_month + x)))
mbed_official 324:406fd2029f23 125 {
mbed_official 324:406fd2029f23 126 datetime->month = x;
mbed_official 324:406fd2029f23 127 break;
mbed_official 324:406fd2029f23 128 }
mbed_official 324:406fd2029f23 129 else
mbed_official 324:406fd2029f23 130 {
mbed_official 324:406fd2029f23 131 Days -= (*(Days_in_month + x));
mbed_official 324:406fd2029f23 132 }
mbed_official 324:406fd2029f23 133 }
mbed_official 324:406fd2029f23 134
mbed_official 324:406fd2029f23 135 datetime->day = Days;
mbed_official 324:406fd2029f23 136 }
mbed_official 324:406fd2029f23 137
mbed_official 324:406fd2029f23 138 /*FUNCTION**********************************************************************
mbed_official 324:406fd2029f23 139 *
mbed_official 324:406fd2029f23 140 * Function Name : RTC_HAL_IsDatetimeCorrectFormat
mbed_official 324:406fd2029f23 141 * Description : checks if the datetime is in correct format.
mbed_official 324:406fd2029f23 142 * This function will check if the given datetime is in the correct format.
mbed_official 324:406fd2029f23 143 *
mbed_official 324:406fd2029f23 144 *END**************************************************************************/
mbed_official 324:406fd2029f23 145 bool RTC_HAL_IsDatetimeCorrectFormat(const rtc_datetime_t * datetime)
mbed_official 324:406fd2029f23 146 {
mbed_official 324:406fd2029f23 147 bool result = false;
mbed_official 324:406fd2029f23 148
mbed_official 324:406fd2029f23 149 /* Test correctness of given parameters*/
mbed_official 324:406fd2029f23 150 if ((datetime->year < YEAR_RANGE_START) || (datetime->year > YEAR_RANGE_END) ||
mbed_official 324:406fd2029f23 151 (datetime->month > 12U) || (datetime->month < 1U) ||
mbed_official 324:406fd2029f23 152 (datetime->day > 31U) || (datetime->day < 1U) ||
mbed_official 324:406fd2029f23 153 (datetime->hour >= HOURS_IN_A_DAY) || (datetime->minute >= MINS_IN_A_HOUR) ||
mbed_official 324:406fd2029f23 154 (datetime->second >= SECONDS_IN_A_MIN))
mbed_official 324:406fd2029f23 155 {
mbed_official 324:406fd2029f23 156 /* If not correct then error*/
mbed_official 324:406fd2029f23 157 result = false;
mbed_official 324:406fd2029f23 158 }
mbed_official 324:406fd2029f23 159 else
mbed_official 324:406fd2029f23 160 {
mbed_official 324:406fd2029f23 161 result = true;
mbed_official 324:406fd2029f23 162 }
mbed_official 324:406fd2029f23 163
mbed_official 324:406fd2029f23 164 /* Is given year un-leap-one?*/
mbed_official 324:406fd2029f23 165 /* Leap year calculation only looks for years divisible by 4 as acceptable years is limited */
mbed_official 324:406fd2029f23 166 if ( result && (datetime->year & 3U))
mbed_official 324:406fd2029f23 167 {
mbed_official 324:406fd2029f23 168 /* Does the obtained number of days exceed number of days in the appropriate month & year?*/
mbed_official 324:406fd2029f23 169 if (ULY[datetime->month] < datetime->day)
mbed_official 324:406fd2029f23 170 {
mbed_official 324:406fd2029f23 171 /* If yes (incorrect datetime inserted) then error*/
mbed_official 324:406fd2029f23 172 result = false;
mbed_official 324:406fd2029f23 173 }
mbed_official 324:406fd2029f23 174 }
mbed_official 324:406fd2029f23 175 else /* Is given year leap-one?*/
mbed_official 324:406fd2029f23 176 {
mbed_official 324:406fd2029f23 177 /* Does the obtained number of days exceed number of days in the appropriate month & year?*/
mbed_official 324:406fd2029f23 178 if (result && (LY[datetime->month] < datetime->day))
mbed_official 324:406fd2029f23 179 {
mbed_official 324:406fd2029f23 180 /* if yes (incorrect date inserted) then error*/
mbed_official 324:406fd2029f23 181 result = false;
mbed_official 324:406fd2029f23 182 }
mbed_official 324:406fd2029f23 183 }
mbed_official 324:406fd2029f23 184
mbed_official 324:406fd2029f23 185 return result;
mbed_official 324:406fd2029f23 186 }
mbed_official 146:f64d43ff0c18 187
mbed_official 146:f64d43ff0c18 188 /*FUNCTION**********************************************************************
mbed_official 146:f64d43ff0c18 189 *
mbed_official 324:406fd2029f23 190 * Function Name : RTC_HAL_ConvertDatetimeToSecs
mbed_official 324:406fd2029f23 191 * Description : converts time data from datetime to seconds.
mbed_official 324:406fd2029f23 192 * This function will convert time data from datetime to seconds.
mbed_official 146:f64d43ff0c18 193 *
mbed_official 146:f64d43ff0c18 194 *END**************************************************************************/
mbed_official 324:406fd2029f23 195 void RTC_HAL_ConvertDatetimeToSecs(const rtc_datetime_t * datetime, uint32_t * seconds)
mbed_official 146:f64d43ff0c18 196 {
mbed_official 324:406fd2029f23 197 /* Compute number of days from 1970 till given year*/
mbed_official 324:406fd2029f23 198 *seconds = (datetime->year - 1970U) * DAYS_IN_A_YEAR;
mbed_official 324:406fd2029f23 199 /* Add leap year days */
mbed_official 324:406fd2029f23 200 *seconds += ((datetime->year / 4) - (1970U / 4));
mbed_official 324:406fd2029f23 201 /* Add number of days till given month*/
mbed_official 324:406fd2029f23 202 *seconds += MONTH_DAYS[datetime->month];
mbed_official 324:406fd2029f23 203 /* Add days in given month*/
mbed_official 324:406fd2029f23 204 *seconds += datetime->day;
mbed_official 324:406fd2029f23 205 /* For leap year if month less than or equal to Febraury, decrement day counter*/
mbed_official 324:406fd2029f23 206 if ((!(datetime->year & 3U)) && (datetime->month <= 2U))
mbed_official 146:f64d43ff0c18 207 {
mbed_official 324:406fd2029f23 208 (*seconds)--;
mbed_official 146:f64d43ff0c18 209 }
mbed_official 146:f64d43ff0c18 210
mbed_official 324:406fd2029f23 211 *seconds = ((*seconds) * SECONDS_IN_A_DAY) + (datetime->hour * SECONDS_IN_A_HOUR) +
mbed_official 324:406fd2029f23 212 (datetime->minute * SECONDS_IN_A_MIN) + datetime->second;
mbed_official 324:406fd2029f23 213 }
mbed_official 146:f64d43ff0c18 214
mbed_official 324:406fd2029f23 215 /*FUNCTION**********************************************************************
mbed_official 324:406fd2029f23 216 *
mbed_official 324:406fd2029f23 217 * Function Name : RTC_HAL_Enable
mbed_official 324:406fd2029f23 218 * Description : initializes the RTC module.
mbed_official 324:406fd2029f23 219 * This function will initiate a soft-reset of the RTC module to reset
mbed_official 324:406fd2029f23 220 * all the RTC registers. It also enables the RTC oscillator.
mbed_official 324:406fd2029f23 221 *
mbed_official 324:406fd2029f23 222 *END**************************************************************************/
mbed_official 324:406fd2029f23 223 void RTC_HAL_Enable(uint32_t rtcBaseAddr)
mbed_official 324:406fd2029f23 224 {
mbed_official 324:406fd2029f23 225 /* Enable RTC oscillator since it is required to start the counter*/
mbed_official 324:406fd2029f23 226 RTC_HAL_SetOscillatorCmd(rtcBaseAddr, true);
mbed_official 324:406fd2029f23 227 }
mbed_official 324:406fd2029f23 228
mbed_official 324:406fd2029f23 229 void RTC_HAL_Disable(uint32_t rtcBaseAddr)
mbed_official 324:406fd2029f23 230 {
mbed_official 324:406fd2029f23 231 /* Disable counter*/
mbed_official 324:406fd2029f23 232 RTC_HAL_EnableCounter(rtcBaseAddr, false);
mbed_official 146:f64d43ff0c18 233
mbed_official 324:406fd2029f23 234 /* Disable RTC oscillator */
mbed_official 324:406fd2029f23 235 RTC_HAL_SetOscillatorCmd(rtcBaseAddr, false);
mbed_official 324:406fd2029f23 236 }
mbed_official 324:406fd2029f23 237
mbed_official 324:406fd2029f23 238 void RTC_HAL_Init(uint32_t rtcBaseAddr)
mbed_official 324:406fd2029f23 239 {
mbed_official 324:406fd2029f23 240 uint32_t seconds = 0x1;
mbed_official 324:406fd2029f23 241
mbed_official 324:406fd2029f23 242 /* Resets the RTC registers except for the SWR bit */
mbed_official 324:406fd2029f23 243 RTC_HAL_SoftwareReset(rtcBaseAddr);
mbed_official 324:406fd2029f23 244 RTC_HAL_SoftwareResetFlagClear(rtcBaseAddr);
mbed_official 324:406fd2029f23 245
mbed_official 324:406fd2029f23 246 /* Set TSR register to 0x1 to avoid the TIF bit being set in the SR register */
mbed_official 324:406fd2029f23 247 RTC_HAL_SetSecsReg(rtcBaseAddr, seconds);
mbed_official 324:406fd2029f23 248
mbed_official 324:406fd2029f23 249 /* Clear the interrupt enable register */
mbed_official 324:406fd2029f23 250 RTC_HAL_SetSecsIntCmd(rtcBaseAddr, false);
mbed_official 324:406fd2029f23 251 RTC_HAL_SetAlarmIntCmd(rtcBaseAddr, false);
mbed_official 324:406fd2029f23 252 RTC_HAL_SetTimeOverflowIntCmd(rtcBaseAddr, false);
mbed_official 324:406fd2029f23 253 RTC_HAL_SetTimeInvalidIntCmd(rtcBaseAddr, false);
mbed_official 324:406fd2029f23 254 }
mbed_official 324:406fd2029f23 255
mbed_official 324:406fd2029f23 256 void RTC_HAL_SetDatetime(uint32_t rtcBaseAddr, const rtc_datetime_t * datetime)
mbed_official 324:406fd2029f23 257 {
mbed_official 324:406fd2029f23 258 uint32_t seconds;
mbed_official 146:f64d43ff0c18 259
mbed_official 324:406fd2029f23 260 /* Protect against null pointers*/
mbed_official 324:406fd2029f23 261 assert(datetime);
mbed_official 324:406fd2029f23 262
mbed_official 324:406fd2029f23 263 RTC_HAL_ConvertDatetimeToSecs(datetime, &seconds);
mbed_official 324:406fd2029f23 264 /* Set time in seconds */
mbed_official 324:406fd2029f23 265 RTC_HAL_SetDatetimeInsecs(rtcBaseAddr, seconds);
mbed_official 324:406fd2029f23 266 }
mbed_official 324:406fd2029f23 267
mbed_official 324:406fd2029f23 268 void RTC_HAL_SetDatetimeInsecs(uint32_t rtcBaseAddr, const uint32_t seconds)
mbed_official 324:406fd2029f23 269 {
mbed_official 324:406fd2029f23 270 /* Disable counter*/
mbed_official 324:406fd2029f23 271 RTC_HAL_EnableCounter(rtcBaseAddr, false);
mbed_official 324:406fd2029f23 272 /* Set seconds counter*/
mbed_official 324:406fd2029f23 273 RTC_HAL_SetSecsReg(rtcBaseAddr, seconds);
mbed_official 324:406fd2029f23 274 /* Enable the counter*/
mbed_official 324:406fd2029f23 275 RTC_HAL_EnableCounter(rtcBaseAddr, true);
mbed_official 324:406fd2029f23 276 }
mbed_official 324:406fd2029f23 277
mbed_official 324:406fd2029f23 278 void RTC_HAL_GetDatetime(uint32_t rtcBaseAddr, rtc_datetime_t * datetime)
mbed_official 324:406fd2029f23 279 {
mbed_official 324:406fd2029f23 280 uint32_t seconds = 0;
mbed_official 324:406fd2029f23 281
mbed_official 324:406fd2029f23 282 /* Protect against null pointers*/
mbed_official 324:406fd2029f23 283 assert(datetime);
mbed_official 324:406fd2029f23 284
mbed_official 324:406fd2029f23 285 RTC_HAL_GetDatetimeInSecs(rtcBaseAddr, &seconds);
mbed_official 146:f64d43ff0c18 286
mbed_official 324:406fd2029f23 287 RTC_HAL_ConvertSecsToDatetime(&seconds, datetime);
mbed_official 324:406fd2029f23 288 }
mbed_official 324:406fd2029f23 289
mbed_official 324:406fd2029f23 290 void RTC_HAL_GetDatetimeInSecs(uint32_t rtcBaseAddr, uint32_t * seconds)
mbed_official 324:406fd2029f23 291 {
mbed_official 324:406fd2029f23 292 /* Protect against null pointers*/
mbed_official 324:406fd2029f23 293 assert(seconds);
mbed_official 324:406fd2029f23 294 *seconds = RTC_HAL_GetSecsReg(rtcBaseAddr);
mbed_official 324:406fd2029f23 295 }
mbed_official 324:406fd2029f23 296
mbed_official 324:406fd2029f23 297 bool RTC_HAL_SetAlarm(uint32_t rtcBaseAddr, const rtc_datetime_t * date)
mbed_official 324:406fd2029f23 298 {
mbed_official 324:406fd2029f23 299 uint32_t alrm_seconds, curr_seconds;
mbed_official 324:406fd2029f23 300
mbed_official 324:406fd2029f23 301 /* Protect against null pointers*/
mbed_official 324:406fd2029f23 302 assert(date);
mbed_official 324:406fd2029f23 303
mbed_official 324:406fd2029f23 304 RTC_HAL_ConvertDatetimeToSecs(date, &alrm_seconds);
mbed_official 324:406fd2029f23 305
mbed_official 324:406fd2029f23 306 /* Get the current time */
mbed_official 324:406fd2029f23 307 curr_seconds = RTC_HAL_GetSecsReg(rtcBaseAddr);
mbed_official 324:406fd2029f23 308
mbed_official 324:406fd2029f23 309 /* Make sure the alarm is for a future time */
mbed_official 324:406fd2029f23 310 if (alrm_seconds <= curr_seconds)
mbed_official 146:f64d43ff0c18 311 {
mbed_official 324:406fd2029f23 312 return false;
mbed_official 146:f64d43ff0c18 313 }
mbed_official 146:f64d43ff0c18 314
mbed_official 324:406fd2029f23 315 /* set alarm in seconds*/
mbed_official 324:406fd2029f23 316 RTC_HAL_SetAlarmReg(rtcBaseAddr, alrm_seconds);
mbed_official 324:406fd2029f23 317
mbed_official 324:406fd2029f23 318 return true;
mbed_official 324:406fd2029f23 319 }
mbed_official 324:406fd2029f23 320
mbed_official 324:406fd2029f23 321 void RTC_HAL_GetAlarm(uint32_t rtcBaseAddr, rtc_datetime_t * date)
mbed_official 324:406fd2029f23 322 {
mbed_official 324:406fd2029f23 323 uint32_t seconds = 0;
mbed_official 324:406fd2029f23 324
mbed_official 324:406fd2029f23 325 /* Protect against null pointers*/
mbed_official 324:406fd2029f23 326 assert(date);
mbed_official 324:406fd2029f23 327
mbed_official 324:406fd2029f23 328 /* Get alarm in seconds */
mbed_official 324:406fd2029f23 329 seconds = RTC_HAL_GetAlarmReg(rtcBaseAddr);
mbed_official 324:406fd2029f23 330
mbed_official 324:406fd2029f23 331 RTC_HAL_ConvertSecsToDatetime(&seconds, date);
mbed_official 324:406fd2029f23 332 }
mbed_official 324:406fd2029f23 333
mbed_official 324:406fd2029f23 334 #if FSL_FEATURE_RTC_HAS_MONOTONIC
mbed_official 324:406fd2029f23 335
mbed_official 324:406fd2029f23 336 void RTC_HAL_GetMonotonicCounter(uint32_t rtcBaseAddr, uint64_t * counter)
mbed_official 324:406fd2029f23 337 {
mbed_official 324:406fd2029f23 338 uint32_t tmpCountHigh = 0;
mbed_official 324:406fd2029f23 339 uint32_t tmpCountLow = 0;
mbed_official 324:406fd2029f23 340
mbed_official 324:406fd2029f23 341 tmpCountHigh = RTC_HAL_GetMonotonicCounterHigh(rtcBaseAddr);
mbed_official 324:406fd2029f23 342 tmpCountLow = RTC_HAL_GetMonotonicCounterLow(rtcBaseAddr);
mbed_official 324:406fd2029f23 343
mbed_official 324:406fd2029f23 344 *counter = (((uint64_t)(tmpCountHigh) << 32) | ((uint64_t)tmpCountLow));
mbed_official 324:406fd2029f23 345 }
mbed_official 324:406fd2029f23 346
mbed_official 324:406fd2029f23 347 void RTC_HAL_SetMonotonicCounter(uint32_t rtcBaseAddr, const uint64_t * counter)
mbed_official 324:406fd2029f23 348 {
mbed_official 324:406fd2029f23 349 uint32_t tmpCountHigh = 0;
mbed_official 324:406fd2029f23 350 uint32_t tmpCountLow = 0;
mbed_official 324:406fd2029f23 351
mbed_official 324:406fd2029f23 352 tmpCountHigh = (uint32_t)((*counter) >> 32);
mbed_official 324:406fd2029f23 353 RTC_HAL_SetMonotonicCounterHigh(rtcBaseAddr, tmpCountHigh);
mbed_official 324:406fd2029f23 354 tmpCountLow = (uint32_t)(*counter);
mbed_official 324:406fd2029f23 355 RTC_HAL_SetMonotonicCounterLow(rtcBaseAddr, tmpCountLow);
mbed_official 324:406fd2029f23 356 }
mbed_official 324:406fd2029f23 357
mbed_official 324:406fd2029f23 358 bool RTC_HAL_IncrementMonotonicCounter(uint32_t rtcBaseAddr)
mbed_official 324:406fd2029f23 359 {
mbed_official 324:406fd2029f23 360 bool result = false;
mbed_official 324:406fd2029f23 361
mbed_official 324:406fd2029f23 362 if((!(RTC_HAL_IsMonotonicCounterOverflow(rtcBaseAddr))) && (!(RTC_HAL_IsTimeInvalid(rtcBaseAddr))))
mbed_official 146:f64d43ff0c18 363 {
mbed_official 324:406fd2029f23 364 /* prepare for incrementing after write*/
mbed_official 324:406fd2029f23 365 RTC_HAL_SetMonotonicEnableCmd(rtcBaseAddr, true);
mbed_official 324:406fd2029f23 366
mbed_official 324:406fd2029f23 367 /* write anything so the counter increments*/
mbed_official 324:406fd2029f23 368 BW_RTC_MCLR_MCL(rtcBaseAddr, 1U);
mbed_official 324:406fd2029f23 369
mbed_official 324:406fd2029f23 370 result = true;
mbed_official 146:f64d43ff0c18 371 }
mbed_official 146:f64d43ff0c18 372
mbed_official 324:406fd2029f23 373 return result;
mbed_official 324:406fd2029f23 374 }
mbed_official 146:f64d43ff0c18 375
mbed_official 146:f64d43ff0c18 376 #endif
mbed_official 146:f64d43ff0c18 377
mbed_official 146:f64d43ff0c18 378 /*******************************************************************************
mbed_official 146:f64d43ff0c18 379 * EOF
mbed_official 146:f64d43ff0c18 380 ******************************************************************************/
mbed_official 146:f64d43ff0c18 381