diff options
-rwxr-xr-x | script/cat.py | 108 | ||||
-rw-r--r-- | src/app/countertest/main.cc | 18 | ||||
-rw-r--r-- | src/arch/msp430fr5994lp/Makefile.inc | 7 |
3 files changed, 122 insertions, 11 deletions
diff --git a/script/cat.py b/script/cat.py new file mode 100755 index 0000000..330669e --- /dev/null +++ b/script/cat.py @@ -0,0 +1,108 @@ +#!/usr/bin/env python3 + +import re +import serial +import serial.threaded +import sys +import time + +cpu_freq = None +timer_overflow = None + + +class SerialReader(serial.threaded.Protocol): + """ + Character- to line-wise data buffer for serial interfaces. + + Reads in new data whenever it becomes available and exposes a line-based + interface to applications. + """ + + def __init__(self, callback): + """Create a new SerialReader object.""" + self.callback = callback + self.recv_buf = "" + + def __call__(self): + return self + + def data_received(self, data): + """Append newly received serial data to the line buffer.""" + try: + str_data = data.decode("UTF-8") + self.recv_buf += str_data + + # We may get anything between \r\n, \n\r and simple \n newlines. + # We assume that \n is always present and use str.strip to remove leading/trailing \r symbols + # Note: Do not call str.strip on lines[-1]! Otherwise, lines may be mangled + lines = self.recv_buf.split("\n") + if len(lines) > 1: + self.recv_buf = lines[-1] + for line in lines[:-1]: + self.callback(str.strip(line)) + + except UnicodeDecodeError: + pass + # sys.stderr.write('UART output contains garbage: {data}\n'.format(data = data)) + + +class SerialMonitor: + """SerialMonitor captures serial output for a specific amount of time.""" + + def __init__(self, port: str, baud: int, callback): + """ + Create a new SerialMonitor connected to port at the specified baud rate. + + Communication uses no parity, no flow control, and one stop bit. + Data collection starts immediately. + """ + self.ser = serial.serial_for_url(port, do_not_open=True) + self.ser.baudrate = baud + self.ser.parity = "N" + self.ser.rtscts = False + self.ser.xonxoff = False + + try: + self.ser.open() + except serial.SerialException as e: + sys.stderr.write( + "Could not open serial port {}: {}\n".format(self.ser.name, e) + ) + sys.exit(1) + + self.reader = SerialReader(callback=callback) + self.worker = serial.threaded.ReaderThread(self.ser, self.reader) + self.worker.start() + + def close(self): + """Close serial connection.""" + self.worker.stop() + self.ser.close() + + +def handle_line(line): + if cpu_freq is not None: + print_line = line + for match in re.finditer("(\d+)/(\d+) cycles", line): + cycles = int(match.group(1)) + overflows = int(match.group(2)) + ms = (cycles + timer_overflow * overflows) * 1000 / cpu_freq + match_line = f"{cycles}/{overflows} cycles" + new_line = f"{ms} ms ({cycles}/{overflows} cycles)" + print_line = re.sub(match_line, new_line, print_line) + print(print_line) + else: + print(line) + + +if __name__ == "__main__": + tty = sys.argv[1] + baud = int(sys.argv[2]) + + if len(sys.argv) > 4: + cpu_freq = int(sys.argv[3]) + timer_overflow = int(sys.argv[4]) + + monitor = SerialMonitor(tty, baud, handle_line) + time.sleep(5) + monitor.close() diff --git a/src/app/countertest/main.cc b/src/app/countertest/main.cc index cf3a136..e0a7394 100644 --- a/src/app/countertest/main.cc +++ b/src/app/countertest/main.cc @@ -12,47 +12,47 @@ void loop(void) { counter.start(); counter.stop(); - kout << "nop: " << counter.value << "/" << counter.overflow << endl; + kout << "nop: " << counter.value << "/" << counter.overflow << " cycles" << endl; counter.start(); arch.delay_us(10); counter.stop(); - kout << "10us: " << counter.value << "/" << counter.overflow << endl; + kout << "10us: " << counter.value << "/" << counter.overflow << " cycles" << endl; counter.start(); arch.delay_us(20); counter.stop(); - kout << "20us: " << counter.value << "/" << counter.overflow << endl; + kout << "20us: " << counter.value << "/" << counter.overflow << " cycles" << endl; counter.start(); arch.delay_ms(1); counter.stop(); - kout << "1ms: " << counter.value << "/" << counter.overflow << endl; + kout << "1ms: " << counter.value << "/" << counter.overflow << " cycles" << endl; counter.start(); arch.delay_ms(2); counter.stop(); - kout << "2ms: " << counter.value << "/" << counter.overflow << endl; + kout << "2ms: " << counter.value << "/" << counter.overflow << " cycles" << endl; counter.start(); arch.delay_ms(4); counter.stop(); - kout << "4ms: " << counter.value << "/" << counter.overflow << endl; + kout << "4ms: " << counter.value << "/" << counter.overflow << " cycles" << endl; counter.start(); arch.delay_ms(8); counter.stop(); - kout << "8ms: " << counter.value << "/" << counter.overflow << endl; + kout << "8ms: " << counter.value << "/" << counter.overflow << " cycles" << endl; counter.start(); arch.delay_ms(16); counter.stop(); - kout << "16ms: " << counter.value << "/" << counter.overflow << endl; + kout << "16ms: " << counter.value << "/" << counter.overflow << " cycles" << endl; counter.start(); arch.delay_ms(32); counter.stop(); - kout << "32ms: " << counter.value << "/" << counter.overflow << endl; + kout << "32ms: " << counter.value << "/" << counter.overflow << " cycles" << endl; } int main(void) diff --git a/src/arch/msp430fr5994lp/Makefile.inc b/src/arch/msp430fr5994lp/Makefile.inc index 1c8b331..57a7d2c 100644 --- a/src/arch/msp430fr5994lp/Makefile.inc +++ b/src/arch/msp430fr5994lp/Makefile.inc @@ -169,7 +169,7 @@ build/system.elf: ${OBJECTS} -Wl,--library-path=/opt/msp430/ti/msp430-gcc-full-linux-5.1.2.0/include/ \ -Wl,--gc-sections \ -o $@ ${OBJECTS} - ${QUIET}${SIZE} build/system.elf | tail -n1 | awk '{ print " ROM: " int(($$1+$$2)*100/49152) "% RAM: " int(($$2+$$3)*100/4096) "%" }' + ${QUIET}${SIZE} build/system.elf | tail -n1 | awk '{ print " ROM: " ($$1+$$2) " (" int(($$1+$$2)*100/49152) "%) RAM: " ($$2 + $$3) " (" int(($$2+$$3)*100/4096) "%)" }' build/system.hex: build/system.elf ${QUIET}${OBJCOPY} -O ihex ${@:.hex=.elf} $@ @@ -183,6 +183,9 @@ program: build/system.hex arch_clean: ${QUIET}rm -f ${OBJECTS} build/system.hex +cat: + ${QUIET}script/cat.py /dev/${SERIAL_PORT} ${uart_freq} ${cpu_freq} 65536 + monitor: ${QUIET}screen /dev/${SERIAL_PORT} ${uart_freq} @@ -217,4 +220,4 @@ attributes: build/system.elf ${QUIET}script/size.py ${SIZE} text,data data,bss endif -.PHONY: arch_clean arch_help arch_info attributes monitor program +.PHONY: arch_clean arch_help arch_info attributes cat monitor program |