cancel
Showing results for 
Search instead for 
Did you mean: 

RTC write

arunl4g
Associate II
Posted on October 12, 2015 at 17:35

hi 

can anyone tel me what wrong with the below code ?..i cant bale to write 

void rtc_Initialize()

{

int i = 0 ;

RTC_TimeTypeDef RTC_TimeStruct;

SystemInit();

/* Enable the PWR clock */

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

 /* Allow access to RTC */

 PWR_BackupAccessCmd(ENABLE);

/***Configures the External Low Speed oscillator (LSE)****/

 RCC_LSEConfig(RCC_LSE_ON);

 /* Wait till LSE is ready */

// while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET){};

 /* Select the RTC Clock Source */

 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

 /* Enable the RTC Clock */

 RCC_RTCCLKCmd(ENABLE);

 /* Wait for RTC APB registers synchronisation */

 RTC_WaitForSynchro();

 /* Calendar Configuration with LSI supposed at 32KHz */

 RTC_InitTypeDef RTC_InitStructure;

 RTC_InitStructure.RTC_AsynchPrediv = 0x7F;

 RTC_InitStructure.RTC_SynchPrediv =  0xFF; /// final one is 1hz

 RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;

 RTC_Init(&RTC_InitStructure);

 RTC_TimeStruct.RTC_H12 = RTC_H12_AM ;

 RTC_TimeStruct.RTC_Hours = 10;

 RTC_TimeStruct.RTC_Minutes = 30;

 RTC_TimeStruct.RTC_Seconds = 0;

 RTC_SetTime(RTC_Format_BIN, &RTC_TimeStruct);

 for(i = 0; i < 100000; i++);

      while(1)

      {

      RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct);

      }

}

8 REPLIES 8
Posted on October 12, 2015 at 17:51

Ok, perhaps you can explain a) what you think it should do, and b) what it's actually doing.

Is there a reason you don't wait for LSE to start? Does it start? Do you have an LSE crystal?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
arunl4g
Associate II
Posted on October 12, 2015 at 20:10

i am using a stm32f429 discovery board. i just want to set the time and get the time..

but i am getting the get time value as 0.

no reason for comment the LSE and even uncomment also get  the same.

jpeacock
Associate II
Posted on October 12, 2015 at 20:11

I assume this is not an 'F1 RTC.

Check your sync prescale:

 RTC_InitStructure.RTC_SynchPrediv =  0xFF; /// final one is 1hz

It should be set to (HSE clock rate / 128) - 1 (13 bit result).

  Jack Peacock

jpeacock
Associate II
Posted on October 12, 2015 at 20:14

Ignore that, it's for an HSE RTC source.  0xFF should be correct if your LSE is 32.768KHz.

Posted on October 12, 2015 at 20:38

i am using a stm32f429 discovery board.

That doesn't come with an LSE crystal. Does it work if you use the LSI?

If you've added one, you should confirm the thing is working by outputting the clock signal via MCO1 (PA8)

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 12, 2015 at 20:56

