|
| 1 | +import sys,os |
| 2 | +from PySide6.QtWidgets import QFileDialog, QApplication, QMainWindow |
| 3 | +from pyocd.core.helpers import ConnectHelper |
| 4 | +from pyocd.core.target import Target |
| 5 | +from pyocd.core.memory_map import MemoryType |
| 6 | +from pyocd.coresight.cortex_m import CortexM |
| 7 | +from pyocd.flash.file_programmer import FileProgrammer |
| 8 | +from pyocd.tools.lists import ListGenerator |
| 9 | +from MCUProg_ui import Ui_MainWindow |
| 10 | + |
| 11 | +class MainWindow(QMainWindow): |
| 12 | + file_path = '' |
| 13 | + allProbes = None |
| 14 | + Probe = None |
| 15 | + session = None |
| 16 | + frequency = {'10MHZ':10000000,'5MHZ':5000000,'2MHZ':2000000,'1MHZ':1000000,'500kHZ':500000,'200kHZ':200000,'100kHZ':100000,'50kHZ':50000,'20kHZ':20000,'10kHZ':10000,'5kHZ':5000} |
| 17 | + def __init__(self, parent = None) : |
| 18 | + super().__init__(parent) |
| 19 | + self.ui = Ui_MainWindow() |
| 20 | + self.ui.setupUi(self) |
| 21 | + self.ui.file_selection_button.clicked.connect(self.file_selection_button_click) |
| 22 | + self.ui.usb_connect_button.clicked.connect(self.usb_connect_button_click) |
| 23 | + self.ui.flash_button.clicked.connect(self.flash_button_click) |
| 24 | + self.ui.usb_comboBox.pop_up.connect(self.usb_selection) |
| 25 | + self.ui.speed_comboBox.addItems(['10MHZ','5MHZ','2MHZ','1MHZ','500kHZ','200kHZ','100kHZ','50kHZ','20kHZ','10kHZ','5kHZ']) |
| 26 | + self.usb_probe() |
| 27 | + list_targets = ListGenerator.list_targets() |
| 28 | + pyocd_version = list_targets['pyocd_version'] |
| 29 | + targets = list_targets['targets'] |
| 30 | + print(pyocd_version) |
| 31 | + for target in targets: |
| 32 | + # print(target['name']) |
| 33 | + self.ui.targets_comboBox.addItem(target['name']) |
| 34 | + |
| 35 | + def usb_selection(self): |
| 36 | + self.usb_probe() |
| 37 | + |
| 38 | + def file_selection_button_click(self): |
| 39 | + self.file_path, _ = QFileDialog.getOpenFileName(self, "选择下载文件",self.file_path or os.getcwd(),'(*.bin *.hex *.elf *.axf)') |
| 40 | + self.ui.file_lineEdit.setText(self.file_path) |
| 41 | + def usb_connect_button_click(self): |
| 42 | + print("usb_connect_button_click") |
| 43 | + if self.ui.usb_comboBox.currentText(): |
| 44 | + if self.session and self.session.is_open: |
| 45 | + self.session.close() |
| 46 | + self.ui.usb_connect_button.setText("连接") |
| 47 | + self.ui.usb_comboBox.setEnabled(True) |
| 48 | + else: |
| 49 | + for Probe in self.allProbes: |
| 50 | + if self.ui.usb_comboBox.currentText() == Probe.description: |
| 51 | + self.Probe = Probe |
| 52 | + break |
| 53 | + # print(self.ui.usb_comboBox.currentText()) |
| 54 | + print(Probe,Probe.unique_id) |
| 55 | + # print(self.ui.targets_comboBox.currentText()) |
| 56 | + # print(self.frequency[self.ui.speed_comboBox.currentText()]) |
| 57 | + if self.Probe: |
| 58 | + self.session = ConnectHelper.session_with_chosen_probe(blocking=False, return_first=False, unique_id=self.Probe.unique_id,options = {"frequency": self.frequency[self.ui.speed_comboBox.currentText()], "target_override": self.ui.targets_comboBox.currentText()}) |
| 59 | + if self.session: |
| 60 | + self.session.open() |
| 61 | + board = self.session.board |
| 62 | + print("Board MSG:") |
| 63 | + print("Board's name:%s" % board.name) |
| 64 | + print("Board's description:%s" % board.description) |
| 65 | + print("Board's target_type:%s" % board.target_type) |
| 66 | + print("Board's unique_id:%s" % board.unique_id) |
| 67 | + print("Board's test_binary:%s" % board.test_binary) |
| 68 | + print("Unique ID: %s" % board.unique_id) |
| 69 | + target = board.target |
| 70 | + print("Part number:%s" % target.part_number) |
| 71 | + memory_map = target.get_memory_map() |
| 72 | + ram_region = memory_map.get_default_region_of_type(MemoryType.RAM) |
| 73 | + rom_region = memory_map.get_boot_memory() |
| 74 | + print("menory map: ",memory_map) |
| 75 | + print("ram_region: ",ram_region) |
| 76 | + print("rom_region: ",rom_region) |
| 77 | + self.ui.usb_connect_button.setText("断开") |
| 78 | + self.ui.usb_comboBox.setEnabled(False) |
| 79 | + def progress(self, Value): |
| 80 | + self.ui.flash_progressBar.setValue(Value*100) |
| 81 | + def flash_button_click(self): |
| 82 | + print("flash_button_click") |
| 83 | + self.ui.flash_progressBar.reset() |
| 84 | + if self.session and self.session.is_open : |
| 85 | + board = self.session.board |
| 86 | + target = board.target |
| 87 | + # Load firmware into device. |
| 88 | + FileProgrammer(self.session,chip_erase="auto",smart_flash=False,trust_crc=False,keep_unwritten=True,no_reset=True,progress = self.progress).program(self.file_path) |
| 89 | + # Reset |
| 90 | + target.reset_and_halt() |
| 91 | + |
| 92 | + def usb_probe(self): |
| 93 | + self.allProbes = ConnectHelper.get_all_connected_probes(False, None, False) |
| 94 | + self.ui.usb_comboBox.clear() |
| 95 | + if self.allProbes is None or len(self.allProbes) == 0: |
| 96 | + print("No Probe",self.allProbes) |
| 97 | + else: |
| 98 | + # print(self.allProbes) |
| 99 | + for Probe in self.allProbes: |
| 100 | + print(Probe) |
| 101 | + print(Probe.description) |
| 102 | + print(Probe.unique_id) |
| 103 | + self.ui.usb_comboBox.addItem(Probe.description) |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == '__main__': |
| 108 | + app = QApplication(sys.argv) |
| 109 | + win = MainWindow() |
| 110 | + win.setWindowTitle("MCUProg") |
| 111 | + win.show() |
| 112 | + app.exit(app.exec()) |
| 113 | + |
| 114 | + |
| 115 | + |
0 commit comments