2020-10-04 12:32 PM
On my STM32L151CB-A (external PCB), the following issue is occurring when using the internal Real Time Clock:
I configure Date, Time and Alarm A using the initialization routines generated by CubeMX. The Alarm A is set to trigger 7 seconds after the beginning of program execution. The alarm interrupt is sent through the EXTI line 17. Clock source of the RTC is the internal LSI 37kHz oscillator with prescaler values 124 and 295 to generate a 1Hz tick.
The RTC alarm interrupt is issued after 7 seconds - as we would expect. In contrast, the RTC Time Register TR (either accessed by RTC->TR or HAL_RTC_GetTime) does not seem to be incremented.
The UART debug output yields:
RTC TR00203000
RTC Seconds: 1800
RTC TR00203000
RTC Seconds: 1800
RTC TR00203000
RTC Seconds: 1800
RTC TR00203000
RTC Seconds: 1800
RTC TR00203000
RTC Seconds: 1800
RTC TR00203000
RTC Seconds: 1800
RTC TR00203000
RTC Seconds: 1800
RTC ALARM A EVENT
RTC TR00203000
RTC Seconds: 1800
RTC TR00203000
RTC Seconds: 1800
RTC TR00203000
RTC Seconds: 1800
The Time Register is updated when using the OCD server for on-chip debugging, inserting break points and reading the SFRs. Therefore, I think it is rather a read/update problem instead of a hardware peripheral issue.
The code of the main plus relevant functions:
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();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* Hello, world! */
debug_val("RTC TR", RTC->TR);
uint16_t seconds;
rtc_getTimeNowSeconds(&seconds);
debug_valdec("RTC Seconds: ", seconds);
debug_str("\r\n");
HAL_Delay(1000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
void rtc_getTimeNowSeconds(uint16_t *seconds) {
RTC_TimeTypeDef now;
HAL_RTC_GetTime(&hrtc, &now, RTC_FORMAT_BIN);
*seconds = 60 * now.Minutes + now.Seconds;
}
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc) {
debug_str("RTC ALARM A EVENT\r\n");
}
I assume CubeMX is not doing anything special. Here are the CubeMX initialization routines:
/* RTC init function */
void MX_RTC_Init(void)
{
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
RTC_AlarmTypeDef sAlarm = {0};
/** Initialize RTC Only
*/
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 124;
hrtc.Init.SynchPrediv = 295;
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 */
/* USER CODE END Check_RTC_BKUP */
/** Initialize RTC and set the Time and Date
*/
sTime.Hours = 20;
sTime.Minutes = 30;
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_MONDAY;
sDate.Month = RTC_MONTH_JANUARY;
sDate.Date = 1;
sDate.Year = 0;
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
{
Error_Handler();
}
/** Enable the Alarm A
*/
sAlarm.AlarmTime.Hours = 20;
sAlarm.AlarmTime.Minutes = 30;
sAlarm.AlarmTime.Seconds = 7;
sAlarm.AlarmTime.SubSeconds = 0;
sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_WEEKDAY;
sAlarm.AlarmDateWeekDay = 1;
sAlarm.Alarm = RTC_ALARM_A;
if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
{
Error_Handler();
}
}
void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle)
{
if(rtcHandle->Instance==RTC)
{
/* USER CODE BEGIN RTC_MspInit 0 */
/* USER CODE END RTC_MspInit 0 */
/* RTC clock enable */
__HAL_RCC_RTC_ENABLE();
/* RTC interrupt Init */
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
/* USER CODE BEGIN RTC_MspInit 1 */
/* USER CODE END RTC_MspInit 1 */
}
}
I have no idea where this problem might originate from. Thank you for your help!
Toolchain:
Solved! Go to Solution.
2020-10-04 12:39 PM
2020-10-04 12:39 PM
Read date after time.
JW
2020-10-04 12:55 PM
Thanks for the quick help! Works perfectly fine! Had been working on a F1 device before and ported the code to the L1 without adjusting the RTC Readout.
SOLVED