mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Revision:
340:28d1f895c6fe
Parent:
216:577900467c9e
Child:
402:09075a3b15e3
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/rtc_api.c	Mon Oct 06 11:45:07 2014 +0100
+++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/rtc_api.c	Thu Oct 09 08:15:07 2014 +0100
@@ -31,74 +31,90 @@
 
 #if DEVICE_RTC
 
-#include "wait_api.h"
-
-#define LSE_STARTUP_TIMEOUT ((uint16_t)500) // delay in ms
+#include "mbed_error.h"
 
 static int rtc_inited = 0;
 
+static RTC_HandleTypeDef RtcHandle;
+
 void rtc_init(void) {
-    uint32_t StartUpCounter = 0;
-    uint32_t LSEStatus = 0;
+    RCC_OscInitTypeDef RCC_OscInitStruct;
     uint32_t rtc_freq = 0;
 
-    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); // Enable PWR clock
+    if (rtc_inited) return;
+    rtc_inited = 1;
 
-    PWR_BackupAccessCmd(ENABLE); // Enable access to Backup domain
+    RtcHandle.Instance = RTC;
 
-    // Reset back up registers
-    RCC_BackupResetCmd(ENABLE);
-    RCC_BackupResetCmd(DISABLE);
+    // Enable Power clock
+    __PWR_CLK_ENABLE();
 
-    // Enable LSE clock
-    RCC_LSEConfig(RCC_LSE_ON);
+    // Enable access to Backup domain
+    HAL_PWR_EnableBkUpAccess();
+
+    // Reset Backup domain
+    __HAL_RCC_BACKUPRESET_FORCE();
+    __HAL_RCC_BACKUPRESET_RELEASE();
 
-    // Wait till LSE is ready
-    do {
-        LSEStatus = RCC_GetFlagStatus(RCC_FLAG_LSERDY);
-        wait_ms(1);
-        StartUpCounter++;
-    } while ((LSEStatus == 0) && (StartUpCounter <= LSE_STARTUP_TIMEOUT));
-
-    if (StartUpCounter > LSE_STARTUP_TIMEOUT) {
-        // The LSE has not started, use LSI instead.
-        // The RTC Clock may vary due to LSI frequency dispersion.
-        RCC_LSEConfig(RCC_LSE_OFF);
-        RCC_LSICmd(ENABLE); // Enable LSI
-        while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} // Wait until ready
-        RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // Select the RTC Clock Source
-        rtc_freq = 40000; // [TODO] To be measured precisely using a timer input capture
+    // Enable LSE Oscillator
+    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; /* External 32.768 kHz clock on OSC_IN/OSC_OUT */
+    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) {
+        // Connect LSE to RTC
+        __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
+        rtc_freq = LSE_VALUE;
     } else {
-        // The LSE has correctly started
-        RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // Select the RTC Clock Source
-        rtc_freq = LSE_VALUE;
+        // 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("RTC error: LSI clock initialization failed.");
+        }
+        // Connect LSI to RTC
+        __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
+        // [TODO] This value is LSI typical value. To be measured precisely using a timer input capture.
+        rtc_freq = 32000;
     }
 
-    RCC_RTCCLKCmd(ENABLE); // Enable RTC Clock
-
-    RTC_WaitForSynchro(); // Wait for RTC registers synchronization
+    // Enable RTC
+    __HAL_RCC_RTC_ENABLE();
 
-    RTC_InitTypeDef RTC_InitStructure;
-    RTC_InitStructure.RTC_AsynchPrediv = 127;
-    RTC_InitStructure.RTC_SynchPrediv    = (rtc_freq / 128) - 1;
-    RTC_InitStructure.RTC_HourFormat   = RTC_HourFormat_24;
-    RTC_Init(&RTC_InitStructure);
+    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;
 
