winIDEA SDK
Loading...
Searching...
No Matches
emmc_example.py
# This script is licensed under BSD License, see file LICENSE.txt, or search for `License` in the SDK online help.
#
# (c) TASKING Germany GmbH, 2023
"""
This script demonstrates all the functions from CStorageDeviceController
and CStorageDeviceFactory classes. The functions from these classes are
used to manipulate the various memory devices.
This script uses eMMC storage device as an example and assumes that there
is "Standard eMMC eMMC" device configured in winIDEA workspace and that debug
debug session is already initialized.
This script also creates a dummy binary file (dummyEmmc1.bin), in workspace
directory, that is later programmed to eMMC.
Be aware as this script also changes contents of eMMC device.
"""
import isystem.connect as ic
# Helper function that creates binary file with specified contents
winidea_id = ''
def createDummyBinaryFile(filePath: str, content: bytes):
fileHandle = None
try:
fileHandle = open(filePath, "wb")
fileHandle.write(content)
except Exception as ex:
print(f"Cannot create file: {filePath}")
finally:
if fileHandle is not None:
fileHandle.close()
# Find out index of a storage device with given name (parameter)
def getStorageDeviceIndex(connMgr: ic.ConnectionMgr, storageDeviceName: str) -> int:
configuredStorageDevices = ic.StrVector()
ic.CStorageDeviceFactory.getDeviceNames(connMgr, configuredStorageDevices)
storageDeviceIndex = 0
for storageDevice in configuredStorageDevices:
if storageDevice == storageDeviceName:
return storageDeviceIndex
storageDeviceIndex += 1
return -1
# Print out device info
def printDeviceInfo(connMgr: ic.ConnectionMgr, storageDeviceName: str):
deviceInfoMap = ic.StrStrMap()
ic.CStorageDeviceFactory.getDeviceInfo(connMgr, storageDeviceName, deviceInfoMap)
print("Device info:")
for key in deviceInfoMap:
print(f"\t{key}: {deviceInfoMap[key]}")
# Print out partition info of partition we are currently connected to via CStorageController
def printPartitionInfo(emmcPartitionCtrl: ic.CStorageDeviceController):
partitionCfg = emmcPartitionCtrl.getPartitionConfigurationModule()
partitionInfo = partitionCfg.getPartitionInfo()
print("Partition info:")
print(f"\tName: {partitionInfo.getName()}")
print(f"\tType: {partitionInfo.getType()}")
print(f"\tBootable: {partitionInfo.isBootable()}")
print(f"\tExtended: {partitionInfo.getExtended()}")
print(f"\tSize [B]: {partitionInfo.getSize()}")
print(f"\tOffset: {partitionInfo.getOffset()}")
print(f"\tEnhanced size [B]: {partitionInfo.getEnhancedSize()}")
print(f"\tEnhanced offset: {partitionInfo.getEnhancedOffset()}")
print(f"\tCan boot status be changed: {partitionInfo.canModifyBoot()}")
print(f"\tCan be removed: {partitionInfo.canRemove()}")
print(f"\tCan size be modified: {partitionInfo.canModifySize()}")
print(f"\tCan offset be modified: {partitionInfo.canModifyOffset()}")
print(f"\tCan name be modified: {partitionInfo.canModifyName()}")
def main():
connMgr = ic.ConnectionMgr()
connMgr.connect(ic.CConnectionConfig().instanceId(winidea_id))
emmcDeviceIndex = getStorageDeviceIndex(connMgr, "Standard eMMC eMMC")
if emmcDeviceIndex < 0:
print("No device named \"Standard eMMC eMMC\" is configured")
return
else:
print(f"eMMC storage device index: {emmcDeviceIndex}")
printDeviceInfo(connMgr, "Standard eMMC eMMC")
try:
# Connect to eMMC device UserData partition using device name
emmcPartitionCtrl = ic.CStorageDeviceFactory.makeDevice(connMgr,
ic.EStorageDevice_EMMCDevice,
"Standard eMMC eMMC",
"UserData")
# Write 4 bytes at address 0 to eMMC UserData partition
baseAddress = 0
size_B = 4
writeBuffer = ic.VectorBYTE([0xDE, 0xAD, 0xBE, 0xEF])
emmcPartitionCtrl.write(ic.IConnectUMI.wProgDevice, baseAddress, size_B, writeBuffer)
# Read 4 bytes from address 0 in eMMC UserData partition
readBuffer = ic.VectorBYTE()
emmcPartitionCtrl.read(baseAddress, size_B, readBuffer)
print(f"Read {size_B} bytes of data from address {baseAddress} in UserData partition:")
for byte in readBuffer:
print(f"\t{hex(byte)}")
# Erase 4 bytes from address 0 in UserData partition
emmcPartitionCtrl.erase(ic.IConnectUMI.wProgDevice, baseAddress, size_B)
# Check if UserData partition is empty (erased) on previously erased region
isEmpty = emmcPartitionCtrl.isDeviceEmpty(baseAddress, size_B)
endAddress = baseAddress + size_B
if isEmpty:
print(f"eMMC UserData partition is empty in range {hex(baseAddress)}-{hex(endAddress)}")
else:
print(f"eMMC UserData partition is not empty in range {hex(baseAddress)}-{hex(endAddress)}")
except Exception as ex:
print(f"Exception occurred in Python eMMC example: {str(ex)}")
finally:
# Terminate connection to eMMC device using device name
ic.CStorageDeviceFactory.terminateDevice(connMgr,
ic.EStorageDevice_EMMCDevice,
"Standard eMMC eMMC")
try:
# Connect to eMMC device Boot2 partition using device index
emmcPartitionCtrl = ic.CStorageDeviceFactory.makeDevice(connMgr,
ic.EStorageDevice_EMMCDevice,
emmcDeviceIndex,
"Boot2")
# Erase entire Boot2 partition
emmcPartitionCtrl.erase(ic.IConnectUMI.wProgDevice)
# Check if Boot2 partition is erased
isEmpty = emmcPartitionCtrl.isDeviceEmpty()
if isEmpty:
print(f"Whole eMMC Boot2 partition is empty")
else:
print(f"Whole eMMC Boot2 partition is not empty")
# create dummy file to program
ideCtrl = ic.CIDEController(connMgr)
workspacePath = ideCtrl.getPath(ic.CIDEController.WORKSPACE_DIR)
filePath = f"{workspacePath}\\dummyBin1.bin"
content = b'\xAA\xBB\xCC\xDD'
fileOffset = 0x0
createDummyBinaryFile(filePath, content)
# Program file to Boot2 partition
emmcPartitionCtrl.write(ic.IConnectUMI.wProgDevice, ic.IConnectUMI.wFileFormatBin, fileOffset, filePath)
# Print last error (if any)
lastError = emmcPartitionCtrl.getLastError()
if len(lastError) != 0:
print(f"Last detected error: {lastError}")
else:
print(f"Last detected error: no error occurred")
except Exception as ex:
print(f"Exception occurred in Python eMMC example: {str(ex)}")
finally:
# Terminate connection to eMMC device using device index
ic.CStorageDeviceFactory.terminateDevice(connMgr,
ic.EStorageDevice_EMMCDevice,
emmcDeviceIndex)
if __name__ == "__main__":
main()