import sys, time
from SerialPort_linux import *
+# serial device to communicate with
+DEVICE="/dev/ttyUSB0"
+# baudrate used for initialization
+INIT_BAUDRATE=9600
+# baudrate used for communication after init
+REAL_BAUDRATE=38400
+
# contains the last received checksum from a READ, WRITE or CHECKSUM command
last_checksum = 0
def sendByte(byte):
- time.sleep(0.01) # just to get sure, wait 10ms
+ time.sleep(0.001) # just to get sure, wait 1ms
tty.write(chr(byte))
tty.flush()
def recvChecksum():
global last_checksum
- # get checksum
last_checksum = recvByte()
last_checksum |= (recvByte() << 8)
# get checksum
recvChecksum()
+# TODO: test this function!
+def cmdCALL(address):
+ # send CALL command
+ sendByte(0x01)
+ if (recvByte() != 0xF1):
+ raise Exception
+ sendByte(0x04)
+ if (recvByte() != 0x84):
+ raise Exception
+ # tell desired address
+ sendDWord(address)
+ # wait for return parameter - not needed here!
+ #return recvByte()
+
+# TODO: test this function!
+def cmdCHECKSUM():
+ # call CHECKSUM command
+ sendByte(0x01)
+ if (recvByte() != 0xF1):
+ raise Exception
+ sendByte(0x05)
+ if (recvByte() != 0x84):
+ raise Exception
+ # get checksum
+ recvChecksum()
+
def cmdBAUDRATE(baudrate):
global last_checksum
sendByte((baudrate >> 24) & 0xFF)
print "Initializing serial port..."
-tty = SerialPort("/dev/ttyUSB0", 100, 9600)
+tty = SerialPort(DEVICE, 100, INIT_BAUDRATE)
print "Please press RESET on your 1337 board..."
print "OK, trying to set baudrate..."
# set baudrate
-cmdBAUDRATE(19200)
-tty = SerialPort("/dev/ttyUSB0", 100, 19200)
-
+cmdBAUDRATE(REAL_BAUDRATE)
+tty = SerialPort(DEVICE, 100, REAL_BAUDRATE)
+"""
print
+sendByte(0x01)
+print recvByte()
+sendByte(0x02)
+print recvByte()
+sys.exit(0)
+"""
+
# write something to the begin of the IRAM
-data = [255, 2, 3, 4]
-print "Writing", data, "to the IRAM..."
-cmdWRITE(0x00030000, 4, data)
+data_wr = []
+checksum = 0
+for i in range(0, 0x400):
+ value = i%256
+ data_wr.append(value)
+ checksum = (checksum + value) % (2**16)
+
+print "Calculated checksum:", checksum
+print "Writing", data_wr, "to the IRAM..."
+cmdWRITE(0x00030000, len(data_wr), data_wr)
print "Received Checksum:", last_checksum
print
print "Reading from the IRAM again..."
-data2 = cmdREAD(0x00030000, 4)
-print "Received data:", data2, "Checksum:", last_checksum
+data_re = cmdREAD(0x00030000, len(data_wr))
+print "Received data:", data_re, "Checksum:", last_checksum