cancel
Showing results for 
Search instead for 
Did you mean: 

Failed to read time and date in STMWLE5 software

W.K. Tam
Associate III

I want to record the time of sensor activity in a program running in STM32WL MCU but only get the value of 0. The program is modified from the example LoRaWAN_End_Node. Below please find the RTC setting and the code to get the date and time. Can any expert help to solve the problem ?

< Codes in rtc.c>

/* RTC init function */
void MX_RTC_Init(void)
{

/* USER CODE BEGIN RTC_Init 0 */

/* USER CODE END RTC_Init 0 */

RTC_AlarmTypeDef sAlarm = {0};
RTC_TimeTypeDef sTime = {0}; // Added
RTC_DateTypeDef sDate = {0}; // Added
/* USER CODE BEGIN RTC_Init 1 */

/* USER CODE END RTC_Init 1 */

/** Initialize RTC Only
*/
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24; // Added
hrtc.Init.AsynchPrediv = RTC_PREDIV_A;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
// hrtc.Init.BinMode = RTC_BINARY_ONLY;
hrtc.Init.BinMode = RTC_BINARY_MIX; // added to try
hrtc.Init.BinMixBcdU = RTC_BINARY_MIX_BCDU_0; // added to try
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}

/* USER CODE BEGIN Check_RTC_BKUP */

/* USER CODE END Check_RTC_BKUP */

/** Initialize RTC and set the Time and Date
*/
if (HAL_RTCEx_SetSSRU_IT(&hrtc) != HAL_OK)
{
Error_Handler();
}

/** Enable the Alarm A
*/
sAlarm.BinaryAutoClr = RTC_ALARMSUBSECONDBIN_AUTOCLR_NO;
sAlarm.AlarmTime.SubSeconds = 0x0;
sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDBINMASK_NONE;
sAlarm.Alarm = RTC_ALARM_A;
if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, 0) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN RTC_Init 2 */
sTime.Hours = 0x22;
sTime.Minutes = 0x30;
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_AUGUST;
sDate.Date = 0x07;
sDate.Year = 0x23;

if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
/* USER CODE END RTC_Init 2 */

}

<Codes in main.c>

// .... <other codes>.....

while (1)
{

//......<other codes>....

if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == GPIO_PIN_RESET) // Set to SET for product
{
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
sTamper.minutes = sTime.Minutes;
sTamper.hours = sTime.Hours;
sTamper.date = sDate.Date;
sTamper.month = sDate.Month;
sTamper.year = sDate.Year;
if (Tamper != 1)
{
// Call tamper event record routine to store tamper start time in EEPROM
// tamper_sRec();
}
// Call tamper event routine to upload tamper event payload
Tamper_event();
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET); // for testing
sprintf((char*)tamtime, "Time: %02d.%02d\n\r", sTamper.hours, sTamper.minutes); // for testing
HAL_UART_Transmit(&huart2, tamtime, sizeof(tamtime), 300); // for testing
Tamper = 1;
tamper_eRec_flag = 0;
} else {
if (Tamper == 1)
{
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
eTamper.minutes = sTime.Minutes;
eTamper.hours = sTime.Hours;
eTamper.date = sDate.Date;
eTamper.month = sDate.Month;
eTamper.year = sDate.Year;
if (tamper_eRec_flag == 0)
{
// Call tamper event record routine to store tamper end time in EEPROM
// tamper_eRec();
tamper_eRec_flag = 1;
// Call tamper clear event routine to upload tamper clear payload
Tamper_clear();
}

HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET); // for testing
} else {
Tamper = 0;
}
}
// Start ADC Conversion
HAL_ADC_Start(&hadc);
// Read the ADC value of PA11
if (HAL_ADC_PollForConversion(&hadc, 100)== HAL_OK)
{
adc_value = HAL_ADC_GetValue(&hadc);
}
// Convert the ADC value to voltage (millivolts)
millivolts = (adc_value * 3300) / 4096;

// Check if voltage is below alert threshold
if (millivolts < 2500) {
if (millivolts < 2000)
{
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
Bateol.minutes = sTime.Minutes;
Bateol.hours = sTime.Hours;
Bateol.date = sDate.Date;
Bateol.month = sDate.Month;
Bateol.year = sDate.Year;
if (BatteryEOL != 1)
{
// Store Battery EOL time in EEPROM
// BatteryEOL_Rec();
// Upload battery end of line time to server
battery_eol_time();
}

BatteryEOL = 1; // Set battery end of life flag
HAL_GPIO_TogglePin(GPIOB, LED1_Pin); // for testing
} else {
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
Batlow.minutes = sTime.Minutes;
Batlow.hours = sTime.Hours;
Batlow.date = sDate.Date;
Batlow.month = sDate.Month;
Batlow.year = sDate.Year;
if (Batterylow != 1)
{
// Store Battery low time in EEPROM
// BatteryLOW_Rec():
// Upload battery low time to server
battery_low_time();
}

Batterylow = 1; // Set battery low alert flag
BatteryEOL = 0; // reset battery end of life flag
HAL_GPIO_WritePin(GPIOB, LED1_Pin, GPIO_PIN_SET); // for testing
}
} else {
// Clear alert indicator
Batterylow = 0;
BatteryEOL = 0;
HAL_GPIO_WritePin(GPIOB, LED1_Pin, GPIO_PIN_RESET);
}
HAL_Delay(30);
}
/* USER CODE END 3 */
}

2 REPLIES 2
Imen.D
ST Employee

Hello @W.K. Tam ,

Try adding below line before calling "MX_RTC_Init()":

hrtc.IsEnabled.RtcFeatures = UINT32_MAX; 

 

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
W.K. Tam
Associate III

Hi Imen,

Thanks for your help. I found that the sample program LoRaWAN_End_Node does not call "MX_RTC_Init()". How does it configure and use the RTC ? I tried to add the "MX_RTC_Init()" in the user code section with your suggested line below put before. I still got the zero in the time - hour and minute.

hrtc.IsEnabled.RtcFeatures = UINT32_MAX; 

Regards,

W.K. Tam