Skip to content

Commit

Permalink
RTF-5 Can download image automatically when new board is connected #2
Browse files Browse the repository at this point in the history
- Extended below single var to list var for multiple boards support
  self.blhost
  self.sdphost
  self.mcuDeviceHabStatus
  self.connectStage

Signed-off-by: Jay Heng <hengjie1989@foxmail.com>
  • Loading branch information
nxp-jay committed Jan 19, 2021
1 parent 00bc7e0 commit 465e439
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 65 deletions.
52 changes: 26 additions & 26 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ def callbackSetPortVid( self, event ):
def callbackSetBaudPid( self, event ):
self.updatePortSetupValue()

def _retryToPingBootloader( self, bootType ):
def _retryToPingBootloader( self, bootType, deviceIndex=0 ):
pingStatus = False
pingCnt = kRetryPingTimes
while (not pingStatus) and pingCnt > 0:
if bootType == kBootloaderType_Rom:
pingStatus = self.pingRom()
pingStatus = self.pingRom(deviceIndex)
elif bootType == kBootloaderType_Flashloader:
# This is mainly for RT1170 flashloader, but it is also ok for other RT devices
if self.isUartPortSelected:
time.sleep(3)
pingStatus = self.pingFlashloader()
pingStatus = self.pingFlashloader(deviceIndex)
else:
pass
if pingStatus:
Expand All @@ -109,11 +109,11 @@ def _doubleCheckBootModeError( self ):
else:
pass

def _connectFailureHandler( self ):
self.connectStage = uidef.kConnectStage_Rom
def _connectFailureHandler( self, deviceIndex=0 ):
self.connectStage[deviceIndex] = uidef.kConnectStage_Rom
self.updateConnectStatus('red')
usbIdList = self.getUsbid()
self.setPortSetupValue(self.connectStage, usbIdList, False )
self.setPortSetupValue(self.connectStage[deviceIndex], usbIdList, False )
self.setInfoStatus(uilang.kMsgLanguageContentDict['connectError_checkUsbCable'][self.languageIndex])

