eris2010

Documentation: http://frombelow.net/projects/eris2010/
Clone: git clone https://git.frombelow.net/eris2010.git
Log | Files | Refs | README | LICENSE

boot.py (2290B)


      1 #!/usr/bin/env python3
      2 """Upload software to Eris 2010"""
      3 
      4 import serial
      5 import sys
      6 import argparse
      7 import time
      8 import pdb
      9 import os
     10 
     11 SERIAL_PORT = '/dev/ttyUSB0'
     12 SERIAL_SPEED = 19200
     13 
     14 # File upload does not work if Eris 2010 is run at 100 kHz clock
     15 # frequency unless SLOW_CPU is set. This flags pauses after byte sent.
     16 SLOW_CPU = False
     17 # SLOW_CPU = True
     18   
     19 def upload_program(filename):
     20     with open(filename, 'rb') as f:
     21         data = f.read()
     22         # Pad to 256 byte block size
     23         pad = 0x100 - (len(data) % 0x100)
     24         if (pad != 0x100):
     25             data = data + b'\x00'*pad
     26         blocks = int(len(data) / 0x100)
     27     with serial.Serial(SERIAL_PORT, SERIAL_SPEED) as s:
     28         print("Triggering reset.")
     29         os.system('stty -F ' + SERIAL_PORT + ' -hup')
     30         s.setDTR(False)
     31         s.setDTR(True)
     32         l = ""
     33         while l != b'Kallisti!\r\n':
     34             l = s.readline()
     35         s.readline()
     36         print("Uploading program.")
     37         s.write(b'\xff')
     38         if SLOW_CPU:
     39             time.sleep(.01)
     40         e = str.encode(chr(blocks))
     41         s.write(e)
     42 
     43         # While transferring data, we calculate a two byte
     44         # checksum. The first byte is the addition of all bytes
     45         # transferred. The second byte is the xor of all bytes
     46         # transferred.
     47         checksum_add = 0
     48         checksum_xor = 0
     49         for d in data:
     50             print(".", end="", flush=True)
     51             if SLOW_CPU:
     52                 time.sleep(.01)
     53             s.write(bytes([d]))
     54             # Update checksums
     55             checksum_add = (checksum_add + d) % 256
     56             checksum_xor = checksum_xor ^ d
     57         # Receive checksums from 8 bit computer and compare to our checksum
     58         cmp_checksum_add = ord(s.read(1))
     59         cmp_checksum_xor = ord(s.read(1))
     60         if ((cmp_checksum_add != checksum_add) or
     61             (cmp_checksum_xor != checksum_xor)):
     62             print('\nERROR: Wrong checksum!')
     63             sys.exit(-1)
     64         print('\nDone.')
     65 
     66 
     67 if __name__ == '__main__':
     68     parser = argparse.ArgumentParser(description='Upload to 8-bit computer')
     69     parser.add_argument('filename', metavar='FILENAME', type=str,
     70                         help='Program to upload')
     71     args = parser.parse_args()
     72     upload_program(filename=args.filename)
     73