UART Printf

From DSignT Support Database
Jump to: navigation, search

Printf via UART

#include <stdio.h>

#define UART_PRINTFBUF_SIZE 256

T_Handle h_uart;


/*******************************************************************//**
 @brief     printf output via UART
 
 @param     format - format string, see printf
 @return    -
 @note      make sure not to exceed UART_PRINTFBUF_SIZE
***********************************************************************/
void UARTPrintf(const char *format, ...)
{
    /***************************************************************************
      locals
    ***************************************************************************/
    va_list argptr;
    static char buffer[UART_PRINTFBUF_SIZE];
    int32_t len;
    
    /***************************************************************************
      initialize variable argument list
    ***************************************************************************/
    va_start (argptr, format);

    /***************************************************************************
      print to buffer
    ***************************************************************************/
    len = vsprintf (buffer, format, argptr);
  
    /***************************************************************************
      check buffer overflow
      exit on overflow
    ***************************************************************************/
    if (len >= UART_PRINTFBUF_SIZE)
    {
        exit (0xFF);
    }
  
    /***************************************************************************
      output string via UART
    ***************************************************************************/
    DM2_uartWriteStr (h_uart, buffer, DM2_UART_NOTIMEOUT);
    
    /***************************************************************************
      end variable argument list
    ***************************************************************************/
    va_end(argptr);
}


Contact Post.png Support Tool.png