cancel
Showing results for 
Search instead for 
Did you mean: 

Internal Timer of STM32F767

akash_praan
Associate III

Hello All, 

I am using the Internal RTC of STM32F767 MCU. 
The VBAT pin is connected to an External 3.3V Coin Cell Battery 

At Power ON (Debug window on STM32 Cube IDE), before HAL_INIT when i see the value of RTC BKP1R Register it is 0x5432 as I intended it to be. 

Then there is system clock config 

 

void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

/** Configure LSE Drive Capability
*/
__HAL_RCC_PWR_CLK_ENABLE();
HAL_PWR_EnableBkUpAccess();
__HAL_RCC_RTC_ENABLE();

__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_HIGH);

/** Configure the main internal regulator output voltage
*/

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);



/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 432;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 9;
RCC_OscInitStruct.PLL.PLLR = 2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}

/** Activate the Over-Drive mode*/
if (HAL_PWREx_EnableOverDrive() != HAL_OK)
{
Error_Handler();
}

/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
{
Error_Handler();
}
}

 

and then there is 

HAL_Delay(100);

/* USER CODE BEGIN SysInit */
backup_value_rtc_4(uint32_t) = hrtc.Instance->BKP1R;
now surprisingly this reads 1517797662 but it should be 21554 (0x5432)

so when i do MX_RTC_Init

 

static void MX_RTC_Init(void)
{
    /* USER CODE BEGIN RTC_Init 0 */
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
   Error_Handler();
}

    backup_value_rtc_1 = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1);
//    printf("Backup Register Value: 0x%04X\n", backup_value);

    /* Check if RTC is already initialized */
    if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) != 0x5432)
    {
        /* USER CODE END RTC_Init 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;

        // Set the time
        sTime.Hours = 12;        // 14:30:45
        sTime.Minutes = 22;
        sTime.Seconds = 45;
        sTime.TimeFormat = RTC_HOURFORMAT12_AM;  // For 24-hour format, this field is ignored
        sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
        sTime.StoreOperation = RTC_STOREOPERATION_RESET;

        if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
        {
           Error_Handler();
        }

        // Set the date
        sDate.WeekDay = RTC_WEEKDAY_THURSDAY;     // Set to Wednesday
        sDate.Month = RTC_MONTH_NOVEMBER;       // November
        sDate.Date = 14;                        // 13th
        sDate.Year = 24;                        // Year 2024

        if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
        {
           Error_Handler();
        }

        /* Mark RTC as initialized in the backup register */
        HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x5432);

        backup_value_rtc_3 = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1);

        /* USER CODE BEGIN RTC_Init 1 */
    }
    /* USER CODE END RTC_Init 2 */
}

 

then my RTC gets written everytime. 
Now SFR says 5432 is stored, why am I not able to read it correct using the API or directly accessing the register

akash_praan_0-1732636383756.png

akash_praan_1-1732636408137.png

 



1 REPLY 1

And what is value of hrtc.Instance?

JW