]> git.sur5r.net Git - openocd/commitdiff
flash: reduce code duplication in stm32 flash probe
authorAndreas Fritiofson <andreas.fritiofson@gmail.com>
Mon, 16 Jul 2012 22:43:46 +0000 (00:43 +0200)
committerFreddie Chopin <freddie.chopin@gmail.com>
Mon, 30 Jul 2012 06:33:58 +0000 (06:33 +0000)
Remove a lot of the repetitive code in stm32f1x flash probe by converting
the large if-selector to a switch, moving the common checks outside it and
concentrating the failure handling to a single point.

Do the same with stm32f2x and stm32lx for consistency.

Change-Id: Ic0ecfb1533c49f5d2108cda5fd20c8372d7c71ef
Signed-off-by: Andreas Fritiofson <andreas.fritiofson@gmail.com>
Reviewed-on: http://openocd.zylin.com/746
Tested-by: jenkins
Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
Reviewed-by: Freddie Chopin <freddie.chopin@gmail.com>
src/flash/nor/stm32f1x.c
src/flash/nor/stm32f2x.c
src/flash/nor/stm32lx.c

index 2a6604dc2a42833e0724adf96f89610dbfb46839..d06016a4d849ff3ae46fd88115616cacd642e591 100644 (file)
@@ -880,6 +880,7 @@ static int stm32x_probe(struct flash_bank *bank)
        struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
        int i;
        uint16_t flash_size_in_kb;
+       uint16_t max_flash_size_in_kb;
        uint32_t device_id;
        int page_size;
        uint32_t base_address = 0x08000000;
@@ -894,116 +895,76 @@ static int stm32x_probe(struct flash_bank *bank)
 
        LOG_INFO("device id = 0x%08" PRIx32 "", device_id);
 
