Skip to content

Commit

Permalink
hc32l110: implement timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
vesim987 committed Sep 16, 2022
1 parent dd9247b commit a9cf541
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions src/target/hc32l110.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ static void hc32l110_bypass(target *t)
target_mem_write32(t, HC32L110_FLASH_BYPASS_ADDR, 0xa5a5);
}

static int hc32l110_check_flash_completion(target *t, unsigned int timeout)
static int hc32l110_check_flash_completion(target *t, unsigned int timeout_ms)
{
// TODO: timeouts
(void)timeout;

while (1) {
uint32_t v = target_mem_read32(t, HC32L110_FLASH_CR_ADDR);
struct platform_timeout timeout;
platform_timeout_set(&timeout, timeout_ms);
uint32_t v = 0xFFFFFFFF;
do {
v = target_mem_read32(t, HC32L110_FLASH_CR_ADDR);
if ((v & HC32L110_FLASH_CR_BUSY) == 0)
break;
}
return true;
} while (!platform_timeout_is_expired(&timeout));

return 0;
return false;
}

// lock the whole flash
Expand Down Expand Up @@ -112,7 +112,6 @@ static bool hc32l110_flash_prepare(struct target_flash *f, flash_operation_e op)
return false;
}

hc32l110_check_flash_completion(f->t, 1000);
hc32l110_slock_unlock_all(f->t);

return true;
Expand All @@ -122,7 +121,6 @@ static bool hc32l110_flash_done(struct target_flash *f, flash_operation_e op)
{
(void)op;
hc32l110_slock_lock_all(f->t);
hc32l110_check_flash_completion(f->t, 1000);

return true;
}
Expand All @@ -132,7 +130,10 @@ static bool hc32l110_flash_erase(struct target_flash *f, target_addr_t addr, siz
(void)len;
// the MCU is automatically erasing the whole sector after one write operation
target_mem_write32(f->t, addr, 0);
hc32l110_check_flash_completion(f->t, 2000);
if (!hc32l110_check_flash_completion(f->t, 1000)) {
DEBUG_WARN("Failed to erase %d bytes at 0x%.8X", len, addr);
return false;
}

return true;
}
Expand All @@ -142,7 +143,10 @@ static bool hc32l110_flash_write(struct target_flash *f, target_addr_t dest, con
for (target_addr_t addr = 0; addr < len; addr += 4) {
uint32_t val = ((const uint32_t *)src)[addr / 4];
target_mem_write32(f->t, addr + dest, val);
hc32l110_check_flash_completion(f->t, 2000);
if (!hc32l110_check_flash_completion(f->t, 1000)) {
DEBUG_WARN("Failed to write %d bytes at 0x%.8X", len, dest);
return false;
}
}

return true;
Expand All @@ -154,13 +158,16 @@ static bool hc32l110_mass_erase(target *t)

hc32l110_bypass(t);
target_mem_write32(t, HC32L110_FLASH_CR_ADDR, HC32L110_FLASH_CR_OP_ERASE_CHIP);
hc32l110_check_flash_completion(t, 1000);
hc32l110_check_flash_completion(t, 500);

hc32l110_slock_unlock_all(t);

// the MCU is automatically erasing the whole flash after one write operation
target_mem_write32(t, 0, 0);
hc32l110_check_flash_completion(t, 4000);
if (!hc32l110_check_flash_completion(t, 4000)) {
DEBUG_WARN("Failed to mass erase");
return false;
}

hc32l110_slock_lock_all(t);

Expand Down

0 comments on commit a9cf541

Please sign in to comment.