cancel
Showing results for 
Search instead for 
Did you mean: 

R/W to RTC backup registers

mbmail4
Associate II
Posted on August 27, 2016 at 04:39

Hi.

I’m using an RTC example program from the CUBE F3

(STM32F3Nucleo)

download, and it work ok, except I cannot read or write to the RTC backup registers.

The example configures RTC as follows:

__HAL_RCC_PWR_CLK_ENABLE();

HAL_PWR_EnableBkUpAccess();

__HAL_RCC_RTC_ENABLE();

When I try to read and write to the backup registers, I cannot read back what I have previously written.

HAL_RTCEx_BKUPWrite(&RtcHandle, RTC_BKP_DR1, 0x32F2);

HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR1);

Even if I precede the read and or write with:

__HAL_RCC_PWR_CLK_ENABLE();

HAL_PWR_EnableBkUpAccess();

Any ideas?

Martin.

2 REPLIES 2
sunny
Associate II
Posted on August 29, 2016 at 05:49

Hi,

This might be happening because your RTC Oscillator Type and RTC Peripheral Clock might not be configured.

Try to call the HAL_RTC_MspInit() and then perform RTC Read/Write.

This should take care.

Regards,

Sunny

mbmail4
Associate II
Posted on September 06, 2016 at 02:52

Hi Sunny.

I'm using the Nucleo board, and so far I cant seem to get the external 32khz clock to work. Most of the code I'm using has come from CUBE.

I changed to external clock as below, but it wont start.  

PeriphClkInit.

RTCClockSelection

= RCC_RTCCLKSOURCE_LSE;

All my work so far has been with clock source set to RCC_RTCCLKSOURCE_LSI at 40khz.

Within the RTC_MspInit() function I call:

__HAL_RCC_PWR_CLK_ENABLE();

HAL_PWR_EnableBkUpAccess();

__HAL_RCC_RTC_ENABLE();

and my sys clock config function is below: Sorry for the edits, blame ST.

void

SystemClock_Config(

void

)

{

RCC_OscInitTypeDef

RCC_OscInitStruct;

RCC_ClkInitTypeDef

RCC_ClkInitStruct;

RCC_PeriphCLKInitTypeDef

PeriphClkInit;

RCC_OscInitStruct.

OscillatorType

= RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;

RCC_OscInitStruct.

HSEState

= RCC_HSE_BYPASS;

RCC_OscInitStruct.

LSIState

= RCC_LSI_ON;

RCC_OscInitStruct.

PLL

.

PLLState

= RCC_PLL_ON;

RCC_OscInitStruct.

PLL

.

PLLSource

= RCC_PLLSOURCE_HSE;

RCC_OscInitStruct.

PLL

.

PLLMUL

= RCC_PLL_MUL9;

RCC_OscInitStruct.

PLL

.

PREDIV

= RCC_PREDIV_DIV1;

if

(HAL_RCC_OscConfig(&RCC_OscInitStruct) !=

HAL_OK

)

{

Error_Handler();

}

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_DIV2;

RCC_ClkInitStruct.

APB2CLKDivider

= RCC_HCLK_DIV1;

if

(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) !=

HAL_OK

)

{

Error_Handler();

}

PeriphClkInit.

PeriphClockSelection

= RCC_PERIPHCLK_USB | RCC_PERIPHCLK_USART1 |

RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 |

RCC_PERIPHCLK_RTC;

PeriphClkInit.

Usart1ClockSelection

= RCC_USART1CLKSOURCE_PCLK2;

PeriphClkInit.

Usart2ClockSelection

= RCC_USART2CLKSOURCE_PCLK1;

PeriphClkInit.

Usart3ClockSelection

= RCC_USART3CLKSOURCE_PCLK1;

PeriphClkInit.

RTCClockSelection

= RCC_RTCCLKSOURCE_LSI;

PeriphClkInit.

USBClockSelection

= RCC_USBCLKSOURCE_PLL_DIV1_5;

if

(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) !=

HAL_OK

)

{

Error_Handler();

}

HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);

HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/* SysTick_IRQn interrupt configuration */

HAL_NVIC_SetPriority(

SysTick_IRQn

, 0, 0);

}

static

void

MX_RTC_Init(

void

)

{

RTC_TimeTypeDef

sTime;

RTC_DateTypeDef

sDate;

RTC_AlarmTypeDef

sAlarm;

RTC_TamperTypeDef

sTamper;

/**Initialize RTC and set the Time and Date

*/

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();

}

sTime.

Hours

= 0x0;

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();

}

sDate.

WeekDay

= RTC_WEEKDAY_MONDAY;

sDate.

Month

= RTC_MONTH_JANUARY;

sDate.

Date

= 0x1;

sDate.

Year

= 0x0;

if

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

HAL_OK

)

{

Error_Handler();

}

/**Enable the Alarm A

*/

sAlarm.

AlarmTime

.

Hours

= 0x0;

sAlarm.

AlarmTime

.

Minutes

= 0x0;

sAlarm.

AlarmTime

.

Seconds

= 0x0;

sAlarm.

AlarmTime

.

SubSeconds

= 0x0;

sAlarm.

AlarmTime

.

DayLightSaving

= RTC_DAYLIGHTSAVING_NONE;

sAlarm.

AlarmTime

.

StoreOperation

= RTC_STOREOPERATION_RESET;

sAlarm.

AlarmMask

= RTC_ALARMMASK_NONE;

sAlarm.

AlarmSubSecondMask

= RTC_ALARMSUBSECONDMASK_ALL;

sAlarm.

AlarmDateWeekDaySel

= RTC_ALARMDATEWEEKDAYSEL_DATE;

sAlarm.

AlarmDateWeekDay

= 0x1;

sAlarm.

Alarm

= RTC_ALARM_A;

if

(HAL_RTC_SetAlarm(&hrtc, &sAlarm, RTC_FORMAT_BCD) !=

HAL_OK

)

{

Error_Handler();

}

/**Enable the Alarm B

*/

sAlarm.

AlarmDateWeekDay

= 0x1;

sAlarm.

Alarm

= RTC_ALARM_B;

if

(HAL_RTC_SetAlarm(&hrtc, &sAlarm, RTC_FORMAT_BCD) !=

HAL_OK

)

{

Error_Handler();

}

/**Enable the WakeUp

*/

if

(HAL_RTCEx_SetWakeUpTimer(&hrtc, 0, RTC_WAKEUPCLOCK_RTCCLK_DIV16) !=

HAL_OK

)

{

Error_Handler();

}

/**Enable the RTC Tamper 2

*/

sTamper.

Tamper

= RTC_TAMPER_2;

sTamper.

Trigger

= RTC_TAMPERTRIGGER_RISINGEDGE;

sTamper.

Filter

= RTC_TAMPERFILTER_DISABLE;

sTamper.

SamplingFrequency

= RTC_TAMPERSAMPLINGFREQ_RTCCLK_DIV32768;

sTamper.

PrechargeDuration

= RTC_TAMPERPRECHARGEDURATION_1RTCCLK;

sTamper.

TamperPullUp

= RTC_TAMPER_PULLUP_ENABLE;

sTamper.

TimeStampOnTamperDetection

= RTC_TIMESTAMPONTAMPERDETECTION_ENABLE;

if

(HAL_RTCEx_SetTamper(&hrtc, &sTamper) !=

HAL_OK

)

{

Error_Handler();

}

}