-       /* get flash size from target. */
-       retval = stm32x_get_flash_size(bank, &flash_size_in_kb);
-       if (retval != ERROR_OK) {
-               LOG_WARNING("failed reading flash size, default to max target family");
-               /* failed reading flash size, default to max target family */
-               flash_size_in_kb = 0xffff;
-       }
-
-       /* some variants read 0 for flash size register
-        * use a max flash size as a default */
-       if (flash_size_in_kb == 0)
-               flash_size_in_kb = 0xffff;
-
-       if ((device_id & 0xfff) == 0x410) {
-               /* medium density - we have 1k pages
-                * 4 pages for a protection area */
+       /* set page size, protection granularity and max flash size depending on family */
+       switch (device_id & 0xfff) {
+       case 0x410: /* medium density */
                page_size = 1024;
                stm32x_info->ppage_size = 4;
-
-               /* check for early silicon */
-               if (flash_size_in_kb == 0xffff) {
-                       /* number of sectors incorrect on revA */
-                       LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash");
-                       flash_size_in_kb = 128;
-               }
-       } else if ((device_id & 0xfff) == 0x412) {
-               /* low density - we have 1k pages
-                * 4 pages for a protection area */
+               max_flash_size_in_kb = 128;
+               break;
+       case 0x412: /* low density */
                page_size = 1024;
                stm32x_info->ppage_size = 4;
-
-               /* check for early silicon */
-               if (flash_size_in_kb == 0xffff) {
-                       /* number of sectors incorrect on revA */
-                       LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 32k flash");
-                       flash_size_in_kb = 32;
-               }
-       } else if ((device_id & 0xfff) == 0x414) {
-               /* high density - we have 2k pages
-                * 2 pages for a protection area */
+               max_flash_size_in_kb = 32;
+               break;
+       case 0x414: /* high density */
                page_size = 2048;
                stm32x_info->ppage_size = 2;
-
-               /* check for early silicon */
-               if (flash_size_in_kb == 0xffff) {
-                       /* number of sectors incorrect on revZ */
-                       LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 512k flash");
-                       flash_size_in_kb = 512;
-               }
-       } else if ((device_id & 0xfff) == 0x418) {
-               /* connectivity line density - we have 2k pages
-                * 2 pages for a protection area */
+               max_flash_size_in_kb = 512;
+               break;
+       case 0x418: /* connectivity line density */
                page_size = 2048;
                stm32x_info->ppage_size = 2;
-
-               /* check for early silicon */
-               if (flash_size_in_kb == 0xffff) {
-                       /* number of sectors incorrect on revZ */
-                       LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 256k flash");
-                       flash_size_in_kb = 256;
-               }
-       } else if ((device_id & 0xfff) == 0x420) {
-               /* value line density - we have 1k pages
-                * 4 pages for a protection area */
+               max_flash_size_in_kb = 256;
+               break;
+       case 0x420: /* value line density */
                page_size = 1024;
                stm32x_info->ppage_size = 4;
-
-               /* check for early silicon */
-               if (flash_size_in_kb == 0xffff) {
-                       /* number of sectors may be incorrect on early silicon */
-                       LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash");
-                       flash_size_in_kb = 128;
-               }
-       } else if ((device_id & 0xfff) == 0x422) {
-               /* stm32f30x - we have 2k pages
-                * 2 pages for a protection area */
+               max_flash_size_in_kb = 128;
+               break;
+       case 0x422: /* stm32f30x */
                page_size = 2048;
                stm32x_info->ppage_size = 2;
-
-               /* check for early silicon */
-               if (flash_size_in_kb == 0xffff) {
-                       LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 256k flash");
-                       flash_size_in_kb = 256;
-               }
-       } else if ((device_id & 0xfff) == 0x428) {
-               /* value line High density - we have 2k pages
-                * 4 pages for a protection area */
+               max_flash_size_in_kb = 256;
+               break;
+       case 0x428: /* value line High density */
                page_size = 2048;
                stm32x_info->ppage_size = 4;
-
-               /* check for early silicon */
-               if (flash_size_in_kb == 0xffff) {
-                       /* number of sectors may be incorrect on early silicon */
-                       LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash");
-                       flash_size_in_kb = 128;
-               }
-       } else if ((device_id & 0xfff) == 0x430) {
-               /* xl line density - we have 2k pages
-                * 2 pages for a protection area */
+               max_flash_size_in_kb = 128;
+               break;
+       case 0x430: /* xl line density (dual flash banks) */
                page_size = 2048;
                stm32x_info->ppage_size = 2;
+               max_flash_size_in_kb = 1024;
                stm32x_info->has_dual_banks = true;
+               break;
+       case 0x432: /* stm32f37x */
+               page_size = 2048;
+               stm32x_info->ppage_size = 2;
+               max_flash_size_in_kb = 256;
+               break;
+       case 0x440: /* stm32f0x */
+               page_size = 1024;
+               stm32x_info->ppage_size = 4;
+               max_flash_size_in_kb = 64;
+               break;
+       default:
+               LOG_WARNING("Cannot identify target as a STM32 family.");
+               return ERROR_FAIL;
+       }
 
-               /* check for early silicon */
-               if (flash_size_in_kb == 0xffff) {
-                       /* number of sectors may be incorrect on early silicon */
-                       LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 1024k flash");
-                       flash_size_in_kb = 1024;
-               }
+       /* get flash size from target. */
+       retval = stm32x_get_flash_size(bank, &flash_size_in_kb);
 
+       /* failed reading flash size or flash size invalid (early silicon),
+        * default to max target family */
+       if (retval != ERROR_OK || flash_size_in_kb == 0xffff || flash_size_in_kb == 0) {
+               LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming %dk flash",
+                       max_flash_size_in_kb);
+               flash_size_in_kb = max_flash_size_in_kb;
+       }
+
+       if (stm32x_info->has_dual_banks) {
                /* split reported size into matching bank */
                if (bank->base != 0x08080000) {
                        /* bank 0 will be fixed 512k */
@@ -1014,32 +975,6 @@ static int stm32x_probe(struct flash_bank *bank)
                        stm32x_info->register_base = FLASH_REG_BASE_B1;
                        base_address = 0x08080000;
                }
-       } else if ((device_id & 0xfff) == 0x432) {
-               /* stm32f37x - we have 2k pages
-                * 2 pages for a protection area */
-               page_size = 2048;
-               stm32x_info->ppage_size = 2;
-
-               /* check for early silicon */
-               if (flash_size_in_kb == 0xffff) {
-                       LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 256k flash");
-                       flash_size_in_kb = 256;
-               }
-       } else if ((device_id & 0xfff) == 0x440) {
-               /* stm32f0x - we have 1k pages
-                * 4 pages for a protection area */
-               page_size = 1024;
-               stm32x_info->ppage_size = 4;
-
-               /* check for early silicon */
-               if (flash_size_in_kb == 0xffff) {
-                       /* number of sectors incorrect on revZ */
-                       LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 64k flash");
-                       flash_size_in_kb = 64;
-               }
-       } else {
-               LOG_WARNING("Cannot identify target as a STM32 family.");
-               return ERROR_FAIL;
        }
 
        LOG_INFO("flash size = %dkbytes", flash_size_in_kb);
index c7abd6a837992091fbae5857f925fd0dda1d9694..2407977f1110cd8a3804605f7e8e22f26efdc7ec 100644 (file)
@@ -586,6 +586,7 @@ static int stm32x_probe(struct flash_bank *bank)
        struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
        int i;
        uint16_t flash_size_in_kb;
+       uint16_t max_flash_size_in_kb;
        uint32_t device_id;
        uint32_t base_address = 0x08000000;
 
@@ -597,36 +598,26 @@ static int stm32x_probe(struct flash_bank *bank)
                return retval;
        LOG_INFO("device id = 0x%08" PRIx32 "", device_id);
 
