Pages Menu
TwitterRssFacebook
Categories Menu

Posted by on Jul 2, 2014 in Getting Started, Microcontrollers, MSP430 | 6 comments

Using IAR Embedded Workbench with MSP430

Using IAR Embedded Workbench with MSP430

Hello folks, I am back with my next tutorial on getting started with IAR Embedded Workbench for TI MSP430 about which I mentioned in the introductory tutorial on MSP430. We also discussed about input-output operations, and some basic aspects of C programming. We also wrote a simple program to assign a port as output or input followed by lighting LEDs when a switch is closed. But that was all theoretical. Let’s get our hands dirty now. Let’s get our hands on real time testing of our code on a controller. Here, I have used the MSP430G2 LaunchPad plugged with 14-pin MSP430G2131 DIP controller.

IAR Embedded Workbench for TI MSP430, also called EW430 is a complete software toolkit you will need to compile, build, link and download the code into your controller. To add to these, you will also learn how to debug your code using the simulator and FET (flash emulation tool) debugger.

Contents

About IAR Embedded Workbench

I mentioned earlier that EW430 software comes for free in two versions. While one is a time limited version which stays for 30 days (after which you’ll need to pay to use it), the other one is code size limited but with no time limit. I would suggest readers to subscribe to the code size limited version with no time limit, wherein the code size is limited to 4 KB of C code, which is more than enough for our learning process.

IAR Embedded Workbench is available for a vast range of processors and controllers which includes ARM and AVR as well. I personally prefer it over Code Composer Essential IDE because I found EW430 a tad easier to use and predict for new users. If you have worked with Code Compressor Studio before, you might find Code Composer Essential familiar to work with.

Installation and Getting Started

  1. The very first task is to install EW430 on your computer. It is compatible with both Windows 7 and Windows 8. You can download it from here.
  2. Now, open up the downloaded .exe file and install the software. It should not take more than fifteen minutes.
  3. After installing, go ahead and open up IAR Embedded Workbench. When you open it up, a dialog box called “IAR License Manager” will automatically pop up asking for a license key. Even if it doesn’t pop up automatically, go to start and search for “IAR License Manager”.
  4. Click on register, it will require internet connection. Follow up the easy steps and register for code limited version of IAR. It will ask you some general questions like your name, institution or company’s name and about the project you want to build using EW430. Just answer all questions and give your correct email address.
  5. After completing all such formalities, you will receive the license key in the email you registered with. Copy and paste the key into the license manager and here you are ready to start with MSP430! Woohoo!

The above steps are required for the first use of the software. After that, you can directly open the IAR Embedded Workbench (henceforth mentioned as EW430). The window of EW430 is usually divided into three parts. We will look at them one by one.

Workspace Window

The EW430 Workspace Window looks something like this. If the image is too small to view, click on it to enlarge.

IAR Windows

IAR Windows (Click to Enlarge)

The top left pane in the IAR window is workspace. This is where the IDE keeps track of all the source code, linker, libraries and dependencies. You can see and access the source code (usually .c or .asm files), header and library files (usually .h files) being used in compiling and building your project. Generally, the workspace area is in collapsed (or compact or aggregated) form by default, but in the figure I have expanded the tree. The workspace simplifies things for you (a lot, especially when managing/handling files is concerned) when you are working on more than one project. In the image above, you can see the currently selected project in bold, underneath which you can find the source codes, libraries and linker files.

Editor Window

So.. any guesses??! Yes, you correct! It is the big white space where we write the code. It goes without saying that it is the top right pane covering most of the area. Remember that code is poetry – you write code, and you’ll have to love writing it as well. This is why you can customize it to suit your needs (and eyes, and fall in love with it)! You can change the font type, font size, color scheme and tons of other features by going to Tools > Options > Editor > Expand Editor and play around. You can also open multiple editor windows in a project and save them in your project directory, or replace them with the current one anytime you want. The editor window also comes in handy during simulation of the code.

Message Window

The message window usually shows the errors and warnings during building. Clicking the error will move the cursor to the line in the code where the error has occurred.

Going about your First Project – Hello World!

If you are working on MSP430 for the first time, creating your first project will surely titillate you. So, without taking much time, let’s go about your Hello World project step by step.

Step 1 – Create New Project

Open IAR Embedded Workbench. Go to Project > Create New Project. A dialog box will appear. We will write the code in C language, hence expand C and click on main. Click OK and usual Save as window will appear. Create a separate directory where you can save all your projects, and save each project and associated files in a separate folder inside it. Trust me, it helps!

IAR Create New Project

IAR Create New Project (Click to Enlarge)

Step 2 – Workspace and Editor appears

The window containing the workspace and the editor space will appear. The window is shown below. You can also see some lines of code already written for you in the editor.

IAR Initial Window

IAR Initial Window (Click to Enlarge)

Step 3 – Write Code

