AnsweredAssumed Answered

Why does MQX crash when using scanf ?

Question asked by Adrian Mango on Dec 25, 2016
Latest reply on Dec 27, 2016 by Daniel Chen

When running this sample code (KSDK_1.3.0), it crashed when the scanf lines are executed, I was able to get around the first scanf by using %s instead, but I can't find a way to make it work with scanf %d.

 

Do you know the reason why printf(%s) worked and printf(%c) didn't ?

And why scanf(%d) didn't also ?

 

When it crashed, I saw in the Task list, unhandled interrupt , unknown error, and the task name is garbage like it was corrupted. When this happened, I no longer can single stepping the code. How do I "trap" the error so it tells me more information ?

 

void Ui_task(uint32_t initial_data)
{
uint32_t numCanTasks, i;
char c;
_task_id ids[10] = {0};

 

printf("\n Ui: Hello World \n");

 

while (1) {
printf("\n>");
c = fgetc(stdin);

 

switch (c) {
case 'c':
printf("\nEnter number of CAN Tasks to create: ");
while (1 != scanf("%d", &numCanTasks)) {
printf("Invalid input, enter number of CAN tasks to create: ");
}

 

if (numCanTasks>10) {
numCanTasks=10;
}
for (i=0; i<numCanTasks; i++) {
ids[i] = _task_create(0, CAN_TASK, i );
printf("\nCreated task 0x%08x", ids[i]);
}
break;

 

case 'd':
for (i=0; i<10; i++) {
if (ids[i]) {
printf("\nDestroying task 0x%08x", ids[i]);
_task_destroy(ids[i]);
ids[i] =0;
}
}
break;

 

case 'a':
for (i=0; i<10; i++) {
if (ids[i]) {
printf("\nAborting task 0x%08x", ids[i]);
_task_abort(ids[i]);
ids[i] =0;
}
}
break;

 

case 'r':
for (i=0; i<10; i++) {
if (ids[i]) {
printf("\nRestarting task 0x%08x", ids[i]);
_task_restart(ids[i],&i, FALSE);
}
}
break;

 

case 'h':
printf("\nc- create can tasks");
// printf("\na- abort can tasks");
printf("\nd- destroy can tasks");
// printf("\nr- restart can tasks");
printf("\nh- help");
break;

 

default:
printf("Invalid selection!\n");
break;

}
}

 

_task_block();
}

Outcomes