Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[xxx_F746XG] enhance RTC api #1638

Merged
merged 2 commits into from
Apr 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@


#if !defined (LSE_STARTUP_TIMEOUT)
#define LSE_STARTUP_TIMEOUT ((uint32_t)100) /*!< Time out for LSE start up, in ms */
#define LSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for LSE start up, in ms */
#endif /* HSE_STARTUP_TIMEOUT */


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@


#if !defined (LSE_STARTUP_TIMEOUT)
#define LSE_STARTUP_TIMEOUT ((uint32_t)100) /*!< Time out for LSE start up, in ms */
#define LSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for LSE start up, in ms */
#endif /* HSE_STARTUP_TIMEOUT */


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@


#if !defined (LSE_STARTUP_TIMEOUT)
#define LSE_STARTUP_TIMEOUT ((uint32_t)100) /*!< Time out for LSE start up, in ms */
#define LSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for LSE start up, in ms */
#endif /* HSE_STARTUP_TIMEOUT */


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#define DEVICE_SPISLAVE 1

#define DEVICE_RTC 1
#define DEVICE_RTC_LSI 0

#define DEVICE_PWMOUT 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#define DEVICE_SPISLAVE 1

#define DEVICE_RTC 1
#define DEVICE_RTC_LSI 0

#define DEVICE_PWMOUT 1

Expand Down
58 changes: 36 additions & 22 deletions libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F7/rtc_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@

#include "mbed_error.h"

#if DEVICE_RTC_LSI
static int rtc_inited = 0;
#endif

static RTC_HandleTypeDef RtcHandle;

void rtc_init(void)
Expand All @@ -52,33 +56,35 @@ void rtc_init(void)
__HAL_RCC_BACKUPRESET_FORCE();
__HAL_RCC_BACKUPRESET_RELEASE();

#if !DEVICE_RTC_LSI
// 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) {
RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { // Check if LSE has started correctly
// Connect LSE to RTC
__HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE);
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
rtc_freq = LSE_VALUE;
} else {
// 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_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI);
__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 = LSI_VALUE;
error("Cannot initialize RTC with LSE\n");
}

// Check if RTC is already initialized
if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) return;
#else
// 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
__HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI);
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
// This value is LSI typical value. To be measured precisely using a timer input capture for example.
rtc_freq = LSI_VALUE;
#endif

// Enable RTC
__HAL_RCC_RTC_ENABLE();
Expand Down Expand Up @@ -117,15 +123,23 @@ void rtc_free(void)
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(RTC->ISR != 7) {
return 1;
} else {
return 0;
}
#if DEVICE_RTC_LSI
return rtc_inited;
#else
if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) {
return 1;
} else {
return 0;
}
#endif
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#define DEVICE_SPISLAVE 1

#define DEVICE_RTC 1
#define DEVICE_RTC_LSI 1

#define DEVICE_PWMOUT 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#define DEVICE_SPISLAVE 1

#define DEVICE_RTC 1
#define DEVICE_RTC_LSI 0

#define DEVICE_PWMOUT 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#define DEVICE_SPISLAVE 1

#define DEVICE_RTC 1
#define DEVICE_RTC_LSI 1

#define DEVICE_PWMOUT 1

Expand Down
54 changes: 35 additions & 19 deletions libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32L1/rtc_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@

#include "mbed_error.h"

#if DEVICE_RTC_LSI
static int rtc_inited = 0;
#endif

RTC_HandleTypeDef RtcHandle;

Expand All @@ -42,8 +44,10 @@ void rtc_init(void)
RCC_OscInitTypeDef RCC_OscInitStruct;
uint32_t rtc_freq = 0;

#if DEVICE_RTC_LSI
if (rtc_inited) return;
rtc_inited = 1;
#endif

RtcHandle.Instance = RTC;

Expand All @@ -57,33 +61,35 @@ void rtc_init(void)
__HAL_RCC_BACKUPRESET_FORCE();
__HAL_RCC_BACKUPRESET_RELEASE();

#if !DEVICE_RTC_LSI
// Enable LSE Oscillator
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
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_ON; // External 32.768 kHz clock on OSC_IN/OSC_OUT
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) {
RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { // Check if LSE has started correctly
// Connect LSE to RTC
__HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE);
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
rtc_freq = LSE_VALUE;
} else {
// 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_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI);
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
// This value is LSI typical value. To be measured precisely using a timer input capture for example.
rtc_freq = 40000;
error("Cannot initialize RTC with LSE\n");
}

// Check if RTC is already initialized
if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) return;
#else
// 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
__HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI);
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
// This value is LSI typical value. To be measured precisely using a timer input capture for example.
rtc_freq = 40000;
#endif

// Enable RTC
__HAL_RCC_RTC_ENABLE();
Expand Down Expand Up @@ -129,12 +135,22 @@ void rtc_free(void)
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
HAL_RCC_OscConfig(&RCC_OscInitStruct);

#if DEVICE_RTC_LSI
rtc_inited = 0;
#endif
}

int rtc_isenabled(void)
{
return rtc_inited;
#if DEVICE_RTC_LSI
return rtc_inited;
#else
if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) {
return 1;
} else {
return 0;
}
#endif
}

/*
Expand Down