Skip to content

Commit b57531e

Browse files
committed
Improve clock cal for STM32L0 in compliance sketch
1 parent 8df712a commit b57531e

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

examples/compliance-otaa-halconfig/compliance-otaa-halconfig.ino

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ void setup() {
631631
LMIC_reset();
632632

633633
// set clock rate error to 0.1%
634-
LMIC_setClockError(1 * MAX_CLOCK_ERROR / 1000);
634+
//LMIC_setClockError(1 * MAX_CLOCK_ERROR / 1000);
635635

636636
// do the network-specific setup prior to join.
637637
setupForNetwork(false);
@@ -967,20 +967,20 @@ uint32_t Stm32_CalibrateSystemClock(void)
967967
CalibLow = 0;
968968
CalibHigh = 0;
969969
mSecondLow = 0;
970-
mSecondHigh = 2000;
970+
mSecondHigh = 2000000;
971971
fHaveSeenLow = fHaveSeenHigh = false;
972972

973973
/* loop until we have a new value */
974974
do {
975975
/* meassure the # of millis per RTC second */
976-
mSecond = MeasureMillisPerRtcSecond();
976+
mSecond = MeasureMicrosPerRtcSecond();
977977

978978
/* invariant: */
979979
if (Calib == CalibNew)
980980
mSecondNew = mSecond;
981981

982982
/* if mSecond is low, this meaans we must increase the system clock */
983-
if (mSecond <= 1000)
983+
if (mSecond <= 1000000)
984984
{
985985
Serial.print('-');
986986
/*
@@ -1098,6 +1098,7 @@ uint32_t Stm32_CalibrateSystemClock(void)
10981098

10991099
if (CalibNew != Calib)
11001100
{
1101+
Serial.print(CalibNew < Calib ? '+' : '-');
11011102
if (fCalibrateMSI)
11021103
{
11031104
__HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(CalibNew);
@@ -1109,11 +1110,13 @@ uint32_t Stm32_CalibrateSystemClock(void)
11091110
delay(500);
11101111
}
11111112

1113+
Serial.print(" 0x");
1114+
Serial.println(CalibNew, HEX);
11121115
return CalibNew;
11131116
}
11141117

11151118
uint32_t
1116-
MeasureMillisPerRtcSecond(
1119+
MeasureMicrosPerRtcSecond(
11171120
void
11181121
)
11191122
{
@@ -1128,7 +1131,7 @@ MeasureMillisPerRtcSecond(
11281131
/* wait for a new second to start, and capture millis() in start */
11291132
do {
11301133
now = RTC->TR & (RTC_TR_ST | RTC_TR_SU);
1131-
start = millis();
1134+
start = micros();
11321135
} while (second == now);
11331136

11341137
/* update our second of interest */
@@ -1140,7 +1143,7 @@ MeasureMillisPerRtcSecond(
11401143
/* wait for the next second to start, and capture millis() */
11411144
do {
11421145
now = RTC->TR & (RTC_TR_ST | RTC_TR_SU);
1143-
end = millis();
1146+
end = micros();
11441147
} while (second == now);
11451148

11461149
/* return the delta */

0 commit comments

Comments
 (0)