0

When place this function in main.c,the program get stuck,can't go to the next statement.I tried to add printf("error!")before eachError_Handler();,But nothing appearred in USART ports.

If it is removed,the whole program could work.

The program edited in MDK-ARM is for STM32F401RCT6 .

Here is the function,It's generated by STM32CubeMX.

 void MX_RTC_Init(void)
{

  RTC_TimeTypeDef sTime = {0};
  RTC_DateTypeDef sDate = {0};

  /** Initialize RTC Only
  */
  hrtc.Instance = RTC;
  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  hrtc.Init.AsynchPrediv = 127;
  hrtc.Init.SynchPrediv = 255;
  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();
  }

  /* USER CODE BEGIN Check_RTC_BKUP */
    if(HAL_RTCEx_BKUPRead(&hrtc,RTC_BKP_DR0)!=0X5050)
    {
  /* USER CODE END Check_RTC_BKUP */

  /** Initialize RTC and set the Time and Date
  */
  sTime.Hours = 9;
  sTime.Minutes = 40;
  sTime.Seconds = 0;
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }
  sDate.WeekDay = RTC_WEEKDAY_THURSDAY ;
  sDate.Month = RTC_MONTH_MARCH ;
  sDate.Date = 24;
  sDate.Year = 22;

  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN RTC_Init 2 */
    HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0X5050);
    }
  /* USER CODE END RTC_Init 2 */

}

here are the contents in main.c

without MX_RTC_Init(),the program could run normally

#include "main.h"
#include "usart.h"
#include "gpio.h"
#include "rtc.h"

void SystemClock_Config(void);

int main(void)
{

  RTC_TimeTypeDef RTC_TimeStruct;
  RTC_DateTypeDef RTC_DateStruct;

  SystemClock_Config();
  delay_init();
  MX_GPIO_Init();
//  MX_RTC_Init();//when add this function,the program get stuck
  MX_USART1_UART_Init();

  while (1)
  {...}
  • welcome to stack overflow. do you know if you are hitting the Error_Handler(), and if yes, what does it do? Often these will have infinite loops in them so that's what I'd look at first. – Sol Arnu May 11 '22 at 17:27
  • Your UART may be not printing because you initialize uart **after** rtc. I suggest you use debugging mode with breakpoints OR you comment out half of RTC function and see if the code reaches the next stage. Find the exact line of code inside MX_RTC_Init that hangs the entire system. – Ilya May 13 '22 at 12:36

0 Answers0