if (sl->flash_type == FLASH_TYPE_F4) {
cr_reg = FLASH_F4_CR;
- cr_lock_shift = STM32L4_FLASH_CR_LOCK;
+ cr_lock_shift = FLASH_F4_CR_LOCK;
} else if (sl->flash_type == FLASH_TYPE_L4) {
cr_reg = STM32L4_FLASH_CR;
cr_lock_shift = STM32L4_FLASH_CR_LOCK;
void stlink_close(stlink_t *sl) {
DLOG("*** stlink_close ***\n");
+ if (!sl)
+ return;
sl->backend->close(sl);
free(sl);
}
if (ret == -1)
return ret;
- if (chip_id == 0)
+ if (*chip_id == 0)
ret = stlink_read_debug32(sl, 0x40015800, chip_id); //Try Corex M0 DBGMCU_IDCODE register address
return ret;
return -1;
}
+ if (params->flash_type == FLASH_TYPE_UNKNOWN) {
+ WLOG("Invalid flash type, please check device declaration\n");
+ return -1;
+ }
+
+
// These are fixed...
sl->flash_base = STM32_FLASH_BASE;
sl->sram_base = STM32_SRAM_BASE;
DLOG(" notice: the firmware doesn't support a swim interface\n");
}
- return -1;
+ return 0;
}
int stlink_target_voltage(stlink_t *sl) {
ret = sl->backend->read_debug32(sl, addr, data);
if (!ret)
- DLOG("*** stlink_read_debug32 %x is %#x\n", data, addr);
+ DLOG("*** stlink_read_debug32 %x is %#x\n", *data, addr);
return ret;
}
flashaddr -= STM32_FLASH_BASE;
if (flashopt & (1lu << STM32L4_FLASH_OPTR_DUALBANK)) {
uint32_t banksize = sl->flash_size / 2;
- if (flashaddr > banksize) {
+ if (flashaddr >= banksize) {
flashaddr -= banksize;
bker = 0x100;
}
// calculate the actual bank+page from the address
uint32_t page = calculate_L4_page(sl, flashaddr);
+ fprintf(stderr, "EraseFlash - Page:0x%x Size:0x%x ", page, stlink_calculate_pagesize(sl, flashaddr));
+
write_flash_cr_bker_pnb(sl, page);
} else if (sl->chip_id == STM32_CHIPID_F7) {
// calculate the actual page from the address
uint32_t sector=calculate_F7_sectornum(flashaddr);
- fprintf(stderr, "EraseFlash - Sector:0x%x Size:0x%x\n", sector, stlink_calculate_pagesize(sl, flashaddr));
+ fprintf(stderr, "EraseFlash - Sector:0x%x Size:0x%x ", sector, stlink_calculate_pagesize(sl, flashaddr));
write_flash_cr_snb(sl, sector);
} else {
// calculate the actual page from the address
uint32_t sector=calculate_F4_sectornum(flashaddr);
- fprintf(stderr, "EraseFlash - Sector:0x%x Size:0x%x\n", sector, stlink_calculate_pagesize(sl, flashaddr));
+ fprintf(stderr, "EraseFlash - Sector:0x%x Size:0x%x ", sector, stlink_calculate_pagesize(sl, flashaddr));
//the SNB values for flash sectors in the second bank do not directly follow the values for the first bank on 2mb devices...
if (sector >= 12) sector += 4;
WLOG("Failed to erase_flash_page(%#zx) == -1\n", addr);
return -1;
}
- fprintf(stdout,"\rFlash page at %5d/%5d erased", i, num_pages);
+ fprintf(stdout,"-> Flash page at %5d/%5d erased\n", i, num_pages);
fflush(stdout);
}
fprintf(stdout, "\n");
0xfb, 0xd1, // bne.n <wait>
0x00, 0xf1, 0x08, 0x00, // add.w r0, r0, #8
0x01, 0xf1, 0x08, 0x01, // add.w r1, r1, #8
- 0xa2, 0xf1, 0x02, 0x02, // add.w r2, r2, #2
+ 0xa2, 0xf1, 0x01, 0x02, // sub.w r2, r2, #1
0xef, 0xe7, // b.n <next>
0x00, 0xbe, // done: bkpt 0x0000
0x00, 0x20, 0x02, 0x40 // flash_base: .word 0x40022000
reg rr;
int i = 0;
- size_t count;
+ size_t count = 0;
DLOG("Running flash loader, write address:%#x, size: %zd\n", target, size);
// FIXME This can never return -1
count = size / sizeof(uint16_t);
if (size % sizeof(uint16_t))
++count;
- } else if (sl->flash_type == FLASH_TYPE_F4 ||
- sl->flash_type == FLASH_TYPE_L4 ||
- sl->flash_type == FLASH_TYPE_L0) {
+ } else if (sl->flash_type == FLASH_TYPE_F4 || sl->flash_type == FLASH_TYPE_L0) {
count = size / sizeof(uint32_t);
if (size % sizeof(uint32_t))
++count;
- if (sl->chip_id == STM32_CHIPID_L4) {
- if (count % 2)
- ++count;
- }
- } else {
- fprintf(stderr, "unknown coreid 0x%x, don't know what flash loader to use\n", sl->core_id);
- return -1;
+ } else if (sl->flash_type == FLASH_TYPE_L4) {
+ count = size / sizeof(uint64_t);
+ if (size % sizeof(uint64_t))
+ ++count;
}
/* setup core */
}
/* check written byte count */
- stlink_read_all_regs(sl, &rr);
+ stlink_read_reg(sl, 2, &rr);
if (rr.r[2] != 0) {
fprintf(stderr, "write error, count == %u\n", rr.r[2]);
return -1;