Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactoring] Optimisation for processing chipid files #1234

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/st-info/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ static void stlink_print_version(stlink_t *sl) {
}

static void stlink_print_info(stlink_t *sl) {
const struct stlink_chipid_params *params = NULL;

if (!sl) { return; }

Expand All @@ -38,9 +37,6 @@ static void stlink_print_info(stlink_t *sl) {
printf(" flash: %u (pagesize: %u)\n", (uint32_t)sl->flash_size, (uint32_t)sl->flash_pgsz);
printf(" sram: %u\n", (uint32_t)sl->sram_size);
printf(" chipid: 0x%.3x\n", sl->chip_id);

params = stlink_chipid_get_params(sl->chip_id);
if (params) { printf(" dev-type: %s\n", params->dev_type); }
Sylensky marked this conversation as resolved.
Show resolved Hide resolved
}

static void stlink_probe(enum connect_type connect, int freq) {
Expand Down Expand Up @@ -69,6 +65,8 @@ static int print_data(int ac, char **av) {
return(0);
}

init_chipids(ETC_STLINK_DIR);

for (int i=2; i<ac; i++) {

if (strcmp(av[i], "--connect-under-reset") == 0) {
Expand Down Expand Up @@ -135,8 +133,6 @@ int main(int ac, char** av) {
return(-1);
}

init_chipids (ETC_STLINK_DIR);

err = print_data(ac, av);

return(err);
Expand Down
44 changes: 19 additions & 25 deletions src/stlink-lib/chipid.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ struct stlink_chipid_params *stlink_chipid_get_params(uint32_t chip_id) {
struct stlink_chipid_params *params = NULL;
for (params = devicelist; params != NULL; params = params->next)
if (params->chip_id == chip_id) {
fprintf(stderr, "\ndetected chip_id parametres\n\n");
dump_a_chip(stderr, params);
fprintf(stdout, "\ndetected chip_id parametres\n\n");
dump_a_chip(stdout, params);
Sylensky marked this conversation as resolved.
Show resolved Hide resolved
break;
}

Expand All @@ -40,7 +40,7 @@ struct stlink_chipid_params *stlink_chipid_get_params(uint32_t chip_id) {

void process_chipfile(char *fname) {
FILE *fp;
char *p, *pp, buf[1025];
char *p, buf[128];
char word[64], value[64];
struct stlink_chipid_params *ts;
int nc;
Expand All @@ -55,29 +55,23 @@ void process_chipfile(char *fname) {

ts = calloc(sizeof(struct stlink_chipid_params), 1);

while (fgets(buf, 1024, fp) != NULL) {
for (p = buf; isspace (*p); p++);
while (fgets(buf, sizeof(buf), fp) != NULL) {

if (!*p) {
continue; // we hit end-of-line with only whitespace
}
Nightwalker-87 marked this conversation as resolved.
Show resolved Hide resolved

if (*p == '#') {
if(strncmp(buf, "#", strlen("#")) == 0)
continue; // ignore comments
Sylensky marked this conversation as resolved.
Show resolved Hide resolved
}

sscanf(p, "%s %s", word, value);
sscanf(buf, "%s %s", word, value);

if (strcmp (word, "dev_type") == 0) {
// ts->dev_type = strdup (value);
buf[strlen(p) - 1] = 0; // chomp newline
sscanf(p, "%*s %n", &nc);
ts->dev_type = strdup(p + nc);
buf[strlen(buf) - 1] = 0; // chomp newline
sscanf(buf, "%*s %n", &nc);
ts->dev_type = strdup(buf + nc);
} else if (strcmp(word, "ref_manual_id") == 0) {
// ts->ref_manual_id = strdup (value);
buf[strlen(p) - 1] = 0; // chomp newline
sscanf(p, "%*s %n", &nc);
ts->ref_manual_id = strdup(p + nc);
buf[strlen(buf) - 1] = 0; // chomp newline
sscanf(buf, "%*s %n", &nc);
ts->ref_manual_id = strdup(buf + nc);
} else if (strcmp(word, "chip_id") == 0) {
if (sscanf(value, "%i", &ts->chip_id) < 1) {
fprintf(stderr, "Failed to parse chip-id\n");
Expand Down Expand Up @@ -138,18 +132,18 @@ void process_chipfile(char *fname) {
fprintf(stderr, "Failed to parse option size\n");
}
} else if (strcmp(word, "flags") == 0) {
pp = strtok (p, " \t\n");
p = strtok (buf, " \t\n");

while ((pp = strtok (NULL, " \t\n"))) {
if (strcmp(pp, "none") == 0) {
while ((p = strtok (NULL, " \t\n"))) {
if (strcmp(p, "none") == 0) {
// NOP
} else if (strcmp(pp, "dualbank") == 0) {
} else if (strcmp(p, "dualbank") == 0) {
ts->flags |= CHIP_F_HAS_DUAL_BANK;
} else if (strcmp(pp, "swo") == 0) {
} else if (strcmp(p, "swo") == 0) {
ts->flags |= CHIP_F_HAS_SWO_TRACING;
} else {
fprintf(stderr, "Unknown flags word in %s: '%s'\n",
fname, pp);
fname, p);
}
}

Expand All @@ -159,7 +153,7 @@ void process_chipfile(char *fname) {
fname, word);
}
}

fclose(fp);
ts->next = devicelist;
devicelist = ts;
}
Expand Down