[fix] stm32l flash write
authorFabien Le Mentec <texane@gmail.com>
Sat, 22 Oct 2011 21:07:13 +0000 (16:07 -0500)
committerFabien Le Mentec <texane@gmail.com>
Sat, 22 Oct 2011 21:07:13 +0000 (16:07 -0500)
example/blink_flash/Makefile
example/blink_flash/main.c
src/stlink-common.c
src/stlink-common.h
src/stlink-usb.c

index 77764715d9f42de214acc8895786329624ff49cb..2c74c32e7c67c67d48969d2c4d47d126df990b19 100644 (file)
@@ -8,7 +8,7 @@ CFLAGS=-O2 -mlittle-endian -mthumb
 
 CFLAGS=-g -O2 -mlittle-endian -mthumb
 ifeq ($(CONFIG_STM32L_DISCOVERY), 1)
-       CFLAGS+=-mcpu=cortex-m3 -DCONFIG_STM32L_DISCOVERY
+       CFLAGS+=-mcpu=cortex-m3 -DCONFIG_STM32L_DISCOVERY=1
 else ifeq ($(CONFIG_STM32VL_DISCOVERY), 1)
        CFLAGS+=-mcpu=cortex-m3 -DCONFIG_STM32VL_DISCOVERY=1
 else ifeq ($(CONFIG_STM32F4_DISCOVERY), 1)
index e93fd728a42d8f121d36530f9f9989db561ee187..6a893560e9d9ce1254c8270913b5fdd19acec16a 100644 (file)
@@ -5,10 +5,6 @@ typedef unsigned int uint32_t;
 
 /* hardware configuration */
 
-#define CONFIG_STM32L_DISCOVERY 1
-#define CONFIG_STM32VL_DISCOVERY 0
-
-
 #if CONFIG_STM32VL_DISCOVERY
 
 # define GPIOC 0x40011000 /* port C */
index 354bab1639a1126fe7364a5d7cbdd925a0675cc8..960375b6d1965238607339ec44d7a2a9403034f1 100644 (file)
@@ -670,14 +670,16 @@ int stlink_fread(stlink_t* sl, const char* path, stm32_addr_t addr, size_t size)
     /* do the copy by 1k blocks */
     for (off = 0; off < size; off += 1024) {
         size_t read_size = 1024;
+       size_t rounded_size;
         if ((off + read_size) > size)
-            read_size = off + read_size;
+         read_size = size - off;
 
         /* round size if needed */
-        if (read_size & 3)
-            read_size = (read_size + 4) & ~(3);
+       rounded_size = read_size;
+        if (rounded_size & 3)
+         rounded_size = (rounded_size + 4) & ~(3);
 
-        stlink_read_mem32(sl, addr + off, read_size);
+        stlink_read_mem32(sl, addr + off, rounded_size);
 
         if (write(fd, sl->q_buf, read_size) != (ssize_t) read_size) {
             fprintf(stderr, "write() != read_size\n");
index 9481609f6fc616e03836a4d23c1197f8112ef116..080a8d60ed2ebc9db9356171022d5624f930e73c 100644 (file)
@@ -162,6 +162,7 @@ extern "C" {
 #define STM32_FLASH_BASE 0x08000000
 #define STM32_FLASH_SIZE (128 * 1024)
 #define STM32_FLASH_PGSZ 1024
+#define STM32L_FLASH_PGSZ 256
         stm32_addr_t flash_base;
         size_t flash_size;
         size_t flash_pgsz;
index ff9ef71de7a4e6afc0b632342b473cc97826ff0b..e7a9ba6428f85e4b949775810cadd198e58bca85 100644 (file)
@@ -586,19 +586,6 @@ stlink_t* stlink_open_usb(const int verbose) {
     
     sl->core_stat = STLINK_CORE_STAT_UNKNOWN;
 
-    /* flash memory settings */
-    sl->flash_base = STM32_FLASH_BASE;
-    sl->flash_size = STM32_FLASH_SIZE;
-    sl->flash_pgsz = STM32_FLASH_PGSZ;
-
-    /* system memory */
-    sl->sys_base = STM32_SYSTEM_BASE;
-    sl->sys_size = STM32_SYSTEM_SIZE;
-
-    /* sram memory settings */
-    sl->sram_base = STM32_SRAM_BASE;
-    sl->sram_size = STM32L_SRAM_SIZE;
-
     if (libusb_init(&(slu->libusb_ctx))) {
        fprintf(stderr, "failed to init libusb context, wrong version of libraries?\n");
        goto on_error;
@@ -698,6 +685,41 @@ stlink_t* stlink_open_usb(const int verbose) {
       stlink_exit_dfu_mode(sl);
     }
     stlink_version(sl);
+
+    /* per device family initialization */
+    stlink_core_id(sl);
+    if (sl->core_id == 0x2ba01477) /* stm32l */ {
+
+      /* flash memory settings */
+      sl->flash_base = STM32_FLASH_BASE;
+      sl->flash_size = STM32_FLASH_SIZE;
+      sl->flash_pgsz = STM32L_FLASH_PGSZ;
+
+      /* system memory */
+      sl->sys_base = STM32_SYSTEM_BASE;
+      sl->sys_size = STM32_SYSTEM_SIZE;
+
+      /* sram memory settings */
+      sl->sram_base = STM32_SRAM_BASE;
+      sl->sram_size = STM32L_SRAM_SIZE;
+
+    } else /* stm32vl */ {
+
+      /* flash memory settings */
+      sl->flash_base = STM32_FLASH_BASE;
+      sl->flash_size = STM32_FLASH_SIZE;
+      sl->flash_pgsz = STM32_FLASH_PGSZ;
+
+      /* system memory */
+      sl->sys_base = STM32_SYSTEM_BASE;
+      sl->sys_size = STM32_SYSTEM_SIZE;
+
+      /* sram memory settings */
+      sl->sram_base = STM32_SRAM_BASE;
+      sl->sram_size = STM32_SRAM_SIZE;
+
+    }
+
     error = 0;
 
 on_libusb_error: