cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 RTC only updates after Reset

Yahya Asl Soleimani
Associate II
Posted on February 14, 2018 at 16:59

Hello. I'm driving The RTC On STM32F4 Discovery and  I'm using STM32Cube for initialization. after everything, It only updates the value after a Reset. I don't know what the problem is. Can you help?

Here is the code for my main.c :

int main(void)

{

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/* MCU Configuration----------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */

HAL_Init();

/* USER CODE BEGIN Init */

/* USER CODE END Init */

/* Configure the system clock */

SystemClock_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_I2C1_Init();

MX_I2S3_Init();

MX_SPI1_Init();

MX_USB_HOST_Init();

MX_RTC_Init();

/* USER CODE BEGIN 2 */

RTC_TimeTypeDef stime;

/* USER CODE END 2 */

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

/* USER CODE END WHILE */

MX_USB_HOST_Process();

/* USER CODE BEGIN 3 */

MX_RTC_Init();

HAL_RTC_GetTime(&hrtc, &stime, FORMAT_BIN);

printf('second : %d, minute : %d, hour : %d\n', stime.Seconds, stime.Minutes, stime.Hours);

HAL_Delay(100);

}

/* USER CODE END 3 */

}

and here the code that Cube generated:

void MX_RTC_Init(void)

{

RTC_TimeTypeDef sTime;

RTC_DateTypeDef sDate;

/**Initialize RTC Only

*/

hrtc.Instance = RTC;

if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2){

hrtc.Init.HourFormat = RTC_HOURFORMAT_24;

hrtc.Init.AsynchPrediv = 100-1;

hrtc.Init.SynchPrediv = 320-1;

hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;

hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;

hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;

if (HAL_RTC_Init(&hrtc) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

/**Initialize RTC and set the Time and Date

*/

sTime.Hours = 0x17;

sTime.Minutes = 0x0;

sTime.Seconds = 0x0;

sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;

sTime.StoreOperation = RTC_STOREOPERATION_RESET;

if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

sDate.WeekDay = RTC_WEEKDAY_WEDNESDAY;

sDate.Month = RTC_MONTH_FEBRUARY;

sDate.Date = 0x14;

sDate.Year = 0x18;

if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);

}

}

void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle)

{

if(rtcHandle->Instance==RTC)

{

/* USER CODE BEGIN RTC_MspInit 0 */

/* USER CODE END RTC_MspInit 0 */

/* RTC clock enable */

__HAL_RCC_RTC_ENABLE();

/* USER CODE BEGIN RTC_MspInit 1 */

/* USER CODE END RTC_MspInit 1 */

}

}

void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle)

{

if(rtcHandle->Instance==RTC)

{

/* USER CODE BEGIN RTC_MspDeInit 0 */

/* USER CODE END RTC_MspDeInit 0 */

/* Peripheral clock disable */

__HAL_RCC_RTC_DISABLE();

/* USER CODE BEGIN RTC_MspDeInit 1 */

/* USER CODE END RTC_MspDeInit 1 */

}

}

what is it here that i'm doing wrong?

1 REPLY 1
john doe
Lead
Posted on February 14, 2018 at 19:17

Read the reference manual for your particular part.  You need to fetch both time AND date in order to read the values from the shadow registers.