Menu

BlinkingLED

Petr Pazourek
Attachments
BlinkingLED.png (3612 bytes)

Blinking LED

There is a very simple example which shows how to work with the Cuml tool.

Drawing the Diagram

For this example I was using the ArgoUML editor. The final diagram is shown on the picture below.

Blinking LED statechart diagram

  1. The initial transition contain an action which starts the timer responsible to generation of the eTimeout signal every half second,
  2. The initial transition leads to the state "Off" which turns the LED off in the entry action (LedOff();),
  3. When the eTimeout event arrives in the "Off" state, the "Off" state transitions to the "On" state,
  4. The "On" state turns the LED on in the entry action (LedOn();),
  5. When the eTimeout event arrives in the "On" state, the "On" state transitions back to the "Off" state. The cycle repeats forever because the eTimeout events keep getting generated at the pre-determined rate.

The diagram is exported into the XMI file you can find attached.

Generating Code

Open console and type in the following command line.

cuml -o . ./BlinkingLED.xmi

This produces the following files into the current folder (specified by -o parameter):

  • HSM_Events.h - definition of all events used in diagram
  • HSM_Framework.h - definition of internal structures
  • HSM_Framework.c - runtime framework
  • blinkingLED_FSM.c - definition of the state machine
  • blinkingLED_FSM.h - implementation of the state machine as graphically specified in diagram
  • HSM_Setup.h - through this setup file should be your state machines connected with the rest of the code. It's generated only if it doesn't exist yet.

Create BSP and the main entry

I have found a nice piece of the code implementing the timer for Win32 and Linux as well at https://www.teuniz.net/Timer_code/. We will use it as the timer BSP.

The main.c is the main entry, it has to initialise the HSM framework and timer. There are also implemented helper functions such as LedOn() and LedOff().

#include <stdio.h>
#include <stdlib.h>
#include "HSM_Framework.h"
#include "blinkingLED_FSM.h"
#include "timer.h"

void LedOn(void)
{
    printf("LED ON\n");
}

void LedOff(void)
{
    printf("LED OFF\n");
}

void TimerHandler(void)
{
    HSM_AddPendingEvent( &blinkingLED_FSM, eTimeout );
}

void StartTimer(int ms)
{
    if(start_timer(ms, &TimerHandler))
    {
        printf("\n timer error\n");
        exit(EXIT_FAILURE);
    }
}

int main(void)
{
    //first of all we need to init state machine framework
    HSM_Init();

    printf("\npress ctl-c to quit.\n");

    //the main infinite loop
    while(1)
    {
        HSM_ProcessEvents();
    }

    return EXIT_SUCCESS;
}

Modify the HSM_Setup.h

The compiler needs to know prototypes of all functions we used in the diagram. The HSM_Setup.h is the right file where to define protopypes or to include header files. Simply add the declaration as follows:

// Include all files you need here
#include <stdint.h>

extern void LedOn(void);
extern void LedOff(void);
extern void StartTimer(int ms);

Makefile and compilation

To be able to build the program on your computer a C-compiler is needed. You are free
to use whatever compiler you have installed. I used the GCC toolchain for MinGW.
The Makefile is easy to write.

CC = gcc
CFLAGS = -I.
LDFLAGS =

%.o: %.c
    $(CC) $(CFLAGS) -o $@ -c $<

blinkingLED: main.o blinkingLED_FSM.o HSM_Framework.o timer.o
    $(CC) -o $@ $^ $(LDFLAGS)

Type make in a console located into your folder. You should see something like this:

gcc -I. -o main.o -c main.c
gcc -I. -o blinkingLED_FSM.o -c blinkingLED_FSM.c
gcc -I. -o HSM_Framework.o -c HSM_Framework.c
gcc -I. -o timer.o -c timer.c
gcc -o blinkingLED main.o blinkingLED_FSM.o HSM_Framework.o timer.o

That's all. Try to run the executable file blinkingLED.exe.
Blinking LED in WIN32 console

You can download the source code and the diagram


Related

Wiki: Home