st-info: Add support for OpenOCD hla_serial printing
[fw/stlink] / src / stlink-common.c
index 76413b4fdca7009df03b7dd8b9ed5b6c18db38b5..18c76fb86c109b242d32c7f8080a88d67941ca60 100644 (file)
@@ -251,7 +251,7 @@ static void lock_flash(stlink_t *sl) {
 
     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;
@@ -481,6 +481,8 @@ static inline void write_flash_cr_bker_pnb(stlink_t *sl, uint32_t n) {
 
 void stlink_close(stlink_t *sl) {
     DLOG("*** stlink_close ***\n");
+    if (!sl)
+         return;
     sl->backend->close(sl);
     free(sl);
 }
@@ -534,7 +536,7 @@ int stlink_chip_id(stlink_t *sl, uint32_t *chip_id) {
     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;
@@ -591,6 +593,12 @@ int stlink_load_device_params(stlink_t *sl) {
         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;
@@ -702,7 +710,7 @@ int stlink_version(stlink_t *sl) {
         DLOG("    notice: the firmware doesn't support a swim interface\n");
     }
 
-    return -1;
+    return 0;
 }
 
 int stlink_target_voltage(stlink_t *sl) {
@@ -726,7 +734,7 @@ int stlink_read_debug32(stlink_t *sl, uint32_t addr, uint32_t *data) {
 
     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;
 }
@@ -1191,7 +1199,7 @@ uint32_t calculate_L4_page(stlink_t *sl, uint32_t flashaddr) {
     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;
         }
@@ -1242,19 +1250,21 @@ int stlink_erase_flash_page(stlink_t *sl, stm32_addr_t flashaddr)
             // 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;
@@ -1384,7 +1394,7 @@ int stlink_erase_flash_mass(stlink_t *sl) {
                 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");
@@ -2063,7 +2073,7 @@ int run_flash_loader(stlink_t *sl, flash_loader_t* fl, stm32_addr_t target, cons
 
     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
@@ -2085,9 +2095,6 @@ int run_flash_loader(stlink_t *sl, flash_loader_t* fl, stm32_addr_t target, cons
         count = size / sizeof(uint64_t);
         if (size % sizeof(uint64_t))
             ++count;
-    } else {
-        fprintf(stderr, "unknown coreid 0x%x, don't know what flash loader to use\n", sl->core_id);
-        return -1;
     }
 
     /* setup core */