33import pycom
44import gc
55
6- __version__ = '1.0.1 '
6+ __version__ = '1.0.5 '
77
88
99PIN_MASK = 0b1010
@@ -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 , ))
32+ self .uart = UART (1 , baudrate = 10000 , pins = (COMM_PIN , ), timeout_chars = 3 )
3333 self .clk_cal_factor = 1
3434 self .uart .read ()
3535 # enable the weak pull-ups control
@@ -39,7 +39,7 @@ def _send(self, data):
3939 self .uart .write (bytes (data ))
4040
4141 def _start (self ):
42- self .uart .sendbreak (20 )
42+ self .uart .sendbreak (12 )
4343 self ._send ([0x55 ])
4444
4545 def _magic (self , address , and_val , or_val , xor_val , expected = None ):
@@ -75,7 +75,10 @@ def poke(self, address, value):
7575 self ._magic (address , 0 , value , 0 )
7676
7777 def peek (self , address ):
78- return self ._magic (address , 0xFF , 0 , 0 )[6 ]
78+ try :
79+ return self ._magic (address , 0xFF , 0 , 0 )[6 ]
80+ except :
81+ return self ._magic (address , 0xFF , 0 , 0 )[6 ]
7982
8083 def setbits (self , address , mask ):
8184 self ._magic (address , 0xFF , mask , 0 )
@@ -100,15 +103,20 @@ def calibrate(self):
100103
101104 # setbits, but limit the number of received bytes to avoid confusion with pattern
102105 self ._magic (CTRL_0_ADDR , 0xFF , 1 << 2 , 0 , 0 )
103- self .uart .deinit ()
104106 self ._pulses = pycom .pulses_get (COMM_PIN , 50 )
105- self .uart = UART ( 1 , baudrate = 10000 , pins = (COMM_PIN , ))
107+ self .uart . init ( baudrate = 10000 , pins = (COMM_PIN , ), timeout_chars = 3 )
106108 try :
107- self .clk_cal_factor = (self ._pulses [4 ][1 ] - self ._pulses [1 ][1 ]) / EXP_RTC_PERIOD
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
108113 except :
109114 pass
110115 if self .clk_cal_factor > 1.25 or self .clk_cal_factor < 0.75 :
111116 self .clk_cal_factor = 1
117+ # flush the buffer
118+ self .uart .read ()
119+ self .get_wake_status ()
112120
113121 def enable_auto_poweroff (self ):
114122 self .setbits (CTRL_0_ADDR , 1 << 1 )
0 commit comments