stm32f2x.c: Handle STM32F42x/43x 1 MiByte devices with DB1M option set.
[openocd.git] / src / flash / nor / stm32f2x.c
index 7b8ac941a6b8657e5cba59cf953324ef8096c7b0..490cccc4a6641f2c6b58e857a2ca16255c45a7b7 100644 (file)
  * To reduce testing complexity and dangers of regressions,
  * a seperate file is used for stm32fx2x.
  *
- * 1mByte part with 4 x 16, 1 x 64, 7 x 128kBytes sectors
+ * Sector sizes in kiBytes:
+ * 1 MiByte part with 4 x 16, 1 x 64, 7 x 128.
+ * 2 MiByte part with 4 x 16, 1 x 64, 7 x 128, 4 x 16, 1 x 64, 7 x 128.
+ * 1 MiByte STM32F42x/43x part with DB1M Option set:
+ *                    4 x 16, 1 x 64, 3 x 128, 4 x 16, 1 x 64, 3 x 128.
  *
- * What's the protection page size???
+ * Protection size is sector size.
  *
  * Tested with STM3220F-EVAL board.
  *
- * STM32F21xx series for reference.
+ * STM32F4xx series for reference.
  *
- * RM0033
- * http://www.st.com/internet/mcu/product/250192.jsp
+ * RM0090
+ * http://www.st.com/web/en/resource/technical/document/reference_manual/DM00031020.pdf
  *
  * PM0059
  * www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/
 #define FLASH_PSIZE_16 (1 << 8)
 #define FLASH_PSIZE_32 (2 << 8)
 #define FLASH_PSIZE_64 (3 << 8)
-#define FLASH_SNB(a)   ((a) << 3)
+/* The sector number encoding is not straight binary for dual bank flash.
+ * Warning: evaluates the argument multiple times */
+#define FLASH_SNB(a)   ((((a) >= 12) ? 0x10 | ((a) - 12) : (a)) << 3)
 #define FLASH_LOCK     (1 << 31)
 
 /* FLASH_SR register bits */
 #define OPT_RDRSTSTOP  3
 #define OPT_RDRSTSTDBY 4
 #define OPT_BFB2       5       /* dual flash bank only */
+#define OPT_DB1M       14      /* 1 MiB devices dual flash bank option */
 
 /* register unlock keys */
 
@@ -256,7 +263,7 @@ static int stm32x_unlock_reg(struct target *target)
                return retval;
 
        if (ctrl & FLASH_LOCK) {
-               LOG_ERROR("flash not unlocked STM32_FLASH_CR: %x", ctrl);
+               LOG_ERROR("flash not unlocked STM32_FLASH_CR: %" PRIx32, ctrl);
                return ERROR_TARGET_FAILURE;
        }
 
@@ -288,7 +295,7 @@ static int stm32x_unlock_option_reg(struct target *target)
                return retval;
 
        if (ctrl & OPT_LOCK) {
-               LOG_ERROR("options not unlocked STM32_FLASH_OPTCR: %x", ctrl);
+               LOG_ERROR("options not unlocked STM32_FLASH_OPTCR: %" PRIx32, ctrl);
                return ERROR_TARGET_FAILURE;
        }
 
@@ -342,8 +349,8 @@ static int stm32x_write_options(struct flash_bank *bank)
 
        /* rebuild option data */
        optiondata = stm32x_info->option_bytes.user_options;
-       buf_set_u32(&optiondata, 8, 8, stm32x_info->option_bytes.RDP);
-       buf_set_u32(&optiondata, 16, 12, stm32x_info->option_bytes.protection);
+       optiondata |= stm32x_info->option_bytes.RDP << 8;
+       optiondata |= (stm32x_info->option_bytes.protection & 0x0fff) << 16;
 
        /* program options */
        retval = target_write_u32(target, STM32_FLASH_OPTCR, optiondata);
