Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 1fbc177

Browse files
author
Daniel Campora
committed
deepsleep.py, pycoproc.py: Improve calibration functions.
1 parent 64f2578 commit 1fbc177

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

deepsleep/deepsleep.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class DeepSleep:
2929
EXP_RTC_PERIOD = const(7000)
3030

3131
def __init__(self):
32-
self.uart = UART(1, baudrate=10000, pins=(COMM_PIN, ), timeout_chars=3)
32+
self.uart = UART(1, baudrate=10000, pins=(COMM_PIN, ), timeout_chars=5)
3333
self.clk_cal_factor = 1
3434
self.uart.read()
3535
# enable the weak pull-ups control
@@ -103,20 +103,20 @@ def calibrate(self):
103103

104104
# setbits, but limit the number of received bytes to avoid confusion with pattern
105105
self._magic(CTRL_0_ADDR, 0xFF, 1 << 2, 0, 0)
106-
self._pulses = pycom.pulses_get(COMM_PIN, 50)
107-
self.uart.init(baudrate=10000, pins=(COMM_PIN, ), timeout_chars=3)
106+
self.uart.deinit()
107+
self._pulses = pycom.pulses_get(COMM_PIN, 150)
108+
self.uart.init(baudrate=10000, pins=(COMM_PIN, ), timeout_chars=5)
109+
idx = 0
110+
for i in range(len(self._pulses)):
111+
if self._pulses[i][1] > EXP_RTC_PERIOD:
112+
idx = i
113+
break
108114
try:
109-
if len(self._pulses) > 6:
110-
self.clk_cal_factor = (self._pulses[6][1] - self._pulses[4][1]) / EXP_RTC_PERIOD
111-
else:
112-
self.clk_cal_factor = (self._pulses[5][1] - self._pulses[3][1]) / EXP_RTC_PERIOD
115+
self.clk_cal_factor = (self._pulses[idx][1] - self._pulses[(idx - 1)][1]) / EXP_RTC_PERIOD
113116
except:
114-
pass
117+
self.clk_cal_factor = 1
115118
if self.clk_cal_factor > 1.25 or self.clk_cal_factor < 0.75:
116119
self.clk_cal_factor = 1
117-
# flush the buffer
118-
self.uart.read()
119-
self.get_wake_status()
120120

121121
def enable_auto_poweroff(self):
122122
self.setbits(CTRL_0_ADDR, 1 << 1)

lib/pycoproc/pycoproc.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,21 @@ def calibrate_rtc(self):
232232
self._write(bytes([CMD_CALIBRATE]), wait=False)
233233
self.i2c.deinit()
234234
Pin('P21', mode=Pin.IN)
235-
pulses = pycom.pulses_get('P21', 20000)
235+
pulses = pycom.pulses_get('P21', 100)
236236
self.i2c.init(mode=I2C.MASTER, pins=(self.sda, self.scl))
237-
period = 0
237+
idx = 0
238+
for i in range(len(pulses)):
239+
if pulses[i][1] > EXP_RTC_PERIOD:
240+
idx = i
241+
break
238242
try:
239-
period = pulses[2][1] - pulses[0][1]
243+
period = pulses[idx][1] - pulses[(idx - 1)][1]
240244
except:
241-
pass
245+
period = 0
242246
if period > 0:
243-
self.clk_cal_factor = ((EXP_RTC_PERIOD * 1.0) / period) * (1000.0 / 1024.0)
247+
self.clk_cal_factor = (EXP_RTC_PERIOD / period) * (1000 / 1024)
248+
if self.clk_cal_factor > 1.25 or self.clk_cal_factor < 0.75:
249+
self.clk_cal_factor = 1
244250

245251
def button_pressed(self):
246252
button = self.peek_memory(PORTA_ADDR) & (1 << 3)

0 commit comments

Comments
 (0)