Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
hid_test.py
00001 # 00002 # DAPLink Interface Firmware 00003 # Copyright (c) 2009-2016, 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 from __future__ import absolute_import 00020 00021 from time import sleep 00022 from random import randrange 00023 import math 00024 00025 from pyocd.core.helpers import ConnectHelper 00026 from pyocd.core.memory_map import MemoryType 00027 from pyocd.flash.loader import FileProgrammer 00028 from pyocd.utility.conversion import float32_to_u32 00029 00030 # TODO - make a dedicated test 00031 # TODO - test all DapLink commands 00032 # TODO - test various clock speeds 00033 # TODO - test turnaround settings 00034 # TODO - test HID speed 00035 # TODO - test ram/rom transfer speeds 00036 00037 00038 def test_hid(workspace, parent_test): 00039 test_info = parent_test.create_subtest("HID test") 00040 board = workspace.board 00041 with ConnectHelper.session_with_chosen_probe(unique_id=board.get_unique_id()) as session: 00042 mbed_board = session.board 00043 target = mbed_board.target 00044 binary_file = workspace.target.bin_path 00045 00046 ram_region = target.memory_map.get_first_region_of_type(MemoryType.RAM) 00047 rom_region = target.memory_map.get_boot_memory() 00048 00049 addr = ram_region.start + 4 00050 size = 0x502 00051 addr_bin = rom_region.start 00052 addr_flash = rom_region.start + rom_region.length // 2 00053 00054 flash = rom_region.flash 00055 00056 test_info.info("\r\n\r\n----- FLASH NEW BINARY -----") 00057 FileProgrammer(session).program(binary_file, base_address=addr_bin) 00058 00059 test_info.info("\r\n\r\n------ GET Unique ID ------") 00060 test_info.info("Unique ID: %s" % mbed_board.unique_id) 00061 00062 test_info.info("\r\n\r\n------ TEST READ / WRITE CORE REGISTER ------") 00063 pc = target.read_core_register('pc') 00064 test_info.info("initial pc: 0x%X" % target.read_core_register('pc')) 00065 # write in pc dummy value 00066 target.write_core_register('pc', 0x3D82) 00067 test_info.info("now pc: 0x%X" % target.read_core_register('pc')) 00068 # write initial pc value 00069 target.write_core_register('pc', pc) 00070 test_info.info("initial pc value rewritten: 0x%X" % 00071 target.read_core_register('pc')) 00072 00073 msp = target.read_core_register('msp') 00074 psp = target.read_core_register('psp') 00075 test_info.info("MSP = 0x%08x; PSP = 0x%08x" % (msp, psp)) 00076 00077 control = target.read_core_register('control') 00078 faultmask = target.read_core_register('faultmask') 00079 basepri = target.read_core_register('basepri') 00080 primask = target.read_core_register('primask') 00081 test_info.info("CONTROL = 0x%02x; FAULTMASK = 0x%02x; BASEPRI = 0x%02x; PRIMASK = 0x%02x" % (control, faultmask, basepri, primask)) 00082 00083 target.write_core_register('primask', 1) 00084 newPrimask = target.read_core_register('primask') 00085 test_info.info("New PRIMASK = 0x%02x" % newPrimask) 00086 target.write_core_register('primask', primask) 00087 newPrimask = target.read_core_register('primask') 00088 test_info.info("Restored PRIMASK = 0x%02x" % newPrimask) 00089 00090 if target.has_fpu: 00091 s0 = target.read_core_register('s0') 00092 test_info.info("S0 = %g (0x%08x)" % (s0, float32_to_u32(s0))) 00093 target.write_core_register('s0', math.pi) 00094 newS0 = target.read_core_register('s0') 00095 test_info.info("New S0 = %g (0x%08x)" % 00096 (newS0, float32_to_u32(newS0))) 00097 target.write_core_register('s0', s0) 00098 newS0 = target.read_core_register('s0') 00099 test_info.info("Restored S0 = %g (0x%08x)" % 00100 (newS0, float32_to_u32(newS0))) 00101 00102 test_info.info("\r\n\r\n------ TEST HALT / RESUME ------") 00103 00104 test_info.info("resume") 00105 target.resume() 00106 sleep(0.2) 00107 00108 test_info.info("halt") 00109 target.halt() 00110 test_info.info("HALT: pc: 0x%X" % target.read_core_register('pc')) 00111 sleep(0.2) 00112 00113 test_info.info("\r\n\r\n------ TEST STEP ------") 00114 00115 test_info.info("reset and halt") 00116 target.reset_and_halt() 00117 currentPC = target.read_core_register('pc') 00118 test_info.info("HALT: pc: 0x%X" % currentPC) 00119 sleep(0.2) 00120 00121 for i in range(4): 00122 test_info.info("step") 00123 target.step() 00124 newPC = target.read_core_register('pc') 00125 test_info.info("STEP: pc: 0x%X" % newPC) 00126 currentPC = newPC 00127 sleep(0.2) 00128 00129 test_info.info("\r\n\r\n------ TEST READ / WRITE MEMORY ------") 00130 target.halt() 00131 test_info.info("READ32/WRITE32") 00132 val = randrange(0, 0xffffffff) 00133 test_info.info("write32 0x%X at 0x%X" % (val, addr)) 00134 target.write32(addr, val) 00135 res = target.read32(addr) 00136 test_info.info("read32 at 0x%X: 0x%X" % (addr, res)) 00137 if res != val: 00138 test_info.failure("ERROR in READ/WRITE 32") 00139 00140 test_info.info("\r\nREAD16/WRITE16") 00141 val = randrange(0, 0xffff) 00142 test_info.info("write16 0x%X at 0x%X" % (val, addr + 2)) 00143 target.write16(addr + 2, val) 00144 res = target.read16(addr + 2) 00145 test_info.info("read16 at 0x%X: 0x%X" % (addr + 2, res)) 00146 if res != val: 00147 test_info.failure("ERROR in READ/WRITE 16") 00148 00149 test_info.info("\r\nREAD8/WRITE8") 00150 val = randrange(0, 0xff) 00151 test_info.info("write8 0x%X at 0x%X" % (val, addr + 1)) 00152 target.write8(addr + 1, val) 00153 res = target.read8(addr + 1) 00154 test_info.info("read8 at 0x%X: 0x%X" % (addr + 1, res)) 00155 if res != val: 00156 test_info.failure("ERROR in READ/WRITE 8") 00157 00158 test_info.info("\r\n\r\n------ TEST READ / WRITE MEMORY BLOCK ------") 00159 data = [randrange(1, 50) for _ in range(size)] 00160 target.write_memory_block8(addr, data) 00161 block = target.read_memory_block8(addr, size) 00162 error = False 00163 for i in range(len(block)): 00164 if (block[i] != data[i]): 00165 error = True 00166 test_info.info("ERROR: 0x%X, 0x%X, 0x%X!!!" % 00167 ((addr + i), block[i], data[i])) 00168 if error: 00169 test_info.failure("TEST FAILED") 00170 else: 00171 test_info.info("TEST PASSED") 00172 00173 test_info.info("\r\n\r\n------ TEST RESET ------") 00174 target.reset() 00175 sleep(0.1) 00176 target.halt() 00177 00178 for i in range(5): 00179 target.step() 00180 test_info.info("pc: 0x%X" % target.read_core_register('pc')) 00181 00182 test_info.info("\r\n\r\n------ TEST PROGRAM/ERASE PAGE ------") 00183 # Fill 3 pages with 0x55 00184 page_size = flash.get_page_info(addr_flash).size 00185 fill = [0x55] * page_size 00186 for i in range(0, 3): 00187 address = addr_flash + page_size * i 00188 # Test only supports a location with 3 aligned 00189 # pages of the same size 00190 current_page_size = flash.get_page_info(addr_flash).size 00191 assert page_size == current_page_size 00192 assert address % current_page_size == 0 00193 00194 flash.init(flash.Operation.ERASE) 00195 flash.erase_page(address) 00196 flash.uninit() 00197 00198 flash.init(flash.Operation.PROGRAM) 00199 flash.program_page(address, fill) 00200 flash.uninit() 00201 # Erase the middle page 00202 flash.init(flash.Operation.ERASE) 00203 flash.erase_page(addr_flash + page_size) 00204 flash.cleanup() 00205 # Verify the 1st and 3rd page were not erased, and that the 2nd page is fully erased 00206 data = target.read_memory_block8(addr_flash, page_size * 3) 00207 expected = fill + [0xFF] * page_size + fill 00208 if data == expected: 00209 test_info.info("TEST PASSED") 00210 else: 00211 test_info.failure("TEST FAILED") 00212 00213 test_info.info("\r\n\r\n----- Restoring image -----") 00214 FileProgrammer(session).program(binary_file, base_address=addr_bin) 00215 00216 target.reset() 00217 test_info.info("HID test complete")
Generated on Tue Jul 12 2022 15:37:18 by
1.7.2