mirror of https://github.com/ARMmbed/mbed-os.git
commit
8577dc9680
|
@ -159,7 +159,7 @@ typedef struct
|
||||||
* @{
|
* @{
|
||||||
*/
|
*/
|
||||||
#define RCC_DBP_TIMEOUT_VALUE ((uint32_t)100)
|
#define RCC_DBP_TIMEOUT_VALUE ((uint32_t)100)
|
||||||
#define RCC_LSE_TIMEOUT_VALUE ((uint32_t)100)
|
#define RCC_LSE_TIMEOUT_VALUE ((uint32_t)5000)
|
||||||
/**
|
/**
|
||||||
* @}
|
* @}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -48,6 +48,7 @@
|
||||||
#define DEVICE_SPISLAVE 1
|
#define DEVICE_SPISLAVE 1
|
||||||
|
|
||||||
#define DEVICE_RTC 1
|
#define DEVICE_RTC 1
|
||||||
|
#define DEVICE_RTC_LSI 0
|
||||||
|
|
||||||
#define DEVICE_PWMOUT 1
|
#define DEVICE_PWMOUT 1
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,9 @@
|
||||||
|
|
||||||
#include "mbed_error.h"
|
#include "mbed_error.h"
|
||||||
|
|
||||||
|
#if DEVICE_RTC_LSI
|
||||||
static int rtc_inited = 0;
|
static int rtc_inited = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
static RTC_HandleTypeDef RtcHandle;
|
static RTC_HandleTypeDef RtcHandle;
|
||||||
|
|
||||||
|
@ -43,8 +45,10 @@ void rtc_init(void)
|
||||||
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
|
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
|
||||||
uint32_t rtc_freq = 0;
|
uint32_t rtc_freq = 0;
|
||||||
|
|
||||||
|
#if DEVICE_RTC_LSI
|
||||||
if (rtc_inited) return;
|
if (rtc_inited) return;
|
||||||
rtc_inited = 1;
|
rtc_inited = 1;
|
||||||
|
#endif
|
||||||
|
|
||||||
RtcHandle.Instance = RTC;
|
RtcHandle.Instance = RTC;
|
||||||
|
|
||||||
|
@ -58,6 +62,7 @@ void rtc_init(void)
|
||||||
__HAL_RCC_BACKUPRESET_FORCE();
|
__HAL_RCC_BACKUPRESET_FORCE();
|
||||||
__HAL_RCC_BACKUPRESET_RELEASE();
|
__HAL_RCC_BACKUPRESET_RELEASE();
|
||||||
|
|
||||||
|
#if !DEVICE_RTC_LSI
|
||||||
// Enable LSE Oscillator
|
// Enable LSE Oscillator
|
||||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | 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.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
|
||||||
|
@ -69,24 +74,27 @@ void rtc_init(void)
|
||||||
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
|
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
|
||||||
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
|
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
|
||||||
rtc_freq = LSE_VALUE;
|
rtc_freq = LSE_VALUE;
|
||||||
} else { // LSE didn't start, try with LSI
|
} else {
|
||||||
// Enable LSI clock
|
error("Cannot initialize RTC with LSE\n");
|
||||||
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
|
|
||||||
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
|
|
||||||
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
|
|
||||||
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
|
|
||||||
error("Cannot initialize RTC with LSI\n");
|
|
||||||
}
|
|
||||||
// This value is LSI typical value. To be measured precisely using a timer input capture for example.
|
|
||||||
rtc_freq = 40000;
|
|
||||||
}
|
}
|
||||||
|
#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
|
||||||
|
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
|
||||||
|
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
|
||||||
|
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
|
||||||
|
error("Cannot initialize RTC with LSI\n");
|
||||||
|
}
|
||||||
|
// This value is LSI typical value. To be measured precisely using a timer input capture for example.
|
||||||
|
rtc_freq = 40000;
|
||||||
|
#endif
|
||||||
|
|
||||||
// Check if RTC is already initialized
|
// Check if RTC is already initialized
|
||||||
if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) return;
|
if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) return;
|
||||||
|
@ -129,12 +137,22 @@ void rtc_free(void)
|
||||||
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
|
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
|
||||||
HAL_RCC_OscConfig(&RCC_OscInitStruct);
|
HAL_RCC_OscConfig(&RCC_OscInitStruct);
|
||||||
|
|
||||||
|
#if DEVICE_RTC_LSI
|
||||||
rtc_inited = 0;
|
rtc_inited = 0;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int rtc_isenabled(void)
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Reference in New Issue