Skip to content

Commit ad2eee4

Browse files
committed
Improve HSM and FSM documentation
1 parent a643bc4 commit ad2eee4

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [0-based versioning](https://0ver.org/).
2121
- Relocate compiler independent macros to `common/macros.h`.
2222
- Allow configuration set to NULL in `am_event_state_ctor()`.
2323
- Allow configuration set to NULL in `am_ao_state_ctor()`.
24+
- Improve HSM and FSM documentation.
2425

2526
### Added
2627

libs/fsm/README.rst

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ Glossary
99
a unique ID plus optionally some data associated with it
1010

1111
*entry event*
12-
an event sent to a state when the state is entered (**AM_EVT_FSM_ENTRY**)
12+
an event sent to a state when the state is entered.
13+
The event has ID :cpp:enumerator:`AM_EVT_FSM_ENTRY <am_fsm_evt_id::AM_EVT_FSM_ENTRY>`.
1314

1415
*exit event*
15-
an event sent to a state when the state is exited (**AM_EVT_FSM_EXIT**)
16+
an event sent to a state when the state is exited.
17+
The event has ID :cpp:enumerator:`AM_EVT_FSM_EXIT <am_fsm_evt_id::AM_EVT_FSM_EXIT>`.
1618

1719
*state*
1820
an event handler. `Idle`, `Processing` and `Completed` are all states
@@ -45,7 +47,7 @@ This library is ideal for applications that require structured and manageable
4547
state control in an embedded system or other C-based environments.
4648

4749
The FSM is a combination of one or more state-handler functions of
48-
type **am_fsm_state**.
50+
type :cpp:type:`am_fsm_state_fn`.
4951

5052
Example FSM
5153
============
@@ -85,19 +87,19 @@ processes it accordingly.
8587

8688
FSM reserved events are defined as follows:
8789

88-
- **AM_EVT_FSM_ENTRY**: Indicates the entry into a state. Entry actions are executed here.
89-
- **AM_EVT_FSM_EXIT**: Indicates the exit from a state. Exit actions are executed here.
90+
- :cpp:enumerator:`AM_EVT_FSM_ENTRY <am_fsm_evt_id::AM_EVT_FSM_ENTRY>`: Indicates the entry into a state. Entry actions are executed here.
91+
- :cpp:enumerator:`AM_EVT_FSM_EXIT <am_fsm_evt_id::AM_EVT_FSM_EXIT>`: Indicates the exit from a state. Exit actions are executed here.
9092

91-
The **am_fsm_dispatch()** function is used to send events to the FSM.
93+
The :cpp:func:`am_fsm_dispatch()` function is used to send events to the FSM.
9294

9395
State Transition
9496
================
9597

9698
The library supports two main types of state transitions:
9799

98-
1. Standard Transition (**AM_FSM_TRAN()**):
100+
1. Standard Transition (:c:macro:`AM_FSM_TRAN()`):
99101
Moves directly from the current state to the new state.
100-
2. Redispatch Transition (**AM_FSM_TRAN_REDISPATCH()**):
102+
2. Redispatch Transition (:c:macro:`AM_FSM_TRAN_REDISPATCH()`):
101103
Transitions to a new state and redispatches the event for further processing.
102104

103105
Both type of state transitions are used within state handlers to initiate
@@ -115,12 +117,12 @@ Initial State
115117
=============
116118

117119
The initial state of the FSM is provided during the FSM’s construction
118-
using the **am_fsm_ctor()** function.
120+
using the :cpp:func:`am_fsm_ctor()` function.
119121

120122
This state is set to handle any initial setup required by the FSM and
121123
ensures that the FSM begins with a predictable configuration.
122124

123-
The function **am_fsm_init()** initiates the FSM with an optional initial event.
125+
The function :cpp:func:`am_fsm_init()` initiates the FSM with an optional initial event.
124126

125127
Example:
126128

@@ -136,12 +138,12 @@ to proceed to the appropriate active state.
136138
FSM Coding Rules
137139
================
138140

139-
1. FSM states must be represented by event handlers of type **am_fsm_state_fn**.
141+
1. FSM states must be represented by event handlers of type :cpp:type:`am_fsm_state_fn`.
140142
2. The name of the first argument of all user event handler functions
141143
must be **me**.
142144
3. For convenience instead of using **struct am_fsm *me** the first argument
143145
can point to a user structure. In this case the user structure
144-
must have **struct am_fsm** instance as its first field.
146+
must have **struct** :cpp:struct:`am_fsm` instance as its first field.
145147
For example, the first argument can be **struct foo *me**, where
146148
**struct foo** is defined like this:
147149

@@ -156,19 +158,23 @@ FSM Coding Rules
156158
events.
157159
5. Avoid placing any code with side effects outside of the switch-case of
158160
event handlers.
159-
6. Processing of **AM_EVT_FSM_ENTRY** and **AM_EVT_FSM_EXIT** events should
161+
6. Processing of :cpp:enumerator:`AM_EVT_FSM_ENTRY <am_fsm_evt_id::AM_EVT_FSM_ENTRY>` and
162+
:cpp:enumerator:`AM_EVT_FSM_EXIT <am_fsm_evt_id::AM_EVT_FSM_EXIT>` events should
160163
not trigger state transitions. It means that user event handlers should
161-
not return **AM_FSM_TRAN()** or **AM_FSM_TRAN_REDISPATCH()** for
164+
not return :c:macro:`AM_FSM_TRAN()` or :c:macro:`AM_FSM_TRAN_REDISPATCH()` for
162165
these events.
166+
7. Processing of :cpp:enumerator:`AM_EVT_FSM_ENTRY <am_fsm_evt_id::AM_EVT_FSM_ENTRY>` and
167+
:cpp:enumerator:`AM_EVT_FSM_EXIT <am_fsm_evt_id::AM_EVT_FSM_EXIT>` events should be
168+
done at the top of the corresponding event handler for better readability.
163169

164170
FSM Initialization
165171
==================
166172

167173
FSM initialization is divided into the following two steps for increased
168174
flexibility and better control of the initialization timeline:
169175

170-
1. the state machine constructor (**am_fsm_ctor()**)
171-
2. the initial transition (**am_fsm_init()**).
176+
1. the state machine constructor (:cpp:func:`am_fsm_ctor()`)
177+
2. the initial transition (:cpp:func:`am_fsm_init()`).
172178

173179
Transition To History
174180
=====================
@@ -204,7 +210,7 @@ happen from state *A* or state *B*. As an example, assume the the FSM
204210
is in the state *A*.
205211

206212
The user code stores the current state in a local variable of type
207-
**am_fsm_state_fn**. This is done with:
213+
:cpp:type:`am_fsm_state_fn`. This is done with:
208214

209215
.. code-block:: C
210216

libs/hsm/README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ HSM Coding Rules
363363
event can optionally only trigger transition to a substate of the state triggering
364364
the transition.
365365
Transition to peer states of superstates is not allowed in this case.
366+
9. Processing of :cpp:enumerator:`AM_EVT_HSM_INIT <am_hsm_evt_id::AM_EVT_HSM_INIT>`,
367+
:cpp:enumerator:`AM_EVT_HSM_ENTRY <am_hsm_evt_id::AM_EVT_HSM_ENTRY>` and
368+
:cpp:enumerator:`AM_EVT_HSM_EXIT <am_hsm_evt_id::AM_EVT_HSM_EXIT>` events should be
369+
done at the top of the corresponding event handler for better readability.
366370

367371
Transition To History
368372
=====================

0 commit comments

Comments
 (0)