mbed library sources

Fork of mbed-src by mbed official

Files at this revision

API Documentation at this revision

Comitter:
mbed_official
Date:
Thu Apr 24 15:30:06 2014 +0100
Parent:
169:60881100c991
Child:
171:3d240fda1f07
Commit message:
Synchronized with git revision 99a8b2179e9ca41d7254f71e1e46202718aa663d

Full URL: https://github.com/mbedmicro/mbed/commit/99a8b2179e9ca41d7254f71e1e46202718aa663d/

[NUCLEO_F302R8] Add LSE configuration for RTC

Changed in this revision

targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/port_api.c Show annotated file Show diff for this revision Revisions of this file
targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/rtc_api.c Show annotated file Show diff for this revision Revisions of this file
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/port_api.c	Thu Apr 24 13:15:06 2014 +0100
+++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/port_api.c	Thu Apr 24 15:30:06 2014 +0100
@@ -28,12 +28,13 @@
  *******************************************************************************
  */
 #include "port_api.h"
+
+#if DEVICE_PORTIN || DEVICE_PORTOUT
+
 #include "pinmap.h"
 #include "gpio_api.h"
 #include "error.h"
 
-#if DEVICE_PORTIN || DEVICE_PORTOUT
-
 extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
 
 // high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...)
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/rtc_api.c	Thu Apr 24 13:15:06 2014 +0100
+++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/rtc_api.c	Thu Apr 24 15:30:06 2014 +0100
@@ -31,37 +31,62 @@
 
 #if DEVICE_RTC
 
+#include "wait_api.h"
+
+#define LSE_STARTUP_TIMEOUT ((uint16_t)500) // delay in ms
+
 static int rtc_inited = 0;
 
 void rtc_init(void) {
+    uint32_t StartUpCounter = 0;
+    uint32_t LSEStatus = 0;
+    uint32_t rtc_freq = 0;
+
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); // Enable PWR clock
 
-    PWR_BackupAccessCmd(ENABLE); // Enable access to RTC
+    PWR_BackupAccessCmd(ENABLE); // Enable access to Backup domain
 
-    // Be sure to start correctly
+    // Reset back up registers
     RCC_BackupResetCmd(ENABLE);
     RCC_BackupResetCmd(DISABLE);
+  
+    // Enable LSE clock
+    RCC_LSEConfig(RCC_LSE_ON);
 
-    // Note: the LSI is used as RTC source clock
-    // The RTC Clock may vary due to LSI frequency dispersion.
-    RCC_LSICmd(ENABLE); // Enable LSI
-
-    while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} // Wait until ready
+    // Wait till LSE is ready
+    do {
+        LSEStatus = RCC_GetFlagStatus(RCC_FLAG_LSERDY);
+        wait_ms(1);
+        StartUpCounter++;
+    } while((LSEStatus == 0) && (StartUpCounter <= LSE_STARTUP_TIMEOUT));
 
-    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // Select LSI as RTC Clock Source
-
-    RCC_RTCCLKCmd(ENABLE); // Enable RTC Clock
-
+    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  
+    }  
+    else {
+        // The LSE has correctly started
+        RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // Select the RTC Clock Source
+        rtc_freq = LSE_VALUE;
+    }
+  
+    RCC_RTCCLKCmd(ENABLE); // Enable RTC Clock 
+      
     RTC_WaitForSynchro(); // Wait for RTC registers synchronization
 
-    uint32_t lsi_freq = 40000; // [TODO] To be measured precisely using a timer input capture
-
     RTC_InitTypeDef RTC_InitStructure;
     RTC_InitStructure.RTC_AsynchPrediv = 127;
-    RTC_InitStructure.RTC_SynchPrediv  = (lsi_freq / 128) - 1;
+    RTC_InitStructure.RTC_SynchPrediv	 = (rtc_freq / 128) - 1;
     RTC_InitStructure.RTC_HourFormat   = RTC_HourFormat_24;
     RTC_Init(&RTC_InitStructure);
-
+    
+    PWR_BackupAccessCmd(DISABLE); // Disable access to Backup domain
+      
     rtc_inited = 1;
 }