printf, sprintf, chprintf ?

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
User avatar
Nemesis2065
Posts: 20
Joined: Fri Nov 15, 2013 9:53 am

printf, sprintf, chprintf ?

Post by Nemesis2065 »

Hello,

I am searching for a printf function in ChibiOS. I have looked into the forum and over internet but I am confused... I did not find any topic on what I need.
I don't get the differences between : sdWrite, chprintf, sprintf...

In my project I am trying to display the accelerometer value on SD3, like this :

Code: Select all

static msg_t Thread_Accelerometer(void *arg)
{
   UNUSED(arg);
   chRegSetThreadName("Accelerometer");

   spiStart(&SPID1, &spi1cfg);
   //spiStart(&SPID2, &spi2cfg);

   /*******************************************************
   * Initializes the LIS302DL Accelerometer
   ********************************************************/
   //  CTRL_REG1
   //   DR | PD | FS | STP | STM | Zen | Yen | Xen |
   //   0  | 1  | 0  |  0  |  0  |  0  |  1  |  1  | = 0x43
   //   0  | 1  | 0  |  0  |  0  |  1  |  1  |  1  | = 0x47
   lis302dlWriteRegister(&SPID1, LIS302DL_CTRL_REG1, 0x43);
   lis302dlWriteRegister(&SPID1, LIS302DL_CTRL_REG2, 0x00);
   lis302dlWriteRegister(&SPID1, LIS302DL_CTRL_REG3, 0x00);

   static sint8  x_Accelero_si8, y_Accelero_si8;
   char data[] = "Data Accelerometer on SD3 : \r\n";

   while (TRUE)
   {
      palClearPad(GPIOD, GPIOD_LED4);

      /* Reading MEMS accelerometer X and Y registers */
      x_Accelero_si8 = lis302dlReadRegister(&SPID1, LIS302DL_OUTX);
      y_Accelero_si8 = lis302dlReadRegister(&SPID1, LIS302DL_OUTY);

      //if(x_Accelero_si8 >0) {palSetPad(GPIOD, LED_RED);} else {palClearPad(GPIOD, LED_RED);}
      //if(y_Accelero_si8 >0) {palSetPad(GPIOD, LED_RED);} else {palClearPad(GPIOD, LED_RED);}


      chprintf((BaseSequentialStream *) &SD3, "%s", data);
      chprintf((BaseSequentialStream *) &SD3, "x_Accelero : %.2f \r\n", x_Accelero_si8);
      chprintf((BaseSequentialStream *) &SD3, "y_Accelero : %.2f \r\n", y_Accelero_si8);


       /* Transmitting accelerometer data over SPI2 */
       //spiSelect(&SPID2);
       //spiStartSendI(&SPID2, 20, &data);
       //spiUnselect(&SPID2);

      palClearPad(GPIOD, LED_GREEN);
      chThdSleepMilliseconds(500);
      palSetPad(GPIOD, LED_GREEN);
      chThdSleepMilliseconds(500);
   }
   return 0;
}


Am I doing it right ? Because I am receiving this on the computer :
x_Accelero : f
y_Accelero : f

Thanks in advance,
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: printf, sprintf, chprintf ?

Post by Tectu »

All three functions do have a different job and are part of a different 'software layer':
  • sprintf() is a standard clib function and simply prints a formatted string to a buffer. It is not from ChibiOS/RT itself. See this.
  • sdWrite() is part of the serial HAL driver. See this.
  • chprintf() is like an ordinary printf() but output is 'sent' to a stream instead of stdout. See this.

To display something over the UART, you can simply use chprintf() using the BaseSequentialStream pointer to SD6:

Code: Select all

chprintf((BaseSequentialStream*)&SD6, "This is some message with a value: %d\r\n", 42);

See /testhal to find examples about how to set up the SD peripheral (sdStart()).

~ Tectu
skute
Posts: 64
Joined: Wed Aug 29, 2012 10:17 pm

Re: printf, sprintf, chprintf ?

Post by skute »

Nemesis2065 wrote:Hello,
Am I doing it right ? Because I am receiving this on the computer :
x_Accelero : f
y_Accelero : f


I'm not sure if floating point is supported by default. Do you have CHPRINTF_USE_FLOAT defined?
tinito
Posts: 112
Joined: Tue Jun 07, 2011 10:32 am
Has thanked: 1 time
Been thanked: 1 time

Re: printf, sprintf, chprintf ?

Post by tinito »

If you need to print integers you should use "%d" specifiers in your format string instead of "%f".
User avatar
Nemesis2065
Posts: 20
Joined: Fri Nov 15, 2013 9:53 am

Re: printf, sprintf, chprintf ?

Post by Nemesis2065 »

Hi everybody,

Thanks for the support, it is now working well (fault to %f instead of %d -_-' )
akshaim
Posts: 20
Joined: Tue Sep 21, 2021 9:47 am
Has thanked: 9 times

Re: printf, sprintf, chprintf ?

Post by akshaim »

Hi,

Is Chprintf /printf thread safe ? Also is there a thread-safe way to enforce printing all characters in the buffer ?

Regards,

Akshai M
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: printf, sprintf, chprintf ?

Post by Giovanni »

Hi,

The function is thread safe and it does no internal buffering.

Giovanni
akshaim
Posts: 20
Joined: Tue Sep 21, 2021 9:47 am
Has thanked: 9 times

Re: printf, sprintf, chprintf ?

Post by akshaim »

Hi Giovanni,

Thanks for the reply.

Regards,

Akshai
User avatar
RoccoMarco
Posts: 655
Joined: Wed Apr 24, 2013 4:11 pm
Has thanked: 83 times
Been thanked: 67 times

Re: printf, sprintf, chprintf ?

Post by RoccoMarco »

Posting to this thread for future memory.

A detailed article about the matter is now available here
Post Reply