tinverse / tsm

A Typed Hierarchical State Machine Framework in C++

Home Page:https://tinverse.github.io/tsm/index.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to build TSM for stm32?

SergeTauger opened this issue · comments

Dear maintainers!
First off all, I'd like to thank you for the library. I didn't test it yet, but I'm really excited about it API, it's the most intuitive among HSM libraries I checked to the moment.

I'd like to use the TSM for embedded systems with no RTOS running on a MCU (bare metal). Is it possible? If so, could you please recommend me how to build it? I am using GNU ARM Embedded toolchain (gcc 10.2).

Thanks! I'm using stdlib synchronization primitives (thread, mutex etc.). These libs don't exist in the embedded toolchain. Non-trivial. Will look into it. Sorry can't promise dates etc.

If I am getting things correctly, the library needs a separate thread for event dispatcher. How accurately does this description (std-based option) matches use of std::thread in TSM? If ideology and interface are alike, it seems not a big deal to port TSM to RTOS since most (all?) of them have inbuilt multitasking and sync primitives and some kind of event dispatcher.

arm-none-eabi-g++ -c -g -specs=nosys.specs -mcpu=cortex-m3 test.cpp -I <path_to_tsm_include_folder>

where test.cpp is something like:

#include <Hsm.h>
#include <SingleThreadedExecutionPolicy.h>

using tsm::Event;
using tsm::Hsm;
using tsm::SingleThreadedExecutionPolicy;
using tsm::State;

struct SocketSM : tsm::Hsm<SocketSM>
{
  public:
    SocketSM()
      : tsm::Hsm<SocketSM>()
    {
        IHsm::setStartState(&Closed);

        add(Closed, sock_open, Ready);
        add(Ready, connect, Open);
        add(Ready, bind, Bound);
        add(Bound, listen, Listening);
        add(Listening, accept, Listening);
        add(Listening, close, Closed);
        add(Open, close, Closed);
    }

    // Events
    Event sock_open, bind, listen, connect, accept, close;

    // States
    State Closed, Ready, Bound, Open, Listening;
};

using SocketHsm = tsm::SingleThreadedExecutionPolicy<SocketSM>;

int
main()
{
    SocketHsm sm;

    // Important!!
    sm.startSM();

    // send events... and step the sm
    sm.sendEvent(sm.sock_open);
    sm.step();

    sm.stopSM();
}