Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hid_usb_test.py Source File

hid_usb_test.py

00001 #
00002 # DAPLink Interface Firmware
00003 # Copyright (c) 2016-2017, ARM Limited, All Rights Reserved
00004 # SPDX-License-Identifier: Apache-2.0
00005 #
00006 # Licensed under the Apache License, Version 2.0 (the "License"); you may
00007 # not use this file except in compliance with the License.
00008 # You may obtain a copy of the License at
00009 #
00010 # http://www.apache.org/licenses/LICENSE-2.0
00011 #
00012 # Unless required by applicable law or agreed to in writing, software
00013 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00014 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 # See the License for the specific language governing permissions and
00016 # limitations under the License.
00017 #
00018 
00019 import mbed_lstools
00020 import threading
00021 import time
00022 import pyocd
00023 
00024 should_exit = False
00025 exit_cond = threading.Condition()
00026 print_mut = threading.RLock()
00027 global_start_time = time.time()
00028 
00029 
00030 def _get_time():
00031     return time.time() - global_start_time
00032 
00033 
00034 def sync_print(msg):
00035     with print_mut:
00036         print(msg)
00037 
00038 
00039 def hid_main(thread_index, board_id):
00040     global should_exit
00041     count = 0
00042     try:
00043         device = pyocd.probe.pydapaccess.DAPAccess.get_device(board_id)
00044         while not should_exit:
00045             device.open()
00046             info = device.vendor(0)
00047             info = str(bytearray(info[1:1 + info[0]]))
00048             assert info == board_id
00049             device.close()
00050             if count % 100 == 0:
00051                 sync_print("Thread %i on loop %10i at %.6f - %s - board %s" %
00052                            (thread_index, count, _get_time(),
00053                             time.strftime("%H:%M:%S"), board_id))
00054             count += 1
00055 
00056     except:
00057         sync_print("Thread %i exception board %s" % (thread_index, board_id))
00058         with exit_cond:
00059             should_exit = 1
00060             exit_cond.notify_all()
00061         raise
00062 
00063 
00064 def main():
00065     global should_exit
00066     lstools = mbed_lstools.create()
00067     mbed_list = lstools.list_mbeds()
00068     for thread_index, mbed in enumerate(mbed_list):
00069         msd_thread = threading.Thread(target=hid_main,
00070                                       args=(thread_index, mbed['target_id']))
00071         msd_thread.start()
00072 
00073     try:
00074         with exit_cond:
00075             while not should_exit:
00076                 exit_cond.wait(1)
00077     except KeyboardInterrupt:
00078         pass
00079     should_exit = True
00080 
00081     sync_print("Exiting")
00082 
00083 
00084 if __name__ == "__main__":
00085     main()