[xxx_F746XG] enhance RTC api

to support LSI and LSE with a define in device.h
pull/1638/head
dbestm 2016-03-31 10:44:01 +02:00
parent e42e174852
commit 8a1a67d189
3 changed files with 38 additions and 22 deletions

View File

@ -48,6 +48,7 @@
#define DEVICE_SPISLAVE 1
#define DEVICE_RTC 1
#define DEVICE_RTC_LSI 0
#define DEVICE_PWMOUT 1

View File

@ -49,6 +49,7 @@
#define DEVICE_SPISLAVE 1
#define DEVICE_RTC 1
#define DEVICE_RTC_LSI 0
#define DEVICE_PWMOUT 1

View File

@ -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)
@ -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 {
error("Cannot initialize RTC with LSE\n");
}
#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.");
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);
// [TODO] This value is LSI typical value. To be measured precisely using a timer input capture
// This value is LSI typical value. To be measured precisely using a timer input capture for example.
rtc_freq = LSI_VALUE;
}
// Check if RTC is already initialized
if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) return;
#endif
// Enable RTC
__HAL_RCC_RTC_ENABLE();
@ -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) {
#if DEVICE_RTC_LSI
return rtc_inited;
#else
if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) {
return 1;
} else {
return 0;
}
#endif
}
/*