<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="id7224478">
  <name>Software Setup and Introductory Assembly programs for the MSP430</name>
  <metadata>
  <md:version>1.1</md:version>
  <md:created>2008/01/18 14:52:41.305 US/Central</md:created>
  <md:revised>2008/04/07 17:53:50.915 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="TexasInstruments">
      <md:firstname/>
      
      <md:surname>TI</md:surname>
      <md:email>j-borgeson@ti.com</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="TexasInstruments">
      <md:firstname/>
      
      <md:surname>TI</md:surname>
      <md:email>j-borgeson@ti.com</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>Assembly</md:keyword>
    <md:keyword>eZ430</md:keyword>
    <md:keyword>Introductory</md:keyword>
    <md:keyword>MSP430</md:keyword>
  </md:keywordlist>

  <md:abstract>This is Lab 3 of the Colorado State University Microprocessors course using the eZ430-F2012 development tool.  This introductory lab will walk you through the process of creating an assembly project, assembling a program, downloading it to the EZ430 (the development kit for the TI-MSP430F2012) and executing / debugging the program. When you complete this lab, you should be able to:

-	Understand the IAR Embedded Workbench IDE.
-	Assemble (build) a program using the IAR Embedded Workbench.
-	Download a program onto your EZ430.
-	Run a program and examine memory registers.
-	Use stepping and breakpoints to debug the program.

