Modified for BG96
Fork of mbed-dev by
Diff: targets/TARGET_STM/rtc_api.c
- Revision:
- 184:08ed48f1de7f
- Parent:
- 182:a56a73fd2a6f
- Child:
- 186:707f6e361f3e
--- a/targets/TARGET_STM/rtc_api.c Tue Mar 20 17:01:51 2018 +0000 +++ b/targets/TARGET_STM/rtc_api.c Thu Apr 19 17:12:19 2018 +0100 @@ -58,10 +58,9 @@ } #if MBED_CONF_TARGET_LSE_AVAILABLE - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! RCC_OscInitStruct.LSEState = RCC_LSE_ON; - RCC_OscInitStruct.LSIState = RCC_LSI_OFF; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { error("Cannot initialize RTC with LSE\n"); @@ -81,9 +80,8 @@ __HAL_RCC_BACKUPRESET_RELEASE(); // Enable LSI clock - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! - RCC_OscInitStruct.LSEState = RCC_LSE_OFF; RCC_OscInitStruct.LSIState = RCC_LSI_ON; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { error("Cannot initialize RTC with LSI\n"); @@ -228,7 +226,10 @@ timeinfo.tm_sec = timeStruct.Seconds; // Convert to timestamp - time_t t = _rtc_mktime(&timeinfo); + time_t t; + if (_rtc_maketime(&timeinfo, &t, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) { + return 0; + } return t; } @@ -242,7 +243,7 @@ // Convert the time into a tm struct tm timeinfo; - if (_rtc_localtime(t, &timeinfo) == false) { + if (_rtc_localtime(t, &timeinfo, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) { return; } @@ -329,6 +330,11 @@ void rtc_set_wake_up_timer(uint32_t delta) { +#define RTC_CLOCK_US (((uint64_t)RTC_CLOCK << 32 ) / 1000000) + + uint32_t WakeUpCounter; + uint32_t WakeUpClock; + /* Ex for Wakeup period resolution with RTCCLK=32768 Hz : * RTCCLK_DIV2: ~122us < wakeup period < ~4s * RTCCLK_DIV4: ~244us < wakeup period < ~8s @@ -337,19 +343,21 @@ * CK_SPRE_16BITS: 1s < wakeup period < (0xFFFF+ 1) x 1 s = 65536 s (18 hours) * CK_SPRE_17BITS: 18h+1s < wakeup period < (0x1FFFF+ 1) x 1 s = 131072 s (36 hours) */ - uint32_t WakeUpClock[6] = {RTC_WAKEUPCLOCK_RTCCLK_DIV2, RTC_WAKEUPCLOCK_RTCCLK_DIV4, RTC_WAKEUPCLOCK_RTCCLK_DIV8, RTC_WAKEUPCLOCK_RTCCLK_DIV16, RTC_WAKEUPCLOCK_CK_SPRE_16BITS, RTC_WAKEUPCLOCK_CK_SPRE_17BITS}; - uint8_t ClockDiv[4] = {2, 4, 8, 16}; - uint32_t WakeUpCounter; - uint8_t DivIndex = 0; - - do { - WakeUpCounter = delta / (ClockDiv[DivIndex] * 1000000 / RTC_CLOCK); - DivIndex++; - } while ( (WakeUpCounter > 0xFFFF) && (DivIndex < 4) ); - - if (WakeUpCounter > 0xFFFF) { - WakeUpCounter = delta / 1000000; - DivIndex++; + if (delta < (0x10000 * 2 / RTC_CLOCK * 1000000) ) { // (0xFFFF + 1) * RTCCLK_DIV2 / RTC_CLOCK * 1s + WakeUpCounter = (((uint64_t)delta * RTC_CLOCK_US) >> 32) >> 1 ; + WakeUpClock = RTC_WAKEUPCLOCK_RTCCLK_DIV2; + } else if (delta < (0x10000 * 4 / RTC_CLOCK * 1000000) ) { + WakeUpCounter = (((uint64_t)delta * RTC_CLOCK_US) >> 32) >> 2 ; + WakeUpClock = RTC_WAKEUPCLOCK_RTCCLK_DIV4; + } else if (delta < (0x10000 * 8 / RTC_CLOCK * 1000000) ) { + WakeUpCounter = (((uint64_t)delta * RTC_CLOCK_US) >> 32) >> 3 ; + WakeUpClock = RTC_WAKEUPCLOCK_RTCCLK_DIV8; + } else if (delta < (0x10000 * 16 / RTC_CLOCK * 1000000) ) { + WakeUpCounter = (((uint64_t)delta * RTC_CLOCK_US) >> 32) >> 4 ; + WakeUpClock = RTC_WAKEUPCLOCK_RTCCLK_DIV16; + } else { + WakeUpCounter = (delta / 1000000) ; + WakeUpClock = RTC_WAKEUPCLOCK_CK_SPRE_16BITS; } irq_handler = (void (*)(void))lp_ticker_irq_handler; @@ -357,8 +365,8 @@ NVIC_EnableIRQ(RTC_WKUP_IRQn); RtcHandle.Instance = RTC; - if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, 0xFFFF & WakeUpCounter, WakeUpClock[DivIndex - 1]) != HAL_OK) { - error("rtc_set_wake_up_timer init error (%d)\n", DivIndex); + if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, (uint32_t)WakeUpCounter, WakeUpClock) != HAL_OK) { + error("rtc_set_wake_up_timer init error\n"); } }