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) 2009-2016, 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 from __future__ import absolute_import
Pawel Zarembski 0:01f31e923fe2 20
Pawel Zarembski 0:01f31e923fe2 21 from time import sleep
Pawel Zarembski 0:01f31e923fe2 22 from random import randrange
Pawel Zarembski 0:01f31e923fe2 23 import math
Pawel Zarembski 0:01f31e923fe2 24
Pawel Zarembski 0:01f31e923fe2 25 from pyocd.core.helpers import ConnectHelper
Pawel Zarembski 0:01f31e923fe2 26 from pyocd.core.memory_map import MemoryType
Pawel Zarembski 0:01f31e923fe2 27 from pyocd.flash.loader import FileProgrammer
Pawel Zarembski 0:01f31e923fe2 28 from pyocd.utility.conversion import float32_to_u32
Pawel Zarembski 0:01f31e923fe2 29
Pawel Zarembski 0:01f31e923fe2 30 # TODO - make a dedicated test
Pawel Zarembski 0:01f31e923fe2 31 # TODO - test all DapLink commands
Pawel Zarembski 0:01f31e923fe2 32 # TODO - test various clock speeds
Pawel Zarembski 0:01f31e923fe2 33 # TODO - test turnaround settings
Pawel Zarembski 0:01f31e923fe2 34 # TODO - test HID speed
Pawel Zarembski 0:01f31e923fe2 35 # TODO - test ram/rom transfer speeds
Pawel Zarembski 0:01f31e923fe2 36
Pawel Zarembski 0:01f31e923fe2 37
Pawel Zarembski 0:01f31e923fe2 38 def test_hid(workspace, parent_test):
Pawel Zarembski 0:01f31e923fe2 39 test_info = parent_test.create_subtest("HID test")
Pawel Zarembski 0:01f31e923fe2 40 board = workspace.board
Pawel Zarembski 0:01f31e923fe2 41 with ConnectHelper.session_with_chosen_probe(unique_id=board.get_unique_id()) as session:
Pawel Zarembski 0:01f31e923fe2 42 mbed_board = session.board
Pawel Zarembski 0:01f31e923fe2 43 target = mbed_board.target
Pawel Zarembski 0:01f31e923fe2 44 binary_file = workspace.target.bin_path
Pawel Zarembski 0:01f31e923fe2 45
Pawel Zarembski 0:01f31e923fe2 46 ram_region = target.memory_map.get_first_region_of_type(MemoryType.RAM)
Pawel Zarembski 0:01f31e923fe2 47 rom_region = target.memory_map.get_boot_memory()
Pawel Zarembski 0:01f31e923fe2 48
Pawel Zarembski 0:01f31e923fe2 49 addr = ram_region.start + 4
Pawel Zarembski 0:01f31e923fe2 50 size = 0x502
Pawel Zarembski 0:01f31e923fe2 51 addr_bin = rom_region.start
Pawel Zarembski 0:01f31e923fe2 52 addr_flash = rom_region.start + rom_region.length // 2
Pawel Zarembski 0:01f31e923fe2 53
Pawel Zarembski 0:01f31e923fe2 54 flash = rom_region.flash
Pawel Zarembski 0:01f31e923fe2 55
Pawel Zarembski 0:01f31e923fe2 56 test_info.info("\r\n\r\n----- FLASH NEW BINARY -----")
Pawel Zarembski 0:01f31e923fe2 57 FileProgrammer(session).program(binary_file, base_address=addr_bin)
Pawel Zarembski 0:01f31e923fe2 58
Pawel Zarembski 0:01f31e923fe2 59 test_info.info("\r\n\r\n------ GET Unique ID ------")
Pawel Zarembski 0:01f31e923fe2 60 test_info.info("Unique ID: %s" % mbed_board.unique_id)
Pawel Zarembski 0:01f31e923fe2 61
Pawel Zarembski 0:01f31e923fe2 62 test_info.info("\r\n\r\n------ TEST READ / WRITE CORE REGISTER ------")
Pawel Zarembski 0:01f31e923fe2 63 pc = target.read_core_register('pc')
Pawel Zarembski 0:01f31e923fe2 64 test_info.info("initial pc: 0x%X" % target.read_core_register('pc'))
Pawel Zarembski 0:01f31e923fe2 65 # write in pc dummy value
Pawel Zarembski 0:01f31e923fe2 66 target.write_core_register('pc', 0x3D82)
Pawel Zarembski 0:01f31e923fe2 67 test_info.info("now pc: 0x%X" % target.read_core_register('pc'))
Pawel Zarembski 0:01f31e923fe2 68 # write initial pc value
Pawel Zarembski 0:01f31e923fe2 69 target.write_core_register('pc', pc)
Pawel Zarembski 0:01f31e923fe2 70 test_info.info("initial pc value rewritten: 0x%X" %
Pawel Zarembski 0:01f31e923fe2 71 target.read_core_register('pc'))
Pawel Zarembski 0:01f31e923fe2 72
Pawel Zarembski 0:01f31e923fe2 73 msp = target.read_core_register('msp')
Pawel Zarembski 0:01f31e923fe2 74 psp = target.read_core_register('psp')
Pawel Zarembski 0:01f31e923fe2 75 test_info.info("MSP = 0x%08x; PSP = 0x%08x" % (msp, psp))
Pawel Zarembski 0:01f31e923fe2 76
Pawel Zarembski 0:01f31e923fe2 77 control = target.read_core_register('control')
Pawel Zarembski 0:01f31e923fe2 78 faultmask = target.read_core_register('faultmask')
Pawel Zarembski 0:01f31e923fe2 79 basepri = target.read_core_register('basepri')
Pawel Zarembski 0:01f31e923fe2 80 primask = target.read_core_register('primask')
Pawel Zarembski 0:01f31e923fe2 81 test_info.info("CONTROL = 0x%02x; FAULTMASK = 0x%02x; BASEPRI = 0x%02x; PRIMASK = 0x%02x" % (control, faultmask, basepri, primask))
Pawel Zarembski 0:01f31e923fe2 82
Pawel Zarembski 0:01f31e923fe2 83 target.write_core_register('primask', 1)
Pawel Zarembski 0:01f31e923fe2 84 newPrimask = target.read_core_register('primask')
Pawel Zarembski 0:01f31e923fe2 85 test_info.info("New PRIMASK = 0x%02x" % newPrimask)
Pawel Zarembski 0:01f31e923fe2 86 target.write_core_register('primask', primask)
Pawel Zarembski 0:01f31e923fe2 87 newPrimask = target.read_core_register('primask')
Pawel Zarembski 0:01f31e923fe2 88 test_info.info("Restored PRIMASK = 0x%02x" % newPrimask)
Pawel Zarembski 0:01f31e923fe2 89
Pawel Zarembski 0:01f31e923fe2 90 if target.has_fpu:
Pawel Zarembski 0:01f31e923fe2 91 s0 = target.read_core_register('s0')
Pawel Zarembski 0:01f31e923fe2 92 test_info.info("S0 = %g (0x%08x)" % (s0, float32_to_u32(s0)))
Pawel Zarembski 0:01f31e923fe2 93 target.write_core_register('s0', math.pi)
Pawel Zarembski 0:01f31e923fe2 94 newS0 = target.read_core_register('s0')
Pawel Zarembski 0:01f31e923fe2 95 test_info.info("New S0 = %g (0x%08x)" %
Pawel Zarembski 0:01f31e923fe2 96 (newS0, float32_to_u32(newS0)))
Pawel Zarembski 0:01f31e923fe2 97 target.write_core_register('s0', s0)
Pawel Zarembski 0:01f31e923fe2 98 newS0 = target.read_core_register('s0')
Pawel Zarembski 0:01f31e923fe2 99 test_info.info("Restored S0 = %g (0x%08x)" %
Pawel Zarembski 0:01f31e923fe2 100 (newS0, float32_to_u32(newS0)))
Pawel Zarembski 0:01f31e923fe2 101
Pawel Zarembski 0:01f31e923fe2 102 test_info.info("\r\n\r\n------ TEST HALT / RESUME ------")
Pawel Zarembski 0:01f31e923fe2 103
Pawel Zarembski 0:01f31e923fe2 104 test_info.info("resume")
Pawel Zarembski 0:01f31e923fe2 105 target.resume()
Pawel Zarembski 0:01f31e923fe2 106 sleep(0.2)
Pawel Zarembski 0:01f31e923fe2 107
Pawel Zarembski 0:01f31e923fe2 108 test_info.info("halt")
Pawel Zarembski 0:01f31e923fe2 109 target.halt()
Pawel Zarembski 0:01f31e923fe2 110 test_info.info("HALT: pc: 0x%X" % target.read_core_register('pc'))
Pawel Zarembski 0:01f31e923fe2 111 sleep(0.2)
Pawel Zarembski 0:01f31e923fe2 112
Pawel Zarembski 0:01f31e923fe2 113 test_info.info("\r\n\r\n------ TEST STEP ------")
Pawel Zarembski 0:01f31e923fe2 114
Pawel Zarembski 0:01f31e923fe2 115 test_info.info("reset and halt")
Pawel Zarembski 0:01f31e923fe2 116 target.reset_and_halt()
Pawel Zarembski 0:01f31e923fe2 117 currentPC = target.read_core_register('pc')
Pawel Zarembski 0:01f31e923fe2 118 test_info.info("HALT: pc: 0x%X" % currentPC)
Pawel Zarembski 0:01f31e923fe2 119 sleep(0.2)
Pawel Zarembski 0:01f31e923fe2 120
Pawel Zarembski 0:01f31e923fe2 121 for i in range(4):
Pawel Zarembski 0:01f31e923fe2 122 test_info.info("step")
Pawel Zarembski 0:01f31e923fe2 123 target.step()
Pawel Zarembski 0:01f31e923fe2 124 newPC = target.read_core_register('pc')
Pawel Zarembski 0:01f31e923fe2 125 test_info.info("STEP: pc: 0x%X" % newPC)
Pawel Zarembski 0:01f31e923fe2 126 currentPC = newPC
Pawel Zarembski 0:01f31e923fe2 127 sleep(0.2)
Pawel Zarembski 0:01f31e923fe2 128
Pawel Zarembski 0:01f31e923fe2 129 test_info.info("\r\n\r\n------ TEST READ / WRITE MEMORY ------")
Pawel Zarembski 0:01f31e923fe2 130 target.halt()
Pawel Zarembski 0:01f31e923fe2 131 test_info.info("READ32/WRITE32")
Pawel Zarembski 0:01f31e923fe2 132 val = randrange(0, 0xffffffff)
Pawel Zarembski 0:01f31e923fe2 133 test_info.info("write32 0x%X at 0x%X" % (val, addr))
Pawel Zarembski 0:01f31e923fe2 134 target.write32(addr, val)
Pawel Zarembski 0:01f31e923fe2 135 res = target.read32(addr)
Pawel Zarembski 0:01f31e923fe2 136 test_info.info("read32 at 0x%X: 0x%X" % (addr, res))
Pawel Zarembski 0:01f31e923fe2 137 if res != val:
Pawel Zarembski 0:01f31e923fe2 138 test_info.failure("ERROR in READ/WRITE 32")
Pawel Zarembski 0:01f31e923fe2 139
Pawel Zarembski 0:01f31e923fe2 140 test_info.info("\r\nREAD16/WRITE16")
Pawel Zarembski 0:01f31e923fe2 141 val = randrange(0, 0xffff)
Pawel Zarembski 0:01f31e923fe2 142 test_info.info("write16 0x%X at 0x%X" % (val, addr + 2))
Pawel Zarembski 0:01f31e923fe2 143 target.write16(addr + 2, val)
Pawel Zarembski 0:01f31e923fe2 144 res = target.read16(addr + 2)
Pawel Zarembski 0:01f31e923fe2 145 test_info.info("read16 at 0x%X: 0x%X" % (addr + 2, res))
Pawel Zarembski 0:01f31e923fe2 146 if res != val:
Pawel Zarembski 0:01f31e923fe2 147 test_info.failure("ERROR in READ/WRITE 16")
Pawel Zarembski 0:01f31e923fe2 148
Pawel Zarembski 0:01f31e923fe2 149 test_info.info("\r\nREAD8/WRITE8")
Pawel Zarembski 0:01f31e923fe2 150 val = randrange(0, 0xff)
Pawel Zarembski 0:01f31e923fe2 151 test_info.info("write8 0x%X at 0x%X" % (val, addr + 1))
Pawel Zarembski 0:01f31e923fe2 152 target.write8(addr + 1, val)
Pawel Zarembski 0:01f31e923fe2 153 res = target.read8(addr + 1)
Pawel Zarembski 0:01f31e923fe2 154 test_info.info("read8 at 0x%X: 0x%X" % (addr + 1, res))
Pawel Zarembski 0:01f31e923fe2 155 if res != val:
Pawel Zarembski 0:01f31e923fe2 156 test_info.failure("ERROR in READ/WRITE 8")
Pawel Zarembski 0:01f31e923fe2 157
Pawel Zarembski 0:01f31e923fe2 158 test_info.info("\r\n\r\n------ TEST READ / WRITE MEMORY BLOCK ------")
Pawel Zarembski 0:01f31e923fe2 159 data = [randrange(1, 50) for _ in range(size)]
Pawel Zarembski 0:01f31e923fe2 160 target.write_memory_block8(addr, data)
Pawel Zarembski 0:01f31e923fe2 161 block = target.read_memory_block8(addr, size)
Pawel Zarembski 0:01f31e923fe2 162 error = False
Pawel Zarembski 0:01f31e923fe2 163 for i in range(len(block)):
Pawel Zarembski 0:01f31e923fe2 164 if (block[i] != data[i]):
Pawel Zarembski 0:01f31e923fe2 165 error = True
Pawel Zarembski 0:01f31e923fe2 166 test_info.info("ERROR: 0x%X, 0x%X, 0x%X!!!" %
Pawel Zarembski 0:01f31e923fe2 167 ((addr + i), block[i], data[i]))
Pawel Zarembski 0:01f31e923fe2 168 if error:
Pawel Zarembski 0:01f31e923fe2 169 test_info.failure("TEST FAILED")
Pawel Zarembski 0:01f31e923fe2 170 else:
Pawel Zarembski 0:01f31e923fe2 171 test_info.info("TEST PASSED")
Pawel Zarembski 0:01f31e923fe2 172
Pawel Zarembski 0:01f31e923fe2 173 test_info.info("\r\n\r\n------ TEST RESET ------")
Pawel Zarembski 0:01f31e923fe2 174 target.reset()
Pawel Zarembski 0:01f31e923fe2 175 sleep(0.1)
Pawel Zarembski 0:01f31e923fe2 176 target.halt()
Pawel Zarembski 0:01f31e923fe2 177
Pawel Zarembski 0:01f31e923fe2 178 for i in range(5):
Pawel Zarembski 0:01f31e923fe2 179 target.step()
Pawel Zarembski 0:01f31e923fe2 180 test_info.info("pc: 0x%X" % target.read_core_register('pc'))
Pawel Zarembski 0:01f31e923fe2 181
Pawel Zarembski 0:01f31e923fe2 182 test_info.info("\r\n\r\n------ TEST PROGRAM/ERASE PAGE ------")
Pawel Zarembski 0:01f31e923fe2 183 # Fill 3 pages with 0x55
Pawel Zarembski 0:01f31e923fe2 184 page_size = flash.get_page_info(addr_flash).size
Pawel Zarembski 0:01f31e923fe2 185 fill = [0x55] * page_size
Pawel Zarembski 0:01f31e923fe2 186 for i in range(0, 3):
Pawel Zarembski 0:01f31e923fe2 187 address = addr_flash + page_size * i
Pawel Zarembski 0:01f31e923fe2 188 # Test only supports a location with 3 aligned
Pawel Zarembski 0:01f31e923fe2 189 # pages of the same size
Pawel Zarembski 0:01f31e923fe2 190 current_page_size = flash.get_page_info(addr_flash).size
Pawel Zarembski 0:01f31e923fe2 191 assert page_size == current_page_size
Pawel Zarembski 0:01f31e923fe2 192 assert address % current_page_size == 0
Pawel Zarembski 0:01f31e923fe2 193
Pawel Zarembski 0:01f31e923fe2 194 flash.init(flash.Operation.ERASE)
Pawel Zarembski 0:01f31e923fe2 195 flash.erase_page(address)
Pawel Zarembski 0:01f31e923fe2 196 flash.uninit()
Pawel Zarembski 0:01f31e923fe2 197
Pawel Zarembski 0:01f31e923fe2 198 flash.init(flash.Operation.PROGRAM)
Pawel Zarembski 0:01f31e923fe2 199 flash.program_page(address, fill)
Pawel Zarembski 0:01f31e923fe2 200 flash.uninit()
Pawel Zarembski 0:01f31e923fe2 201 # Erase the middle page
Pawel Zarembski 0:01f31e923fe2 202 flash.init(flash.Operation.ERASE)
Pawel Zarembski 0:01f31e923fe2 203 flash.erase_page(addr_flash + page_size)
Pawel Zarembski 0:01f31e923fe2 204 flash.cleanup()
Pawel Zarembski 0:01f31e923fe2 205 # Verify the 1st and 3rd page were not erased, and that the 2nd page is fully erased
Pawel Zarembski 0:01f31e923fe2 206 data = target.read_memory_block8(addr_flash, page_size * 3)
Pawel Zarembski 0:01f31e923fe2 207 expected = fill + [0xFF] * page_size + fill
Pawel Zarembski 0:01f31e923fe2 208 if data == expected:
Pawel Zarembski 0:01f31e923fe2 209 test_info.info("TEST PASSED")
Pawel Zarembski 0:01f31e923fe2 210 else:
Pawel Zarembski 0:01f31e923fe2 211 test_info.failure("TEST FAILED")
Pawel Zarembski 0:01f31e923fe2 212
Pawel Zarembski 0:01f31e923fe2 213 test_info.info("\r\n\r\n----- Restoring image -----")
Pawel Zarembski 0:01f31e923fe2 214 FileProgrammer(session).program(binary_file, base_address=addr_bin)
Pawel Zarembski 0:01f31e923fe2 215
Pawel Zarembski 0:01f31e923fe2 216 target.reset()
Pawel Zarembski 0:01f31e923fe2 217 test_info.info("HID test complete")