Win avr presentation_2006


Published on

Published in: Self Improvement, Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Win avr presentation_2006

  1. 1. WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06
  2. 2. Introduction <ul><li>WinAVR is a set of developing tools for Atmel AVR RISC microprocessors </li></ul><ul><li>Programs written in C, compiled with GCC and avr-libc </li></ul><ul><li>Open source, obtained at: </li></ul>
  3. 3. Installing <ul><li>Fairly easy to install (for use at home) </li></ul><ul><li> </li></ul><ul><li>Comes with all the tools you need: Programmer’s Notepad, MFile </li></ul>
  4. 4. Programmer’s Notepad <ul><li>Programmer’s Notepad is the main tool you will be using to write your C code </li></ul><ul><li>Just like any other compiler </li></ul><ul><ul><li>syntax highlighting </li></ul></ul><ul><ul><li>Support for different programming languages </li></ul></ul><ul><ul><li>Ability to compile your code (using gcc compiler) </li></ul></ul>
  5. 5. Adding external tools <ul><li>In Programmer’s Notepad, select Tools->Options, and select “Tools” on the left side of screen </li></ul>
  6. 6. Adding external tools (continued) <ul><li>Select a Scheme (C/C++) </li></ul><ul><li>Click on “Add” </li></ul><ul><li>“ Name” is an identifier for this tool </li></ul><ul><li>Command is the command used </li></ul><ul><li>Folder should be %d (Path of file) </li></ul><ul><li>This tool will call “make extcoff”, and is now available under “Tools” menu </li></ul><ul><li>We will need this later for debugging in AVR Studio </li></ul>
  7. 7. Example C Program <ul><li>// blinky.c </li></ul><ul><li>#include <avr/io.h> // Standard AVR header </li></ul><ul><li>#include <avr/delay.h> // Delay loop functions </li></ul><ul><li>int main(void) </li></ul><ul><li>{ </li></ul><ul><li>DDRA = 0xFF; // PORTA is output </li></ul><ul><li>while (1) { </li></ul><ul><li> for (int i=1; i<=128; i*=2) { </li></ul><ul><li>PORTA = i; </li></ul><ul><li>_delay_loop_2(30000); </li></ul><ul><li> } </li></ul><ul><li> for (int i=128; i>1; i/=2) { </li></ul><ul><li>PORTA = i; </li></ul><ul><li>_delay_loop_2(30000); </li></ul><ul><li> } </li></ul><ul><li>} // end while </li></ul><ul><li>} </li></ul>
  8. 8. Building your source <ul><li>Write your C source, save as blinky.c </li></ul><ul><li>Open up MFile </li></ul><ul><ul><li>Makefile -> Main File Name = blinky (no .c) </li></ul></ul><ul><ul><li>Makefile -> MCU Type = atmega32 </li></ul></ul><ul><li>Other values should be fine at default </li></ul><ul><li>File -> Save As to blinky.c directory </li></ul><ul><li>In Programmer’s Notepad: Select Tools ->Make All </li></ul>
  9. 9. MFile <ul><li>Simple program to make Makefiles for compiling your C code </li></ul><ul><li>A Makefile is a configuration file that tells the compiler how to compile your code </li></ul><ul><ul><li>What chip you’re using (atmega32) </li></ul></ul><ul><ul><li>Target filename (blinky.c) </li></ul></ul>
  10. 10. GNU Make <ul><li>WinAVR uses “Makefiles” when building projects, with GNU Make </li></ul><ul><li>GNU Make builds dependencies and then source files </li></ul><ul><li>Will only rebuild files from updated or new source (saves time) </li></ul><ul><li>Very powerful tool: see C:WinAVRdocgnumake.html for more info </li></ul>
  11. 11. Makefiles (continued) <ul><li>Makefiles are tab-sensitive: tab != space </li></ul><ul><li>Lines starting with tab are executed as commands </li></ul><ul><li>Misuse of tabs will lead to “improper separator” error </li></ul>
  12. 12. Example Makefile <ul><li>## ‘all’ and ‘clean’ targets must be defined! </li></ul><ul><li># ‘make’ or ‘make all’ will build dependencies in the order they are given </li></ul><ul><li>all: begin project2 end </li></ul><ul><li>begin: </li></ul><ul><li>@echo “Starting build” </li></ul><ul><li>project2: </li></ul><ul><li>avr-gcc project2.c </li></ul><ul><li>end: </li></ul><ul><li>@echo “Build complete” </li></ul><ul><li>clean: </li></ul><ul><li>rm project2.o </li></ul>
  13. 13. Programming your ATmega32 <ul><li>To program your chip with the C code you wrote: </li></ul><ul><ul><li>Go to AVR Studio </li></ul></ul><ul><ul><li>Connect to your chip using JTAG ICE </li></ul></ul><ul><ul><li>Go to Fuses tab, make sure Ext Clock is set </li></ul></ul><ul><ul><li>In Program tab, flash your chip with the .hex file you compiled in Programmer’s Notepad </li></ul></ul><ul><ul><li>That’s it! </li></ul></ul>
  14. 14. C Debugging in AVR Studio <ul><li>Use the editor to edit the flags in makefile : </li></ul><ul><ul><li>DEBUG = stabs // will allow for C debugging </li></ul></ul><ul><ul><li>OPT = 0 // will turn off compiler’s optimization </li></ul></ul><ul><li>Save the makefile </li></ul>AVR Studio provides a way to debug both the C source code and the assembly code. To do that you just need to change the type of the COF file generated by the compiler. Open the Programmer’s Notepad (WinAVR)
  15. 16. Building C code for Debugging <ul><li>Go to Tools  Options  Tools </li></ul><ul><li>Pull down the Schemes window and click on C/C++ option </li></ul><ul><li>A make extcoff option will appear; click OK </li></ul><ul><li>Click on Tools again </li></ul><ul><li>Click on make extcoff to generate the COF file </li></ul><ul><li>(this will generate the correct debug file for AVR Studio that includes the C code information) </li></ul>In the Programmer’s Notepad (WinAVR):
  16. 18. Debugging in AVR Studio <ul><li>Connect Olimex to PC and JTAG </li></ul><ul><li>Open the AVR Studio </li></ul><ul><ul><li>Open the COF file </li></ul></ul><ul><ul><li>(it will guide you to select the debug platform (JTAG) and the device (Atmega 32)) </li></ul></ul><ul><ul><li>The C code will appear in the main window </li></ul></ul><ul><ul><li>Optional (useful!) view the assembly code: </li></ul></ul><ul><ul><ul><li>go to View  Dissassembler </li></ul></ul></ul><ul><ul><li>You can put assembly code next to C code </li></ul></ul><ul><ul><ul><li>( tile vertically ) and step through both codes! </li></ul></ul></ul>
  17. 20. Additional Information <ul><li>Additional Information can be found at the WinAVR website: </li></ul><ul><ul><ul><li> </li></ul></ul></ul>