Page 1 of 1

printf, sprintf, chprintf ?

Posted: Wed Dec 18, 2013 9:22 am
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,

Re: printf, sprintf, chprintf ?

Posted: Wed Dec 18, 2013 1:11 pm
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

Re: printf, sprintf, chprintf ?

Posted: Wed Dec 18, 2013 5:53 pm
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?

Re: printf, sprintf, chprintf ?

Posted: Thu Dec 19, 2013 11:59 am
by tinito
If you need to print integers you should use "%d" specifiers in your format string instead of "%f".

Re: printf, sprintf, chprintf ?

Posted: Thu Dec 19, 2013 1:24 pm
by Nemesis2065
Hi everybody,

Thanks for the support, it is now working well (fault to %f instead of %d -_-' )

Re: printf, sprintf, chprintf ?

Posted: Mon Oct 18, 2021 5:15 pm
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

Re: printf, sprintf, chprintf ?

Posted: Mon Oct 18, 2021 5:19 pm
by Giovanni
Hi,

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

Giovanni

Re: printf, sprintf, chprintf ?

Posted: Tue Oct 19, 2021 9:13 am
by akshaim
Hi Giovanni,

Thanks for the reply.

Regards,

Akshai

Re: printf, sprintf, chprintf ?

Posted: Thu Mar 14, 2024 9:08 am
by RoccoMarco
Posting to this thread for future memory.

A detailed article about the matter is now available here