mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
Diff: targets/TARGET_STM/rtc_api.c
- Revision:
- 189:f392fc9709a3
- Parent:
- 188:bcfe06ba3d64
--- a/targets/TARGET_STM/rtc_api.c Thu Nov 08 11:46:34 2018 +0000 +++ b/targets/TARGET_STM/rtc_api.c Wed Feb 20 22:31:08 2019 +0000 @@ -93,6 +93,10 @@ // Enable RTC __HAL_RCC_RTC_ENABLE(); +#if defined __HAL_RCC_RTCAPB_CLK_ENABLE /* part of STM32L4 */ + __HAL_RCC_RTCAPB_CLK_ENABLE(); +#endif /* __HAL_RCC_RTCAPB_CLK_ENABLE */ + RtcHandle.Instance = RTC; RtcHandle.State = HAL_RTC_STATE_RESET; @@ -105,6 +109,12 @@ RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; +#if defined (RTC_OUTPUT_REMAP_NONE) + RtcHandle.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE; +#endif /* defined (RTC_OUTPUT_REMAP_NONE) */ +#if defined (RTC_OUTPUT_PULLUP_NONE) + RtcHandle.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE; +#endif /* defined (RTC_OUTPUT_PULLUP_NONE) */ #endif /* TARGET_STM32F1 */ if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { @@ -257,11 +267,11 @@ int rtc_isenabled(void) { -#if !(TARGET_STM32F1) - return ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS); -#else /* TARGET_STM32F1 */ +#if defined (RTC_FLAG_INITS) /* all STM32 except STM32F1 */ + return LL_RTC_IsActiveFlag_INITS(RTC); +#else /* RTC_FLAG_INITS */ /* TARGET_STM32F1 */ return ((RTC->CRL & RTC_CRL_RSF) == RTC_CRL_RSF); -#endif /* TARGET_STM32F1 */ +#endif /* RTC_FLAG_INITS */ } @@ -296,7 +306,9 @@ } } +#ifdef __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG(); +#endif } uint32_t rtc_read_lp(void) @@ -341,25 +353,60 @@ void rtc_set_wake_up_timer(timestamp_t timestamp) { + /* RTC periodic auto wake up timer is used + * This WakeUpTimer is loaded to an init value => WakeUpCounter + * then timer starts counting down (even in low-power modes) + * When it reaches 0, the WUTF flag is set in the RTC_ISR register + */ uint32_t WakeUpCounter; - uint32_t current_lp_time; + uint32_t WakeUpClock = RTC_WAKEUPCLOCK_RTCCLK_DIV4; - current_lp_time = rtc_read_lp(); + core_util_critical_section_enter(); + /* MBED API gives the timestamp value to set + * WakeUpCounter is then the delta between timestamp and the current tick (LPTICKER_counter) + * If the current tick preceeds timestamp value, max U32 is added + */ + uint32_t current_lp_time = rtc_read_lp(); if (timestamp < current_lp_time) { WakeUpCounter = 0xFFFFFFFF - current_lp_time + timestamp; } else { WakeUpCounter = timestamp - current_lp_time; } + /* RTC WakeUpCounter is 16 bits + * Corresponding time value depends on WakeUpClock + * - RTC clock divided by 4 : max WakeUpCounter value is 8s (precision around 122 us) + * - RTC clock divided by 8 : max WakeUpCounter value is 16s (precision around 244 us) + * - RTC clock divided by 16 : max WakeUpCounter value is 32s (precision around 488 us) + * - 1 Hz internal clock 16b : max WakeUpCounter value is 18h (precision 1 s) + * - 1 Hz internal clock 17b : max WakeUpCounter value is 36h (precision 1 s) + */ if (WakeUpCounter > 0xFFFF) { - WakeUpCounter = 0xFFFF; + WakeUpClock = RTC_WAKEUPCLOCK_RTCCLK_DIV8; + WakeUpCounter = WakeUpCounter / 2; + + if (WakeUpCounter > 0xFFFF) { + WakeUpClock = RTC_WAKEUPCLOCK_RTCCLK_DIV16; + WakeUpCounter = WakeUpCounter / 2; + + if (WakeUpCounter > 0xFFFF) { + /* Tick value needs to be translated in seconds : TICK * 16 (previous div16 value) / RTC clock (32768) */ + WakeUpClock = RTC_WAKEUPCLOCK_CK_SPRE_16BITS; + WakeUpCounter = WakeUpCounter / 2048; + + if (WakeUpCounter > 0xFFFF) { + /* In this case 2^16 is added to the 16-bit counter value */ + WakeUpClock = RTC_WAKEUPCLOCK_CK_SPRE_17BITS; + WakeUpCounter = WakeUpCounter - 0x10000; + } + } + } } - core_util_critical_section_enter(); RtcHandle.Instance = RTC; HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle); - if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, WakeUpCounter, RTC_WAKEUPCLOCK_RTCCLK_DIV4) != HAL_OK) { + if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, WakeUpCounter, WakeUpClock) != HAL_OK) { error("rtc_set_wake_up_timer init error\n"); }