Keil Logo

RTX166 TINY: Using Printf, Sprintf, Scanf, and Sscanf


Information in this article applies to:

  • RTX166 Tiny All Versions

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 \KEIL\C166\RTX_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 recommend 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 ARTX-166. So you may want to consider using ARTX-166.

MORE INFORMATION

  • Refer to printf in the C166 User's Guide.
  • Refer to scanf in the C166 User's Guide.
  • Refer to sprintf in the C166 User's Guide.
  • Refer to sscanf in the C166 User's Guide.

Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.