Skip to content

FSM and Control Flow

The FSM is the board's traffic controller. It decides when the CAN-Gateway is initializing, waiting for fresh data, processing sensors, publishing outputs, or switching into bootloader or fault handling.

What it is for

The board has a lot of small jobs, but it should not try to do them all at once. The FSM keeps the runtime organized by making one mode own the current moment, then handing off cleanly to the next one.

The gateway version of that logic is in board_fsm.c. It builds a tiny event snapshot and then chooses the next mode from the current state, the bootloader request flag, and the fault latch.

c
if (FSM_is_fault_latched(&board_fsm_driver))
{
	return BOARD_FSM_MODE_FAULT;
}

if (board_events->force_fault != 0U)
{
	return BOARD_FSM_MODE_FAULT;
}

if (board_events->bootloader_requested != 0U)
{
	return BOARD_FSM_MODE_BOOTLOADER;
}

switch ((Board_FSM_Mode_t)current_mode)
{
	case BOARD_FSM_MODE_INIT:
		return BOARD_FSM_MODE_IDLE;

	case BOARD_FSM_MODE_IDLE:
		if (board_events->adc_ready != 0U)
		{
			return BOARD_FSM_MODE_PROCESS_SENSORS;
		}
		return BOARD_FSM_MODE_IDLE;

	case BOARD_FSM_MODE_PROCESS_SENSORS:
		return BOARD_FSM_MODE_OUTPUT_SENSORS;

	case BOARD_FSM_MODE_OUTPUT_SENSORS:
		return BOARD_FSM_MODE_IDLE;

	case BOARD_FSM_MODE_BOOTLOADER:
		return BOARD_FSM_MODE_BOOTLOADER;

	case BOARD_FSM_MODE_FAULT:
	default:
		return BOARD_FSM_MODE_FAULT;
}

That is the important behavior: fault and bootloader requests win immediately, then the normal loop alternates between idle, processing, and output.

NOTE

This is a board FSM, not a reusable app framework. It is tightly coupled to the CAN-Gateway's sensor flow, CAN traffic, and bootloader path.

Pages in this section

Why it matters

The FSM is what keeps the board understandable under pressure. If a bootloader request arrives, it can take over. If sensor data is not ready yet, the board stays put. If everything is healthy, it moves through the normal process and output stages.

That makes the page worth reading even before you inspect the source files: it tells you the board's runtime story in a single pass.

Released under the MIT License.