using the ee 109 emacs quick reference [mac/linux] • emacs uses the keyboard for all editing...

Download Using the EE 109 Emacs Quick Reference [Mac/Linux] • Emacs uses the keyboard for all editing commands

Post on 12-Sep-2019




0 download

Embed Size (px)


  • 1

    Using the EE 109 Tools

    Allan Weber

    Mark Redekopp

  • 2

    Note and Disclaimer

    • Please follow the instructions at to setup new labs/projects for your Arduino

    • Some of this material is just background info that is just for reference…the stuff we'll use a lot will be repeated as we do it more and more.

  • 3

    Building a Program

    • The programming/debugging loop:

    – Edit [emacs, TextWrangler, Notepad++]

    – Compile/Link [make, avr-gcc]

    – Download and execute [make flash, avrdude]

    – Observe [ you! ]

    – Repeat [ you! ]

    • Programs can be edited in any “plain text” editor

    – Available on the VHE 205 Macs:

    • Emacs (command line program)

    • Vi/Vim (command line program)

    • TextWrangler (Mac GUI app)

    • Don’t use a word processor (Microsoft Word or Apple TextEdit etc.)

    • How to start a fight: Tell another programmer that your favorite text

    editor is better than theirs.

  • 4

    Editing the Program [Mac/Linux] • To run command line editors

    on the Mac, start the Terminal

    application from the dock at

    bottom of the screen

    • Emacs and Vi require some

    knowledge of a few Unix-type

    commands in order to be

    useful (cd, ls, rm, more, etc.)

    • Run the editors by just typing

    the editor name and the name

    of the file.

    emacs myfile.c

    vi myprog.c

  • 5

    Emacs Quick Reference [Mac/Linux]

    • Emacs uses the keyboard for all editing commands.

    • Easiest way to learn is via the built-in tutorial.

    • Type the command “emacs” and then once in the editor, enter the command “ESC X help- with-tutorial”

    • Read along and follow the instructions

  • 6

    Emacs Quick Reference [Mac/Linux] • Open (Ctrl-x, Ctrl-f)

    – Then type in the name of the file to open or create if it doesn't exist

    • Save (Ctrl-x, Ctrl-s)

    • Exit (Ctrl-x, Ctrl-c)

    • Cancel Command (Ctrl-g)

    • Cut a whole line of text (Ctrl-k)

    • Cut a segment: (Ctrl-space) at start, (Ctrl-w) at end

    • Paste (Ctrl-y)

    • Search (Ctrl-s) then type the word and hit enter – Hitting Ctrl-s again will go to the

    next occurrence

  • 7

    Editing the Program [Mac] • The TextWrangler

    text editor can be

    started by dragging

    and dropping the

    program file onto the

    icon in the dock.

  • 8

    Editing the Program [Windows] • The Notepad++ text editor

    can be started by

    – First selecting the file (left-click)

    in Explorer

    – Then right-click the selected file

    and choose

    'Edit with Notepad++'

  • 9


  • 10

    Compilation Units

    • Often rather than putting all our code/functions in one file it is easier to maintain and re-use code if we break them into multiple files

    • We want functions defined in one file to be able to be called in another

    • But the compiler only compiles one file at a time…how does it know if the functions exist elsewhere?


    void flash(int delay)


    PORTB |= (1

  • 11

    Compilation Units • We must prototype any function we want to use

    that is in another file

    • Rather than make us type in the prototypes for each new program we write that needs that function, put prototypes in a header file that can be reused (included) for any new program


    void flash(int delay);

    int main()



    // do something else


    return 0;




    void flash(int delay)


    PORTB |= (1 = 200){



    return 0;



    void flash(int delay);

  • 12

    Compiling to Object Code

    • Two issues: – We may not want to distribute our source code files

    – With a large program, we don’t want to re-compile all the files if the code only changed in one

    • Solution – Compiling to object code, creates the machine code/assembly code

    for just a single file BUT DOESN’T try to link any function calls to other files nor does it try to create an executable

    – Use: gcc –c filename.c void flash(int delay)


    PORTB |= (1

  • 13


    • After we compile to object code we eventually need to link all the files together and their function calls

    • Without the –c, gcc will always try to link

    • You can give gcc source files (.c files) or object (.o files)


    (Plain source)


    (Plain source)



    / object code)



    / object code)



    gcc -g –Wall –c flash.c gcc -g –Wall –c flash1_test.c

    Note: gcc -g –Wall –o flash1_test flash.c flash1_test.c

    Would also work and be fine and thus not require you to

    compile to object code in a separate step

    gcc -g –Wall –o flash1_test flash.o flash1_test.o

  • 14

    gcc/g++ Options

    • Most basic usage – gcc c_filenames

    – Creates an executable a.out

    • Options – -o => Specifies output executable name (other than default a.out)

    – -g => Include info needed by debuggers like gdb, kdbg, etc.

    – -Wall => show all warnings

    – -c => compile but don't link (i.e. create an object file)

    – -Ipath => add path into #include search directory

    – -Lpath => add path into library search directory

    – -Dmacro => #define macro

    – -llibname => link in the code in library, libname

    – -On => n=[0..6] => Optimization level 0-6

  • 15


  • 16

    XKCD #303

    Courtesy of Randall Munroe @

  • 17

    'make' and Makefiles • We really use the 'avr-gcc' compiler (just like you use g++ for CS 103)

    – But to compile for the Arduino requires many complex settings/options

    – Also, a single program can be made of many source files and library code files

    • 'make' is a utility program on most Linux/Unix machines which processes commands in a provided Makefile

    • Helps automate compilation process – Essentially can use Makefiles as scripts of commands to be run

    – Let ‘make’ fill in all the complex compilation options

    • Helps provide 'smart' compilation – Compiles only files or those that depend on files that have changed since last


    – Reduces wait time for compilation process especially for large programs

  • 18

    Analogy: Evaluating Expressions

    • Take the equation:

    – x = 5*y + (8*z + 3)

    – Evaluate for y=9, z=7

    – We evaluate term by term & add

    • What if only y changed and we needed to find the new value of x? What would you re- evaluate

    • What if only z changed, what operations would be needed to find the new value of x

  • 19


    Smart Compilation

    • Only compile code that has changed and any files that DEPEND on that code


    (Plain source)

    [ CHANGED ]


    (Plain source)



    / object code)



    / object code)



    gcc -g –Wall –c flash.c gcc -g –Wall –c flash1_test.c

    gcc -g –Wall –o flash flash.o flash1_test.c

  • 20

    Compiling the Program • A 'Makefile' tells 'make'

    – what files the program is composed of

    – how they depend on each other

    – how to compile each file (convert .c to .o or .o to an executable)

    • 'make' looks at the modification dates on files to determine what

    needs to be compiled, and only compiles what it has to.


    main.o input.o output.o



    main.c input.c output.c sub1.o sub2.o sub3.o

    sub1.c sub2.c sub3.c

  • 21

    Compiling the Program • The make program uses the text file “Makefile” for

    information on the dependencies (i.e. The contents of

    'Makefile' is the script that tells 'make' what to do)

    Sample Makefile

  • 22

    Compiling the Program • The "Makefile" for the

    Lab 2 program.