about printf and sprintf

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

about printf and sprintf

ソリューションへジャンプ
2,778件の閲覧回数
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,930件の閲覧回数
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,931件の閲覧回数
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,930件の閲覧回数
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