// STM32F4 SWV-LSI-RTC-SIMPLE - sourcer32@gmail.com
#include <
stdio.h
>
#include ''stm32f4xx.h''
//******************************************************************************
// From http://forums.arm.com/index.php?showtopic=13949
volatile unsigned int *DWT_CYCCNT = (volatile unsigned int *)0xE0001004; //address of the register
volatile unsigned int *DWT_CONTROL = (volatile unsigned int *)0xE0001000; //address of the register
volatile unsigned int *SCB_DEMCR = (volatile unsigned int *)0xE000EDFC; //address of the register
//******************************************************************************
void EnableTiming(void)
{
static int enabled = 0;
if (!enabled)
{
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
*SCB_DEMCR |= 0x01000000;
*DWT_CYCCNT = 0; // reset the counter
*DWT_CONTROL |= 1 ; // enable the counter
enabled = 1;
}
}
//******************************************************************************
void TimingDelay(unsigned int tick)
{
unsigned int start, current;
start = *DWT_CYCCNT;
do
{
current = *DWT_CYCCNT;
} while((current - start) < 
tick
);
}
//******************************************************************************
//#define RTC_CLOCK_SOURCE_LSE /* LSE used as RTC source clock */
#define RTC_CLOCK_SOURCE_LSI /* LSI used as RTC source clock */
void RTC_Configuration(void)
{
RTC_InitTypeDef RTC_InitStructure;
RTC_TimeTypeDef RTC_TimeStructure;
RTC_DateTypeDef RTC_DateStructure;
uint32_t uwSynchPrediv, uwAsynchPrediv;
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
PWR_BackupAccessCmd(ENABLE);
#if defined (RTC_CLOCK_SOURCE_LSI) /* LSI used as RTC source clock*/
/* The RTC Clock may varies due to LSI frequency dispersion. */
/* Enable the LSI OSC */
RCC_LSICmd(ENABLE);
/* Wait till LSI is ready */
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
{
}
/* Select the LSI as RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
/* ck_spre(1Hz) = RTCCLK(LSI) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)*/
uwSynchPrediv
= 
0xFF
;
uwAsynchPrediv
= 
0x7F
;
#elif defined (RTC_CLOCK_SOURCE_LSE) /* LSE used as RTC source clock */
/* Enable the LSE OSC */
RCC_LSEConfig(RCC_LSE_ON);
/* Wait till LSE is ready */
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{
}
/* Select the LSE as RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
/* ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)*/
uwSynchPrediv
= 
0xFF
;
uwAsynchPrediv
= 
0x7F
;
#else
#error Please select the RTC Clock source inside the main.c file
#endif /* RTC_CLOCK_SOURCE_LSI */
/* Enable the RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
/* Set the Time - 23:59:57 */
RTC_TimeStructure.RTC_Hours
= 
0x23
;
RTC_TimeStructure.RTC_Minutes
= 
0x59
;
RTC_TimeStructure.RTC_Seconds
= 
0x57
;
/* Set the Date - 30-Nov-2014 */
RTC_DateStructure.RTC_Month
= 
RTC_Month_November
;
RTC_DateStructure.RTC_Date
= 
0x30
;
RTC_DateStructure.RTC_Year
= 
0x14
;
RTC_DateStructure.RTC_WeekDay
= 
RTC_Weekday_Sunday
;
/* Calendar Configuration */
RTC_InitStructure.RTC_AsynchPrediv
= 
uwAsynchPrediv
;
RTC_InitStructure.RTC_SynchPrediv
= 
uwSynchPrediv
;
RTC_InitStructure.RTC_HourFormat
= 
RTC_HourFormat_24
;
RTC_Init(&RTC_InitStructure);
/* Set Current Time and Date */
RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);
RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure);
if (RCC->CSR & 0x1)
printf(''LSI On
'');
else
printf(''LSI Off
'');
if (RCC->CSR & 0x2)
printf(''LSI Ready
'');
else
printf(''LSI Not Ready
'');
if (RCC->BDCR & 0x1)
printf(''LSE On
'');
else
printf(''LSE Off
'');
if (RCC->BDCR & 0x2)
printf(''LSE Ready
'');
else
printf(''LSE Not Ready
'');
if (RCC->BDCR & 0x4)
printf(''LSE ByPass On
'');
else
printf(''LSE ByPass Off
'');
if (RCC->BDCR & 0x8000)
printf(''RTC Clock Enabled
'');
else
printf(''RTC Clock Disabled
'');
switch(RCC->BDCR & 0x300)
{
case 0x100 : puts(''RTC Clock Source LSE''); break;
case 0x200 : puts(''RTC Clock Source LSI''); break;
case 0x300 : printf(''RTC Clock Source HSE/%d'', (RCC->CFGR >> 16) & 0x1F); break;
default : puts(''RTC Clock Unknown'');
}
}
//******************************************************************************
void RTC_TimeShow(void)
{
RTC_TimeTypeDef RTC_TimeStructure;
RTC_DateTypeDef RTC_DateStructure;
/* Get the current Time */
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);
RTC_GetDate(RTC_Format_BIN, &RTC_DateStructure);
/* Display time Format : hh:mm:ss DD/MM/YY */
printf(''%0.2d:%0.2d:%0.2d %0.2d/%0.2d/%0.2d
'',
RTC_TimeStructure.RTC_Hours, RTC_TimeStructure.RTC_Minutes, RTC_TimeStructure.RTC_Seconds,
RTC_DateStructure.RTC_Date, RTC_DateStructure.RTC_Month, RTC_DateStructure.RTC_Year );
}
//******************************************************************************
#if defined (RTC_CLOCK_SOURCE_LSE) /* LSE used as RTC source clock*/
void ClockMCO(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOs clocks */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
/* Enable SYSCFG clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* Configure MCO (PA8) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Output LSE clock on MCO pin (PA8) */
RCC_MCO1Config(RCC_MCO1Source_LSE, RCC_MCO1Div_1);
}
#endif
//****************************************************************************
int main(void)
{
int i;
EnableTiming();
RTC_Configuration();
#if defined (RTC_CLOCK_SOURCE_LSE) /* LSE used as RTC source clock*/
Clock_MCO();
#endif
for(i=0; i<
10
; i++)
{
RTC_TimeShow();
TimingDelay(SystemCoreClock); // One Second of ticks
}
while(1); /* Infinite loop */
}
//******************************************************************************
// Hosting of stdio functionality through SWV - Serial Wire Viewer
//******************************************************************************
#include <rt_misc.h>
#pragma import(__use_no_semihosting_swi)
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
int fputc(int ch, FILE *f)
{
ITM_SendChar(ch);
return(ch);
}
int fgetc(FILE *f)
{
char ch;
ch = '?';
return((int)ch);
}
int ferror(FILE *f)
{
/* Your implementation of ferror */
return EOF;
}
void _ttywrch(int ch)
{
ITM_SendChar(ch);
}
void _sys_exit(int return_code)
{
label: goto label; /* endless loop */
}
//****************************************************************************
#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) */
/* Infinite loop */
while (1)
{
}
}
#endif
//******************************************************************************
LSI On
LSI Ready
LSE Off
LSE Not Ready
LSE ByPass Off
RTC Clock Enabled
RTC Clock Source LSI
23:59:57 30/11/14
23:59:57 30/11/14
23:59:58 30/11/14
23:59:59 30/11/14
00:00:00 01/12/14
00:00:01 01/12/14
00:00:02 01/12/14
00:00:03 01/12/14
00:00:04 01/12/14
00:00:05 01/12/14

