winIDEA SDK
emmc_example.py
1# This script is licensed under BSD License, see file LICENSE.txt.
2#
3# (c) TASKING Germany GmbH, 2023
4
5"""
6This script demonstrates all the functions from CStorageDeviceController
7and CStorageDeviceFactory classes. The functions from these classes are
8used to manipulate the various memory devices.
9
10This script uses eMMC storage device as an example and assumes that there
11is "Standard eMMC eMMC" device configured in winIDEA workspace and that debug
12debug session is already initialized.
13
14This script also creates a dummy binary file (dummyEmmc1.bin), in workspace
15directory, that is later programmed to eMMC.
16
17Be aware as this script also changes contents of eMMC device.
18"""
19
20import isystem.connect as ic
21
22# Helper function that creates binary file with specified contents
23
24winidea_id = ''
25
26
27def createDummyBinaryFile(filePath: str, content: bytes):
28 fileHandle = None
29 try:
30 fileHandle = open(filePath, "wb")
31 fileHandle.write(content)
32 except Exception as ex:
33 print(f"Cannot create file: {filePath}")
34 finally:
35 if fileHandle is not None:
36 fileHandle.close()
37
38
39# Find out index of a storage device with given name (parameter)
40def getStorageDeviceIndex(connMgr: ic.ConnectionMgr, storageDeviceName: str) -> int:
41 configuredStorageDevices = ic.StrVector()
42 ic.CStorageDeviceFactory.getDeviceNames(connMgr, configuredStorageDevices)
43 storageDeviceIndex = 0
44
45 for storageDevice in configuredStorageDevices:
46 if storageDevice == storageDeviceName:
47 return storageDeviceIndex
48 storageDeviceIndex += 1
49
50 return -1
51
52# Print out device info
53def printDeviceInfo(connMgr: ic.ConnectionMgr, storageDeviceName: str):
54 deviceInfoMap = ic.StrStrMap()
55 ic.CStorageDeviceFactory.getDeviceInfo(connMgr, storageDeviceName, deviceInfoMap)
56 print("Device info:")
57 for key in deviceInfoMap:
58 print(f"\t{key}: {deviceInfoMap[key]}")
59
60# Print out partition info of partition we are currently connected to via CStorageController
61def printPartitionInfo(emmcPartitionCtrl: ic.CStorageDeviceController):
62 partitionCfg = emmcPartitionCtrl.getPartitionConfigurationModule()
63 partitionInfo = partitionCfg.getPartitionInfo()
64
65 print("Partition info:")
66 print(f"\tName: {partitionInfo.getName()}")
67 print(f"\tType: {partitionInfo.getType()}")
68 print(f"\tBootable: {partitionInfo.isBootable()}")
69 print(f"\tExtended: {partitionInfo.getExtended()}")
70 print(f"\tSize [B]: {partitionInfo.getSize()}")
71 print(f"\tOffset: {partitionInfo.getOffset()}")
72 print(f"\tEnhanced size [B]: {partitionInfo.getEnhancedSize()}")
73 print(f"\tEnhanced offset: {partitionInfo.getEnhancedOffset()}")
74 print(f"\tCan boot status be changed: {partitionInfo.canModifyBoot()}")
75 print(f"\tCan be removed: {partitionInfo.canRemove()}")
76 print(f"\tCan size be modified: {partitionInfo.canModifySize()}")
77 print(f"\tCan offset be modified: {partitionInfo.canModifyOffset()}")
78 print(f"\tCan name be modified: {partitionInfo.canModifyName()}")
79
80def main():
81 connMgr = ic.ConnectionMgr()
82 connMgr.connect(ic.CConnectionConfig().instanceId(winidea_id))
83
84 emmcDeviceIndex = getStorageDeviceIndex(connMgr, "Standard eMMC eMMC")
85 if emmcDeviceIndex < 0:
86 print("No device named \"Standard eMMC eMMC\" is configured")
87 return
88 else:
89 print(f"eMMC storage device index: {emmcDeviceIndex}")
90
91 printDeviceInfo(connMgr, "Standard eMMC eMMC")
92
93 try:
94 # Connect to eMMC device UserData partition using device name
95 emmcPartitionCtrl = ic.CStorageDeviceFactory.makeDevice(connMgr,
96 ic.EStorageDevice_EMMCDevice,
97 "Standard eMMC eMMC",
98 "UserData")
99
100 # Write 4 bytes at address 0 to eMMC UserData partition
101 baseAddress = 0
102 size_B = 4
103 writeBuffer = ic.VectorBYTE([0xDE, 0xAD, 0xBE, 0xEF])
104 emmcPartitionCtrl.write(ic.IConnectUMI.wProgDevice, baseAddress, size_B, writeBuffer)
105
106
107 # Read 4 bytes from address 0 in eMMC UserData partition
108 readBuffer = ic.VectorBYTE()
109 emmcPartitionCtrl.read(baseAddress, size_B, readBuffer)
110 print(f"Read {size_B} bytes of data from address {baseAddress} in UserData partition:")
111 for byte in readBuffer:
112 print(f"\t{hex(byte)}")
113
114 # Erase 4 bytes from address 0 in UserData partition
115 emmcPartitionCtrl.erase(ic.IConnectUMI.wProgDevice, baseAddress, size_B)
116
117 # Check if UserData partition is empty (erased) on previously erased region
118 isEmpty = emmcPartitionCtrl.isDeviceEmpty(baseAddress, size_B)
119 endAddress = baseAddress + size_B
120 if isEmpty:
121 print(f"eMMC UserData partition is empty in range {hex(baseAddress)}-{hex(endAddress)}")
122 else:
123 print(f"eMMC UserData partition is not empty in range {hex(baseAddress)}-{hex(endAddress)}")
124 except Exception as ex:
125 print(f"Exception occurred in Python eMMC example: {str(ex)}")
126 finally:
127 # Terminate connection to eMMC device using device name
128 ic.CStorageDeviceFactory.terminateDevice(connMgr,
129 ic.EStorageDevice_EMMCDevice,
130 "Standard eMMC eMMC")
131
132 try:
133 # Connect to eMMC device Boot2 partition using device index
134 emmcPartitionCtrl = ic.CStorageDeviceFactory.makeDevice(connMgr,
135 ic.EStorageDevice_EMMCDevice,
136 emmcDeviceIndex,
137 "Boot2")
138
139 # Erase entire Boot2 partition
140 emmcPartitionCtrl.erase(ic.IConnectUMI.wProgDevice)
141
142 # Check if Boot2 partition is erased
143 isEmpty = emmcPartitionCtrl.isDeviceEmpty()
144 if isEmpty:
145 print(f"Whole eMMC Boot2 partition is empty")
146 else:
147 print(f"Whole eMMC Boot2 partition is not empty")
148
149 # create dummy file to program
150 ideCtrl = ic.CIDEController(connMgr)
151 workspacePath = ideCtrl.getPath(ic.CIDEController.WORKSPACE_DIR)
152 filePath = f"{workspacePath}\\dummyBin1.bin"
153 content = b'\xAA\xBB\xCC\xDD'
154 fileOffset = 0x0
155 createDummyBinaryFile(filePath, content)
156
157 # Program file to Boot2 partition
158 emmcPartitionCtrl.write(ic.IConnectUMI.wProgDevice, ic.IConnectUMI.wFileFormatBin, fileOffset, filePath)
159
160 #printPartitionInfo(emmcPartitionCtrl) #TODO: uncomment when fixed or remove
161
162 # Print last error (if any)
163 lastError = emmcPartitionCtrl.getLastError()
164 if len(lastError) != 0:
165 print(f"Last detected error: {lastError}")
166 else:
167 print(f"Last detected error: no error occurred")
168
169 except Exception as ex:
170 print(f"Exception occurred in Python eMMC example: {str(ex)}")
171 finally:
172 # Terminate connection to eMMC device using device index
173 ic.CStorageDeviceFactory.terminateDevice(connMgr,
174 ic.EStorageDevice_EMMCDevice,
175 emmcDeviceIndex)
176
177if __name__ == "__main__":
178 main()