-       /* get flash size from target. */
-       retval = target_read_u16(target, 0x1FFF7A22, &flash_size_in_kb);
-       if (retval != ERROR_OK) {
-               LOG_WARNING("failed reading flash size, default to max target family");
-               /* failed reading flash size, default to max target family */
-               flash_size_in_kb = 0xffff;
+       /* set max flash size depending on family */
+       switch (device_id & 0xfff) {
+       case 0x411:
+       case 0x413:
+               max_flash_size_in_kb = 1024;
+               break;
+       default:
+               LOG_WARNING("Cannot identify target as a STM32 family.");
+               return ERROR_FAIL;
        }
 
-       /* some variants read 0 for flash size register
-        * use a max flash size as a default */
-       if (flash_size_in_kb == 0)
-               flash_size_in_kb = 0xffff;
+       /* get flash size from target. */
+       retval = target_read_u16(target, 0x1FFF7A22, &flash_size_in_kb);
 
-       if ((device_id & 0xfff) == 0x411) {
-               /* check for early silicon */
-               if (flash_size_in_kb == 0xffff) {
-                       /* number of sectors may be incorrect on early silicon */
-                       LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 1024k flash");
-                       flash_size_in_kb = 1024;
-               }
-       } else if ((device_id & 0xfff) == 0x413) {
-               /* check for early silicon */
-               if (flash_size_in_kb == 0xffff) {
-                       /* number of sectors may be incorrect on early silicon */
-                       LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 1024k flash");
-                       flash_size_in_kb = 1024;
-               }
-       } else {
-               LOG_WARNING("Cannot identify target as a STM32 family.");
-               return ERROR_FAIL;
+       /* failed reading flash size or flash size invalid (early silicon),
+        * default to max target family */
+       if (retval != ERROR_OK || flash_size_in_kb == 0xffff || flash_size_in_kb == 0) {
+               LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming %dk flash",
+                       max_flash_size_in_kb);
+               flash_size_in_kb = max_flash_size_in_kb;
        }
 
        LOG_INFO("flash size = %dkbytes", flash_size_in_kb);
index fedab300619638bcb80fb19064bf2ee5795ac0a0..ab61538ffe2267321a4385f1469720b2d4ecf0b0 100644 (file)
@@ -463,6 +463,7 @@ static int stm32lx_probe(struct flash_bank *bank)
        struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
        int i;
        uint16_t flash_size_in_kb;
+       uint16_t max_flash_size_in_kb;
        uint32_t device_id;
 
        stm32lx_info->probed = 0;
@@ -474,36 +475,28 @@ static int stm32lx_probe(struct flash_bank *bank)
 
        LOG_DEBUG("device id = 0x%08" PRIx32 "", device_id);
 
-       /* get flash size from target. */
-       retval = target_read_u16(target, F_SIZE, &flash_size_in_kb);
-       if (retval != ERROR_OK) {
-               LOG_WARNING("failed reading flash size, default to max target family");
-               /* failed reading flash size, default to max target family */
-               flash_size_in_kb = 0xffff;
+       /* set max flash size depending on family */
+       switch (device_id & 0xfff) {
+       case 0x416:
+               max_flash_size_in_kb = 128;
+               break;
+       case 0x436:
+               max_flash_size_in_kb = 384;
+               break;
+       default:
+               LOG_WARNING("Cannot identify target as a STM32L family.");
+               return ERROR_FAIL;
        }
 
-       /* some variants read 0 for flash size register
-        * use a max flash size as a default */
-       if (flash_size_in_kb == 0)
-               flash_size_in_kb = 0xffff;
+       /* get flash size from target. */
+       retval = target_read_u16(target, F_SIZE, &flash_size_in_kb);
 
-       if ((device_id & 0xfff) == 0x416) {
-               /* check for early silicon */
-               if (flash_size_in_kb == 0xffff) {
-                       /* number of sectors may be incorrect on early silicon */
-                       LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash");
-                       flash_size_in_kb = 128;
-               }
-       } else if ((device_id & 0xfff) == 0x436) {
-               /* check for early silicon */
-               if (flash_size_in_kb == 0xffff) {
-                       /* number of sectors may be incorrect on early silicon */
-                       LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 384k flash");
-                       flash_size_in_kb = 384;
-               }
-       } else {
-               LOG_WARNING("Cannot identify target as a STM32L family.");
-               return ERROR_FAIL;
+       /* failed reading flash size or flash size invalid (early silicon),
+        * default to max target family */
+       if (retval != ERROR_OK || flash_size_in_kb == 0xffff || flash_size_in_kb == 0) {
+               LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming %dk flash",
+                       max_flash_size_in_kb);
+               flash_size_in_kb = max_flash_size_in_kb;
        }
 
        /* STM32L - we have 32 sectors, 16 pages per sector -> 512 pages