Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: m-labs/artiq
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 3ff3afe696e2
Choose a base ref
...
head repository: m-labs/artiq
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 94218f785e50
Choose a head ref
  • 2 commits
  • 2 files changed
  • 2 contributors

Commits on Dec 2, 2014

  1. Copy the full SHA
    20adb57 View commit details
  2. comm_serial: cleanup

    sbourdeauducq committed Dec 2, 2014
    Copy the full SHA
    94218f7 View commit details
Showing with 35 additions and 15 deletions.
  1. +26 −14 artiq/coredevice/comm_serial.py
  2. +9 −1 soc/runtime/comm_serial.c
40 changes: 26 additions & 14 deletions artiq/coredevice/comm_serial.py
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ class _H2DMsgType(Enum):
REQUEST_IDENT = 1
LOAD_OBJECT = 2
RUN_KERNEL = 3
SET_BAUD_RATE = 4


class _D2HMsgType(Enum):
@@ -58,6 +59,11 @@ class Comm:
def __init__(self, dev="/dev/ttyUSB1", baud=115200):
self._fd = os.open(dev, os.O_RDWR | os.O_NOCTTY)
self.port = os.fdopen(self._fd, "r+b", buffering=0)
self.set_baud(115200)
self.set_remote_baud(baud)
self.set_baud(baud)

def set_baud(self, baud):
iflag, oflag, cflag, lflag, ispeed, ospeed, cc = \
termios.tcgetattr(self._fd)
iflag = termios.IGNBRK | termios.IGNPAR
@@ -72,9 +78,27 @@ def __init__(self, dev="/dev/ttyUSB1", baud=115200):
termios.tcdrain(self._fd)
termios.tcflush(self._fd, termios.TCOFLUSH)
termios.tcflush(self._fd, termios.TCIFLUSH)
logger.debug("connected to {} at {} baud".format(dev, baud))
logger.debug("baud rate set to".format(baud))

def set_remote_baud(self, baud):
_write_exactly(self.port, struct.pack(
">lbl", 0x5a5a5a5a, _H2DMsgType.SET_BAUD_RATE.value, baud))
handshake = 0
while handshake < 4:
recv = struct.unpack(
"B", _read_exactly(self.port, 1))
if recv[0] == 0x5a:
handshake += 1
else:
# FIXME: when loading immediately after a board reset,
# we erroneously get some zeros back.
logger.warning("unexpected sync character: {:02x}".format(int(recv[0])))
handshake = 0
self.set_baud(baud)
logger.debug("synchronized")

def close(self):
self.set_remote_baud(115200)
self.port.close()

def __enter__(self):
@@ -85,19 +109,7 @@ def __exit__(self, type, value, traceback):

def _get_device_msg(self):
while True:
# FIXME: when loading immediately after a board reset,
# we erroneously get some zeros back.
# Ignore them with a warning for now.
spurious_zero_count = 0
while True:
(reply, ) = struct.unpack("B", _read_exactly(self.port, 1))
if reply == 0:
spurious_zero_count += 1
else:
break
if spurious_zero_count:
logger.warning("received {} spurious zeros".format(
spurious_zero_count))
(reply, ) = struct.unpack("B", _read_exactly(self.port, 1))
msg = _D2HMsgType(reply)
if msg == _D2HMsgType.LOG:
(length, ) = struct.unpack(">h", _read_exactly(self.port, 2))
10 changes: 9 additions & 1 deletion soc/runtime/comm_serial.c
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ enum {
MSGTYPE_REQUEST_IDENT = 1,
MSGTYPE_LOAD_OBJECT,
MSGTYPE_RUN_KERNEL,
MSGTYPE_SET_BAUD_RATE,
};

/* device to host */
@@ -161,7 +162,14 @@ void comm_serve(object_loader load_object, kernel_runner run_kernel)
receive_and_load_object(load_object);
else if(msgtype == MSGTYPE_RUN_KERNEL)
receive_and_run_kernel(run_kernel);
else
else if(msgtype == MSGTYPE_SET_BAUD_RATE) {
unsigned int ftw;

ftw = ((long long)receive_int() << 32LL)/(long long)identifier_frequency_read();
send_int(0x5a5a5a5a);
uart_sync();
uart_tuning_word_write(ftw);
} else
send_char(MSGTYPE_MESSAGE_UNRECOGNIZED);
}
}