1+ import serial
2+ import serial .tools .list_ports
3+ import sys
4+ import os
5+ from enum import Enum
6+ import time
7+ from binascii import hexlify
8+
9+
10+ class Codes (Enum ):
11+ COMMAND_OK = 'A'
12+ COMMAND_ERROR = 'B'
13+ CODE_META = 'C'
14+ CODE_BEGIN = 'D'
15+ TAG_OK = 'E'
16+ TAG_ERROR_WRITE = 'F'
17+ TAG_ERROR_READ = 'G'
18+ TAG_ERROR_VERIFY = 'H'
19+ TAG_NEXT = 'I'
20+ TAG_WAITING = 'J'
21+ TAG_FIRST = 'K'
22+ TAG_NOT_FIRST = 'L'
23+ TAG_UPDATING_META = 'M'
24+ BUFFER_OK = 'N'
25+ BUFFER_ERROR = 'O'
26+ BUFFER_REPEAT = 'P'
27+ BUFFER_WAITING = 'Q'
28+ ALL_DONE = 'R'
29+ INIT_OK = 'S'
30+
31+
32+ if len (sys .argv ) != 2 :
33+ print 'Input file not specified'
34+ sys .exit (1 )
35+
36+ port = list (serial .tools .list_ports .comports ())
37+
38+ for a in port :
39+ print a [0 ] + ' ' + a [1 ]
40+
41+ selected = raw_input ('Port: ' ).upper ()
42+ print 'Selected port ' + selected
43+
44+ try :
45+ file = open (sys .argv [1 ], 'rb' )
46+ except :
47+ print 'Error opening file'
48+ sys .exit (1 )
49+
50+ codeSize = os .path .getsize (sys .argv [1 ])
51+ print 'Code size: %d bytes' % codeSize
52+
53+ if codeSize == 0 :
54+ print 'Why would you upload nothing?'
55+ sys .exit (1 )
56+
57+ # Adjusting code size to the nearest block
58+ if codeSize % 16 != 0 :
59+ codeSize += 16 - (codeSize % 16 )
60+ print "Adjusted code size: %d" % codeSize
61+
62+ try :
63+ ser = serial .Serial (selected , 115200 , timeout = 2 )
64+ except :
65+ print 'Error opening Serial port'
66+ sys .exit (1 )
67+
68+ while ser .read (1 ) != Codes .INIT_OK .value :
69+ pass
70+
71+ ser .write (Codes .CODE_META .value + str (codeSize ))
72+
73+ retcode = ser .read (1 )
74+ if retcode != Codes .COMMAND_OK .value :
75+ print 'Error'
76+
77+ # Bytes of code written
78+ addr = 0
79+
80+ retcode = ser .read (1 )
81+ while True :
82+ if retcode != Codes .TAG_WAITING .value :
83+ print "Wait for tag error"
84+ raw_input ('[Press any key]' )
85+
86+ ser .write (Codes .TAG_NEXT .value )
87+ print 'Scan tag'
88+
89+ while ser .in_waiting == 0 :
90+ pass
91+
92+ for x in range (0 , 3 ):
93+ print ser .readline ().strip ()
94+ while ser .in_waiting == 0 :
95+ pass
96+
97+ tokencap = int (ser .readline (), 10 )
98+ if tokencap == 0 :
99+ print "Not supported"
100+ continue
101+ else : print 'Token capacity: %d bytes' % tokencap
102+
103+ while True :
104+ retcode = ser .read (1 )
105+ if retcode == Codes .TAG_FIRST .value :
106+ break
107+ if retcode == Codes .TAG_WAITING .value :
108+ break
109+ if retcode == Codes .TAG_UPDATING_META .value :
110+ print "Updating tag metadata"
111+ elif retcode == Codes .BUFFER_WAITING .value :
112+ ser .write (Codes .CODE_BEGIN .value )
113+
114+ # Write to buffer
115+ code = file .read (16 )
116+ while len (code ) < 16 :
117+ code += '\xFF '
118+ print '%8X: %r' % (addr , hexlify (code ))
119+
120+ while True :
121+ # come back here if there is an error with buffer
122+ ser .write (code )
123+ retcode = ser .read (1 )
124+ if retcode != Codes .BUFFER_OK .value :
125+ print "buffer fill error"
126+ while ser .in_waiting < 16 :
127+ pass
128+
129+ isError = False
130+
131+ for x in range (0 , 16 , 1 ):
132+ retcode = ser .read (1 )
133+ if code [x ] != retcode :
134+ print 'Buffer error %d!=%d!' % (ord (code [x ]), ord (retcode ))
135+ ser .write (Codes .BUFFER_ERROR .value )
136+ isError = True
137+ break
138+ if isError == False : break
139+
140+ ser .write (Codes .BUFFER_OK .value )
141+ addr += 16
142+
143+ while ser .in_waiting == 0 :
144+ pass
145+ retcode = ser .read (1 )
146+ if retcode == Codes .TAG_ERROR_WRITE .value :
147+ print 'Error writing to block'
148+
149+ retcode = ser .read (1 )
150+ if retcode == Codes .TAG_ERROR_READ .value :
151+ print 'Error reading block'
152+ if retcode == Codes .TAG_ERROR_VERIFY .value :
153+ print 'Error verifying block'
154+ elif retcode == Codes .TAG_OK .value : pass
155+
156+ if retcode == Codes .TAG_FIRST .value : break
157+
158+ # We're done with this file
159+ file .close ()
160+
161+ while True :
162+ # Ask for first tag
163+ if retcode == Codes .TAG_FIRST .value :
164+ print 'Prepare first tag'
165+ raw_input ('[Press any key]' )
166+ ser .write (Codes .TAG_NEXT .value )
167+ print 'Scan first tag'
168+
169+ # What if tag is not first
170+ while ser .in_waiting == 0 : pass
171+ retcode = ser .read (1 )
172+ if retcode != Codes .TAG_OK .value :
173+ if retcode == Codes .TAG_NOT_FIRST .value :
174+ print 'Tag is not first'
175+ else :
176+ print 'Unknown error %d' % ord (retcode )
177+ retcode = ser .read (1 )
178+ elif retcode == Codes .TAG_OK .value : break
179+
180+
181+
182+ # Waiting for end of communication
183+ while ser .in_waiting == 0 :
184+ pass
185+ retcode = ser .read (1 )
186+ if retcode == Codes .ALL_DONE .value :
187+ print 'Done programming'
188+
189+ if codeSize != addr :
190+ print 'Written different amount of data (%d | %d)' % (addr , codeSize )
191+ ser .close ()
0 commit comments