Advertisement
Guest User

Untitled

a guest
Mar 11th, 2012
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.79 KB | None | 0 0
  1. commit 96e5eb153e2366e81e170174be7928b8b5b6598e
  2. Author: Bertrik Sikken <bertrik@sikken.nl>
  3. Date:   Sun Mar 11 15:59:55 2012 +0100
  4.  
  5.     Convert IMX233 RTC driver (used in fuze+) to use mktime and gmtime
  6.    
  7.     Change-Id: Ie71d495509a81db5e02078398f5d722c00136072
  8.  
  9. diff --git a/firmware/drivers/rtc/rtc_imx233.c b/firmware/drivers/rtc/rtc_imx233.c
  10. index 9e62476..f9694a5 100644
  11. --- a/firmware/drivers/rtc/rtc_imx233.c
  12. +++ b/firmware/drivers/rtc/rtc_imx233.c
  13. @@ -19,34 +19,14 @@
  14.   *
  15.   ****************************************************************************/
  16.  
  17. +#include "config.h"
  18. +#include "time.h"
  19.  #include "system.h"
  20.  #include "rtc.h"
  21.  #include "timefuncs.h"
  22.  #include "rtc-imx233.h"
  23.  
  24. -#if defined(SANSA_FUZEPLUS)
  25. -#define SECS_ADJUST 315532800   /* seconds between 1970-1-1 and 1980-1-1 */
  26. -#else
  27. -#define SECS_ADJUST 0
  28. -#endif
  29. -
  30. -#define MINUTE_SECONDS      60
  31. -#define HOUR_SECONDS        3600
  32. -#define DAY_SECONDS         86400
  33. -#define WEEK_SECONDS        604800
  34. -#define YEAR_SECONDS        31536000
  35. -#define LEAP_YEAR_SECONDS   31622400
  36. -
  37. -/* Days in each month */
  38. -static unsigned int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  39. -
  40. -static inline bool is_leapyear(int year)
  41. -{
  42. -    if( ((year%4)==0) && (((year%100)!=0) || ((year%400)==0)) )
  43. -        return true;
  44. -    else
  45. -        return false;
  46. -}
  47. +#define YEAR1980    315532800   /* 1980/1/1 00:00:00 in UTC */
  48.  
  49.  void rtc_init(void)
  50.  {
  51. @@ -55,7 +35,7 @@ void rtc_init(void)
  52.  
  53.  int rtc_read_datetime(struct tm *tm)
  54.  {
  55. -    uint32_t seconds = imx233_rtc_read_seconds() - SECS_ADJUST;
  56. +    uint32_t seconds = imx233_rtc_read_seconds();
  57.      #ifdef SANSA_FUZEPLUS
  58.      /* The OF uses PERSISTENT2 register to keep the adjustment and only changes
  59.       * SECONDS if necessary. */
  60. @@ -63,100 +43,19 @@ int rtc_read_datetime(struct tm *tm)
  61.      #else
  62.      /* The Freescale recommended way of keeping time is the number of seconds
  63.       * since 00:00 1/1/1980 */
  64. +    seconds += YEAR1980;
  65.      #endif
  66.  
  67. -    /* Convert seconds since 00:00 1/1/xxxx (xxxx=year) */
  68. -
  69. -    /* weekday */
  70. -    tm->tm_wday = ((seconds % WEEK_SECONDS) / DAY_SECONDS + 2) % 7;
  71. -
  72. -    /* Year */
  73. -    int year = 1980;
  74. -    while(seconds >= LEAP_YEAR_SECONDS)
  75. -    {
  76. -        if(is_leapyear(year))
  77. -            seconds -= LEAP_YEAR_SECONDS;
  78. -        else
  79. -            seconds -= YEAR_SECONDS;
  80. -
  81. -        year++;
  82. -    }
  83. -
  84. -    if(is_leapyear(year))
  85. -        days_in_month[1] = 29;
  86. -    else
  87. -    {
  88. -        days_in_month[1] = 28;
  89. -        if(seconds>YEAR_SECONDS)
  90. -        {
  91. -            year++;
  92. -            seconds -= YEAR_SECONDS;
  93. -        }
  94. -    }
  95. -    tm->tm_year = year % 100 + 100;
  96. -
  97. -    /* Month */
  98. -    for(int i = 0; i < 12; i++)
  99. -    {
  100. -        if(seconds < days_in_month[i] * DAY_SECONDS)
  101. -        {
  102. -            tm->tm_mon = i;
  103. -            break;
  104. -        }
  105. -
  106. -        seconds -= days_in_month[i] * DAY_SECONDS;
  107. -    }
  108. -
  109. -    /* Month Day */
  110. -    int mday = seconds / DAY_SECONDS;
  111. -    seconds -= mday * DAY_SECONDS;
  112. -    tm->tm_mday = mday + 1; /* 1 ... 31 */
  113. -
  114. -    /* Hour */
  115. -    int hour = seconds / HOUR_SECONDS;
  116. -    seconds -= hour*HOUR_SECONDS;
  117. -    tm->tm_hour = hour;
  118. -
  119. -    /* Minute */
  120. -    int min = seconds / MINUTE_SECONDS;
  121. -    seconds -= min*MINUTE_SECONDS;
  122. -    tm->tm_min = min;
  123. -
  124. -    /* Second */
  125. -    tm->tm_sec = seconds;
  126. +    gmtime_r(&seconds, tm);
  127.  
  128.      return 0;
  129.  }
  130.  
  131.  int rtc_write_datetime(const struct tm *tm)
  132.  {
  133. -    int i, year;
  134. -    unsigned int year_days = 0;
  135. -    unsigned int month_days = 0;
  136. -    unsigned int seconds = 0;
  137. -
  138. -    year = 2000 + tm->tm_year - 100;
  139. -
  140. -    if(is_leapyear(year))
  141. -        days_in_month[1] = 29;
  142. -    else
  143. -        days_in_month[1] = 28;
  144. -
  145. -    /* Number of days in months gone by this year*/
  146. -    for(i = 0; i < tm->tm_mon; i++)
  147. -        month_days += days_in_month[i];
  148. -
  149. -    /* Number of days in years gone by since 1-Jan-1980 */
  150. -    year_days = 365*(tm->tm_year-100+20) + (tm->tm_year-100-1)/4 + 6;
  151. -
  152. -    /* Convert to seconds since 1-Jan-1980 */
  153. -    seconds = tm->tm_sec
  154. -            + tm->tm_min*MINUTE_SECONDS
  155. -            + tm->tm_hour*HOUR_SECONDS
  156. -            + (tm->tm_mday-1)*DAY_SECONDS
  157. -            + month_days*DAY_SECONDS
  158. -            + year_days*DAY_SECONDS;
  159. -    seconds += SECS_ADJUST;
  160. +    uint32_t seconds;
  161. +    
  162. +    seconds = mktime((struct tm *)tm);
  163.  
  164.      #ifdef SANSA_FUZEPLUS
  165.      /* The OF uses PERSISTENT2 register to keep the adjustment and only changes
  166. @@ -168,8 +67,9 @@ int rtc_write_datetime(const struct tm *tm)
  167.      #else
  168.      /* The Freescale recommended way of keeping time is the number of seconds
  169.       * since 00:00 1/1/1980 */
  170. -    imx233_rtc_write_seconds(seconds);
  171. +    imx233_rtc_write_seconds(seconds - YEAR1980);
  172.      #endif
  173. +
  174.      return 0;
  175.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement