13
13
# See the License for the specific language governing permissions and
14
14
# limitations under the License.
15
15
16
- from intelhex import IntelHex
17
- import itertools
18
16
import os
19
- from pyOCD .board import MbedBoard
20
-
21
17
from .host_test_plugins import HostTestPluginBase
22
-
23
-
24
- def _enum_continguous_addr_start_end (addr_list ):
25
- """Generator to get contiguous address ranges with start and end address"""
26
- for _ , b in itertools .groupby (enumerate (addr_list ), lambda x_y : x_y [1 ] - x_y [0 ]):
27
- b = list (b )
28
- yield b [0 ][1 ], b [- 1 ][1 ]
18
+ from pyocd .core .helpers import ConnectHelper
19
+ from pyocd .flash .loader import FileProgrammer
29
20
30
21
31
22
class HostTestPluginCopyMethod_pyOCD (HostTestPluginBase ):
@@ -48,7 +39,6 @@ def setup(self, *args, **kwargs):
48
39
49
40
def execute (self , capability , * args , ** kwargs ):
50
41
"""! Executes capability by name
51
-
52
42
@param capability Capability name
53
43
@param args Additional arguments
54
44
@param kwargs Additional arguments
@@ -67,7 +57,7 @@ def execute(self, capability, *args, **kwargs):
67
57
68
58
target_id = kwargs ['target_id' ]
69
59
image_path = os .path .normpath (kwargs ['image_path' ])
70
- with MbedBoard . chooseBoard ( board_id = target_id ) as board :
60
+ with ConnectHelper . session_with_chosen_probe ( unique_id = target_id , resume_on_disconnect = False ) as session :
71
61
# Performance hack!
72
62
# Eventually pyOCD will know default clock speed
73
63
# per target
@@ -81,51 +71,11 @@ def execute(self, capability, *args, **kwargs):
81
71
test_clock = 1000000
82
72
83
73
# Configure link
84
- board .link .set_clock (test_clock )
85
- board .link .set_deferred_transfer (True )
86
-
87
- # Collect address, data pairs for programming
88
- program_list = []
89
- extension = os .path .splitext (image_path )[1 ]
90
- if extension == '.bin' :
91
- # Binary file format
92
- memory_map = board .target .getMemoryMap ()
93
- rom_region = memory_map .getBootMemory ()
94
- with open (image_path , "rb" ) as file_handle :
95
- program_data = file_handle .read ()
96
- program_list .append ((rom_region .start , program_data ))
97
- elif extension == '.hex' :
98
- # Intel hex file format
99
- ihex = IntelHex (image_path )
100
- addresses = ihex .addresses ()
101
- addresses .sort ()
102
- for start , end in _enum_continguous_addr_start_end (addresses ):
103
- size = end - start + 1
104
- data = ihex .tobinarray (start = start , size = size )
105
- data = bytearray (data )
106
- program_list .append ((start , data ))
107
- else :
108
- # Unsupported
109
- raise Exception ("Unsupported file format %s" % extension )
110
-
111
- # Program data
112
- flash_builder = board .flash .getFlashBuilder ()
113
- for addr , data in program_list :
114
- flash_builder .addData (addr , list (bytearray (data )))
115
- flash_builder .program ()
116
-
117
- # Read back and verify programming was successful
118
- for addr , data in program_list :
119
- read_data = board .target .readBlockMemoryUnaligned8 (addr ,
120
- len (data ))
121
- read_data = bytearray (read_data )
122
- if bytes (data ) != bytes (read_data ):
123
- raise Exception ("Flash programming error - failed to "
124
- "program address 0x%x size %s" %
125
- (addr , len (data )))
74
+ session .probe .set_clock (test_clock )
126
75
127
- # Cleanup
128
- board .uninit (resume = False )
76
+ # Program the file
77
+ programmer = FileProgrammer (session )
78
+ programmer .program (image_path )
129
79
130
80
return True
131
81
0 commit comments