1#!/usr/bin/python3
2#Usage: cd to the directory  tensorflow/lite/micro/tools/make/targets/ecm3531 and type ./flash_program executable_name to load an executable from the directory tensorflow/lite/micro/tools/make/gen/ecm3531_cortex-m3/bin/ into flash
3#
4#
5# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
6#
7#Licensed under the Apache License, Version 2.0 (the "License");
8#you may not use this file except in compliance with the License.
9#You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#Unless required by applicable law or agreed to in writing, software
14#distributed under the License is distributed on an "AS IS" BASIS,
15#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16#See the License for the specific language governing permissions and
17#limitations under the License.
18#==============================================================================
19
20
21import sys, getopt
22import os
23import telnetlib
24
25def send_ocd_cmd(line):
26    ocd_sock.write(bytes(line,encoding = 'utf-8'))
27    print(ocd_sock.read_until(b'> ').decode('utf-8'), end='')
28
29def get_ocd_response():
30    print(ocd_sock.read_until(b'> ').decode('utf-8'), end='')
31
32#get hooked up to openocd daemon
33ocd_sock = telnetlib.Telnet(host='localhost', port=4444)
34get_ocd_response() # clean it out
35
36# git path to project elf file
37cur_dir = os.getcwd()
38#elf_file = cur_dir + '/../../gen/ecm3531_cortex-m3/bin/' + 'micro_speech'
39elf_file = cur_dir + '/../../gen/ecm3531_cortex-m3/bin/' + sys.argv[1]
40print("elf_file = ",elf_file)
41
42
43# use these to download and run the elf fle
44ocd_commands = ["halt\n",
45                "flash erase_sector 0 0 127\n",
46                "flash write_image {}\n".format(elf_file),
47                "mww 0x1001fff8 0\n",
48                "reset\n"]
49
50# OK now do what we came here for!!!
51for x in ocd_commands:
52    print(x)
53    send_ocd_cmd(x)
54