Skip to content

Fix #216: Move system clock calibration and read analog code to BSP #217

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,7 @@ This sketch demonstrates the use of the Catena FSM class to implement the `Turns

- HEAD includes the following changes.

- [#216](https://github.com/mcci-catena/Catena-Arduino-Platform/issues/216) Move system clock calibration and read analog code to BSP. (0.17.0.25)
- [#222](https://github.com/mcci-catena/Catena-Arduino-Platform/issues/222) Use `Arduino_LoRaWAN_network` consistently as base for `LoRaWAN` classes. Requires Arduino_LoRaWAN 0.6.0.20 or later. (0.17.0.20)
- [#213](https://github.com/mcci-catena/Catena-Arduino-Platform/issues/213) Vbat voltage read is inaccurate on STM32L0 platform. (0.17.0.12)
- [#211](https://github.com/mcci-catena/Catena-Arduino-Platform/issues/211) `Catena_Si1133` cannot be copied or moved. (0.17.0.11)
Expand Down
2 changes: 1 addition & 1 deletion src/CatenaBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Copyright notice:
#define CATENA_ARDUINO_PLATFORM_VERSION_CALC(major, minor, patch, local) \
(((major) << 24u) | ((minor) << 16u) | ((patch) << 8u) | (local))

#define CATENA_ARDUINO_PLATFORM_VERSION CATENA_ARDUINO_PLATFORM_VERSION_CALC(0, 17, 0, 20) /* v0.17.0.20 */
#define CATENA_ARDUINO_PLATFORM_VERSION CATENA_ARDUINO_PLATFORM_VERSION_CALC(0, 17, 0, 25) /* v0.17.0.25 */

#define CATENA_ARDUINO_PLATFORM_VERSION_GET_MAJOR(v) \
(((v) >> 24u) & 0xFFu)
Expand Down
21 changes: 21 additions & 0 deletions src/CatenaStm32L0.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,22 @@ class CatenaStm32L0::LoRaWAN : public Arduino_LoRaWAN_network,
CatenaStm32L0 *m_pCatena;
};

#ifdef _mcci_arduino_version
# if _mcci_arduino_version < _mcci_arduino_version_calc(2, 6, 0, 10)
# define MCCI_ARDUINO_BSP_SUPPORT_READ_ANALOG 0
# define MCCI_ARDUINO_BSP_SUPPORT_CLOCK_CALIBRATION 0
# else
# define MCCI_ARDUINO_BSP_SUPPORT_READ_ANALOG 1
# define MCCI_ARDUINO_BSP_SUPPORT_CLOCK_CALIBRATION 1
# endif
#else
# define MCCI_ARDUINO_BSP_SUPPORT_READ_ANALOG 0
# define MCCI_ARDUINO_BSP_SUPPORT_CLOCK_CALIBRATION 0
#endif

#if MCCI_ARDUINO_BSP_SUPPORT_READ_ANALOG
# include <stm32_adc.h>
#else
// this function is called from a trampoline C function that
// needs to invoke analog reads for checking USB presence.
bool CatenaStm32L0_ReadAnalog(
Expand All @@ -165,6 +181,11 @@ bool CatenaStm32L0_ReadAnalog(
uint32_t Multiplier,
uint32_t *pValue
);
#endif /* MCCI_ARDUINO_BSP_SUPPORT_READ_ANALOG */

#if MCCI_ARDUINO_BSP_SUPPORT_CLOCK_CALIBRATION
# include <stm32_clock.h>
#endif

} // namespace McciCatena

Expand Down
16 changes: 14 additions & 2 deletions src/CatenaStm32L0Rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ class CatenaStm32L0Rtc
bool Advance(uint32_t delta);
};

// static constexpr ostime_t CATENA_STM32L0_RTC_BASE_YEAR = 2017;

CatenaStm32L0Rtc() {};
bool begin(bool fResetTime = false);

Expand All @@ -94,6 +92,20 @@ class CatenaStm32L0Rtc

} // namespace McciCatena

#ifdef _mcci_arduino_version
# if _mcci_arduino_version < _mcci_arduino_version_calc(2, 6, 0, 10)
# define MCCI_ARDUINO_BSP_SUPPORT_RTC 0
# else
# define MCCI_ARDUINO_BSP_SUPPORT_RTC 1
# endif
#else
# define MCCI_ARDUINO_BSP_SUPPORT_RTC 0
#endif

#if MCCI_ARDUINO_BSP_SUPPORT_RTC
# include <stm32_rtc.h>
#endif

#endif /* STM32L0xx */

/**** end of CatenaStm32L0Rtc.h ****/
Expand Down
7 changes: 7 additions & 0 deletions src/lib/stm32/catena461x/Catena4610_ReadVoltage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,20 @@ extern "C" {

uint32_t USBD_LL_ConnectionState(void)
{
#if ! MCCI_ARDUINO_BSP_SUPPORT_READ_ANALOG
uint32_t vBus;
bool fStatus;

fStatus = CatenaStm32L0_ReadAnalog(
Catena461x::ANALOG_CHANNEL_VBUS, 1, 3, &vBus
);
return (fStatus && vBus < 3000) ? 0 : 1;
#else
uint32_t vBus;

vBus = Stm32ReadAnalog(Catena461x::ANALOG_CHANNEL_VBUS, 1, 3);
return vBus < 3000 ? 0 : 1;
#endif
}

}
Expand Down
15 changes: 13 additions & 2 deletions src/lib/stm32/catena461x/Catena4611_ReadVoltage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ Catena4611::ReadVbat(void) const
float
Catena4611::ReadVbus(void) const
{
float volt = this->ReadAnalog(Catena461x::ANALOG_CHANNEL_VBUS, 1, 3);
// on the Catena 4611, the Vbus divider impedance is very high,
// so charge transfer from the previous channel causes problems.
// Therefore, let's read one extra time; the prvious ADC will
// have had some time to bleed off.
float volt = this->ReadAnalog(Catena461x::ANALOG_CHANNEL_VBUS, 6, 3);
return volt / 1000;
}

Expand All @@ -94,13 +98,20 @@ extern "C" {

uint32_t USBD_LL_ConnectionState(void)
{
#if ! MCCI_ARDUINO_BSP_SUPPORT_READ_ANALOG
uint32_t vBus;
bool fStatus;

fStatus = CatenaStm32L0_ReadAnalog(
Catena461x::ANALOG_CHANNEL_VBUS, 1, 3, &vBus
Catena461x::ANALOG_CHANNEL_VBUS, 6, 3, &vBus
);
return (fStatus && vBus < 3000) ? 0 : 1;
#else
uint32_t vBus;

vBus = Stm32ReadAnalog(Catena461x::ANALOG_CHANNEL_VBUS, 6, 3);
return vBus < 3000 ? 0 : 1;
#endif
}

}
Expand Down
7 changes: 7 additions & 0 deletions src/lib/stm32/catena461x/Catena4612_ReadVoltage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,20 @@ extern "C" {

uint32_t USBD_LL_ConnectionState(void)
{
#if ! MCCI_ARDUINO_BSP_SUPPORT_READ_ANALOG
uint32_t vBus;
bool fStatus;

fStatus = CatenaStm32L0_ReadAnalog(
Catena461x::ANALOG_CHANNEL_VBUS, 6, 3, &vBus
);
return (fStatus && vBus < 3000) ? 0 : 1;
#else
uint32_t vBus;

vBus = Stm32ReadAnalog(Catena461x::ANALOG_CHANNEL_VBUS, 6, 3);
return vBus < 3000 ? 0 : 1;
#endif
}

}
Expand Down
7 changes: 7 additions & 0 deletions src/lib/stm32/catena461x/Catena4617_ReadVoltage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,20 @@ extern "C" {

uint32_t USBD_LL_ConnectionState(void)
{
#if ! MCCI_ARDUINO_BSP_SUPPORT_READ_ANALOG
uint32_t vBus;
bool fStatus;

fStatus = CatenaStm32L0_ReadAnalog(
Catena461x::ANALOG_CHANNEL_VBUS, 6, 3, &vBus
);
return (fStatus && vBus < 3000) ? 0 : 1;
#else
uint32_t vBus;

vBus = Stm32ReadAnalog(Catena461x::ANALOG_CHANNEL_VBUS, 6, 3);
return vBus < 3000 ? 0 : 1;
#endif
}

}
Expand Down
7 changes: 7 additions & 0 deletions src/lib/stm32/catena461x/Catena4618_ReadVoltage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,20 @@ extern "C" {

uint32_t USBD_LL_ConnectionState(void)
{
#if ! MCCI_ARDUINO_BSP_SUPPORT_READ_ANALOG
uint32_t vBus;
bool fStatus;

fStatus = CatenaStm32L0_ReadAnalog(
Catena461x::ANALOG_CHANNEL_VBUS, 6, 3, &vBus
);
return (fStatus && vBus < 3000) ? 0 : 1;
#else
uint32_t vBus;

vBus = Stm32ReadAnalog(Catena461x::ANALOG_CHANNEL_VBUS, 6, 3);
return vBus < 3000 ? 0 : 1;
#endif
}

}
Expand Down
7 changes: 7 additions & 0 deletions src/lib/stm32/catena463x/Catena4630_ReadVoltage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,20 @@ extern "C" {

uint32_t USBD_LL_ConnectionState(void)
{
#if ! MCCI_ARDUINO_BSP_SUPPORT_READ_ANALOG
uint32_t vBus;
bool fStatus;

fStatus = CatenaStm32L0_ReadAnalog(
Catena463x::ANALOG_CHANNEL_VBUS, 1, 3, &vBus
);
return (fStatus && vBus < 3000) ? 0 : 1;
#else
uint32_t vBus;

vBus = Stm32ReadAnalog(Catena463x::ANALOG_CHANNEL_VBUS, 1, 3);
return vBus < 3000 ? 0 : 1;
#endif
}

}
Expand Down
17 changes: 14 additions & 3 deletions src/lib/stm32/stm32l0/CatenaStm32L0Rtc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ using namespace McciCatena;
|
\****************************************************************************/

#if ! MCCI_ARDUINO_BSP_SUPPORT_RTC

extern "C" {

static volatile uint32_t *gs_pAlarm;
Expand Down Expand Up @@ -126,8 +128,13 @@ uint32_t HAL_AddTick(

} /* extern "C" */

#endif /* MCCI_ARDUINO_BSP_SUPPORT_RTC */

bool CatenaStm32L0Rtc::begin(bool fResetTime)
{
#if MCCI_ARDUINO_BSP_SUPPORT_RTC
return Stm32RtcInit(&this->m_hRtc);
#else
RTC_TimeTypeDef Time;
RTC_DateTypeDef Date;
uint32_t RtcClock;
Expand Down Expand Up @@ -234,14 +241,14 @@ bool CatenaStm32L0Rtc::begin(bool fResetTime)

HAL_RTC_DeactivateAlarm(&this->m_hRtc, RTC_ALARM_A);
return true;
#endif /* MCCI_ARDUINO_BSP_SUPPORT_RTC */
}

const uint16_t CatenaStm32L0Rtc::md[13] =
{
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
};


CatenaStm32L0Rtc::CalendarTime
CatenaStm32L0Rtc::GetTime(void)
{
Expand Down Expand Up @@ -321,10 +328,9 @@ void CatenaStm32L0Rtc::SetAlarm(const CalendarTime *pNow)

this->m_fAlarmEnabled = true;
this->m_Alarm = 0;
gs_pAlarm = &this->m_Alarm;

// Serial.print("SetAlarm: ");
// Serial.print(pNow->Year + CATENA_STM32L0_RTC_BASE_YEAR);
// Serial.print(pNow->Year);
// Serial.print("-");
// Serial.print(pNow->Month);
// Serial.print("-");
Expand All @@ -351,7 +357,12 @@ void CatenaStm32L0Rtc::SetAlarm(const CalendarTime *pNow)
Alarm.Alarm = RTC_ALARM_A;

/* Set RTC_Alarm */
#if MCCI_ARDUINO_BSP_SUPPORT_RTC
Stm32RtcSetAlarm(&this->m_hRtc, &Alarm, RTC_FORMAT_BIN, &this->m_Alarm);
#else
gs_pAlarm = &this->m_Alarm;
HAL_RTC_SetAlarm_IT(&this->m_hRtc, &Alarm, RTC_FORMAT_BIN);
#endif /* MCCI_ARDUINO_BSP_SUPPORT_RTC */
}

bool CatenaStm32L0Rtc::PollAlarmState(void)
Expand Down
32 changes: 21 additions & 11 deletions src/lib/stm32/stm32l0/CatenaStm32L0_CalibrateSystemClock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ using namespace McciCatena;
|
\****************************************************************************/

static uint32_t MeasureMillisPerRtcSecond(void);
#if ! MCCI_ARDUINO_BSP_SUPPORT_CLOCK_CALIBRATION
static uint32_t MeasureMicrosPerRtcSecond(void);
#endif /* MCCI_ARDUINO_BSP_SUPPORT_CLOCK_CALIBRATION */


/****************************************************************************\
Expand Down Expand Up @@ -85,7 +87,7 @@ Name: CatenaStm32L0::CalibrateSystemClock()
use TIM21, but there's no sample code. We're need to
do better than we've been doing, so the first version
of this function runs for several seconds and compares
millis (driven by system clock) to the output of the
micros (driven by system clock) to the output of the
RTC (driven by LSE).

In general, this routine works by stepping the clock
Expand All @@ -110,6 +112,9 @@ Name: CatenaStm32L0::CalibrateSystemClock()

uint32_t CatenaStm32L0::CalibrateSystemClock(void)
{
#if MCCI_ARDUINO_BSP_SUPPORT_CLOCK_CALIBRATION
return Stm32CalibrateSystemClock();
#else
uint32_t Calib;
uint32_t CalibNew;
uint32_t CalibLow;
Expand Down Expand Up @@ -142,13 +147,13 @@ uint32_t CatenaStm32L0::CalibrateSystemClock(void)
CalibLow = 0;
CalibHigh = 0;
mSecondLow = 0;
mSecondHigh = 2000;
mSecondHigh = 2000000;
fHaveSeenLow = fHaveSeenHigh = false;

/* loop until we have a new value */
do {
/* meassure the # of millis per RTC second */
mSecond = MeasureMillisPerRtcSecond();
/* meassure the # of micros per RTC second */
mSecond = MeasureMicrosPerRtcSecond();

/* invariant: */
if (Calib == CalibNew)
Expand All @@ -161,7 +166,7 @@ uint32_t CatenaStm32L0::CalibrateSystemClock(void)
);

/* if mSecond is low, this meaans we must increase the system clock */
if (mSecond <= 1000)
if (mSecond <= 1000000)
{
/*
|| the following condition establishes that we're
Expand Down Expand Up @@ -297,11 +302,14 @@ uint32_t CatenaStm32L0::CalibrateSystemClock(void)
mSecondNew
);
return CalibNew;
#endif /* MCCI_ARDUINO_BSP_SUPPORT_CLOCK_CALIBRATION */
}

#if ! MCCI_ARDUINO_BSP_SUPPORT_CLOCK_CALIBRATION

static
uint32_t
MeasureMillisPerRtcSecond(
MeasureMicrosPerRtcSecond(
void
)
{
Expand All @@ -313,10 +321,10 @@ MeasureMillisPerRtcSecond(
/* get the starting time */
second = RTC->TR & (RTC_TR_ST | RTC_TR_SU);

/* wait for a new second to start, and capture millis() in start */
/* wait for a new second to start, and capture micros() in start */
do {
now = RTC->TR & (RTC_TR_ST | RTC_TR_SU);
start = millis();
start = micros();
} while (second == now);

/* update our second of interest */
Expand All @@ -325,16 +333,18 @@ MeasureMillisPerRtcSecond(
/* no point in watching the register until we get close */
delay(500);

/* wait for the next second to start, and capture millis() */
/* wait for the next second to start, and capture micros() */
do {
now = RTC->TR & (RTC_TR_ST | RTC_TR_SU);
end = millis();
end = micros();
} while (second == now);

/* return the delta */
return end - start;
}

#endif /* MCCI_ARDUINO_BSP_SUPPORT_CLOCK_CALIBRATION */

#endif // ARDUINO_ARCH_STM32

/**** end of CatenaStm32L0_CalibrateSystemClock.cpp ****/
Loading