Mark,
That actually sounds like a very legit solution, but I still hit the breakpoints very often.
Almost everytime the TPR transitions from 0x7FFF to 0.
Code snippet below.
Thanks,
Cecylia
/*******************************************************************************
* Get K22F's RTC in milliseconds
******************************************************************************/
uint32_t k22f_get_rtc(void) {
static uint32_t last_sec = 0, last_tpr = 0;
uint32_t dummy_tpr, dummy_tpr1, current_tpr;
uint32_t rtc_milliseconds;
uint32_t dummy_rtc, current_sec;
uint32_t cnt;
const uint32_t cnt_max = 0xFF;
uint32_t temp_SR;
//debug
static uint32_t last_total_ms = 0;
uint32_t total_ms_diff, current_total_ms;
/* WARNING: There will be consequences if you change the order of the reading!
* Make sure you know what you're doing.
*
* For example:
* At time 0 second, 0x7FFF
* Read TPR: get 0x7FFF
* Read second: get 1 (already incremented)
* So you need to fix it.
*
* If you read second first
* second: get 0,
* TPR: get 0 (already incremented).
* Must handle these cases.
*/
//cw testing todo
// Disable time counters TSR and TPR before writing
temp_SR = BR_RTC_SR_TCE(RTC_BASE);
BW_RTC_SR_TCE(RTC_BASE, 0);
/* We have to read twice or more until we get same values */
dummy_tpr = HW_RTC_TPR_RD(RTC_BASE);
current_tpr = HW_RTC_TPR_RD(RTC_BASE);
cnt = 0;
while (dummy_tpr != current_tpr) {
if (++cnt == cnt_max) {
/* Put a count to avoid infinite loop. Shouldn't arrive here unless HW
* goes kaput. */
return UINT32_MAX;
}
dummy_tpr = HW_RTC_TPR_RD(RTC_BASE);
current_tpr = HW_RTC_TPR_RD(RTC_BASE);
}
/* We have to read twice or more until we get same values */
dummy_rtc = HW_RTC_TSR_RD(RTC_BASE);
current_sec = HW_RTC_TSR_RD(RTC_BASE);
cnt = 0;
while (dummy_rtc != current_sec) {
if (++cnt == cnt_max) {
/* Put a count to avoid infinite loop. Shouldn't arrive here unless HW
* goes kaput. */
return UINT32_MAX;
}
dummy_rtc = HW_RTC_TSR_RD(RTC_BASE);
current_sec = HW_RTC_TSR_RD(RTC_BASE);
}
// /* Check again. If TPR has gone back to 0, then we should reduce the sec.
// * Meaning, on our first read, say time was
// * 0/7FFF
// * during read we got: 7FFF, then 1. ---> 1/7FFF which is 1 second off
// * now time is actually 1/000
// * We reduce the second by 1 to get: 0/7FFF.
// */
// dummy_tpr = HW_RTC_TPR_RD(RTC_BASE);
// dummy_tpr1 = HW_RTC_TPR_RD(RTC_BASE);
// while (dummy_tpr != dummy_tpr1) {
// if (++cnt == cnt_max) {
// /* Put a count to avoid infinite loop. Shouldn't arrive here unless HW
// * goes kaput. */
// return UINT32_MAX;
// }
// dummy_tpr = HW_RTC_TPR_RD(RTC_BASE);
// dummy_tpr1 = HW_RTC_TPR_RD(RTC_BASE);
// }
// if (dummy_tpr < current_tpr) {
// current_sec = current_sec - 1;
// }
// TPR = 1 equals to ~30.5us = 1000000us / 32768Hz = (30.5/1000)ms = (1000/32768)ms
rtc_milliseconds = current_tpr * 1000 / 32768;
current_total_ms = (current_sec * 1000) + rtc_milliseconds;
if (current_total_ms < last_total_ms) {
//dummy. this is not good!
dummy_tpr = 0; ----> [cw] Put a breakpoint here
}
last_sec = current_sec;
last_tpr = current_tpr;
last_total_ms = current_total_ms;
//cw todo re enable Timer
BW_RTC_SR_TCE(RTC_BASE, temp_SR);
return current_total_ms;
}
/*******************************************************************************
* Enable K22F's RTC registers (TSR - seconds counter, TPR - prescaler counter
* TSR is incremented every second
* TPR is incremented every 32.768 kHz
******************************************************************************/
void k22f_enable_rtc() {
// Enable 32.768 kHz oscillator
BW_RTC_CR_OSCE(RTC_BASE, 1);
// Wait 100ms for oscillator output to settle down
OSA_TimeDelay(100);
// Disable time counters TSR and TPR before writing
BW_RTC_SR_TCE(RTC_BASE, 0);
// Clear the RTC_TPR counter
HW_RTC_TPR_CLR(RTC_BASE, 0xFFFFFFFFU);
// Write the RTC_TSR with 1 second value
HW_RTC_TSR_WR(RTC_BASE, 1);
// Enable time counters TSR and TPR after writing
BW_RTC_SR_TCE(RTC_BASE, 1);
}
// Main loop
{
//some init
k22f_enable_rtc();
while (true)
{
k22f_get_rtc();
for (uint16_t tmp_cnt = 0; tmp_cnt < 0x255; tmp_cnt++) {
;
}
//doing other bunch of stuff
}
}