def _connectStateMachine( self, deviceIndex=0 ):
Expand All @@ -131,22 +131,22 @@ def _connectStateMachine( self, deviceIndex=0 ):
isConnectionFailureOnce = False
while connectSteps:
if not self.updatePortSetupValue(retryToDetectUsb):
self._connectFailureHandler()
self._connectFailureHandler(deviceIndex)
if not isConnectionFailureOnce:
isConnectionFailureOnce = True
continue
else:
return False
if self.connectStage == uidef.kConnectStage_Rom:
self.connectToDevice(self.connectStage, deviceIndex)
if self._retryToPingBootloader(kBootloaderType_Rom):
if self.connectStage[deviceIndex] == uidef.kConnectStage_Rom:
self.connectToDevice(self.connectStage[deviceIndex], deviceIndex)
if self._retryToPingBootloader(kBootloaderType_Rom, deviceIndex):
if (self.mcuSeries == uidef.kMcuSeries_iMXRT10yy) or \
(self.mcuSeries == uidef.kMcuSeries_iMXRT11yy):
self.getMcuDeviceHabStatus()
if self.jumpToFlashloader():
self.connectStage = uidef.kConnectStage_Flashloader
self.getMcuDeviceHabStatus(deviceIndex)
if self.jumpToFlashloader(deviceIndex):
self.connectStage[deviceIndex] = uidef.kConnectStage_Flashloader
usbIdList = self.getUsbid()
self.setPortSetupValue(self.connectStage, usbIdList, True )
self.setPortSetupValue(self.connectStage[deviceIndex], usbIdList, True )
else:
self.updateConnectStatus('red')
self.setInfoStatus(uilang.kMsgLanguageContentDict['connectError_failToJumpToFl'][self.languageIndex])
Expand All @@ -155,32 +155,32 @@ def _connectStateMachine( self, deviceIndex=0 ):
(self.mcuSeries == uidef.kMcuSeries_LPC) or \
(self.mcuSeries == uidef.kMcuSeries_Kinetis):
self.updateConnectStatus('green')
self.connectStage = uidef.kConnectStage_Ready
self.connectStage[deviceIndex] = uidef.kConnectStage_Ready
else:
pass
else:
self.updateConnectStatus('red')
self._doubleCheckBootModeError()
return False
elif self.connectStage == uidef.kConnectStage_Flashloader:
self.connectToDevice(self.connectStage, deviceIndex)
if self._retryToPingBootloader(kBootloaderType_Flashloader):
elif self.connectStage[deviceIndex] == uidef.kConnectStage_Flashloader:
self.connectToDevice(self.connectStage[deviceIndex], deviceIndex)
if self._retryToPingBootloader(kBootloaderType_Flashloader, deviceIndex):
self.updateConnectStatus('green')
self.connectStage = uidef.kConnectStage_Ready
self.connectStage[deviceIndex] = uidef.kConnectStage_Ready
else:
self.setInfoStatus(uilang.kMsgLanguageContentDict['connectError_failToPingFl'][self.languageIndex])
self._connectFailureHandler()
self._connectFailureHandler(deviceIndex)
return False
elif self.connectStage == uidef.kConnectStage_Ready:
elif self.connectStage[deviceIndex] == uidef.kConnectStage_Ready:
if connectSteps == 1:
self.setInfoStatus(uilang.kMsgLanguageContentDict['connectInfo_readyForDownload'][self.languageIndex])
return True
else:
if self._retryToPingBootloader(kBootloaderType_Flashloader):
if self._retryToPingBootloader(kBootloaderType_Flashloader, deviceIndex):
self.setInfoStatus(uilang.kMsgLanguageContentDict['connectInfo_readyForDownload'][self.languageIndex])
return True
else:
self.connectStage = uidef.kConnectStage_Rom
self.connectStage[deviceIndex] = uidef.kConnectStage_Rom
connectSteps += 1
else:
pass
Expand Down Expand Up @@ -208,16 +208,16 @@ def _doUartAllInOneAction( self ):
operations += 1
if self._connectStateMachine(board):
for i in range(len(self.sbAppFiles)):
if self.flashSbImage(self.sbAppFiles[i]):
if self.flashSbImage(self.sbAppFiles[i], board):
if i == len(self.sbAppFiles) - 1:
successes += 1
self.updateConnectStatus('blue')
self.setInfoStatus(uilang.kMsgLanguageContentDict['downloadInfo_success'][self.languageIndex])
else:
self.updateConnectStatus('red')
break
self.resetMcuDevice()
self.connectStage = uidef.kConnectStage_Rom
self.resetMcuDevice(board)
self.connectStage[board] = uidef.kConnectStage_Rom
self._setUartUsbPort()
self.setDownloadOperationResults(operations, successes)

Expand Down
73 changes: 37 additions & 36 deletions src/run/runcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ class flashRun(uicore.flashUi):

def __init__(self, parent):
uicore.flashUi.__init__(self, parent)
self.blhost = None
self.sdphost = None
self.blhost = [None] * uidef.kMaxMfgBoards
self.sdphost = [None] * uidef.kMaxMfgBoards
self.tgt = None
self.cpuDir = None
self.sdphostVectorsDir = os.path.join(self.exeTopRoot, 'tools', 'sdphost', 'win', 'vectors')
self.blhostVectorsDir = os.path.join(self.exeTopRoot, 'tools', 'blhost2_3', 'win', 'vectors')
self.mcuDeviceHabStatus = None
self.mcuDeviceHabStatus = [None] * uidef.kMaxMfgBoards
self.createMcuTarget()

