Printf style debugging over AVR-ISP pins!

Hi everyone!

Recently, i implemented the capability of printf style debugging over AVR-ISP pins with the Little Wire. Main motivation is that if you are breadboarding an AVR chip, you are already using the SPI pins for programming the device; therefore it would be nice to use that cable for low-level debugging also. I wrote an Arduino library for that purpose and it is working! That means, you can set up an Arduino on a breadboard and use the Little Wire as programmer and also console interface for printf style debugging! But, you should tie down the SS pin to the ground as well.

Main idea is simple. Arduino act as a SPI slave. It has a transmit buffer available and when you want to send a message, it loads that message into buffer first and opens the SPI peripheral. As a protection, when Arduino wants to send a message, it first checks whether that there is an ongoing message sending process or not. If there is message sending activity, it first waits it to finish and then starts to send the next one. User also can check it by using the isBusy(); method.

/* Send the new message buffer */
void LittleWire::print(uint8_t* buffer,uint8_t new_length)
{
	while(lw_messageSent==0);
	lw_length=new_length+1;
	lw_txBuffer=buffer;
	lw_index=0;
	lw_messageSent = 0;
	SPCR = lw_register;
}

Later, when Little Wire requests response over the SPI, Arduino shifts the index of the buffer each time. When it reaches the end of the message, closes the SPI interface and sets the “messageSent” flag.

/* SPI transfer complete interrupt */
ISR(SPI_STC_vect)
{
  if(lw_messageSent==0)
  {
    SPDR = lw_txBuffer[lw_index++]; /* Send a new char */
    if(lw_index==lw_length)
    {
	  lw_index=0;
	  lw_messageSent=1;
	  SPCR=0;
    }
  }
}

Here is a basic example with this library. User can either print static predefined strings or they can use sprintf to create formatted strings.

#include <LittleWire.h>
LittleWire lw;

uint16_t analogValue;
char txBuffer[32];

void setup()
{
  lw.begin();
  lw.print("n");
  lw.print("Prints analog_in #0 to console!n");
  lw.print("Program started!n");
  delay(2000);
}

void loop()
{
  analogValue=analogRead(0);
  sprintf(txBuffer,"Value: %dn",analogValue);
  lw.print(txBuffer);
}

Computer interface constantly sends dummy SPI messages, when it gets other than NULL response, it prints the received character back to the console. You can look for the computer interface program and the Arduino library at the GitHub page. Below you can find a working example output!

Feel free to comment!
ihsan.

9 thoughts on “Printf style debugging over AVR-ISP pins!

  1. Great idea! A real innovation!

    Since I (and thousands of other people out there) already have a USBTinyISP programmer I was wondering if this could be used as a debugging tool as well.

  2. Pingback: Using printf-style output to debug Arduino - Hack a Day

  3. Pingback: Using printf-style output to debug Arduino » Geko Geek

  4. Hi,

    I am very impressed with your little wire project. I am experimenting with your hex files.

    I have one basic doubt. In which USB interface PC software communicates with the tinay85 . ( USB serial or any other ) ?

    Thanks
    Naveen

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>