Introduction: Finite State Machine on a MSP430

About: Software Engineer - doing a lot of stuff with state machines.

I'm going to show you how to program a MSP430G2 Launchpad with Finite State Machines (FSM) using YAKINDU Statechart Tools directly in Texas Instruments Code Composer Studio.

This tutorial contains six steps:

  1. Installing YAKINDU Statechart Tools as a plugin to Code Composer Studio
  2. Getting started with State Machines in Code Composer Studio
  3. Create the Blinky State Machine
  4. Generate the State Machine C code
  5. Calling the State Machine from your code
  6. Run the project!

This project can be used as a blueprint for any MPS430 or MSP432!

Supplies

Step 1: Installing YAKINDU Statechart Tools As a Plugin to Code Composer Studio

First, you need to install TI's Code Composer Studio. I've successfully tested the setup with CCS version 9.2 and older once. You can find a download link here:

Download CCS

Click for download and ensure you have at least chosen the MSP430 ultra-low-power MCUs while installing. Meanwhile, you can grab the update site for YAKINDU Statechart Tools. Go to:

Download YAKINDU Statechart Tools

On this site, click on Download Now and follow the instructions. As you can see in the photo: It is free for non-commercial use. After the registration, you'll get an overview of the download options. Click on STANDARD DOWNLOAD SITE and click on INSTALL FROM UPDATE SITE. There you'll get a link under Stable Releases. Grab this link and save it or put it in your clipboard.

Installing YAKINDU Statechart Tools into Code Composer Studio can be a little bit tricky, depending on the version of Code Composer Studio you're using. Maybe these steps are outdated meanwhile - however: don't hesitate to ask me in the comments if you need any help.

These are my steps:

After successfully installing CCS to your system, open the Help tab and click on Install New Software... In this wizard, add the following update site via the Add... button:

https://download.eclipse.org/releases/2018-09/

Don't add anything, it just needs to be resolved. You can see it in the picture.

After this, repeat the steps and click on the Add... button. Insert the YAKINDU Statechart Tools update site, which you've hopefully saved. Then, select the YAKINDU License Management checkbox and click on next. Follow the instructions. Your IDE may restart once.

Finally, you can install YAKINDU Statechart tools. Repeat the last steps, but this time choose YAKINDU Statechart Tools Standard Edition. Once again, follow the instructions. After restarting you have successfully installed YAKINDU Statechart Tools.

Step 2: Getting Started With State Machines in Code Composer Studio

To use state machines proceed as normal and create a new CCS Project. Open the File tab, open New and click on CCS Project. Define a project name, choose your microcontroller and create an empty project, which contains an empty main.c file. I used the MSP430G2553.

Now you can start working with state machines!

Right-click the project, open New and choose Statechart Model. A Wizard will be opened in which you can select your project and name your statechart. For example, call it blinkyStateMachine.sct.

The Statechart model will appear in the project folder after clicking on Finish. Click on Yes if you are asked to change the perspective.

Step 3: Create the Blinky State Machine

Now you can start creating the statechart!

On the left side, you'll find the so-called Definition Section. There you can add textual elements to the model, for example, operations, which can be called in the state machine.

Delete everything from it and simply add these three operation definitions:

internal:
operation init() 
operation redOn() 
operation redOff()

Afterward, switch to the statechart model and add three states:

  • Initialization
  • red LED on
  • red LED off

Connect the states as shown on the picture and add the transitions and entry actions. You can find them on the included picture again.

Step 4: Generate the State Machine C Code

Now it is time to generate the C-Code. To do this, a generator model must be added. Right-click the project once again and open New and click on Code Generator Model. Choose a name for the sgen-file. It is a good practice to stay with the state machine name. Call it blinkyStateMachine.sgen and click on Next. Choose the desired State Machine by clicking on the checkbox. Ensure that you have chosen the YAKINDU SCT C Code Generator (as we want to generate C-Code) and click on Finish.

Usually, the C-Code will be generated automatically, but if not, you can right-click the sgen-file and click on Generate Code Artifacts to do so. The folders src and src-gen should appear in your project. They include the generated C-Code, which will be automatically be updated when you edit and save the statechart.

Because this statechart uses time-based events, a timer service needs to be implemented. To prepare this, you need these two files: sc_timer_service.c and sc_timer_service.h You can get them from GitHub or download them here. You must add them to the src folder.

Step 5: Calling the State Machine From Your Code

Finally, the state machine can be used in your main function!

First, you have to include the state machine and the timer service. Then the state machine, the timer service and the required functions for the timer service must be declared and defined. Additionally, the defined operations which are turning the red led on and off again must be implemented.

#include  <msp430.h>
#include "src-gen/BlinkyStateMachine.h"
#include "src/sc_timer_service.h"

BlinkyStateMachine blinky;

#define MAX_TIMERS 4

static sc_timer_t timers[MAX_TIMERS];

static sc_timer_service_t timer_service;

//! callback implementation for the setting up time events
extern void blinkyStateMachine_setTimer(BlinkyStateMachine* handle, const sc_eventid evid, const sc_integer time_ms, const sc_boolean periodic)
{
    sc_timer_start(&timer_service, handle, evid, time_ms, periodic);
}

//! callback implementation for canceling time events.
extern void blinkyStateMachine_unsetTimer(BlinkyStateMachine* handle, const sc_eventid evid)
{
    sc_timer_cancel(&timer_service, evid);
}

//! defining operations
extern void blinkyStateMachineInternal_init(const BlinkyStateMachine* handle)
{
    WDTCTL = WDT_MDLY_32;
    IE1 |= WDTIE;
    P1DIR |= BIT0;
}

extern void blinkyStateMachineInternal_redOn(const BlinkyStateMachine* handle)
{
    P1OUT |= BIT0;
}

extern void blinkyStateMachineInternal_redOff(const BlinkyStateMachine* handle)
{
    P1OUT &= ~BIT0;
}

The main function contains two parts:

The initialization and the enter function of the state machine and the initialization of the timer.

The second part is an endless loop - the while(1) loop. Within this loop, the run cycle function of the state machine is called. Afterward, the MSP430 will be set into the Low Power Mode 0 and the General Interrupt Enable bit gets set. Now the microcontroller is sleeping and waits for an interrupt. After the interrupt of the WDT, the timer will proceed. This means that every timer gets updated and the elapsed time gets incremented by 32 - the time in milliseconds, which is proceed after every interrupt of the WDT.

void main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

// Init timer and state machine
sc_timer_service_init(&timer_service, timers, MAX_TIMERS, (sc_raise_time_event_fp) &blinkyStateMachine_raiseTimeEvent); blinkyStateMachine_init(&blinky); blinkyStateMachine_enter(&blinky);

while (1) { // call state machine every 32 ms blinkyStateMachine_runCycle(&blinky); __bis_SR_register(LPM0_bits + GIE); sc_timer_service_proceed(&timer_service, 32); } }

// WDT ISR #pragma vector=WDT_VECTOR __interrupt void watchdog_timer(void) { __bic_SR_register_on_exit(LPM0_bits + GIE); }

Attachments

Step 6: Run the Project!

That’s it - Now you can build and upload the program to your MSP430!

Hopefully, this tutorial helped you to successfully create a project for your MSP430. Now it's time to implement your own ideas!

For code generation, YAKINDU Statechart Tools is required as a Plugin in your Code Composer Studio.

> You can get the update site here! <

It starts with 30 days trial version. Afterward, you can get a free license for non-commercial use!