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.
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.
Hi,
Thanks for your kind words!
I’ve read that the original USBTinyISP firmware also have the capability of sending/receiving arbitrary SPI messages. I don’t have one at hand therefore i can’t test it.
But basic idea is global. Shouldn’t be hard to port the code and library.
ihsan.
While the port of the code itself is beyond my own skills a little research about the subject is not. Some people tried similar things:
Manderson realized a “generic spi device example w/host + target code”
http://forums.adafruit.com/viewtopic.php?f=20&t=8079
And the official USBTinyISP page at Ladyada gives some hints as well:
http://www.ladyada.net/make/usbtinyisp/use.html
I wrote this a few years back to do just that.
https://github.com/jmesmon/libmuc/tree/master/spi_console_pc
You know what, it looks like I’m miss remembering things. That link is just a modified version of the one by Manderson.
His tool should be exactly what you’re looking for.
Pingback: Using printf-style output to debug Arduino - Hack a Day
Pingback: Using printf-style output to debug Arduino » Geko Geek