Because you will perform all of these procedures in every lab you do after this, a complete understanding of the material in this lab is necessary.</md:abstract>
</metadata>
  <content>
    <section id="id-0766752926177">
      <name>3.1 - Objectives</name>
      <para id="id6844368">This introductory lab will walk you through the process of creating an assembly project, assembling a program, downloading it to the EZ430 (the development kit for the TI-MSP430F2012) and executing / debugging the program. When you complete this lab, you should be able to:</para>
      <list type="bulleted" id="id6844376">
        <item>Understand the IAR Embedded Workbench IDE.</item>
        <item>Assemble (build) a program using the IAR Embedded Workbench.</item>
        <item>Download a program onto your EZ430.</item>
        <item>Run a program and examine memory registers.</item>
        <item>Use stepping and breakpoints to debug the program.</item>
      </list>
      <para id="id6844408"><emphasis>Because you will perform all of these procedures in every lab you do after this, a complete understanding of the material in this lab is necessary</emphasis>.</para>
    </section>
    <section id="id-654484580125">
      <name>3.2 - Reading Material</name>
      <para id="id6844444">You will need to read the following introduction material prior to completing this lab, all are available on the ECE 251 website.</para>
      <list type="enumerated" id="id6844451">
        <item>Introduction to the MSP430-F2012 controller</item>
        <item>Introduction to assembly programming</item>
        <item>MSP430 Instruction Reference Sheet</item>
      </list>
    </section>
    <section id="id-67429012039">
      <name>3.3 - Hardware / Software Setup</name>
      <para id="id6844491">The eZ430 debugger/emulator should install its drivers automatically.  To install IAR Embedded Workbench Kickstart Version, please visit www.ti.com or www.iar.com for the latest version of the software with installation instructions.</para>
    </section>
    <section id="id-516849544566">
      <name>3.4 - Introduction to IAR Embedded Workbench IDE</name>
      <para id="id6844522">Invoke the IAR Embedded Workbench in one of the following two ways:</para>
      <list type="enumerated" id="id6844528"><item>Left click your mouse on the START menu on the bottom left of your screen. Then follow the path, Programs -&gt; IAR Systems -&gt; IAR Embedded Workbench Kickstart for MSP430 V4.1-&gt; IAR Embedded Workbench. </item>
        <item>Open Windows explorer and locate IAR Embedded Workbench.exe in the folder C:\Program Files\IAR Systems\Embedded Workbench 4.1\common\bin\IarIdePm.exe</item>
      </list>
      <para id="id7158121">To create the shortcut on your desktop, right click the mouse on IarIdePm.exe and then left click on <emphasis>create shortcut</emphasis>. Then click on this shortcut to invoke the IAR Embedded Workbench application. </para>
      <section id="id-179037812773">
        <name>Step 1 – Creating a Project</name>
        <para id="id7158141">The first thing you will see is the <emphasis>Embedded Workbench Startup</emphasis> screen. This screen will be a good shortcut to open existing workspaces and create new projects in the future, but for now, click the <emphasis>Cancel</emphasis> button. Note that a <emphasis>workspace</emphasis> can hold one or more <emphasis>projects</emphasis>, each which can hold groups of <emphasis>source files</emphasis> (the files that contain your assembly code).</para>
        <para id="id7158177">Click on <emphasis>File -&gt; New -&gt; Workspace</emphasis>. This opens a new workspace in which to place your projects. You must always open a workspace before you can create a new project. To create a new project, click <emphasis>Project -&gt; Create New Project</emphasis>… The <emphasis>Create New Project</emphasis> box will appear. IAR lets you choose whether you want to create an assembly (<emphasis>asm</emphasis>), C, or C++ project. Expand the <emphasis>asm</emphasis> tab and click <emphasis>OK</emphasis> once you have selected the <emphasis>asm</emphasis> option. </para>
        <figure id="id7158248"><name>New Project Window</name>
          <media type="image/png" src="graphics1.png">
            <param name="height" value="373"/>
            <param name="width" value="434"/>
          </media>
        </figure>
        <para id="id7158271">IAR will ask you to specify where you would like to save your .ewp project file. It is recommended you create a folder for your workspaces on your u: drive such as u:\ee251\Labs\ in which you can store all your future labs. Create a folder called <emphasis>Lab 3</emphasis>, name your project Lab 3, and click Save. At this point, you should also download the add_primes.s43 file provided on the class webpage into the same folder. </para>
        <para id="id7158298">Look at the left of your screen. You will see the workspace window where your projects and source files are organized. </para>
        <figure id="id7158312"><name>Workspace Window</name>
          <media type="image/png" src="graphics2.png">
            <param name="height" value="219"/>
            <param name="width" value="292"/>
          </media>
        </figure>
        <para id="id7158336">On the right is the project window. The project window shows the assembly file template discussed in Introduction To Assembly Programming from the pre-lab material. You will not be using this file for this tutorial. Before anything else, click on File -&gt; Save Workspace and save your workspace in the same folder as the project. </para>
        <para id="id7158362">Now add the file we are going to examine in the tutorial. In your workspace window, right click on the project name and select Add -&gt; Add Files… At the bottom of the page, change the Files of type tab to the Assembler Files option. If you downloaded the file from the class webpage, go ahead and select the add_primes.s43 file and click Open. You should see the file added to your workspace window. Right click on the asm.s43 template and remove it from the project then take a look at the add_primes.s43 code. This assembly program has stored the first six prime numbers in an array in memory and adds them up into register six. See if you understand what is going on by referencing the pre-lab material.</para>
      </section>
      <section id="id-447407249357">
        <name>Step 2 – Setting the Project Options</name>
        <para id="id7184975">Before we can compile our code into an executable object file, we need to set the project options. Using the Project pull-down menu select Options or simply right click on the project and select Options… This will bring you to the Options for node “&lt;project_name&gt;” screen. </para>
        <figure id="id7185003"><name>Project Options Screen</name>
          <media type="image/png" src="graphics3.png">
            <param name="height" value="446"/>
            <param name="width" value="527"/>
          </media>
        </figure>
        
        <list type="bulleted" id="id7185037">
          <item>In the General Options menu: <list type="bulleted" id="id7185052"><item>Set the Device to the MSP430F2012 or whatever MSP430 device number you are using.</item></list></item>
          <item>In the Debugger menu:<list type="bulleted" id="id7185077"><item>Set the Driver to FET Debugger. This makes sure that once compiled into an object file, the program will be loaded onto your physical microcontroller, not the IAR simulator. </item><item>If you would ever like to work on your code but you do not have your EZ430, you can set the Driver option to Simulator and the IAR Embedded Workbench will simulate your microcontroller. </item></list></item>
        </list>
        <list type="bulleted" id="id7185111">
          <item>Under the actual FET Debugger menu: <list type="bulleted" id="id7185126"><item>Change the Connection from LPT -&gt; LPT1 to TI USB FET. This tells IAR to look for your USB stick and not a port on your computer. </item><item>Change the Target VCC option at the bottom of the screen to 3.0 </item></list></item>
          <item>Click OK to finalize your settings. You are now ready to build and compile your project.</item>
        </list>
      </section>
      <section id="id-0681951827931">
        <name>Step 3 – Running the Program</name>
        <para id="id7185178">Before we compile the project, make sure to save your project to preserve its settings. Select the add_primes.s43 file in the workspace window and click Project -&gt; Compile. Alternatively, you can right click on the source file and select Compile. When the workbench is finished compiling your source code, you will see a new window at the bottom of the screen called your Build Messages Window. It should say that there are 0 errors and 0 warnings. </para>
        <figure id="id7185209"><name>The "build message" window</name>
          <media type="image/png" src="graphics4.png">
            <param name="height" value="209"/>
            <param name="width" value="350"/>
          </media>
        </figure>
        
        <para id="id7185239">To see what happens when your code contains an error, erase one of the semicolons preceding a comment in the add_primes.s43 code and re-compile. The build message window displays: </para>
        <list type="bulleted" id="id7185246">
          <item>Any errors/warnings in your code </item>
          <item>The assembler’s best attempts of an explanation</item>
          <item>The name of the files in which the errors/warnings occurred </item>
          <item>The line number where the errors/warnings occurred </item>
        </list>
        <para id="id6874070">Double click the line that says “Error[0]: Invalid syntax.” In the file, your cursor will be placed close to the location of the error. Fix the error, and then re-compile. </para>
        <para id="id6874080">Now make sure that your EZ430 is plugged in and click Project -&gt; Debug. This will run your code on the EZ430 and put you in debugging mode.</para>
      </section>
      <section id="id-555076552305">
        <name>Step 4 – Debugging A Project</name>
        <para id="id6874101">A new window has been added to the screen in debugging mode. It is called the Disassembly Window. After the assembler compiles your code into machine code, a disassembler program does the opposite and creates the contents of the disassembly window. You can apply the debugging commands to either the coded assembly file or the disassembly window. Generally, this tool is more useful when the developer has written his/her application using the C programming language and wants to see how the machine interpreted his/her code in assembly. </para>
        <para id="id6874120">The workbench allows users to move and dock windows as they wish. To see this, click <emphasis>View -&gt; Memory and View -&gt; Registers</emphasis>. Click on the window labels to drag and dock them in the same <emphasis>tab group</emphasis> as the <emphasis>Debug Log</emphasis> and <emphasis>Build</emphasis> windows. These windows are updated as the program executes, giving the user extremely useful insight to the inner workings of his/her program. </para>
        <para id="id6874158">NOTE: If the contents of the window selected in each tab group ever change, the changed contents will be highlighted red to show the change. </para>
        <section id="id-905763030444">
          <name>The Memory Window</name>
          <para id="id6874172">The memory window allows the user to navigate the entire memory map of the MSP430. Since we used assembler directives to load the array of prime numbers starting at memory location 0200h, type 0x0200 into the <emphasis>Go to</emphasis> box. You will see the array stored in consecutive words in memory. </para>
          <figure id="id6874190"><name>Memory Window</name>
            <media type="image/png" src="graphics5.png">
              <param name="height" value="200"/>
              <param name="width" value="625"/>
            </media>
          </figure>
          
          <para id="id6874219">Instead of <emphasis>Memory</emphasis>, IAR also gives you the option to looking at:</para>
          <list type="bulleted" id="id6874230">
            <item>SFR: The special function and peripheral registers</item>
            <item>RAM: Memory</item>
            <item>INFO: Information FLASH memory (ROM)</item>
            <item>FLASH: Main FLASH memory (ROM)</item>
          </list>
          <para id="id6874255">NOTE_01: You can use the information and main memories just the same. The only difference is how the memory is physically segmented and the address. </para>
          <para id="id6874262">NOTE_02: If you look at the FLASH memory, notice that this is where the main program is stored. The numbers you see are the instructions of the program that have been stored for program execution.</para>
        </section>
        <section id="id-342485954455">
          <name>The Register Window</name>
          <para id="id6874278">The register window shows the user any of the important registers whether they be peripheral control registers, special function registers, or just the basic CPU registers. For the most part, you will only be interested in the CPU Registers, but know that the other registers are available for viewing. Also note that the Status Register (SR) containing the status bits can be expanded to see each individually labeled bit.</para>
          <para id="id6874289">While stepping through the program, periodically check the Memory and Register windows to see how they change according to your code. </para>
        </section>
        <section id="id-601065471257">
          <name>Inspecting Source Statements</name>
          <para id="id6874304">Run your mouse over these buttons in the top left corner of your debugging session and read their function in the bottom left corner of the IAR workbench frame. </para>
          <figure id="id6874314" orient="vertical">
