From 0ce2ca748b502d09acf878c338fe23776cf6d8e2 Mon Sep 17 00:00:00 2001 From: Andreas Fritiofson Date: Tue, 9 Jul 2013 21:49:07 +0200 Subject: [PATCH] flash/stm32*: Rewrite info functions Factor out common bit masking and printing code and use intermediate strings to avoid buffer size handling. Change-Id: I7d8c12df11ade6cdca8c917b5524372daa498bf4 Signed-off-by: Andreas Fritiofson Reviewed-on: http://openocd.zylin.com/1496 Tested-by: jenkins Reviewed-by: Spencer Oliver --- src/flash/nor/stm32f1x.c | 318 ++++++++++++++++++--------------------- src/flash/nor/stm32f2x.c | 92 +++++------ src/flash/nor/stm32lx.c | 95 ++++++------ 3 files changed, 239 insertions(+), 266 deletions(-) diff --git a/src/flash/nor/stm32f1x.c b/src/flash/nor/stm32f1x.c index 037fea07..3c360fcd 100644 --- a/src/flash/nor/stm32f1x.c +++ b/src/flash/nor/stm32f1x.c @@ -1016,204 +1016,174 @@ COMMAND_HANDLER(stm32x_handle_part_id_command) 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) == 0x410) { - printed = snprintf(buf, buf_size, "stm32x (Medium Density) - 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 >> 16) { - case 0x0000: - snprintf(buf, buf_size, "A"); - break; + switch (device_id) { + case 0x410: + device_str = "stm32x (Medium Density)"; - case 0x2000: - snprintf(buf, buf_size, "B"); - break; + switch (rev_id) { + case 0x0000: + rev_str = "A"; + break; - case 0x2001: - snprintf(buf, buf_size, "Z"); - break; + case 0x2000: + rev_str = "B"; + break; - case 0x2003: - snprintf(buf, buf_size, "Y"); - break; + case 0x2001: + rev_str = "Z"; + break; - default: - snprintf(buf, buf_size, "unknown"); - break; + case 0x2003: + rev_str = "Y"; + break; } - } else if ((device_id & 0xfff) == 0x412) { - printed = snprintf(buf, buf_size, "stm32x (Low Density) - Rev: "); - buf += printed; - buf_size -= printed; - - switch (device_id >> 16) { - case 0x1000: - snprintf(buf, buf_size, "A"); - break; - - default: - snprintf(buf, buf_size, "unknown"); - break; + break; + + case 0x412: + device_str = "stm32x (Low Density)"; + + switch (rev_id) { + case 0x1000: + rev_str = "A"; + break; } - } else if ((device_id & 0xfff) == 0x414) { - printed = snprintf(buf, buf_size, "stm32x (High Density) - 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 0x414: + device_str = "stm32x (High Density)"; + + switch (rev_id) { + case 0x1000: + rev_str = "A"; + break; + + case 0x1001: + rev_str = "Z"; + break; } - } else if ((device_id & 0xfff) == 0x418) { - printed = snprintf(buf, buf_size, "stm32x (Connectivity) - 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 0x418: + device_str = "stm32x (Connectivity)"; + + switch (rev_id) { + case 0x1000: + rev_str = "A"; + break; + + case 0x1001: + rev_str = "Z"; + break; } - } else if ((device_id & 0xfff) == 0x420) { - printed = snprintf(buf, buf_size, "stm32x (Value) - 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 0x420: + device_str = "stm32x (Value)"; + + switch (rev_id) { + case 0x1000: + rev_str = "A"; + break; + + case 0x1001: + rev_str = "Z"; + break; } - } else if ((device_id & 0xfff) == 0x422) { - printed = snprintf(buf, buf_size, "stm32f30x - 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; - - case 0x2000: - snprintf(buf, buf_size, "B"); - break; - - default: - snprintf(buf, buf_size, "unknown"); - break; + break; + + case 0x422: + device_str = "stm32f30x"; + + switch (rev_id) { + case 0x1000: + rev_str = "A"; + break; + + case 0x1001: + rev_str = "Z"; + break; + + case 0x2000: + rev_str = "B"; + break; } - } else if ((device_id & 0xfff) == 0x428) { - printed = snprintf(buf, buf_size, "stm32x (Value HD) - 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 0x428: + device_str = "stm32x (Value HD)"; + + switch (rev_id) { + case 0x1000: + rev_str = "A"; + break; + + case 0x1001: + rev_str = "Z"; + break; } - } else if ((device_id & 0xfff) == 0x430) { - printed = snprintf(buf, buf_size, "stm32x (XL) - Rev: "); - buf += printed; - buf_size -= printed; - - switch (device_id >> 16) { - case 0x1000: - snprintf(buf, buf_size, "A"); - break; - - default: - snprintf(buf, buf_size, "unknown"); - break; + break; + + case 0x430: + device_str = "stm32x (XL)"; + + switch (rev_id) { + case 0x1000: + rev_str = "A"; + break; } - } else if ((device_id & 0xfff) == 0x432) { - printed = snprintf(buf, buf_size, "stm32f37x - Rev: "); - buf += printed; - buf_size -= printed; - - switch (device_id >> 16) { - case 0x1000: - snprintf(buf, buf_size, "A"); - break; - - case 0x2000: - snprintf(buf, buf_size, "B"); - break; - - default: - snprintf(buf, buf_size, "unknown"); - break; + break; + + case 0x432: + device_str = "stm32f37x"; + + switch (rev_id) { + case 0x1000: + rev_str = "A"; + break; + + case 0x2000: + rev_str = "B"; + break; } - } else if (((device_id & 0xfff) == 0x440) || - ((device_id & 0xfff) == 0x444)) { - printed = snprintf(buf, buf_size, "stm32f0x - Rev: "); - buf += printed; - buf_size -= printed; - - switch (device_id >> 16) { - case 0x1000: - snprintf(buf, buf_size, "1.0"); - break; - - case 0x2000: - snprintf(buf, buf_size, "2.0"); - break; - - default: - snprintf(buf, buf_size, "unknown"); - break; + break; + + case 0x440: + case 0x444: + device_str = "stm32f0x"; + + switch (rev_id) { + case 0x1000: + rev_str = "1.0"; + break; + + case 0x2000: + rev_str = "2.0"; + break; } - } else { + break; + + default: snprintf(buf, buf_size, "Cannot identify target as a stm32x\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; } diff --git a/src/flash/nor/stm32f2x.c b/src/flash/nor/stm32f2x.c index 1922a8c9..0593bfdd 100644 --- a/src/flash/nor/stm32f2x.c +++ b/src/flash/nor/stm32f2x.c @@ -857,68 +857,70 @@ 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 >> 16) { - case 0x1000: - snprintf(buf, buf_size, "A"); - break; + switch (device_id) { + case 0x411: + device_str = "stm32f2x"; - 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 0x2000: + rev_str = "B"; + break; - case 0x2001: - snprintf(buf, buf_size, "Y"); - break; + case 0x1001: + rev_str = "Z"; + break; - case 0x2003: - snprintf(buf, buf_size, "X"); - break; + case 0x2001: + rev_str = "Y"; + break; - default: - snprintf(buf, buf_size, "unknown"); - break; + case 0x2003: + rev_str = "X"; + 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 0x413: + case 0x419: + device_str = "stm32f4x"; + + switch (rev_id) { + case 0x1000: + rev_str = "A"; + break; + + case 0x1001: + rev_str = "Z"; + break; } - } else { + break; + + default: snprintf(buf, buf_size, "Cannot identify target as a stm32x\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; } diff --git a/src/flash/nor/stm32lx.c b/src/flash/nor/stm32lx.c index ec696d2b..b907ff22 100644 --- a/src/flash/nor/stm32lx.c +++ b/src/flash/nor/stm32lx.c @@ -695,73 +695,74 @@ static int stm32lx_get_info(struct flash_bank *bank, char *buf, int buf_size) { /* This method must return a string displaying information about the bank */ - struct target *target = bank->target; - uint32_t device_id; - int printed; + uint32_t dbgmcu_idcode; /* read stm32 device id register */ - int retval = target_read_u32(target, DBGMCU_IDCODE, &device_id); + int retval = target_read_u32(bank->target, DBGMCU_IDCODE, &dbgmcu_idcode); if (retval != ERROR_OK) return retval; - if ((device_id & 0xfff) == 0x416) { - printed = snprintf(buf, buf_size, "stm32lx - 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 >> 16) { - case 0x1000: - snprintf(buf, buf_size, "A"); - break; + switch (device_id) { + case 0x416: + device_str = "stm32lx"; - case 0x1008: - snprintf(buf, buf_size, "Y"); - break; + switch (rev_id) { + case 0x1000: + rev_str = "A"; + break; - case 0x1018: - snprintf(buf, buf_size, "X"); - break; + case 0x1008: + rev_str = "Y"; + break; - case 0x1038: - snprintf(buf, buf_size, "W"); - break; + case 0x1018: + rev_str = "X"; + break; - case 0x1078: - snprintf(buf, buf_size, "V"); - break; + case 0x1038: + rev_str = "W"; + break; - default: - snprintf(buf, buf_size, "unknown"); - break; + case 0x1078: + rev_str = "V"; + break; } - } else if (((device_id & 0xfff) == 0x436) || - ((device_id & 0xfff) == 0x427)) { - printed = snprintf(buf, buf_size, "stm32lx (HD) - Rev: "); - buf += printed; - buf_size -= printed; - - switch (device_id >> 16) { - case 0x1000: - snprintf(buf, buf_size, "A"); - break; + break; - case 0x1008: - snprintf(buf, buf_size, "Z"); - break; + case 0x436: + case 0x427: + device_str = "stm32lx (HD)"; - case 0x1018: - snprintf(buf, buf_size, "Y"); - break; + switch (rev_id) { + case 0x1000: + rev_str = "A"; + break; - default: - snprintf(buf, buf_size, "unknown"); - break; + case 0x1008: + rev_str = "Z"; + break; + + case 0x1018: + rev_str = "Y"; + break; } - } else { + break; + + default: snprintf(buf, buf_size, "Cannot identify target as a stm32lx"); 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; } -- 2.39.5