Page 1 of 1

RTC Clock Nightmare - STM32F103

Posted: Sun Feb 09, 2014 6:41 pm
by enly1
Hi All,

My code and the tesHAL RTC sample do not work on my board (chibistudio latest preview) + custom stmf103c8t6 board. everything just locked up. The RTC clock was ticking (clock callback once per second working) as long as I didn't call rtcGetTime(&RTCD1, &timespec);

I've not been able to get the debugger environment up on my machine, so have spent the best part of 10 hours trying to make the debugger work and get RTC working.

So traced the failing call down into rtc_lld.c from rtcv1 folder and tweeked away.

I have it working now, but no idea why the original doesn't - must be a compiler option, a bad cast operation or something.

The following works:

Code: Select all

/* Loops until two consecutive read returning the same value.*/
  do {
    timespec->tv_sec = (uint32_t)RTC->CNTH << 16 | (uint32_t)RTC->CNTL;
    time_frac = (uint32_t)RTC->DIVH << 16 | (uint32_t)RTC->DIVL;
    temptime = (uint32_t)RTC->CNTH << 16 | (uint32_t)RTC->CNTL;
  } while (timespec->tv_sec != temptime);
  timespec->tv_msec = (uint16_t)(((STM32_RTCCLK - 1 - time_frac) * 1000) / STM32_RTCCLK);


The following doesn't:

Code: Select all

do {
    timespec->tv_sec = (uint32_t)RTC->CNTH << 16 | (uint32_t)RTC->CNTL;
    time_frac = (uint32_t)RTC->DIVH << 16 | (uint32_t)RTC->DIVL;
  } while (timespec->tv_sec != (uint32_t)RTC->CNTH << 16 | (uint32_t)RTC->CNTL);

  timespec->tv_msec = (uint16_t)(((STM32_RTCCLK - 1 - time_frac) * 1000) / STM32_RTCCLK);


The difference is just the use of the temptime value placed into the 'while loop' instead of the expression being used as comparison directly in the while clause.

Anyone give me any ideas of whats going on here. - Its been 20 years since I wrote any C code, so am more than a little rusty on the syntax structure etc.

PS - The above code isn't the original RTC lld code as I've swapped out the + for an OR operation during tweeking - but the same issue with the original code.

Thanks in advance as always.

Re: RTC Clock Nightmare - STM32F103

Posted: Sun Feb 09, 2014 6:56 pm
by enly1
After all that - I've just copied out of the github repo the following function code just to make sure I've not tweeked too far and introduced some of my own errors:

Code: Select all

 do {
    timespec->tv_sec = ((uint32_t)(RTC->CNTH) << 16) + RTC->CNTL;
    time_frac = (uint32_t)RTC->DIVH << 16 | (uint32_t)RTC->DIVL;
    } while ((timespec->tv_sec) != (((uint32_t)(RTC->CNTH) << 16) + RTC->CNTL));

  timespec->tv_msec = (uint16_t)(((STM32_RTCCLK - 1 - time_frac) * 1000) / STM32_RTCCLK);


If I compile this with optimisations enabled ... it never returns from the while loop and thus (as its in a chSysLock();/chSysUnlock(); block) locks up the system.

Code: Select all

 USE_OPT = -O2 -ggdb -fomit-frame-pointer -falign-functions=16


if I remove -O2 or use -O0 ... it all works fine

Code: Select all

 USE_OPT =  -ggdb -fomit-frame-pointer -falign-functions=16


:oops: :roll:

Re: RTC Clock Nightmare - STM32F103

Posted: Sun Feb 09, 2014 7:38 pm
by Giovanni
Hi,

That code has not been touched for a while. The fact that disabling optimization it works and your workaround make me think to a compiler bug related to the handling of volatile. It is like "timespec->tv_sec = ((uint32_t)(RTC->CNTH) << 16) + RTC->CNTL;" is moved outside the loop, I will have to look at the generated code.

You may try to update the compiler in ChibiStudio and see if there is a successive version that fixes the problem.

Giovanni

Re: RTC Clock Nightmare - STM32F103

Posted: Mon Feb 10, 2014 9:26 pm
by enly1
Hi Giovanni,

May have to confess some stupidity here ... while I was hunting through every failure, I found a slight error in my makefile.

The code I tested with was built on a makefile I had ported from an STM F3 discovery project and I had cortex-m4 not cortex-m3 set as he mcu type.

I think I've since recompiled with -O2 back in place (which was required to make the debugger work for me as well - another strange one) and it appears to work with the github code stub in place. So no workaround.

I'll confirm when I get a chance to work on it, but looks like that may have been the cause of my optimization weirdness. So just thought best to fess up instead of someone else get confused by the posting - but may help someone thats as silly as me :-)

Re: RTC Clock Nightmare - STM32F103

Posted: Mon Feb 10, 2014 9:32 pm
by Giovanni
No problems, thanks for finding anyway :)

Giovanni