about printf and sprintf

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

about printf and sprintf

跳至解决方案
1,855 次查看
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                                                                                                                                                                                                                                           

 

标签 (1)
标记 (3)
0 项奖励
1 解答
1,007 次查看
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 项奖励
2 回复数
1,008 次查看
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 项奖励
1,007 次查看
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