@@ -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 )
0 commit comments