summaryrefslogtreecommitdiff
path: root/cts/cts.py
blob: 9c4940b6ffb62c844629346ad161ea13e3076159 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/python2
# Copyright 2016 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# This file is a utility to quickly flash boards


import os
import subprocess as sp
import sys
import argparse

# example of call this method will make
# make BOARD=nucleo-f072rb CTS_MODULE=gpio -j

ocd_script_dir = '/usr/local/share/openocd/scripts'
th_board = 'stm32l476g-eval'
th_serial_filename = 'th_hla_serial'

def make(module, dut_board, ecDirectory):
    sp.call(['make', '--directory=' + str(ecDirectory),
              'BOARD=stm32l476g-eval', 'CTS_MODULE=' + module, '-j'])

    sp.call(['make', '--directory=' + str(ecDirectory),
              'BOARD=' + dut_board, 'CTS_MODULE=' + module, '-j'])

def openocd_cmd(command_list, board_cfg):
    args = ['openocd', '-s', ocd_script_dir,
            '-f', board_cfg]
    for c in command_list:
        args.append('-c')
        args.append(c)
    args.append('-c')
    args.append('shutdown')
    sp.call(args)

def get_stlink_serial_numbers():
    usb_args = ['lsusb', '-v', '-d', '0x0483:0x374b']
    usb_process = sp.Popen(usb_args, stdout=sp.PIPE, shell=False)
    st_link_info = usb_process.communicate()[0]
    st_serials = []
    for line in st_link_info.split('\n'):
        if 'iSerial' in line:
            st_serials.append(line.split()[2])
    return st_serials

# This function is necessary because the dut might be using an st-link debugger
# params: th_hla_serial is your personal th board's serial
def identify_dut(th_hla_serial):
    stlink_serials = get_stlink_serial_numbers()
    if len(stlink_serials) == 1:
        return None
    # If 2 st-link devices connected, find dut's serial number
    elif len(stlink_serials) == 2:
        dut = [s for s in stlink_serials if th_hla_serial not in s]
        if len(dut) != 1:
            print 'ERROR: Check your TH hla_serial'
            return None
        else:
            return dut[0] # Found your other st-link device serial!
    else:
        print 'ERROR: Please connect TH and your DUT and remove all other st-link devices'
        return None

def update_th_serial(dest_dir):
    serial = get_stlink_serial_numbers()
    if len(serial) != 1:
        print 'Connect your TH and remove other st-link devices'
    else:
        ser = serial[0]
        f = open(os.path.join(dest_dir, th_serial_filename), mode='w')
        f.write(ser)
        f.close()
        return ser

def get_board_config_name(board):
    board_config_locs = {
        'stm32l476g-eval' : 'board/stm32l4discovery.cfg',
        'nucleo-f072rb' : 'board/st_nucleo_f0.cfg'
    }
    return board_config_locs[board]

def flash_boards(dut_board, th_serial_loc):
    th_hla = None
    dut_hla = None
    try:
        th_hla = open(th_serial_loc).read()
    except:
        print 'Your th hla_serial may not have been saved.'
        print 'Connect only your th and run ./cts --th, then try again.'
        print sys.exc_info()[0]
        return
    dut_hla = identify_dut(th_hla)
    th_cfg = get_board_config_name(th_board)
    dut_cfg = get_board_config_name(dut_board)

    if(th_cfg == None or dut_cfg == None):
        print 'Board cfg files not found'
        return

    th_flash_cmds = ['hla_serial ' + th_hla,
                     'reset_config connect_assert_srst',
                     'init',
                     'reset init',
                     'flash write_image erase build/' + th_board + '/ec.bin 0x08000000',
                     'reset halt']

    dut_flash_cmds = ['reset_config connect_assert_srst',
                      'init',
                      'reset init',
                      'flash write_image erase build/' + dut_board + '/ec.bin 0x08000000',
                      'reset halt']

    if dut_hla != None:
        dut_flash_cmds.insert(0, 'hla_serial ' + dut_hla)

    openocd_cmd(th_flash_cmds, th_cfg)
    openocd_cmd(dut_flash_cmds, dut_cfg)
    openocd_cmd(['init', 'reset init', 'resume'], th_cfg)
    openocd_cmd(['init', 'reset init', 'resume'], dut_cfg)

def main():
    global ocd_script_dir
    path = os.path.abspath(__file__)
    ec_dir = os.path.join(os.path.dirname(path), '..')
    os.chdir(ec_dir)
    th_serial_dir = os.path.join(ec_dir, 'build', th_board)
    dut_board = 'nucleo-f072rb' #nucleo by default
    module = 'gpio' #gpio by default

    parser = argparse.ArgumentParser(description='Used to build/flash boards')
    parser.add_argument('-d',
                        '--dut',
                        help='Specify DUT you want to build/flash')
    parser.add_argument('-m',
                        '--module',
                        help='Specify module you want to build/flash')
    parser.add_argument('-t',
                        '--th',
                        action='store_true',
                        help='Connect only the th to save its serial')
    parser.add_argument('-b',
                        '--build',
                        action='store_true',
                        help='Build test suite (no flashing)')
    parser.add_argument('-f',
                        '--flash',
                        action='store_true',
                        help='Flash boards with last image built for them')

    args = parser.parse_args()
    args = parser.parse_args()

    if args.th:
        serial = update_th_serial(th_serial_dir)
        if(serial != None):
            print 'Your th hla_serial # has been saved as: ' + serial
        return

    if args.module:
        module = args.module

    if args.dut:
        dut_board = args.dut

    elif args.build:
        make(module, dut_board, ec_dir)

    elif args.flash:
        flash_boards(dut_board, os.path.join(th_serial_dir, th_serial_filename))

    else:
        make(module, dut_board, ec_dir)
        flash_boards(dut_board, os.path.join(th_serial_dir, th_serial_filename))

if __name__ == "__main__":
    main()