about printf and sprintf

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

about printf and sprintf

Jump to solution
1,831 Views
goodluck
Contributor I

I use mc9s12xdp512 kit.

 

my code uses many sprintf and printf.


method I use is add termoi.c and stdio.h


but my main code is not work.


My kit to communicate with other kit.but It will stop with the error and display illegal bp.


I think that it is caused by sprintf. The reason is when I remove the sprintf, since it works well.       


what is the problem? help me                                                                                                                                                                                                                                           

 

Labels (1)
Tags (3)
0 Kudos
1 Solution
983 Views
RadekS
NXP Employee
NXP Employee

termio.c contains general code which has to be updated for your application (TERMIO_Init function, baud rate according your bus clock).

illegal bp means that debugger detects some unexpected behavior like unexpected interrupt or reset.

Here you can find example code for Interrupt catcher (catch unexpected interrupts):

https://community.freescale.com/docs/DOC-93775

Here is most simple example code for sprintf function and SCI module (without termio.c):

#include <stdio.h>

void main(void) {

  unsigned int i;

  char text[30];    // must be longer than string

  char result = 0x42;

 

  SCI0BD = 0x0034;  // SCI baud rate = SCI bus clock / (16 * SCIBR[12:0]) = 8MHz/(16*9600)= 0x34

  SCI0CR2 = 0x08;   // transmitter enable

 

(void)sprintf(text,"Hello World, answer is : %X \n\r",result);

  i=0;

  while(text[i]!='\0')

  {

   while(!SCI0SR1_TC);

   SCI0DRL=text[i++];

  }

  for(;;) {} /* loop forever */

}

I hope that help you.


View solution in original post

0 Kudos
2 Replies
984 Views
RadekS
NXP Employee
NXP Employee

termio.c contains general code which has to be updated for your application (TERMIO_Init function, baud rate according your bus clock).

illegal bp means that debugger detects some unexpected behavior like unexpected interrupt or reset.

Here you can find example code for Interrupt catcher (catch unexpected interrupts):

https://community.freescale.com/docs/DOC-93775

Here is most simple example code for sprintf function and SCI module (without termio.c):

#include <stdio.h>

void main(void) {

  unsigned int i;

  char text[30];    // must be longer than string

  char result = 0x42;

 

  SCI0BD = 0x0034;  // SCI baud rate = SCI bus clock / (16 * SCIBR[12:0]) = 8MHz/(16*9600)= 0x34

  SCI0CR2 = 0x08;   // transmitter enable

 

(void)sprintf(text,"Hello World, answer is : %X \n\r",result);

  i=0;

  while(text[i]!='\0')

  {

   while(!SCI0SR1_TC);

   SCI0DRL=text[i++];

  }

  for(;;) {} /* loop forever */

}

I hope that help you.


0 Kudos
983 Views
CompilerGuru
NXP Employee
NXP Employee

Could also be a stack overflow, as (s)printf needs quite a bit of stack space.

Also, as the OP guesses sprintf might be the issue (which is not using termio at all), the usual printf issues might be causing it too.

- non matching arguments to the format

- sprintf (as it is defined by ANSI) does not support to check for destination buffer overflows

- stack space overflows


Also, wondering if in this statement

> The reason is when I remove the sprintf, since it works well.

calls to sprintf only were removed while keeping printf calls, or were both sprintf and printf calls removed?

printf is the only one using termio, so if the issue is with sprintf only, then termio is out of the picture.


I did once post a trivial snprintf version here, but I don't see it attached in this thread


https://community.freescale.com/message/17411#17411


Daniel