Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Debug
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Debug

by syrox on Fri Aug 26, 2011 9:48 am

AVR newbie here. . .

I'll pose the question first to save some read time:
What do I need to do to in-circuit debug? I've got a ATTiny4313 (same as 2313 for most intents and purposes) and a USBTinyISP. I'm using a 2313 target board from Evil Mad Science (which is just a minimal board).

I'm looking to be able to debug code better, for example when reading an analog value, actually see what it is on my computer. I'm assuming this would be through the debug


Back story:
I've spent a considerable amount of time learning the basics of AVR programming, and how to get things set up. At this point, I'm thinking of writing a tutorial since it's taken a long time to find the right set of information to get a simple program running. I'm not afraid of a challenge, but there's a lot of simple basic questions that continue unanswered. All that said, it's so exciting to delve into the world of embedded programming, and to have a great community for support.

Anyway, to the matter at hand, I've tried finding the easy answer to this question, and would be very appreciative if someone could set me straight on this matter.
syrox
 
Posts: 2
Joined: Fri Aug 26, 2011 9:35 am

Re: Debug

by brucef on Fri Aug 26, 2011 12:30 pm

My understanding is that Atmel's AVR debugging protocols are proprietary and unpublished, so the only way you can get at them is to use Atmel's debugging hardware and software.

Apparently you can debug the 4313 with the AVR dragon, so that plus Atmel's free AVR Studio software (Windows only) is probably your cheapest way to do any on-chip debugging.

Beyond that I'm afraid you're stuck with the embedded version of "printf debugging", which means if you want to see an ADC value for example you'll need to have your program read the value and chuck it out on your serial line.
- Bruce

brucef
 
Posts: 215
Joined: Tue May 03, 2011 4:51 pm

Re: Debug

by syrox on Fri Aug 26, 2011 1:06 pm

BruceF wrote:My understanding is that Atmel's AVR debugging protocols are proprietary and unpublished, so the only way you can get at them is to use Atmel's debugging hardware and software.

Apparently you can debug the 4313 with the AVR dragon, so that plus Atmel's free AVR Studio software (Windows only) is probably your cheapest way to do any on-chip debugging.

Beyond that I'm afraid you're stuck with the embedded version of "printf debugging", which means if you want to see an ADC value for example you'll need to have your program read the value and chuck it out on your serial line.


Thank you for the reply!

I believe that using printf would be easy enough, but I'm not sure what steps need to be taken to establish a serial line. I'm using the USBTinyISP, and I understand that a serial connection cannot be established through this? So what's the best option from there?

I guess I'll get a dragon. . . doesn't seem horribly expensive, but it would still be great to know more about serial debugging. Can you point me in the right direction for learning more about that?

Thanks
syrox
 
Posts: 2
Joined: Fri Aug 26, 2011 9:35 am

Re: Debug

by brucef on Fri Aug 26, 2011 5:56 pm

Word to the wise: I'm not an expert, I'm just a little ahead of you on microcontroller stuff myself. Hopefully somebody more experienced will chime in if I say something sufficiently stupid.

I think the easiest way to get a serial connection to a 4313 would be to get a USB FTDI TTL-232 cable and figure out the connections required to get it talking to your MCU's USART port. Then maybe start with a 'hello world' program on the MCU that just chats to the USART continuously and see if you can get the text to show up in some kind of window on your computer.

What OS are you running on the computer end of things, anyway? I'm on a Mac, so the PC options may differ slightly. I just got that cable and I'm expecting to try talking to my MCU (an xmega, so quite different from yours) using 'screen' in Terminal.app, where I *think* the easiest way to see this stuff on Windows might involve HyperTerminal.

The other thing I should mention is that in my investigation of the AVR Dragon, I came across a lot of chatter that it's easy to kill part of the board by doing something wrong, and that doing that wrong thing was disconcertingly easy. I have to run right now, but I'll try to dig that up later. If you wander over to the forums at avrfreaks.net, you may be able to find that info yourself. I wouldn't use that board until I knew about that issue, myself. Sorry to be dumping vague recollections on you, but I have to run.
- Bruce

brucef
 
Posts: 215
Joined: Tue May 03, 2011 4:51 pm

Re: Debug

by uhe on Sat Aug 27, 2011 8:27 am

The only thing I'd add to what BruceF said:
Code: Select all | TOGGLE FULL SIZE
/** ATTiny 2313 USART example */
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/io.h>

#ifndef F_CPU
#define F_CPU 14745600    //14,7456 Mhz crystal
#endif

// UART
#define UART_BAUD_RATE 9600
#define UART_BAUD_CALC(UART_BAUD_RATE,F_OSC) ((F_CPU)/((UART_BAUD_RATE)*16L)-1)

volatile unsigned char get = 'a'; 

int main(void) {   
  UBRRH = (uint8_t) (UART_BAUD_CALC(UART_BAUD_RATE,F_CPU)>>8);
  UBRRL = (uint8_t)  UART_BAUD_CALC(UART_BAUD_RATE,F_CPU);

  UCSRB |= _BV(RXEN) | _BV(TXEN);     // enable receiver and transmitter
  UCSRC = (3<<UCSZ0);                 // 8 bit (default: asynchronous, no parity, 1 stop-bit)

  UCSRB |= _BV(RXCIE) | _BV(TXCIE);   // enable RX/TX IRQs
 
  while (!(UCSRA & (1<<UDRE)));       // wait for empty transmit buffer
  UDR = 'x';                          // put data into buffer, sends the data
 
   sei();                                         // global IRQ enable   
   while(1){    }
   
   return 0;
}


/* USART Receive Compete Interrupt
 * - needs RXCIE bit in UCSRB reg!
 * receive a char and send it back
 */
ISR(USART_RX_vect) {
  get = UDR;
  UDR = get;
}

/* USART Transmit Compete Interrupt
 * - needs TXCIE bit in UCSRB reg!
 */
ISR(USART_TX_vect) {
  //UDR = get;
}

/* USART Data Register Empty
 * - gets called when data register is empty
 * - needs UDRIE bit in UCSRB reg!
 */
//ISR(USART0_UDRE_vect) { }

//EoF

Use the while loop or the IRQ to determine if the transmit buffer is empty.

HTH
"I have not failed. I've just found 10,000 ways that won't work." - Thomas Edison
uhe
 
Posts: 178
Joined: Mon Sep 03, 2007 4:50 pm
Location: metric world

Please be positive and constructive with your questions and comments.