def createMcuTarget( self ):
Expand Down Expand Up @@ -120,7 +120,7 @@ def connectToDevice( self , connectStage, deviceIndex=0 ):
else:
pass
if self.mcuSeries == uidef.kMcuSeries_iMXRT10yy:
self.sdphost = bltest.createBootloader(self.tgt,
self.sdphost[deviceIndex] = bltest.createBootloader(self.tgt,
self.sdphostVectorsDir,
xPeripheral,
uartBaudrate, uartComPort,
Expand All @@ -129,7 +129,7 @@ def connectToDevice( self , connectStage, deviceIndex=0 ):
(self.mcuSeries == uidef.kMcuSeries_iMXRTxxx) or \
(self.mcuSeries == uidef.kMcuSeries_LPC) or \
(self.mcuSeries == uidef.kMcuSeries_Kinetis):
self.blhost = bltest.createBootloader(self.tgt,
self.blhost[deviceIndex] = bltest.createBootloader(self.tgt,
self.blhostVectorsDir,
xPeripheral,
uartBaudrate, uartComPort,
Expand All @@ -150,26 +150,27 @@ def connectToDevice( self , connectStage, deviceIndex=0 ):
usbDevicePath = self.usbDevicePath[deviceIndex]['flashloader']
else:
pass
self.blhost = bltest.createBootloader(self.tgt,
self.blhost[deviceIndex] = bltest.createBootloader(self.tgt,
self.blhostVectorsDir,
blPeripheral,
uartBaudrate, uartComPort,
usbDevicePath,
True)
elif connectStage == uidef.kConnectStage_Reset:
self.tgt = None
#self.tgt = None
pass
else:
pass

def pingRom( self ):
def pingRom( self, deviceIndex=0 ):
if self.mcuSeries == uidef.kMcuSeries_iMXRT10yy:
status, results, cmdStr = self.sdphost.errorStatus()
status, results, cmdStr = self.sdphost[deviceIndex].errorStatus()
return (status == boot.status.kSDP_Status_HabEnabled or status == boot.status.kSDP_Status_HabDisabled)
elif (self.mcuSeries == uidef.kMcuSeries_iMXRT11yy) or \
(self.mcuSeries == uidef.kMcuSeries_iMXRTxxx) or \
(self.mcuSeries == uidef.kMcuSeries_LPC) or \
(self.mcuSeries == uidef.kMcuSeries_Kinetis):
status, results, cmdStr = self.blhost.getProperty(boot.properties.kPropertyTag_CurrentVersion)
status, results, cmdStr = self.blhost[deviceIndex].getProperty(boot.properties.kPropertyTag_CurrentVersion)
return (status == boot.status.kStatus_Success)
else:
pass
Expand All @@ -185,11 +186,11 @@ def _getVal32FromBinFile( self, filename, offset=0):
var32Vaule = (ord(var32Vaule[3])<<24) + (ord(var32Vaule[2])<<16) + (ord(var32Vaule[1])<<8) + ord(var32Vaule[0])
return var32Vaule

def _getDeviceRegisterBySdphost( self, regAddr):
def _getDeviceRegisterBySdphost( self, regAddr, deviceIndex=0 ):
if self.tgt.hasSdpReadRegisterCmd:
filename = 'readReg.dat'
filepath = os.path.join(self.sdphostVectorsDir, filename)
status, results, cmdStr = self.sdphost.readRegister(regAddr, 32, 4, filename)
status, results, cmdStr = self.sdphost[deviceIndex].readRegister(regAddr, 32, 4, filename)
if (status == boot.status.kSDP_Status_HabEnabled or status == boot.status.kSDP_Status_HabDisabled):
regVal = self._getVal32FromBinFile(filepath)
return regVal
Expand All @@ -200,82 +201,82 @@ def _getDeviceRegisterBySdphost( self, regAddr):
except:
pass

def getMcuDeviceHabStatus( self ):
def getMcuDeviceHabStatus( self, deviceIndex=0 ):
if self.mcuSeries == uidef.kMcuSeries_iMXRT10yy:
if self.tgt.hasSdpReadRegisterCmd:
secConfig = self._getDeviceRegisterBySdphost( self.tgt.registerAddrDict['kRegisterAddr_SRC_SBMR2'])
secConfig = self._getDeviceRegisterBySdphost( self.tgt.registerAddrDict['kRegisterAddr_SRC_SBMR2'], deviceIndex)
if secConfig != None:
self.mcuDeviceHabStatus = ((secConfig & self.tgt.registerDefnDict['kRegisterMask_SRC_SBMR2_SecConfig']) >> self.tgt.registerDefnDict['kRegisterShift_SRC_SBMR2_SecConfig'])
if self.mcuDeviceHabStatus == rundef.kHabStatus_FAB:
self.mcuDeviceHabStatus[deviceIndex] = ((secConfig & self.tgt.registerDefnDict['kRegisterMask_SRC_SBMR2_SecConfig']) >> self.tgt.registerDefnDict['kRegisterShift_SRC_SBMR2_SecConfig'])
if self.mcuDeviceHabStatus[deviceIndex] == rundef.kHabStatus_FAB:
self.setHabStatus(u"FAB")
elif self.mcuDeviceHabStatus == rundef.kHabStatus_Open:
elif self.mcuDeviceHabStatus[deviceIndex] == rundef.kHabStatus_Open:
self.setHabStatus(u"Open")
elif self.mcuDeviceHabStatus == rundef.kHabStatus_Closed0 or self.mcuDeviceHabStatus == rundef.kHabStatus_Closed1:
elif self.mcuDeviceHabStatus[deviceIndex] == rundef.kHabStatus_Closed0 or self.mcuDeviceHabStatus == rundef.kHabStatus_Closed1:
self.setHabStatus(u"Closed")
else:
pass
else:
status, results, cmdStr = self.sdphost.errorStatus()
status, results, cmdStr = self.sdphost[deviceIndex].errorStatus()
if status == boot.status.kSDP_Status_HabEnabled:
self.mcuDeviceHabStatus = rundef.kHabStatus_Closed0
self.mcuDeviceHabStatus[deviceIndex] = rundef.kHabStatus_Closed0
self.setHabStatus(u"Closed")
elif status == boot.status.kSDP_Status_HabDisabled:
self.mcuDeviceHabStatus = rundef.kHabStatus_Open
self.mcuDeviceHabStatus[deviceIndex] = rundef.kHabStatus_Open
self.setHabStatus(u"Open")
else:
pass
elif self.mcuSeries == uidef.kMcuSeries_iMXRT11yy:
status, results, cmdStr = self.blhost.getProperty(boot.properties.kPropertyTag_FlashSecurityState)
status, results, cmdStr = self.blhost[deviceIndex].getProperty(boot.properties.kPropertyTag_FlashSecurityState)
if status == boot.status.kStatus_Success:
if results[0] == 0:
self.mcuDeviceHabStatus = rundef.kHabStatus_Open
self.mcuDeviceHabStatus[deviceIndex] = rundef.kHabStatus_Open
self.setHabStatus(u"Open")
else:
self.mcuDeviceHabStatus = rundef.kHabStatus_Closed0
self.mcuDeviceHabStatus[deviceIndex] = rundef.kHabStatus_Closed0
self.setHabStatus(u"Closed")
else:
pass
else:
pass

def jumpToFlashloader( self ):
def jumpToFlashloader( self, deviceIndex=0 ):
flashloaderBinFile = None
if self.mcuDeviceHabStatus == rundef.kHabStatus_Closed0 or self.mcuDeviceHabStatus == rundef.kHabStatus_Closed1:
if self.mcuDeviceHabStatus[deviceIndex] == rundef.kHabStatus_Closed0 or self.mcuDeviceHabStatus[deviceIndex] == rundef.kHabStatus_Closed1:
flashloaderBinFile = os.path.join(self.cpuDir, 'ivt_flashloader_signed.bin')
if not os.path.isfile(flashloaderBinFile):
self.setInfoStatus(uilang.kMsgLanguageContentDict['connectError_notValidSignedFl'][self.languageIndex])
return False
elif self.mcuDeviceHabStatus == rundef.kHabStatus_FAB or self.mcuDeviceHabStatus == rundef.kHabStatus_Open:
elif self.mcuDeviceHabStatus[deviceIndex] == rundef.kHabStatus_FAB or self.mcuDeviceHabStatus[deviceIndex] == rundef.kHabStatus_Open:
flashloaderBinFile = os.path.join(self.cpuDir, 'ivt_flashloader.bin')
else:
pass
if self.mcuSeries == uidef.kMcuSeries_iMXRT10yy:
status, results, cmdStr = self.sdphost.writeFile(self.tgt.flashloaderLoadAddr, flashloaderBinFile)
status, results, cmdStr = self.sdphost[deviceIndex].writeFile(self.tgt.flashloaderLoadAddr, flashloaderBinFile)
if status != boot.status.kSDP_Status_HabEnabled and status != boot.status.kSDP_Status_HabDisabled:
return False
status, results, cmdStr = self.sdphost.jumpAddress(self.tgt.flashloaderJumpAddr)
status, results, cmdStr = self.sdphost[deviceIndex].jumpAddress(self.tgt.flashloaderJumpAddr)
if status != boot.status.kSDP_Status_HabEnabled and status != boot.status.kSDP_Status_HabDisabled:
return False
elif self.mcuSeries == uidef.kMcuSeries_iMXRT11yy:
status, results, cmdStr = self.blhost.loadImage(flashloaderBinFile)
status, results, cmdStr = self.blhost[deviceIndex].loadImage(flashloaderBinFile)
if status != boot.status.kStatus_Success:
return False
else:
pass
return True

def pingFlashloader( self ):
status, results, cmdStr = self.blhost.getProperty(boot.properties.kPropertyTag_CurrentVersion)
def pingFlashloader( self, deviceIndex=0 ):
status, results, cmdStr = self.blhost[deviceIndex].getProperty(boot.properties.kPropertyTag_CurrentVersion)
return (status == boot.status.kStatus_Success)

def flashSbImage( self, sbAppFile ):
status, results, cmdStr = self.blhost.receiveSbFile(sbAppFile)
def flashSbImage( self, sbAppFile, deviceIndex=0 ):
status, results, cmdStr = self.blhost[deviceIndex].receiveSbFile(sbAppFile)
if (status == boot.status.kStatus_Success) or (status == boot.status.kStatus_AbortDataPhase):
return True
else:
self.setInfoStatus(uilang.kMsgLanguageContentDict['downloadError_failToDownload'][self.languageIndex])
return False

def resetMcuDevice( self ):
status, results, cmdStr = self.blhost.reset()
def resetMcuDevice( self, deviceIndex=0 ):
status, results, cmdStr = self.blhost[deviceIndex].reset()
return (status == boot.status.kStatus_Success)
6 changes: 3 additions & 3 deletions src/ui/uicore.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, parent):
if not os.path.isfile(exeMainFile):
self.exeTopRoot = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))

self.connectStage = uidef.kConnectStage_Rom
self.connectStage = [uidef.kConnectStage_Rom] * uidef.kMaxMfgBoards
self.connectStatusColor = None

self.isUartPortSelected = None
Expand Down Expand Up @@ -199,7 +199,7 @@ def _retryToDetectUsbhidDevice( self, needToRetry = True ):
hidDevice = hidFilter.get_devices()
self._setUsbDetectedBoardNum(len(hidDevice))
if (not self.isDymaticUsbDetection) or (len(hidDevice) > 0):
if self.connectStage == uidef.kConnectStage_Rom:
if self.connectStage[0] == uidef.kConnectStage_Rom:
for i in range(len(hidDevice)):
if len(self.usbDevicePath):
hasThisPath = False
Expand All @@ -217,7 +217,7 @@ def _retryToDetectUsbhidDevice( self, needToRetry = True ):
'flashloader':None
}
self.usbDevicePath.append(usbDevicePath)
elif self.connectStage == uidef.kConnectStage_Flashloader:
elif self.connectStage[0] == uidef.kConnectStage_Flashloader:
for i in range(len(hidDevice)):
hasThisPath = False
for j in range(len(self.usbDevicePath)):
Expand Down
2 changes: 2 additions & 0 deletions src/ui/uidef.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import wx
import sys, os

kMaxMfgBoards = 50

kConnectStage_Rom = 1
kConnectStage_Flashloader = 2
kConnectStage_Ready = 3
Expand Down

0 comments on commit 465e439

Please sign in to comment.