I just find a solution but it is not 'elegant'. If set the LPO option the RTC_TPR begin increment and RTC_TSR an increment every 32 seconds.
The code is the following:
void rtc_init_def(void)
{
/*enable the clock to SRTC module register space*/
SIM_SCGC6 |= SIM_SCGC6_RTC_MASK;
RTC_SR &= ~RTC_SR_TCE_MASK;
//Configure the TSR and TAR
RTC_TSR = 0x00000000; //RTC Time Seconds Register
RTC_TPR = 0x00007c18; //valore da calibrare
RTC_TAR = RTC_TSR + ALARM_TIME; //RTC Time Alarm Register
/* enable the RTC_CLKIN function */
SIM_SOPT1 &= SIM_SOPT1_OSC32KSEL_MASK;
SIM_SOPT1 |= SIM_SOPT1_OSC32KSEL(3); /* Selects the 1 kHz clock source (LPO) for RTC */
RTC_IER |= RTC_IER_TSIE_MASK | RTC_IER_TAIE_MASK | RTC_IER_TOIE_MASK | RTC_IER_TIIE_MASK;
RTC_SR |= RTC_SR_TCE_MASK; //Enable RTC_SR_TCE
#ifdef CMSIS
NVIC_EnableIRQ(RTC_IRQn);
NVIC_EnableIRQ(RTC_Seconds_IRQn);
#else
enable_irq(INT_RTC-16);
enable_irq(INT_RTC_Seconds-16);
#endif
}
#ifdef CMSIS
void RTC_Seconds_IRQHandler(void)
#else
void rtc_isrv_seconds(void)
#endif
{
RTC_SR = 0x00000000;
RTC_TPR = 0x00007C18;
RTC_SR = 0x00000010;
rtc_seconds_isrv_count++;
}
#ifdef CMSIS
void RTC_IRQHandler(void)
#else
void rtc_isrv(void)
#endif
{
uint32 rtc_sr = RTC_SR;
if(rtc_sr & RTC_SR_TAF_MASK) // RTC timer alarm flag is set
{
LED1_TOGGLE;
RTC_TAR = RTC_TAR + ALARM_TIME; // write new value to TAR to clear TAF
seconds_count++;
}
if(rtc_sr & RTC_SR_TOF_MASK) // RTC timer Overlow flag is set
{
RTC_SR |= RTC_SR_TOF_MASK;
}
if (rtc_sr & RTC_SR_TIF_MASK) // Timer Invalid flag
{
RTC_SR &= ~RTC_SR_TCE_MASK; //Disable timer
RTC_TSR = 0x00; // write to clear TOF or TIF
RTC_SR |= RTC_SR_TCE_MASK; //re-enable timer
}
}