[352] | 1 | /* mbed Microcontroller Library
|
---|
| 2 | * Copyright (c) 2006-2015 ARM Limited
|
---|
| 3 | *
|
---|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 5 | * you may not use this file except in compliance with the License.
|
---|
| 6 | * You may obtain a copy of the License at
|
---|
| 7 | *
|
---|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 9 | *
|
---|
| 10 | * Unless required by applicable law or agreed to in writing, software
|
---|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 13 | * See the License for the specific language governing permissions and
|
---|
| 14 | * limitations under the License.
|
---|
| 15 | */
|
---|
| 16 |
|
---|
| 17 | #include "mbed_assert.h"
|
---|
| 18 | #include "device.h"
|
---|
| 19 |
|
---|
| 20 | #if DEVICE_RTC
|
---|
| 21 |
|
---|
| 22 | #include "rtc_api.h"
|
---|
[374] | 23 | #include "iodefine.h"
|
---|
| 24 | #include "mbed_drv_cfg.h"
|
---|
| 25 | #include "mbed_mktime.h"
|
---|
[352] | 26 |
|
---|
| 27 | #define RCR1_VAL_ON (0x08u) // AIE = 1
|
---|
| 28 | #define RCR1_VAL_OFF (0x00u)
|
---|
[374] | 29 | #define RCR3_VAL (0x00u)
|
---|
| 30 | #define RCR5_VAL (0x00u)
|
---|
| 31 |
|
---|
| 32 | #ifdef USE_RTCX1_CLK
|
---|
| 33 | #define RCR2_VAL_ALLSTOP (0x08u)
|
---|
| 34 | #define RCR2_VAL_START (0x09u) // START = 1
|
---|
| 35 | #define RCR2_VAL_RESET (0x0Au) // RESET = 1
|
---|
| 36 | #define RCR5_VAL_RTCX1 (0x00u) // RCKSEL = clock rtc from RTCX1(32.768 kHz)
|
---|
| 37 | #elif defined(USE_EXTAL_CLK)
|
---|
[352] | 38 | #define RCR2_VAL_ALLSTOP (0x00u)
|
---|
| 39 | #define RCR2_VAL_START (0x01u) // START = 1
|
---|
| 40 | #define RCR2_VAL_RESET (0x02u) // RESET = 1
|
---|
[374] | 41 | #define RCR5_VAL_EXTAL (0x01u) // RCKSEL = clock rtc from EXTAL
|
---|
[352] | 42 | #define RFRH_VAL_13333 (0x8003u) // 13.3333MHz (= 64Hz * 0x32DCD)
|
---|
| 43 | #define RFRL_VAL_13333 (0x2DCDu) //
|
---|
[374] | 44 | #elif defined(USE_RTCX3_CLK)
|
---|
| 45 | #define RCR2_VAL_ALLSTOP (0x08u)
|
---|
| 46 | #define RCR2_VAL_START (0x09u) // START = 1
|
---|
| 47 | #define RCR2_VAL_RESET (0x0Au) // RESET = 1
|
---|
| 48 | #define RCR5_VAL_RTCX3 (0x02u) // RCKSEL = clock rtc from RTCX3(4.000 MHz)
|
---|
| 49 | #define RFRH_VAL_4000 (0x8000u) // 4.000MHz (= 64Hz * 0xF424)
|
---|
| 50 | #define RFRL_VAL_4000 (0xF424u) //
|
---|
| 51 | #else
|
---|
| 52 | #error Select RTC clock input !
|
---|
| 53 | #endif
|
---|
| 54 |
|
---|
[352] | 55 | #define RFRH_VAL_MAX (0x0007u) // MAX value (= 128Hz * 0x7FFFF)
|
---|
| 56 | #define RFRL_VAL_MAX (0xFFFFu) //
|
---|
| 57 |
|
---|
| 58 | #define MASK_00_03_POS (0x000Fu)
|
---|
| 59 | #define MASK_04_07_POS (0x00F0u)
|
---|
| 60 | #define MASK_08_11_POS (0x0F00u)
|
---|
| 61 | #define MASK_12_15_POS (0xF000u)
|
---|
| 62 | #define MASK_16_20_POS (0x000F0000u)
|
---|
| 63 | #define SHIFT_1_HBYTE (4u)
|
---|
| 64 | #define SHIFT_2_HBYTE (8u)
|
---|
| 65 | #define SHIFT_3_HBYTE (12u)
|
---|
| 66 | #define SHIFT_1BYTE (8u)
|
---|
| 67 | #define SHIFT_2BYTE (16u)
|
---|
| 68 |
|
---|
[374] | 69 | #define TIME_ERROR_VAL (0u)
|
---|
[352] | 70 |
|
---|
| 71 | static int rtc_dec8_to_hex(uint8_t dec_val, uint8_t offset, int *hex_val);
|
---|
| 72 | static int rtc_dec16_to_hex(uint16_t dec_val, uint16_t offset, int *hex_val);
|
---|
| 73 | static uint8_t rtc_hex8_to_dec(uint8_t hex_val);
|
---|
| 74 | static uint16_t rtc_hex16_to_dec(uint16_t hex_val);
|
---|
| 75 |
|
---|
| 76 |
|
---|
| 77 | /*
|
---|
| 78 | * Setup the RTC based on a time structure.
|
---|
| 79 | * The rtc_init function should be executed first.
|
---|
| 80 | * [in]
|
---|
| 81 | * None.
|
---|
| 82 | * [out]
|
---|
| 83 | * None.
|
---|
| 84 | */
|
---|
[374] | 85 | void rtc_init(void)
|
---|
| 86 | {
|
---|
[352] | 87 | volatile uint8_t dummy_read;
|
---|
| 88 |
|
---|
[374] | 89 | CPG.STBCR6 &= ~(CPG_STBCR6_BIT_MSTP60);
|
---|
| 90 |
|
---|
[352] | 91 | // Set control register
|
---|
| 92 | RTC.RCR2 = RCR2_VAL_ALLSTOP;
|
---|
| 93 | RTC.RCR1 = RCR1_VAL_ON;
|
---|
| 94 | RTC.RCR3 = RCR3_VAL;
|
---|
[374] | 95 |
|
---|
| 96 | #ifdef USE_RTCX1_CLK
|
---|
| 97 | RTC.RCR5 = RCR5_VAL_RTCX1;
|
---|
| 98 | RTC.RFRH = 0;
|
---|
| 99 | RTC.RFRL = 0;
|
---|
| 100 | #elif defined(USE_EXTAL_CLK)
|
---|
[352] | 101 | RTC.RCR5 = RCR5_VAL_EXTAL;
|
---|
| 102 | RTC.RFRH = RFRH_VAL_13333;
|
---|
| 103 | RTC.RFRL = RFRL_VAL_13333;
|
---|
[374] | 104 | #else
|
---|
| 105 | RTC.RCR5 = RCR5_VAL_RTCX3;
|
---|
| 106 | RTC.RFRH = RFRH_VAL_4000;
|
---|
| 107 | RTC.RFRL = RFRL_VAL_4000;
|
---|
| 108 | #endif
|
---|
[352] | 109 | // Dummy read
|
---|
| 110 | dummy_read = RTC.RCR2;
|
---|
| 111 | dummy_read = RTC.RCR2;
|
---|
| 112 |
|
---|
[374] | 113 | RTC.RCR2 = RCR2_VAL_START; // SRART = 1
|
---|
[352] | 114 |
|
---|
| 115 | // Dummy read
|
---|
| 116 | dummy_read = RTC.RCR2;
|
---|
| 117 | dummy_read = RTC.RCR2;
|
---|
| 118 |
|
---|
[374] | 119 | (void)dummy_read;
|
---|
[352] | 120 |
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 |
|
---|
| 124 | /*
|
---|
| 125 | * Release the RTC based on a time structure.
|
---|
[374] | 126 | * @note This function does not stop the RTC from counting
|
---|
[352] | 127 | * [in]
|
---|
| 128 | * None.
|
---|
| 129 | * [out]
|
---|
| 130 | * None.
|
---|
| 131 | */
|
---|
[374] | 132 | void rtc_free(void)
|
---|
| 133 | {
|
---|
[352] | 134 | }
|
---|
| 135 |
|
---|
| 136 |
|
---|
| 137 | /*
|
---|
| 138 | * Check the RTC has been enabled.
|
---|
| 139 | * Clock Control Register RTC.RCR1(bit3): 0 = Disabled, 1 = Enabled.
|
---|
| 140 | * [in]
|
---|
| 141 | * None.
|
---|
| 142 | * [out]
|
---|
| 143 | * 0:Disabled, 1:Enabled.
|
---|
| 144 | */
|
---|
[374] | 145 | int rtc_isenabled(void)
|
---|
| 146 | {
|
---|
[352] | 147 | int ret_val = 0;
|
---|
| 148 |
|
---|
| 149 | if ((RTC.RCR1 & RCR1_VAL_ON) != 0) { // RTC ON ?
|
---|
| 150 | ret_val = 1;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | return ret_val;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 | /*
|
---|
| 158 | * RTC read function.
|
---|
| 159 | * [in]
|
---|
| 160 | * None.
|
---|
| 161 | * [out]
|
---|
| 162 | * UNIX timestamp value.
|
---|
| 163 | */
|
---|
[374] | 164 | time_t rtc_read(void)
|
---|
| 165 | {
|
---|
[352] | 166 |
|
---|
| 167 | struct tm timeinfo;
|
---|
| 168 | int err = 0;
|
---|
| 169 | uint8_t tmp_regdata;
|
---|
| 170 | time_t t;
|
---|
| 171 |
|
---|
| 172 | if (rtc_isenabled() != 0) {
|
---|
| 173 | RTC.RCR1 &= ~0x10u; // CIE = 0
|
---|
| 174 | do {
|
---|
| 175 | // before reading process
|
---|
| 176 | tmp_regdata = RTC.RCR1;
|
---|
| 177 | tmp_regdata &= ~0x80u; // CF = 0
|
---|
| 178 | tmp_regdata |= 0x01u; // AF = 1
|
---|
| 179 | RTC.RCR1 = tmp_regdata;
|
---|
| 180 |
|
---|
| 181 | // Read RTC register
|
---|
| 182 | err = rtc_dec8_to_hex(RTC.RSECCNT , 0 , &timeinfo.tm_sec);
|
---|
| 183 | err += rtc_dec8_to_hex(RTC.RMINCNT , 0 , &timeinfo.tm_min);
|
---|
| 184 | err += rtc_dec8_to_hex(RTC.RHRCNT , 0 , &timeinfo.tm_hour);
|
---|
| 185 | err += rtc_dec8_to_hex(RTC.RDAYCNT , 0 , &timeinfo.tm_mday);
|
---|
| 186 | err += rtc_dec8_to_hex(RTC.RMONCNT , 1 , &timeinfo.tm_mon);
|
---|
| 187 | err += rtc_dec16_to_hex(RTC.RYRCNT , 1900 , &timeinfo.tm_year);
|
---|
| 188 | } while ((RTC.RCR1 & 0x80u) != 0);
|
---|
| 189 | } else {
|
---|
| 190 | err = 1;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | if (err == 0) {
|
---|
| 194 | // Convert to timestamp
|
---|
[374] | 195 | if (_rtc_maketime(&timeinfo, &t, RTC_FULL_LEAP_YEAR_SUPPORT) == false) {
|
---|
| 196 | return TIME_ERROR_VAL;
|
---|
| 197 | }
|
---|
[352] | 198 | } else {
|
---|
| 199 | // Error
|
---|
| 200 | t = TIME_ERROR_VAL;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | return t;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | /*
|
---|
| 207 | * Dec(8bit) to Hex function for RTC.
|
---|
| 208 | * [in]
|
---|
| 209 | * dec_val:Decimal value (from 0x00 to 0x99).
|
---|
| 210 | * offset:Subtract offset from dec_val.
|
---|
| 211 | * hex_val:Pointer of output hexadecimal value.
|
---|
| 212 | * [out]
|
---|
| 213 | * 0:Success
|
---|
| 214 | * 1:Error
|
---|
| 215 | */
|
---|
[374] | 216 | static int rtc_dec8_to_hex(uint8_t dec_val, uint8_t offset, int *hex_val)
|
---|
| 217 | {
|
---|
[352] | 218 | int err = 0;
|
---|
| 219 | uint8_t ret_val;
|
---|
| 220 |
|
---|
| 221 | if (hex_val != NULL) {
|
---|
| 222 | if (((dec_val & MASK_04_07_POS) >= (0x0A << SHIFT_1_HBYTE)) ||
|
---|
| 223 | ((dec_val & MASK_00_03_POS) >= 0x0A)) {
|
---|
| 224 | err = 1;
|
---|
| 225 | } else {
|
---|
| 226 | ret_val = ((dec_val & MASK_04_07_POS) >> SHIFT_1_HBYTE) * 10 +
|
---|
| 227 | (dec_val & MASK_00_03_POS);
|
---|
| 228 | if (ret_val < offset) {
|
---|
| 229 | err = 1;
|
---|
| 230 | } else {
|
---|
| 231 | *hex_val = ret_val - offset;
|
---|
| 232 | }
|
---|
| 233 | }
|
---|
| 234 | } else {
|
---|
| 235 | err = 1;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | return err;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | /*
|
---|
| 242 | * Dec(16bit) to Hex function for RTC
|
---|
| 243 | * [in]
|
---|
| 244 | * dec_val:Decimal value (from 0x0000 to 0x9999).
|
---|
| 245 | * offset:Subtract offset from dec_val.
|
---|
| 246 | * hex_val:Pointer of output hexadecimal value.
|
---|
| 247 | * [out]
|
---|
| 248 | * 0:Success
|
---|
| 249 | * 1:Error
|
---|
| 250 | */
|
---|
[374] | 251 | static int rtc_dec16_to_hex(uint16_t dec_val, uint16_t offset, int *hex_val)
|
---|
| 252 | {
|
---|
[352] | 253 | int err = 0;
|
---|
| 254 | uint16_t ret_val;
|
---|
| 255 |
|
---|
| 256 | if (hex_val != NULL) {
|
---|
| 257 | if (((dec_val & MASK_12_15_POS) >= (0x0A << SHIFT_3_HBYTE)) ||
|
---|
| 258 | ((dec_val & MASK_08_11_POS) >= (0x0A << SHIFT_2_HBYTE)) ||
|
---|
| 259 | ((dec_val & MASK_04_07_POS) >= (0x0A << SHIFT_1_HBYTE)) ||
|
---|
| 260 | ((dec_val & MASK_00_03_POS) >= 0x0A)) {
|
---|
| 261 | err = 1;
|
---|
| 262 | *hex_val = 0;
|
---|
| 263 | } else {
|
---|
| 264 | ret_val = (((dec_val & MASK_12_15_POS)) >> SHIFT_3_HBYTE) * 1000 +
|
---|
| 265 | (((dec_val & MASK_08_11_POS)) >> SHIFT_2_HBYTE) * 100 +
|
---|
| 266 | (((dec_val & MASK_04_07_POS)) >> SHIFT_1_HBYTE) * 10 +
|
---|
| 267 | (dec_val & MASK_00_03_POS);
|
---|
| 268 | if (ret_val < offset) {
|
---|
| 269 | err = 1;
|
---|
| 270 | } else {
|
---|
| 271 | *hex_val = ret_val - offset;
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 | } else {
|
---|
| 275 | err = 1;
|
---|
| 276 | }
|
---|
| 277 | return err;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | /*
|
---|
| 281 | * RTC write function
|
---|
| 282 | * [in]
|
---|
| 283 | * t:UNIX timestamp value
|
---|
| 284 | * [out]
|
---|
| 285 | * None.
|
---|
| 286 | */
|
---|
[374] | 287 | void rtc_write(time_t t)
|
---|
| 288 | {
|
---|
[352] | 289 |
|
---|
[374] | 290 | struct tm timeinfo;
|
---|
| 291 | if (_rtc_localtime(t, &timeinfo, RTC_FULL_LEAP_YEAR_SUPPORT) == false) {
|
---|
| 292 | return;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[352] | 295 | volatile uint16_t dummy_read;
|
---|
| 296 |
|
---|
| 297 | if (rtc_isenabled() != 0) {
|
---|
| 298 | RTC.RCR2 = RCR2_VAL_ALLSTOP;
|
---|
| 299 | dummy_read = (uint16_t)RTC.RCR2;
|
---|
| 300 | dummy_read = (uint16_t)RTC.RCR2;
|
---|
| 301 | RTC.RCR2 = RCR2_VAL_RESET; // RESET = 1
|
---|
| 302 | dummy_read = (uint16_t)RTC.RCR2;
|
---|
| 303 | dummy_read = (uint16_t)RTC.RCR2;
|
---|
| 304 |
|
---|
[374] | 305 | RTC.RSECCNT = rtc_hex8_to_dec(timeinfo.tm_sec);
|
---|
| 306 | RTC.RMINCNT = rtc_hex8_to_dec(timeinfo.tm_min);
|
---|
| 307 | RTC.RHRCNT = rtc_hex8_to_dec(timeinfo.tm_hour);
|
---|
| 308 | RTC.RDAYCNT = rtc_hex8_to_dec(timeinfo.tm_mday);
|
---|
| 309 | RTC.RMONCNT = rtc_hex8_to_dec(timeinfo.tm_mon + 1);
|
---|
| 310 | RTC.RYRCNT = rtc_hex16_to_dec(timeinfo.tm_year + 1900);
|
---|
[352] | 311 | dummy_read = (uint16_t)RTC.RYRCNT;
|
---|
| 312 | dummy_read = (uint16_t)RTC.RYRCNT;
|
---|
| 313 |
|
---|
| 314 | RTC.RCR2 = RCR2_VAL_START; // START = 1
|
---|
| 315 |
|
---|
| 316 | dummy_read = (uint16_t)RTC.RCR2;
|
---|
| 317 | dummy_read = (uint16_t)RTC.RCR2;
|
---|
[374] | 318 | (void)dummy_read;
|
---|
[352] | 319 | }
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | /*
|
---|
| 323 | * HEX to Dec(8bit) function for RTC.
|
---|
| 324 | * [in]
|
---|
| 325 | * hex_val:Hexadecimal value.
|
---|
| 326 | * [out]
|
---|
| 327 | * decimal value:From 0x00 to 0x99.
|
---|
| 328 | */
|
---|
[374] | 329 | static uint8_t rtc_hex8_to_dec(uint8_t hex_val)
|
---|
| 330 | {
|
---|
[352] | 331 | uint32_t calc_data;
|
---|
| 332 |
|
---|
| 333 | calc_data = hex_val / 10 * 0x10;
|
---|
| 334 | calc_data += hex_val % 10;
|
---|
| 335 |
|
---|
| 336 | if (calc_data > 0x99) {
|
---|
| 337 | calc_data = 0;
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | return (uint8_t)calc_data;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | /*
|
---|
| 344 | * HEX to Dec(16bit) function for RTC.
|
---|
| 345 | * [in]
|
---|
| 346 | * hex_val:Hexadecimal value.
|
---|
| 347 | * [out]
|
---|
| 348 | * decimal value:From 0x0000 to 0x9999.
|
---|
| 349 | */
|
---|
[374] | 350 | static uint16_t rtc_hex16_to_dec(uint16_t hex_val)
|
---|
| 351 | {
|
---|
[352] | 352 | uint32_t calc_data;
|
---|
| 353 | calc_data = hex_val / 1000 * 0x1000;
|
---|
| 354 | calc_data += ((hex_val / 100) % 10) * 0x100;
|
---|
| 355 | calc_data += ((hex_val / 10) % 10) * 0x10;
|
---|
| 356 | calc_data += hex_val % 10;
|
---|
| 357 |
|
---|
| 358 | if (calc_data > 0x9999) {
|
---|
| 359 | calc_data = 0;
|
---|
| 360 | }
|
---|
| 361 | return (uint16_t)calc_data;
|
---|
| 362 |
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | #endif /* DEVICE_RTC */
|
---|