2
2
import serial
3
3
import argparse
4
4
import struct
5
- from crc16 import crc16xmodem as crc16
5
+ from crccheck .crc import Crc16CcittFalse
6
+ from time import sleep
7
+ import os
6
8
7
9
CHUNK_SIZE = 128
8
10
14
16
# Payload Size (2-bytes)
15
17
HEADER_FORMAT = "<HH" .format (CHUNK_SIZE )
16
18
19
+ crc16 = Crc16CcittFalse .calc
17
20
18
21
def read_in_chunks (file_object , chunk_size = 256 ):
19
22
while True :
@@ -33,6 +36,8 @@ def main():
33
36
# Create the OTA serial
34
37
ser = serial .Serial (args .port , 115200 , timeout = 0.1 )
35
38
39
+ binary_size = os .path .getsize (args .binary )
40
+
36
41
# Open the binary
37
42
with open (args .binary , 'rb' ) as f :
38
43
@@ -57,24 +62,30 @@ def main():
57
62
58
63
sequence_num = 0
59
64
for chunk in read_in_chunks (f , CHUNK_SIZE ):
60
-
61
- if len (chunk ) != CHUNK_SIZE :
65
+ bytes_written_after = sequence_num * ( CHUNK_SIZE + 1 )
66
+ if len (chunk ) != CHUNK_SIZE or bytes_written_after >= binary_size :
62
67
sequence_num = 0xFFFF
63
68
64
69
# Pack the header
65
70
tx_buf = struct .pack (HEADER_FORMAT , sequence_num , len (chunk )) + chunk
66
71
67
72
# Calculate the CRC16 of the chunk and the header info
73
+ #print(tx_buf.hex())
74
+
68
75
crc = crc16 (bytes (tx_buf ))
69
76
print ("Sending chunk: crc({}), seq num({}), size({})" .format (crc , sequence_num , len (chunk )))
70
77
71
78
tx_buf = struct .pack ("<H" , crc ) + tx_buf
72
79
73
80
# Encode with COBS
74
81
tx_cobs = cobsr .encode (tx_buf ) + bytes ([0 ])
75
- print (type (tx_cobs ))
76
- ser .write (tx_cobs )
77
- print (tx_cobs .hex ())
82
+ #print(type(tx_cobs))
83
+ #ser.write(tx_cobs)
84
+ #print(tx_cobs.hex())
85
+
86
+ for b in tx_cobs :
87
+ ser .write (bytes ([b ]))
88
+ sleep (0.001 )
78
89
79
90
while (True ):
80
91
# Wait until the bootloader is ready
@@ -85,5 +96,12 @@ def main():
85
96
86
97
sequence_num = sequence_num + 1
87
98
99
+
100
+ ser .close ()
101
+ ser = serial .Serial (args .port , 9600 )
102
+ while (True ):
103
+ print (ser .readline ())
104
+
105
+
88
106
if __name__ == '__main__' :
89
- main ()
107
+ main ()
0 commit comments