ARM Technical Support Knowledge Articles

USING PRINTF, SPRINTF, SCANF, AND SSCANF

Applies to: RTX166 Tiny Real-time Kernel

Answer


Information in this article applies to:


QUESTION

I have a very simple program that uses Round-Robin task scheduling and the printf function. Sometimes the output from sprintf and printf is corrupted as demonstrated by the following test code.

void task1() _task_ 2 {
  static char buffer[20];       // static because of RTX166 Tiny stack
  while (1) {
    sprintf(buffer, "TEST %d;", (int)255);
    if (strcmp(buffer, "TEST 255;") != 0) {
      _nop_();                  // program reaches this point, but
                                // this should never be the case.
    }
  }
}

void task2() _task_ 1 {
    while (1) printf("a");
}

How can I solve this problem.

ANSWER

The RTX166 Tiny README.TXT file found in the KEILC166RTX_TINY folder describes that using pointers to stack-based variables is not safe.

The printf, sprintf, scanf, and sscanf library routines (as well as any other routines with variable-length argument lists) use pointers to access the parameters (which are passed on the stack). Since RTX166 may reorganize the stack after a task switch, you see the strange output results that you do.

The result is that any function which uses variable-length argument lists (like printf, scanf, and so on) will not work as expected.

RESOLUTION

We recommed that you disable the RTX Tiny Hardware Timer Interrupt for those routines that access the stack via pointer. For example:

.
.
.
T0IE = 0;   // disable RTX Timer interrupt
sprintf(buffer, "TEST %d;", (int)255);
T0IE = 1;   // enable RTX Timer interrupt
.
.
.
T0IE = 0;   // disable RTX Timer interrupt
printf("a");
T0IE = 1;   // enable RTX Timer interrupt    }
.
.
.

Note that this problem does not exist with RTX166 Full. So you may want to consider using RTX166 Full.

MORE INFORMATION

Article last edited on: 2005-10-20 07:49:45

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential