cancel
Showing results for 
Search instead for 
Did you mean: 

How to run RTC with Nucleo STM32WB55

JM?�l
Associate II
/* USER CODE BEGIN PV */
RTC_TimeTypeDef RTC_Time={0,0,0,0,0,0,0,0};
/* USER CODE END PV */
 
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_RTC_Init();
  /* USER CODE BEGIN 2 */
  printf("A\n");
  printf("System Started\n");
 
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  HAL_GPIO_TogglePin(LD1_GPIO_Port,LD1_Pin);
	  HAL_Delay(1000);
	  HAL_RTC_GetTime(&hrtc,&RTC_Time,RTC_FORMAT_BIN);
	  printf("hh: %02d mm: %02d ss: %02d \n",RTC_Time.Hours,RTC_Time.Minutes, RTC_Time.Seconds);
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
 
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
 
  /** Configure LSE Drive Capability 
  */
  __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
  /** Configure the main internal regulator output voltage 
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE
                              |RCC_OSCILLATORTYPE_LSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK4|RCC_CLOCKTYPE_HCLK2
                              |RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.AHBCLK2Divider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.AHBCLK4Divider = RCC_SYSCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the peripherals clocks 
  */
  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SMPS|RCC_PERIPHCLK_RTC;
  PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  PeriphClkInitStruct.SmpsClockSelection = RCC_SMPSCLKSOURCE_HSE;
  PeriphClkInitStruct.SmpsDivSelection = RCC_SMPSCLKDIV_RANGE0;
 
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
}
 
static void MX_RTC_Init(void)
{
	/* USER CODE BEGIN RTC_Init 0 */
 
	/* USER CODE END RTC_Init 0 */
 
	RTC_TimeTypeDef sTime = {0};
	RTC_DateTypeDef sDate = {0};
 
	/* USER CODE BEGIN RTC_Init 1 */
 
	/* USER CODE END RTC_Init 1 */
	/** 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;
	hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
	if (HAL_RTC_Init(&hrtc) != HAL_OK)
	{
		Error_Handler();
	}
 
	/* USER CODE BEGIN Check_RTC_BKUP */
	if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2){
		/* USER CODE END Check_RTC_BKUP */
 
		/** Initialize RTC and set the Time and Date
		 */
		sTime.Hours = 0x0;
		sTime.Minutes = 0x0;
		sTime.Seconds = 0x0;
		sTime.SubSeconds = 0x0;
		sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
		sTime.StoreOperation = RTC_STOREOPERATION_RESET;
		if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
		{
			Error_Handler();
		}
		sDate.WeekDay = RTC_WEEKDAY_MONDAY;
		sDate.Month = RTC_MONTH_SEPTEMBER;
		sDate.Date = 0x1;
		sDate.Year = 0x0;
		if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
		{
			Error_Handler();
		}
		/* USER CODE BEGIN RTC_Init 2 */
		HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR0, 0x32F2);
	}else{
		if(__HAL_RCC_GET_FLAG(RCC_FLAG_BORRST)!= RESET){
			HAL_GPIO_WritePin(LD2_GPIO_Port,LD2_Pin,GPIO_PIN_SET);
		}
		if(__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST)!= RESET){
			HAL_GPIO_WritePin(LD3_GPIO_Port,LD3_Pin,GPIO_PIN_SET);
		}
	}
	__HAL_RCC_CLEAR_RESET_FLAGS();
 
	/* USER CODE END RTC_Init 2 */
}
 
void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
{
  if(hrtc->Instance==RTC)
  {
  /* USER CODE BEGIN RTC_MspInit 0 */
	 RCC_OscInitTypeDef RCC_OSCInitStruct;
	 RCC_PeriphCLKInitTypeDef PeripClkInitStruct;
 
	 HAL_PWR_EnableBkUpAccess();
	 RCC_OSCInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
	 RCC_OSCInitStruct.PLL.PLLState = RCC_PLL_NONE;
	 RCC_OSCInitStruct.LSEState = RCC_LSE_ON;
	 if(HAL_RCC_OscConfig(&RCC_OSCInitStruct)!=HAL_OK){
		 Error_Handler();
	 }
	 PeripClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
	 PeripClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
	 if(HAL_RCCEx_PeriphCLKConfig(&PeripClkInitStruct) != HAL_OK){
		 Error_Handler();
	 }
	 __HAL_RCC_RTCAPB_CLK_ENABLE();
  /* USER CODE END RTC_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_RTC_ENABLE();
  /* USER CODE BEGIN RTC_MspInit 1 */
 
  /* USER CODE END RTC_MspInit 1 */
  }
 
}

I try to run the RTC on an STM32WB55 nucleo board but i just reach to get 00:00:01.

Is there a command i don´t find in the examples to run the RTC.

I try to run with HSE/32 LSI1/LSI2 and LSE.

When I set the RTC witch HAL_RTC_SetTime() I can set the clock but the time is still standing on the setted clock value.

Is there a Solderbridge i had to remove or to Set on the nucleo?

Whene I read the manuale I belive that in the standard setting of jumper and solderbridge the RTC runs with the current of the ST-Link USB.

0 REPLIES 0