Please make sure you’ve read and understood the previous post before you even continue to read further. Let’s write our first program to light up the two LEDs on the MSP430 Launchpad. If you notice, the red LED (LED1) is connected to P1.0 and the green LED (LED2) is connected to  P1.6. Both of them are in active high configuration.

Thus, all you need to do is to add the following two lines of code.

    P1DIR = 0xFF; // Making port 1 output ports
    P1OUT = 0x41; // Making pins P1.0 and P1.6 high

The overall code would look somewhat like this:

#include "io430.h"

int main (void)
{
    // Stop Watchdog Timer to prevent timeout reset
    // We'll learn about it a little later
    // For now, just keep in mind that it is needed
    // for most MSP430 projects
    WDTCTL = WDTPW + WDTHOLD;    // WDTCTL = WDTPW | WDTHOLD; would work as well

    P1DIR = 0xFF; // Making port 1 output ports
    P1OUT = 0x41; // Making pins P1.0 and P1.6 high
}

Just make sure, you save the source code every time you make any changes to that.

To get the hexadecimal equivalent of your binary number, you can make use of Windows Calculator. You’ll need to change its view to Programmer. Click on the bits you want to set and change the result type from Decimal to Hexadecimal and you’ll get the required hexadecimal equivalent of 01000001. For instance, look at the image below.

Calculating Hex Value

Calculating Hex Value

Step 4 – Choose your Device

Now, go to Project > Options, and then select General Options under Category. This is where you get to choose the device you are using. I am using MSP430G2131 which is present on the Launchpad.

IAR Choose Device

IAR Choose Device (Click to Enlarge)

Now click on the Debugger category. This is required for us to step through and control the execution of the code. We have two types of debugger in this IDE.

  • Simulator: It runs entirely on our computer and don’t need any external hardware. It shows the results based on the features of devices that it models. It is unsuitable if we want to work with external signal interacting with the device.
  • Emulator: It looks similar to simulator on computer but the program runs on a real device. To achieve this, we need a separate piece of hardware, called emulation tool, along with the microcontroller we’re using. MSP430G2 LaunchPad is a pretty good choice since it features on-board emulator. It also has some side effects that come into picture when we use timers and interrupts, but let’s not worry about it now.

We will deal with both simulator and FET debugger, but let’s proceed with simulator, and then click OK.

Step 5 – Save and Compile your Code

If you haven’t done so already, save the project and your code. You can choose any fancy name you like (it’s your project after all). We will now compile our code. Go to Project > Compile or simply press Ctrl+F7. You should see some messages in the message window show up. If you have followed everything like a nice little kid up until now, there shouldn’t be any error or warning; and you should see something similar to this:

IAR Successful Build

IAR Successful Build (Click to Enlarge)

Step – 5A

But you know, since life isn’t always fair and since Murphy’s Law always holds good, you might and will come across errors and warnings sooner or later. Even though we secretly wish that you may never have to see this step again, we would still like to show you how it looks to get an error! Let’s introduce an (obviously intentional) error and see what happens. Let’s say you misspelled P1DIR and wrote P1DRI instead. Upon compiling, you should get an error as shown in the figure below. Double clicking the error in message window will automatically position the cursor to the erroneous line. Now go ahead, fix it if you can! And don’t just fix it, re-compile it as well!

IAR Unsuccessful Build

IAR Unsuccessful Build (Click to Enlarge)

Step 6 – Build your Code

Now that we have our source code ready and compiled, the next step is to make the code. Go to Projects > Make or press F7 to make the code. This will build your code and update your built tree in workspace. You shouldn’t see any errors with the program in context, but in case you get any error, please comment below the post. And please do not post any source code in the comment. If you absolutely need to share your code with us, use pastebin.com and share the link. Thank you for your cooperation.

Step 7 – Debug/Simulate your Code

The next step is to debug the code we just made (or built). Since, we chose simulator in step 4, we don’t need any hardware and we can see the state of registers in the IDE itself.

  • Click on Project > Download and Debug. You will see a new window pop up towards the right side of your screen. This partition, called Disassembly, will show the assembly language equivalent of your C code. This is what the compiler has generated for you.
  • We also want to check the status of the registers changing with each line getting executed. For that, click on View > Registers. Again, a new partition on the right pops up and will show you the status of registers. By default, it displays the CPU registers, which you can change to Port1/2 to analyze.
  • The green colored highlighted line on the editor window indicates the next line to be executed. At this stage, your window should look something like this:
IAR Code Debugging

IAR Code Debugging (Click to Enlarge)

Step 8 – Step Through your Code

Once you enter debug mode, you can see a new debug toolbar appear in your IDE. Click on the Step-in button in the toolbar and you will see the highlighted line getting executed and states of registers getting changed in the Register partition. You can also debug using step-into, step-over, and step-out buttons, but they are useless for this example. They are useful for playing around when you have several function calls in your program. We’ll skip them for now in order to keep things simple and straight. However we do encourage you to try them out as well. In case of any questions, you can use the discussion panel below this post.

