toprammer-gui 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896
  1. #!/usr/bin/env python3
  2. """
  3. # TOP2049 Open Source programming suite
  4. #
  5. # Qt-based graphical user interface
  6. #
  7. # Copyright (c) 2010-2023 Michael Buesch <m@bues.ch>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. """
  23. from libtoprammer.main import *
  24. from libtoprammer.util import *
  25. import sys
  26. import time
  27. import html
  28. import configparser
  29. try:
  30. from PyQt6.QtCore import *
  31. from PyQt6.QtGui import *
  32. from PyQt6.QtWidgets import *
  33. Signal = pyqtSignal
  34. except ImportError as e:
  35. print("Failed to import PyQt6 modules: %s" % str(e))
  36. print("Please install PyQt6. On Debian Linux run: apt install python3-pyqt6")
  37. sys.exit(1)
  38. EVENT_HWTHREAD = QEvent.Type(QEvent.Type.User + 0)
  39. def stringRemoveChars(string, chars):
  40. ret = []
  41. for c in string:
  42. if c not in chars:
  43. ret.append(c)
  44. return "".join(ret)
  45. def htmlEscape(plaintext):
  46. return html.escape(plaintext, True)
  47. def getIconPath(name):
  48. return pkg_resources.resource_filename("libtoprammer",
  49. "icons/" + name + ".png")
  50. def getIcon(name):
  51. return QIcon(getIconPath(name))
  52. class Wrapper(object):
  53. def __init__(self, obj):
  54. self.obj = obj
  55. def __eq__(self, other):
  56. return self.obj == other.obj
  57. def __ne__(self, other):
  58. return self.obj != other.obj
  59. class ZifPinButton(QWidget):
  60. TEXT_RIGHT = 0
  61. TEXT_LEFT = 1
  62. stateChanged = Signal(bool)
  63. class Label(QLabel):
  64. clicked = Signal()
  65. def __init__(self, text, parent=None):
  66. QLabel.__init__(self, text, parent)
  67. def mousePressEvent(self, event):
  68. self.clicked.emit()
  69. def __init__(self, text, textPos=TEXT_RIGHT, parent=None):
  70. QWidget.__init__(self, parent)
  71. self.setLayout(QHBoxLayout(self))
  72. self.layout().setContentsMargins(QMargins())
  73. self.checkbox = QCheckBox(self)
  74. self.checkbox.stateChanged.connect(self.__cbStateChanged)
  75. self.label = self.Label(text, self)
  76. self.label.clicked.connect(self.toggle)
  77. if textPos == self.TEXT_RIGHT:
  78. self.label.setAlignment(Qt.AlignmentFlag.AlignLeft)
  79. self.layout().addWidget(self.checkbox)
  80. self.layout().addWidget(self.label)
  81. else:
  82. self.label.setAlignment(Qt.AlignmentFlag.AlignRight)
  83. self.layout().addWidget(self.label)
  84. self.layout().addWidget(self.checkbox)
  85. def __cbStateChanged(self, newState):
  86. self.stateChanged.emit(newState == Qt.CheckState.Checked)
  87. def state(self):
  88. return self.checkbox.checkState() == Qt.CheckState.Checked
  89. def setState(self, en):
  90. self.checkbox.setCheckState(Qt.CheckState.Checked if en else Qt.CheckState.Unchecked)
  91. def toggle(self):
  92. self.setState(not self.state())
  93. class ZifWidget(QGroupBox):
  94. def __init__(self, unitest, nrZifPins):
  95. QGroupBox.__init__(self, "ZIF socket", unitest)
  96. self.unitest = unitest
  97. self.setLayout(QGridLayout())
  98. self.nrPins = nrZifPins
  99. assert(self.nrPins % 2 == 0)
  100. self.blockedPins = []
  101. self.ignoreOutChange = False
  102. label = QLabel("ZIF\nsocket", self)
  103. label.setFrameShape(QFrame.Shape.Panel)
  104. label.setFrameShadow(QFrame.Shadow.Sunken)
  105. label.setAlignment(Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter)
  106. label.setMinimumWidth(80)
  107. self.layout().addWidget(label, 0, 2, self.nrPins // 2, 1)
  108. self.pins = [ None ] * self.nrPins
  109. self.pinOuten = [ None ] * self.nrPins
  110. for i in range(0, self.nrPins // 2):
  111. left = i + 1
  112. right = self.nrPins - i
  113. self.pins[left - 1] = ZifPinButton(str(left),
  114. ZifPinButton.TEXT_LEFT, self)
  115. self.pins[left - 1].stateChanged.connect(self.__outChanged)
  116. self.pinOuten[left - 1] = ZifPinButton("out",
  117. ZifPinButton.TEXT_LEFT, self)
  118. self.pinOuten[left - 1].stateChanged.connect(self.__outEnChanged)
  119. self.layout().addWidget(self.pinOuten[left - 1], left - 1, 0)
  120. self.layout().addWidget(self.pins[left - 1], left - 1, 1)
  121. self.pins[right - 1] = ZifPinButton(str(right),
  122. ZifPinButton.TEXT_RIGHT, self)
  123. self.pins[right - 1].stateChanged.connect(self.__outChanged)
  124. self.pinOuten[right - 1] = ZifPinButton("out",
  125. ZifPinButton.TEXT_RIGHT, self)
  126. self.pinOuten[right - 1].stateChanged.connect(self.__outEnChanged)
  127. self.layout().addWidget(self.pins[right - 1], self.nrPins - right, 3)
  128. self.layout().addWidget(self.pinOuten[right - 1], self.nrPins - right, 4)
  129. self.__outEnChanged()
  130. self.__outChanged()
  131. def readInputs(self):
  132. try:
  133. inputMask = self.unitest.queryTop("top.getChip().getInputs()")
  134. except (TOPException) as e:
  135. QMessageBox.critical(self, "TOP communication failed",
  136. "Failed to fetch input states:\n" +\
  137. str(e))
  138. return False
  139. for i in range(0, self.nrPins):
  140. if not self.pinOuten[i].state() and\
  141. i + 1 not in self.blockedPins:
  142. state = False
  143. if inputMask & bit(i):
  144. state = True
  145. self.ignoreOutChange = True
  146. self.pins[i].setState(state)
  147. self.ignoreOutChange = False
  148. return True
  149. def __updateInOutStates(self):
  150. for i in range(0, self.nrPins):
  151. if i + 1 in self.blockedPins:
  152. self.pins[i].setEnabled(False)
  153. self.pins[i].setState(False)
  154. self.pinOuten[i].setEnabled(False)
  155. self.pinOuten[i].setState(False)
  156. else:
  157. self.pinOuten[i].setEnabled(True)
  158. if self.pinOuten[i].state():
  159. self.pins[i].setEnabled(True)
  160. else:
  161. self.pins[i].setEnabled(False)
  162. def __outEnChanged(self, unused=False):
  163. self.__updateInOutStates()
  164. outEnMask = self.getOutEnMask()
  165. try:
  166. self.unitest.queryTop("top.getChip().setOutputEnableMask(...)",
  167. outEnMask)
  168. except (TOPException) as e:
  169. QMessageBox.critical(self, "TOP communication failed",
  170. "Failed to set output-enable states:\n" +\
  171. str(e))
  172. return
  173. self.readInputs()
  174. def __outChanged(self, unused=False):
  175. if self.ignoreOutChange:
  176. return
  177. outMask = self.getOutMask()
  178. try:
  179. self.unitest.queryTop("top.getChip().setOutputs(...)",
  180. outMask)
  181. except (TOPException) as e:
  182. QMessageBox.critical(self, "TOP communication failed",
  183. "Failed to set output states:\n" +\
  184. str(e))
  185. return
  186. self.readInputs()
  187. def setBlockedPins(self, blockedPins):
  188. self.blockedPins = blockedPins
  189. self.__updateInOutStates()
  190. self.readInputs()
  191. def getOutEnMask(self):
  192. outEnMask = 0
  193. for i in range(0, self.nrPins):
  194. if self.pinOuten[i].state():
  195. outEnMask |= bit(i)
  196. return outEnMask
  197. def setOutEnMask(self, mask):
  198. for i in range(0, self.nrPins):
  199. if mask & bit(i):
  200. self.pinOuten[i].setState(True)
  201. else:
  202. self.pinOuten[i].setState(False)
  203. mask &= ~bit(i)
  204. if mask:
  205. raise TOPException("ZIF out-en mask has too many bits set")
  206. def getOutMask(self):
  207. outMask = 0
  208. for i in range(0, self.nrPins):
  209. if self.pins[i].state():
  210. outMask |= bit(i)
  211. return outMask
  212. def setOutMask(self, mask):
  213. for i in range(0, self.nrPins):
  214. if mask & bit(i):
  215. self.pins[i].setState(True)
  216. else:
  217. self.pins[i].setState(False)
  218. mask &= ~bit(i)
  219. if mask:
  220. raise TOPException("ZIF out mask has too many bits set")
  221. class UnitestDialog(QDialog):
  222. def __init__(self, mainWindow):
  223. QDialog.__init__(self, mainWindow)
  224. self.setWindowTitle("Universal logic tester")
  225. self.mainWindow = mainWindow
  226. self.setLayout(QGridLayout())
  227. self.inputPollBlocked = 0
  228. self.uiChangeBlocked = 0
  229. # Initialize the unitest chip
  230. (failed, returnValue) = self.mainWindow.runOperationSync(
  231. HwThread.TASK_INITCHIP,
  232. "unitest")
  233. if failed:
  234. raise TOPException("Failed to load 'unitest' chip: %s" % str(returnValue))
  235. self.queryTop("top.getChip().reset()")
  236. # Query the hardware layer for common parameters
  237. self.param_topType = self.queryTop("top.getProgrammerType()")
  238. self.param_gndLayouts = self.queryTop("top.gnd.supportedLayouts()")
  239. self.param_nrZifPins = self.queryTop("top.gnd.getNrOfPins()")
  240. self.param_vccLayouts = self.queryTop("top.vcc.supportedLayouts()")
  241. self.param_minVccVolt = self.queryTop("top.vcc.minVoltage()")
  242. self.param_maxVccVolt = self.queryTop("top.vcc.maxVoltage()")
  243. self.param_vppLayouts = self.queryTop("top.vpp.supportedLayouts()")
  244. self.param_minVppVolt = self.queryTop("top.vpp.minVoltage()")
  245. self.param_maxVppVolt = self.queryTop("top.vpp.maxVoltage()")
  246. self.param_oscFreq = self.queryTop("top.getOscillatorHz()")
  247. assert(self.param_nrZifPins % 2 == 0)
  248. self.param_vppLayouts.sort(key=lambda layId_layMask: layId_layMask[1])
  249. self.menuBar = QMenuBar(self)
  250. self.menuBar.addAction("&Load settings...", self.loadSettings)
  251. self.menuBar.addAction("&Save settings...", self.saveSettings)
  252. self.menuBar.addAction("&Raw command...", self.mainWindow.sendRawCommand)
  253. self.layout().addWidget(self.menuBar, 0, 0, 1, 3)
  254. self.zifWidget = ZifWidget(self, self.param_nrZifPins)
  255. self.layout().addWidget(self.zifWidget, 1, 0, 10, 1)
  256. group = QGroupBox("GND layout", self)
  257. group.setLayout(QGridLayout())
  258. self.gndLayout = QComboBox(self)
  259. self.gndLayout.addItem("Not connected", 0)
  260. for (layId, layMask) in self.param_gndLayouts:
  261. if not layMask:
  262. continue
  263. descr = "GND on pin "
  264. for i in range(0, self.param_nrZifPins):
  265. if layMask & bit(i):
  266. descr += str(i + 1) + " "
  267. self.gndLayout.addItem(descr, layId)
  268. group.layout().addWidget(self.gndLayout, 0, 0)
  269. self.layout().addWidget(group, 1, 1)
  270. group = QGroupBox("VCC layout", self)
  271. group.setLayout(QGridLayout())
  272. self.vccVoltage = QDoubleSpinBox(self)
  273. self.vccVoltage.setSuffix(" V")
  274. self.vccVoltage.setMinimum(self.param_minVccVolt)
  275. self.vccVoltage.setMaximum(self.param_maxVccVolt)
  276. self.vccVoltage.setSingleStep(0.1)
  277. group.layout().addWidget(self.vccVoltage, 0, 0)
  278. self.vccLayout = QComboBox(self)
  279. self.vccLayout.addItem("Not connected", 0)
  280. for (layId, layMask) in self.param_vccLayouts:
  281. if not layMask:
  282. continue
  283. descr = "VCC on pin "
  284. for i in range(0, self.param_nrZifPins):
  285. if layMask & bit(i):
  286. descr += str(i + 1) + " "
  287. self.vccLayout.addItem(descr, layId)
  288. group.layout().addWidget(self.vccLayout, 1, 0)
  289. self.layout().addWidget(group, 2, 1)
  290. group = QGroupBox("Input polling", self)
  291. group.setLayout(QGridLayout())
  292. self.inputPollEn = QCheckBox("Enabled", self)
  293. self.inputPollEn.setCheckState(Qt.CheckState.Checked)
  294. group.layout().addWidget(self.inputPollEn, 0, 0)
  295. self.inputPollInterval = QDoubleSpinBox(self)
  296. self.inputPollInterval.setPrefix("Interval ")
  297. self.inputPollInterval.setSuffix(" seconds")
  298. self.inputPollInterval.setMinimum(0.25)
  299. self.inputPollInterval.setSingleStep(0.25)
  300. self.inputPollInterval.setValue(1.0)
  301. group.layout().addWidget(self.inputPollInterval, 1, 0)
  302. self.layout().addWidget(group, 3, 1)
  303. group = QGroupBox("Frequency counter", self)
  304. group.setLayout(QGridLayout())
  305. self.fcntEn = QCheckBox("Enabled", self)
  306. self.fcntEn.setCheckState(Qt.CheckState.Unchecked)
  307. group.layout().addWidget(self.fcntEn, 0, 0)
  308. self.fcntPosEdge = QCheckBox("Positive edge", self)
  309. self.fcntPosEdge.setCheckState(Qt.CheckState.Checked)
  310. group.layout().addWidget(self.fcntPosEdge, 1, 0)
  311. self.fcntPin = QComboBox(self)
  312. group.layout().addWidget(self.fcntPin, 2, 0)
  313. self.fcntValueLabel = QLabel(self)
  314. group.layout().addWidget(self.fcntValueLabel, 3, 0)
  315. self.fcntValue = 0
  316. self.layout().addWidget(group, 4, 1)
  317. group = QGroupBox("Oscillator", self)
  318. group.setLayout(QGridLayout())
  319. self.oscPin = QComboBox(self)
  320. self.oscPin.addItem("Disabled", Wrapper(0))
  321. for i in range(0, self.param_nrZifPins):
  322. self.oscPin.addItem("On pin %d" % (i + 1),
  323. Wrapper(bit(i)))
  324. group.layout().addWidget(self.oscPin, 0, 0)
  325. self.oscDiv = QSpinBox(self)
  326. self.oscDiv.setPrefix("Divider ")
  327. self.oscDiv.setSingleStep(1)
  328. self.oscDiv.setMinimum(1)
  329. self.oscDiv.setMaximum(self.param_oscFreq)
  330. self.oscDiv.setValue(self.param_oscFreq // 1000)
  331. group.layout().addWidget(self.oscDiv, 1, 0)
  332. self.oscFreq = QLabel(self)
  333. group.layout().addWidget(self.oscFreq, 2, 0)
  334. self.layout().addWidget(group, 5, 1)
  335. group = QGroupBox("VPP layout", self)
  336. group.setLayout(QGridLayout())
  337. self.vppVoltage = QDoubleSpinBox(self)
  338. self.vppVoltage.setSuffix(" V")
  339. self.vppVoltage.setMinimum(self.param_minVppVolt)
  340. self.vppVoltage.setMaximum(self.param_maxVppVolt)
  341. self.vppVoltage.setSingleStep(0.1)
  342. group.layout().addWidget(self.vppVoltage, 0, 0, 1, 2)
  343. self.vppLayouts = {}
  344. xOffset = 0
  345. yOffset = 0
  346. for (layId, layMask) in self.param_vppLayouts:
  347. if not layMask:
  348. continue
  349. for i in range(0, self.param_nrZifPins):
  350. if layMask & bit(i):
  351. descr = str(i + 1) + " "
  352. self.vppLayouts[layId] = QCheckBox(descr, self)
  353. self.vppLayouts[layId].stateChanged.connect(
  354. self.vppLayoutChanged)
  355. group.layout().addWidget(self.vppLayouts[layId],
  356. yOffset + 1, xOffset)
  357. yOffset += 1
  358. if yOffset == len(self.param_vppLayouts) // 2:
  359. yOffset = 0
  360. xOffset += 1
  361. self.layout().addWidget(group, 1, 2, 8, 1)
  362. self.inputPollTimer = QTimer()
  363. self.inputPollTimer.setSingleShot(True)
  364. self.inputPollTimer.timeout.connect(self.doInputPollTimer)
  365. self.inputPollEn.stateChanged.connect(self.inputPollChanged)
  366. self.inputPollInterval.valueChanged.connect(self.inputPollChanged)
  367. self.gndLayout.currentIndexChanged.connect(self.gndLayoutChanged)
  368. self.vccVoltage.valueChanged.connect(self.vccLayoutChanged)
  369. self.vccLayout.currentIndexChanged.connect(self.vccLayoutChanged)
  370. self.vppVoltage.valueChanged.connect(self.vppLayoutChanged)
  371. self.fcntEn.stateChanged.connect(self.fcntChanged)
  372. self.fcntPosEdge.stateChanged.connect(self.fcntChanged)
  373. self.fcntPin.currentIndexChanged.connect(self.fcntChanged)
  374. self.oscPin.currentIndexChanged.connect(self.oscChanged)
  375. self.oscDiv.valueChanged.connect(self.oscChanged)
  376. self.__modeChanged()
  377. self.gndLayoutChanged()
  378. self.vccLayoutChanged()
  379. self.inputPollChanged()
  380. self.oscChanged()
  381. def __readFcntValue(self):
  382. try:
  383. self.fcntValue = self.queryTop("top.getChip().getFreqCount()")
  384. except (TOPException) as e:
  385. QMessageBox.critical(self, "TOP communication failed",
  386. "Failed to read frequency counter value:\n" +\
  387. str(e))
  388. return False
  389. return True
  390. def __updateFcntValueLabel(self):
  391. if self.getMode() != Chip_Unitest.MODE_FCNT:
  392. self.fcntValueLabel.clear()
  393. return
  394. if self.fcntValue:
  395. hz = "%.3f" % (float(self.param_oscFreq) / self.fcntValue)
  396. else:
  397. hz = "~~~"
  398. self.fcntValueLabel.setText("Measured: %s Hz" % hz)
  399. def __modeChanged(self):
  400. mode = self.getMode()
  401. activePinMask = self.queryTop("top.getChip().getActivePinMask()")
  402. self.uiChangeBlocked += 1
  403. # Update freq count UI
  404. self.fcntValue = 0
  405. self.fcntPin.clear()
  406. self.fcntPin.setEnabled(False)
  407. self.fcntPosEdge.setEnabled(False)
  408. if mode == Chip_Unitest.MODE_FCNT:
  409. for i in range(0, self.param_nrZifPins):
  410. if bit(i) & activePinMask == 0:
  411. continue
  412. self.fcntPin.addItem("On pin %d" % (i + 1), i)
  413. self.fcntPin.setEnabled(True)
  414. self.fcntPosEdge.setEnabled(True)
  415. self.__updateFcntValueLabel()
  416. # Update the ZIF widget
  417. self.updateZifCheckboxes()
  418. self.uiChangeBlocked -= 1
  419. def switchMode(self, mode):
  420. if self.getMode() == mode:
  421. return
  422. self.queryTop("top.getChip().setMode(...)", mode)
  423. self.__modeChanged()
  424. def __loadFile(self, filename):
  425. try:
  426. p = configparser.ConfigParser()
  427. p.read((filename,))
  428. sect = "TOPRAMMER-UNITEST-SETTINGS"
  429. if not p.has_section(sect):
  430. raise TOPException("Invalid file format")
  431. ver = p.getint(sect, "fileVersion")
  432. verExpected = 1
  433. if ver != verExpected:
  434. raise TOPException("Unsupported file version (got %d, expected %d)" %\
  435. (ver, verExpected))
  436. pType = p.get(sect, "programmerType")
  437. if pType.upper() != self.param_topType.upper():
  438. raise TOPException("Programmer type mismatch (file = %s, connected = %s)" %\
  439. (pType, self.param_topType))
  440. layout = p.getint(sect, "gndLayout")
  441. idx = self.gndLayout.findData(layout)
  442. if idx < 0:
  443. raise TOPException("Invalid GND layout")
  444. self.gndLayout.setCurrentIndex(idx)
  445. layout = p.getint(sect, "vccLayout")
  446. idx = self.vccLayout.findData(layout)
  447. if idx < 0:
  448. raise TOPException("Invalid VCC layout")
  449. self.vccLayout.setCurrentIndex(idx)
  450. voltage = p.getfloat(sect, "vccVoltage")
  451. self.vccVoltage.setValue(voltage)
  452. layouts = p.get(sect, "vppLayout").split(",")
  453. for layId in list(self.vppLayouts.keys()):
  454. if layId in layouts:
  455. self.vppLayouts[layId].setCheckState(Qt.CheckState.Checked)
  456. else:
  457. self.vppLayouts[layId].setCheckState(Qt.CheckState.Unchecked)
  458. voltage = p.getfloat(sect, "vppVoltage")
  459. self.vppVoltage.setValue(voltage)
  460. interval = p.getfloat(sect, "inputPollInterval")
  461. self.inputPollInterval.setValue(interval)
  462. enabled = p.getboolean(sect, "inputPollEnabled")
  463. if enabled:
  464. self.inputPollEn.setCheckState(Qt.CheckState.Checked)
  465. else:
  466. self.inputPollEn.setCheckState(Qt.CheckState.Unchecked)
  467. div = p.getint(sect, "oscillatorDiv")
  468. self.oscDiv.setValue(div)
  469. mask = int(p.get(sect, "oscillatorMask"), 16)
  470. for i in range(0, self.oscPin.count()):
  471. if self.oscPin.itemData(i).obj == mask:
  472. break
  473. else:
  474. raise TOPException("Invalid oscillator mask")
  475. self.oscPin.setCurrentIndex(i)
  476. mask = int(p.get(sect, "zifOutEnMask"), 16)
  477. self.zifWidget.setOutEnMask(mask)
  478. mask = int(p.get(sect, "zifOutMask"), 16)
  479. self.zifWidget.setOutMask(mask)
  480. enabled = p.getboolean(sect, "fcntEnabled")
  481. if enabled:
  482. self.fcntEn.setCheckState(Qt.CheckState.Checked)
  483. else:
  484. self.fcntEn.setCheckState(Qt.CheckState.Unchecked)
  485. pos = p.getboolean(sect, "fcntPosEdge")
  486. if pos:
  487. self.fcntPosEdge.setCheckState(Qt.CheckState.Checked)
  488. else:
  489. self.fcntPosEdge.setCheckState(Qt.CheckState.Unchecked)
  490. pin = p.getint(sect, "fcntPin")
  491. if pin >= 0:
  492. idx = self.fcntPin.findData(pin)
  493. if idx < 0:
  494. raise TOPException("Invalid fcntPin")
  495. self.fcntPin.setCurrentIndex(idx)
  496. except (configparser.Error, TOPException, ValueError) as e:
  497. QMessageBox.critical(self, "Failed to load settings",
  498. "Failed to load settings: %s" % str(e))
  499. def loadSettings(self):
  500. (fn, selFltr) = QFileDialog.getOpenFileName(
  501. self, "Load settings", "",
  502. "Toprammer-unitest settings (*.tus);;"
  503. "All files (*)")
  504. if not fn:
  505. return
  506. self.__loadFile(fn)
  507. def __saveFile(self, filename):
  508. try:
  509. fd = open(filename, "w", encoding="UTF-8")
  510. fd.write("[TOPRAMMER-UNITEST-SETTINGS]\r\n")
  511. fd.write("fileVersion=%d\r\n" % 1)
  512. fd.write("programmerType=%s\r\n" % self.param_topType)
  513. idx = self.gndLayout.currentIndex()
  514. fd.write("gndLayout=%d\r\n" % self.gndLayout.itemData(idx))
  515. idx = self.vccLayout.currentIndex()
  516. fd.write("vccLayout=%d\r\n" % self.vccLayout.itemData(idx))
  517. fd.write("vccVoltage=%f\r\n" % self.vccVoltage.value())
  518. vppLayouts = ""
  519. for layId in list(self.vppLayouts.keys()):
  520. if self.vppLayouts[layId].checkState() == Qt.CheckState.Checked:
  521. if vppLayouts:
  522. vppLayouts += ","
  523. vppLayouts += str(layId)
  524. if not vppLayouts:
  525. vppLayouts = "0"
  526. fd.write("vppLayout=%s\r\n" % vppLayouts)
  527. fd.write("vppVoltage=%f\r\n" % self.vppVoltage.value())
  528. fd.write("inputPollEnabled=%d\r\n" % int(self.inputPollEn.checkState() == Qt.CheckState.Checked))
  529. fd.write("inputPollInterval=%f\r\n" % self.inputPollInterval.value())
  530. fd.write("fcntEnabled=%d\r\n" % (int(self.fcntEn.checkState() == Qt.CheckState.Checked)))
  531. fd.write("fcntPosEdge=%d\r\n" % (int(self.fcntPosEdge.checkState() == Qt.CheckState.Checked)))
  532. idx = self.fcntPin.currentIndex()
  533. pin = -1
  534. if idx >= 0:
  535. pin = self.fcntPin.itemData(idx)
  536. fd.write("fcntPin=%d\r\n" % pin)
  537. idx = self.oscPin.currentIndex()
  538. fd.write("oscillatorMask=%X\r\n" % self.oscPin.itemData(idx).obj)
  539. fd.write("oscillatorDiv=%d\r\n" % self.oscDiv.value())
  540. fd.write("zifOutEnMask=%X\r\n" % self.zifWidget.getOutEnMask())
  541. fd.write("zifOutMask=%X\r\n" % self.zifWidget.getOutMask())
  542. except (IOError, UnicodeError) as e:
  543. QMessageBox.critical(self, "Failed to save settings",
  544. "Failed to write settings to file: %s" % str(e))
  545. def saveSettings(self):
  546. (fn, selFltr) = QFileDialog.getSaveFileName(
  547. self, "Save settings", "",
  548. "Toprammer-unitest settings (*.tus)")
  549. if not fn:
  550. return
  551. if not fn.endswith(".tus"):
  552. fn += ".tus"
  553. self.__saveFile(fn)
  554. def closeEvent(self, e):
  555. self.inputPollTimer.stop()
  556. def queryTop(self, funcname, *parameters):
  557. self.inputPollBlocked += 1 # Avoid recursion
  558. (failed, returnValue) = self.mainWindow.runOperationSync(
  559. HwThread.TASK_GENERICTOPCALL,
  560. GenericTopCall(funcname, *parameters))
  561. self.inputPollBlocked -= 1
  562. if failed:
  563. raise TOPException("Failed to query TOP %s\n%s" % (funcname, str(returnValue)))
  564. return returnValue
  565. def getMode(self):
  566. return self.queryTop("top.getChip().getMode()")
  567. def shutdown(self):
  568. self.inputPollTimer.stop()
  569. def updateZifCheckboxes(self):
  570. mode = self.getMode()
  571. blockedPins = []
  572. # Basic mask
  573. activePinMask = self.queryTop("top.getChip().getActivePinMask()")
  574. for i in range(0, self.zifWidget.nrPins):
  575. if activePinMask & bit(i) == 0:
  576. blockedPins.append(i + 1)
  577. # GND
  578. idx = self.gndLayout.currentIndex()
  579. lay = self.gndLayout.itemData(idx)
  580. blockedPins.extend(self.queryTop("top.gnd.ID2pinlist(...)", lay))
  581. # VCC
  582. idx = self.vccLayout.currentIndex()
  583. lay = self.vccLayout.itemData(idx)
  584. blockedPins.extend(self.queryTop("top.vcc.ID2pinlist(...)", lay))
  585. # VPP
  586. for key in list(self.vppLayouts.keys()):
  587. if self.vppLayouts[key].checkState() == Qt.CheckState.Checked:
  588. blockedPins.extend(self.queryTop("top.vpp.ID2pinlist(...)", key))
  589. # OSC
  590. idx = self.oscPin.currentIndex()
  591. mask = self.oscPin.itemData(idx).obj
  592. for i in range(0, self.zifWidget.nrPins):
  593. if mask & bit(i):
  594. blockedPins.append(i + 1)
  595. # Freq counter
  596. if mode == Chip_Unitest.MODE_FCNT:
  597. idx = self.fcntPin.currentIndex()
  598. pinNumber = self.fcntPin.itemData(idx)
  599. blockedPins.append(pinNumber + 1)
  600. # Set blocked pins in ZIF widget
  601. self.zifWidget.setBlockedPins(tuple(set(blockedPins)))
  602. def gndLayoutChanged(self, unused=None):
  603. if self.uiChangeBlocked:
  604. return
  605. idx = self.gndLayout.currentIndex()
  606. selLayout = self.gndLayout.itemData(idx)
  607. try:
  608. self.queryTop("top.getChip().setGND(...)", selLayout)
  609. except (TOPException) as e:
  610. QMessageBox.critical(self, "TOP communication failed",
  611. "Failed to set GND layout:\n" +\
  612. str(e))
  613. return
  614. self.updateZifCheckboxes()
  615. def vccLayoutChanged(self, unused=None):
  616. if self.uiChangeBlocked:
  617. return
  618. selVoltage = self.vccVoltage.value()
  619. idx = self.vccLayout.currentIndex()
  620. selLayout = self.vccLayout.itemData(idx)
  621. try:
  622. self.queryTop("top.getChip().setVCC(...)", selVoltage, selLayout)
  623. except (TOPException) as e:
  624. QMessageBox.critical(self, "TOP communication failed",
  625. "Failed to set VCC layout:\n" +\
  626. str(e))
  627. return
  628. self.updateZifCheckboxes()
  629. def vppLayoutChanged(self, unused=None):
  630. if self.uiChangeBlocked:
  631. return
  632. selVoltage = self.vppVoltage.value()
  633. selLayouts = []
  634. for key in list(self.vppLayouts.keys()):
  635. if self.vppLayouts[key].checkState() == Qt.CheckState.Checked:
  636. selLayouts.append(key)
  637. try:
  638. self.queryTop("top.getChip().setVPP(...)", selVoltage, selLayouts)
  639. except (TOPException) as e:
  640. QMessageBox.critical(self, "TOP communication failed",
  641. "Failed to set VPP layout:\n" +\
  642. str(e))
  643. return
  644. self.updateZifCheckboxes()
  645. def doInputPollTimer(self):
  646. if self.inputPollBlocked:
  647. # Blocked. Reschedule.
  648. self.inputPollTimer.start(1)
  649. return
  650. ok = self.zifWidget.readInputs()
  651. if ok:
  652. if self.fcntEn.checkState() == Qt.CheckState.Checked:
  653. ok = self.__readFcntValue()
  654. if ok:
  655. self.__updateFcntValueLabel()
  656. if not ok:
  657. # Whoops, error. Disable input polling
  658. self.inputPollEn.setCheckState(Qt.CheckState.Unchecked)
  659. return False
  660. # Reschedule
  661. inter = int(self.inputPollInterval.value() * 1000)
  662. self.inputPollTimer.start(inter)
  663. return True
  664. def inputPollChanged(self, unused=None):
  665. if self.uiChangeBlocked:
  666. return
  667. if self.inputPollEn.checkState() == Qt.CheckState.Checked:
  668. self.inputPollInterval.setEnabled(True)
  669. if self.doInputPollTimer():
  670. inter = int(self.inputPollInterval.value() * 1000)
  671. self.inputPollTimer.start(inter)
  672. else:
  673. self.inputPollInterval.setEnabled(False)
  674. self.inputPollTimer.stop()
  675. def fcntChanged(self, unused=None):
  676. if self.uiChangeBlocked:
  677. return
  678. try:
  679. if self.fcntEn.checkState() == Qt.CheckState.Checked:
  680. self.switchMode(Chip_Unitest.MODE_FCNT)
  681. idx = self.fcntPin.currentIndex()
  682. pinNumber = self.fcntPin.itemData(idx)
  683. inv = (self.fcntPosEdge.checkState() != Qt.CheckState.Checked)
  684. self.queryTop("top.getChip().setFreqCountPin(...)",
  685. pinNumber, inv)
  686. self.__readFcntValue()
  687. self.__updateFcntValueLabel()
  688. # Force enable input polling
  689. self.inputPollEn.setCheckState(Qt.CheckState.Checked)
  690. self.inputPollEn.setEnabled(False)
  691. else:
  692. self.switchMode(Chip_Unitest.MODE_UNITEST)
  693. self.__updateFcntValueLabel()
  694. self.inputPollEn.setEnabled(True)
  695. except (TOPException) as e:
  696. QMessageBox.critical(self, "TOP communication failed",
  697. "Failed configure frequency counter:\n" +\
  698. str(e))
  699. return
  700. self.updateZifCheckboxes()
  701. def oscChanged(self, unused=None):
  702. if self.uiChangeBlocked:
  703. return
  704. try:
  705. div = self.oscDiv.value()
  706. self.queryTop("top.getChip().setOscDivider(...)", div)
  707. idx = self.oscPin.currentIndex()
  708. mask = self.oscPin.itemData(idx).obj
  709. self.queryTop("top.getChip().setOscMask(...)", mask)
  710. except (TOPException) as e:
  711. QMessageBox.critical(self, "TOP communication failed",
  712. "Failed configure oscillator:\n" +\
  713. str(e))
  714. return
  715. self.oscFreq.setText("%.03f Hz" % (float(self.param_oscFreq) / div))
  716. self.updateZifCheckboxes()
  717. class GuiUserInterface(AbstractUserInterface):
  718. # Global progress
  719. PROGRESSMETER_USER_GLOBAL = AbstractUserInterface.PROGRESSMETER_USER + 0
  720. def __init__(self, hwThread):
  721. self.hwThread = hwThread
  722. def progressMeterInit(self, meterId, message, nrSteps):
  723. self.hwThread.appendMessage("progrInit", (meterId, message, nrSteps))
  724. def progressMeterFinish(self, meterId):
  725. self.hwThread.appendMessage("progrFinish", meterId)
  726. def progressMeter(self, meterId, step):
  727. self.hwThread.appendMessage("progress", (meterId, step))
  728. def __consoleMessage(self, message):
  729. self.hwThread.appendMessage("console", message + "\n")
  730. def warningMessage(self, message):
  731. self.__consoleMessage("WARNING: " + message)
  732. def infoMessage(self, message):
  733. self.__consoleMessage(message)
  734. def debugMessage(self, message):
  735. self.__consoleMessage(message)
  736. class GenericTopCall(object):
  737. "One call to 'class Top'. Used by TASK_GENERICTOPCALL"
  738. def __init__(self, methodName, *parameters, **userData):
  739. self.methodName = methodName
  740. self.parameters = parameters
  741. self.result = None
  742. self.userData = userData
  743. class HwThread(QThread):
  744. TASK_SHUTDOWN = 0
  745. TASK_INITCHIP = 1
  746. TASK_GENERICTOPCALL = 2
  747. class CancelException(Exception):
  748. def __init__(self):
  749. Exception.__init__(self, "Operation cancelled")
  750. def __init__(self, mainWindow):
  751. QThread.__init__(self, mainWindow)
  752. self.mainWindow = mainWindow
  753. self.killRequest = False
  754. self.messageQueue = []
  755. self.top = None
  756. self.task = None
  757. self.opaqueId = None
  758. self.taskParameter = None
  759. self.cancel = False
  760. self.cancellationBlocked = 0
  761. self.waitCondition = QWaitCondition()
  762. self.mutex = QMutex()
  763. self.setTopParameters()
  764. self.setDevice()
  765. self.start()
  766. def killThread(self):
  767. if self.isRunning():
  768. self.mutex.lock()
  769. self.task = self.TASK_SHUTDOWN
  770. self.killRequest = True
  771. self.waitCondition.wakeAll()
  772. self.mutex.unlock()
  773. self.wait()
  774. def setDevice(self, devIdentifier=None):
  775. assert(self.top is None and self.task is None)
  776. self.param_devIdentifier = devIdentifier
  777. def setTopParameters(self, verbose=2, forceLevel=0,
  778. usebroken=True, forceBitfileUpload=False):
  779. assert(self.top is None and self.task is None)
  780. self.param_verbose = verbose
  781. self.param_forceLevel = forceLevel
  782. self.param_usebroken = usebroken
  783. self.param_forceBitfileUpload = forceBitfileUpload
  784. def triggerTask(self, task, taskParameter=None,
  785. opaqueId=None, mutexIsLocked=False):
  786. if not mutexIsLocked:
  787. self.mutex.lock()
  788. self.cancel = False
  789. self.task = task
  790. self.opaqueId = opaqueId
  791. self.taskParameter = taskParameter
  792. self.waitCondition.wakeAll()
  793. if not mutexIsLocked:
  794. self.mutex.unlock()
  795. def run(self):
  796. self.mutex.lock()
  797. while True:
  798. if not self.killRequest and self.task is None:
  799. self.waitCondition.wait(self.mutex)
  800. self.mutex.unlock()
  801. self.__taskWorker()
  802. self.mutex.lock()
  803. if self.killRequest and self.task is None:
  804. break
  805. self.mutex.unlock()
  806. def cancelTask(self):
  807. self.mutex.lock()
  808. self.cancel = True
  809. self.mutex.unlock()
  810. def __blockCancellation(self):
  811. self.cancellationBlocked += 1
  812. def __unblockCancellation(self):
  813. self.cancellationBlocked -= 1
  814. def __cancellationPoint(self):
  815. # Mutex must be locked
  816. if not self.cancel:
  817. return
  818. if self.cancellationBlocked:
  819. return
  820. self.cancel = False
  821. self.mutex.unlock()
  822. raise HwThread.CancelException() # Caught in __taskWorker
  823. def __doCancelTask(self):
  824. # Make sure the device is in a consistent state.
  825. self.__blockCancellation()
  826. if self.top:
  827. print("Operation cancelled. Resetting chip.")
  828. self.top.resetChip()
  829. self.__unblockCancellation()
  830. def appendMessage(self, message, data, nocancel=False):
  831. # Append a message to the message queue.
  832. # This is a cancellation point!
  833. self.mutex.lock()
  834. if not nocancel:
  835. self.__cancellationPoint()
  836. self.messageQueue.append( (message, data) )
  837. self.mutex.unlock()
  838. self.__notifyMainWindow()
  839. def __taskWorker(self):
  840. failed = True
  841. try:
  842. self.cancellationBlocked = 0
  843. result = self.__runTask(self.task)
  844. failed = False
  845. except (TOPException) as e:
  846. result = e
  847. except (HwThread.CancelException) as e:
  848. self.__doCancelTask()
  849. result = e
  850. except (Exception) as e:
  851. result = e
  852. self.appendMessage("finished", (self.opaqueId, failed, result),
  853. nocancel=True)
  854. self.task = None
  855. self.taskParameter = None
  856. self.opaqueId = None
  857. def __runTask(self, task):
  858. retval = None
  859. if task == self.TASK_SHUTDOWN or task == self.TASK_INITCHIP:
  860. if self.top:
  861. self.__blockCancellation()
  862. self.top.shutdownChip()
  863. self.top.shutdownProgrammer()
  864. self.top = None
  865. self.__unblockCancellation()
  866. if task == self.TASK_SHUTDOWN:
  867. return None
  868. if not self.top:
  869. # Initialize Hardware access
  870. self.__blockCancellation()
  871. self.top = TOP(devIdentifier = self.param_devIdentifier,
  872. verbose = self.param_verbose,
  873. forceLevel = self.param_forceLevel,
  874. usebroken = self.param_usebroken,
  875. forceBitfileUpload = self.param_forceBitfileUpload,
  876. userInterface = GuiUserInterface(self))
  877. self.__unblockCancellation()
  878. if task == self.TASK_INITCHIP:
  879. self.__blockCancellation()
  880. self.top.initializeChip(self.taskParameter)
  881. chip = self.top.getChip()
  882. asciiArtLayout = None
  883. layoutGen = chip.getLayoutGenerator()
  884. if layoutGen:
  885. asciiArtLayout = layoutGen.zifLayoutAsciiArt()
  886. self.__unblockCancellation()
  887. retval = (chip, asciiArtLayout)
  888. elif task == self.TASK_GENERICTOPCALL:
  889. # The parameter is one GenericTopCall
  890. # or a list of calls.
  891. topCallList = self.taskParameter
  892. if not isinstance(topCallList, list) and\
  893. not isinstance(topCallList, tuple):
  894. # It is only a single call. Make an iterable.
  895. topCallList = (topCallList, )
  896. self.__globalProgressInit(len(topCallList))
  897. # Iterate over the list of GenericTopCalls
  898. for topCallIndex, topCall in enumerate(topCallList):
  899. method = topCall.methodName.strip()
  900. if method.endswith(")"): # strip (...) suffix
  901. method = method[:method.rfind("(")]
  902. if method.startswith("top."): # strip 'top.' prefix
  903. method = method[4:]
  904. paramList = []
  905. for i in range(len(topCall.parameters)):
  906. paramList.append("topCall.parameters[%d]" % i)
  907. result = eval("self.top.%s(%s)" % (method, ", ".join(paramList)))
  908. topCall.result = result
  909. self.__globalProgress(topCallIndex + 1)
  910. # If there was only one call, return the result.
  911. # Otherwise return the complete call-list including results.
  912. if len(topCallList) == 1:
  913. retval = topCallList[0].result
  914. else:
  915. retval = topCallList
  916. else:
  917. raise TOPException("INTERNAL ERROR: HwThread: unknown task")
  918. return retval
  919. def __globalProgressInit(self, nrSteps):
  920. self.appendMessage("progrInit",
  921. (GuiUserInterface.PROGRESSMETER_USER_GLOBAL,
  922. None, nrSteps))
  923. def __globalProgress(self, step):
  924. self.appendMessage("progress",
  925. (GuiUserInterface.PROGRESSMETER_USER_GLOBAL,
  926. step))
  927. return step
  928. def __notifyMainWindow(self):
  929. QApplication.postEvent(self.mainWindow, QEvent(EVENT_HWTHREAD))
  930. def handleMessageQueue(self):
  931. self.mutex.lock()
  932. for (message, data) in self.messageQueue:
  933. if message == "finished":
  934. self.mainWindow.hardwareTaskFinished(
  935. opaqueId=data[0], failed=data[1], returnValue=data[2])
  936. elif message == "console":
  937. self.mainWindow.console.showMessage(data)
  938. elif message == "progrInit":
  939. (meterId, message, nrSteps) = data
  940. self.mainWindow.console.progressMeterInit(meterId, nrSteps)
  941. elif message == "progrFinish":
  942. meterId = data
  943. self.mainWindow.console.progressMeter(meterId, -1)
  944. elif message == "progress":
  945. (meterId, step) = data
  946. self.mainWindow.console.progressMeter(meterId, step)
  947. else:
  948. assert(0)
  949. self.messageQueue = []
  950. self.mutex.unlock()
  951. class Console(QDockWidget):
  952. STAT_OK = 0
  953. STAT_ERROR = 1
  954. STAT_PROGRESS = 2
  955. def __init__(self, mainWindow):
  956. QDockWidget.__init__(self, "Console", mainWindow)
  957. self.addPrefix = True
  958. self.statusUpdateBlocked = 0
  959. self.setFeatures(self.DockWidgetFeature.DockWidgetMovable |
  960. self.DockWidgetFeature.DockWidgetFloatable)
  961. self.setWidget(QWidget(self))
  962. self.widget().show()
  963. self.widget().setLayout(QGridLayout(self.widget()))
  964. self.consoleMsgs = []
  965. self.consoleText = QTextEdit(self)
  966. self.consoleText.setReadOnly(True)
  967. self.widget().layout().addWidget(self.consoleText, 0, 0, 1, 3)
  968. self.statusLabel = QLabel(self)
  969. self.widget().layout().addWidget(self.statusLabel, 1, 0, 2, 1)
  970. self.chipaccessProgress = QProgressBar(self)
  971. self.widget().layout().addWidget(self.chipaccessProgress, 1, 1)
  972. self.globalProgress = QProgressBar(self)
  973. self.widget().layout().addWidget(self.globalProgress, 2, 1)
  974. self.cancelButton = QPushButton("Cancel", self)
  975. self.cancelButton.setEnabled(False)
  976. self.widget().layout().addWidget(self.cancelButton, 1, 2, 2, 1)
  977. self.setStatus(self.STAT_OK)
  978. self.idToProgressBar = {
  979. GuiUserInterface.PROGRESSMETER_CHIPACCESS : self.chipaccessProgress,
  980. GuiUserInterface.PROGRESSMETER_USER_GLOBAL : self.globalProgress,
  981. }
  982. self.cancelButton.released.connect(mainWindow.cancelHardwareTask)
  983. def blockStatusUpdate(self):
  984. self.statusUpdateBlocked += 1
  985. def unblockStatusUpdate(self):
  986. self.statusUpdateBlocked -= 1
  987. def setTaskRunning(self, running, success=True):
  988. if self.statusUpdateBlocked:
  989. return
  990. self.cancelButton.setEnabled(running)
  991. if running:
  992. self.setStatus(Console.STAT_PROGRESS)
  993. # Set progress meters to "busy"
  994. for meterId in list(self.idToProgressBar.keys()):
  995. self.progressMeterInit(meterId, 0)
  996. else:
  997. if success:
  998. self.setStatus(Console.STAT_OK)
  999. else:
  1000. self.setStatus(Console.STAT_ERROR)
  1001. # Reset progress meters to 0%
  1002. for meterId in list(self.idToProgressBar.keys()):
  1003. self.progressMeterInit(meterId, 2)
  1004. def setStatus(self, status):
  1005. if self.statusUpdateBlocked:
  1006. return
  1007. if status == self.STAT_OK:
  1008. path = getIconPath("ok")
  1009. elif status == self.STAT_ERROR:
  1010. path = getIconPath("error")
  1011. elif status == self.STAT_PROGRESS:
  1012. path = getIconPath("progress")
  1013. else:
  1014. assert(0)
  1015. self.statusLabel.setPixmap(QPixmap(path))
  1016. def __commitText(self):
  1017. limit = 100
  1018. if len(self.consoleMsgs) > limit:
  1019. self.consoleMsgs.pop(0)
  1020. assert(len(self.consoleMsgs) == limit)
  1021. html = "<HTML><BODY>" + "".join(self.consoleMsgs) + "</BODY></HTML>"
  1022. self.consoleText.setHtml(html)
  1023. # Scroll to end
  1024. scroll = self.consoleText.verticalScrollBar()
  1025. scroll.setTracking(True)
  1026. scroll.setSliderPosition(scroll.maximum())
  1027. scroll.setValue(scroll.maximum())
  1028. def showMessage(self, message, bold=False):
  1029. message = str(message)
  1030. if not message:
  1031. return
  1032. newline = False
  1033. if message.endswith("\n"):
  1034. newline = True
  1035. message = message[:-1]
  1036. message = htmlEscape(message)
  1037. if bold:
  1038. message = "<B>" + message + "</B>"
  1039. if self.addPrefix:
  1040. time = str(QTime.currentTime().toString("hh:mm:ss"))
  1041. message = "<I>[%s]</I>&nbsp;&nbsp;%s" % (time, message)
  1042. self.addPrefix = newline
  1043. if newline:
  1044. message += "<BR />"
  1045. lastmsg = None
  1046. if self.consoleMsgs:
  1047. lastmsg = self.consoleMsgs[-1]
  1048. if lastmsg and not lastmsg.endswith("<BR />"):
  1049. self.consoleMsgs[-1] = self.consoleMsgs[-1] + message
  1050. else:
  1051. self.consoleMsgs.append(message)
  1052. self.__commitText()
  1053. def progressMeterInit(self, meterId, nrSteps):
  1054. if self.statusUpdateBlocked:
  1055. return
  1056. progress = self.idToProgressBar[meterId]
  1057. progress.setMinimum(0)
  1058. progress.setMaximum(max(0, nrSteps - 1))
  1059. progress.setValue(progress.maximum())
  1060. progress.setValue(0)
  1061. def progressMeter(self, meterId, step):
  1062. if self.statusUpdateBlocked:
  1063. return
  1064. progress = self.idToProgressBar[meterId]
  1065. if progress.maximum() == 0:
  1066. progress.setMaximum(1)
  1067. progress.setValue(1)
  1068. else:
  1069. progress.setValue(step)
  1070. class HexEditWidget(QWidget):
  1071. def __init__(self, scrollArea, parent=None):
  1072. QWidget.__init__(self, parent)
  1073. self.scrollArea = scrollArea
  1074. self.bgColor = QColor("#FFFFFF")
  1075. self.fgColor = QColor("#000000")
  1076. self.cursor0Color = QColor("#A0A07F")
  1077. self.cursor1Color = QColor("#D0D0AF")
  1078. font = self.font()
  1079. font.setFamily("Courier")
  1080. font.setStyleHint(QFont.StyleHint.Courier)
  1081. font.setWeight(QFont.Weight.Normal)
  1082. font.setPointSize(10)
  1083. font.setBold(False)
  1084. font.setFixedPitch(True)
  1085. self.setFont(font)
  1086. self.charWidth = self.fontMetrics().boundingRect("X" * 100).width() / 100
  1087. self.charHeight = self.fontMetrics().height()
  1088. self.bytesPerLine = 16
  1089. self.previousData = b""
  1090. self.setData(b"")
  1091. self.__setCursor(0, False)
  1092. self.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
  1093. def updateText(self):
  1094. lines = []
  1095. if self.data:
  1096. for i in range(0, len(self.data), self.bytesPerLine):
  1097. end = min(i + self.bytesPerLine, len(self.data))
  1098. data = self.data[i : end]
  1099. try:
  1100. prevData = self.previousData[i : end]
  1101. except IndexError:
  1102. prevData = None
  1103. if prevData == data:
  1104. lines.append(self.textLines[i // self.bytesPerLine])
  1105. else:
  1106. text = [ "[%08X]: " % i ]
  1107. for byte in data:
  1108. text.append(" " + byte2hex(byte))
  1109. if len(data) % self.bytesPerLine:
  1110. # padding
  1111. nrLeft = self.bytesPerLine - (len(data) % self.bytesPerLine)
  1112. text.append(" " * nrLeft)
  1113. text.append(" |")
  1114. text.append(bytes2ascii(data))
  1115. text.append("|")
  1116. lines.append("".join(text))
  1117. else:
  1118. lines = [ "<The buffer is empty>", ]
  1119. self.textLines = lines
  1120. self.nrLines = len(lines)
  1121. self.previousData = self.getData()
  1122. width = self.fontMetrics().boundingRect(self.textLines[0]).width()
  1123. height = self.nrLines * self.charHeight
  1124. self.resize(width + 20, height + 20)
  1125. self.repaint()
  1126. def getData(self):
  1127. return bytes(self.data)
  1128. def setData(self, data):
  1129. if not data:
  1130. data = b""
  1131. assert isinstance(data, (bytes, bytearray))
  1132. self.data = bytearray(data)
  1133. self.updateText()
  1134. self.__setCursor(0, False)
  1135. def charInput(self, char):
  1136. if not self.data:
  1137. return
  1138. assert(self.cursorByte < len(self.data))
  1139. if self.cursorOnAscii:
  1140. self.data[self.cursorByte] = ord(char)
  1141. self.__setCursor(self.cursorByte + 1, True)
  1142. else:
  1143. dataByte = self.data[self.cursorByte]
  1144. char = ord(char.upper())
  1145. if char >= ord("0") and char <= ord("9"):
  1146. nibble = char - ord("0")
  1147. elif char >= ord("A") and char <= ord("F"):
  1148. nibble = char - ord("A") + 10
  1149. else:
  1150. return
  1151. if self.cursorNibble == 0:
  1152. dataByte = (dataByte & 0xF0) | nibble
  1153. else:
  1154. dataByte = (dataByte & 0x0F) | (nibble << 4)
  1155. self.data[self.cursorByte] = dataByte
  1156. if self.cursorNibble == 1:
  1157. self.cursorNibble = 0
  1158. else:
  1159. self.__setCursor(self.cursorByte + 1, False)
  1160. self.updateText()
  1161. def keyPressEvent(self, e):
  1162. linesOnScreen = self.scrollArea.viewport().height() // self.charHeight
  1163. if e.matches(QKeySequence.StandardKey.Delete) or\
  1164. e.key() == Qt.Key.Key_Backspace:
  1165. self.__setCursor(self.cursorByte - 1, self.cursorOnAscii)
  1166. return
  1167. if e.matches(QKeySequence.StandardKey.MoveToNextChar):
  1168. self.__setCursor(self.cursorByte + 1, self.cursorOnAscii)
  1169. return
  1170. if e.matches(QKeySequence.StandardKey.MoveToPreviousChar):
  1171. self.__setCursor(self.cursorByte - 1, self.cursorOnAscii)
  1172. return
  1173. if e.matches(QKeySequence.StandardKey.MoveToStartOfLine):
  1174. self.__setCursor(self.cursorByte - self.cursorByte % self.bytesPerLine,
  1175. self.cursorOnAscii)
  1176. return
  1177. if e.matches(QKeySequence.StandardKey.MoveToEndOfLine):
  1178. self.__setCursor(self.cursorByte - self.cursorByte % self.bytesPerLine
  1179. + self.bytesPerLine - 1,
  1180. self.cursorOnAscii)
  1181. return
  1182. if e.matches(QKeySequence.StandardKey.MoveToNextLine):
  1183. self.__setCursor(self.cursorByte + self.bytesPerLine, self.cursorOnAscii)
  1184. return
  1185. if e.matches(QKeySequence.StandardKey.MoveToPreviousLine):
  1186. self.__setCursor(self.cursorByte - self.bytesPerLine, self.cursorOnAscii)
  1187. return
  1188. if e.matches(QKeySequence.StandardKey.MoveToStartOfDocument):
  1189. self.__setCursor(0, self.cursorOnAscii)
  1190. return
  1191. if e.matches(QKeySequence.StandardKey.MoveToEndOfDocument):
  1192. self.__setCursor(len(self.data) - 1, self.cursorOnAscii)
  1193. return
  1194. if e.matches(QKeySequence.StandardKey.MoveToNextPage):
  1195. self.__setCursor(self.cursorByte + int(linesOnScreen * 0.8) * self.bytesPerLine,
  1196. self.cursorOnAscii)
  1197. return
  1198. if e.matches(QKeySequence.StandardKey.MoveToPreviousPage):
  1199. self.__setCursor(self.cursorByte - int(linesOnScreen * 0.8) * self.bytesPerLine,
  1200. self.cursorOnAscii)
  1201. return
  1202. text = str(e.text())
  1203. if text:
  1204. key = ord(text[0])
  1205. if self.cursorOnAscii:
  1206. self.charInput(chr(key))
  1207. else:
  1208. if (key >= ord("0") and key <= ord("9")) or\
  1209. (key >= ord("a") and key <= ord("f")) or\
  1210. (key >= ord("A") and key <= ord("F")):
  1211. self.charInput(chr(key))
  1212. def __setCursor(self, byteNr, asciiArea):
  1213. byteNr = min(byteNr, len(self.data) - 1)
  1214. byteNr = max(byteNr, 0)
  1215. self.cursorOnAscii = asciiArea
  1216. self.cursorByte = byteNr
  1217. self.cursorNibble = 1
  1218. self.repaint()
  1219. (x, y) = self.__cursorUpperLeftEdge()
  1220. self.scrollArea.ensureVisible(x, y)
  1221. def mousePressEvent(self, e):
  1222. x = e.pos().x()
  1223. y = e.pos().y()
  1224. byteAreaX = round(self.charWidth * 13)
  1225. byteAreaY = 3
  1226. asciiAreaX = round(self.charWidth * (13 + 3 * self.bytesPerLine + 3 - 1))
  1227. asciiAreaY = 3
  1228. if x >= byteAreaX and\
  1229. y >= byteAreaY and\
  1230. x <= round(byteAreaX + self.charWidth * 3 * self.bytesPerLine) and\
  1231. y <= byteAreaY + self.charHeight * self.nrLines:
  1232. # Click in byte area.
  1233. x -= byteAreaX
  1234. y -= byteAreaY
  1235. line = y // self.charHeight
  1236. column = round(x // (self.charWidth * 3))
  1237. self.__setCursor(line * self.bytesPerLine + column,
  1238. False)
  1239. return
  1240. if x >= asciiAreaX and\
  1241. y >= asciiAreaY and\
  1242. x <= round(asciiAreaX + self.charWidth * self.bytesPerLine) and\
  1243. y <= asciiAreaY + self.charHeight * self.nrLines:
  1244. # Click on ascii area.
  1245. x -= asciiAreaX
  1246. y -= asciiAreaY
  1247. line = y // self.charHeight
  1248. column = round(x // self.charWidth)
  1249. self.__setCursor(line * self.bytesPerLine + column,
  1250. True)
  1251. return
  1252. def __cursorUpperLeftEdge(self):
  1253. if self.cursorOnAscii:
  1254. x = self.charWidth * (13 + 3 * self.bytesPerLine + 3 - 1)
  1255. x += self.charWidth * (self.cursorByte % self.bytesPerLine)
  1256. y = self.charHeight * (self.cursorByte // self.bytesPerLine)
  1257. y += 3
  1258. else:
  1259. x = self.charWidth * (13 + 3 * (self.cursorByte % self.bytesPerLine))
  1260. y = self.charHeight * (self.cursorByte // self.bytesPerLine)
  1261. y += 3
  1262. return (round(x), round(y))
  1263. def paintEvent(self, e):
  1264. p = QPainter(self)
  1265. # Background
  1266. p.fillRect(0, 0, self.width(), self.height(), self.bgColor)
  1267. # Cursor
  1268. if self.data and self.hasFocus():
  1269. (x, y) = self.__cursorUpperLeftEdge()
  1270. if self.cursorOnAscii:
  1271. p.fillRect(x, y, round(self.charWidth), self.charHeight,
  1272. self.cursor0Color)
  1273. else:
  1274. p.fillRect(x, y, round(self.charWidth * 2), self.charHeight,
  1275. self.cursor1Color)
  1276. if self.cursorNibble == 0:
  1277. x = round(x + self.charWidth)
  1278. p.fillRect(x, y, round(self.charWidth), self.charHeight,
  1279. self.cursor0Color)
  1280. # Text
  1281. p.setPen(QPen(self.fgColor))
  1282. y = self.charHeight
  1283. for line in self.textLines:
  1284. p.drawText(0, y, line)
  1285. y += self.charHeight
  1286. class HexEdit(QScrollArea):
  1287. def __init__(self, parent=None):
  1288. QScrollArea.__init__(self, parent)
  1289. self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn)
  1290. self.setWidgetResizable(False)
  1291. self.setWidget(HexEditWidget(self, self))
  1292. def setData(self, data):
  1293. self.widget().setData(data)
  1294. def getData(self):
  1295. return self.widget().getData()
  1296. class BufferWidget(QWidget):
  1297. def __init__(self, mainWindow, name):
  1298. QWidget.__init__(self, mainWindow)
  1299. self.mainWindow = mainWindow
  1300. self.name = name
  1301. self.hide()
  1302. self.setLayout(QGridLayout(self))
  1303. self.setAvailable(False)
  1304. self.setReadOnly()
  1305. def getName(self):
  1306. return self.name
  1307. def setAvailable(self, available):
  1308. self.available = available
  1309. def isAvailable(self):
  1310. return self.available
  1311. def setReadOnly(self, readOnly=True):
  1312. self.readOnly = readOnly
  1313. def isReadOnly(self):
  1314. return self.readOnly
  1315. def getRawData(self):
  1316. return None
  1317. def setRawData(self, data):
  1318. return False
  1319. def setDataWithIHexInterpreter(self, interp, readRaw):
  1320. return False
  1321. def clear(self):
  1322. pass
  1323. class InfoBufferWidget(BufferWidget):
  1324. def __init__(self, mainWindow, name):
  1325. BufferWidget.__init__(self, mainWindow, name)
  1326. self.zifLayout = QLabel(self)
  1327. font = self.zifLayout.font()
  1328. font.setFixedPitch(True)
  1329. font.setFamily("monospace")
  1330. font.setPointSize(7)
  1331. self.zifLayout.setFont(font)
  1332. self.layout().addWidget(self.zifLayout, 0, 0, 6, 1)
  1333. def addLabel(name, y):
  1334. l = QLabel(name, self)
  1335. l.setAlignment(Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft)
  1336. self.layout().addWidget(l, y, 1)
  1337. label = QLabel(self)
  1338. label.setAlignment(Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft)
  1339. font = label.font()
  1340. font.setBold(True)
  1341. label.setFont(font)
  1342. self.layout().addWidget(label, y, 2)
  1343. return label
  1344. self.chipName = addLabel("Chip name:", 0)
  1345. self.comment = addLabel("Comment:", 1)
  1346. self.packages = addLabel("Packages:", 2)
  1347. self.chipSig = addLabel("Chip signature bytes:", 3)
  1348. self.maintainer = addLabel("Chip maintainer:", 4)
  1349. self.layout().setColumnStretch(3, 99)
  1350. self.layout().setRowStretch(5, 99)
  1351. self.clear()
  1352. def clear(self):
  1353. self.chipName.setText("<No chip selected>")
  1354. self.setComment(None)
  1355. self.setChipSignature(None)
  1356. self.setChipLayout(None)
  1357. self.setPackages(None)
  1358. self.setMaintainer(None)
  1359. def setChipSignature(self, bindata):
  1360. if bindata:
  1361. self.chipSig.setText(bytes2hex(bindata))
  1362. else:
  1363. self.chipSig.setText("None")
  1364. def setComment(self, comment):
  1365. if comment:
  1366. self.comment.setText(comment)
  1367. else:
  1368. self.comment.setText("None")
  1369. def setPackages(self, packages):
  1370. def p2str(param):
  1371. (packageName, description) = param
  1372. text = packageName
  1373. if description:
  1374. text += " (%s)" % description
  1375. return text
  1376. if packages:
  1377. pckgs = list(map(p2str, packages))
  1378. self.packages.setText("\n".join(pckgs))
  1379. else:
  1380. self.packages.setText("Unknown")
  1381. def setMaintainer(self, maintainer):
  1382. if maintainer:
  1383. # Remove email address
  1384. idx = maintainer.find("<")
  1385. if idx >= 0:
  1386. maintainer = maintainer[:idx]
  1387. self.maintainer.setText(maintainer.strip())
  1388. else:
  1389. self.maintainer.setText("None")
  1390. def setupDescription(self, chipDescription):
  1391. self.chipName.setText(chipDescription.description)
  1392. self.setComment(chipDescription.comment)
  1393. self.setPackages(chipDescription.packages)
  1394. self.setMaintainer(chipDescription.maintainer)
  1395. def setChipLayout(self, asciiArtLayout):
  1396. if asciiArtLayout:
  1397. self.zifLayout.setText(asciiArtLayout)
  1398. self.zifLayout.setFrameShape(QFrame.Shape.StyledPanel)
  1399. else:
  1400. self.zifLayout.clear()
  1401. self.zifLayout.setFrameShape(QFrame.Shape.NoFrame)
  1402. class ImageBufferWidget(BufferWidget):
  1403. def __init__(self, mainWindow, name):
  1404. BufferWidget.__init__(self, mainWindow, name)
  1405. self.browser = HexEdit(self)
  1406. self.layout().addWidget(self.browser, 0, 0)
  1407. def loadImage(self, image):
  1408. self.browser.setData(image)
  1409. def getRawData(self):
  1410. return self.browser.getData()
  1411. def setRawData(self, data):
  1412. if self.isReadOnly():
  1413. return False
  1414. self.loadImage(data)
  1415. return True
  1416. def clear(self):
  1417. self.loadImage(None)
  1418. class BitBufferWidget(BufferWidget):
  1419. def __init__(self, mainWindow, name, bitDescriptionsAttr):
  1420. BufferWidget.__init__(self, mainWindow, name)
  1421. self.bitDescriptionsAttr = bitDescriptionsAttr
  1422. self.bitsAreaScroll = QScrollArea(self)
  1423. self.layout().addWidget(self.bitsAreaScroll, 0, 0)
  1424. self.bitsAreaScroll.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn)
  1425. self.bitsAreaScroll.setWidgetResizable(False)
  1426. self.bitsArea = None
  1427. self.image = None
  1428. self.checkboxes = []
  1429. def __getBitDescription(self, bitNr):
  1430. if not self.mainWindow.currentChipDescription:
  1431. return ""
  1432. bitDescList = getattr(self.mainWindow.currentChipDescription,
  1433. self.bitDescriptionsAttr, None)
  1434. if not bitDescList:
  1435. return ""
  1436. bitDesc = [x for x in bitDescList if x.bitNr == bitNr]
  1437. if not bitDesc:
  1438. return ""
  1439. return bitDesc[0].description
  1440. def loadImage(self, image):
  1441. self.image = image
  1442. for checkbox in self.checkboxes:
  1443. checkbox.deleteLater()
  1444. self.checkboxes = []
  1445. if self.bitsArea:
  1446. self.bitsArea.deleteLater()
  1447. self.bitsArea = None
  1448. if not self.image:
  1449. return
  1450. self.bitsArea = QWidget(self)
  1451. self.bitsArea.setLayout(QGridLayout(self.bitsArea))
  1452. self.bitsArea.layout().addWidget(QLabel("Currently set %s:" % self.getName(),
  1453. self.bitsArea),
  1454. 0, 0)
  1455. self.hexView = QLabel(self.bitsArea)
  1456. font = self.hexView.font()
  1457. font.setBold(True)
  1458. self.hexView.setFont(font)
  1459. self.bitsArea.layout().addWidget(self.hexView, 0, 1)
  1460. for i in range(0, len(image) * 8):
  1461. desc = self.__getBitDescription(i)
  1462. if desc:
  1463. desc = " (%s)" % desc
  1464. checkbox = QCheckBox("bit %d%s" % (i, desc), self.bitsArea)
  1465. if byte2int(image[i // 8]) & bit(i % 8):
  1466. checkbox.setCheckState(Qt.CheckState.Checked)
  1467. self.bitsArea.layout().addWidget(checkbox, 1 + i, 0, 1, 2)
  1468. checkbox.stateChanged.connect(self.__bitStateChanged)
  1469. self.checkboxes.append(checkbox)
  1470. self.__updateHexView()
  1471. def __updateHexView(self):
  1472. self.hexView.setText(bytes2hex(self.image))
  1473. self.bitsAreaScroll.setWidget(self.bitsArea)
  1474. def __bitStateChanged(self, unused):
  1475. self.image = ""
  1476. tmp = 0
  1477. i = 0
  1478. for checkbox in self.checkboxes:
  1479. if checkbox.checkState() == Qt.CheckState.Checked:
  1480. tmp |= bit(i)
  1481. i += 1
  1482. if i == 8:
  1483. self.image += chr(tmp)
  1484. i = 0
  1485. tmp = 0
  1486. assert(i == 0)
  1487. self.__updateHexView()
  1488. def getRawData(self):
  1489. return self.image
  1490. def setRawData(self, data):
  1491. if self.isReadOnly():
  1492. return False
  1493. if len(data) > 32: # Don't want huge files
  1494. return False
  1495. self.loadImage(data)
  1496. return True
  1497. def clear(self):
  1498. self.loadImage(None)
  1499. class ProgmemBufferWidget(ImageBufferWidget):
  1500. def __init__(self, mainWindow):
  1501. ImageBufferWidget.__init__(self, mainWindow, "program memory")
  1502. def setDataWithIHexInterpreter(self, interp, readRaw):
  1503. return self.setRawData(interp.getProgmem(dontInterpretSections = readRaw))
  1504. class EEPROMBufferWidget(ImageBufferWidget):
  1505. def __init__(self, mainWindow):
  1506. ImageBufferWidget.__init__(self, mainWindow, "(E)EPROM memory")
  1507. def setDataWithIHexInterpreter(self, interp, readRaw):
  1508. return self.setRawData(interp.getEEPROM(dontInterpretSections = readRaw))
  1509. class RAMBufferWidget(ImageBufferWidget):
  1510. def __init__(self, mainWindow):
  1511. ImageBufferWidget.__init__(self, mainWindow, "RAM memory")
  1512. def setDataWithIHexInterpreter(self, interp, readRaw):
  1513. return self.setRawData(interp.getRAM(dontInterpretSections = readRaw))
  1514. class FuseBufferWidget(BitBufferWidget):
  1515. def __init__(self, mainWindow):
  1516. BitBufferWidget.__init__(self, mainWindow, "fuse bits", "fuseDesc")
  1517. def setDataWithIHexInterpreter(self, interp, readRaw):
  1518. return self.setRawData(interp.getFusebits(dontInterpretSections = readRaw))
  1519. class LockBufferWidget(BitBufferWidget):
  1520. def __init__(self, mainWindow):
  1521. BitBufferWidget.__init__(self, mainWindow, "lock bits", "lockbitDesc")
  1522. def setDataWithIHexInterpreter(self, interp, readRaw):
  1523. return self.setRawData(interp.getLockbits(dontInterpretSections = readRaw))
  1524. class BufferTabWidget(QTabWidget):
  1525. def __init__(self, mainWindow):
  1526. QTabWidget.__init__(self, mainWindow)
  1527. self.mainWindow = mainWindow
  1528. self.infoBuffer = InfoBufferWidget(mainWindow, "chip info")
  1529. self.progmemBuffer = ProgmemBufferWidget(mainWindow)
  1530. self.eepromBuffer = EEPROMBufferWidget(mainWindow)
  1531. self.fuseBuffer = FuseBufferWidget(mainWindow)
  1532. self.lockBuffer = LockBufferWidget(mainWindow)
  1533. self.ramBuffer = RAMBufferWidget(mainWindow)
  1534. self.setTabPosition(self.TabPosition.South)
  1535. self.__addBufferTab(self.infoBuffer, readOnly=True)
  1536. self.removeAll()
  1537. def __addBufferTab(self, bufferWidget, readOnly=False):
  1538. self.addTab(bufferWidget, bufferWidget.getName())
  1539. bufferWidget.setReadOnly(readOnly)
  1540. bufferWidget.setAvailable(True)
  1541. def __removeBufferTab(self, bufferWidget):
  1542. self.removeTab(self.indexOf(bufferWidget))
  1543. bufferWidget.clear()
  1544. bufferWidget.setAvailable(False)
  1545. def __removeAll(self):
  1546. self.__removeBufferTab(self.progmemBuffer)
  1547. self.__removeBufferTab(self.eepromBuffer)
  1548. self.__removeBufferTab(self.fuseBuffer)
  1549. self.__removeBufferTab(self.lockBuffer)
  1550. self.__removeBufferTab(self.ramBuffer)
  1551. def removeAll(self):
  1552. self.__removeAll()
  1553. def setupBuffers(self, chipSupportFlags):
  1554. prevSelect = self.getCurrentBuffer()
  1555. self.__removeAll()
  1556. for (bufferWidget, flagsMask) in (
  1557. (self.progmemBuffer, Chip.SUPPORT_PROGMEMREAD | Chip.SUPPORT_PROGMEMWRITE),
  1558. (self.eepromBuffer, Chip.SUPPORT_EEPROMREAD | Chip.SUPPORT_EEPROMWRITE),
  1559. (self.fuseBuffer, Chip.SUPPORT_FUSEREAD | Chip.SUPPORT_FUSEWRITE),
  1560. (self.lockBuffer, Chip.SUPPORT_LOCKREAD | Chip.SUPPORT_LOCKWRITE),
  1561. (self.ramBuffer, Chip.SUPPORT_RAMREAD | Chip.SUPPORT_RAMWRITE)):
  1562. if chipSupportFlags & flagsMask:
  1563. self.__addBufferTab(bufferWidget)
  1564. if prevSelect and prevSelect.isAvailable():
  1565. self.setCurrentIndex(self.indexOf(prevSelect))
  1566. def loadBuffers(self, images):
  1567. try:
  1568. self.progmemBuffer.loadImage(images["progmem"])
  1569. except KeyError:
  1570. pass # Ignore
  1571. try:
  1572. self.eepromBuffer.loadImage(images["eeprom"])
  1573. except KeyError:
  1574. pass # Ignore
  1575. try:
  1576. self.fuseBuffer.loadImage(images["fusebits"])
  1577. except KeyError:
  1578. pass # Ignore
  1579. try:
  1580. self.lockBuffer.loadImage(images["lockbits"])
  1581. except KeyError:
  1582. pass # Ignore
  1583. try:
  1584. self.ramBuffer.loadImage(images["ram"])
  1585. except KeyError:
  1586. pass # Ignore
  1587. def verifyBuffers(self, images):
  1588. fail = False
  1589. for (imageName, bufferWidget) in (("progmem", self.progmemBuffer),
  1590. ("eeprom", self.eepromBuffer),
  1591. ("fusebits", self.fuseBuffer),
  1592. ("ram", self.ramBuffer)):
  1593. try:
  1594. image = images[imageName]
  1595. except KeyError:
  1596. continue
  1597. bufImage = bufferWidget.getRawData()
  1598. if not bufImage:
  1599. if not image:
  1600. # Both images are empty. Go on...
  1601. continue
  1602. self.mainWindow.console.showMessage(
  1603. "Chip verify of %s FAILED! "
  1604. "There is no data in the "
  1605. "%s buffer. First load data into "
  1606. "the buffer tab, please.\n" %\
  1607. (bufferWidget.getName(),
  1608. bufferWidget.getName()),
  1609. bold=True)
  1610. fail = True
  1611. continue
  1612. if len(bufImage) > len(image):
  1613. self.mainWindow.console.showMessage(
  1614. "Chip verify of %s FAILED! "
  1615. "Buffer data is larger than "
  1616. "chip memory.\n" %\
  1617. bufferWidget.getName(),
  1618. bold=True)
  1619. fail = True
  1620. continue
  1621. compareSize = min(len(image), len(bufImage))
  1622. if len(image) != len(bufImage):
  1623. self.mainWindow.console.showMessage(
  1624. "%s: Only comparing the first 0x%X bytes due "
  1625. "to a mismatch of the buffer size (0x%X) "
  1626. "with the actual device image size (0x%X).\n" %\
  1627. (bufferWidget.getName(), compareSize,
  1628. len(bufImage), len(image)))
  1629. if bufImage[:compareSize] != image[:compareSize]:
  1630. self.mainWindow.console.showMessage(
  1631. "Chip verify of %s FAILED! "
  1632. "Image data mismatch.\n" %\
  1633. bufferWidget.getName(),
  1634. bold=True)
  1635. fail = True
  1636. continue
  1637. if fail:
  1638. self.mainWindow.console.setStatus(Console.STAT_ERROR)
  1639. else:
  1640. self.mainWindow.console.showMessage("Chip verify succeed\n", bold=True)
  1641. self.mainWindow.console.setStatus(Console.STAT_OK)
  1642. return not fail
  1643. def getCurrentBuffer(self):
  1644. return self.currentWidget()
  1645. class ChipSelectDialog(QDialog):
  1646. ALL_VENDORS = "--- All vendors ---"
  1647. def __init__(self, parent):
  1648. QDialog.__init__(self, parent)
  1649. self.setWindowTitle("Select chip")
  1650. self.setLayout(QGridLayout(self))
  1651. groupBox = QGroupBox("Chip type", self)
  1652. groupBox.setLayout(QGridLayout(groupBox))
  1653. self.allRadio = QRadioButton("All types", groupBox)
  1654. self.allRadio.setChecked(True)
  1655. groupBox.layout().addWidget(self.allRadio, 0, 0)
  1656. self.mcuRadio = QRadioButton("Microcontrollers", groupBox)
  1657. groupBox.layout().addWidget(self.mcuRadio, 1, 0)
  1658. self.epromRadio = QRadioButton("EPROMs", groupBox)
  1659. groupBox.layout().addWidget(self.epromRadio, 2, 0)
  1660. self.eepromRadio = QRadioButton("EEPROMs", groupBox)
  1661. groupBox.layout().addWidget(self.eepromRadio, 3, 0)
  1662. self.galRadio = QRadioButton("PALs / GALs", groupBox)
  1663. groupBox.layout().addWidget(self.galRadio, 4, 0)
  1664. self.sramRadio = QRadioButton("Static RAM", groupBox)
  1665. groupBox.layout().addWidget(self.sramRadio, 5, 0)
  1666. self.logicRadio = QRadioButton("Logic chips", groupBox)
  1667. groupBox.layout().addWidget(self.logicRadio, 6, 0)
  1668. self.showBroken = QCheckBox("Show broken implementations", groupBox)
  1669. groupBox.layout().addWidget(self.showBroken, 7, 0)
  1670. self.layout().addWidget(groupBox, 0, 0, 2, 2)
  1671. l = QLabel("Vendor:", self)
  1672. self.layout().addWidget(l, 0, 2, 1, 2)
  1673. self.vendorList = QListWidget(self)
  1674. self.layout().addWidget(self.vendorList, 1, 2, 1, 2)
  1675. l = QLabel("Chip:", self)
  1676. self.layout().addWidget(l, 0, 4, 1, 2)
  1677. self.chipList = QListWidget(self)
  1678. self.layout().addWidget(self.chipList, 1, 4, 1, 2)
  1679. self.okButton = QPushButton("&Ok", self)
  1680. self.layout().addWidget(self.okButton, 2, 0, 1, 3)
  1681. self.cancelButton = QPushButton("&Cancel", self)
  1682. self.layout().addWidget(self.cancelButton, 2, 3, 1, 3)
  1683. self.okButton.released.connect(self.accept)
  1684. self.cancelButton.released.connect(self.reject)
  1685. self.vendorList.itemSelectionChanged.connect(self.vendorSelectionChanged)
  1686. self.chipList.itemSelectionChanged.connect(self.chipSelectionChanged)
  1687. self.chipList.itemDoubleClicked.connect(self.accept)
  1688. self.showBroken.stateChanged.connect(self.typeToggled)
  1689. for radio in (self.allRadio, self.mcuRadio, self.epromRadio,
  1690. self.eepromRadio, self.galRadio,
  1691. self.sramRadio, self.logicRadio):
  1692. radio.toggled.connect(self.typeToggled)
  1693. self.__updateVendorList()
  1694. self.__updateChipList()
  1695. def __updateVendorList(self):
  1696. previousVendor = self.__getSelectedVendor()
  1697. selType = self.__getSelectedChipType()
  1698. vendors = getRegisteredVendors()
  1699. self.vendorList.clear()
  1700. QListWidgetItem(self.ALL_VENDORS, self.vendorList)
  1701. for vendorName in list(vendors.keys()):
  1702. descriptors = vendors[vendorName]
  1703. if self.showBroken.checkState() != Qt.CheckState.Checked:
  1704. descriptors = [d for d in descriptors if not d.broken]
  1705. descriptors = [d for d in descriptors if (d.chipType == selType or selType == -1) and\
  1706. (d.chipType != ChipDescription.TYPE_INTERNAL)]
  1707. if not descriptors:
  1708. continue
  1709. item = QListWidgetItem(vendorName, self.vendorList)
  1710. item.setData(Qt.ItemDataRole.UserRole, descriptors)
  1711. self.vendorList.sortItems()
  1712. if previousVendor:
  1713. items = self.vendorList.findItems(previousVendor, Qt.MatchFlag.MatchExactly)
  1714. if len(items) == 1:
  1715. self.vendorList.setCurrentItem(items[0])
  1716. else:
  1717. self.vendorList.setCurrentRow(0)
  1718. else:
  1719. self.vendorList.setCurrentRow(0)
  1720. def __descriptorText(self, descriptor):
  1721. text = descriptor.description
  1722. if not descriptor.maintainer:
  1723. text += " (Orphaned)"
  1724. return text
  1725. def __updateChipList(self):
  1726. previousChip = self.getSelectedChip()
  1727. selType = self.__getSelectedChipType()
  1728. selVendor = self.__getSelectedVendor()
  1729. self.chipList.clear()
  1730. for descriptor in getRegisteredChips():
  1731. if descriptor.broken and\
  1732. self.showBroken.checkState() != Qt.CheckState.Checked:
  1733. continue
  1734. if descriptor.chipType != selType and selType != -1:
  1735. continue
  1736. if descriptor.chipType == ChipDescription.TYPE_INTERNAL:
  1737. continue
  1738. if selVendor != self.ALL_VENDORS and selVendor not in descriptor.chipVendors:
  1739. continue
  1740. item = QListWidgetItem(self.__descriptorText(descriptor),
  1741. self.chipList)
  1742. item.setData(Qt.ItemDataRole.UserRole, descriptor)
  1743. self.chipList.sortItems()
  1744. if previousChip:
  1745. items = self.chipList.findItems(self.__descriptorText(previousChip),
  1746. Qt.MatchFlag.MatchExactly)
  1747. if len(items) == 1:
  1748. self.chipList.setCurrentItem(items[0])
  1749. else:
  1750. self.chipList.setCurrentRow(0)
  1751. else:
  1752. self.chipList.setCurrentRow(0)
  1753. def typeToggled(self, unused):
  1754. self.__updateVendorList()
  1755. self.__updateChipList()
  1756. def vendorSelectionChanged(self):
  1757. self.__updateChipList()
  1758. def chipSelectionChanged(self):
  1759. item = self.chipList.currentItem()
  1760. self.okButton.setEnabled(bool(item))
  1761. def __getSelectedVendor(self):
  1762. item = self.vendorList.currentItem()
  1763. if not item:
  1764. return None
  1765. return str(item.text())
  1766. def __getSelectedChipType(self):
  1767. # Returns ChipDescription.TYPE_... or -1 if all are selected
  1768. for (typeId, radioButton) in (
  1769. (ChipDescription.TYPE_MCU, self.mcuRadio),
  1770. (ChipDescription.TYPE_EPROM, self.epromRadio),
  1771. (ChipDescription.TYPE_EEPROM, self.eepromRadio),
  1772. (ChipDescription.TYPE_GAL, self.galRadio),
  1773. (ChipDescription.TYPE_SRAM, self.sramRadio),
  1774. (ChipDescription.TYPE_LOGIC, self.logicRadio)):
  1775. if radioButton.isChecked():
  1776. return typeId
  1777. assert(self.allRadio.isChecked())
  1778. return -1
  1779. def getSelectedChip(self):
  1780. item = self.chipList.currentItem()
  1781. if not item:
  1782. return None
  1783. chipDescription = item.data(Qt.ItemDataRole.UserRole)
  1784. return chipDescription
  1785. class StatusBar(QStatusBar):
  1786. def __init__(self, mainWindow):
  1787. QStatusBar.__init__(self, mainWindow)
  1788. class TopToolBar(QToolBar):
  1789. def __init__(self, mainWindow):
  1790. QToolBar.__init__(self, "Toolbar", mainWindow)
  1791. self.mainWindow = mainWindow
  1792. self.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextUnderIcon)
  1793. self.setup(0)
  1794. def setup(self, chipSupportFlags):
  1795. mainWindow = self.mainWindow
  1796. self.clear()
  1797. a = self.addAction(getIcon("open"), "Load buffer", mainWindow.loadBuffer)
  1798. a.setEnabled(chipSupportFlags != 0)
  1799. a = self.addAction(getIcon("save"), "Save buffer", mainWindow.saveBuffer)
  1800. a.setEnabled(chipSupportFlags != 0)
  1801. self.addSeparator()
  1802. self.addAction(getIcon("chip"), "Run logic tester", mainWindow.startUnitest)
  1803. self.addAction(getIcon("chip"), "Select chip", mainWindow.selectChip)
  1804. self.addSeparator()
  1805. a = self.addAction(getIcon("up"), "Read chip", mainWindow.readChip)
  1806. a.setEnabled(chipSupportFlags != 0)
  1807. a = self.addAction(getIcon("verify"), "Verify all", mainWindow.verifyChip)
  1808. a.setEnabled(chipSupportFlags != 0)
  1809. class RightToolBar(QToolBar):
  1810. def __init__(self, mainWindow):
  1811. QToolBar.__init__(self, "Toolbar", mainWindow)
  1812. self.mainWindow = mainWindow
  1813. self.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextUnderIcon)
  1814. self.setup(0)
  1815. def setup(self, chipSupportFlags):
  1816. mainWindow = self.mainWindow
  1817. self.clear()
  1818. if chipSupportFlags & Chip.SUPPORT_ERASE:
  1819. self.addAction(getIcon("erase"), "Erase", mainWindow.eraseChip)
  1820. if chipSupportFlags & Chip.SUPPORT_PROGMEMWRITE:
  1821. self.addAction(getIcon("memory"), "Write progmem", mainWindow.writeChipProgmem)
  1822. if chipSupportFlags & Chip.SUPPORT_EEPROMWRITE:
  1823. self.addAction(getIcon("memory"), "Write EPROM", mainWindow.writeChipEeprom)
  1824. if chipSupportFlags & Chip.SUPPORT_FUSEWRITE:
  1825. self.addAction(getIcon("memory"), "Write fuses", mainWindow.writeChipFuses)
  1826. if chipSupportFlags & Chip.SUPPORT_RAMWRITE:
  1827. self.addAction(getIcon("memory"), "Write RAM", mainWindow.writeRam)
  1828. if chipSupportFlags & Chip.SUPPORT_LOCKWRITE:
  1829. self.addAction(getIcon("lock"), "Write lockbits", mainWindow.writeChipLockbits)
  1830. if chipSupportFlags & Chip.SUPPORT_TEST:
  1831. self.addAction(getIcon("test"), "Run unit-test", mainWindow.runTest)
  1832. class AutorunWidget(QDockWidget):
  1833. def __init__(self, mainWindow):
  1834. QDockWidget.__init__(self, "Autorun", mainWindow)
  1835. self.mainWindow = mainWindow
  1836. self.running = False
  1837. self.currentAction = -1
  1838. self.setFeatures(self.DockWidgetFeature.DockWidgetMovable |
  1839. self.DockWidgetFeature.DockWidgetFloatable)
  1840. self.setup(0)
  1841. def __addCheckBox(self, row, condition, text, checked):
  1842. if condition:
  1843. checkbox = QCheckBox(text, self.widget())
  1844. if checked:
  1845. checkbox.setCheckState(Qt.CheckState.Checked)
  1846. self.widget().layout().addWidget(checkbox, row, 0)
  1847. row += 1
  1848. return (checkbox, row)
  1849. return (None, row)
  1850. def setup(self, chipSupportFlags):
  1851. self.setWidget(QWidget(self))
  1852. self.widget().show()
  1853. self.widget().setLayout(QGridLayout(self.widget()))
  1854. row = 0
  1855. (self.testCheckBox, row) = self.__addCheckBox(row,
  1856. chipSupportFlags & Chip.SUPPORT_TEST,
  1857. "Run unit-test", True)
  1858. (self.eraseCheckBox, row) = self.__addCheckBox(row,
  1859. chipSupportFlags & Chip.SUPPORT_ERASE,
  1860. "Erase", True)
  1861. (self.progmemCheckBox, row) = self.__addCheckBox(row,
  1862. chipSupportFlags & Chip.SUPPORT_PROGMEMWRITE,
  1863. "Write program memory", True)
  1864. (self.eepromCheckBox, row) = self.__addCheckBox(row,
  1865. chipSupportFlags & Chip.SUPPORT_EEPROMWRITE,
  1866. "Write (E)EPROM", True)
  1867. (self.ramCheckBox, row) = self.__addCheckBox(row,
  1868. chipSupportFlags & Chip.SUPPORT_RAMWRITE,
  1869. "Write RAM", True)
  1870. (self.fusesCheckBox, row) = self.__addCheckBox(row,
  1871. chipSupportFlags & Chip.SUPPORT_FUSEWRITE,
  1872. "Write fuses", True)
  1873. (self.verifyCheckBox, row) = self.__addCheckBox(row,
  1874. chipSupportFlags != 0,
  1875. "Verify selected memories", True)
  1876. (self.lockbitsCheckBox, row) = self.__addCheckBox(row,
  1877. chipSupportFlags & Chip.SUPPORT_LOCKWRITE,
  1878. "Write lock bits", False)
  1879. self.runButton = None
  1880. if chipSupportFlags != 0:
  1881. self.runButton = QPushButton("Run", self.widget())
  1882. self.runButton.setIconSize(QSize(32, 32))
  1883. self.runButton.setIcon(getIcon("run"))
  1884. self.widget().layout().addWidget(self.runButton, row, 0)
  1885. row += 1
  1886. self.runButton.released.connect(self.runNextChip)
  1887. self.widget().layout().setRowStretch(row, 99)
  1888. def isRunning(self):
  1889. return self.running
  1890. def abortRunWithMessage(self):
  1891. self.abortRun(withMessage=True)
  1892. def abortRun(self, withMessage=False):
  1893. if withMessage:
  1894. QMessageBox.critical(self, "Aborting autorun",
  1895. "One action failed. Aborting autorun.\n"
  1896. "See console messages for details.")
  1897. self.running = False
  1898. self.currentAction = -1;
  1899. self.mainWindow.guiUpdateEnable()
  1900. def runNextChip(self):
  1901. self.running = True
  1902. self.currentAction = 0
  1903. res = QMessageBox.information(self, "Autorun - Please insert chip",
  1904. "Please insert a new %s chip into the ZIF socket "
  1905. "and press Ok to perform the operations..." %\
  1906. self.mainWindow.currentChipDescription.description,
  1907. QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel,
  1908. QMessageBox.StandardButton.Ok)
  1909. if res != QMessageBox.StandardButton.Ok:
  1910. self.abortRun()
  1911. return
  1912. self.runNextAction()
  1913. def runNextAction(self):
  1914. assert(self.currentAction >= 0)
  1915. action = 0
  1916. if self.testCheckBox:
  1917. if self.currentAction == action:
  1918. if self.__runAction(self.mainWindow.runTest,
  1919. self.testCheckBox,
  1920. "Run unit-test"):
  1921. return
  1922. action += 1
  1923. if self.eraseCheckBox:
  1924. if self.currentAction == action:
  1925. if self.__runAction(self.mainWindow.eraseChip,
  1926. self.eraseCheckBox,
  1927. "Erase"):
  1928. return
  1929. action += 1
  1930. if self.progmemCheckBox:
  1931. if self.currentAction == action:
  1932. if self.__runAction(self.mainWindow.writeChipProgmem,
  1933. self.progmemCheckBox,
  1934. "Write program memory"):
  1935. return
  1936. action += 1
  1937. if self.eepromCheckBox:
  1938. if self.currentAction == action:
  1939. if self.__runAction(self.mainWindow.writeChipEeprom,
  1940. self.eepromCheckBox,
  1941. "Write (E)EPROM"):
  1942. return
  1943. action += 1
  1944. if self.ramCheckBox:
  1945. if self.currentAction == action:
  1946. if self.__runAction(self.mainWindow.writeRam,
  1947. self.ramCheckBox,
  1948. "Write RAM"):
  1949. return
  1950. action += 1
  1951. if self.fusesCheckBox:
  1952. if self.currentAction == action:
  1953. if self.__runAction(self.mainWindow.writeChipFuses,
  1954. self.fusesCheckBox,
  1955. "Write fuses"):
  1956. return
  1957. action += 1
  1958. if self.verifyCheckBox:
  1959. if self.currentAction == action:
  1960. if self.__runAction(self.__verifySelectedMemories,
  1961. self.verifyCheckBox,
  1962. "Verify selected memories"):
  1963. return
  1964. action += 1
  1965. if self.lockbitsCheckBox:
  1966. if self.currentAction == action:
  1967. if self.__runAction(self.mainWindow.writeChipLockbits,
  1968. self.lockbitsCheckBox,
  1969. "Write lock bits"):
  1970. return
  1971. action += 1
  1972. self.runNextChip()
  1973. def __runAction(self, function, checkbox, actionName):
  1974. self.currentAction += 1
  1975. if checkbox.checkState() == Qt.CheckState.Checked:
  1976. self.mainWindow.console.showMessage(
  1977. "Running action: %s\n" % actionName, bold=True)
  1978. if not function():
  1979. self.abortRun(withMessage=True)
  1980. return True
  1981. return False
  1982. def __verifySelectedMemories(self):
  1983. def checked(cb):
  1984. return cb and (cb.checkState() == Qt.CheckState.Checked)
  1985. return self.mainWindow.verifySelectedMemories(
  1986. verifyProgmem = checked(self.progmemCheckBox),
  1987. verifyEEPROM = checked(self.eepromCheckBox),
  1988. verifyFuse = checked(self.fusesCheckBox),
  1989. verifyRAM = checked(self.ramCheckBox))
  1990. class ProgrammerSelectDialog(QDialog):
  1991. def __init__(self, parent=None):
  1992. QDialog.__init__(self, parent)
  1993. self.setWindowTitle("Select programmer device")
  1994. self.setLayout(QGridLayout(self))
  1995. self.deviceList = QListWidget(self)
  1996. self.layout().addWidget(self.deviceList, 0, 0, 1, 2)
  1997. self.rescanButton = QPushButton("&Rescan USB busses", self)
  1998. self.layout().addWidget(self.rescanButton, 1, 0, 1, 2)
  1999. self.okButton = QPushButton("&OK", self)
  2000. self.layout().addWidget(self.okButton, 2, 0)
  2001. self.cancelButton = QPushButton("&Cancel", self)
  2002. self.layout().addWidget(self.cancelButton, 2, 1)
  2003. self.rescan()
  2004. self.deviceList.currentRowChanged.connect(self.selectionChanged)
  2005. self.deviceList.itemDoubleClicked.connect(self.accept)
  2006. self.okButton.released.connect(self.accept)
  2007. self.cancelButton.released.connect(self.reject)
  2008. self.rescanButton.released.connect(self.rescan)
  2009. def rescan(self):
  2010. self.deviceList.clear()
  2011. for foundDev in TOP.findDevices():
  2012. text = "%s (%s)" % (foundDev.toptype,
  2013. foundDev.devIdentifier)
  2014. item = QListWidgetItem(text, self.deviceList)
  2015. item.setData(Qt.ItemDataRole.UserRole, foundDev.devIdentifier)
  2016. self.deviceList.setCurrentRow(0)
  2017. def getIdentifier(self):
  2018. item = self.deviceList.currentItem()
  2019. if not item:
  2020. return None
  2021. return item.data(Qt.ItemDataRole.UserRole)
  2022. def selectionChanged(self, unused):
  2023. if self.deviceList.currentItem():
  2024. self.okButton.setEnabled(True)
  2025. else:
  2026. self.okButton.setEnabled(False)
  2027. class Operation(object):
  2028. OP_NONE = 0
  2029. OP_SHUTDOWN = 1
  2030. OP_INITCHIP = 2
  2031. OP_READALL = 3
  2032. OP_ERASE = 4
  2033. OP_WRITEPROG = 5
  2034. OP_WRITEEPROM = 6
  2035. OP_WRITEFUSE = 7
  2036. OP_WRITELOCK = 8
  2037. OP_WRITERAM = 9
  2038. OP_TEST = 10
  2039. OP_VERIFY = 11
  2040. OP_RAWCOMMAND = 12
  2041. OP_SYNCHRONOUS = 13
  2042. def __init__(self, op, hwTask, hwTaskParam=None):
  2043. self.op = op
  2044. self.hwTask = hwTask
  2045. self.hwTaskParam = hwTaskParam
  2046. self.returnValue = None
  2047. self.failed = False
  2048. self.finished = False
  2049. class MainWindow(QMainWindow):
  2050. def __init__(self, parent=None):
  2051. QMainWindow.__init__(self, parent)
  2052. self.setWindowTitle("TOPrammer v%s - Open Source programming suite" % VERSION)
  2053. self.guiDisable = 0
  2054. self.chip = None # Chip instance
  2055. self.currentChipDescription = None
  2056. self.previousRawCommand = ""
  2057. self.setStatusBar(StatusBar(self))
  2058. self.topToolBar = TopToolBar(self)
  2059. self.rightToolBar = RightToolBar(self)
  2060. self.addToolBar(Qt.ToolBarArea.TopToolBarArea, self.topToolBar)
  2061. self.addToolBar(Qt.ToolBarArea.RightToolBarArea, self.rightToolBar)
  2062. self.setMenuBar(QMenuBar(self))
  2063. menu = QMenu("&File", self)
  2064. menu.addAction("&Load buffer...", self.loadBuffer)
  2065. menu.addAction("&Save buffer...", self.saveBuffer)
  2066. menu.addSeparator()
  2067. menu.addAction("&Exit", self.close)
  2068. self.menuBar().addMenu(menu)
  2069. self.runMenu = QMenu("&Run", self)
  2070. self.setupRunMenu(0)
  2071. self.menuBar().addMenu(self.runMenu)
  2072. menu = QMenu("&Programmer", self)
  2073. menu.addAction("&Universal logic tester...", self.startUnitest)
  2074. menu.addSeparator()
  2075. menu.addAction("Select &programmer...", self.selectProgrammer)
  2076. menu.addAction("&Send raw command...", self.sendRawCommand)
  2077. self.menuBar().addMenu(menu)
  2078. menu = QMenu("&Help", self)
  2079. menu.addAction("&About", self.showAbout)
  2080. self.menuBar().addMenu(menu)
  2081. self.bufferTab = BufferTabWidget(self)
  2082. self.setCentralWidget(self.bufferTab)
  2083. self.console = Console(self)
  2084. self.addDockWidget(Qt.DockWidgetArea.BottomDockWidgetArea, self.console)
  2085. self.autorun = AutorunWidget(self)
  2086. self.addDockWidget(Qt.DockWidgetArea.LeftDockWidgetArea, self.autorun)
  2087. self.hwThread = HwThread(self)
  2088. self.threadRunning = False
  2089. self.queuedOps = []
  2090. self.runningOps = {}
  2091. def setupRunMenu(self, chipSupportFlags):
  2092. menu = self.runMenu
  2093. menu.clear()
  2094. menu.addAction("&Select chip", self.selectChip)
  2095. menu.addSeparator()
  2096. a = menu.addAction("&Read chip", self.readChip)
  2097. a.setEnabled(chipSupportFlags != 0)
  2098. a = menu.addAction("&Erase", self.eraseChip)
  2099. a.setEnabled(bool(chipSupportFlags & Chip.SUPPORT_ERASE))
  2100. a = menu.addAction("Write &program memory", self.writeChipProgmem)
  2101. a.setEnabled(bool(chipSupportFlags & Chip.SUPPORT_PROGMEMWRITE))
  2102. a = menu.addAction("Write (E)EP&ROM", self.writeChipEeprom)
  2103. a.setEnabled(bool(chipSupportFlags & Chip.SUPPORT_EEPROMWRITE))
  2104. a = menu.addAction("Write &fuses", self.writeChipFuses)
  2105. a.setEnabled(bool(chipSupportFlags & Chip.SUPPORT_FUSEWRITE))
  2106. a = menu.addAction("Write &lock bits", self.writeChipLockbits)
  2107. a.setEnabled(bool(chipSupportFlags & Chip.SUPPORT_LOCKWRITE))
  2108. a = menu.addAction("Write R&AM", self.writeRam)
  2109. a.setEnabled(bool(chipSupportFlags & Chip.SUPPORT_RAMWRITE))
  2110. a = menu.addAction("Run &Unit-test", self.runTest)
  2111. a.setEnabled(bool(chipSupportFlags & Chip.SUPPORT_TEST))
  2112. a = menu.addAction("&Verify whole chip", self.verifyChip)
  2113. a.setEnabled(chipSupportFlags != 0)
  2114. def event(self, e):
  2115. if e.type() == EVENT_HWTHREAD:
  2116. self.hwThread.handleMessageQueue()
  2117. e.accept()
  2118. return True
  2119. return QMainWindow.event(self, e)
  2120. def closeEvent(self, e):
  2121. if self.threadRunning:
  2122. res = QMessageBox.critical(self, "Task running",
  2123. "A hardware access task is running.\n\n"
  2124. "'Cancel' the close request and continue, or\n"
  2125. "'Abort' the task an quit the application?",
  2126. QMessageBox.Cancel | QMessageBox.Abort,
  2127. QMessageBox.Cancel)
  2128. if res == QMessageBox.Abort:
  2129. e.accept()
  2130. # Force quit
  2131. QApplication.exit(1)
  2132. return
  2133. e.ignore()
  2134. return
  2135. self.threadRunning = False
  2136. self.hwThread.killThread()
  2137. e.accept()
  2138. def showAbout(self):
  2139. QMessageBox.information(self, "About TOPrammer",
  2140. "Copyright (c) Michael Büsch <m@bues.ch>")
  2141. def startUnitest(self):
  2142. self.bufferTab.setupBuffers(0)
  2143. self.bufferTab.infoBuffer.clear()
  2144. self.autorun.setup(0)
  2145. self.guiDisable += 1
  2146. self.console.blockStatusUpdate()
  2147. try:
  2148. dlg = UnitestDialog(self)
  2149. dlg.exec()
  2150. except (TOPException) as e:
  2151. self.console.showMessage("Failed to start Unitest: %s\n" % str(e),
  2152. bold=True)
  2153. self.console.unblockStatusUpdate()
  2154. self.guiDisable -= 1
  2155. self.guiUpdateEnable()
  2156. self.runOperation(Operation(Operation.OP_SHUTDOWN,
  2157. HwThread.TASK_SHUTDOWN))
  2158. def selectProgrammer(self):
  2159. dlg = ProgrammerSelectDialog(self)
  2160. if dlg.exec() != QDialog.DialogCode.Accepted:
  2161. return
  2162. devIdentifier = dlg.getIdentifier()
  2163. self.runOperationSync(HwThread.TASK_SHUTDOWN)
  2164. self.hwThread.setDevice(devIdentifier)
  2165. if self.currentChipDescription:
  2166. self.runOperation(Operation(Operation.OP_INITCHIP,
  2167. HwThread.TASK_INITCHIP,
  2168. self.currentChipDescription.chipID))
  2169. def sendRawCommand(self):
  2170. (string, ok) = QInputDialog.getText(self,
  2171. "Send raw command to programmer",
  2172. "Enter raw command to send, in " +\
  2173. "hex format (AABB1122...).\n" +\
  2174. "WARNING: The programmer will malfunction on invalid commands.",
  2175. QLineEdit.EchoMode.Normal,
  2176. self.previousRawCommand)
  2177. if not ok:
  2178. return
  2179. string = str(string).strip().upper()
  2180. string = stringRemoveChars(string, " \t\r\n")
  2181. if stringRemoveChars(string, "0123456789ABCDEF"):
  2182. QMessageBox.critical(self, "Invalid characters",
  2183. "Invalid characters in raw command string.\n" +\
  2184. "Only hex characters 0-9,a-f allowed.")
  2185. return
  2186. if len(string) % 2 != 0 or len(string) // 2 > 64:
  2187. QMessageBox.critical(self, "Invalid length",
  2188. "Invalid command length. Length must be even " +\
  2189. "and smaller or equal to 64 bytes.")
  2190. return
  2191. self.previousRawCommand = string
  2192. command = hex2bin(string)
  2193. if len(command) == 0:
  2194. return
  2195. self.runOperation(Operation(Operation.OP_RAWCOMMAND,
  2196. HwThread.TASK_GENERICTOPCALL,
  2197. GenericTopCall("top.hw.runCommandSync(...)", command)))
  2198. def loadBuffer(self):
  2199. bufWidget = self.bufferTab.getCurrentBuffer()
  2200. data = None
  2201. if not bufWidget:
  2202. return
  2203. if bufWidget.isReadOnly():
  2204. QMessageBox.critical(self, "Buffer is read only",
  2205. "Cannot load data into the %s buffer.\n"
  2206. "The buffer is read-only." %\
  2207. bufWidget.getName())
  2208. return
  2209. (fn, selectedFilter) = QFileDialog.getOpenFileName(self,
  2210. "%s - open file" % bufWidget.getName(),
  2211. "",
  2212. "Autodetect file format (*);;"
  2213. "Intel hex file (*.ihex *.hex);;"
  2214. "Hex file with ASCII dump (*.ahex);;"
  2215. "Binary file (*.bin)")
  2216. if not fn:
  2217. return
  2218. extensions = str(selectedFilter).split("(")[1].\
  2219. split(")")[0].replace("*", "").strip().split()
  2220. try:
  2221. dataIn = open(fn, "rb").read()
  2222. except (IOError) as e:
  2223. QMessageBox.critical(self, "Failed to read file",
  2224. "Failed to read %s:\n%s" %\
  2225. (str(fn), str(e.strerror)))
  2226. return
  2227. try:
  2228. if ".bin" in extensions:
  2229. handler = IO_binary()
  2230. elif ".ahex" in extensions:
  2231. handler = IO_ahex()
  2232. elif ".ihex" in extensions:
  2233. handler = IO_ihex()
  2234. elif not extensions: # auto
  2235. handler = IO_autodetect(dataIn)()
  2236. else:
  2237. assert(0)
  2238. if isinstance(handler, IO_ihex):
  2239. interp = self.chip.getIHexInterpreter()
  2240. interp.interpret(dataIn)
  2241. if interp.cumulativeSupported():
  2242. res = QMessageBox.question(self,
  2243. "Parse IHEX sections?",
  2244. "This IHEX file might contain sections for "
  2245. "the different memory areas (progmem, eeprom, etc...).\n"
  2246. "\n"
  2247. "Should the sections be interpreted?\n"
  2248. "\n"
  2249. "If 'Yes' is selected, only the section corresponding "
  2250. "to the current buffer is extracted from the IHEX file.\n"
  2251. "If 'No' is selected, the IHEX file will be read "
  2252. "in raw mode and all of its data will be "
  2253. "put into the current buffer.",
  2254. QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
  2255. QMessageBox.StandardButton.Yes)
  2256. readRaw = (res != QMessageBox.StandardButton.Yes)
  2257. else:
  2258. readRaw = True
  2259. ok = bufWidget.setDataWithIHexInterpreter(interp, readRaw)
  2260. else:
  2261. ok = bufWidget.setRawData(handler.toBinary(dataIn))
  2262. if not ok:
  2263. QMessageBox.critical(self, "Failed to load data",
  2264. "Failed to load the file into the buffer")
  2265. except (TOPException) as e:
  2266. QMessageBox.critical(self, "Failed to convert data",
  2267. "Failed to convert the input file data to binary\n%s" % str(e))
  2268. return
  2269. def saveBuffer(self):
  2270. bufWidget = self.bufferTab.getCurrentBuffer()
  2271. if not bufWidget:
  2272. return
  2273. data = bufWidget.getRawData()
  2274. if not data:
  2275. return
  2276. (fn, selectedFilter) = QFileDialog.getSaveFileName(self,
  2277. "%s - save file" % bufWidget.getName(),
  2278. "",
  2279. "Intel hex file (*.ihex *.hex);;"
  2280. "Hex file with ASCII dump (*.ahex);;"
  2281. "Binary file (*)")
  2282. if not fn:
  2283. return
  2284. extensions = str(selectedFilter).split("(")[1].\
  2285. split(")")[0].replace("*", "").strip().split()
  2286. if not extensions:
  2287. extensions = [ "" ]
  2288. if not '.' in fn or\
  2289. not '.' + fn.split('.')[-1] in extensions:
  2290. fn += extensions[0] # Default ext
  2291. if ".ihex" in extensions:
  2292. data = IO_ihex().fromBinary(data)
  2293. elif ".ahex" in extensions:
  2294. data = IO_ahex().fromBinary(data)
  2295. elif not extensions[0]:
  2296. data = IO_binary().fromBinary(data)
  2297. else:
  2298. assert(0)
  2299. try:
  2300. if isinstance(data, str):
  2301. data = data.encode("UTF-8")
  2302. open(fn, "wb").write(data)
  2303. except (IOError, UnicodeError) as e:
  2304. QMessageBox.critical(self, "Failed to write file",
  2305. "Failed to write %s:\n%s" %\
  2306. (str(fn), str(e.strerror)))
  2307. return
  2308. def selectChip(self):
  2309. dlg = ChipSelectDialog(self)
  2310. if dlg.exec() != QDialog.DialogCode.Accepted:
  2311. return
  2312. chipDescription = dlg.getSelectedChip()
  2313. if not chipDescription:
  2314. return
  2315. self.currentChipDescription = chipDescription
  2316. return self.runOperation(Operation(Operation.OP_INITCHIP,
  2317. HwThread.TASK_INITCHIP,
  2318. chipDescription.chipID))
  2319. def __makeReadCallChain(self, mayReadSig=True, mayReadProgmem=True,
  2320. mayReadEEPROM=True, mayReadFuse=True,
  2321. mayReadLockbits=True, mayReadRAM=True):
  2322. callChain = []
  2323. suppFlags = self.chip.getSupportFlags()
  2324. callChain.append(GenericTopCall("top.checkChip()",
  2325. name = "check"))
  2326. if mayReadSig and (suppFlags & Chip.SUPPORT_SIGREAD):
  2327. callChain.append(GenericTopCall("top.readSignature()",
  2328. name = "signature"))
  2329. if mayReadProgmem and (suppFlags & Chip.SUPPORT_PROGMEMREAD):
  2330. callChain.append(GenericTopCall("top.readProgmem()",
  2331. name = "progmem"))
  2332. if mayReadEEPROM and (suppFlags & Chip.SUPPORT_EEPROMREAD):
  2333. callChain.append(GenericTopCall("top.readEEPROM()",
  2334. name = "eeprom"))
  2335. if mayReadFuse and (suppFlags & Chip.SUPPORT_FUSEREAD):
  2336. callChain.append(GenericTopCall("top.readFuse()",
  2337. name = "fusebits"))
  2338. if mayReadLockbits and (suppFlags & Chip.SUPPORT_LOCKREAD):
  2339. callChain.append(GenericTopCall("top.readLockbits()",
  2340. name = "lockbits"))
  2341. if mayReadRAM and (suppFlags & Chip.SUPPORT_RAMREAD):
  2342. callChain.append(GenericTopCall("top.readRAM()",
  2343. name = "ram"))
  2344. return callChain
  2345. def __readCallChain_GetResultImages(self, callChain):
  2346. images = {}
  2347. for topCall in (callChain if callChain else []):
  2348. name = topCall.userData["name"]
  2349. if name in ("signature", "progmem", "eeprom",
  2350. "fusebits", "lockbits", "ram"):
  2351. images[name] = topCall.result
  2352. elif name == "check":
  2353. pass # Nothing to do
  2354. else:
  2355. assert(0)
  2356. return images
  2357. def readChip(self):
  2358. return self.runOperation(Operation(Operation.OP_READALL,
  2359. HwThread.TASK_GENERICTOPCALL,
  2360. self.__makeReadCallChain()))
  2361. def eraseChip(self):
  2362. return self.runOperation(Operation(Operation.OP_ERASE,
  2363. HwThread.TASK_GENERICTOPCALL,
  2364. GenericTopCall("top.eraseChip()")))
  2365. def writeChipProgmem(self):
  2366. bufferWidget = self.bufferTab.progmemBuffer
  2367. data = bufferWidget.getRawData()
  2368. if not bufferWidget.isAvailable() or not data:
  2369. QMessageBox.critical(self, "No program memory",
  2370. "No program memory available")
  2371. return False
  2372. return self.runOperation(Operation(Operation.OP_WRITEPROG,
  2373. HwThread.TASK_GENERICTOPCALL,
  2374. GenericTopCall("top.writeProgmem(...)", data)))
  2375. def writeChipEeprom(self):
  2376. bufferWidget = self.bufferTab.eepromBuffer
  2377. data = bufferWidget.getRawData()
  2378. if not bufferWidget.isAvailable() or not data:
  2379. QMessageBox.critical(self, "No (E)EPROM memory",
  2380. "No (E)EPROM memory available")
  2381. return False
  2382. return self.runOperation(Operation(Operation.OP_WRITEEPROM,
  2383. HwThread.TASK_GENERICTOPCALL,
  2384. GenericTopCall("top.writeEEPROM(...)", data)))
  2385. def writeChipFuses(self):
  2386. bufferWidget = self.bufferTab.fuseBuffer
  2387. data = bufferWidget.getRawData()
  2388. if not bufferWidget.isAvailable() or not data:
  2389. QMessageBox.critical(self, "No fuse bits",
  2390. "No fuse bits available")
  2391. return False
  2392. return self.runOperation(Operation(Operation.OP_WRITEFUSE,
  2393. HwThread.TASK_GENERICTOPCALL,
  2394. GenericTopCall("top.writeFuse(...)", data)))
  2395. def writeChipLockbits(self):
  2396. bufferWidget = self.bufferTab.lockBuffer
  2397. data = bufferWidget.getRawData()
  2398. if not bufferWidget.isAvailable() or not data:
  2399. QMessageBox.critical(self, "No lock bits",
  2400. "No lock bits available")
  2401. return False
  2402. return self.runOperation(Operation(Operation.OP_WRITELOCK,
  2403. HwThread.TASK_GENERICTOPCALL,
  2404. GenericTopCall("top.writeLockbits(...)", data)))
  2405. def writeRam(self):
  2406. bufferWidget = self.bufferTab.ramBuffer
  2407. data = bufferWidget.getRawData()
  2408. if not bufferWidget.isAvailable() or not data:
  2409. QMessageBox.critical(self, "No RAM memory",
  2410. "No RAM memory available")
  2411. return False
  2412. return self.runOperation(Operation(Operation.OP_WRITERAM,
  2413. HwThread.TASK_GENERICTOPCALL,
  2414. GenericTopCall("top.writeRAM(...)", data)))
  2415. def runTest(self):
  2416. return self.runOperation(Operation(Operation.OP_TEST,
  2417. HwThread.TASK_GENERICTOPCALL,
  2418. GenericTopCall("top.testChip()")))
  2419. def verifyChip(self):
  2420. return self.verifySelectedMemories() # defaults to "all".
  2421. def verifySelectedMemories(self, verifyProgmem=True,
  2422. verifyEEPROM=True,
  2423. verifyFuse=True,
  2424. verifyRAM=True):
  2425. callChain = self.__makeReadCallChain(
  2426. mayReadSig = False,
  2427. mayReadProgmem = verifyProgmem,
  2428. mayReadEEPROM = verifyEEPROM,
  2429. mayReadFuse = verifyFuse,
  2430. mayReadLockbits = False,
  2431. mayReadRAM = verifyRAM)
  2432. return self.runOperation(Operation(Operation.OP_VERIFY,
  2433. HwThread.TASK_GENERICTOPCALL,
  2434. callChain))
  2435. def guiUpdateEnable(self):
  2436. enable = not self.guiDisable and\
  2437. not self.threadRunning and\
  2438. not self.autorun.isRunning()
  2439. self.menuBar().setEnabled(enable)
  2440. self.topToolBar.setEnabled(enable)
  2441. self.rightToolBar.setEnabled(enable)
  2442. self.autorun.setEnabled(enable)
  2443. self.bufferTab.setEnabled(enable)
  2444. def cancelHardwareTask(self):
  2445. if self.autorun.isRunning():
  2446. self.autorun.abortRun()
  2447. if self.threadRunning:
  2448. self.hwThread.cancelTask()
  2449. def __triggerOperation(self, op, threadMutexIsLocked=False):
  2450. self.hwThread.triggerTask(op.hwTask, op.hwTaskParam,
  2451. id(op), threadMutexIsLocked)
  2452. self.threadRunning = True
  2453. self.runningOps[id(op)] = op
  2454. self.console.setTaskRunning(True)
  2455. self.guiUpdateEnable()
  2456. def __triggerNextOperation(self, threadMutexIsLocked=False):
  2457. assert(self.queuedOps)
  2458. op = self.queuedOps.pop(0)
  2459. self.__triggerOperation(op, threadMutexIsLocked)
  2460. def runOperation(self, op):
  2461. self.queuedOps.append(op)
  2462. if not self.threadRunning:
  2463. self.__triggerNextOperation()
  2464. return True
  2465. def runOperationSync(self, hwTask, taskParameter=None):
  2466. op = Operation(Operation.OP_SYNCHRONOUS,
  2467. hwTask, taskParameter)
  2468. self.runOperation(op)
  2469. while not op.finished:
  2470. QApplication.processEvents(QEventLoop.ProcessEventsFlag.AllEvents, 50)
  2471. self.runningOps.pop(id(op))
  2472. return (op.failed, op.returnValue)
  2473. def setOperationFinished(self, failed):
  2474. self.console.setTaskRunning(running=False, success=not failed)
  2475. if self.autorun.isRunning():
  2476. if failed:
  2477. QTimer.singleShot(0, self.autorun.abortRunWithMessage)
  2478. else:
  2479. QTimer.singleShot(0, self.autorun.runNextAction)
  2480. def __hardwareTaskFinished(self, op):
  2481. if op.op == Operation.OP_SYNCHRONOUS:
  2482. self.console.setTaskRunning(running=False, success=True)
  2483. return
  2484. self.runningOps.pop(id(op))
  2485. if op.failed:
  2486. self.console.showMessage("[op %d task %d failed] %s\n" %\
  2487. (op.op, op.hwTask, str(op.returnValue)),
  2488. bold=True)
  2489. self.setOperationFinished(failed=True)
  2490. return
  2491. # Task succeed
  2492. if op.op == Operation.OP_INITCHIP:
  2493. assert(op.hwTask == HwThread.TASK_INITCHIP)
  2494. (self.chip, asciiArtLayout) = op.returnValue
  2495. self.bufferTab.setupBuffers(self.chip.getSupportFlags())
  2496. self.bufferTab.infoBuffer.clear()
  2497. self.bufferTab.infoBuffer.setupDescription(self.currentChipDescription)
  2498. self.bufferTab.infoBuffer.setChipLayout(asciiArtLayout)
  2499. self.autorun.setup(self.chip.getSupportFlags())
  2500. self.topToolBar.setup(self.chip.getSupportFlags())
  2501. self.rightToolBar.setup(self.chip.getSupportFlags())
  2502. self.setupRunMenu(self.chip.getSupportFlags())
  2503. elif op.op == Operation.OP_SHUTDOWN:
  2504. pass # Nothing to do
  2505. elif op.op == Operation.OP_READALL:
  2506. assert(op.hwTask == HwThread.TASK_GENERICTOPCALL)
  2507. topCallList = op.returnValue
  2508. images = self.__readCallChain_GetResultImages(topCallList)
  2509. self.bufferTab.setupBuffers(self.chip.getSupportFlags())
  2510. self.bufferTab.loadBuffers(images)
  2511. try:
  2512. self.bufferTab.infoBuffer.setChipSignature(images["signature"])
  2513. except KeyError:
  2514. pass
  2515. elif op.op == Operation.OP_ERASE:
  2516. pass # Nothing to do
  2517. elif op.op == Operation.OP_WRITEPROG:
  2518. pass # Nothing to do
  2519. elif op.op == Operation.OP_WRITEEPROM:
  2520. pass # Nothing to do
  2521. elif op.op == Operation.OP_WRITEFUSE:
  2522. pass # Nothing to do
  2523. elif op.op == Operation.OP_WRITELOCK:
  2524. pass # Nothing to do
  2525. elif op.op == Operation.OP_WRITERAM:
  2526. pass # Nothing to do
  2527. elif op.op == Operation.OP_TEST:
  2528. self.console.showMessage("Unit-test success. The chip seems to be OK.\n",
  2529. bold=True)
  2530. elif op.op == Operation.OP_VERIFY:
  2531. assert(op.hwTask == HwThread.TASK_GENERICTOPCALL)
  2532. topCallList = op.returnValue
  2533. images = self.__readCallChain_GetResultImages(topCallList)
  2534. op.failed = not self.bufferTab.verifyBuffers(images)
  2535. elif op.op == Operation.OP_RAWCOMMAND:
  2536. self.console.showMessage("Successfully sent raw command\n", bold=True)
  2537. else:
  2538. print("ERROR: No handler for op %d task %d" % (op.op, op.hwTask))
  2539. self.setOperationFinished(op.failed)
  2540. def hardwareTaskFinished(self, opaqueId, failed, returnValue):
  2541. if opaqueId is None:
  2542. return
  2543. op = self.runningOps[opaqueId]
  2544. op.returnValue = returnValue
  2545. op.failed = failed
  2546. self.threadRunning = False
  2547. self.__hardwareTaskFinished(op)
  2548. op.op = Operation.OP_NONE
  2549. op.finished = True
  2550. if self.queuedOps:
  2551. self.__triggerNextOperation(threadMutexIsLocked=True)
  2552. return
  2553. self.guiUpdateEnable()
  2554. def main():
  2555. app = QApplication(sys.argv)
  2556. mainwnd = MainWindow()
  2557. mainwnd.show()
  2558. mainwnd.resize(int(mainwnd.width() * 1.6),
  2559. int(mainwnd.height() * 1.2))
  2560. return app.exec()
  2561. if __name__ == "__main__":
  2562. sys.exit(main())