<name>The Debug Buttons</name>
            <subfigure id="debug1">
            <media type="image/png" src="graphics6.png">
              <param name="height" value="26"/>
              <param name="width" value="239"/>

            </media></subfigure>
            <subfigure id="debug2">
            <media type="image/png" src="graphics7.png">
              <param name="height" value="25"/>
              <param name="width" value="25"/>
            </media>
            <caption>Program Reset Resets the execution of your program. Takes you back to the instruction address located in the reset interrupt vector.</caption>
            </subfigure>
         
            <subfigure id="debug3">
            <media type="image/png" src="graphics8.png">
              <param name="height" value="24"/>
              <param name="width" value="28"/>
            </media>
            <caption>Step Over - Steps over the current step point.</caption>
            </subfigure>
          <subfigure id="debug4"><media type="image/png" src="graphics9.png"><param name="height" value="26"/><param name="width" value="26"/>
             </media>
             <caption>Step Into - When you debug a program with subroutines in assembly, you can choose to “Step Over” the subroutines, meaning that the subroutine is executed but you don’t step through it in the debugger, or you can choose to “Step Into” the subroutine. Stepping into a subroutine will allow the user to see the individual op-codes executed.</caption>
             </subfigure>
          
          <subfigure id="debug5"><media type="image/png" src="graphics10.png"><param name="height" value="26"/><param name="width" value="23"/></media>

            <caption>Step Out Of - If you have stepped into a subroutine, steps out of the subroutine and to the next instruction after the subroutine call.</caption>
            </subfigure> 
          <subfigure id="debug6"><media type="image/png" src="graphics11.png"><param name="height" value="25"/><param name="width" value="24"/></media>
            <caption>Step to Next - Steps to the next statement, regardless of the circumstances.</caption>
            </subfigure> 
         
            <subfigure id="debug7"><media type="image/png" src="graphics12.png"><param name="height" value="24"/><param name="width" value="25"/></media>
            <caption>Execute to Current Cursor Position - Executes the program to the line at which the cursor is  currently located.</caption>
            </subfigure> 

            <subfigure id="debug8"><media type="image/png" src="graphics13.png"><param name="height" value="26"/><param name="width" value="27"/></media>
           <caption>Run - Runs the program straight through or until a breakpoint.</caption>
           </subfigure> 

           <subfigure id="debug9"><media type="image/png" src="graphics14.png"><param name="height" value="23"/><param name="width" value="29"/></media>
           <caption>Stop Debugging - Stops the debugging session returns the user to programming mode.  At the top right-hand side of the page, note this button as well:</caption>
           </subfigure>  

           <subfigure id="debug10"><media type="image/png" src="graphics15.png"><param name="height" value="26"/><param name="width" value="29"/></media>
           <caption>Make and Reset - Press when code has been modified so that it needs to be re-compiled and the debugger needs to be reset.</caption>
           </subfigure> 
           </figure>
      </section>
        <section id="id-174973145547">
          <name>Breakpoints</name>
          <para id="id6858919">Sometimes it is beneficial, if you want to know how your program is executing at a certain point in its execution, to use breakpoints. To place a breakpoint in the program, double click on the margin next to the line at which you wish to set a breakpoint (or right click on the line and select toggle breakpoint (conditional)). </para>
          <para id="id6858938">For example, the initial operators in every assembly code project that initialize the controller after a Power Up Clear can often be assumed to work properly. If you would like to run the program starting immediately after this section of code, add a breakpoint at the line labeled main. Since we have two clear operators in the beginning of our code that we know will work, place a breakpoint at the first mov.w instruction line of the main program. Press the Program Reset button to restart program execution and then press the Run to execute until the breakpoint. From here on, play with the other buttons to gain some intuition as to their function. </para>
          <para id="id7573157">NOTE: Debugging can also be done in the disassembly window. Try it out and see if you can follow what all the numbers mean in regards to the CPU registers and memory. </para>
          <para id="id7573164">For more information on debugging, reference the EZ430_UserGuide. An in-depth understanding of the debugging module is a truly powerful tool for future programming. </para>
          <para id="id7573171">After completing this tutorial, you should feel comfortable working with the IAR Embedded Workbench. If you do not, you may want to go back and do it again. Also, make sure you have read and understand the material from lab section 3.2. It will be critical to the execution of future labs. </para>
        </section>
      </section>
    </section>
    <section id="id-177022295664">
      <name>3.5 – Procedure </name>
      <para id="id6858943">1. Set up the EZ430, IAR Embedded Workbench, and all necessary hardware.</para>
      <para id="id7573196">2. Complete the tutorial.</para>
      <para id="id7573202"/>
      <para id="id7573206">Call the lab TA and repeat the tutorial in their presence, without reading the directions. When you can do this without looking at the instructions, the TA will check you off. Review any part of this lab you are not sure about.</para>
    </section>
    <section id="id-161445795623">
      <name>3.6 – Questions </name>
      <list type="enumerated" id="id7573224">
        <item>Describe the register set for the MSP430F2012. What are the Special Function Registers (SFRs) and their functions? Where are they located?</item>
        <item>How are single stepping and breakpoints used to debug a program? Why might you use breakpoints rather than single stepping?</item>
        <item>In the “Disassembler” window, what do the numbers on the far left mean? What does this have to do with the contents in R0 during program execution?</item>
      </list>
    </section>
    <section id="id-589313508962">
      <name>3.7. – Lab Report</name>
      <para id="id7573275">The lab report is due at the beginning of the next lab. For the lab write up, include the following:</para>
      <list type="enumerated" id="id7573282">
        <item>Your marked up version of this document. </item>
        <item>A brief discussion of the objectives of the lab and the procedures performed in the lab.</item>
        <item>Answer to any questions in the discussion, procedure or question sections of the lab.</item>
      </list>
    </section>
    <section id="id-436277958507">
      <name>Bibliography</name>
      <para id="id7573312">"Msp430x2xx Family User's Guide." Dallas, Texas: Texas Instruments Incorporated, 2005.</para>
      <para id="id7573318">.</para>
    </section>
  </content>
</document>
