test.py 2.48 KB
Newer Older
1 2 3 4
#!/usr/bin/env python3

import os
import sys
5
import time
6
import intelhex
7
import subprocess
lwc-tester committed
8 9 10 11
import serial.tools.list_ports
from test_common import (
    DeviceUnderTestAeadUARTP,
    eprint,
12
    test_main,
Enrico Pozzobon committed
13
    FileMutex
lwc-tester committed
14
)
15

16

lwc-tester committed
17 18 19 20 21 22 23 24 25
def get_serial():
    ports = serial.tools.list_ports.comports()
    devices = [
        p.device
        for p in ports
        if (p.vid == 0x1A86 and p.pid == 0x7523)
    ]
    devices.sort()
    return devices[0]
26

27

lwc-tester committed
28
class Uno(DeviceUnderTestAeadUARTP):
29

lwc-tester committed
30 31
    def __init__(self, build_dir):
        DeviceUnderTestAeadUARTP.__init__(self)
32

33 34 35
        self.uart_device = get_serial()
        devname = os.path.basename(self.uart_device)
        self.lock = FileMutex('/var/lock/lwc-compare.%s.lock' % devname)
lwc-tester committed
36
        self.build_dir = build_dir
lwc-tester committed
37 38 39 40
        self.template_path = os.path.dirname(sys.argv[0])

        self.firmware_path = os.path.join(
            build_dir, 'firmware.hex')
lwc-tester committed
41

42 43 44 45 46 47 48 49 50 51 52
        ih = intelhex.IntelHex()
        ih.loadhex(self.firmware_path)
        total_size = 0
        for start, stop in ih.segments():
            print(start, stop, stop-start)
            total_size += stop-start
        self._firmware_size = total_size

    def firmware_size(self):
        return self._firmware_size

53 54 55 56 57 58
    def reset(self):
        self.ser.setDTR(True)
        time.sleep(0.01)
        self.ser.setDTR(False)
        time.sleep(1)

lwc-tester committed
59 60
    def flash(self):
        pipe = subprocess.PIPE
lwc-tester committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        avrdude_dir = '/home/tester/.platformio/packages/tool-avrdude/'

        cmd = [os.path.join(avrdude_dir, 'avrdude')]
        cmd += ['-C', os.path.join(avrdude_dir, 'avrdude.conf')]
        cmd += ['-v']
        cmd += ['-p', 'atmega328p']
        cmd += ['-c', 'arduino']
        cmd += ['-P', self.uart_device]
        cmd += ['-b', '115200']
        cmd += ['-D']
        cmd += ['-U', 'flash:w:%s:i' % os.path.abspath(self.firmware_path)]

        self.ser = serial.Serial(
            self.uart_device,
            baudrate=115200,
            timeout=5)
        self.ser.setDTR(True)
        time.sleep(0.01)
        self.ser.setDTR(False)
        self.ser.close()

lwc-tester committed
82 83 84
        p = subprocess.Popen(
            cmd, stdout=sys.stderr, stdin=pipe)
        stdout, stderr = p.communicate("")
85
        assert p.returncode == 0
lwc-tester committed
86 87
        eprint("Firmware flashed.")

88 89 90 91 92 93
        self.ser = serial.Serial(
            self.uart_device,
            baudrate=115200,
            timeout=5)
        self.reset()

lwc-tester committed
94 95
    def dump_ram(self):
        return None
96 97


98
if __name__ == "__main__":
99
    sys.exit(test_main(Uno, 0x0800, sys.argv))