@@ -353,7 +360,7 @@ static int stm32x_write_options(struct flash_bank *bank)
        if (stm32x_info->has_large_mem) {
 
                uint32_t optiondata2 = 0;
-               buf_set_u32(&optiondata2, 16, 12, stm32x_info->option_bytes.protection >> 12);
+               optiondata2 |= (stm32x_info->option_bytes.protection & 0x00fff000) << 4;
                retval = target_write_u32(target, STM32_FLASH_OPTCR1, optiondata2);
                if (retval != ERROR_OK)
                        return retval;
@@ -370,7 +377,7 @@ static int stm32x_write_options(struct flash_bank *bank)
                return retval;
 
        /* relock registers */
-       retval = target_write_u32(target, STM32_FLASH_OPTCR, OPT_LOCK);
+       retval = target_write_u32(target, STM32_FLASH_OPTCR, optiondata | OPT_LOCK);
        if (retval != ERROR_OK)
                return retval;
 
@@ -379,14 +386,8 @@ static int stm32x_write_options(struct flash_bank *bank)
 
 static int stm32x_protect_check(struct flash_bank *bank)
 {
-       struct target *target = bank->target;
        struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
 
-       if (target->state != TARGET_HALTED) {
-               LOG_ERROR("Target not halted");
-               return ERROR_TARGET_NOT_HALTED;
-       }
-
        /* read write protection settings */
        int retval = stm32x_read_options(bank);
        if (retval != ERROR_OK) {
@@ -409,6 +410,9 @@ static int stm32x_erase(struct flash_bank *bank, int first, int last)
        struct target *target = bank->target;
        int i;
 
+       assert(first < bank->num_sectors);
+       assert(last < bank->num_sectors);
+
        if (bank->target->state != TARGET_HALTED) {
                LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
@@ -424,7 +428,7 @@ static int stm32x_erase(struct flash_bank *bank, int first, int last)
        To erase a sector, follow the procedure below:
        1. Check that no Flash memory operation is ongoing by checking the BSY bit in the
          FLASH_SR register
-       2. Set the SER bit and select the sector (out of the 12 sectors in the main memory block)
+       2. Set the SER bit and select the sector
          you wish to erase (SNB) in the FLASH_CR register
        3. Set the STRT bit in the FLASH_CR register
        4. Wait for the BSY bit to be cleared
@@ -482,7 +486,7 @@ static int stm32x_protect(struct flash_bank *bank, int set, int first, int last)
        return ERROR_OK;
 }
 
-static int stm32x_write_block(struct flash_bank *bank, uint8_t *buffer,
+static int stm32x_write_block(struct flash_bank *bank, const uint8_t *buffer,
                uint32_t offset, uint32_t count)
 {
        struct target *target = bank->target;
@@ -542,7 +546,7 @@ static int stm32x_write_block(struct flash_bank *bank, uint8_t *buffer,
 
        retval = target_write_buffer(target, write_algorithm->address,
                        sizeof(stm32x_flash_write_code),
-                       (uint8_t *)stm32x_flash_write_code);
+                       stm32x_flash_write_code);
        if (retval != ERROR_OK)
                return retval;
 
@@ -590,7 +594,7 @@ static int stm32x_write_block(struct flash_bank *bank, uint8_t *buffer,
                        LOG_ERROR("flash memory write protected");
 
                if (error != 0) {
-                       LOG_ERROR("flash write failed = %08x", error);
+                       LOG_ERROR("flash write failed = %08" PRIx32, error);
                        /* Clear but report errors */
                        target_write_u32(target, STM32_FLASH_SR, error);
                        retval = ERROR_FAIL;
@@ -609,7 +613,7 @@ static int stm32x_write_block(struct flash_bank *bank, uint8_t *buffer,
        return retval;
 }
 
-static int stm32x_write(struct flash_bank *bank, uint8_t *buffer,
+static int stm32x_write(struct flash_bank *bank, const uint8_t *buffer,
                uint32_t offset, uint32_t count)
 {
        struct target *target = bank->target;
@@ -776,7 +780,14 @@ static int stm32x_probe(struct flash_bank *bank)
                break;
        case 0x419:
                max_flash_size_in_kb = 2048;
-               stm32x_info->has_large_mem = true;
+               break;
+       case 0x423:
+               max_flash_size_in_kb = 256;
+               break;
+       case 0x431:
+       case 0x433:
+       case 0x421:
+               max_flash_size_in_kb = 512;
                break;
        default:
                LOG_WARNING("Cannot identify target as a STM32 family.");
@@ -809,7 +820,25 @@ static int stm32x_probe(struct flash_bank *bank)
        /* calculate numbers of pages */
        int num_pages = (flash_size_in_kb / 128) + 4;
 
-       /* check for larger 2048 bytes devices */
+       /* Devices with > 1024 kiByte always are dual-banked */
+       if (flash_size_in_kb > 1024)
+               stm32x_info->has_large_mem = true;
+
+       /* F42x/43x 1024 kiByte devices have a dual bank option */
+       if ((device_id & 0xfff) == 0x419 && (flash_size_in_kb == 1024)) {
+               uint32_t optiondata;
+               retval = target_read_u32(target, STM32_FLASH_OPTCR, &optiondata);
+               if (retval != ERROR_OK) {
+                       LOG_DEBUG("unable to read option bytes");
+                       return retval;
+               }
+               if (optiondata & (1 << OPT_DB1M)) {
+                       stm32x_info->has_large_mem = true;
+                       LOG_INFO("Dual Bank 1024 kiB STM32F42x/43x found");
+               }
+       }
+
+       /* check for dual-banked devices */
        if (stm32x_info->has_large_mem)
                num_pages += 4;
 
@@ -830,19 +859,21 @@ static int stm32x_probe(struct flash_bank *bank)
        setup_sector(bank, 0, 4, 16 * 1024);
        setup_sector(bank, 4, 1, 64 * 1024);
 
-       /* dynamic memory */
-       setup_sector(bank, 4 + 1, MIN(12, num_pages) - 5, 128 * 1024);
-
        if (stm32x_info->has_large_mem) {
-
-               /* fixed memory for larger devices */
-               setup_sector(bank, 12, 4, 16 * 1024);
-               setup_sector(bank, 16, 1, 64 * 1024);
-
-               /* dynamic memory for larger devices */
-               setup_sector(bank, 16 + 1, num_pages - 5 - 12, 128 * 1024);
+               if (flash_size_in_kb == 1024) {
+                       setup_sector(bank,  5, 3, 128 * 1024);
+                       setup_sector(bank, 12, 4,  16 * 1024);
+                       setup_sector(bank, 16, 1,  64 * 1024);
+                       setup_sector(bank, 17, 3, 128 * 1024);
+               } else {
+                       setup_sector(bank,  5, 7, 128 * 1024);
+                       setup_sector(bank, 12, 4,  16 * 1024);
+                       setup_sector(bank, 16, 1,  64 * 1024);
+                       setup_sector(bank, 17, 7, 128 * 1024);
+               }
+       } else {
+               setup_sector(bank, 4 + 1, MIN(12, num_pages) - 5, 128 * 1024);
        }
-
        for (i = 0; i < num_pages; i++) {
                bank->sectors[i].is_erased = -1;
                bank->sectors[i].is_protected = 0;
@@ -863,68 +894,106 @@ static int stm32x_auto_probe(struct flash_bank *bank)
 
 static int get_stm32x_info(struct flash_bank *bank, char *buf, int buf_size)
 {
-       uint32_t device_id;
-       int printed;
+       uint32_t dbgmcu_idcode;
 
        /* read stm32 device id register */
-       int retval = stm32x_get_device_id(bank, &device_id);
+       int retval = stm32x_get_device_id(bank, &dbgmcu_idcode);
        if (retval != ERROR_OK)
                return retval;
 
-       if ((device_id & 0xfff) == 0x411) {
-               printed = snprintf(buf, buf_size, "stm32f2x - Rev: ");
-               buf += printed;
-               buf_size -= printed;
+       uint16_t device_id = dbgmcu_idcode & 0xfff;
+       uint16_t rev_id = dbgmcu_idcode >> 16;
+       const char *device_str;
+       const char *rev_str = NULL;
+
+       switch (device_id) {
+       case 0x411:
+               device_str = "STM32F2xx";
+
+               switch (rev_id) {
+               case 0x1000:
+                       rev_str = "A";
+                       break;
+
+               case 0x2000:
+                       rev_str = "B";
+                       break;
+
+               case 0x1001:
+                       rev_str = "Z";
+                       break;
+
+               case 0x2001:
+                       rev_str = "Y";
+                       break;
+
+               case 0x2003:
+                       rev_str = "X";
+                       break;
+               }
+               break;
 
-               switch (device_id >> 16) {
-                       case 0x1000:
-                               snprintf(buf, buf_size, "A");
-                               break;
+       case 0x413:
+       case 0x419:
+               device_str = "STM32F4xx";
 
-                       case 0x2000:
-                               snprintf(buf, buf_size, "B");
-                               break;
+               switch (rev_id) {
+               case 0x1000:
+                       rev_str = "A";
+                       break;
 
-                       case 0x1001:
-                               snprintf(buf, buf_size, "Z");
-                               break;
+               case 0x1001:
+                       rev_str = "Z";
+                       break;
 
-                       case 0x2001:
-                               snprintf(buf, buf_size, "Y");
-                               break;
+               case 0x1003:
+                       rev_str = "Y";
+                       break;
 
-                       case 0x2003:
-                               snprintf(buf, buf_size, "X");
-                               break;
+               case 0x1007:
+                       rev_str = "1";
+                       break;
 
-                       default:
-                               snprintf(buf, buf_size, "unknown");
-                               break;
+               case 0x2001:
+                       rev_str = "3";
+                       break;
                }
-       } else if (((device_id & 0xfff) == 0x413) ||
-                       ((device_id & 0xfff) == 0x419)) {
-               printed = snprintf(buf, buf_size, "stm32f4x - Rev: ");
-               buf += printed;
-               buf_size -= printed;
-
-               switch (device_id >> 16) {
-                       case 0x1000:
-                               snprintf(buf, buf_size, "A");
-                               break;
-
-                       case 0x1001:
-                               snprintf(buf, buf_size, "Z");
-                               break;
-
-                       default:
-                               snprintf(buf, buf_size, "unknown");
-                               break;
+               break;
+       case 0x421:
+               device_str = "STM32F446";
+
+               switch (rev_id) {
+               case 0x1000:
+                       rev_str = "A";
+                       break;
                }
-       } else {
-               snprintf(buf, buf_size, "Cannot identify target as a stm32x\n");
+               break;
+       case 0x423:
+       case 0x431:
+       case 0x433:
+               device_str = "STM32F4xx (Low Power)";
+
+               switch (rev_id) {
+               case 0x1000:
+                       rev_str = "A";
+                       break;
+
+               case 0x1001:
+                       rev_str = "Z";
+                       break;
+               }
+               break;
+
+       default:
+               snprintf(buf, buf_size, "Cannot identify target as a STM32F2/4\n");
                return ERROR_FAIL;
        }
 
+       if (rev_str != NULL)
+               snprintf(buf, buf_size, "%s - Rev: %s", device_str, rev_str);
+       else
+               snprintf(buf, buf_size, "%s - Rev: unknown (0x%04x)", device_str, rev_id);
+
        return ERROR_OK;
 }
 

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)