EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

printf streams

Started by Darren Logan April 18, 2005
'ello,
 
i've use printf on UART0 on an F149
what's the easiest way to switch between say two (or more) ports using
printf (e.g. switch between UART0 and UART1)?
 
with some PIC C compilers i've used, you put the stream name within the
printf arguments and the compiler routes the
printf to that stream
 
duzzent look like it's as easy wiv IAR C, or am i wrong?

	Vbr,

	Darren

 





Beginning Microcontrollers with the MSP430

> i've use printf on UART0 on an F149
>
> what's the easiest way to switch between say two (or more) ports
> using printf (e.g. switch between UART0 and UART1)?
>
> with some PIC C compilers i've used, you put the stream name within
> the printf arguments and the compiler routes the printf to that
> stream
>
> duzzent look like it's as easy wiv IAR C, or am i wrong?

Well, it depends on how you would like to do it, basically there are
two basic approaches.

* In your low-level output routine you can check a global variable and
  send the output in the right direction.  (If you're using CLib that
  is "putchar", in DLib that is the function __write.)

  So, for example:

  int useUart = 0;

  size_t __write(int handle, const unsigned char *buf, size_t size)
  {
    if (buf)
    {
      if (useUart == 0)
      {
         /* Print to UART0 */
      }
      else
      {
         /* Print to UART1 */
      }
    }
  }


  And in the code that use it:

  {
    useUart = 0;
    printf("Hello world\n");

    useUart = 1;
    printf("Hello moon\n");
  }


* The second method is perhaps a bit overkill.  It only works with the
  full configuration of the DLib library (or a custom configuration
  with FILE enabled.)  Then you could map different file handles to
  different ports and use fprintf.  For example:

  size_t __write(int handle, const unsigned char *buf, size_t size)
  {
    if (buf)
    {
      if (handle == 0)
      {
         /* Print to UART0 */
      }
      else
      {
         /* Print to UART1 */
      }
    }
  }


  And in the code that use it:

  {
    fprintf(uart0, "Goodbye world\n");
    fprintf(uart1, "Goodbye mood\n");
  }

    -- Anders Lindgren, IAR Systems
-- 
Disclaimer: Opinions expressed in this posting are strictly my own and
not necessarily those of my employer.


The 2024 Embedded Online Conference