cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4Discovery : Using HSE clock source for RTC.

m_fawaz
Associate II
Posted on March 31, 2014 at 11:58

Dear community,

Knowing that the RTC manual stated that the LSI is not suitable for the RTC, I am trying to use the HSE clock source for the RTC.

I initialized the clock and RTC but the i was unable to read the time. Can you please help?

I initialized the clock as follows:

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

  PWR_BackupAccessCmd(ENABLE);

  RCC_HSEConfig( RCC_HSE_ON );

  while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET)  {}

  RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div8);

  RCC_RTCCLKCmd(ENABLE);

  RTC_WaitForSynchro();

  RTC_InitStructure.RTC_AsynchPrediv = 0x7C;

  RTC_InitStructure.RTC_SynchPrediv  = 0x1F3F;

  RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;

  RTC_Init(&RTC_InitStructure);

  RTC_TimeStructure.RTC_H12     = RTC_H12_AM;

  RTC_TimeStructure.RTC_Hours   = 1;

  RTC_TimeStructure.RTC_Minutes = 0;

  RTC_TimeStructure.RTC_Seconds = 0;

  RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);

In order to read the clock i am using the following:

        RTC_GetTime(RTC_Format_BCD, &RTC_TimeStructure);

        uwMin = RTC_TimeStructure.RTC_Minutes;

        uwSecond = RTC_TimeStructure.RTC_Seconds;

        uwSecond_h   = (((uint8_t)(RTC_TimeStructure.RTC_Seconds & 0xF0) >> 0x04)+ 0x30);

        uwSecond_l   = (((uint8_t)(RTC_TimeStructure.RTC_Seconds & 0x0F)) + 0x30);

        uwSecondfraction = 1000 - ((uint32_t)((uint32_t)RTC_GetSubSecond() * 1000) / (uint32_t)0x3FF);

Unfortunately all the variables (uwMin, uwSecond, uwSecond_h, uwSecond_l, uwSecondfraction) has 0 as value.

Thank you in advance for the help.

Best regards,

Marc

#stm32f4discovery-rcc-hse-rtc
4 REPLIES 4
Posted on March 31, 2014 at 14:44

Unfortunately all the variables (uwMin, uwSecond, uwSecond_h, uwSecond_l, uwSecondfraction) has 0 as value.

Seems a little implausable as uwSecond_h and uwSecond_l have 48 added too them, how are you debugging this?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
m_fawaz
Associate II
Posted on March 31, 2014 at 16:00

Dear Mr Clive1,

In fact, I verified the result using 2 medias:

1) Using the watch function of the CoIDE of COOCOX (RTC_TimeStructure.RTC_Seconds = 0)

2) Writing the values of the variables on an SDCard

It is worthy of a note that i used the same code with LSI as a source and it worked. I only changed the clock source to use HSE instead.

Seems a little implausable as uwSecond_h and uwSecond_l have 48 added too them, how are you debugging this?

The library i am using to write on the SD card takes as input characters not integers. As a result the int had to be tranformed into a char. The easiest way is to add 48 to obtain the ASCII equivalent code.

The ASCII code of 0 is 48.

I am sorry for the inconvenience.

Thank you and best regards,

marc

Posted on April 01, 2014 at 04:31

This works for me in Keil,

