toprammer-gui 80 KB

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