mbed library sources. Supersedes mbed-src.
Fork of mbed by
Revision 148:4802eb17e82b, committed 2016-10-17
- Comitter:
- rodriguise
- Date:
- Mon Oct 17 18:47:01 2016 +0000
- Parent:
- 147:30b64687e01f
- Commit message:
- backup
Changed in this revision
diff -r 30b64687e01f -r 4802eb17e82b targets/cmsis/TARGET_STM/TARGET_STM32L4/stm32l4xx_hal_rcc.h --- a/targets/cmsis/TARGET_STM/TARGET_STM32L4/stm32l4xx_hal_rcc.h Fri Sep 16 16:24:25 2016 +0100 +++ b/targets/cmsis/TARGET_STM/TARGET_STM32L4/stm32l4xx_hal_rcc.h Mon Oct 17 18:47:01 2016 +0000 @@ -163,7 +163,7 @@ * @{ */ #define RCC_DBP_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */ -#define RCC_LSE_TIMEOUT_VALUE LSE_STARTUP_TIMEOUT +#define RCC_LSE_TIMEOUT_VALUE ((uint32_t)5000) // LSE_STARTUP_TIMEOUT /** * @} */
diff -r 30b64687e01f -r 4802eb17e82b targets/hal/TARGET_STM/TARGET_STM32L4/device.h --- a/targets/hal/TARGET_STM/TARGET_STM32L4/device.h Fri Sep 16 16:24:25 2016 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32L4/device.h Mon Oct 17 18:47:01 2016 +0000 @@ -37,4 +37,6 @@ #include "objects.h" +#define DEVICE_RTC_LSI 0 + #endif
diff -r 30b64687e01f -r 4802eb17e82b targets/hal/TARGET_STM/TARGET_STM32L4/rtc_api.c --- a/targets/hal/TARGET_STM/TARGET_STM32L4/rtc_api.c Fri Sep 16 16:24:25 2016 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32L4/rtc_api.c Mon Oct 17 18:47:01 2016 +0000 @@ -28,31 +28,40 @@ ******************************************************************************* */ #include "rtc_api.h" - + #if DEVICE_RTC - + #include "mbed_error.h" - -#if DEVICE_RTC_LSI -static int rtc_inited = 0; -#endif - + +#if DEVICE_RTC_LSI + static int rtc_inited = 0; +#endif + static RTC_HandleTypeDef RtcHandle; - + void rtc_init(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; uint32_t rtc_freq = 0; - -#if DEVICE_RTC_LSI - if (rtc_inited) return; - rtc_inited = 1; -#endif - + +#if DEVICE_RTC_LSI + rtc_inited = 1; +#endif + RtcHandle.Instance = RTC; - -#if !DEVICE_RTC_LSI + + // Enable Power clock + __HAL_RCC_PWR_CLK_ENABLE(); + + // Enable access to Backup domain + HAL_PWR_EnableBkUpAccess(); + + // Reset Backup domain + __HAL_RCC_BACKUPRESET_FORCE(); + __HAL_RCC_BACKUPRESET_RELEASE(); + + // Enable LSE Oscillator RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! @@ -64,73 +73,60 @@ PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); rtc_freq = LSE_VALUE; - } else { - error("Cannot initialize RTC with LSE\n"); + } + else { + error("RTC error: LSE clock initialization failed."); } #else - // Enable Power clock - __HAL_RCC_PWR_CLK_ENABLE(); - - // Enable access to Backup domain - HAL_PWR_EnableBkUpAccess(); - - // Reset Backup domain - __HAL_RCC_BACKUPRESET_FORCE(); - __HAL_RCC_BACKUPRESET_RELEASE(); - - // Enable LSI clock - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; - 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"); + // Enable LSI clock + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; + 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"); + } + // Connect LSI to RTC + PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; + PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { + error("Cannot initialize RTC with LSI\n"); + } + // This value is LSI typical value. To be measured precisely using a timer input capture for example. + rtc_freq = 40000; } - // Connect LSI to RTC - PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; - PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; - if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { - error("Cannot initialize RTC with LSI\n"); - } - // This value is LSI typical value (see device datasheet) - rtc_freq = 32000; #endif - - // Check if RTC is already initialized - if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) return; - + // Enable RTC __HAL_RCC_RTC_ENABLE(); - + RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; RtcHandle.Init.AsynchPrediv = 127; RtcHandle.Init.SynchPrediv = (rtc_freq / 128) - 1; RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; - + if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { error("Cannot initialize RTC\n"); } } - + void rtc_free(void) { -#if DEVICE_RTC_LSI // Enable Power clock __HAL_RCC_PWR_CLK_ENABLE(); - + // Enable access to Backup domain HAL_PWR_EnableBkUpAccess(); - + // Reset Backup domain __HAL_RCC_BACKUPRESET_FORCE(); __HAL_RCC_BACKUPRESET_RELEASE(); - + // Disable access to Backup domain HAL_PWR_DisableBkUpAccess(); -#endif - + // Disable LSI and LSE clocks RCC_OscInitTypeDef RCC_OscInitStruct; RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; @@ -138,25 +134,23 @@ RCC_OscInitStruct.LSIState = RCC_LSI_OFF; RCC_OscInitStruct.LSEState = RCC_LSE_OFF; HAL_RCC_OscConfig(&RCC_OscInitStruct); - #if DEVICE_RTC_LSI rtc_inited = 0; #endif + } - + int rtc_isenabled(void) { #if DEVICE_RTC_LSI - return rtc_inited; + return rtc_inited; #else - if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) { - return 1; - } else { - return 0; - } + if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) return 1; + else return 0; #endif + } - + /* RTC Registers RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday @@ -179,56 +173,58 @@ RTC_DateTypeDef dateStruct; RTC_TimeTypeDef timeStruct; struct tm timeinfo; - + RtcHandle.Instance = RTC; - + // Read actual date and time // Warning: the time must be read first! HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN); HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN); - + // Setup a tm structure based on the RTC timeinfo.tm_wday = dateStruct.WeekDay; timeinfo.tm_mon = dateStruct.Month - 1; timeinfo.tm_mday = dateStruct.Date; - timeinfo.tm_year = dateStruct.Year + 68; + timeinfo.tm_year = dateStruct.Year + 100; timeinfo.tm_hour = timeStruct.Hours; timeinfo.tm_min = timeStruct.Minutes; timeinfo.tm_sec = timeStruct.Seconds; - // Daylight Saving Time information is not available - timeinfo.tm_isdst = -1; - + // Convert to timestamp time_t t = mktime(&timeinfo); - + return t; } - + void rtc_write(time_t t) { RTC_DateTypeDef dateStruct; RTC_TimeTypeDef timeStruct; - + RtcHandle.Instance = RTC; - + + // Enable Power clock + __HAL_RCC_PWR_CLK_ENABLE(); + + // Enable access to Backup domain + HAL_PWR_EnableBkUpAccess(); + // Convert the time into a tm struct tm *timeinfo = localtime(&t); - + // Fill RTC structures dateStruct.WeekDay = timeinfo->tm_wday; dateStruct.Month = timeinfo->tm_mon + 1; dateStruct.Date = timeinfo->tm_mday; - dateStruct.Year = timeinfo->tm_year - 68; + dateStruct.Year = timeinfo->tm_year - 100; timeStruct.Hours = timeinfo->tm_hour; timeStruct.Minutes = timeinfo->tm_min; timeStruct.Seconds = timeinfo->tm_sec; - timeStruct.TimeFormat = RTC_HOURFORMAT_24; + timeStruct.TimeFormat = RTC_HOURFORMAT12_PM; timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; - + // Change the RTC current date/time HAL_RTC_SetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN); HAL_RTC_SetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN); -} - -#endif +} \ No newline at end of file