|
| 1 | +/* |
| 2 | + This example is based on the Ambiq SDK EVB2 RTC example. |
| 3 | +*/ |
| 4 | + |
| 5 | +#include "RTC.h" |
| 6 | + |
| 7 | +am_hal_rtc_time_t hal_time; |
| 8 | + |
| 9 | +// String arrays to index Days and Months with the values returned by the RTC. |
| 10 | +char const *pcWeekday[] = |
| 11 | + { |
| 12 | + "Sunday", |
| 13 | + "Monday", |
| 14 | + "Tuesday", |
| 15 | + "Wednesday", |
| 16 | + "Thursday", |
| 17 | + "Friday", |
| 18 | + "Saturday", |
| 19 | + "Invalid day"}; |
| 20 | + |
| 21 | +char const *pcMonth[] = |
| 22 | + { |
| 23 | + "January", |
| 24 | + "February", |
| 25 | + "March", |
| 26 | + "April", |
| 27 | + "May", |
| 28 | + "June", |
| 29 | + "July", |
| 30 | + "August", |
| 31 | + "September", |
| 32 | + "October", |
| 33 | + "November", |
| 34 | + "December", |
| 35 | + "Invalid month"}; |
| 36 | + |
| 37 | +//Constructor |
| 38 | +APM3_RTC::APM3_RTC() |
| 39 | +{ |
| 40 | + // Enable the XT for the RTC. |
| 41 | + am_hal_clkgen_control(AM_HAL_CLKGEN_CONTROL_XTAL_START, 0); |
| 42 | + |
| 43 | + // Select XT for RTC clock source |
| 44 | + am_hal_rtc_osc_select(AM_HAL_RTC_OSC_XT); |
| 45 | + |
| 46 | + // Enable the RTC. |
| 47 | + am_hal_rtc_osc_enable(); |
| 48 | +} |
| 49 | + |
| 50 | +void APM3_RTC::setTime(uint8_t hour, uint8_t min, uint8_t sec, uint8_t hund, uint8_t dayOfMonth, uint8_t month, uint16_t year) |
| 51 | +{ |
| 52 | + hal_time.ui32Hour = hour; |
| 53 | + hal_time.ui32Minute = min; |
| 54 | + hal_time.ui32Second = sec; |
| 55 | + hal_time.ui32Hundredths = hund; |
| 56 | + |
| 57 | + hal_time.ui32DayOfMonth = dayOfMonth; |
| 58 | + hal_time.ui32Month = month - 1; //HAL is expecting 0 to 11 months |
| 59 | + hal_time.ui32Year = year; |
| 60 | + hal_time.ui32Century = 0; |
| 61 | + |
| 62 | + hal_time.ui32Weekday = am_util_time_computeDayofWeek(2000 + year, month + 1, dayOfMonth); |
| 63 | + |
| 64 | + am_hal_rtc_time_set(&hal_time); //Initialize the RTC with this date/time |
| 65 | +} |
| 66 | + |
| 67 | +//Takes the time from the last build and uses it as the current time |
| 68 | +//Works well as an arduino sketch |
| 69 | +void APM3_RTC::setToCompilerTime() |
| 70 | +{ |
| 71 | + //Get the current date/time from the compiler |
| 72 | + //Alternatively, you can set these values manually |
| 73 | + hal_time.ui32Hour = toVal(&__TIME__[0]); |
| 74 | + hal_time.ui32Minute = toVal(&__TIME__[3]); |
| 75 | + hal_time.ui32Second = toVal(&__TIME__[6]); |
| 76 | + hal_time.ui32Hundredths = 00; |
| 77 | + hal_time.ui32Weekday = am_util_time_computeDayofWeek(2000 + toVal(&__DATE__[9]), mthToIndex(&__DATE__[0]) + 1, toVal(&__DATE__[4])); |
| 78 | + hal_time.ui32DayOfMonth = toVal(&__DATE__[4]); |
| 79 | + hal_time.ui32Month = mthToIndex(&__DATE__[0]); |
| 80 | + hal_time.ui32Year = toVal(&__DATE__[9]); |
| 81 | + hal_time.ui32Century = 0; |
| 82 | + |
| 83 | + am_hal_rtc_time_set(&hal_time); //Initialize the RTC with this date/time |
| 84 | +} |
| 85 | + |
| 86 | +void APM3_RTC::getTime() |
| 87 | +{ |
| 88 | + am_hal_rtc_time_get(&hal_time); |
| 89 | + |
| 90 | + hour = hal_time.ui32Hour; |
| 91 | + minute = hal_time.ui32Minute; |
| 92 | + seconds = hal_time.ui32Second; |
| 93 | + hundredths = hal_time.ui32Hundredths; |
| 94 | + |
| 95 | + month = hal_time.ui32Month + 1; //Convert from 0-11 to 1-12 |
| 96 | + dayOfMonth = hal_time.ui32DayOfMonth; |
| 97 | + year = hal_time.ui32Year; |
| 98 | + |
| 99 | + weekday = hal_time.ui32Weekday; |
| 100 | + textWeekday = pcWeekday[hal_time.ui32Weekday]; //Given a number (day of week) return the string that represents the name |
| 101 | +} |
| 102 | + |
| 103 | +// mthToIndex() converts a string indicating a month to an index value. |
| 104 | +// The return value is a value 0-12, with 0-11 indicating the month given |
| 105 | +// by the string, and 12 indicating that the string is not a month. |
| 106 | +int APM3_RTC::mthToIndex(char const *pcMon) |
| 107 | +{ |
| 108 | + int idx; |
| 109 | + for (idx = 0; idx < 12; idx++) |
| 110 | + { |
| 111 | + if (am_util_string_strnicmp(pcMonth[idx], pcMon, 3) == 0) |
| 112 | + return idx; |
| 113 | + } |
| 114 | + return 12; //Error |
| 115 | +} |
| 116 | + |
| 117 | +// toVal() converts a string to an ASCII value. |
| 118 | +int APM3_RTC::toVal(char const *pcAsciiStr) |
| 119 | +{ |
| 120 | + int iRetVal = 0; |
| 121 | + iRetVal += pcAsciiStr[1] - '0'; |
| 122 | + iRetVal += pcAsciiStr[0] == ' ' ? 0 : (pcAsciiStr[0] - '0') * 10; |
| 123 | + return iRetVal; |
| 124 | +} |
0 commit comments