diff --git a/mik32_gpio.py b/mik32_gpio.py new file mode 100644 index 0000000..2c146c1 --- /dev/null +++ b/mik32_gpio.py @@ -0,0 +1,83 @@ +# -------------------------- +# PM register offset +# -------------------------- +from enum import Enum +from tclrpc import OpenOcdTclRpc + + +class MIK_VERSION(Enum): + MIK32V0 = "MIK32V0" + MIK32V2 = "MIK32V2" + + def __str__(self): + return self.value + + +PAD_CONFIG_BASE_ADDRESS = 0x00050C00 + +class PAD_CONFIG_REGS_V0(Enum): + PORT_0_CFG = 0x00 + PORT_1_CFG = 0x04 + PORT_2_CFG = 0x08 + PORT_0_DS = 0x0C + PORT_1_DS = 0x10 + PORT_2_DS = 0x14 + PORT_0_PUD = 0x18 + PORT_1_PUD = 0x1C + PORT_2_PUD = 0x20 + +class PAD_CONFIG_REGS_V2(Enum): + PORT_0_CFG = 0x00 + PORT_0_DS = 0x04 + PORT_0_PUD = 0x08 + PORT_1_CFG = 0x0C + PORT_1_DS = 0x10 + PORT_1_PUD = 0x14 + PORT_2_CFG = 0x18 + PORT_2_DS = 0x1C + PORT_2_PUD = 0x20 + + +port2_value = 0 + + +def gpio_init(openocd: OpenOcdTclRpc, version: MIK_VERSION): + + port2_addr = 0 + if version == MIK_VERSION.MIK32V0: + port2_addr = PAD_CONFIG_BASE_ADDRESS + PAD_CONFIG_REGS_V0.PORT_2_CFG.value + elif version == MIK_VERSION.MIK32V2: + port2_addr = PAD_CONFIG_BASE_ADDRESS + PAD_CONFIG_REGS_V2.PORT_2_CFG.value + else: + return + + openocd.halt() + port2_value = openocd.read_memory(port2_addr, 32, 1)[0] + + port2_value_updated = port2_value + + port2_value_updated &= 0xF000 + + if version == MIK_VERSION.MIK32V0: + port2_value_updated |= 0x000 + elif version == MIK_VERSION.MIK32V2: + port2_value_updated |= 0x555 + else: + return + + + openocd.write_word(port2_addr, port2_value_updated) + + openocd.write_word(port2_addr + 8, 0x0500) + + +def gpio_deinit(openocd: OpenOcdTclRpc, version: MIK_VERSION): + + if version == MIK_VERSION.MIK32V0: + port2_addr = PAD_CONFIG_BASE_ADDRESS + PAD_CONFIG_REGS_V0.PORT_2_CFG.value + elif version == MIK_VERSION.MIK32V2: + port2_addr = PAD_CONFIG_BASE_ADDRESS + PAD_CONFIG_REGS_V2.PORT_2_CFG.value + else: + return + + openocd.write_word(port2_addr, port2_value) diff --git a/mik32_pm.py b/mik32_pm.py index 5c723e3..8556356 100644 --- a/mik32_pm.py +++ b/mik32_pm.py @@ -131,7 +131,7 @@ def pm_init(openocd: OpenOcdTclRpc): PM_CLOCK_APB_M_WU_M ) # 0x89 - APB_P_default = 0 + APB_P_default = PM_CLOCK_APB_P_GPIO_2_M # 0x00 openocd.halt() diff --git a/mik32_spifi.py b/mik32_spifi.py index 46ac203..bbc76c4 100644 --- a/mik32_spifi.py +++ b/mik32_spifi.py @@ -278,7 +278,7 @@ def spifi_send_command( data: List[int] = [], dma: Union[DMA, None] = None ) -> List[int]: - if dma is not None and direction == SPIFI_Direction.WRITE: + if (dma is not None) and (direction == SPIFI_Direction.WRITE): openocd.write_memory(0x02003F00, 8, data) dma.channels[0].start( @@ -286,28 +286,36 @@ def spifi_send_command( SPIFI_CONFIG_DATA32, 255 ) - elif dma is not None and direction == SPIFI_Direction.READ: + elif (dma is not None) and (direction == SPIFI_Direction.READ): dma.channels[1].start( SPIFI_CONFIG_DATA32, 0x02003F00, 255 ) - if address != 0: - openocd.write_word(SPIFI_CONFIG_ADDR, address) - if idata != 0: - openocd.write_word(SPIFI_CONFIG_IDATA, idata) - if cache_limit != 0: - openocd.write_word(SPIFI_CONFIG_CLIMIT, cache_limit) + openocd.write_word(SPIFI_CONFIG_ADDR, address) + # if idata != 0: + # openocd.write_word(SPIFI_CONFIG_IDATA, idata) + # if cache_limit != 0: + # openocd.write_word(SPIFI_CONFIG_CLIMIT, cache_limit) - # spifi_intrq_clear(openocd) - openocd.write_word(SPIFI_CONFIG_CMD, (cmd << SPIFI_CONFIG_CMD_OPCODE_S) | + # # spifi_intrq_clear(openocd) + # openocd.write_word(SPIFI_CONFIG_CMD, (cmd << SPIFI_CONFIG_CMD_OPCODE_S) | + # (frameform.value << SPIFI_CONFIG_CMD_FRAMEFORM_S) | + # (fieldform.value << SPIFI_CONFIG_CMD_FIELDFORM_S) | + # (byte_count << SPIFI_CONFIG_CMD_DATALEN_S) | + # (idata_length << SPIFI_CONFIG_CMD_INTLEN_S) | + # (direction.value << SPIFI_CONFIG_CMD_DOUT_S)) + # # spifi_wait_intrq_timeout(openocd, "Timeout executing write enable command") + + cmd_write_value = ((cmd << SPIFI_CONFIG_CMD_OPCODE_S) | (frameform.value << SPIFI_CONFIG_CMD_FRAMEFORM_S) | (fieldform.value << SPIFI_CONFIG_CMD_FIELDFORM_S) | (byte_count << SPIFI_CONFIG_CMD_DATALEN_S) | (idata_length << SPIFI_CONFIG_CMD_INTLEN_S) | (direction.value << SPIFI_CONFIG_CMD_DOUT_S)) - # spifi_wait_intrq_timeout(openocd, "Timeout executing write enable command") + + openocd.write_memory(SPIFI_CONFIG_CMD, 32, [cmd_write_value, address, idata, cache_limit]) if direction == SPIFI_Direction.READ: out_list = [] @@ -519,6 +527,10 @@ def write_pages(pages: Dict[int, List[int]], openocd: OpenOcdTclRpc, use_quad_sp openocd.halt() spifi_init(openocd) + JEDEC_ID = spifi_send_command(openocd, 0x9F, SPIFI_Frameform.OPCODE_NOADDR, SPIFI_Fieldform.ALL_SERIAL, 3) + + print(f"JEDEC_ID {JEDEC_ID[0]:02x} {JEDEC_ID[1]:02x} {JEDEC_ID[2]:02x}") + dma = DMA(openocd) dma.init() @@ -565,10 +577,16 @@ def write_pages(pages: Dict[int, List[int]], openocd: OpenOcdTclRpc, use_quad_sp else: spifi_erase(openocd, EraseType.SECTOR_ERASE, get_segments_list(list(pages), 4*1024)) - address = 0 + + # for addr in range(0, 4096*2, 256): + # result = spifi_read_data(openocd, addr, 256, [0xFF]*256, dma=dma) + + # if result == 1: + # print("Data error") + # return result if (use_quad_spi): - print("quad enable") + print("Quad Enable") spifi_quad_enable(openocd) else: spifi_quad_disable(openocd) diff --git a/mik32_upload.py b/mik32_upload.py index ab41a89..d3f84dc 100644 --- a/mik32_upload.py +++ b/mik32_upload.py @@ -7,6 +7,7 @@ import time from enum import Enum from typing import List, Dict, NamedTuple, Union from tclrpc import OpenOcdTclRpc, TclException +from mik32_gpio import MIK_VERSION, gpio_init, gpio_deinit import mik32_eeprom import mik32_spifi import mik32_ram @@ -219,25 +220,15 @@ class OpenOCDStartupException(Exception): return f"OpenOCD Startup Exception: {self.msg}" -adapter_speed_not_supported = [ - "altera-usb-blaster", -] - def run_openocd( openocd_exec=openocd_exec_path, openocd_scripts=openocd_scripts_path, openocd_interface=openocd_interface_path, openocd_target=openocd_target_path, - adapter_speed=adapter_default_speed, is_open_console=False ) -> subprocess.Popen: - cmd = [openocd_exec, "-s", openocd_scripts, "-f", openocd_interface] - - if (all(openocd_interface.find(i) == -1 for i in adapter_speed_not_supported)): - cmd.extend(["-c", f"adapter speed {adapter_speed}"]) - - cmd.extend(["-f", openocd_target]) - + cmd = [openocd_exec, "-s", openocd_scripts, + "-f", openocd_interface, "-f", openocd_target] creation_flags = subprocess.SW_HIDE if is_open_console: @@ -282,6 +273,11 @@ def form_pages(segments: List[Segment], boot_mode=BootMode.UNDEFINED) -> Pages: return Pages(pages_eeprom, pages_spifi) +adapter_speed_not_supported = [ + "altera-usb-blaster", +] + + def upload_file( filename: str, host: str = '127.0.0.1', @@ -296,7 +292,8 @@ def upload_file( is_open_console=False, boot_mode=BootMode.UNDEFINED, log_path=default_log_path, - post_action=default_post_action + post_action=default_post_action, + mik_version=MIK_VERSION.MIK32V2 ) -> int: """ Write ihex or binary file into MIK32 EEPROM or external flash memory @@ -304,6 +301,8 @@ def upload_file( @return: return 0 if successful, 1 if failed """ + print(f"Using {mik_version.value}") + result = 0 if not os.path.exists(filename): @@ -320,8 +319,8 @@ def upload_file( try: logging.debug("OpenOCD try start!") - proc = run_openocd(openocd_exec, openocd_scripts, openocd_interface, - openocd_target, adapter_speed, is_open_console) + proc = run_openocd(openocd_exec, openocd_scripts, + openocd_interface, openocd_target, is_open_console) logging.debug("OpenOCD started!") @@ -329,6 +328,8 @@ def upload_file( raise OpenOCDStartupException(e) try: with OpenOcdTclRpc(host, port) as openocd: + if (all(openocd_interface.find(i) == -1 for i in adapter_speed_not_supported)): + openocd.run(f"adapter speed {adapter_speed}") openocd.run(f"log_output \"{log_path}\"") openocd.run(f"debug_level 1") @@ -348,6 +349,7 @@ def upload_file( write_size = pages.pages_eeprom.__len__() * memory_page_size[MemoryType.EEPROM] print(f"Wrote {write_size} bytes in {write_time:.2f} seconds (effective {(write_size/(write_time*1024)):.1f} kbyte/s)") if (pages.pages_spifi.__len__() > 0): + gpio_init(openocd, mik_version) start_time = time.perf_counter() result |= mik32_spifi.write_pages( @@ -356,6 +358,7 @@ def upload_file( write_time = time.perf_counter() - start_time write_size = pages.pages_spifi.__len__() * memory_page_size[MemoryType.SPIFI] print(f"Wrote {write_size} bytes in {write_time:.2f} seconds (effective {(write_size/(write_time*1024)):.1f} kbyte/s)") + gpio_deinit(openocd, mik_version) segments_ram = list(filter( lambda segment: (segment.memory is not None) and (segment.memory.type == MemoryType.RAM), segments)) @@ -488,7 +491,16 @@ def createParser(): default=False, help='Вывод без последовательностей управления терминалом. Временно не используется' ) - + parser.add_argument( + '-t', + '--mcu-type', + dest='mcu_type', + type=MIK_VERSION, + choices=list(MIK_VERSION), + default=MIK_VERSION.MIK32V2, + help="Выбор микроконтроллера. " + f"По умолчанию: {MIK_VERSION.MIK32V2}" + ) return parser @@ -514,6 +526,7 @@ if __name__ == '__main__': boot_mode=namespace.boot_mode, log_path=namespace.log_path, post_action=namespace.post_action, + mik_version=namespace.mcu_type ) else: print("Nothing to upload")