-    PWR_BackupAccessCmd(DISABLE); // Disable access to Backup domain
-
-    rtc_inited = 1;
+    if (HAL_RTC_Init(&RtcHandle) != HAL_OK) {
+        error("RTC error: RTC initialization failed.");
+    }
 }
 
 void rtc_free(void) {
-    // Reset RTC
-    PWR_BackupAccessCmd(ENABLE); // Enable access to Backup Domain
-    RTC_DeInit();
-    RCC_BackupResetCmd(ENABLE);
-    RCC_BackupResetCmd(DISABLE);
-    // Disable RTC, LSE and LSI clocks
-    RCC_RTCCLKCmd(DISABLE);
-    RCC_LSEConfig(RCC_LSE_OFF);
-    RCC_LSICmd(DISABLE);
+    // Enable Power clock
+    __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();
+
+    // Disable LSI and LSE clocks
+    RCC_OscInitTypeDef RCC_OscInitStruct;
+    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
+    RCC_OscInitStruct.PLL.PLLState   = RCC_PLL_NONE;
+    RCC_OscInitStruct.LSIState       = RCC_LSI_OFF;
+    RCC_OscInitStruct.LSEState       = RCC_LSE_OFF;
+    HAL_RCC_OscConfig(&RCC_OscInitStruct);
 
     rtc_inited = 0;
 }
@@ -129,18 +145,21 @@
     RTC_TimeTypeDef timeStruct;
     struct tm timeinfo;
 
+    RtcHandle.Instance = RTC;
+
     // Read actual date and time
-    RTC_GetTime(RTC_Format_BIN, &timeStruct);
-    RTC_GetDate(RTC_Format_BIN, &dateStruct);
+    // Warning: the time must be read first!
+    HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN);
+    HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN);
 
     // Setup a tm structure based on the RTC
-    timeinfo.tm_wday = dateStruct.RTC_WeekDay;
-    timeinfo.tm_mon  = dateStruct.RTC_Month - 1;
-    timeinfo.tm_mday = dateStruct.RTC_Date;
-    timeinfo.tm_year = dateStruct.RTC_Year + 100;
-    timeinfo.tm_hour = timeStruct.RTC_Hours;
-    timeinfo.tm_min  = timeStruct.RTC_Minutes;
-    timeinfo.tm_sec  = timeStruct.RTC_Seconds;
+    timeinfo.tm_wday = dateStruct.WeekDay;
+    timeinfo.tm_mon  = dateStruct.Month - 1;
+    timeinfo.tm_mday = dateStruct.Date;
+    timeinfo.tm_year = dateStruct.Year + 100;
+    timeinfo.tm_hour = timeStruct.Hours;
+    timeinfo.tm_min  = timeStruct.Minutes;
+    timeinfo.tm_sec  = timeStruct.Seconds;
 
     // Convert to timestamp
     time_t t = mktime(&timeinfo);
@@ -152,24 +171,26 @@
     RTC_DateTypeDef dateStruct;
     RTC_TimeTypeDef timeStruct;
 
+    RtcHandle.Instance = RTC;
+
     // Convert the time into a tm
     struct tm *timeinfo = localtime(&t);
 
     // Fill RTC structures
-    dateStruct.RTC_WeekDay = timeinfo->tm_wday;
-    dateStruct.RTC_Month   = timeinfo->tm_mon + 1;
-    dateStruct.RTC_Date    = timeinfo->tm_mday;
-    dateStruct.RTC_Year    = timeinfo->tm_year - 100;
-    timeStruct.RTC_Hours   = timeinfo->tm_hour;
-    timeStruct.RTC_Minutes = timeinfo->tm_min;
-    timeStruct.RTC_Seconds = timeinfo->tm_sec;
-    timeStruct.RTC_H12     = RTC_HourFormat_24;
+    dateStruct.WeekDay        = timeinfo->tm_wday;
+    dateStruct.Month          = timeinfo->tm_mon + 1;
+    dateStruct.Date           = timeinfo->tm_mday;
+    dateStruct.Year           = timeinfo->tm_year - 100;
+    timeStruct.Hours          = timeinfo->tm_hour;
+    timeStruct.Minutes        = timeinfo->tm_min;
+    timeStruct.Seconds        = timeinfo->tm_sec;
+    timeStruct.TimeFormat     = RTC_HOURFORMAT12_PM;
+    timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
+    timeStruct.StoreOperation = RTC_STOREOPERATION_RESET;
 
     // Change the RTC current date/time
-    PWR_BackupAccessCmd(ENABLE); // Enable access to RTC
-    RTC_SetDate(RTC_Format_BIN, &dateStruct);
-    RTC_SetTime(RTC_Format_BIN, &timeStruct);
-    PWR_BackupAccessCmd(DISABLE); // Disable access to RTC
+    HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN);
+    HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN);
 }
 
 #endif