// STM32F4-Discovery RTC HSE Demo (Keil) - sourcer32@gmail.com
#include ''stm32f4_discovery.h''
#include <
stdio.h
>
/**************************************************************************/
void RTC_Configuration(void)
{
RTC_InitTypeDef RTC_InitStructure;
RTC_TimeTypeDef RTC_TimeStructure;
RTC_DateTypeDef RTC_DateStructure;
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
PWR_BackupAccessCmd(ENABLE);
// Reset the backup domain, clear RTC
RCC_BackupResetCmd(ENABLE);
RCC_BackupResetCmd(DISABLE);
if (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET) /* HSE Already running? */
{
/* Enable the LSI OSC */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till LSI is ready */
while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
}
/* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div8);
/* Enable the RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
/* Calendar Configuration with HSE/8 supposed at 1 MHz */
RTC_InitStructure.RTC_AsynchPrediv = 125 - 1;
RTC_InitStructure.RTC_SynchPrediv = 8000 - 1; /* (1MHz / 125) - 1 = 7999*/
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
RTC_Init(&RTC_InitStructure);
/* Set the date: Tuesday April 1st 2014 */
RTC_DateStructure.RTC_Year = 0x14;
RTC_DateStructure.RTC_Month = RTC_Month_April;
RTC_DateStructure.RTC_Date = 0x01;
RTC_DateStructure.RTC_WeekDay = RTC_Weekday_Tuesday;
RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure);
RTC_TimeStructure.RTC_H12 = RTC_H12_AM;
RTC_TimeStructure.RTC_Hours = 1;
RTC_TimeStructure.RTC_Minutes = 0;
RTC_TimeStructure.RTC_Seconds = 0;
RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);
}
/**************************************************************************/
volatile uint32_t SystemTick = 0;
void SysTick_Handler(void)
{
SystemTick++;
}
/**************************************************************************/
void DelayMs(uint32_t Milliseconds)
{
uint32_t StartTick = SystemTick;
while((SystemTick - StartTick) < 
Milliseconds
)
{
// __WFI(); // Idle CPU
}
}
/**************************************************************************/
int main(void)
{
printf(''RTC HSE Demo

'');
if (SysTick_Config(SystemCoreClock / 1000))
{
printf(''SysTick setup failure

'');
/* Capture error */
while (1);
}
RTC_Configuration();
#define DELAY 1100
while(1) // Do not exit
{
RTC_TimeTypeDef RTC_TimeStructureA, RTC_TimeStructureB;
uint32_t subsecondA, subsecondB;
uint32_t MilliSeconds;
do
{
subsecondA
= 
RTC_GetSubSecond
(); // 0..7999
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructureA);
subsecondB
= 
RTC_GetSubSecond
();
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructureB);
}
while((RTC_TimeStructureA.RTC_Seconds == RTC_TimeStructureB.RTC_Seconds) && (subsecondA == subsecondB));
MilliSeconds = ((7999 - subsecondB) / (8000 / 1000));
printf(''%02d:%02d.%03d'',
RTC_TimeStructureB.RTC_Minutes,
RTC_TimeStructureB.RTC_Seconds,
MilliSeconds);
// Predict Next expected MM:SS.sss
MilliSeconds += DELAY;
while(MilliSeconds >= 1000)
{
MilliSeconds -= 1000;
RTC_TimeStructureB.RTC_Seconds++;
if (RTC_TimeStructureB.RTC_Seconds >= 60)
{
RTC_TimeStructureB.RTC_Seconds -= 60;
RTC_TimeStructureB.RTC_Minutes++;
if (RTC_TimeStructureB.RTC_Minutes >= 60)
RTC_TimeStructureB.RTC_Minutes -= 60;
}
}
printf('' --> '');
printf(''%02d:%02d.%03d'',
RTC_TimeStructureB.RTC_Minutes,
RTC_TimeStructureB.RTC_Seconds,
MilliSeconds);
printf(''

'');
DelayMs(DELAY);
}
}
/**************************************************************************/
/* Implementation of putchar (also used by printf function to output data) */
int SendChar(int ch) /* Write character to Serial Port */
{
ITM_SendChar(ch); // From core_cm4.c
return(ch);
}
/**************************************************************************/
#include <
rt_misc.h
>
#pragma import(__use_no_semihosting_swi)
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
int fputc (int ch, FILE *f) { return (SendChar(ch)); }
int ferror (FILE *f) {
/* Your implementation of ferror */
return EOF;
}
void _ttywrch (int ch) { SendChar(ch); }
void _sys_exit (int return_code) { for (;;); }
/**************************************************************************/
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d

'', file, line) */
while (1)
{}
}
#endif
/**************************************************************************/

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on April 01, 2014 at 04:41

0690X00000605YCQAY.png
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..