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 <andreas.fritiofson@gmail.com>
Reviewed-on: http://openocd.zylin.com/1496
Tested-by: jenkins
Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
This commit is contained in:
Andreas Fritiofson 2013-07-09 21:49:07 +02:00 committed by Spencer Oliver
parent 3646e86c97
commit 0ce2ca748b
3 changed files with 209 additions and 236 deletions

View File

@ -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) static int get_stm32x_info(struct flash_bank *bank, char *buf, int buf_size)
{ {
uint32_t device_id; uint32_t dbgmcu_idcode;
int printed;
/* read stm32 device id register */ /* 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) if (retval != ERROR_OK)
return retval; return retval;
if ((device_id & 0xfff) == 0x410) { uint16_t device_id = dbgmcu_idcode & 0xfff;
printed = snprintf(buf, buf_size, "stm32x (Medium Density) - Rev: "); uint16_t rev_id = dbgmcu_idcode >> 16;
buf += printed; const char *device_str;
buf_size -= printed; const char *rev_str = NULL;
switch (device_id >> 16) { switch (device_id) {
case 0x0000: case 0x410:
snprintf(buf, buf_size, "A"); device_str = "stm32x (Medium Density)";
break;
case 0x2000: switch (rev_id) {
snprintf(buf, buf_size, "B"); case 0x0000:
break; rev_str = "A";
break;
case 0x2001: case 0x2000:
snprintf(buf, buf_size, "Z"); rev_str = "B";
break; break;
case 0x2003: case 0x2001:
snprintf(buf, buf_size, "Y"); rev_str = "Z";
break; break;
default: case 0x2003:
snprintf(buf, buf_size, "unknown"); rev_str = "Y";
break; break;
} }
} else if ((device_id & 0xfff) == 0x412) { break;
printed = snprintf(buf, buf_size, "stm32x (Low Density) - Rev: ");
buf += printed;
buf_size -= printed;
switch (device_id >> 16) { case 0x412:
case 0x1000: device_str = "stm32x (Low Density)";
snprintf(buf, buf_size, "A");
break;
default: switch (rev_id) {
snprintf(buf, buf_size, "unknown"); case 0x1000:
break; rev_str = "A";
break;
} }
} else if ((device_id & 0xfff) == 0x414) { break;
printed = snprintf(buf, buf_size, "stm32x (High Density) - Rev: ");
buf += printed;
buf_size -= printed;
switch (device_id >> 16) { case 0x414:
case 0x1000: device_str = "stm32x (High Density)";
snprintf(buf, buf_size, "A");
break;
case 0x1001: switch (rev_id) {
snprintf(buf, buf_size, "Z"); case 0x1000:
break; rev_str = "A";
break;
default: case 0x1001:
snprintf(buf, buf_size, "unknown"); rev_str = "Z";
break; break;
} }
} else if ((device_id & 0xfff) == 0x418) { break;
printed = snprintf(buf, buf_size, "stm32x (Connectivity) - Rev: ");
buf += printed;
buf_size -= printed;
switch (device_id >> 16) { case 0x418:
case 0x1000: device_str = "stm32x (Connectivity)";
snprintf(buf, buf_size, "A");
break;
case 0x1001: switch (rev_id) {
snprintf(buf, buf_size, "Z"); case 0x1000:
break; rev_str = "A";
break;
default: case 0x1001:
snprintf(buf, buf_size, "unknown"); rev_str = "Z";
break; break;
} }
} else if ((device_id & 0xfff) == 0x420) { break;
printed = snprintf(buf, buf_size, "stm32x (Value) - Rev: ");
buf += printed;
buf_size -= printed;
switch (device_id >> 16) { case 0x420:
case 0x1000: device_str = "stm32x (Value)";
snprintf(buf, buf_size, "A");
break;
case 0x1001: switch (rev_id) {
snprintf(buf, buf_size, "Z"); case 0x1000:
break; rev_str = "A";
break;
default: case 0x1001:
snprintf(buf, buf_size, "unknown"); rev_str = "Z";
break; break;
} }
} else if ((device_id & 0xfff) == 0x422) { break;
printed = snprintf(buf, buf_size, "stm32f30x - Rev: ");
buf += printed;
buf_size -= printed;
switch (device_id >> 16) { case 0x422:
case 0x1000: device_str = "stm32f30x";
snprintf(buf, buf_size, "A");
break;
case 0x1001: switch (rev_id) {
snprintf(buf, buf_size, "Z"); case 0x1000:
break; rev_str = "A";
break;
case 0x2000: case 0x1001:
snprintf(buf, buf_size, "B"); rev_str = "Z";
break; break;
default: case 0x2000:
snprintf(buf, buf_size, "unknown"); rev_str = "B";
break; break;
} }
} else if ((device_id & 0xfff) == 0x428) { break;
printed = snprintf(buf, buf_size, "stm32x (Value HD) - Rev: ");
buf += printed;
buf_size -= printed;
switch (device_id >> 16) { case 0x428:
case 0x1000: device_str = "stm32x (Value HD)";
snprintf(buf, buf_size, "A");
break;
case 0x1001: switch (rev_id) {
snprintf(buf, buf_size, "Z"); case 0x1000:
break; rev_str = "A";
break;
default: case 0x1001:
snprintf(buf, buf_size, "unknown"); rev_str = "Z";
break; break;
} }
} else if ((device_id & 0xfff) == 0x430) { break;
printed = snprintf(buf, buf_size, "stm32x (XL) - Rev: ");
buf += printed;
buf_size -= printed;
switch (device_id >> 16) { case 0x430:
case 0x1000: device_str = "stm32x (XL)";
snprintf(buf, buf_size, "A");
break;
default: switch (rev_id) {
snprintf(buf, buf_size, "unknown"); case 0x1000:
break; rev_str = "A";
break;
} }
} else if ((device_id & 0xfff) == 0x432) { break;
printed = snprintf(buf, buf_size, "stm32f37x - Rev: ");
buf += printed;
buf_size -= printed;
switch (device_id >> 16) { case 0x432:
case 0x1000: device_str = "stm32f37x";
snprintf(buf, buf_size, "A");
break;
case 0x2000: switch (rev_id) {
snprintf(buf, buf_size, "B"); case 0x1000:
break; rev_str = "A";
break;
default: case 0x2000:
snprintf(buf, buf_size, "unknown"); rev_str = "B";
break; break;
} }
} else if (((device_id & 0xfff) == 0x440) || break;
((device_id & 0xfff) == 0x444)) {
printed = snprintf(buf, buf_size, "stm32f0x - Rev: ");
buf += printed;
buf_size -= printed;
switch (device_id >> 16) { case 0x440:
case 0x1000: case 0x444:
snprintf(buf, buf_size, "1.0"); device_str = "stm32f0x";
break;
case 0x2000: switch (rev_id) {
snprintf(buf, buf_size, "2.0"); case 0x1000:
break; rev_str = "1.0";
break;
default: case 0x2000:
snprintf(buf, buf_size, "unknown"); rev_str = "2.0";
break; break;
} }
} else { break;
default:
snprintf(buf, buf_size, "Cannot identify target as a stm32x\n"); snprintf(buf, buf_size, "Cannot identify target as a stm32x\n");
return ERROR_FAIL; 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; return ERROR_OK;
} }

View File

@ -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) static int get_stm32x_info(struct flash_bank *bank, char *buf, int buf_size)
{ {
uint32_t device_id; uint32_t dbgmcu_idcode;
int printed;
/* read stm32 device id register */ /* 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) if (retval != ERROR_OK)
return retval; return retval;
if ((device_id & 0xfff) == 0x411) { uint16_t device_id = dbgmcu_idcode & 0xfff;
printed = snprintf(buf, buf_size, "stm32f2x - Rev: "); uint16_t rev_id = dbgmcu_idcode >> 16;
buf += printed; const char *device_str;
buf_size -= printed; const char *rev_str = NULL;
switch (device_id >> 16) { switch (device_id) {
case 0x1000: case 0x411:
snprintf(buf, buf_size, "A"); device_str = "stm32f2x";
break;
case 0x2000: switch (rev_id) {
snprintf(buf, buf_size, "B"); case 0x1000:
break; rev_str = "A";
break;
case 0x1001: case 0x2000:
snprintf(buf, buf_size, "Z"); rev_str = "B";
break; break;
case 0x2001: case 0x1001:
snprintf(buf, buf_size, "Y"); rev_str = "Z";
break; break;
case 0x2003: case 0x2001:
snprintf(buf, buf_size, "X"); rev_str = "Y";
break; break;
default: case 0x2003:
snprintf(buf, buf_size, "unknown"); rev_str = "X";
break; break;
} }
} else if (((device_id & 0xfff) == 0x413) || break;
((device_id & 0xfff) == 0x419)) {
printed = snprintf(buf, buf_size, "stm32f4x - Rev: ");
buf += printed;
buf_size -= printed;
switch (device_id >> 16) { case 0x413:
case 0x1000: case 0x419:
snprintf(buf, buf_size, "A"); device_str = "stm32f4x";
break;
case 0x1001: switch (rev_id) {
snprintf(buf, buf_size, "Z"); case 0x1000:
break; rev_str = "A";
break;
default: case 0x1001:
snprintf(buf, buf_size, "unknown"); rev_str = "Z";
break; break;
} }
} else { break;
default:
snprintf(buf, buf_size, "Cannot identify target as a stm32x\n"); snprintf(buf, buf_size, "Cannot identify target as a stm32x\n");
return ERROR_FAIL; 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; return ERROR_OK;
} }

View File

@ -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 */ /* This method must return a string displaying information about the bank */
struct target *target = bank->target; uint32_t dbgmcu_idcode;
uint32_t device_id;
int printed;
/* read stm32 device id register */ /* 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) if (retval != ERROR_OK)
return retval; return retval;
if ((device_id & 0xfff) == 0x416) { uint16_t device_id = dbgmcu_idcode & 0xfff;
printed = snprintf(buf, buf_size, "stm32lx - Rev: "); uint16_t rev_id = dbgmcu_idcode >> 16;
buf += printed; const char *device_str;
buf_size -= printed; const char *rev_str = NULL;
switch (device_id >> 16) { switch (device_id) {
case 0x1000: case 0x416:
snprintf(buf, buf_size, "A"); device_str = "stm32lx";
break;
case 0x1008: switch (rev_id) {
snprintf(buf, buf_size, "Y"); case 0x1000:
break; rev_str = "A";
break;
case 0x1018: case 0x1008:
snprintf(buf, buf_size, "X"); rev_str = "Y";
break; break;
case 0x1038: case 0x1018:
snprintf(buf, buf_size, "W"); rev_str = "X";
break; break;
case 0x1078: case 0x1038:
snprintf(buf, buf_size, "V"); rev_str = "W";
break; break;
default: case 0x1078:
snprintf(buf, buf_size, "unknown"); rev_str = "V";
break; break;
} }
} else if (((device_id & 0xfff) == 0x436) || break;
((device_id & 0xfff) == 0x427)) {
printed = snprintf(buf, buf_size, "stm32lx (HD) - Rev: ");
buf += printed;
buf_size -= printed;
switch (device_id >> 16) { case 0x436:
case 0x1000: case 0x427:
snprintf(buf, buf_size, "A"); device_str = "stm32lx (HD)";
break;
case 0x1008: switch (rev_id) {
snprintf(buf, buf_size, "Z"); case 0x1000:
break; rev_str = "A";
break;
case 0x1018: case 0x1008:
snprintf(buf, buf_size, "Y"); rev_str = "Z";
break; break;
default: case 0x1018:
snprintf(buf, buf_size, "unknown"); rev_str = "Y";
break; break;
} }
} else { break;
default:
snprintf(buf, buf_size, "Cannot identify target as a stm32lx"); snprintf(buf, buf_size, "Cannot identify target as a stm32lx");
return ERROR_FAIL; 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; return ERROR_OK;
} }