Why does MQX crash when using scanf ?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Why does MQX crash when using scanf ?

551 Views
danny30
Contributor I

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();
}

Tags (2)
0 Kudos
1 Reply

370 Views
danielchen
NXP TechSupport
NXP TechSupport

Hi Adrian:

I put the following code regarding scanf into the hello world demo, and my result is it can work.

    uint32_t a;
    printf("Please input : a   \r\n");
    scanf("%d", &a);
    printf("a= %d", a);

I tried this with sdk_1.3_FRDM-K64F and classic MQX 4.2,  with  IAR 7.70.1,  and it works as expected.  Could you double check it ?

Regards

Daniel

0 Kudos