Once you are done observing the outputs in register window, you can stop debugging and get back to your editor window for some more fun. :D

Step 9 – Connect your Hardware

Now, let’s debug using the emulator. Remember in step 4 we mentioned the need for a hardware in order to emulate? Connect your LaunchPad to your computer using mini-USB cable provided with it. Your computer shouldn’t show any error in getting the device connected, since the drivers should have been installed while you were installing IAR EW430. Once connected, go to Project > Options and change the Debugger driver to FET Debugger and click OK.

Step 10 – Debug/Emulate your Code

Again, go to Project >Download and Debug. It will initialize your device and check the connections first. Sometimes, it might give you some type of fatal errors, which means your emulation hardware is not connected to MSP430. If it says, communication error, it means that your Rx and Tx pins are not connected.

Note: If you are working on MSP430G2 LaunchPad, make sure you connect all the five jumpers between emulation and MSP430G2 device.

Soon enough, your code will be ready for debugging. Just click on the step-in button line by line or if you are eager to see your code working directly, then let it go for a free run by clicking the GO button. And don’t forget to share your excitement with us when you see the LEDs glowing on your board.

Let’s blink some LEDs now!

Well, up until now, we were merely glowing the two LEDs. Now let’s blink one of them. Modify the code to the following and repeat all the steps. This code blinks the red LED (LED1) on the LaunchPad.

// This code blinks the red LED (LED1)
// connected to P1.0
#include "io430.h"
int main(void)
{
    int i;

    // stop watchdog timer
    WDTCTL = WDTPW | WDTHOLD;

    // set up P1.0 (LED1) as output
    P1DIR = 0x01;

    // intialize P1.0 (LED1) to 0 (off)
    P1OUT = 0x00;

    // loop forever
    for(;;) {
        // toggle LED1 on P1.0
        P1OUT ^= 0x01;
        // wait! we need to see it at least
        for (i = 0; i < 0x6000; i++);
    }
}

The Infinite Loop

Notice the use of an infinite loop in the program. No, this is not an error, and yes, it is intentional. You surely don’t want your LED to stop blinking after some time, do you? This goes on with most of the embedded systems application. You want your application to run forever (until the system is rebooted, or some interrupt occurs). For example, you want the engine control unit to control the throttle of your car for as long as the engine is running. What would happen if it stops working after you have driven a couple of miles?

The Delay

With the implementation of loops in any embedded system comes the topic delay. In this case, we want us to see the LED blinking. Thus you should give enough delay for the LED to turn on and off. In fact, LEDs can be operated at a much much higher frequency as well (and it works perfectly fine), it’s just that our eyes are too slow to notice them. So give enough delay to see it blink. However, delay plays a much higher and important role in embedded systems. In fact, embedded systems is all about constraining your execution to meet specific timing deadlines. Discussion on this topic is out of scope of this post. May be sometime later when we discuss about real-time embedded systems, we’ll have a post dedicated to this. Till then, enjoy!

More Blinking Patterns

Sure! Go ahead and try out blinking LED2 as well, and try out different blinking patterns as well with different delays. Feel free to share your experience with us down below.

Summary

In this post, we learned how to use one of the available software tools (IAR Embedded Workbench) for compiling, building and debugging code for MSP430. We also learned how to blink LEDs and perform some basic I/O port operations using MSP430.

That’s it for now! Feel free to share your thoughts down below! And please do not post any source code in the comment. If you absolutely need to share your code with us, use pastebin.com and share the link. Thank you for your cooperation.

Written by Chahat Ahuja
support@maxEmbedded.com

6 Comments

  1. I am trying to run sample codes for my MSP430G2553 device. I have been writing and running my code using IAR Embedded Workbench version 5.60.It isworking on simulator mode with no problem. However, when I am trying to debug the code on the device by using the USB debugger interface, I get the following error:

    Fatal error: Failed to re-initialize Session aborted!
    How can i rectify this error.

  2. Hi, I’m interested by IAR because I want to program a cc2541 BLE TI. Generally I program Atmel chip with AVRSTUDIO. So for the programming of my ‘SOC’, can I follow this tutorial or the IAR for cc2541 is different?

    • I don’t know! It should probably be a different version.

    • Hello! For programming cc2541, you need the IAR workbench for the 8051. I don’t know if they have a free version and licensed version costs 1000s of dollars. Also you need a CC Debugger to program the SoC which you can buy from Ti’s website. Good luck!

  3. Hello/
    Can you help me with blinking 3 different leds, one at a time, randomly in msp430g2553?

  4. is it possible to build our own bluetooth module( using chip like cc2650)? can you help me?

Leave a Reply

%d bloggers like this: