|
| 1 | +/* |
| 2 | + RTC library for Arduino Zero. |
| 3 | + Copyright (c) 2015 Arduino LLC. All right reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +#include "RTCZero.h" |
| 21 | + |
| 22 | +static bool __time24 = false; |
| 23 | + |
| 24 | +void RTCZero::begin(bool timeRep) |
| 25 | +{ |
| 26 | + uint16_t tmp_reg = 0; |
| 27 | + |
| 28 | + if (timeRep) |
| 29 | + __time24 = true; // 24h representation chosen |
| 30 | + |
| 31 | + PM->APBAMASK.reg |= PM_APBAMASK_RTC; // turn on digital interface clock |
| 32 | + config32kOSC(); |
| 33 | + |
| 34 | + // Setup clock GCLK2 with OSC32K divided by 32 |
| 35 | + GCLK->GENDIV.reg = GCLK_GENDIV_ID(2)|GCLK_GENDIV_DIV(4); |
| 36 | + while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY) |
| 37 | + ; |
| 38 | + GCLK->GENCTRL.reg = (GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K | GCLK_GENCTRL_ID(2) | GCLK_GENCTRL_DIVSEL ); |
| 39 | + while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY) |
| 40 | + ; |
| 41 | + GCLK->CLKCTRL.reg = (uint32_t)((GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK2 | (RTC_GCLK_ID << GCLK_CLKCTRL_ID_Pos))); |
| 42 | + while (GCLK->STATUS.bit.SYNCBUSY) |
| 43 | + ; |
| 44 | + |
| 45 | + RTCdisable(); |
| 46 | + |
| 47 | + RTCreset(); |
| 48 | + |
| 49 | + tmp_reg |= RTC_MODE2_CTRL_MODE_CLOCK; // set clock operating mode |
| 50 | + tmp_reg |= RTC_MODE2_CTRL_PRESCALER_DIV1024; // set prescaler to 1024 for MODE2 |
| 51 | + tmp_reg &= ~RTC_MODE2_CTRL_MATCHCLR; // disable clear on match |
| 52 | + |
| 53 | + if (timeRep) |
| 54 | + tmp_reg |= RTC_MODE2_CTRL_CLKREP; // 24h time representation |
| 55 | + else |
| 56 | + tmp_reg &= ~RTC_MODE2_CTRL_CLKREP; // 12h time representation |
| 57 | + |
| 58 | + RTC->MODE2.READREQ.reg &= ~RTC_READREQ_RCONT; // disable continuously mode |
| 59 | + |
| 60 | + RTC->MODE2.CTRL.reg = tmp_reg; |
| 61 | + while (RTCisSyncing()) |
| 62 | + ; |
| 63 | + |
| 64 | + RTCenable(); |
| 65 | + RTCresetRemove(); |
| 66 | +} |
| 67 | + |
| 68 | +/* |
| 69 | + * Get Functions |
| 70 | + */ |
| 71 | + |
| 72 | +uint8_t RTCZero::getSeconds() |
| 73 | +{ |
| 74 | + return RTC->MODE2.CLOCK.bit.SECOND; |
| 75 | +} |
| 76 | + |
| 77 | +uint8_t RTCZero::getMinutes() |
| 78 | +{ |
| 79 | + return RTC->MODE2.CLOCK.bit.MINUTE; |
| 80 | +} |
| 81 | + |
| 82 | +uint8_t RTCZero::getHours() |
| 83 | +{ |
| 84 | + return RTC->MODE2.CLOCK.bit.HOUR; |
| 85 | +} |
| 86 | + |
| 87 | +uint8_t RTCZero::getDay() |
| 88 | +{ |
| 89 | + return RTC->MODE2.CLOCK.bit.DAY; |
| 90 | +} |
| 91 | + |
| 92 | +uint8_t RTCZero::getMonth() |
| 93 | +{ |
| 94 | + return RTC->MODE2.CLOCK.bit.MONTH; |
| 95 | +} |
| 96 | + |
| 97 | +uint8_t RTCZero::getYear() |
| 98 | +{ |
| 99 | + return RTC->MODE2.CLOCK.bit.YEAR; |
| 100 | +} |
| 101 | + |
| 102 | +/* |
| 103 | + * Set Functions |
| 104 | + */ |
| 105 | + |
| 106 | +void RTCZero::setSeconds(uint8_t seconds) |
| 107 | +{ |
| 108 | + RTC->MODE2.CLOCK.bit.SECOND = seconds; |
| 109 | + while (RTCisSyncing()) |
| 110 | + ; |
| 111 | +} |
| 112 | + |
| 113 | +void RTCZero::setMinutes(uint8_t minutes) |
| 114 | +{ |
| 115 | + RTC->MODE2.CLOCK.bit.MINUTE = minutes; |
| 116 | + while (RTCisSyncing()) |
| 117 | + ; |
| 118 | +} |
| 119 | + |
| 120 | +void RTCZero::setHours(uint8_t hours) |
| 121 | +{ |
| 122 | + if (__time24) { |
| 123 | + RTC->MODE2.CLOCK.bit.HOUR = hours; |
| 124 | + } else { |
| 125 | + RTC->MODE2.CLOCK.bit.HOUR = hours - 12; |
| 126 | + } |
| 127 | + while (RTCisSyncing()) |
| 128 | + ; |
| 129 | +} |
| 130 | + |
| 131 | +void RTCZero::setTime(uint8_t hours, uint8_t minutes, uint8_t seconds) |
| 132 | +{ |
| 133 | + setSeconds(seconds); |
| 134 | + setMinutes(minutes); |
| 135 | + setHours(hours); |
| 136 | +} |
| 137 | + |
| 138 | +void RTCZero::setDay(uint8_t day) |
| 139 | +{ |
| 140 | + RTC->MODE2.CLOCK.bit.DAY = day; |
| 141 | + while (RTCisSyncing()) |
| 142 | + ; |
| 143 | +} |
| 144 | + |
| 145 | +void RTCZero::setMonth(uint8_t month) |
| 146 | +{ |
| 147 | + RTC->MODE2.CLOCK.bit.MONTH = month; |
| 148 | + while (RTCisSyncing()) |
| 149 | + ; |
| 150 | +} |
| 151 | + |
| 152 | +void RTCZero::setYear(uint8_t year) |
| 153 | +{ |
| 154 | + RTC->MODE2.CLOCK.bit.YEAR = year; |
| 155 | + while (RTCisSyncing()) |
| 156 | + ; |
| 157 | +} |
| 158 | + |
| 159 | +void RTCZero::setDate(uint8_t day, uint8_t month, uint8_t year) |
| 160 | +{ |
| 161 | + setDay(day); |
| 162 | + setMonth(month); |
| 163 | + setYear(year); |
| 164 | +} |
| 165 | + |
| 166 | +/* |
| 167 | + * Private Utility Functions |
| 168 | + */ |
| 169 | + |
| 170 | +/* Configure the 32768Hz Oscillator */ |
| 171 | +void RTCZero::config32kOSC() |
| 172 | +{ |
| 173 | + SYSCTRL->XOSC32K.reg = SYSCTRL_XOSC32K_ONDEMAND | |
| 174 | + SYSCTRL_XOSC32K_EN32K | |
| 175 | + SYSCTRL_XOSC32K_XTALEN | |
| 176 | + SYSCTRL_XOSC32K_STARTUP(6) | |
| 177 | + SYSCTRL_XOSC32K_ENABLE; |
| 178 | +} |
| 179 | + |
| 180 | +/* Wait for sync in write operations */ |
| 181 | +bool RTCZero::RTCisSyncing() |
| 182 | +{ |
| 183 | + return (RTC->MODE2.STATUS.bit.SYNCBUSY); |
| 184 | +} |
| 185 | + |
| 186 | +void RTCZero::RTCdisable() |
| 187 | +{ |
| 188 | + RTC->MODE2.CTRL.reg &= ~RTC_MODE2_CTRL_ENABLE; // disable RTC |
| 189 | + while (RTCisSyncing()) |
| 190 | + ; |
| 191 | +} |
| 192 | + |
| 193 | +void RTCZero::RTCenable() |
| 194 | +{ |
| 195 | + RTC->MODE2.CTRL.reg |= RTC_MODE2_CTRL_ENABLE; // enable RTC |
| 196 | + while (RTCisSyncing()) |
| 197 | + ; |
| 198 | +} |
| 199 | + |
| 200 | +void RTCZero::RTCreset() |
| 201 | +{ |
| 202 | + RTC->MODE2.CTRL.reg |= RTC_MODE2_CTRL_SWRST; // software reset |
| 203 | + while (RTCisSyncing()) |
| 204 | + ; |
| 205 | +} |
| 206 | + |
| 207 | +void RTCZero::RTCresetRemove() |
| 208 | +{ |
| 209 | + RTC->MODE2.CTRL.reg &= ~RTC_MODE2_CTRL_SWRST; // software reset remove |
| 210 | + while (RTCisSyncing()) |
| 211 | + ; |
| 212 | +} |
| 213 | + |
0 commit comments