| 1 |
/* |
| 2 |
* The MIT License (MIT) |
| 3 |
* |
| 4 |
* Copyright (c) 2021 Renesas Electronics Corporation |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and associated documentation files (the "Software"), to deal |
| 8 |
* in the Software without restriction, including without limitation the rights |
| 9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
* copies of the Software, and to permit persons to whom the Software is |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be included in |
| 14 |
* all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
* THE SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
#include <stdint.h> |
| 27 |
#include <stdbool.h> |
| 28 |
#include "hal_data.h" |
| 29 |
#include "ra_config.h" |
| 30 |
#include "ra_rtc.h" |
| 31 |
|
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include "hal_data.h" |
| 35 |
#include "ra_rtc.h" |
| 36 |
|
| 37 |
static R_RTC_Type *rtc_reg = R_RTC; |
| 38 |
static R_SYSTEM_Type *system_reg = (R_SYSTEM_Type *)0x4001E000; |
| 39 |
|
| 40 |
#if defined(VECTOR_NUMBER_RTC_ALARM) |
| 41 |
ra_rtc_cb_t ra_rtc_func_alarm = NULL; |
| 42 |
void *ra_rtc_param_alarm = NULL; |
| 43 |
#endif |
| 44 |
#if defined(VECTOR_NUMBER_RTC_PERIOD) |
| 45 |
ra_rtc_cb_t ra_rtc_func_period = NULL; |
| 46 |
void *ra_rtc_param_period = NULL; |
| 47 |
#endif |
| 48 |
|
| 49 |
static inline uint8_t int_to_bcd(int num) { |
| 50 |
return (uint8_t)(((num / 10) << 4) | (num % 10)); |
| 51 |
} |
| 52 |
|
| 53 |
static inline int bcd_to_int(uint8_t bcd) { |
| 54 |
return (int)(((bcd >> 4) * 10) + (bcd & 0x0F)); |
| 55 |
} |
| 56 |
|
| 57 |
int ra_rtc_get_year(void) { |
| 58 |
return bcd_to_int((uint8_t)rtc_reg->RYRCNT) + 2000; |
| 59 |
} |
| 60 |
|
| 61 |
int ra_rtc_get_month(void) { |
| 62 |
return bcd_to_int((uint8_t)rtc_reg->RMONCNT); |
| 63 |
} |
| 64 |
|
| 65 |
int ra_rtc_get_date(void) { |
| 66 |
return bcd_to_int((uint8_t)rtc_reg->RDAYCNT); |
| 67 |
} |
| 68 |
|
| 69 |
int ra_rtc_get_hour(void) { |
| 70 |
return bcd_to_int((uint8_t)(0x3f & rtc_reg->RHRCNT)); |
| 71 |
} |
| 72 |
|
| 73 |
int ra_rtc_get_minute(void) { |
| 74 |
return bcd_to_int((uint8_t)rtc_reg->RMINCNT); |
| 75 |
} |
| 76 |
|
| 77 |
int ra_rtc_get_second(void) { |
| 78 |
return bcd_to_int((uint8_t)rtc_reg->RSECCNT); |
| 79 |
} |
| 80 |
|
| 81 |
int ra_rtc_get_weekday(void) { |
| 82 |
return bcd_to_int((uint8_t)rtc_reg->RWKCNT); |
| 83 |
} |
| 84 |
|
| 85 |
#if defined(VECTOR_NUMBER_RTC_PERIOD) |
| 86 |
|
| 87 |
void ra_rtc_period_on() { |
| 88 |
// Enable periodic interrupt |
| 89 |
rtc_reg->RCR1_b.PIE = 1; |
| 90 |
while (!rtc_reg->RCR1_b.PIE) { |
| 91 |
; |
| 92 |
} |
| 93 |
// Enable NVIC RTC Alarm interrupt |
| 94 |
R_BSP_IrqCfg((IRQn_Type const)RTC_PERIOD_IRQn, (uint32_t)RA_PRI_RTC_WKUP, (void *)NULL); |
| 95 |
R_BSP_IrqEnable((IRQn_Type const)RTC_PERIOD_IRQn); |
| 96 |
} |
| 97 |
|
| 98 |
void ra_rtc_period_off() { |
| 99 |
// Disable NVIC RTC Alarm interrupt |
| 100 |
R_BSP_IrqDisable((IRQn_Type const)RTC_PERIOD_IRQn); |
| 101 |
// Disable periodic interrupt |
| 102 |
rtc_reg->RCR1_b.PIE = 0; |
| 103 |
while (rtc_reg->RCR1_b.PIE) { |
| 104 |
; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// period |
| 109 |
// 6 : every 1/256(s) |
| 110 |
// 7 : every 1/128(s) |
| 111 |
// 8 : every 1/64(s) |
| 112 |
// 9 : every 1/32(s) |
| 113 |
// 10 : every 1/16(s) |
| 114 |
// 11 : every 1/8(s) |
| 115 |
// 12 : every 1/4(s) |
| 116 |
// 13 : every 1/2(s) |
| 117 |
// 14 : every 1(s) |
| 118 |
// 15 : every 2(s) |
| 119 |
void ra_rtc_set_period_time(uint32_t period) { |
| 120 |
if ((period < 6) | (period > 15)) { |
| 121 |
return; |
| 122 |
} |
| 123 |
ra_rtc_period_off(); |
| 124 |
uint8_t rcr1 = rtc_reg->RCR1; |
| 125 |
rcr1 &= (uint8_t) ~R_RTC_RCR1_PES_Msk; |
| 126 |
rcr1 |= (uint8_t)(period << R_RTC_RCR1_PES_Pos); |
| 127 |
rtc_reg->RCR1 = rcr1; |
| 128 |
while (rtc_reg->RCR1 != rcr1) { |
| 129 |
; |
| 130 |
} |
| 131 |
ra_rtc_period_on(); |
| 132 |
} |
| 133 |
|
| 134 |
void ra_rtc_set_period_func(void *cb, void *param) { |
| 135 |
ra_rtc_period_off(); |
| 136 |
ra_rtc_func_period = (ra_rtc_cb_t)cb; |
| 137 |
ra_rtc_param_period = param; |
| 138 |
ra_rtc_period_on(); |
| 139 |
} |
| 140 |
|
| 141 |
#endif |
| 142 |
|
| 143 |
#if defined(VECTOR_NUMBER_RTC_ALARM) |
| 144 |
|
| 145 |
void ra_rtc_alarm_on() { |
| 146 |
// Enable alarm interrupt |
| 147 |
rtc_reg->RCR1_b.AIE = 1; |
| 148 |
while (!rtc_reg->RCR1_b.AIE) { |
| 149 |
; |
| 150 |
} |
| 151 |
// Enable NVIC RTC Alarm interrupt |
| 152 |
R_BSP_IrqCfg((IRQn_Type const)RTC_ALARM_IRQn, (uint32_t)RA_PRI_RTC_WKUP, (void *)NULL); |
| 153 |
R_BSP_IrqEnable((IRQn_Type const)RTC_ALARM_IRQn); |
| 154 |
} |
| 155 |
|
| 156 |
void ra_rtc_alarm_off() { |
| 157 |
// Disable NVIC RTC Alarm interrupt |
| 158 |
R_BSP_IrqDisable((IRQn_Type const)RTC_ALARM_IRQn); |
| 159 |
// Disable alarm interrupt |
| 160 |
rtc_reg->RCR1_b.AIE = 0; |
| 161 |
while (rtc_reg->RCR1_b.AIE) { |
| 162 |
; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
void ra_rtc_set_alarm_time(int hour, int min, int week_flag) { |
| 167 |
ra_rtc_alarm_off(); |
| 168 |
// Configure the alarm |
| 169 |
rtc_reg->RMINAR = (uint8_t)int_to_bcd(min); |
| 170 |
rtc_reg->RHRAR = (uint8_t)int_to_bcd(hour); |
| 171 |
if (week_flag <= 0x06) { |
| 172 |
rtc_reg->RWKAR = (uint8_t)week_flag; |
| 173 |
} |
| 174 |
rtc_reg->RMINAR_b.ENB = 1; |
| 175 |
rtc_reg->RHRAR_b.ENB = 1; |
| 176 |
if (week_flag <= 0x06) { |
| 177 |
rtc_reg->RWKAR_b.ENB = 1; |
| 178 |
} else { |
| 179 |
rtc_reg->RWKAR_b.ENB = 0; |
| 180 |
} |
| 181 |
ra_rtc_alarm_on(); |
| 182 |
} |
| 183 |
|
| 184 |
void ra_rtc_set_alarm_func(void *cb, void *param) { |
| 185 |
ra_rtc_period_off(); |
| 186 |
ra_rtc_func_alarm = (ra_rtc_cb_t)cb; |
| 187 |
ra_rtc_param_alarm = param; |
| 188 |
ra_rtc_period_on(); |
| 189 |
} |
| 190 |
|
| 191 |
#endif |
| 192 |
|
| 193 |
// adj: adjustment bit (number of sub clocks) |
| 194 |
// 0 : no adjustment |
| 195 |
// 0<: |
| 196 |
// 0>: |
| 197 |
// aadjp: specify adjustment period |
| 198 |
// 0: 1 minute (RTC_PERIOD_MINUTE) |
| 199 |
// 1: 10 seconds (RTC_PERIOD_SECOND) |
| 200 |
void ra_rtc_set_adjustment(int adj, int aadjp) { |
| 201 |
int tmp_int; |
| 202 |
aadjp &= 1; |
| 203 |
if (adj == 0) { |
| 204 |
// no adjustment |
| 205 |
rtc_reg->RADJ = 0x00; |
| 206 |
while (rtc_reg->RADJ != 0x00) { |
| 207 |
; |
| 208 |
} |
| 209 |
} else if (adj > 0) { |
| 210 |
// plus adjustment |
| 211 |
rtc_reg->RADJ = 0x00; |
| 212 |
while (rtc_reg->RADJ != 0x00) { |
| 213 |
; |
| 214 |
} |
| 215 |
// enable auto adjustment |
| 216 |
rtc_reg->RCR2_b.AADJE = 1; |
| 217 |
while (rtc_reg->RCR2_b.AADJE != 1) { |
| 218 |
; |
| 219 |
} |
| 220 |
rtc_reg->RCR2_b.AADJP = |
| 221 |
aadjp == RTC_PERIOD_MINUTE ? RTC_PERIOD_MINUTE : RTC_PERIOD_SECOND; |
| 222 |
while (rtc_reg->RCR2_b.AADJP != aadjp) { |
| 223 |
; |
| 224 |
} |
| 225 |
tmp_int = 0x40 | (0x3F & adj); |
| 226 |
rtc_reg->RADJ = (uint8_t)tmp_int; |
| 227 |
while (rtc_reg->RADJ != (uint8_t)tmp_int) { |
| 228 |
; |
| 229 |
} |
| 230 |
} else { |
| 231 |
// minus adjustment |
| 232 |
rtc_reg->RADJ = 0x00; |
| 233 |
while (rtc_reg->RADJ != 0x00) { |
| 234 |
; |
| 235 |
} |
| 236 |
// enable adjustment |
| 237 |
rtc_reg->RCR2_b.AADJE = 1; |
| 238 |
while (rtc_reg->RCR2_b.AADJE != 1) { |
| 239 |
; |
| 240 |
} |
| 241 |
rtc_reg->RCR2_b.AADJP = |
| 242 |
aadjp == RTC_PERIOD_MINUTE ? RTC_PERIOD_MINUTE : RTC_PERIOD_SECOND; |
| 243 |
while (rtc_reg->RCR2_b.AADJP != aadjp) { |
| 244 |
; |
| 245 |
} |
| 246 |
tmp_int = 0x80 | (0x3F & abs(adj)); |
| 247 |
rtc_reg->RADJ = (uint8_t)tmp_int; |
| 248 |
while (rtc_reg->RADJ != (uint8_t)tmp_int) { |
| 249 |
; |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
uint8_t ra_rtc_get_adjustment(void) { |
| 255 |
return rtc_reg->RADJ; |
| 256 |
} |
| 257 |
|
| 258 |
bool ra_rtc_set_time(ra_rtc_t *time) { |
| 259 |
// Write 0 to RTC start bit |
| 260 |
rtc_reg->RCR2_b.START = 0x0; |
| 261 |
// Wait for start bit to clear |
| 262 |
while (0 != rtc_reg->RCR2_b.START) { |
| 263 |
; |
| 264 |
} |
| 265 |
// Alarm enable bits are undefined after a reset, |
| 266 |
// disable non-required alarm features |
| 267 |
rtc_reg->RWKAR_b.ENB = 0; |
| 268 |
rtc_reg->RDAYAR_b.ENB = 0; |
| 269 |
rtc_reg->RMONAR_b.ENB = 0; |
| 270 |
rtc_reg->RYRAREN_b.ENB = 0; |
| 271 |
// Operate RTC in 24-hr mode |
| 272 |
rtc_reg->RCR2_b.HR24 = 0x1; |
| 273 |
rtc_reg->RYRCNT = (uint16_t)int_to_bcd(time->year % 100); |
| 274 |
rtc_reg->RMONCNT = (uint8_t)int_to_bcd(time->month); |
| 275 |
rtc_reg->RDAYCNT = (uint8_t)int_to_bcd(time->date); |
| 276 |
rtc_reg->RHRCNT = (uint8_t)int_to_bcd(time->hour); |
| 277 |
rtc_reg->RMINCNT = (uint8_t)int_to_bcd(time->minute); |
| 278 |
rtc_reg->RSECCNT = (uint8_t)int_to_bcd(time->second); |
| 279 |
rtc_reg->RWKCNT = (uint8_t)int_to_bcd(time->weekday); |
| 280 |
// Start the clock |
| 281 |
rtc_reg->RCR2_b.START = 0x1; |
| 282 |
// Wait until the start bit is set to 1 |
| 283 |
while (1 != rtc_reg->RCR2_b.START) { |
| 284 |
; |
| 285 |
} |
| 286 |
return true; |
| 287 |
} |
| 288 |
|
| 289 |
bool ra_rtc_get_time(ra_rtc_t *time) { |
| 290 |
time->year = (uint16_t)(bcd_to_int((uint8_t)rtc_reg->RYRCNT) + 2000); |
| 291 |
time->month = (uint8_t)bcd_to_int(rtc_reg->RMONCNT); |
| 292 |
time->date = (uint8_t)bcd_to_int(rtc_reg->RDAYCNT); |
| 293 |
time->hour = (uint8_t)bcd_to_int((uint8_t)(0x3f & rtc_reg->RHRCNT)); |
| 294 |
time->minute = (uint8_t)bcd_to_int((uint8_t)rtc_reg->RMINCNT); |
| 295 |
time->second = (uint8_t)bcd_to_int((uint8_t)rtc_reg->RSECCNT); |
| 296 |
time->weekday = (uint8_t)bcd_to_int((uint8_t)rtc_reg->RWKCNT); |
| 297 |
return true; |
| 298 |
} |
| 299 |
|
| 300 |
static void wait(volatile int count) { |
| 301 |
while (count--) { |
| 302 |
__asm__ __volatile__ ("nop"); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
// source |
| 307 |
// 0: subclock |
| 308 |
// 1: LOCO |
| 309 |
static void ra_rtc_set_subclock(uint8_t source) { |
| 310 |
// Set RTC clock input from sub-clock, and supply to RTC module |
| 311 |
rtc_reg->RCR4_b.RCKSEL = source; |
| 312 |
if (0 == source) { |
| 313 |
R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS); |
| 314 |
} else { |
| 315 |
R_BSP_SoftwareDelay(200, BSP_DELAY_UNITS_MICROSECONDS); |
| 316 |
} |
| 317 |
// Stop the clock |
| 318 |
rtc_reg->RCR2_b.START = 0x0; |
| 319 |
// Wait for start bit to clear |
| 320 |
while (0 != rtc_reg->RCR2_b.START) { |
| 321 |
; |
| 322 |
} |
| 323 |
if (source == 1) { |
| 324 |
rtc_reg->RFRH = 0; |
| 325 |
rtc_reg->RFRL = (uint16_t)0x00ff; // assume 32.768khz |
| 326 |
} |
| 327 |
rtc_reg->RCR2_b.CNTMD = 0; |
| 328 |
while (0 != rtc_reg->RCR2_b.CNTMD) { |
| 329 |
; |
| 330 |
} |
| 331 |
// Reset the RTC unit |
| 332 |
rtc_reg->RCR2_b.RESET = 0x1; |
| 333 |
// Wait until reset is complete |
| 334 |
while (0 != rtc_reg->RCR2_b.RESET) { |
| 335 |
; |
| 336 |
} |
| 337 |
// Start the clock |
| 338 |
rtc_reg->RCR2_b.START = 0x1; |
| 339 |
// Wait until the start bit is set to 1 |
| 340 |
while (1 != rtc_reg->RCR2_b.START) { |
| 341 |
; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
bool ra_rtc_init(uint8_t source) { |
| 346 |
system_reg->PRCR = 0xA503; |
| 347 |
// Check if the MCU has come from a cold start (power on reset) |
| 348 |
if (0 == system_reg->RSTSR2_b.CWSF) { |
| 349 |
// cold start |
| 350 |
system_reg->VBTCR1_b.BPWSWSTP = 1; |
| 351 |
// Set the warm start flag |
| 352 |
system_reg->RSTSR2_b.CWSF = 1; |
| 353 |
// Disable the sub-clock oscillator |
| 354 |
system_reg->SOSCCR_b.SOSTP = 1; |
| 355 |
// Wait for register modification to complete |
| 356 |
while (1 != system_reg->SOSCCR_b.SOSTP) { |
| 357 |
; |
| 358 |
} |
| 359 |
// Start sub-clock |
| 360 |
system_reg->SOSCCR_b.SOSTP = 0; |
| 361 |
// Perform 8 delay iterations |
| 362 |
for (uint8_t i = 0; i < 8; i++) { |
| 363 |
// Wait in while loop for ~0.5 seconds |
| 364 |
wait(0xFFFFE); |
| 365 |
} |
| 366 |
} else { |
| 367 |
// Start sub-clock |
| 368 |
system_reg->SOSCCR_b.SOSTP = 0; |
| 369 |
// Wait for the register modification to complete |
| 370 |
while (0 != system_reg->SOSCCR_b.SOSTP) { |
| 371 |
; |
| 372 |
} |
| 373 |
} |
| 374 |
system_reg->PRCR = 0xA500; |
| 375 |
// call back |
| 376 |
#if defined(VECTOR_NUMBER_RTC_ALARM) |
| 377 |
ra_rtc_func_alarm = NULL; |
| 378 |
#endif |
| 379 |
#if defined(VECTOR_NUMBER_RTC_PERIOD) |
| 380 |
ra_rtc_func_period = NULL; |
| 381 |
#endif |
| 382 |
if ((rtc_reg->RCR2_b.START == 0) || (rtc_reg->RCR4_b.RCKSEL != source)) { |
| 383 |
rtc_reg->RCR1 = 0; |
| 384 |
rtc_reg->RCR2 = 0; |
| 385 |
ra_rtc_set_subclock(source); |
| 386 |
} |
| 387 |
return true; |
| 388 |
} |
| 389 |
|
| 390 |
bool ra_rtc_deinit(void) { |
| 391 |
#if defined(VECTOR_NUMBER_RTC_ALARM) |
| 392 |
ra_rtc_alarm_off(); |
| 393 |
ra_rtc_func_alarm = NULL; |
| 394 |
ra_rtc_param_alarm = NULL; |
| 395 |
#endif |
| 396 |
#if defined(VECTOR_NUMBER_RTC_PERIOD) |
| 397 |
ra_rtc_period_off(); |
| 398 |
ra_rtc_func_period = NULL; |
| 399 |
ra_rtc_param_period = NULL; |
| 400 |
#endif |
| 401 |
rtc_reg->RCR4_b.RCKSEL = 1; |
| 402 |
rtc_reg->RCR1 = 0; |
| 403 |
return true; |
| 404 |
} |
| 405 |
|
| 406 |
void rtc_alarm_periodic_isr(void) { |
| 407 |
IRQn_Type irq = R_FSP_CurrentIrqGet(); |
| 408 |
#if defined(VECTOR_NUMBER_RTC_PERIOD) |
| 409 |
if (irq == RTC_PERIOD_IRQn) { |
| 410 |
if (ra_rtc_func_period) { |
| 411 |
ra_rtc_func_period(ra_rtc_param_period); |
| 412 |
} |
| 413 |
} |
| 414 |
#endif |
| 415 |
#if defined(VECTOR_NUMBER_RTC_ALARM) |
| 416 |
if (irq == RTC_ALARM_IRQn) { |
| 417 |
if (ra_rtc_func_alarm) { |
| 418 |
ra_rtc_func_alarm(ra_rtc_param_alarm); |
| 419 |
} |
| 420 |
} |
| 421 |
#endif |
| 422 |
R_BSP_IrqStatusClear(irq); |
| 423 |
} |
| 424 |
|
| 425 |
#if defined(VECTOR_NUMBER_RTC_CARRY) |
| 426 |
void rtc_carry_isr(void) { |
| 427 |
IRQn_Type irq = R_FSP_CurrentIrqGet(); |
| 428 |
R_BSP_IrqStatusClear(irq); |
| 429 |
} |
| 430 |
#endif |