Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lypinator 0:bb348c97df44 1 /* mbed Microcontroller Library
lypinator 0:bb348c97df44 2 * Copyright (c) 2017-2017 ARM Limited
lypinator 0:bb348c97df44 3 *
lypinator 0:bb348c97df44 4 * Licensed under the Apache License, Version 2.0 (the "License");
lypinator 0:bb348c97df44 5 * you may not use this file except in compliance with the License.
lypinator 0:bb348c97df44 6 * You may obtain a copy of the License at
lypinator 0:bb348c97df44 7 *
lypinator 0:bb348c97df44 8 * http://www.apache.org/licenses/LICENSE-2.0
lypinator 0:bb348c97df44 9 *
lypinator 0:bb348c97df44 10 * Unless required by applicable law or agreed to in writing, software
lypinator 0:bb348c97df44 11 * distributed under the License is distributed on an "AS IS" BASIS,
lypinator 0:bb348c97df44 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
lypinator 0:bb348c97df44 13 * See the License for the specific language governing permissions and
lypinator 0:bb348c97df44 14 * limitations under the License.
lypinator 0:bb348c97df44 15 */
lypinator 0:bb348c97df44 16
lypinator 0:bb348c97df44 17 #include "mbed_mktime.h"
lypinator 0:bb348c97df44 18
lypinator 0:bb348c97df44 19 /* Time constants. */
lypinator 0:bb348c97df44 20 #define SECONDS_BY_MINUTES 60
lypinator 0:bb348c97df44 21 #define MINUTES_BY_HOUR 60
lypinator 0:bb348c97df44 22 #define SECONDS_BY_HOUR (SECONDS_BY_MINUTES * MINUTES_BY_HOUR)
lypinator 0:bb348c97df44 23 #define HOURS_BY_DAY 24
lypinator 0:bb348c97df44 24 #define SECONDS_BY_DAY (SECONDS_BY_HOUR * HOURS_BY_DAY)
lypinator 0:bb348c97df44 25 #define LAST_VALID_YEAR 206
lypinator 0:bb348c97df44 26
lypinator 0:bb348c97df44 27 /* Macros which will be used to determine if we are within valid range. */
lypinator 0:bb348c97df44 28 #define EDGE_TIMESTAMP_FULL_LEAP_YEAR_SUPPORT 3220095 // 7th of February 1970 at 06:28:15
lypinator 0:bb348c97df44 29 #define EDGE_TIMESTAMP_4_YEAR_LEAP_YEAR_SUPPORT 3133695 // 6th of February 1970 at 06:28:15
lypinator 0:bb348c97df44 30
lypinator 0:bb348c97df44 31 /*
lypinator 0:bb348c97df44 32 * 2 dimensional array containing the number of seconds elapsed before a given
lypinator 0:bb348c97df44 33 * month.
lypinator 0:bb348c97df44 34 * The second index map to the month while the first map to the type of year:
lypinator 0:bb348c97df44 35 * - 0: non leap year
lypinator 0:bb348c97df44 36 * - 1: leap year
lypinator 0:bb348c97df44 37 */
lypinator 0:bb348c97df44 38 static const uint32_t seconds_before_month[2][12] = {
lypinator 0:bb348c97df44 39 {
lypinator 0:bb348c97df44 40 0,
lypinator 0:bb348c97df44 41 31 * SECONDS_BY_DAY,
lypinator 0:bb348c97df44 42 (31 + 28) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 43 (31 + 28 + 31) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 44 (31 + 28 + 31 + 30) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 45 (31 + 28 + 31 + 30 + 31) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 46 (31 + 28 + 31 + 30 + 31 + 30) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 47 (31 + 28 + 31 + 30 + 31 + 30 + 31) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 48 (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 49 (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 50 (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 51 (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 52 },
lypinator 0:bb348c97df44 53 {
lypinator 0:bb348c97df44 54 0,
lypinator 0:bb348c97df44 55 31 * SECONDS_BY_DAY,
lypinator 0:bb348c97df44 56 (31 + 29) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 57 (31 + 29 + 31) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 58 (31 + 29 + 31 + 30) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 59 (31 + 29 + 31 + 30 + 31) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 60 (31 + 29 + 31 + 30 + 31 + 30) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 61 (31 + 29 + 31 + 30 + 31 + 30 + 31) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 62 (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 63 (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 64 (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 65 (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30) *SECONDS_BY_DAY,
lypinator 0:bb348c97df44 66 }
lypinator 0:bb348c97df44 67 };
lypinator 0:bb348c97df44 68
lypinator 0:bb348c97df44 69 bool _rtc_is_leap_year(int year, rtc_leap_year_support_t leap_year_support)
lypinator 0:bb348c97df44 70 {
lypinator 0:bb348c97df44 71 /*
lypinator 0:bb348c97df44 72 * since in practice, the value manipulated by this algorithm lie in the
lypinator 0:bb348c97df44 73 * range: [70 : 206] the algorithm can be reduced to: year % 4 with exception for 200 (year 2100 is not leap year).
lypinator 0:bb348c97df44 74 * The algorithm valid over the full range of value is:
lypinator 0:bb348c97df44 75
lypinator 0:bb348c97df44 76 year = 1900 + year;
lypinator 0:bb348c97df44 77 if (year % 4) {
lypinator 0:bb348c97df44 78 return false;
lypinator 0:bb348c97df44 79 } else if (year % 100) {
lypinator 0:bb348c97df44 80 return true;
lypinator 0:bb348c97df44 81 } else if (year % 400) {
lypinator 0:bb348c97df44 82 return false;
lypinator 0:bb348c97df44 83 }
lypinator 0:bb348c97df44 84 return true;
lypinator 0:bb348c97df44 85
lypinator 0:bb348c97df44 86 */
lypinator 0:bb348c97df44 87 if (leap_year_support == RTC_FULL_LEAP_YEAR_SUPPORT && year == 200) {
lypinator 0:bb348c97df44 88 return false; // 2100 is not a leap year
lypinator 0:bb348c97df44 89 }
lypinator 0:bb348c97df44 90
lypinator 0:bb348c97df44 91 return (year) % 4 ? false : true;
lypinator 0:bb348c97df44 92 }
lypinator 0:bb348c97df44 93
lypinator 0:bb348c97df44 94 bool _rtc_maketime(const struct tm *time, time_t *seconds, rtc_leap_year_support_t leap_year_support)
lypinator 0:bb348c97df44 95 {
lypinator 0:bb348c97df44 96 if (seconds == NULL || time == NULL) {
lypinator 0:bb348c97df44 97 return false;
lypinator 0:bb348c97df44 98 }
lypinator 0:bb348c97df44 99
lypinator 0:bb348c97df44 100 /* Partial check for the upper bound of the range - check years only. Full check will be performed after the
lypinator 0:bb348c97df44 101 * elapsed time since the beginning of the year is calculated.
lypinator 0:bb348c97df44 102 */
lypinator 0:bb348c97df44 103 if ((time->tm_year < 70) || (time->tm_year > LAST_VALID_YEAR)) {
lypinator 0:bb348c97df44 104 return false;
lypinator 0:bb348c97df44 105 }
lypinator 0:bb348c97df44 106
lypinator 0:bb348c97df44 107 uint32_t result = time->tm_sec;
lypinator 0:bb348c97df44 108 result += time->tm_min * SECONDS_BY_MINUTES;
lypinator 0:bb348c97df44 109 result += time->tm_hour * SECONDS_BY_HOUR;
lypinator 0:bb348c97df44 110 result += (time->tm_mday - 1) * SECONDS_BY_DAY;
lypinator 0:bb348c97df44 111 result += seconds_before_month[_rtc_is_leap_year(time->tm_year, leap_year_support)][time->tm_mon];
lypinator 0:bb348c97df44 112
lypinator 0:bb348c97df44 113 /* Check if we are within valid range. */
lypinator 0:bb348c97df44 114 if (time->tm_year == LAST_VALID_YEAR) {
lypinator 0:bb348c97df44 115 if ((leap_year_support == RTC_FULL_LEAP_YEAR_SUPPORT && result > EDGE_TIMESTAMP_FULL_LEAP_YEAR_SUPPORT) ||
lypinator 0:bb348c97df44 116 (leap_year_support == RTC_4_YEAR_LEAP_YEAR_SUPPORT && result > EDGE_TIMESTAMP_4_YEAR_LEAP_YEAR_SUPPORT)) {
lypinator 0:bb348c97df44 117 return false;
lypinator 0:bb348c97df44 118 }
lypinator 0:bb348c97df44 119 }
lypinator 0:bb348c97df44 120
lypinator 0:bb348c97df44 121 if (time->tm_year > 70) {
lypinator 0:bb348c97df44 122 /* Valid in the range [70:206]. */
lypinator 0:bb348c97df44 123 uint32_t count_of_leap_days = ((time->tm_year - 1) / 4) - (70 / 4);
lypinator 0:bb348c97df44 124 if (leap_year_support == RTC_FULL_LEAP_YEAR_SUPPORT) {
lypinator 0:bb348c97df44 125 if (time->tm_year > 200) {
lypinator 0:bb348c97df44 126 count_of_leap_days--; // 2100 is not a leap year
lypinator 0:bb348c97df44 127 }
lypinator 0:bb348c97df44 128 }
lypinator 0:bb348c97df44 129
lypinator 0:bb348c97df44 130 result += (((time->tm_year - 70) * 365) + count_of_leap_days) * SECONDS_BY_DAY;
lypinator 0:bb348c97df44 131 }
lypinator 0:bb348c97df44 132
lypinator 0:bb348c97df44 133 *seconds = result;
lypinator 0:bb348c97df44 134
lypinator 0:bb348c97df44 135 return true;
lypinator 0:bb348c97df44 136 }
lypinator 0:bb348c97df44 137
lypinator 0:bb348c97df44 138 bool _rtc_localtime(time_t timestamp, struct tm *time_info, rtc_leap_year_support_t leap_year_support)
lypinator 0:bb348c97df44 139 {
lypinator 0:bb348c97df44 140 if (time_info == NULL) {
lypinator 0:bb348c97df44 141 return false;
lypinator 0:bb348c97df44 142 }
lypinator 0:bb348c97df44 143
lypinator 0:bb348c97df44 144 uint32_t seconds = (uint32_t)timestamp;
lypinator 0:bb348c97df44 145
lypinator 0:bb348c97df44 146 time_info->tm_sec = seconds % 60;
lypinator 0:bb348c97df44 147 seconds = seconds / 60; // timestamp in minutes
lypinator 0:bb348c97df44 148 time_info->tm_min = seconds % 60;
lypinator 0:bb348c97df44 149 seconds = seconds / 60; // timestamp in hours
lypinator 0:bb348c97df44 150 time_info->tm_hour = seconds % 24;
lypinator 0:bb348c97df44 151 seconds = seconds / 24; // timestamp in days;
lypinator 0:bb348c97df44 152
lypinator 0:bb348c97df44 153 /* Compute the weekday.
lypinator 0:bb348c97df44 154 * The 1st of January 1970 was a Thursday which is equal to 4 in the weekday representation ranging from [0:6].
lypinator 0:bb348c97df44 155 */
lypinator 0:bb348c97df44 156 time_info->tm_wday = (seconds + 4) % 7;
lypinator 0:bb348c97df44 157
lypinator 0:bb348c97df44 158 /* Years start at 70. */
lypinator 0:bb348c97df44 159 time_info->tm_year = 70;
lypinator 0:bb348c97df44 160 while (true) {
lypinator 0:bb348c97df44 161 if (_rtc_is_leap_year(time_info->tm_year, leap_year_support) && seconds >= 366) {
lypinator 0:bb348c97df44 162 ++time_info->tm_year;
lypinator 0:bb348c97df44 163 seconds -= 366;
lypinator 0:bb348c97df44 164 } else if (!_rtc_is_leap_year(time_info->tm_year, leap_year_support) && seconds >= 365) {
lypinator 0:bb348c97df44 165 ++time_info->tm_year;
lypinator 0:bb348c97df44 166 seconds -= 365;
lypinator 0:bb348c97df44 167 } else {
lypinator 0:bb348c97df44 168 /* The remaining days are less than a years. */
lypinator 0:bb348c97df44 169 break;
lypinator 0:bb348c97df44 170 }
lypinator 0:bb348c97df44 171 }
lypinator 0:bb348c97df44 172
lypinator 0:bb348c97df44 173 time_info->tm_yday = seconds;
lypinator 0:bb348c97df44 174
lypinator 0:bb348c97df44 175 /* Convert days into seconds and find the current month. */
lypinator 0:bb348c97df44 176 seconds *= SECONDS_BY_DAY;
lypinator 0:bb348c97df44 177 time_info->tm_mon = 11;
lypinator 0:bb348c97df44 178 bool leap = _rtc_is_leap_year(time_info->tm_year, leap_year_support);
lypinator 0:bb348c97df44 179 for (uint32_t i = 0; i < 12; ++i) {
lypinator 0:bb348c97df44 180 if ((uint32_t) seconds < seconds_before_month[leap][i]) {
lypinator 0:bb348c97df44 181 time_info->tm_mon = i - 1;
lypinator 0:bb348c97df44 182 break;
lypinator 0:bb348c97df44 183 }
lypinator 0:bb348c97df44 184 }
lypinator 0:bb348c97df44 185
lypinator 0:bb348c97df44 186 /* Remove month from timestamp and compute the number of days.
lypinator 0:bb348c97df44 187 * Note: unlike other fields, days are not 0 indexed.
lypinator 0:bb348c97df44 188 */
lypinator 0:bb348c97df44 189 seconds -= seconds_before_month[leap][time_info->tm_mon];
lypinator 0:bb348c97df44 190 time_info->tm_mday = (seconds / SECONDS_BY_DAY) + 1;
lypinator 0:bb348c97df44 191
lypinator 0:bb348c97df44 192 return true;
lypinator 0:bb348c97df44 193 }