Minimalistic Tracing

swIf you are just in the middle of deep debugging of your IQRF application and you need a simple runtime tracing or debugging tool with a small HW and memory footprint then the following code is just for you. It actually implements minimalistic software UART TX available over any free output MCU pin.

All you have to do on the HW side is to connect you application with a terminal program and watch the output. You will probably use 3.3V TTL UART to USB converter connected to the PC USB port and your favorite terminal application.

On the SW side just [1] choose the MCU pin to use and [2] specify UART baud rate. Then [3] call once SwTxInit() to initialize the tracing and every time you want to send one byte [4] call SwTxSend( data ).

The following example shows both the tracing implementation as well as its usage:

// IQRF Header
#include "template-basic.h"

// MCU pin used for SW UART TX [1]
#define    SWTX_PORT PORTA.0
#define    SWTX_TRIS TRISA.0
// SW UART baud rate (115200 is maximum) [2]
#define    SWTX_BAUD115200

void SwTxInit();
void SwTxSend( uns8 data );

void APPLICATION( void )
{
  // Initialize SW UART TX [3]
  SwTxInit();

  // Example of sending UART data [4]
  SwTxSend( 'H' );
  SwTxSend( 'e' );
  SwTxSend( 'l' );
  SwTxSend( 'l' );
  SwTxSend( 'o' );

  uns8 value = 0;
  for ( ;; )
  {
    waitDelay( 100 );
    SwTxSend( ++value );
  } 
}


void SwTxInit()
{
  SWTX_TRIS = 0;
  SWTX_PORT = 1;
}

void SwTxDelay()
{
  #define    SWTX_LOOPS    ( (2 * 1000000 / SWTX_BAUD / 3 ) - 5 )

  #if SWTX_LOOPS == 0
  nop();
  nop();
  nop();
  #else
  uns8 i = SWTX_LOOPS;
  #pragma update_RP 0 /* OFF */
  do {} while ( --i != 0 );
  #pragma update_RP 1 /* ON */
  #endif
}

void SwTxSend( uns8 data )
{
  GIE = 0;

  SWTX_PORT = 0;  // start bit
  SwTxDelay();
  uns8 i = 8;
  do
  {
  #pragma update_RP 0 /* OFF */
  data = rr( data );
  #pragma update_RP 1 /* ON */
  if ( Carry )
    SWTX_PORT = 1;
  else
  {
    SWTX_PORT = 0;
    nop();
  }
  SwTxDelay();
  } while( --i != 0 );

  SWTX_PORT = 1;  // stop bit
  SwTxDelay();
  SwTxDelay();
  SwTxDelay();
  SwTxDelay();

  GIE = 1;
}

Post sent by Hynek Syrovátka.

Leave a Reply

Your email address will not be published. Required fields are marked *