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

Fix issue 129; check RTC clock source and set prescaler value #201

Merged
merged 2 commits into from
Jul 19, 2019
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
5 changes: 5 additions & 0 deletions src/CatenaSamd21.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 5 additions & 0 deletions src/CatenaStm32L0.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
66 changes: 53 additions & 13 deletions src/lib/stm32/stm32l0/CatenaStm32L0Rtc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Copyright notice:
#ifdef ARDUINO_ARCH_STM32

#include "CatenaStm32L0Rtc.h"
#include "Catena_Log.h"

using namespace McciCatena;

/****************************************************************************\
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down