fsl_rtc.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_rtc.h"
  35. /*******************************************************************************
  36. * Definitions
  37. ******************************************************************************/
  38. /* Component ID definition, used by tools. */
  39. #ifndef FSL_COMPONENT_ID
  40. #define FSL_COMPONENT_ID "platform.drivers.lpc_rtc"
  41. #endif
  42. #define SECONDS_IN_A_DAY (86400U)
  43. #define SECONDS_IN_A_HOUR (3600U)
  44. #define SECONDS_IN_A_MINUTE (60U)
  45. #define DAYS_IN_A_YEAR (365U)
  46. #define YEAR_RANGE_START (1970U)
  47. #define YEAR_RANGE_END (2099U)
  48. /*******************************************************************************
  49. * Prototypes
  50. ******************************************************************************/
  51. /*!
  52. * @brief Checks whether the date and time passed in is valid
  53. *
  54. * @param datetime Pointer to structure where the date and time details are stored
  55. *
  56. * @return Returns false if the date & time details are out of range; true if in range
  57. */
  58. static bool RTC_CheckDatetimeFormat(const rtc_datetime_t *datetime);
  59. /*!
  60. * @brief Converts time data from datetime to seconds
  61. *
  62. * @param datetime Pointer to datetime structure where the date and time details are stored
  63. *
  64. * @return The result of the conversion in seconds
  65. */
  66. static uint32_t RTC_ConvertDatetimeToSeconds(const rtc_datetime_t *datetime);
  67. /*!
  68. * @brief Converts time data from seconds to a datetime structure
  69. *
  70. * @param seconds Seconds value that needs to be converted to datetime format
  71. * @param datetime Pointer to the datetime structure where the result of the conversion is stored
  72. */
  73. static void RTC_ConvertSecondsToDatetime(uint32_t seconds, rtc_datetime_t *datetime);
  74. /*******************************************************************************
  75. * Code
  76. ******************************************************************************/
  77. static bool RTC_CheckDatetimeFormat(const rtc_datetime_t *datetime)
  78. {
  79. assert(datetime);
  80. /* Table of days in a month for a non leap year. First entry in the table is not used,
  81. * valid months start from 1
  82. */
  83. uint8_t daysPerMonth[] = {0U, 31U, 28U, 31U, 30U, 31U, 30U, 31U, 31U, 30U, 31U, 30U, 31U};
  84. /* Check year, month, hour, minute, seconds */
  85. if ((datetime->year < YEAR_RANGE_START) || (datetime->year > YEAR_RANGE_END) || (datetime->month > 12U) ||
  86. (datetime->month < 1U) || (datetime->hour >= 24U) || (datetime->minute >= 60U) || (datetime->second >= 60U))
  87. {
  88. /* If not correct then error*/
  89. return false;
  90. }
  91. /* Adjust the days in February for a leap year */
  92. if ((((datetime->year & 3U) == 0) && (datetime->year % 100 != 0)) || (datetime->year % 400 == 0))
  93. {
  94. daysPerMonth[2] = 29U;
  95. }
  96. /* Check the validity of the day */
  97. if ((datetime->day > daysPerMonth[datetime->month]) || (datetime->day < 1U))
  98. {
  99. return false;
  100. }
  101. return true;
  102. }
  103. static uint32_t RTC_ConvertDatetimeToSeconds(const rtc_datetime_t *datetime)
  104. {
  105. assert(datetime);
  106. /* Number of days from begin of the non Leap-year*/
  107. /* Number of days from begin of the non Leap-year*/
  108. uint16_t monthDays[] = {0U, 0U, 31U, 59U, 90U, 120U, 151U, 181U, 212U, 243U, 273U, 304U, 334U};
  109. uint32_t seconds;
  110. /* Compute number of days from 1970 till given year*/
  111. seconds = (datetime->year - 1970U) * DAYS_IN_A_YEAR;
  112. /* Add leap year days */
  113. seconds += ((datetime->year / 4) - (1970U / 4));
  114. /* Add number of days till given month*/
  115. seconds += monthDays[datetime->month];
  116. /* Add days in given month. We subtract the current day as it is
  117. * represented in the hours, minutes and seconds field*/
  118. seconds += (datetime->day - 1);
  119. /* For leap year if month less than or equal to Febraury, decrement day counter*/
  120. if ((!(datetime->year & 3U)) && (datetime->month <= 2U))
  121. {
  122. seconds--;
  123. }
  124. seconds = (seconds * SECONDS_IN_A_DAY) + (datetime->hour * SECONDS_IN_A_HOUR) +
  125. (datetime->minute * SECONDS_IN_A_MINUTE) + datetime->second;
  126. return seconds;
  127. }
  128. static void RTC_ConvertSecondsToDatetime(uint32_t seconds, rtc_datetime_t *datetime)
  129. {
  130. assert(datetime);
  131. uint32_t x;
  132. uint32_t secondsRemaining, days;
  133. uint16_t daysInYear;
  134. /* Table of days in a month for a non leap year. First entry in the table is not used,
  135. * valid months start from 1
  136. */
  137. uint8_t daysPerMonth[] = {0U, 31U, 28U, 31U, 30U, 31U, 30U, 31U, 31U, 30U, 31U, 30U, 31U};
  138. /* Start with the seconds value that is passed in to be converted to date time format */
  139. secondsRemaining = seconds;
  140. /* Calcuate the number of days, we add 1 for the current day which is represented in the
  141. * hours and seconds field
  142. */
  143. days = secondsRemaining / SECONDS_IN_A_DAY + 1;
  144. /* Update seconds left*/
  145. secondsRemaining = secondsRemaining % SECONDS_IN_A_DAY;
  146. /* Calculate the datetime hour, minute and second fields */
  147. datetime->hour = secondsRemaining / SECONDS_IN_A_HOUR;
  148. secondsRemaining = secondsRemaining % SECONDS_IN_A_HOUR;
  149. datetime->minute = secondsRemaining / 60U;
  150. datetime->second = secondsRemaining % SECONDS_IN_A_MINUTE;
  151. /* Calculate year */
  152. daysInYear = DAYS_IN_A_YEAR;
  153. datetime->year = YEAR_RANGE_START;
  154. while (days > daysInYear)
  155. {
  156. /* Decrease day count by a year and increment year by 1 */
  157. days -= daysInYear;
  158. datetime->year++;
  159. /* Adjust the number of days for a leap year */
  160. if (datetime->year & 3U)
  161. {
  162. daysInYear = DAYS_IN_A_YEAR;
  163. }
  164. else
  165. {
  166. daysInYear = DAYS_IN_A_YEAR + 1;
  167. }
  168. }
  169. /* Adjust the days in February for a leap year */
  170. if (!(datetime->year & 3U))
  171. {
  172. daysPerMonth[2] = 29U;
  173. }
  174. for (x = 1U; x <= 12U; x++)
  175. {
  176. if (days <= daysPerMonth[x])
  177. {
  178. datetime->month = x;
  179. break;
  180. }
  181. else
  182. {
  183. days -= daysPerMonth[x];
  184. }
  185. }
  186. datetime->day = days;
  187. }
  188. void RTC_Init(RTC_Type *base)
  189. {
  190. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  191. /* Enable the RTC peripheral clock */
  192. CLOCK_EnableClock(kCLOCK_Rtc);
  193. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  194. /* Make sure the reset bit is cleared */
  195. base->CTRL &= ~RTC_CTRL_SWRESET_MASK;
  196. #if !(defined(FSL_FEATURE_RTC_HAS_NO_OSC_PD) && FSL_FEATURE_RTC_HAS_NO_OSC_PD)
  197. /* Make sure the RTC OSC is powered up */
  198. base->CTRL &= ~RTC_CTRL_RTC_OSC_PD_MASK;
  199. #endif
  200. }
  201. status_t RTC_SetDatetime(RTC_Type *base, const rtc_datetime_t *datetime)
  202. {
  203. assert(datetime);
  204. /* Return error if the time provided is not valid */
  205. if (!(RTC_CheckDatetimeFormat(datetime)))
  206. {
  207. return kStatus_InvalidArgument;
  208. }
  209. /* Set time in seconds */
  210. base->COUNT = RTC_ConvertDatetimeToSeconds(datetime);
  211. return kStatus_Success;
  212. }
  213. void RTC_GetDatetime(RTC_Type *base, rtc_datetime_t *datetime)
  214. {
  215. assert(datetime);
  216. uint32_t seconds = 0;
  217. seconds = base->COUNT;
  218. RTC_ConvertSecondsToDatetime(seconds, datetime);
  219. }
  220. status_t RTC_SetAlarm(RTC_Type *base, const rtc_datetime_t *alarmTime)
  221. {
  222. assert(alarmTime);
  223. uint32_t alarmSeconds = 0;
  224. uint32_t currSeconds = 0;
  225. /* Return error if the alarm time provided is not valid */
  226. if (!(RTC_CheckDatetimeFormat(alarmTime)))
  227. {
  228. return kStatus_InvalidArgument;
  229. }
  230. alarmSeconds = RTC_ConvertDatetimeToSeconds(alarmTime);
  231. /* Get the current time */
  232. currSeconds = base->COUNT;
  233. /* Return error if the alarm time has passed */
  234. if (alarmSeconds < currSeconds)
  235. {
  236. return kStatus_Fail;
  237. }
  238. /* Set alarm in seconds*/
  239. base->MATCH = alarmSeconds;
  240. return kStatus_Success;
  241. }
  242. void RTC_GetAlarm(RTC_Type *base, rtc_datetime_t *datetime)
  243. {
  244. assert(datetime);
  245. uint32_t alarmSeconds = 0;
  246. /* Get alarm in seconds */
  247. alarmSeconds = base->MATCH;
  248. RTC_ConvertSecondsToDatetime(alarmSeconds, datetime);
  249. }