Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.

Upstream: https://github.com/ARMmbed/DAPLink

Committer:
Pawel Zarembski
Date:
Tue Apr 07 12:55:42 2020 +0200
Revision:
0:01f31e923fe2
hani: DAPLink with reset workaround

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pawel Zarembski 0:01f31e923fe2 1 #
Pawel Zarembski 0:01f31e923fe2 2 # DAPLink Interface Firmware
Pawel Zarembski 0:01f31e923fe2 3 # Copyright (c) 2016-2017, ARM Limited, All Rights Reserved
Pawel Zarembski 0:01f31e923fe2 4 # SPDX-License-Identifier: Apache-2.0
Pawel Zarembski 0:01f31e923fe2 5 #
Pawel Zarembski 0:01f31e923fe2 6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
Pawel Zarembski 0:01f31e923fe2 7 # not use this file except in compliance with the License.
Pawel Zarembski 0:01f31e923fe2 8 # You may obtain a copy of the License at
Pawel Zarembski 0:01f31e923fe2 9 #
Pawel Zarembski 0:01f31e923fe2 10 # http://www.apache.org/licenses/LICENSE-2.0
Pawel Zarembski 0:01f31e923fe2 11 #
Pawel Zarembski 0:01f31e923fe2 12 # Unless required by applicable law or agreed to in writing, software
Pawel Zarembski 0:01f31e923fe2 13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Pawel Zarembski 0:01f31e923fe2 14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Pawel Zarembski 0:01f31e923fe2 15 # See the License for the specific language governing permissions and
Pawel Zarembski 0:01f31e923fe2 16 # limitations under the License.
Pawel Zarembski 0:01f31e923fe2 17 #
Pawel Zarembski 0:01f31e923fe2 18
Pawel Zarembski 0:01f31e923fe2 19 import mbed_lstools
Pawel Zarembski 0:01f31e923fe2 20 import threading
Pawel Zarembski 0:01f31e923fe2 21 import time
Pawel Zarembski 0:01f31e923fe2 22 import os
Pawel Zarembski 0:01f31e923fe2 23 import sys
Pawel Zarembski 0:01f31e923fe2 24
Pawel Zarembski 0:01f31e923fe2 25
Pawel Zarembski 0:01f31e923fe2 26 # This prevents the following error message from getting
Pawel Zarembski 0:01f31e923fe2 27 # displayed on windows if the mbed dismounts unexpectedly
Pawel Zarembski 0:01f31e923fe2 28 # during a transfer:
Pawel Zarembski 0:01f31e923fe2 29 # There is no disk in the drive. Please insert a disk into
Pawel Zarembski 0:01f31e923fe2 30 # drive \Device\<Harddiskx>\<rdrive>
Pawel Zarembski 0:01f31e923fe2 31 def disable_popup():
Pawel Zarembski 0:01f31e923fe2 32 if sys.platform.startswith("win"):
Pawel Zarembski 0:01f31e923fe2 33 # pylint: disable=invalid-name
Pawel Zarembski 0:01f31e923fe2 34 import ctypes
Pawel Zarembski 0:01f31e923fe2 35 SEM_FAILCRITICALERRORS = 1
Pawel Zarembski 0:01f31e923fe2 36 GetErrorMode = \
Pawel Zarembski 0:01f31e923fe2 37 ctypes.windll.kernel32.GetErrorMode # @UndefinedVariable
Pawel Zarembski 0:01f31e923fe2 38 GetErrorMode.restype = ctypes.c_uint
Pawel Zarembski 0:01f31e923fe2 39 GetErrorMode.argtypes = []
Pawel Zarembski 0:01f31e923fe2 40 SetErrorMode = \
Pawel Zarembski 0:01f31e923fe2 41 ctypes.windll.kernel32.SetErrorMode # @UndefinedVariable
Pawel Zarembski 0:01f31e923fe2 42 SetErrorMode.restype = ctypes.c_uint
Pawel Zarembski 0:01f31e923fe2 43 SetErrorMode.argtypes = [ctypes.c_uint]
Pawel Zarembski 0:01f31e923fe2 44
Pawel Zarembski 0:01f31e923fe2 45 err_mode = GetErrorMode()
Pawel Zarembski 0:01f31e923fe2 46 err_mode |= SEM_FAILCRITICALERRORS
Pawel Zarembski 0:01f31e923fe2 47 SetErrorMode(err_mode)
Pawel Zarembski 0:01f31e923fe2 48
Pawel Zarembski 0:01f31e923fe2 49 MAX_REMOUNT_TIME = 5 * 60
Pawel Zarembski 0:01f31e923fe2 50 should_exit = False
Pawel Zarembski 0:01f31e923fe2 51 exit_cond = threading.Condition()
Pawel Zarembski 0:01f31e923fe2 52 print_mut = threading.RLock()
Pawel Zarembski 0:01f31e923fe2 53 global_start_time = time.time()
Pawel Zarembski 0:01f31e923fe2 54
Pawel Zarembski 0:01f31e923fe2 55
Pawel Zarembski 0:01f31e923fe2 56 class ExitException(Exception):
Pawel Zarembski 0:01f31e923fe2 57 pass
Pawel Zarembski 0:01f31e923fe2 58
Pawel Zarembski 0:01f31e923fe2 59
Pawel Zarembski 0:01f31e923fe2 60 def _get_time():
Pawel Zarembski 0:01f31e923fe2 61 return time.time() - global_start_time
Pawel Zarembski 0:01f31e923fe2 62
Pawel Zarembski 0:01f31e923fe2 63
Pawel Zarembski 0:01f31e923fe2 64 def sync_print(msg):
Pawel Zarembski 0:01f31e923fe2 65 with print_mut:
Pawel Zarembski 0:01f31e923fe2 66 print(msg)
Pawel Zarembski 0:01f31e923fe2 67
Pawel Zarembski 0:01f31e923fe2 68
Pawel Zarembski 0:01f31e923fe2 69 def get_mount_point(board_id):
Pawel Zarembski 0:01f31e923fe2 70 lstools = mbed_lstools.create()
Pawel Zarembski 0:01f31e923fe2 71 mbed_list = lstools.list_mbeds()
Pawel Zarembski 0:01f31e923fe2 72 for mbed in mbed_list:
Pawel Zarembski 0:01f31e923fe2 73 if mbed['target_id'] == board_id:
Pawel Zarembski 0:01f31e923fe2 74 return mbed['mount_point']
Pawel Zarembski 0:01f31e923fe2 75 else:
Pawel Zarembski 0:01f31e923fe2 76 Exception("Board %s not found" % board_id)
Pawel Zarembski 0:01f31e923fe2 77
Pawel Zarembski 0:01f31e923fe2 78
Pawel Zarembski 0:01f31e923fe2 79 def msd_remount_main(thread_index, board_id):
Pawel Zarembski 0:01f31e923fe2 80 global should_exit
Pawel Zarembski 0:01f31e923fe2 81 try:
Pawel Zarembski 0:01f31e923fe2 82 mount_point = get_mount_point(board_id)
Pawel Zarembski 0:01f31e923fe2 83 while True:
Pawel Zarembski 0:01f31e923fe2 84 if should_exit:
Pawel Zarembski 0:01f31e923fe2 85 raise ExitException()
Pawel Zarembski 0:01f31e923fe2 86
Pawel Zarembski 0:01f31e923fe2 87 # Trigger a remount
Pawel Zarembski 0:01f31e923fe2 88 sync_print("Triggering remount for %i %s - %s at %.6f - %s" %
Pawel Zarembski 0:01f31e923fe2 89 (thread_index, mount_point, board_id, _get_time(),
Pawel Zarembski 0:01f31e923fe2 90 time.strftime("%H:%M:%S")))
Pawel Zarembski 0:01f31e923fe2 91 file_path = mount_point + "/" + "refresh.act"
Pawel Zarembski 0:01f31e923fe2 92 with open(file_path, "wb") as _:
Pawel Zarembski 0:01f31e923fe2 93 pass
Pawel Zarembski 0:01f31e923fe2 94
Pawel Zarembski 0:01f31e923fe2 95 # Wait for board to dismount
Pawel Zarembski 0:01f31e923fe2 96 start_time = time.time()
Pawel Zarembski 0:01f31e923fe2 97 while os.path.exists(mount_point):
Pawel Zarembski 0:01f31e923fe2 98 if should_exit:
Pawel Zarembski 0:01f31e923fe2 99 raise ExitException()
Pawel Zarembski 0:01f31e923fe2 100 if time.time() - start_time > MAX_REMOUNT_TIME:
Pawel Zarembski 0:01f31e923fe2 101 raise Exception("Board remount timed out")
Pawel Zarembski 0:01f31e923fe2 102 time.sleep(0.1)
Pawel Zarembski 0:01f31e923fe2 103 sync_print("Drive %s dismount" % mount_point)
Pawel Zarembski 0:01f31e923fe2 104
Pawel Zarembski 0:01f31e923fe2 105 # Wait for drive to come back
Pawel Zarembski 0:01f31e923fe2 106 mount_point = None
Pawel Zarembski 0:01f31e923fe2 107 start_time = time.time()
Pawel Zarembski 0:01f31e923fe2 108 while mount_point is None:
Pawel Zarembski 0:01f31e923fe2 109 if should_exit:
Pawel Zarembski 0:01f31e923fe2 110 raise ExitException()
Pawel Zarembski 0:01f31e923fe2 111 mount_point = get_mount_point(board_id)
Pawel Zarembski 0:01f31e923fe2 112 if time.time() - start_time > MAX_REMOUNT_TIME:
Pawel Zarembski 0:01f31e923fe2 113 raise Exception("Board remount timed out")
Pawel Zarembski 0:01f31e923fe2 114 time.sleep(0.1)
Pawel Zarembski 0:01f31e923fe2 115 assert os.path.exists(mount_point)
Pawel Zarembski 0:01f31e923fe2 116
Pawel Zarembski 0:01f31e923fe2 117 sync_print("Remount complete as %s" % mount_point)
Pawel Zarembski 0:01f31e923fe2 118 except ExitException:
Pawel Zarembski 0:01f31e923fe2 119 pass
Pawel Zarembski 0:01f31e923fe2 120 except:
Pawel Zarembski 0:01f31e923fe2 121 sync_print("Thread %i exception board %s" % (thread_index, board_id))
Pawel Zarembski 0:01f31e923fe2 122 with exit_cond:
Pawel Zarembski 0:01f31e923fe2 123 should_exit = 1
Pawel Zarembski 0:01f31e923fe2 124 exit_cond.notify_all()
Pawel Zarembski 0:01f31e923fe2 125 raise
Pawel Zarembski 0:01f31e923fe2 126
Pawel Zarembski 0:01f31e923fe2 127
Pawel Zarembski 0:01f31e923fe2 128 def main():
Pawel Zarembski 0:01f31e923fe2 129 global should_exit
Pawel Zarembski 0:01f31e923fe2 130 disable_popup()
Pawel Zarembski 0:01f31e923fe2 131 lstools = mbed_lstools.create()
Pawel Zarembski 0:01f31e923fe2 132 mbed_list = lstools.list_mbeds()
Pawel Zarembski 0:01f31e923fe2 133 for thread_index, mbed in enumerate(mbed_list):
Pawel Zarembski 0:01f31e923fe2 134 msd_thread = threading.Thread(target=msd_remount_main,
Pawel Zarembski 0:01f31e923fe2 135 args=(thread_index, mbed['target_id']))
Pawel Zarembski 0:01f31e923fe2 136 msd_thread.start()
Pawel Zarembski 0:01f31e923fe2 137
Pawel Zarembski 0:01f31e923fe2 138 try:
Pawel Zarembski 0:01f31e923fe2 139 with exit_cond:
Pawel Zarembski 0:01f31e923fe2 140 while not should_exit:
Pawel Zarembski 0:01f31e923fe2 141 exit_cond.wait(1)
Pawel Zarembski 0:01f31e923fe2 142 except KeyboardInterrupt:
Pawel Zarembski 0:01f31e923fe2 143 pass
Pawel Zarembski 0:01f31e923fe2 144 should_exit = True
Pawel Zarembski 0:01f31e923fe2 145
Pawel Zarembski 0:01f31e923fe2 146 sync_print("Exiting")
Pawel Zarembski 0:01f31e923fe2 147
Pawel Zarembski 0:01f31e923fe2 148 if __name__ == "__main__":
Pawel Zarembski 0:01f31e923fe2 149 main()