This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to rework "printf" function to use for two UARTS in the cygnal's MCU?

Hi,everyone!
A problem here:The cygnal's MCU has two
serial ports,so "printf" function must be reworked.But I don't know how to do it.
Give me a hand!
Thanks a lot!

  • You can rework putchar to use both ports. There is also sprintf which knows nothing about ports.
    Regards,
    - Mike

  • See my UART driver example at http://www.embeddedfw.com for how to do this. Basically, you just re-define putchar() which Keil's printf() calls. When ever you override a library function, the linker will take your function and not the library version.

    - Mark

  • Note that the printf library function is huge - it has to be to support all those formats & options!

    I think you'll find that the classic "Hello, world" first 'C' program will take up nearly all the 2K codespace limit of the C51 Eval version!

    For embedded stuff, it's usually better to write your own routines, implementing only those specific features which are actually needed in your application.
    You could then include a parameter which specifies which serial port to use.

    I did this, and included a "null" value for the port - this makes it easy, for example, to turn debug output on & off without obfuscating your mainline code with loads of "if( debug )" clauses.

  • Instead of if (debug) clauses, try this:

    #ifdef DBG
    #    define debug
    #else
    #    define debug while (0)
    #endif
    
    // Usage: single line
    debug printf("Got here\n");
    
    // Usage: multiple line
    debug
    {
        g_errno = 12;
        printf("Got error, %d\n", g_errno);
    }

    Now watch the magic happen when you don't define DBG.

    Regards.

    - Mark

  • Mark,

    Yes, of course, that gives you compile-time control to produce an image which either has debug output or doesn't.

    I needed the ability to enable/disable the debug output at run-time

    In fact, I did also use conditional-compilation so that I could build either a completely "clean" (debug-free) image, or one with the runtime-controllable debug

  • Hi,My friends.
    I will have a try!
    Thank you very much!

  • This is a start on this problem:

    //-----------------------------------------------
    #include "stdio.h"
    
    volatile unsigned char sio_selector = 0;
    
    //-----------------------------------------------
    char putchar (char c)
    {
    if (sio_selector == 0)
      {
      while (!TI_0);  /* wait until transmitter ready */
      TI_0 = 0;
      SBUF0 = c;      /* output character */
      }
    else
      {
      while (!TI_1);  /* wait until transmitter ready */
      TI_1 = 0;
      SBUF1 = c;      /* output character */
      }
    
    return (c);
    }
    
    
    //-----------------------------------------------
    char _getkey (void)
    {
    char c;
    
    if (sio_selector == 0)
      {
      while (!RI_0);  /* wait for a character */
      c = SBUF0;
      RI_0 = 0;       /* clear receive interrupt bit */
      }
    else
      {
      while (!RI_1);  /* wait for a character */
      c = SBUF1;
      RI_1 = 0;       /* clear receive interrupt bit */
      }
    
    return (c);
    }
    
    
    //-----------------------------------------------
    void main (void)
    {
    //Initialize SIO0
    //Initialize SIO1
    
    while (1)
      {
      sio_selector = 0;
      printf ("Serial Port 0\r\n");
    
      sio_selector = 1;
      printf ("Serial Port 1\r\n");
      }
    }
    //-----------------------------------------------

    Of course this is polled and uses the putchar and _getkey functions. You'll have to provide your own code to initialize the serial ports and timers.

    Jon