ece 353 winavr and c debugging tutorial by adam bailin ece 353 fall ‘06

20
ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

Post on 20-Dec-2015

228 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

ECE 353

WinAVR and C Debugging Tutorial

By Adam Bailin

ECE 353 Fall ‘06

Page 2: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

2ECE 353

Introduction WinAVR is a set of developing tools for

Atmel AVR RISC microprocessors

Programs written in C, compiled with GCC and avr-libc

Open source, obtained at: winavr.sourceforge.net

Page 3: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

3ECE 353

Installing Fairly easy to install (for use at home) http://winavr.sourceforge.net/ Comes with all the tools you need:

Programmer’s Notepad, MFile

Page 4: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

4ECE 353

Programmer’s Notepad Programmer’s Notepad is the main tool

you will be using to write your C code Just like any other compiler

• syntax highlighting• Support for different programming languages• Ability to compile your code (using gcc

compiler)

Page 5: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

5ECE 353

Adding external tools In Programmer’s Notepad, select Tools->Options,

and select “Tools” on the left side of screen

Page 6: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

6ECE 353

Adding external tools (continued) Select a Scheme (C/C++) Click on “Add” “Name” is an identifier for this

tool Command is the command

used Folder should be %d (Path of

file) This tool will call “make

extcoff”, and is now available under “Tools” menu

We will need this later for debugging in AVR Studio

Page 7: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

7ECE 353

Example C Program// blinky.c#include <avr/io.h> // Standard AVR header#include <avr/delay.h> // Delay loop functions

int main(void) {

DDRA = 0xFF; // PORTA is output while (1) { for (int i=1; i<=128; i*=2) {

PORTA = i;_delay_loop_2(30000);

} for (int i=128; i>1; i/=2) {

PORTA = i;_delay_loop_2(30000);

}} // end while

}

Page 8: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

8ECE 353

Building your source Write your C source, save as blinky.c Open up MFile

• Makefile -> Main File Name = blinky (no .c)• Makefile -> MCU Type = atmega32

Other values should be fine at default File -> Save As to blinky.c directory In Programmer’s Notepad: Select Tools

->Make All

Page 9: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

9ECE 353

MFile Simple program to make Makefiles for

compiling your C code A Makefile is a configuration file that tells

the compiler how to compile your code• What chip you’re using (atmega32)• Target filename (blinky.c)

Page 10: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

10ECE 353

GNU Make WinAVR uses “Makefiles” when building

projects, with GNU Make

GNU Make builds dependencies and then source files

Will only rebuild files from updated or new source (saves time)

Very powerful tool: see C:\WinAVR\doc\gnu\make.html for more info

Page 11: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

11ECE 353

Makefiles (continued)

Makefiles are tab-sensitive: tab != space

Lines starting with tab are executed as commands

Misuse of tabs will lead to “improper separator” error

Page 12: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

12ECE 353

Example Makefile## ‘all’ and ‘clean’ targets must be defined!

# ‘make’ or ‘make all’ will build dependencies in the order they are given

all: begin project2 end

begin:

@echo “Starting build”

project2:

avr-gcc project2.c

end:

@echo “Build complete”

clean:

rm project2.o

Page 13: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

13ECE 353

Programming your ATmega32 To program your chip with the C code you

wrote:• Go to AVR Studio• Connect to your chip using JTAG ICE• Go to Fuses tab, make sure Ext Clock is set• In Program tab, flash your chip with the .hex

file you compiled in Programmer’s Notepad• That’s it!

Page 14: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

14ECE 353

C Debugging in AVR Studio

Use the editor to edit the flags in makefile:

DEBUG = stabs // will allow for C debugging

OPT = 0 // will turn off compiler’s optimization

Save the makefile

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)

Page 15: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

15ECE 353

Page 16: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

16ECE 353

Building C code for Debugging

Go to Tools Options Tools Pull down the Schemes window and click on

C/C++ option A make extcoff option will appear; click OK Click on Tools again Click on make extcoff to generate the COF file

(this will generate the correct debug file for AVR Studio that includes the C code information)

In the Programmer’s Notepad (WinAVR):

Page 17: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

17ECE 353

Page 18: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

18ECE 353

Debugging in AVR Studio Connect Olimex to PC and JTAG Open the AVR Studio

• Open the COF file

(it will guide you to select the debug platform (JTAG) and the device (Atmega 32))

• The C code will appear in the main window

• Optional (useful!) view the assembly code:

go to View Dissassembler

• You can put assembly code next to C code

(tile vertically) and step through both codes!

Page 19: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

19ECE 353

Page 20: ECE 353 WinAVR and C Debugging Tutorial By Adam Bailin ECE 353 Fall ‘06

20ECE 353

Additional Information Additional Information can be found at the

WinAVR website:

winavr.sourceforge.net