Skip to content

Commit 99a8b21

Browse files
committed
Merge pull request #274 from bcostm/master
[NUCLEO_F302R8] Add LSE configuration for RTC
2 parents 5eb40d3 + 3abdd41 commit 99a8b21

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/port_api.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@
2828
*******************************************************************************
2929
*/
3030
#include "port_api.h"
31+
32+
#if DEVICE_PORTIN || DEVICE_PORTOUT
33+
3134
#include "pinmap.h"
3235
#include "gpio_api.h"
3336
#include "error.h"
3437

35-
#if DEVICE_PORTIN || DEVICE_PORTOUT
36-
3738
extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
3839

3940
// high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...)

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/rtc_api.c

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,62 @@
3131

3232
#if DEVICE_RTC
3333

34+
#include "wait_api.h"
35+
36+
#define LSE_STARTUP_TIMEOUT ((uint16_t)500) // delay in ms
37+
3438
static int rtc_inited = 0;
3539

3640
void rtc_init(void) {
41+
uint32_t StartUpCounter = 0;
42+
uint32_t LSEStatus = 0;
43+
uint32_t rtc_freq = 0;
44+
3745
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); // Enable PWR clock
3846

39-
PWR_BackupAccessCmd(ENABLE); // Enable access to RTC
47+
PWR_BackupAccessCmd(ENABLE); // Enable access to Backup domain
4048

41-
// Be sure to start correctly
49+
// Reset back up registers
4250
RCC_BackupResetCmd(ENABLE);
4351
RCC_BackupResetCmd(DISABLE);
44-
45-
// Note: the LSI is used as RTC source clock
46-
// The RTC Clock may vary due to LSI frequency dispersion.
47-
RCC_LSICmd(ENABLE); // Enable LSI
48-
49-
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} // Wait until ready
50-
51-
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // Select LSI as RTC Clock Source
52-
53-
RCC_RTCCLKCmd(ENABLE); // Enable RTC Clock
54-
52+
53+
// Enable LSE clock
54+
RCC_LSEConfig(RCC_LSE_ON);
55+
56+
// Wait till LSE is ready
57+
do {
58+
LSEStatus = RCC_GetFlagStatus(RCC_FLAG_LSERDY);
59+
wait_ms(1);
60+
StartUpCounter++;
61+
} while((LSEStatus == 0) && (StartUpCounter <= LSE_STARTUP_TIMEOUT));
62+
63+
if (StartUpCounter > LSE_STARTUP_TIMEOUT) {
64+
// The LSE has not started, use LSI instead.
65+
// The RTC Clock may vary due to LSI frequency dispersion.
66+
RCC_LSEConfig(RCC_LSE_OFF);
67+
RCC_LSICmd(ENABLE); // Enable LSI
68+
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} // Wait until ready
69+
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // Select the RTC Clock Source
70+
rtc_freq = 40000; // [TODO] To be measured precisely using a timer input capture
71+
}
72+
else {
73+
// The LSE has correctly started
74+
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // Select the RTC Clock Source
75+
rtc_freq = LSE_VALUE;
76+
}
77+
78+
RCC_RTCCLKCmd(ENABLE); // Enable RTC Clock
79+
5580
RTC_WaitForSynchro(); // Wait for RTC registers synchronization
5681

57-
uint32_t lsi_freq = 40000; // [TODO] To be measured precisely using a timer input capture
58-
5982
RTC_InitTypeDef RTC_InitStructure;
6083
RTC_InitStructure.RTC_AsynchPrediv = 127;
61-
RTC_InitStructure.RTC_SynchPrediv = (lsi_freq / 128) - 1;
84+
RTC_InitStructure.RTC_SynchPrediv = (rtc_freq / 128) - 1;
6285
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
6386
RTC_Init(&RTC_InitStructure);
64-
87+
88+
PWR_BackupAccessCmd(DISABLE); // Disable access to Backup domain
89+
6590
rtc_inited = 1;
6691
}
6792

0 commit comments

Comments
 (0)