In other circumstance you might want to check the configuration state of the RTC, and reset it when necessary
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
arunl4g
Associate II
Posted on October 13, 2015 at 11:12

thank you clive.

As i am in learnign stage, i would like to know 1.what are the three registers SCB_DEMCR,DWT_CONTROL,DWT_CYCCNT. 2. why we have to use it . 3 and also why i have to configure MCO. cant i use LSE without configure MCO?

volatile unsigned int *DWT_CYCCNT = (volatile unsigned int *)0xE0001004; //address of the register
volatile unsigned int *DWT_CONTROL = (volatile unsigned int *)0xE0001000; //address of the register
volatile unsigned int *SCB_DEMCR = (volatile unsigned int *)0xE000EDFC; //address of the register
volatile unsigned int *DWT_CYCCNT = (volatile unsigned int *)0xE0001004; //address of the register
volatile unsigned int *DWT_CONTROL = (volatile unsigned int *)0xE0001000; //address of the register
volatile unsigned int *SCB_DEMCR = (volatile unsigned int *)0xE000EDFC; //address of the register

Posted on October 13, 2015 at 13:47

I'm using the cycle counter in the Data Watchpoint and Trace Unit so I can make a ONE second delay that is agnostic to the speed the processor is running. It did this rather than use some arbitrary software delay because it's predictable. ARM has documentation for these registers, check the TRM for the M3 core, or online resources.

I set up MCO so the LSE could be measured externally, if you probe the pins of the crystal you will alter the characteristics of the circuit. I did this so we don't get into a circular conversations about ''is the LSE working''. The RTC doesn't need the MCO output, it's for observation. Additionally I added code to output the state of some internal bit settings, to allow for a better understand of what is and is not working.

The prescaler for the LSI are probably a bit off, as the frequency is likely closer to 37 KHz than 32.768 KHz.

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