diff --git a/src/CatenaSamd21.h b/src/CatenaSamd21.h index 60b3154..d434741 100644 --- a/src/CatenaSamd21.h +++ b/src/CatenaSamd21.h @@ -83,6 +83,11 @@ class CatenaSamd21 : public CatenaBase virtual void Sleep(uint32_t howLongInSeconds) override; + CatenaRTC::CalendarTime GetCalendarTime(void) + { + return this->m_Rtc.GetTime(); + } + protected: virtual void registerCommands(void); diff --git a/src/CatenaStm32L0.h b/src/CatenaStm32L0.h index f917d1d..0f47c18 100644 --- a/src/CatenaStm32L0.h +++ b/src/CatenaStm32L0.h @@ -102,6 +102,11 @@ class CatenaStm32L0 : public CatenaStm32 virtual void Sleep(uint32_t howLongInSeconds) override; + CatenaStm32L0Rtc::CalendarTime GetCalendarTime(void) + { + return this->m_Rtc.GetTime(); + } + protected: // methods virtual void registerCommands(void); diff --git a/src/lib/stm32/stm32l0/CatenaStm32L0Rtc.cpp b/src/lib/stm32/stm32l0/CatenaStm32L0Rtc.cpp index 9f1b035..82fbf17 100644 --- a/src/lib/stm32/stm32l0/CatenaStm32L0Rtc.cpp +++ b/src/lib/stm32/stm32l0/CatenaStm32L0Rtc.cpp @@ -16,6 +16,8 @@ Copyright notice: #ifdef ARDUINO_ARCH_STM32 #include "CatenaStm32L0Rtc.h" +#include "Catena_Log.h" + using namespace McciCatena; /****************************************************************************\ @@ -128,20 +130,38 @@ bool CatenaStm32L0Rtc::begin(bool fResetTime) { RTC_TimeTypeDef Time; RTC_DateTypeDef Date; + uint32_t RtcClock; // memset(&this->m_hRtc, 0, sizeof(this->m_hRtc)); this->m_hRtc.Instance = RTC; this->m_hRtc.Init.HourFormat = RTC_HOURFORMAT_24; -// this->m_hRtc.Init.AsynchPrediv = 37 - 1; /* 37kHz / 37 = 1000Hz */ -// this->m_hRtc.Init.SynchPrediv = 1000 - 1; /* 1000Hz / 1000 = 1Hz */ - -// this->m_hRtc.Init.AsynchPrediv = 64 - 1; /* 40kHz / 64 = 625Hz */ -// this->m_hRtc.Init.SynchPrediv = 625 - 1; /* 625Hz / 625 = 1Hz */ + RtcClock = __HAL_RCC_GET_RTC_SOURCE(); + if (RtcClock == RCC_RTCCLKSOURCE_LSI) + { + this->m_hRtc.Init.AsynchPrediv = 37 - 1; /* 37kHz / 37 = 1000Hz */ + this->m_hRtc.Init.SynchPrediv = 1000 - 1; /* 1000Hz / 1000 = 1Hz */ + } + else if (RtcClock == RCC_RTCCLKSOURCE_LSE) + { + this->m_hRtc.Init.AsynchPrediv = 128 - 1; /* 32768Hz / 128 = 256Hz */ + this->m_hRtc.Init.SynchPrediv = 256 - 1; /* 256Hz / 256 = 1Hz */ + } + else + { + /* + || use HSE clock -- + || we don't support use of HSE as RTC because it's connected to + || TCXO_OUT, and that's controlled by the LoRaWAN software. + */ + gLog.printf( + gLog.kError, + "?CatenaStm32L0Rtc::begin:" + " HSE can not be used for RTC clock!\n" + ); + return false; + } - this->m_hRtc.Init.AsynchPrediv = 128 - 1; /* 32768Hz / 128 = 256Hz */ -// this->m_hRtc.Init.SynchPrediv = 256 - 1; /* 256Hz / 256 = 1Hz */ - this->m_hRtc.Init.SynchPrediv = 256; /* 256Hz / 256 = 1Hz */ this->m_hRtc.Init.OutPut = RTC_OUTPUT_DISABLE; this->m_hRtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE; @@ -152,7 +172,11 @@ bool CatenaStm32L0Rtc::begin(bool fResetTime) if (HAL_RTC_Init(&this->m_hRtc) != HAL_OK) { - Serial.println("HAL_RTC_Init() failed"); + gLog.printf( + gLog.kError, + "?CatenaStm32L0Rtc::begin:" + " HAL_RTC_Init() failed\n" + ); return false; } @@ -174,7 +198,11 @@ bool CatenaStm32L0Rtc::begin(bool fResetTime) RTC_FORMAT_BIN ) != HAL_OK) { - Serial.println("HAL_RTC_SetTime() failed"); + gLog.printf( + gLog.kError, + "?CatenaStm32L0Rtc::begin:" + " HAL_RTC_SetTime() failed\n" + ); return false; } @@ -190,7 +218,11 @@ bool CatenaStm32L0Rtc::begin(bool fResetTime) RTC_FORMAT_BIN ) != HAL_OK) { - Serial.println("HAL_RTC_SetDate() failed"); + gLog.printf( + gLog.kError, + "?CatenaStm32L0Rtc::begin:" + " HAL_RTC_SetDate() failed\n" + ); return false; } @@ -249,7 +281,11 @@ bool CatenaStm32L0Rtc::SetTime(const CalendarTime *pNow) if (HAL_RTC_SetTime(&this->m_hRtc, &Time, RTC_FORMAT_BIN) != HAL_OK) { - Serial.println("HAL_RTC_SetTime() failed"); + gLog.printf( + gLog.kError, + "?CatenaStm32L0Rtc::begin:" + " HAL_RTC_SetTime() failed\n" + ); return false; } @@ -260,7 +296,11 @@ bool CatenaStm32L0Rtc::SetTime(const CalendarTime *pNow) if (HAL_RTC_SetDate(&this->m_hRtc, &Date, RTC_FORMAT_BIN) != HAL_OK) { - Serial.println("HAL_RTC_SetDate() failed"); + gLog.printf( + gLog.kError, + "?CatenaStm32L0Rtc::begin:" + " HAL_RTC_SetDate() failed\n" + ); return false; }