openwch / openocd_wch

OpenOCD for WCH CH32
12 stars 1 forks source link

Incorrect memory erase #2

Open MikeSmith78 opened 5 months ago

MikeSmith78 commented 5 months ago

My firmware stores user-data to flash memory at address 0x08030000. But this memory is erased after starting hardware debugging. I see 0xE339E339... instead my data. Probably at the beginning of debugging the mass-erase command is executed.

I found sources of OpenOCD_wch, and I see this code:

static int ch32vx_erase(struct flash_bank* bank, int first, int last)
{
  if (pageerase)
    return ERROR_OK;
  if ((riscvchip == 5) || (riscvchip == 6) || (riscvchip == 9)|| (riscvchip == 0x0c)||(riscvchip==0x0e))
  {
    int retval = wlnik_protect_check();
    if (retval == 4)
    {
        LOG_ERROR("Read-Protect Status Currently Enabled");
        return ERROR_FAIL;
    }
  }
  if (noloadflag)
    return ERROR_OK;
  int ret = wlink_erase();
  target_halt(bank->target);
  if (ret)
    return ERROR_OK;
  else
    return ERROR_FAIL;
  return ERROR_OK;
}

Parameters first and last store the beginning and end of the memory to be erased. But they are not used!!! The debugger passes the memory address to erase to function ch32vx_erase, but every time all memory is erased (used mass erase).

Please implement the ch32vx_erase function completely, as done in stm32x_erase, for example.

Used tools:

kossnikita commented 1 month ago

Could you tell where you found the sources?

MikeSmith78 commented 1 month ago

https://github.com/search?q=openocd+wch&type=repositories https://github.com/karlp/openocd-hacks