1 |
2 |
idiolatrie |
#!/usr/bin/env python
|
2 |
|
|
#------------------------------------------------------------------------------#
|
3 |
|
|
# Serial Upload Tool #
|
4 |
|
|
#------------------------------------------------------------------------------#
|
5 |
|
|
# Uses pySerial visit [1] for the module. #
|
6 |
|
|
# #
|
7 |
|
|
# REFERENCES #
|
8 |
|
|
# [1] pySerial, http://pyserial.sourceforge.net/ #
|
9 |
|
|
# [2] Fabio Varesano, http://www.varesano.net/blog/fabio/ #
|
10 |
|
|
# serial%20rs232%20connections%20python #
|
11 |
|
|
#------------------------------------------------------------------------------#
|
12 |
|
|
# Copyright (C) 2011 Mathias Hoertnagl, mathias.hoertnagl@gmail.com #
|
13 |
|
|
# #
|
14 |
|
|
# This program is free software; you can redistribute it and/or modify it #
|
15 |
|
|
# under the terms of the GNU General Public License as published by the Free #
|
16 |
|
|
# Software Foundation; either version 3 of the License, or (at your option) #
|
17 |
|
|
# any later version. #
|
18 |
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT #
|
19 |
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
20 |
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
21 |
|
|
# more details. #
|
22 |
|
|
# You should have received a copy of the GNU General Public License along with #
|
23 |
|
|
# this program; if not, see <http://www.gnu.org/licenses/>. #
|
24 |
|
|
#------------------------------------------------------------------------------#
|
25 |
|
|
import sys, struct, time
|
26 |
|
|
import serial
|
27 |
|
|
|
28 |
|
|
if len(sys.argv) != 2:
|
29 |
|
|
print "Usage: python", sys.argv[0], "<*.bin file>"
|
30 |
|
|
sys.exit()
|
31 |
|
|
|
32 |
|
|
# Set up serial port object.
|
33 |
|
|
s = serial.Serial(
|
34 |
|
|
port = 4, # 'COM5',
|
35 |
|
|
baudrate = 19200
|
36 |
|
|
)
|
37 |
|
|
|
38 |
|
|
# Load binary file.
|
39 |
|
|
inp = open(sys.argv[1], 'rb')
|
40 |
|
|
bin = inp.read()
|
41 |
|
|
inp.close()
|
42 |
|
|
|
43 |
|
|
print ""
|
44 |
|
|
print "************************************************************************"
|
45 |
|
|
print "* Upload *"
|
46 |
|
|
print "************************************************************************"
|
47 |
|
|
|
48 |
|
|
size = len(bin)
|
49 |
|
|
print "File size of '{0}' is: {1} bytes.".format(sys.argv[1], size)
|
50 |
|
|
|
51 |
|
|
if s.isOpen():
|
52 |
|
|
s.write(struct.pack('>I', size))
|
53 |
|
|
|
54 |
|
|
# void echoes the image size, after it has erased the flash.
|
55 |
|
|
# Check if it is the correct size.
|
56 |
|
|
esize = struct.unpack('>I', s.read(4))[0]
|
57 |
|
|
|
58 |
|
|
if esize != size:
|
59 |
|
|
print "ERROR: Size echo is {0}. Expected: {1}.".format(esize, size)
|
60 |
|
|
sys.exit()
|
61 |
|
|
|
62 |
|
|
print "Flash ready. Size echo correct."
|
63 |
|
|
|
64 |
|
|
print "Sending data ..."
|
65 |
|
|
|
66 |
|
|
# NOTE: Writing the entire file with s.write(bin) does not work. Possibly
|
67 |
|
|
# due to limited buffer size.
|
68 |
|
|
for c in bin:
|
69 |
|
|
s.write(c)
|
70 |
|
|
|
71 |
|
|
s.close()
|
72 |
|
|
|
73 |
|
|
print "Done!"
|