STM32L4P5: HardFault when tamper event occurs during breakpoint
I'm using the tamper peripheral clocked from the LSE (32.768Khz crystal), triggered by a tamper event on PC13. The core is clocked from the HSI, using the PLL to run at 120MHz.
The tamper peripheral seems to work fine under normal operation, but if an unmasked tamper event occurs during a breakpoint (paused in a debugger, or a semihosting printf for example), the core hardfaults.
This sounds it could potentially be related to Errata 2.15.1 where the RTC peripheral has incorrect behaviour when the core is stopped in a breakpoint.
I haven't tested them, but I think the RTC_Tamper examples in STM32CubeL4 should showcase this behaviour, but I'll include my code here for the avoidance of doubt:
RCC_OscInitTypeDef RCC_OscInitStruct = {
.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE,
.PLL.PLLState = RCC_PLL_NONE,
.LSEState = RCC_LSE_ON_RTC_ONLY,
.LSIState = RCC_LSI_OFF,
};
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {
.PeriphClockSelection = RCC_PERIPHCLK_RTC,
.RTCClockSelection = RCC_RTCCLKSOURCE_LSE,
};
__HAL_RCC_PWR_CLK_ENABLE();
HAL_PWR_EnableBkUpAccess();
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
Error_Handler();
}
__HAL_RCC_RTC_ENABLE();
__HAL_RCC_RTCAPB_CLK_ENABLE();
NVIC_SetPriority(TAMP_STAMP_IRQn, 0);
NVIC_EnableIRQ(TAMP_STAMP_IRQn);
RTC_TamperTypeDef stamperstructure = {
.Tamper = RTC_TAMPER_1,
.Trigger = RTC_TAMPERTRIGGER_LOWLEVEL,
.Filter = RTC_TAMPERFILTER_8SAMPLE,
.SamplingFrequency = RTC_TAMPERSAMPLINGFREQ_RTCCLK_DIV16384,
.PrechargeDuration = RTC_TAMPERPRECHARGEDURATION_1RTCCLK,
.TamperPullUp = RTC_TAMPER_PULLUP_DISABLE,
.TimeStampOnTamperDetection = RTC_TIMESTAMPONTAMPERDETECTION_DISABLE,
.NoErase = RTC_TAMPER_ERASE_BACKUP_ENABLE,
.MaskFlag = RTC_TAMPERMASK_FLAG_DISABLE,
};
RTC_HandleTypeDef RtcHandle = {
.Instance = RTC,
.Init.HourFormat = RTC_HOURFORMAT_24,
.Init.AsynchPrediv = LSE_VALUE / 1000,
.Init.SynchPrediv = 1000,
};
if (HAL_RTC_Init(&RtcHandle) != HAL_OK) {
Error_Handler();
}
/* Configure RTC Tamper Interrupt: EXTI configuration */
__HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
__HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_FALLING_EDGE();
/* Enable interrupt on selected tamper */
TAMP->IER |= stamperstructure.Tamper;
if (HAL_RTCEx_SetTamper(&RtcHandle, &stamperstructure) != HAL_OK) {
Error_Handler();
}As a side note, the HAL function HAL_RTCEx_SetTamper_IT does not initialise the trigger level properly in CR2, the function HAL_RTCEx_SetTamper seems correct though. I suspect the updated code from HAL_RTCEx_SetTamper just needs to be copied across to HAL_RTCEx_SetTamper_IT.
